From 29b5ec28d51182c46544b008ac2d6ecaa29b036a Mon Sep 17 00:00:00 2001 From: Pragnesh Date: Sat, 2 Dec 2023 21:25:27 +0530 Subject: [PATCH] if ssh_key_id is specified don't import ssh public key --- builder/digitalocean/step_create_ssh_key.go | 49 +++++++++++---------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/builder/digitalocean/step_create_ssh_key.go b/builder/digitalocean/step_create_ssh_key.go index 3eeb526..6bf0eb0 100644 --- a/builder/digitalocean/step_create_ssh_key.go +++ b/builder/digitalocean/step_create_ssh_key.go @@ -19,36 +19,37 @@ func (s *stepCreateSSHKey) Run(ctx context.Context, state multistep.StateBag) mu client := state.Get("client").(*godo.Client) ui := state.Get("ui").(packersdk.Ui) c := state.Get("config").(*Config) + if c.SSHKeyID == 0 { + if c.Comm.SSHPublicKey == nil { + ui.Say("No public SSH key found; skipping SSH public key import...") + return multistep.ActionContinue + } - if c.Comm.SSHPublicKey == nil { - ui.Say("No public SSH key found; skipping SSH public key import...") - return multistep.ActionContinue - } + ui.Say("Importing SSH public key...") - ui.Say("Importing SSH public key...") + // The name of the public key on DO + name := fmt.Sprintf("packer-%s", uuid.TimeOrderedUUID()) - // The name of the public key on DO - name := fmt.Sprintf("packer-%s", uuid.TimeOrderedUUID()) + // Create the key! + key, _, err := client.Keys.Create(context.TODO(), &godo.KeyCreateRequest{ + Name: name, + PublicKey: string(c.Comm.SSHPublicKey), + }) + if err != nil { + err := fmt.Errorf("Error creating temporary SSH key: %s", err) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } - // Create the key! - key, _, err := client.Keys.Create(context.TODO(), &godo.KeyCreateRequest{ - Name: name, - PublicKey: string(c.Comm.SSHPublicKey), - }) - if err != nil { - err := fmt.Errorf("Error creating temporary SSH key: %s", err) - state.Put("error", err) - ui.Error(err.Error()) - return multistep.ActionHalt - } + // We use this to check cleanup + s.keyId = key.ID - // We use this to check cleanup - s.keyId = key.ID + log.Printf("temporary ssh key name: %s", name) - log.Printf("temporary ssh key name: %s", name) - - // Remember some state for the future - state.Put("ssh_key_id", key.ID) + // Remember some state for the future + state.Put("ssh_key_id", key.ID) + } return multistep.ActionContinue }