Skip to content

Commit

Permalink
if ssh_key_id is specified don't import ssh public key
Browse files Browse the repository at this point in the history
  • Loading branch information
pragnesh committed Dec 2, 2023
1 parent ece74c0 commit 29b5ec2
Showing 1 changed file with 25 additions and 24 deletions.
49 changes: 25 additions & 24 deletions builder/digitalocean/step_create_ssh_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 29b5ec2

Please sign in to comment.