2013-08-27 00:57:23 -04:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2014-10-27 19:40:49 -04:00
|
|
|
|
2015-06-12 00:16:43 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"github.com/rackspace/gophercloud/openstack/compute/v2/extensions/keypairs"
|
|
|
|
"github.com/rackspace/gophercloud/openstack/compute/v2/servers"
|
2013-08-27 00:57:23 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type StepRunSourceServer struct {
|
2015-06-12 11:10:10 -04:00
|
|
|
Name string
|
|
|
|
SourceImage string
|
|
|
|
SecurityGroups []string
|
|
|
|
Networks []string
|
|
|
|
AvailabilityZone string
|
2013-08-27 00:57:23 -04:00
|
|
|
|
2015-06-12 00:16:43 -04:00
|
|
|
server *servers.Server
|
2013-08-27 00:57:23 -04:00
|
|
|
}
|
|
|
|
|
2013-08-31 15:37:07 -04:00
|
|
|
func (s *StepRunSourceServer) Run(state multistep.StateBag) multistep.StepAction {
|
2015-06-12 00:16:43 -04:00
|
|
|
config := state.Get("config").(Config)
|
2015-06-12 10:32:31 -04:00
|
|
|
flavor := state.Get("flavor_id").(string)
|
2013-08-31 15:37:07 -04:00
|
|
|
keyName := state.Get("keyPair").(string)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-08-27 00:57:23 -04:00
|
|
|
|
2015-06-12 00:16:43 -04:00
|
|
|
// We need the v2 compute client
|
|
|
|
computeClient, err := config.computeV2Client()
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Error initializing compute client: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
2014-01-17 18:46:35 -05:00
|
|
|
}
|
|
|
|
|
2015-06-12 00:16:43 -04:00
|
|
|
networks := make([]servers.Network, len(s.Networks))
|
2014-06-15 14:56:19 -04:00
|
|
|
for i, networkUuid := range s.Networks {
|
2015-06-12 00:16:43 -04:00
|
|
|
networks[i].UUID = networkUuid
|
2014-06-15 14:56:19 -04:00
|
|
|
}
|
|
|
|
|
2015-06-12 10:32:31 -04:00
|
|
|
ui.Say("Launching server...")
|
2015-06-12 00:16:43 -04:00
|
|
|
s.server, err = servers.Create(computeClient, keypairs.CreateOptsExt{
|
|
|
|
CreateOptsBuilder: servers.CreateOpts{
|
2015-06-12 11:10:10 -04:00
|
|
|
Name: s.Name,
|
|
|
|
ImageRef: s.SourceImage,
|
|
|
|
FlavorRef: flavor,
|
|
|
|
SecurityGroups: s.SecurityGroups,
|
|
|
|
Networks: networks,
|
|
|
|
AvailabilityZone: s.AvailabilityZone,
|
2015-06-12 00:16:43 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
KeyName: keyName,
|
|
|
|
}).Extract()
|
2013-08-27 00:57:23 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-06-12 10:32:31 -04:00
|
|
|
ui.Message(fmt.Sprintf("Server ID: %s", s.server.ID))
|
2015-06-12 00:16:43 -04:00
|
|
|
log.Printf("server id: %s", s.server.ID)
|
2013-08-27 00:57:23 -04:00
|
|
|
|
2015-06-12 10:32:31 -04:00
|
|
|
ui.Say("Waiting for server to become ready...")
|
2013-08-27 00:57:23 -04:00
|
|
|
stateChange := StateChangeConf{
|
|
|
|
Pending: []string{"BUILD"},
|
|
|
|
Target: "ACTIVE",
|
2015-06-12 00:16:43 -04:00
|
|
|
Refresh: ServerStateRefreshFunc(computeClient, s.server),
|
2013-08-27 00:57:23 -04:00
|
|
|
StepState: state,
|
|
|
|
}
|
|
|
|
latestServer, err := WaitForState(&stateChange)
|
|
|
|
if err != nil {
|
2015-06-12 00:16:43 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-06-12 00:16:43 -04:00
|
|
|
s.server = latestServer.(*servers.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
|
|
|
|
}
|
|
|
|
|
2015-06-12 00:16:43 -04:00
|
|
|
config := state.Get("config").(Config)
|
2013-08-31 15:37:07 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-08-27 00:57:23 -04:00
|
|
|
|
2015-06-12 00:16:43 -04:00
|
|
|
// We need the v2 compute client
|
|
|
|
computeClient, err := config.computeV2Client()
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error terminating server, may still be around: %s", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-08-27 00:57:23 -04:00
|
|
|
ui.Say("Terminating the source server...")
|
2015-06-12 00:16:43 -04:00
|
|
|
if err := servers.Delete(computeClient, s.server.ID).ExtractErr(); err != nil {
|
2013-08-27 00:57:23 -04:00
|
|
|
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"},
|
2015-06-12 00:16:43 -04:00
|
|
|
Refresh: ServerStateRefreshFunc(computeClient, s.server),
|
2013-08-28 01:08:18 -04:00
|
|
|
Target: "DELETED",
|
2013-08-27 00:57:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
WaitForState(&stateChange)
|
|
|
|
}
|