2013-08-27 00:57:23 -04:00
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-08-27 00:57:23 -04:00
|
|
|
"fmt"
|
2015-06-12 22:55:39 -04:00
|
|
|
"io/ioutil"
|
2013-08-27 00:57:23 -04:00
|
|
|
"log"
|
2014-10-27 19:40:49 -04:00
|
|
|
|
2018-05-24 06:20:22 -04:00
|
|
|
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/bootfromvolume"
|
2016-11-27 19:59:26 -05:00
|
|
|
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/keypairs"
|
|
|
|
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-08-27 00:57:23 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type StepRunSourceServer struct {
|
2018-05-24 06:20:22 -04:00
|
|
|
Name string
|
|
|
|
SourceImage string
|
|
|
|
SourceImageName string
|
|
|
|
SecurityGroups []string
|
|
|
|
Networks []string
|
|
|
|
Ports []string
|
|
|
|
AvailabilityZone string
|
|
|
|
UserData string
|
|
|
|
UserDataFile string
|
|
|
|
ConfigDrive bool
|
|
|
|
InstanceMetadata map[string]string
|
|
|
|
UseBlockStorageVolume bool
|
|
|
|
server *servers.Server
|
2013-08-27 00:57:23 -04:00
|
|
|
}
|
|
|
|
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s *StepRunSourceServer) Run(_ context.Context, 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
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-08-03 17:32:34 -04:00
|
|
|
networks := make([]servers.Network, len(s.Networks)+len(s.Ports))
|
2018-08-03 16:46:36 -04:00
|
|
|
i := 0
|
2018-08-15 07:12:47 -04:00
|
|
|
for ; i < len(s.Ports); i++ {
|
|
|
|
networks[i].Port = s.Ports[i]
|
2018-08-03 16:46:36 -04:00
|
|
|
}
|
2018-08-15 07:12:47 -04:00
|
|
|
for ; i < len(networks); i++ {
|
|
|
|
networks[i].UUID = s.Networks[i]
|
2014-06-15 14:56:19 -04:00
|
|
|
}
|
|
|
|
|
2015-06-12 22:55:39 -04:00
|
|
|
userData := []byte(s.UserData)
|
|
|
|
if s.UserDataFile != "" {
|
|
|
|
userData, err = ioutil.ReadFile(s.UserDataFile)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("Error reading user data file: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-12 10:32:31 -04:00
|
|
|
ui.Say("Launching server...")
|
2015-06-12 00:16:43 -04:00
|
|
|
|
2016-10-08 17:18:19 -04:00
|
|
|
serverOpts := servers.CreateOpts{
|
|
|
|
Name: s.Name,
|
|
|
|
ImageRef: s.SourceImage,
|
|
|
|
ImageName: s.SourceImageName,
|
|
|
|
FlavorRef: flavor,
|
|
|
|
SecurityGroups: s.SecurityGroups,
|
|
|
|
Networks: networks,
|
|
|
|
AvailabilityZone: s.AvailabilityZone,
|
|
|
|
UserData: userData,
|
2016-11-27 19:59:26 -05:00
|
|
|
ConfigDrive: &s.ConfigDrive,
|
2016-12-19 16:47:09 -05:00
|
|
|
ServiceClient: computeClient,
|
2017-01-04 21:15:16 -05:00
|
|
|
Metadata: s.InstanceMetadata,
|
2016-10-08 17:18:19 -04:00
|
|
|
}
|
2018-07-14 00:25:26 -04:00
|
|
|
|
|
|
|
// check if image filter returned a source image ID and replace
|
2018-08-22 07:37:43 -04:00
|
|
|
if imageID, ok := state.GetOk("source_image"); ok {
|
|
|
|
serverOpts.ImageRef = imageID.(string)
|
2018-07-14 00:25:26 -04:00
|
|
|
}
|
|
|
|
|
2016-10-08 17:18:19 -04:00
|
|
|
var serverOptsExt servers.CreateOptsBuilder
|
2018-05-24 06:20:22 -04:00
|
|
|
|
|
|
|
// Create root volume in the Block Storage service if required.
|
|
|
|
// Add block device mapping v2 to the server create options if required.
|
|
|
|
if s.UseBlockStorageVolume {
|
|
|
|
volume := state.Get("volume_id").(string)
|
|
|
|
blockDeviceMappingV2 := []bootfromvolume.BlockDevice{
|
2018-05-24 06:49:33 -04:00
|
|
|
{
|
2018-05-24 06:20:22 -04:00
|
|
|
BootIndex: 0,
|
|
|
|
DestinationType: bootfromvolume.DestinationVolume,
|
|
|
|
SourceType: bootfromvolume.SourceVolume,
|
|
|
|
UUID: volume,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
// ImageRef and block device mapping is an invalid options combination.
|
|
|
|
serverOpts.ImageRef = ""
|
|
|
|
serverOptsExt = bootfromvolume.CreateOptsExt{
|
|
|
|
CreateOptsBuilder: serverOpts,
|
|
|
|
BlockDevice: blockDeviceMappingV2,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
serverOptsExt = serverOpts
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add keypair to the server create options.
|
2018-08-27 09:58:00 -04:00
|
|
|
keyName := config.Comm.SSHKeyPair
|
|
|
|
if keyName != "" {
|
2016-10-08 17:18:19 -04:00
|
|
|
serverOptsExt = keypairs.CreateOptsExt{
|
2018-05-24 06:20:22 -04:00
|
|
|
CreateOptsBuilder: serverOptsExt,
|
2018-08-27 09:58:00 -04:00
|
|
|
KeyName: keyName,
|
2016-10-08 17:18:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-24 06:20:22 -04:00
|
|
|
ui.Say("Launching server...")
|
2016-10-08 17:18:19 -04:00
|
|
|
s.server, err = servers.Create(computeClient, serverOptsExt).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"},
|
2015-06-12 22:50:59 -04:00
|
|
|
Target: []string{"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
|
|
|
|
}
|
|
|
|
|
2015-12-17 14:12:52 -05:00
|
|
|
ui.Say(fmt.Sprintf("Terminating the source server: %s ...", s.server.ID))
|
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{
|
2015-06-12 22:50:59 -04:00
|
|
|
Pending: []string{"ACTIVE", "BUILD", "REBUILD", "SUSPENDED", "SHUTOFF", "STOPPED"},
|
2015-06-12 00:16:43 -04:00
|
|
|
Refresh: ServerStateRefreshFunc(computeClient, s.server),
|
2015-06-12 22:50:59 -04:00
|
|
|
Target: []string{"DELETED"},
|
2013-08-27 00:57:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
WaitForState(&stateChange)
|
|
|
|
}
|