129 lines
4.5 KiB
Go
129 lines
4.5 KiB
Go
package digitalocean
|
|
|
|
import (
|
|
"cgl.tideland.biz/identifier"
|
|
"encoding/hex"
|
|
"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...")
|
|
// priv, err := rsa.GenerateKey(rand.Reader, 2014)
|
|
// if err != nil {
|
|
// ui.Error(err.Error())
|
|
// return multistep.ActionHalt
|
|
// }
|
|
|
|
// priv_der := x509.MarshalPKCS1PrivateKey(priv)
|
|
// priv_blk := pem.Block{
|
|
// Type: "RSA PRIVATE KEY",
|
|
// Headers: nil,
|
|
// Bytes: priv_der,
|
|
// }
|
|
|
|
// Set the pem formatted private key on the state for later
|
|
// state["privateKey"] = string(pem.EncodeToMemory(&priv_blk))
|
|
// log.Printf("PRIVATE KEY:\n\n%v\n\n", state["privateKey"])
|
|
|
|
// Create the public key for uploading to DO
|
|
// pub := priv.PublicKey
|
|
|
|
// pub_bytes, err := x509.MarshalPKIXPublicKey(&pub)
|
|
|
|
// pub_blk := pem.Block{
|
|
// Type: "RSA PUBLIC KEY",
|
|
// Headers: nil,
|
|
// Bytes: pub_bytes,
|
|
// }
|
|
|
|
// if err != nil {
|
|
// ui.Error(err.Error())
|
|
// return multistep.ActionHalt
|
|
// }
|
|
|
|
// // Encode the public key to base64
|
|
// pub_str := base64.StdEncoding.EncodeToString(pub_bytes)
|
|
// pub_str = "ssh-rsa " + pub_str
|
|
|
|
// log.Printf("PUBLIC KEY:\n\n%v\n\n", string(pem.EncodeToMemory(&pub_blk)))
|
|
// log.Printf("PUBLIC KEY BASE64:\n\n%v\n\n", pub_str)
|
|
|
|
pub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fr1r9LKgKhb0sRX1+DsdWqfN6J7Sw==
|
|
-----END RSA PRIVATE KEY-----`
|
|
|
|
// The name of the public key on DO
|
|
name := fmt.Sprintf("packer-%s", hex.EncodeToString(identifier.NewUUID().Raw()))
|
|
|
|
// Create the key!
|
|
keyId, err := client.CreateKey(name, pub_str)
|
|
|
|
if err != nil {
|
|
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
|
|
state["ssh_key_id"] = keyId
|
|
|
|
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)
|
|
|
|
ui.Say("Deleting temporary ssh key...")
|
|
err := client.DestroyKey(s.keyId)
|
|
if err != nil {
|
|
log.Printf("Error cleaning up ssh key: %v", err.Error())
|
|
ui.Error(fmt.Sprintf(
|
|
"Error cleaning up ssh key. Please delete the key manually: %v", s.keyId))
|
|
}
|
|
}
|