2013-08-27 00:57:23 -04:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"log"
|
2014-10-27 19:40:49 -04:00
|
|
|
|
|
|
|
"github.com/mitchellh/gophercloud-fork-40444fb"
|
2013-08-27 00:57:23 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type StepRunSourceServer struct {
|
2014-01-17 18:46:35 -05:00
|
|
|
Flavor string
|
|
|
|
Name string
|
|
|
|
SourceImage string
|
|
|
|
SecurityGroups []string
|
2014-06-15 14:56:19 -04:00
|
|
|
Networks []string
|
2013-08-27 00:57:23 -04:00
|
|
|
|
|
|
|
server *gophercloud.Server
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:37:07 -04:00
|
|
|
func (s *StepRunSourceServer) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
csp := state.Get("csp").(gophercloud.CloudServersProvider)
|
|
|
|
keyName := state.Get("keyPair").(string)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-08-27 00:57:23 -04:00
|
|
|
|
|
|
|
// XXX - validate image and flavor is available
|
|
|
|
|
2014-01-17 18:46:35 -05:00
|
|
|
securityGroups := make([]map[string]interface{}, len(s.SecurityGroups))
|
|
|
|
for i, groupName := range s.SecurityGroups {
|
|
|
|
securityGroups[i] = make(map[string]interface{})
|
|
|
|
securityGroups[i]["name"] = groupName
|
|
|
|
}
|
|
|
|
|
2014-06-15 14:56:19 -04:00
|
|
|
networks := make([]gophercloud.NetworkConfig, len(s.Networks))
|
|
|
|
for i, networkUuid := range s.Networks {
|
|
|
|
networks[i].Uuid = networkUuid
|
|
|
|
}
|
|
|
|
|
2013-08-27 00:57:23 -04:00
|
|
|
server := gophercloud.NewServer{
|
2014-01-17 18:46:35 -05:00
|
|
|
Name: s.Name,
|
|
|
|
ImageRef: s.SourceImage,
|
|
|
|
FlavorRef: s.Flavor,
|
|
|
|
KeyPairName: keyName,
|
|
|
|
SecurityGroup: securityGroups,
|
2014-06-15 14:56:19 -04:00
|
|
|
Networks: networks,
|
2013-08-27 00:57:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
serverResp, err := csp.CreateServer(server)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error launching source server: %s", err)
|
2013-08-31 15:37:07 -04:00
|
|
|
state.Put("error", err)
|
2013-08-27 00:57:23 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
s.server, err = csp.ServerById(serverResp.Id)
|
|
|
|
log.Printf("server id: %s", s.server.Id)
|
|
|
|
|
|
|
|
ui.Say(fmt.Sprintf("Waiting for server (%s) to become ready...", s.server.Id))
|
|
|
|
stateChange := StateChangeConf{
|
|
|
|
Pending: []string{"BUILD"},
|
|
|
|
Target: "ACTIVE",
|
2013-08-28 01:08:18 -04:00
|
|
|
Refresh: ServerStateRefreshFunc(csp, s.server),
|
2013-08-27 00:57:23 -04:00
|
|
|
StepState: state,
|
|
|
|
}
|
|
|
|
latestServer, err := WaitForState(&stateChange)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error waiting for server (%s) to become ready: %s", s.server.Id, err)
|
2013-08-31 15:37:07 -04:00
|
|
|
state.Put("error", err)
|
2013-08-27 00:57:23 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
s.server = latestServer.(*gophercloud.Server)
|
2013-08-31 15:37:07 -04:00
|
|
|
state.Put("server", s.server)
|
2013-08-27 00:57:23 -04:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:37:07 -04:00
|
|
|
func (s *StepRunSourceServer) Cleanup(state multistep.StateBag) {
|
2013-08-27 00:57:23 -04:00
|
|
|
if s.server == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:37:07 -04:00
|
|
|
csp := state.Get("csp").(gophercloud.CloudServersProvider)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-08-27 00:57:23 -04:00
|
|
|
|
|
|
|
ui.Say("Terminating the source server...")
|
|
|
|
if err := csp.DeleteServerById(s.server.Id); err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error terminating server, may still be around: %s", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
stateChange := StateChangeConf{
|
2013-08-28 01:08:18 -04:00
|
|
|
Pending: []string{"ACTIVE", "BUILD", "REBUILD", "SUSPENDED"},
|
|
|
|
Refresh: ServerStateRefreshFunc(csp, s.server),
|
|
|
|
Target: "DELETED",
|
2013-08-27 00:57:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
WaitForState(&stateChange)
|
|
|
|
}
|