cloudstack: Updated after review

This commit is contained in:
Rickard von Essen 2017-07-26 21:34:11 +02:00
parent 89dcc93f1c
commit 26cd27dc7c
No known key found for this signature in database
GPG Key ID: E0C0327388876CBA
3 changed files with 47 additions and 56 deletions

View File

@ -64,16 +64,16 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
HTTPPortMax: b.config.HTTPPortMax, HTTPPortMax: b.config.HTTPPortMax,
}, },
&stepKeypair{ &stepKeypair{
Debug: b.config.PackerDebug, Debug: b.config.PackerDebug,
DebugKeyPath: fmt.Sprintf("cs_%s.pem", b.config.PackerBuildName), DebugKeyPath: fmt.Sprintf("cs_%s.pem", b.config.PackerBuildName),
SSHAgentAuth: b.config.Comm.SSHAgentAuth, KeyPair: b.config.Keypair,
TemporaryKeyPair: b.config.TemporaryKeypair, PrivateKeyFile: b.config.Comm.SSHPrivateKey,
KeyPair: b.config.Keypair, SSHAgentAuth: b.config.Comm.SSHAgentAuth,
PrivateKeyFile: b.config.Comm.SSHPrivateKey, TemporaryKeyPairName: b.config.TemporaryKeypairName,
}, },
&stepCreateInstance{ &stepCreateInstance{
Debug: b.config.PackerDebug,
Ctx: b.config.ctx, Ctx: b.config.ctx,
Debug: b.config.PackerDebug,
}, },
&stepSetupNetworking{}, &stepSetupNetworking{},
&communicator.StepConnect{ &communicator.StepConnect{

View File

@ -27,24 +27,24 @@ type Config struct {
HTTPGetOnly bool `mapstructure:"http_get_only"` HTTPGetOnly bool `mapstructure:"http_get_only"`
SSLNoVerify bool `mapstructure:"ssl_no_verify"` SSLNoVerify bool `mapstructure:"ssl_no_verify"`
CIDRList []string `mapstructure:"cidr_list"` CIDRList []string `mapstructure:"cidr_list"`
DiskOffering string `mapstructure:"disk_offering"` DiskOffering string `mapstructure:"disk_offering"`
DiskSize int64 `mapstructure:"disk_size"` DiskSize int64 `mapstructure:"disk_size"`
Expunge bool `mapstructure:"expunge"` Expunge bool `mapstructure:"expunge"`
Hypervisor string `mapstructure:"hypervisor"` Hypervisor string `mapstructure:"hypervisor"`
InstanceName string `mapstructure:"instance_name"` InstanceName string `mapstructure:"instance_name"`
Keypair string `mapstructure:"keypair"` Keypair string `mapstructure:"keypair"`
TemporaryKeypair string `mapstructure:"temporary_keypair"` TemporaryKeypairName string `mapstructure:"temporary_keypair_name"`
Network string `mapstructure:"network"` Network string `mapstructure:"network"`
Project string `mapstructure:"project"` Project string `mapstructure:"project"`
PublicIPAddress string `mapstructure:"public_ip_address"` PublicIPAddress string `mapstructure:"public_ip_address"`
ServiceOffering string `mapstructure:"service_offering"` ServiceOffering string `mapstructure:"service_offering"`
SourceTemplate string `mapstructure:"source_template"` SourceTemplate string `mapstructure:"source_template"`
SourceISO string `mapstructure:"source_iso"` SourceISO string `mapstructure:"source_iso"`
UserData string `mapstructure:"user_data"` UserData string `mapstructure:"user_data"`
UserDataFile string `mapstructure:"user_data_file"` UserDataFile string `mapstructure:"user_data_file"`
UseLocalIPAddress bool `mapstructure:"use_local_ip_address"` UseLocalIPAddress bool `mapstructure:"use_local_ip_address"`
Zone string `mapstructure:"zone"` Zone string `mapstructure:"zone"`
TemplateName string `mapstructure:"template_name"` TemplateName string `mapstructure:"template_name"`
TemplateDisplayText string `mapstructure:"template_display_text"` TemplateDisplayText string `mapstructure:"template_display_text"`
@ -121,13 +121,12 @@ func NewConfig(raws ...interface{}) (*Config, error) {
c.TemplateDisplayText = c.TemplateName c.TemplateDisplayText = c.TemplateName
} }
// If we are not given an explicit keypair or ssh_private_key_file, then create // If we are not given an explicit keypair, ssh_password or ssh_private_key_file,
// a temporary one, but only if the temporary_keypair has not been provided and // then create a temporary one, but only if the temporary_keypair_name has not
// we are not using ssh_password. // been provided.
if c.Keypair == "" && c.TemporaryKeypair == "" && if c.Keypair == "" && c.TemporaryKeypairName == "" &&
c.Comm.SSHPrivateKey == "" && c.Comm.SSHPassword == "" { c.Comm.SSHPrivateKey == "" && c.Comm.SSHPassword == "" {
c.TemporaryKeypairName = fmt.Sprintf("packer_%s", uuid.TimeOrderedUUID())
c.TemporaryKeypair = fmt.Sprintf("packer_%s", uuid.TimeOrderedUUID())
} }
// Process required parameters. // Process required parameters.

View File

@ -12,14 +12,12 @@ import (
) )
type stepKeypair struct { type stepKeypair struct {
Debug bool Debug bool
SSHAgentAuth bool DebugKeyPath string
DebugKeyPath string KeyPair string
TemporaryKeyPair string PrivateKeyFile string
KeyPair string SSHAgentAuth bool
PrivateKeyFile string TemporaryKeyPairName string
doCleanup bool
} }
func (s *stepKeypair) Run(state multistep.StateBag) multistep.StepAction { func (s *stepKeypair) Run(state multistep.StateBag) multistep.StepAction {
@ -50,19 +48,17 @@ func (s *stepKeypair) Run(state multistep.StateBag) multistep.StepAction {
return multistep.ActionContinue return multistep.ActionContinue
} }
if s.TemporaryKeyPair == "" { if s.TemporaryKeyPairName == "" {
ui.Say("Not using temporary keypair") ui.Say("Not using a keypair")
state.Put("keypair", "") state.Put("keypair", "")
return multistep.ActionContinue return multistep.ActionContinue
} }
client := state.Get("client").(*cloudstack.CloudStackClient) client := state.Get("client").(*cloudstack.CloudStackClient)
ui.Say(fmt.Sprintf("Creating temporary keypair: %s ...", s.TemporaryKeyPair)) ui.Say(fmt.Sprintf("Creating temporary keypair: %s ...", s.TemporaryKeyPairName))
p := client.SSH.NewCreateSSHKeyPairParams(
s.TemporaryKeyPair,
)
p := client.SSH.NewCreateSSHKeyPairParams(s.TemporaryKeyPairName)
keypair, err := client.SSH.CreateSSHKeyPair(p) keypair, err := client.SSH.CreateSSHKeyPair(p)
if err != nil { if err != nil {
err := fmt.Errorf("Error creating temporary keypair: %s", err) err := fmt.Errorf("Error creating temporary keypair: %s", err)
@ -78,10 +74,9 @@ func (s *stepKeypair) Run(state multistep.StateBag) multistep.StepAction {
return multistep.ActionHalt return multistep.ActionHalt
} }
ui.Say(fmt.Sprintf("Created temporary keypair: %s", s.TemporaryKeyPair)) ui.Say(fmt.Sprintf("Created temporary keypair: %s", s.TemporaryKeyPairName))
// If we're in debug mode, output the private key to the working // If we're in debug mode, output the private key to the working directory.
// directory.
if s.Debug { if s.Debug {
ui.Message(fmt.Sprintf("Saving key for debug purposes: %s", s.DebugKeyPath)) ui.Message(fmt.Sprintf("Saving key for debug purposes: %s", s.DebugKeyPath))
f, err := os.Create(s.DebugKeyPath) f, err := os.Create(s.DebugKeyPath)
@ -110,32 +105,29 @@ func (s *stepKeypair) Run(state multistep.StateBag) multistep.StepAction {
} }
} }
// we created a temporary key, so remember to clean it up
s.doCleanup = true
// Set some state data for use in future steps // Set some state data for use in future steps
state.Put("keypair", s.TemporaryKeyPair) state.Put("keypair", s.TemporaryKeyPairName)
state.Put("privateKey", keypair.Privatekey) state.Put("privateKey", keypair.Privatekey)
return multistep.ActionContinue return multistep.ActionContinue
} }
func (s *stepKeypair) Cleanup(state multistep.StateBag) { func (s *stepKeypair) Cleanup(state multistep.StateBag) {
if !s.doCleanup { if s.TemporaryKeyPairName == "" {
return return
} }
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
client := state.Get("client").(*cloudstack.CloudStackClient) client := state.Get("client").(*cloudstack.CloudStackClient)
ui.Say(fmt.Sprintf("Deleting temporary keypair: %s ...", s.TemporaryKeyPair)) ui.Say(fmt.Sprintf("Deleting temporary keypair: %s ...", s.TemporaryKeyPairName))
_, err := client.SSH.DeleteSSHKeyPair(client.SSH.NewDeleteSSHKeyPairParams( _, err := client.SSH.DeleteSSHKeyPair(client.SSH.NewDeleteSSHKeyPairParams(
s.TemporaryKeyPair, s.TemporaryKeyPairName,
)) ))
if err != nil { if err != nil {
ui.Error(err.Error()) ui.Error(err.Error())
ui.Error(fmt.Sprintf( ui.Error(fmt.Sprintf(
"Error cleaning up keypair. Please delete the key manually: %s", s.TemporaryKeyPair)) "Error cleaning up keypair. Please delete the key manually: %s", s.TemporaryKeyPairName))
} }
} }