From 26cd27dc7cb7ea2dd8bb3d3afbaf033ae4da385c Mon Sep 17 00:00:00 2001 From: Rickard von Essen Date: Wed, 26 Jul 2017 21:34:11 +0200 Subject: [PATCH] cloudstack: Updated after review --- builder/cloudstack/builder.go | 14 ++++----- builder/cloudstack/config.go | 47 +++++++++++++++--------------- builder/cloudstack/step_keypair.go | 42 +++++++++++--------------- 3 files changed, 47 insertions(+), 56 deletions(-) diff --git a/builder/cloudstack/builder.go b/builder/cloudstack/builder.go index 528e34a01..c4ad8a5b2 100644 --- a/builder/cloudstack/builder.go +++ b/builder/cloudstack/builder.go @@ -64,16 +64,16 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe HTTPPortMax: b.config.HTTPPortMax, }, &stepKeypair{ - Debug: b.config.PackerDebug, - DebugKeyPath: fmt.Sprintf("cs_%s.pem", b.config.PackerBuildName), - SSHAgentAuth: b.config.Comm.SSHAgentAuth, - TemporaryKeyPair: b.config.TemporaryKeypair, - KeyPair: b.config.Keypair, - PrivateKeyFile: b.config.Comm.SSHPrivateKey, + Debug: b.config.PackerDebug, + DebugKeyPath: fmt.Sprintf("cs_%s.pem", b.config.PackerBuildName), + KeyPair: b.config.Keypair, + PrivateKeyFile: b.config.Comm.SSHPrivateKey, + SSHAgentAuth: b.config.Comm.SSHAgentAuth, + TemporaryKeyPairName: b.config.TemporaryKeypairName, }, &stepCreateInstance{ - Debug: b.config.PackerDebug, Ctx: b.config.ctx, + Debug: b.config.PackerDebug, }, &stepSetupNetworking{}, &communicator.StepConnect{ diff --git a/builder/cloudstack/config.go b/builder/cloudstack/config.go index 8571a0e03..513ad8eed 100644 --- a/builder/cloudstack/config.go +++ b/builder/cloudstack/config.go @@ -27,24 +27,24 @@ type Config struct { HTTPGetOnly bool `mapstructure:"http_get_only"` SSLNoVerify bool `mapstructure:"ssl_no_verify"` - CIDRList []string `mapstructure:"cidr_list"` - DiskOffering string `mapstructure:"disk_offering"` - DiskSize int64 `mapstructure:"disk_size"` - Expunge bool `mapstructure:"expunge"` - Hypervisor string `mapstructure:"hypervisor"` - InstanceName string `mapstructure:"instance_name"` - Keypair string `mapstructure:"keypair"` - TemporaryKeypair string `mapstructure:"temporary_keypair"` - Network string `mapstructure:"network"` - Project string `mapstructure:"project"` - PublicIPAddress string `mapstructure:"public_ip_address"` - ServiceOffering string `mapstructure:"service_offering"` - SourceTemplate string `mapstructure:"source_template"` - SourceISO string `mapstructure:"source_iso"` - UserData string `mapstructure:"user_data"` - UserDataFile string `mapstructure:"user_data_file"` - UseLocalIPAddress bool `mapstructure:"use_local_ip_address"` - Zone string `mapstructure:"zone"` + CIDRList []string `mapstructure:"cidr_list"` + DiskOffering string `mapstructure:"disk_offering"` + DiskSize int64 `mapstructure:"disk_size"` + Expunge bool `mapstructure:"expunge"` + Hypervisor string `mapstructure:"hypervisor"` + InstanceName string `mapstructure:"instance_name"` + Keypair string `mapstructure:"keypair"` + TemporaryKeypairName string `mapstructure:"temporary_keypair_name"` + Network string `mapstructure:"network"` + Project string `mapstructure:"project"` + PublicIPAddress string `mapstructure:"public_ip_address"` + ServiceOffering string `mapstructure:"service_offering"` + SourceTemplate string `mapstructure:"source_template"` + SourceISO string `mapstructure:"source_iso"` + UserData string `mapstructure:"user_data"` + UserDataFile string `mapstructure:"user_data_file"` + UseLocalIPAddress bool `mapstructure:"use_local_ip_address"` + Zone string `mapstructure:"zone"` TemplateName string `mapstructure:"template_name"` TemplateDisplayText string `mapstructure:"template_display_text"` @@ -121,13 +121,12 @@ func NewConfig(raws ...interface{}) (*Config, error) { c.TemplateDisplayText = c.TemplateName } - // If we are not given an explicit keypair or ssh_private_key_file, then create - // a temporary one, but only if the temporary_keypair has not been provided and - // we are not using ssh_password. - if c.Keypair == "" && c.TemporaryKeypair == "" && + // If we are not given an explicit keypair, ssh_password or ssh_private_key_file, + // then create a temporary one, but only if the temporary_keypair_name has not + // been provided. + if c.Keypair == "" && c.TemporaryKeypairName == "" && c.Comm.SSHPrivateKey == "" && c.Comm.SSHPassword == "" { - - c.TemporaryKeypair = fmt.Sprintf("packer_%s", uuid.TimeOrderedUUID()) + c.TemporaryKeypairName = fmt.Sprintf("packer_%s", uuid.TimeOrderedUUID()) } // Process required parameters. diff --git a/builder/cloudstack/step_keypair.go b/builder/cloudstack/step_keypair.go index 41e180fc0..675994fc1 100644 --- a/builder/cloudstack/step_keypair.go +++ b/builder/cloudstack/step_keypair.go @@ -12,14 +12,12 @@ import ( ) type stepKeypair struct { - Debug bool - SSHAgentAuth bool - DebugKeyPath string - TemporaryKeyPair string - KeyPair string - PrivateKeyFile string - - doCleanup bool + Debug bool + DebugKeyPath string + KeyPair string + PrivateKeyFile string + SSHAgentAuth bool + TemporaryKeyPairName string } 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 } - if s.TemporaryKeyPair == "" { - ui.Say("Not using temporary keypair") + if s.TemporaryKeyPairName == "" { + ui.Say("Not using a keypair") state.Put("keypair", "") return multistep.ActionContinue } client := state.Get("client").(*cloudstack.CloudStackClient) - ui.Say(fmt.Sprintf("Creating temporary keypair: %s ...", s.TemporaryKeyPair)) - p := client.SSH.NewCreateSSHKeyPairParams( - s.TemporaryKeyPair, - ) + ui.Say(fmt.Sprintf("Creating temporary keypair: %s ...", s.TemporaryKeyPairName)) + p := client.SSH.NewCreateSSHKeyPairParams(s.TemporaryKeyPairName) keypair, err := client.SSH.CreateSSHKeyPair(p) if err != nil { 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 } - 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 - // directory. + // If we're in debug mode, output the private key to the working directory. if s.Debug { ui.Message(fmt.Sprintf("Saving key for debug purposes: %s", 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 - state.Put("keypair", s.TemporaryKeyPair) + state.Put("keypair", s.TemporaryKeyPairName) state.Put("privateKey", keypair.Privatekey) return multistep.ActionContinue } func (s *stepKeypair) Cleanup(state multistep.StateBag) { - if !s.doCleanup { + if s.TemporaryKeyPairName == "" { return } ui := state.Get("ui").(packer.Ui) 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( - s.TemporaryKeyPair, + s.TemporaryKeyPairName, )) if err != nil { ui.Error(err.Error()) 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)) } }