2013-06-13 11:58:06 -04:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
2013-06-15 16:42:40 -04:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
2013-06-13 11:58:06 -04:00
|
|
|
"fmt"
|
2014-08-28 14:24:31 -04:00
|
|
|
"log"
|
|
|
|
|
|
|
|
"code.google.com/p/gosshold/ssh"
|
2015-06-10 17:02:06 -04:00
|
|
|
"github.com/digitalocean/godo"
|
2013-06-13 11:58:06 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
2013-10-16 22:21:14 -04:00
|
|
|
"github.com/mitchellh/packer/common/uuid"
|
2013-06-13 11:58:06 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stepCreateSSHKey struct {
|
2015-06-10 17:02:06 -04:00
|
|
|
keyId int
|
2013-06-13 11:58:06 -04:00
|
|
|
}
|
|
|
|
|
2013-08-31 15:25:08 -04:00
|
|
|
func (s *stepCreateSSHKey) Run(state multistep.StateBag) multistep.StepAction {
|
2015-06-10 17:02:06 -04:00
|
|
|
client := state.Get("client").(*godo.Client)
|
2013-08-31 15:25:08 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-06-13 11:58:06 -04:00
|
|
|
|
|
|
|
ui.Say("Creating temporary ssh key for droplet...")
|
2013-06-15 16:42:40 -04:00
|
|
|
|
|
|
|
priv, err := rsa.GenerateKey(rand.Reader, 2014)
|
|
|
|
|
|
|
|
// ASN.1 DER encoded form
|
|
|
|
priv_der := x509.MarshalPKCS1PrivateKey(priv)
|
|
|
|
priv_blk := pem.Block{
|
|
|
|
Type: "RSA PRIVATE KEY",
|
|
|
|
Headers: nil,
|
|
|
|
Bytes: priv_der,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the private key in the statebag for later
|
2013-08-31 15:25:08 -04:00
|
|
|
state.Put("privateKey", string(pem.EncodeToMemory(&priv_blk)))
|
2013-06-15 16:42:40 -04:00
|
|
|
|
|
|
|
// Marshal the public key into SSH compatible format
|
2013-09-20 00:30:22 -04:00
|
|
|
// TODO properly handle the public key error
|
|
|
|
pub, _ := ssh.NewPublicKey(&priv.PublicKey)
|
2013-09-15 15:21:21 -04:00
|
|
|
pub_sshformat := string(ssh.MarshalAuthorizedKey(pub))
|
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!
|
2015-06-10 17:02:06 -04:00
|
|
|
key, _, err := client.Keys.Create(&godo.KeyCreateRequest{
|
|
|
|
Name: name,
|
|
|
|
PublicKey: pub_sshformat,
|
|
|
|
})
|
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)
|
2013-08-31 15:25:08 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-06-13 11:58:06 -04:00
|
|
|
|
|
|
|
ui.Say("Deleting temporary ssh key...")
|
2015-06-10 17:02:06 -04:00
|
|
|
_, err := client.Keys.DeleteByID(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
|
|
|
}
|
|
|
|
}
|