Files

174 lines
5.1 KiB
Go
Raw Permalink Normal View History

2013-08-26 21:57:23 -07:00
package openstack
import (
2018-01-22 15:32:33 -08:00
"context"
2013-08-26 21:57:23 -07:00
"fmt"
"io/ioutil"
2013-08-26 21:57:23 -07:00
"log"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/bootfromvolume"
2016-11-28 13:59:26 +13:00
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/keypairs"
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
2020-12-17 13:29:25 -08:00
"github.com/hashicorp/packer-plugin-sdk/multistep"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
2013-08-26 21:57:23 -07:00
)
type StepRunSourceServer struct {
Name string
SecurityGroups []string
AvailabilityZone string
UserData string
UserDataFile string
ConfigDrive bool
InstanceMetadata map[string]string
UseBlockStorageVolume bool
2019-03-11 18:39:47 +00:00
ForceDelete bool
server *servers.Server
2013-08-26 21:57:23 -07:00
}
func (s *StepRunSourceServer) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
config := state.Get("config").(*Config)
2015-06-12 10:32:31 -04:00
flavor := state.Get("flavor_id").(string)
sourceImage := state.Get("source_image").(string)
networks := state.Get("networks").([]servers.Network)
ui := state.Get("ui").(packersdk.Ui)
2013-08-26 21:57:23 -07: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 15:46:35 -08: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
serverOpts := servers.CreateOpts{
Name: s.Name,
ImageRef: sourceImage,
FlavorRef: flavor,
SecurityGroups: s.SecurityGroups,
Networks: networks,
AvailabilityZone: s.AvailabilityZone,
UserData: userData,
2016-11-28 13:59:26 +13:00
ConfigDrive: &s.ConfigDrive,
ServiceClient: computeClient,
2017-01-05 02:15:16 +00:00
Metadata: s.InstanceMetadata,
}
var serverOptsExt servers.CreateOptsBuilder
// 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{
{
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-09-18 16:22:17 +02:00
keyName := config.Comm.SSHKeyPairName
2018-08-27 15:58:00 +02:00
if keyName != "" {
serverOptsExt = keypairs.CreateOptsExt{
CreateOptsBuilder: serverOptsExt,
2018-08-27 15:58:00 +02:00
KeyName: keyName,
}
}
ui.Say("Launching server...")
s.server, err = servers.Create(computeClient, serverOptsExt).Extract()
2013-08-26 21:57:23 -07:00
if err != nil {
err := fmt.Errorf("Error launching source server: %s", err)
2013-08-31 12:37:07 -07:00
state.Put("error", err)
2013-08-26 21:57:23 -07: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-26 21:57:23 -07:00
2015-06-12 10:32:31 -04:00
ui.Say("Waiting for server to become ready...")
2013-08-26 21:57:23 -07: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-26 21:57:23 -07: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 12:37:07 -07:00
state.Put("error", err)
2013-08-26 21:57:23 -07:00
ui.Error(err.Error())
return multistep.ActionHalt
}
2015-06-12 00:16:43 -04:00
s.server = latestServer.(*servers.Server)
2013-08-31 12:37:07 -07:00
state.Put("server", s.server)
// instance_id is the generic term used so that users can have access to the
// instance id inside of the provisioners, used in step_provision.
state.Put("instance_id", s.server.ID)
2013-08-26 21:57:23 -07:00
return multistep.ActionContinue
}
2013-08-31 12:37:07 -07:00
func (s *StepRunSourceServer) Cleanup(state multistep.StateBag) {
2013-08-26 21:57:23 -07:00
if s.server == nil {
return
}
config := state.Get("config").(*Config)
ui := state.Get("ui").(packersdk.Ui)
2013-08-26 21:57:23 -07: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 11:12:52 -08:00
ui.Say(fmt.Sprintf("Terminating the source server: %s ...", s.server.ID))
2019-03-11 18:39:47 +00:00
if config.ForceDelete {
if err := servers.ForceDelete(computeClient, s.server.ID).ExtractErr(); err != nil {
ui.Error(fmt.Sprintf("Error terminating server, may still be around: %s", err))
return
}
} else {
if err := servers.Delete(computeClient, s.server.ID).ExtractErr(); err != nil {
ui.Error(fmt.Sprintf("Error terminating server, may still be around: %s", err))
return
}
2013-08-26 21:57:23 -07:00
}
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-26 21:57:23 -07:00
}
WaitForState(&stateChange)
}