packer-cn/builder/openstack/step_run_source_server.go

145 lines
3.9 KiB
Go
Raw Normal View History

package openstack
import (
"context"
"fmt"
"io/ioutil"
"log"
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"
"github.com/hashicorp/packer/helper/multistep"
2017-04-04 16:39:01 -04:00
"github.com/hashicorp/packer/packer"
)
type StepRunSourceServer struct {
2015-06-12 11:10:10 -04:00
Name string
SourceImage string
SourceImageName string
2015-06-12 11:10:10 -04:00
SecurityGroups []string
Networks []string
AvailabilityZone string
UserData string
UserDataFile string
ConfigDrive bool
2017-01-04 21:15:16 -05:00
InstanceMetadata map[string]string
server *servers.Server
}
func (s *StepRunSourceServer) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
2015-06-12 00:16:43 -04:00
config := state.Get("config").(Config)
flavor := state.Get("flavor_id").(string)
2013-08-31 15:37:07 -04:00
ui := state.Get("ui").(packer.Ui)
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
}
2015-06-12 00:16:43 -04:00
networks := make([]servers.Network, len(s.Networks))
for i, networkUuid := range s.Networks {
2015-06-12 00:16:43 -04:00
networks[i].UUID = networkUuid
}
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
}
}
ui.Say("Launching server...")
2015-06-12 00:16:43 -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,
ServiceClient: computeClient,
2017-01-04 21:15:16 -05:00
Metadata: s.InstanceMetadata,
}
var serverOptsExt servers.CreateOptsBuilder
keyName, hasKey := state.GetOk("keyPair")
if hasKey {
serverOptsExt = keypairs.CreateOptsExt{
CreateOptsBuilder: serverOpts,
KeyName: keyName.(string),
}
} else {
serverOptsExt = serverOpts
}
s.server, err = servers.Create(computeClient, serverOptsExt).Extract()
if err != nil {
err := fmt.Errorf("Error launching source server: %s", err)
2013-08-31 15:37:07 -04:00
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
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)
ui.Say("Waiting for server to become ready...")
stateChange := StateChangeConf{
Pending: []string{"BUILD"},
Target: []string{"ACTIVE"},
2015-06-12 00:16:43 -04:00
Refresh: ServerStateRefreshFunc(computeClient, s.server),
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)
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)
return multistep.ActionContinue
}
2013-08-31 15:37:07 -04:00
func (s *StepRunSourceServer) Cleanup(state multistep.StateBag) {
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)
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
}
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 {
ui.Error(fmt.Sprintf("Error terminating server, may still be around: %s", err))
return
}
stateChange := StateChangeConf{
Pending: []string{"ACTIVE", "BUILD", "REBUILD", "SUSPENDED", "SHUTOFF", "STOPPED"},
2015-06-12 00:16:43 -04:00
Refresh: ServerStateRefreshFunc(computeClient, s.server),
Target: []string{"DELETED"},
}
WaitForState(&stateChange)
}