2013-06-13 11:58:06 -04:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
|
|
|
"cgl.tideland.biz/identifier"
|
2013-06-15 16:42:40 -04:00
|
|
|
"code.google.com/p/go.crypto/ssh"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/x509"
|
2013-06-13 11:58:06 -04:00
|
|
|
"encoding/hex"
|
2013-06-15 16:42:40 -04:00
|
|
|
"encoding/pem"
|
2013-06-13 11:58:06 -04:00
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stepCreateSSHKey struct {
|
|
|
|
keyId uint
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepCreateSSHKey) Run(state map[string]interface{}) multistep.StepAction {
|
|
|
|
client := state["client"].(*DigitalOceanClient)
|
|
|
|
ui := state["ui"].(packer.Ui)
|
|
|
|
|
|
|
|
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
|
|
|
|
state["privateKey"] = string(pem.EncodeToMemory(&priv_blk))
|
|
|
|
|
|
|
|
// Marshal the public key into SSH compatible format
|
|
|
|
pub := priv.PublicKey
|
|
|
|
pub_sshformat := string(ssh.MarshalAuthorizedKey(&pub))
|
2013-06-14 09:26:03 -04:00
|
|
|
|
|
|
|
// The name of the public key on DO
|
|
|
|
name := fmt.Sprintf("packer-%s", hex.EncodeToString(identifier.NewUUID().Raw()))
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
state["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-06-13 12:48:19 -04:00
|
|
|
state["ssh_key_id"] = keyId
|
2013-06-13 11:58:06 -04:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepCreateSSHKey) Cleanup(state map[string]interface{}) {
|
|
|
|
// If no key name is set, then we never created it, so just return
|
|
|
|
if s.keyId == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
client := state["client"].(*DigitalOceanClient)
|
|
|
|
ui := state["ui"].(packer.Ui)
|
2013-06-17 08:22:29 -04:00
|
|
|
c := state["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'",
|
|
|
|
DIGITALOCEAN_API_URL, s.keyId, c.ClientID, c.APIKey)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|