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"
|
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 {
|
|
|
|
keyId uint
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:25:08 -04:00
|
|
|
func (s *stepCreateSSHKey) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
client := state.Get("client").(*DigitalOceanClient)
|
|
|
|
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!
|
2013-06-15 16:42:40 -04:00
|
|
|
keyId, err := client.CreateKey(name, 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
|
|
|
|
s.keyId = keyId
|
|
|
|
|
|
|
|
log.Printf("temporary ssh key name: %s", name)
|
|
|
|
|
|
|
|
// Remember some state for the future
|
2013-08-31 15:25:08 -04:00
|
|
|
state.Put("ssh_key_id", keyId)
|
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
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:25:08 -04:00
|
|
|
client := state.Get("client").(*DigitalOceanClient)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
c := state.Get("config").(config)
|
2013-06-13 11:58:06 -04:00
|
|
|
|
|
|
|
ui.Say("Deleting temporary ssh key...")
|
|
|
|
err := client.DestroyKey(s.keyId)
|
2013-06-17 08:22:29 -04:00
|
|
|
|
|
|
|
curlstr := fmt.Sprintf("curl '%v/ssh_keys/%v/destroy?client_id=%v&api_key=%v'",
|
2014-08-28 14:24:31 -04:00
|
|
|
c.APIURL, s.keyId, c.ClientID, c.APIKey)
|
2013-06-17 08:22:29 -04:00
|
|
|
|
2013-06-13 11:58:06 -04:00
|
|
|
if err != nil {
|
2013-06-14 09:26:03 -04:00
|
|
|
log.Printf("Error cleaning up ssh key: %v", err.Error())
|
2013-06-13 11:58:06 -04:00
|
|
|
ui.Error(fmt.Sprintf(
|
2013-06-17 08:22:29 -04:00
|
|
|
"Error cleaning up ssh key. Please delete the key manually: %v", curlstr))
|
2013-06-13 11:58:06 -04:00
|
|
|
}
|
|
|
|
}
|