From cd851f8ac26e3b6d8101a35b6635b430d1bee3ee Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Wed, 29 Aug 2018 14:01:55 +0200 Subject: [PATCH] use public ssh key & key name from config instead of the statebag --- builder/oneandone/config.go | 1 - builder/oneandone/step_create_server.go | 8 ++------ builder/oracle/classic/step_add_keys.go | 14 +++++++------- builder/oracle/classic/step_create_instance.go | 3 +-- builder/oracle/common/step_ssh_key_pair.go | 3 +-- builder/oracle/oci/step_create_instance.go | 8 ++++---- 6 files changed, 15 insertions(+), 22 deletions(-) diff --git a/builder/oneandone/config.go b/builder/oneandone/config.go index f89453439..24df56fc8 100644 --- a/builder/oneandone/config.go +++ b/builder/oneandone/config.go @@ -20,7 +20,6 @@ type Config struct { Token string `mapstructure:"token"` Url string `mapstructure:"url"` - SSHKey string SnapshotName string `mapstructure:"image_name"` DataCenterName string `mapstructure:"data_center_name"` DataCenterId string diff --git a/builder/oneandone/step_create_server.go b/builder/oneandone/step_create_server.go index cffd8c440..9b848f92e 100644 --- a/builder/oneandone/step_create_server.go +++ b/builder/oneandone/step_create_server.go @@ -17,10 +17,6 @@ func (s *stepCreateServer) Run(_ context.Context, state multistep.StateBag) mult ui := state.Get("ui").(packer.Ui) c := state.Get("config").(*Config) - if sshkey, ok := state.GetOk("publicKey"); ok { - c.SSHKey = sshkey.(string) - } - token := oneandone.SetToken(c.Token) //Create an API client @@ -72,8 +68,8 @@ func (s *stepCreateServer) Run(_ context.Context, state multistep.StateBag) mult if c.Comm.SSHPassword != "" { req.Password = c.Comm.SSHPassword } - if c.SSHKey != "" { - req.SSHKey = c.SSHKey + if len(c.Comm.SSHPublicKey) != 0 { + req.SSHKey = string(c.Comm.SSHPublicKey) } server_id, server, err := api.CreateServer(&req) diff --git a/builder/oracle/classic/step_add_keys.go b/builder/oracle/classic/step_add_keys.go index b09310a1f..f1f14df80 100644 --- a/builder/oracle/classic/step_add_keys.go +++ b/builder/oracle/classic/step_add_keys.go @@ -1,9 +1,9 @@ package classic import ( + "bytes" "context" "fmt" - "strings" "github.com/hashicorp/go-oracle-terraform/compute" "github.com/hashicorp/packer/common/uuid" @@ -25,7 +25,7 @@ func (s *stepAddKeysToAPI) Run(_ context.Context, state multistep.StateBag) mult } // grab packer-generated key from statebag context. - sshPublicKey := strings.TrimSpace(state.Get("publicKey").(string)) + sshPublicKey := bytes.TrimSpace(config.Comm.SSHPublicKey) // form API call to add key to compute cloud sshKeyName := fmt.Sprintf("/Compute-%s/%s/packer_generated_key_%s", @@ -36,7 +36,7 @@ func (s *stepAddKeysToAPI) Run(_ context.Context, state multistep.StateBag) mult sshKeysClient := client.SSHKeys() sshKeysInput := compute.CreateSSHKeyInput{ Name: sshKeyName, - Key: sshPublicKey, + Key: string(sshPublicKey), Enabled: true, } @@ -48,20 +48,20 @@ func (s *stepAddKeysToAPI) Run(_ context.Context, state multistep.StateBag) mult state.Put("error", err) return multistep.ActionHalt } - state.Put("key_name", keyInfo.Name) + config.Comm.SSHKeyPairName = keyInfo.Name return multistep.ActionContinue } func (s *stepAddKeysToAPI) Cleanup(state multistep.StateBag) { // Delete the keys we created during this run - keyName, ok := state.GetOk("key_name") - if !ok { + config := state.Get("config").(*Config) + if len(config.Comm.SSHKeyPairName) == 0 { // No keys were generated; none need to be cleaned up. return } ui := state.Get("ui").(packer.Ui) ui.Say("Deleting SSH keys...") - deleteInput := compute.DeleteSSHKeyInput{Name: keyName.(string)} + deleteInput := compute.DeleteSSHKeyInput{Name: config.Comm.SSHKeyPairName} client := state.Get("client").(*compute.ComputeClient) deleteClient := client.SSHKeys() err := deleteClient.DeleteSSHKey(&deleteInput) diff --git a/builder/oracle/classic/step_create_instance.go b/builder/oracle/classic/step_create_instance.go index d52500e09..4d0646a8a 100644 --- a/builder/oracle/classic/step_create_instance.go +++ b/builder/oracle/classic/step_create_instance.go @@ -38,8 +38,7 @@ func (s *stepCreateInstance) Run(_ context.Context, state multistep.StateBag) mu Attributes: config.attribs, } if config.Comm.Type == "ssh" { - keyName := state.Get("key_name").(string) - input.SSHKeys = []string{keyName} + input.SSHKeys = []string{config.Comm.SSHKeyPairName} } instanceInfo, err := instanceClient.CreateInstance(input) diff --git a/builder/oracle/common/step_ssh_key_pair.go b/builder/oracle/common/step_ssh_key_pair.go index cd74be7d2..04e82b986 100644 --- a/builder/oracle/common/step_ssh_key_pair.go +++ b/builder/oracle/common/step_ssh_key_pair.go @@ -75,8 +75,7 @@ func (s *StepKeyPair) Run(_ context.Context, state multistep.StateBag) multistep return multistep.ActionHalt } - pubSSHFormat := string(ssh.MarshalAuthorizedKey(pub)) - state.Put("publicKey", pubSSHFormat) + s.Comm.SSHPublicKey = ssh.MarshalAuthorizedKey(pub) // If we're in debug mode, output the private key to the working // directory. diff --git a/builder/oracle/oci/step_create_instance.go b/builder/oracle/oci/step_create_instance.go index 7ca52bf32..6746fdad4 100644 --- a/builder/oracle/oci/step_create_instance.go +++ b/builder/oracle/oci/step_create_instance.go @@ -12,14 +12,14 @@ type stepCreateInstance struct{} func (s *stepCreateInstance) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { var ( - driver = state.Get("driver").(Driver) - ui = state.Get("ui").(packer.Ui) - publicKey = state.Get("publicKey").(string) + driver = state.Get("driver").(Driver) + ui = state.Get("ui").(packer.Ui) + config = state.Get("config").(*Config) ) ui.Say("Creating instance...") - instanceID, err := driver.CreateInstance(ctx, publicKey) + instanceID, err := driver.CreateInstance(ctx, string(config.Comm.SSHPublicKey)) if err != nil { err = fmt.Errorf("Problem creating instance: %s", err) ui.Error(err.Error())