2013-06-13 11:58:06 -04:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
2017-04-08 15:52:57 -04:00
|
|
|
"context"
|
2013-06-13 11:58:06 -04:00
|
|
|
"fmt"
|
2014-08-28 14:24:31 -04:00
|
|
|
"log"
|
|
|
|
|
2015-06-10 17:02:06 -04:00
|
|
|
"github.com/digitalocean/godo"
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/uuid"
|
2013-06-13 11:58:06 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type stepCreateSSHKey struct {
|
2015-06-10 17:02:06 -04:00
|
|
|
keyId int
|
2013-06-13 11:58:06 -04:00
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepCreateSSHKey) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2015-06-10 17:02:06 -04:00
|
|
|
client := state.Get("client").(*godo.Client)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2018-08-27 09:58:00 -04:00
|
|
|
c := state.Get("config").(*Config)
|
2013-06-13 11:58:06 -04:00
|
|
|
|
2021-04-07 04:33:05 -04:00
|
|
|
if c.Comm.SSHPublicKey == nil {
|
|
|
|
ui.Say("No public SSH key found; skipping SSH public key import...")
|
|
|
|
return multistep.ActionContinue
|
2019-11-08 13:35:48 -05:00
|
|
|
}
|
2013-06-15 16:42:40 -04:00
|
|
|
|
2021-04-07 04:33:05 -04:00
|
|
|
ui.Say("Importing SSH public key...")
|
2013-06-14 09:26:03 -04:00
|
|
|
|
|
|
|
// The name of the public key on DO
|
2013-10-16 22:21:14 -04:00
|
|
|
name := fmt.Sprintf("packer-%s", uuid.TimeOrderedUUID())
|
2013-06-14 09:26:03 -04:00
|
|
|
|
|
|
|
// Create the key!
|
2017-04-09 14:33:05 -04:00
|
|
|
key, _, err := client.Keys.Create(context.TODO(), &godo.KeyCreateRequest{
|
2015-06-10 17:02:06 -04:00
|
|
|
Name: name,
|
2021-04-07 04:33:05 -04:00
|
|
|
PublicKey: string(c.Comm.SSHPublicKey),
|
2015-06-10 17:02:06 -04:00
|
|
|
})
|
2013-06-13 11:58:06 -04:00
|
|
|
if err != nil {
|
2013-06-20 00:00:51 -04:00
|
|
|
err := fmt.Errorf("Error creating temporary SSH key: %s", err)
|
2013-08-31 15:25:08 -04:00
|
|
|
state.Put("error", err)
|
2013-06-13 11:58:06 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// We use this to check cleanup
|
2015-06-10 17:02:06 -04:00
|
|
|
s.keyId = key.ID
|
2013-06-13 11:58:06 -04:00
|
|
|
|
|
|
|
log.Printf("temporary ssh key name: %s", name)
|
|
|
|
|
|
|
|
// Remember some state for the future
|
2015-06-10 17:02:06 -04:00
|
|
|
state.Put("ssh_key_id", key.ID)
|
2013-06-13 11:58:06 -04:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:25:08 -04:00
|
|
|
func (s *stepCreateSSHKey) Cleanup(state multistep.StateBag) {
|
2013-06-13 11:58:06 -04:00
|
|
|
// If no key name is set, then we never created it, so just return
|
|
|
|
if s.keyId == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-06-10 17:02:06 -04:00
|
|
|
client := state.Get("client").(*godo.Client)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2013-06-13 11:58:06 -04:00
|
|
|
|
|
|
|
ui.Say("Deleting temporary ssh key...")
|
2017-04-09 14:33:05 -04:00
|
|
|
_, err := client.Keys.DeleteByID(context.TODO(), s.keyId)
|
2013-06-13 11:58:06 -04:00
|
|
|
if err != nil {
|
2015-06-10 17:02:06 -04:00
|
|
|
log.Printf("Error cleaning up ssh key: %s", err)
|
2013-06-13 11:58:06 -04:00
|
|
|
ui.Error(fmt.Sprintf(
|
2015-06-10 17:02:06 -04:00
|
|
|
"Error cleaning up ssh key. Please delete the key manually: %s", err))
|
2013-06-13 11:58:06 -04:00
|
|
|
}
|
|
|
|
}
|