From 25d5874042be12c65d44c9f702ff21b6d32079d8 Mon Sep 17 00:00:00 2001 From: Jack Pearkes Date: Sat, 15 Jun 2013 22:42:40 +0200 Subject: [PATCH 1/6] builder/digitalocean: generate temp rsa keypairs for ssh communication --- builder/digitalocean/step_create_ssh_key.go | 84 +++++---------------- 1 file changed, 19 insertions(+), 65 deletions(-) diff --git a/builder/digitalocean/step_create_ssh_key.go b/builder/digitalocean/step_create_ssh_key.go index 1be83d706..6046f3e58 100644 --- a/builder/digitalocean/step_create_ssh_key.go +++ b/builder/digitalocean/step_create_ssh_key.go @@ -2,7 +2,12 @@ package digitalocean import ( "cgl.tideland.biz/identifier" + "code.google.com/p/go.crypto/ssh" + "crypto/rand" + "crypto/rsa" + "crypto/x509" "encoding/hex" + "encoding/pem" "fmt" "github.com/mitchellh/multistep" "github.com/mitchellh/packer/packer" @@ -18,80 +23,29 @@ func (s *stepCreateSSHKey) Run(state map[string]interface{}) multistep.StepActio 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, - // } + priv, err := rsa.GenerateKey(rand.Reader, 2014) - // 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"]) + // ASN.1 DER encoded form + priv_der := x509.MarshalPKCS1PrivateKey(priv) + priv_blk := pem.Block{ + Type: "RSA PRIVATE KEY", + Headers: nil, + Bytes: priv_der, + } - // Create the public key for uploading to DO - // pub := priv.PublicKey + // Set the private key in the statebag for later + state["privateKey"] = string(pem.EncodeToMemory(&priv_blk)) - // 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_str := `ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD29LZNMe0f7nOmdOIXDrF6eAmLZEk1yrnnsPI+xjLsnKxggMjdD3HvkBPXMdhakOj3pEF6DNtXbK43A7Pilezvu7y2awz+dxCavgUNtwaJkiTJw3C2qleNDDgrq7ZYLJ/wKmfhgPO4jZBej/8ONA0VjxemCNBPTTBeZ8FaeOpeUqopdhk78KGeGmUJ8Bvl8ACuYNdtJ5Y0BQCZkJT+g1ntTwHvuq/Vy/E2uCwJ2xV3vCDkLlqXVyksuVIcLJxTPtd5LdasD4WMQwoOPNdNMBLBG6ZBhXC/6kCVbMgzy5poSZ7r6BK0EA6b2EdAanaojYs3i52j6JeCIIrYtu9Ub173 jack@jose.local` - state["privateKey"] = `-----BEGIN RSA PRIVATE KEY----- -MIIEpAIBAAKCAQEA9vS2TTHtH+5zpnTiFw6xengJi2RJNcq557DyPsYy7JysYIDI -3Q9x75AT1zHYWpDo96RBegzbV2yuNwOz4pXs77u8tmsM/ncQmr4FDbcGiZIkycNw -tqpXjQw4K6u2WCyf8Cpn4YDzuI2QXo//DjQNFY8XpgjQT00wXmfBWnjqXlKqKXYZ -O/ChnhplCfAb5fAArmDXbSeWNAUAmZCU/oNZ7U8B77qv1cvxNrgsCdsVd7wg5C5a -l1cpLLlSHCycUz7XeS3WrA+FjEMKDjzXTTASwRumQYVwv+pAlWzIM8uaaEme6+gS -tBAOm9hHQGp2qI2LN4udo+iXgiCK2LbvVG9e9wIDAQABAoIBABuBB6izTciHoyO/ -0spknYmZQt7ebXTrPic6wtAQ/OzzShN5ZGWSacsXjc4ixAjaKMgj6BLyyZ8EAKcp -52ft8LSGgS8D3y+cDSJe1WtAnh7GQwihlrURZazU1pCukCFj3vA9mNI5rWs5gQG3 -Id3wGCD1jdm1E5Yxb5ikD5nG67tTW5Pn4+tidsavTNsDLsks/pW/0EcPcKAS+TJ8 -Zy15MsGGfHVVkxf+ldULIxxidAeplQhWuED6wkbuD3LQi6Kt4yElHS+UCATca8Fe -CvXNcQWrEHiYUvpyrvU3ybw7WEUUWFa/dctSZwmHvkvRD/bwJPf5M8sIIl8zlyuy -3YCIlSkCgYEA/ZqGOnYIK/bA/QVuyFkFkP3aJjOKJtH0RV9V5XVKSBlU1/Lm3DUZ -XVmp7JuWZHVhPxZa8tswj4x15dX+TwTvGdoUuqPC7K/UMOt6Qzk11o0+o2VRYU97 -GzYyEDxGEnRqoZsc1922I6nBv8YqsW4WkMRhkFN4JNzLJBVXMTXcDCMCgYEA+Uob -VQfVF+7BfCOCNdSu9dqZoYRCyBm5JNEp5bqF1kiEbGw4FhJYp95Ix5ogD3Ug4aqe -8ylwUK86U2BhfkKmGQ5yf+6VNoTx3EPFaGrODIi82BUraYPyYEN10ZrR8Czy5X9g -1WC+WuboRgvTZs+grwnDVJwqQIOqIB2L0p+SdR0CgYEAokHavc7E/bP72CdAsSjb -+d+hUq3JJ3tPiY8suwnnQ+gJM72y3ZOPrf1vTfZiK9Y6KQ4ZlKaPFFkvGaVn95DV -ljnE54FddugsoDwZVqdk/egS+qIZhmQ/BLMRJvgZcTdQ/iLrOmYdYgX788JLkIg6 -Ide0AI6XISavRl/tEIxARPcCgYEAlgh+6K8dFhlRA7iPPnyxjDAzdF0YoDuzDTCB -icy3jh747BQ5sTb7epSyssbU8tiooIjCv1A6U6UScmm4Y3gTZVMnoE1kKnra4Zk8 -LzrQpgSJu3cKOKf78OnI+Ay4u1ciHPOLwQBHsIf2VWn6oo7lg1NZ5wtR9qAHfOqr -Y2k8iRUCgYBKQCtY4SNDuFb6+r5YSEFVfelCn6DJzNgTxO2mkUzzM7RcgejHbd+i -oqgnYXsFLJgm+NpN1eFpbs2RgAe8Zd4pKQNwJFJf0EbEP57sW3kujgFFEsPYJPOp -n8wFU32yrKgrVCftmCk1iI+WPfr1r9LKgKhb0sRX1+DsdWqfN6J7Sw== ------END RSA PRIVATE KEY-----` + // Marshal the public key into SSH compatible format + pub := priv.PublicKey + pub_sshformat := string(ssh.MarshalAuthorizedKey(&pub)) // 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) + keyId, err := client.CreateKey(name, pub_sshformat) if err != nil { ui.Error(err.Error()) From c12e9ff9a8e059234ac01f379a60ec782164dbb8 Mon Sep 17 00:00:00 2001 From: Jack Pearkes Date: Mon, 17 Jun 2013 13:01:42 +0200 Subject: [PATCH 2/6] builder/digitalocean: implement artifacts with the snapshot name --- builder/digitalocean/artifact.go | 27 +++++++++++++++++++++++++++ builder/digitalocean/artifact_test.go | 23 +++++++++++++++++++++++ builder/digitalocean/builder.go | 2 +- 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 builder/digitalocean/artifact.go create mode 100644 builder/digitalocean/artifact_test.go diff --git a/builder/digitalocean/artifact.go b/builder/digitalocean/artifact.go new file mode 100644 index 000000000..7afa35c48 --- /dev/null +++ b/builder/digitalocean/artifact.go @@ -0,0 +1,27 @@ +package digitalocean + +import ( + "fmt" +) + +type Artifact struct { + // The name of the snapshot + snapshotName string +} + +func (*Artifact) BuilderId() string { + return BuilderId +} + +func (*Artifact) Files() []string { + // No files with DigitalOcean + return nil +} + +func (a *Artifact) Id() string { + return a.snapshotName +} + +func (a *Artifact) String() string { + return fmt.Sprintf("A snapshot was created: %v", a.snapshotName) +} diff --git a/builder/digitalocean/artifact_test.go b/builder/digitalocean/artifact_test.go new file mode 100644 index 000000000..cad84f207 --- /dev/null +++ b/builder/digitalocean/artifact_test.go @@ -0,0 +1,23 @@ +package digitalocean + +import ( + "github.com/mitchellh/packer/packer" + "testing" +) + +func TestArtifact_Impl(t *testing.T) { + var raw interface{} + raw = &Artifact{} + if _, ok := raw.(packer.Artifact); !ok { + t.Fatalf("Artifact should be artifact") + } +} + +func TestArtifactString(t *testing.T) { + a := &Artifact{"packer-foobar"} + expected := "A snapshot was created: packer-foobar" + + if a.String() != expected { + t.Fatalf("artifact string should match: %v", expected) + } +} diff --git a/builder/digitalocean/builder.go b/builder/digitalocean/builder.go index 366f6b9e9..f2c841464 100644 --- a/builder/digitalocean/builder.go +++ b/builder/digitalocean/builder.go @@ -148,7 +148,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe b.runner.Run(state) - return nil, nil + return &Artifact{b.config.SnapshotName}, nil } func (b *Builder) Cancel() { From 1e17e90acd0d3a0adf14f2d864a9d068e07d7f03 Mon Sep 17 00:00:00 2001 From: Jack Pearkes Date: Mon, 17 Jun 2013 13:28:21 +0200 Subject: [PATCH 3/6] builder/digitalocean: add configurable "event_delay" for sleeps --- builder/digitalocean/builder.go | 15 ++++++++++ builder/digitalocean/builder_test.go | 32 +++++++++++++++++++++ builder/digitalocean/step_create_droplet.go | 5 +++- builder/digitalocean/step_power_off.go | 5 +++- 4 files changed, 55 insertions(+), 2 deletions(-) diff --git a/builder/digitalocean/builder.go b/builder/digitalocean/builder.go index f2c841464..5cb4cd6a2 100644 --- a/builder/digitalocean/builder.go +++ b/builder/digitalocean/builder.go @@ -31,10 +31,12 @@ type config struct { SSHUsername string `mapstructure:"ssh_username"` SSHPort uint `mapstructure:"ssh_port"` SSHTimeout time.Duration + EventDelay time.Duration PackerDebug bool `mapstructure:"packer_debug"` RawSSHTimeout string `mapstructure:"ssh_timeout"` + RawEventDelay string `mapstructure:"event_delay"` } type Builder struct { @@ -88,6 +90,12 @@ func (b *Builder) Prepare(raws ...interface{}) error { b.config.RawSSHTimeout = "1m" } + if b.config.RawEventDelay == "" { + // Default to 5 second delays after creating events + // to allow DO to process + b.config.RawEventDelay = "5s" + } + // A list of errors on the configuration errs := make([]error, 0) @@ -100,12 +108,19 @@ func (b *Builder) Prepare(raws ...interface{}) error { if b.config.APIKey == "" { errs = append(errs, errors.New("an api_key must be specified")) } + timeout, err := time.ParseDuration(b.config.RawSSHTimeout) if err != nil { errs = append(errs, fmt.Errorf("Failed parsing ssh_timeout: %s", err)) } b.config.SSHTimeout = timeout + delay, err := time.ParseDuration(b.config.RawEventDelay) + if err != nil { + errs = append(errs, fmt.Errorf("Failed parsing event_delay: %s", err)) + } + b.config.EventDelay = delay + if len(errs) > 0 { return &packer.MultiError{errs} } diff --git a/builder/digitalocean/builder_test.go b/builder/digitalocean/builder_test.go index 627779bee..aba0bb1b7 100644 --- a/builder/digitalocean/builder_test.go +++ b/builder/digitalocean/builder_test.go @@ -220,6 +220,38 @@ func TestBuilderPrepare_SSHTimeout(t *testing.T) { } +func TestBuilderPrepare_EventDelay(t *testing.T) { + var b Builder + config := testConfig() + + // Test default + err := b.Prepare(config) + if err != nil { + t.Fatalf("should not have error: %s", err) + } + + if b.config.RawEventDelay != "5s" { + t.Errorf("invalid: %d", b.config.RawEventDelay) + } + + // Test set + config["event_delay"] = "10s" + b = Builder{} + err = b.Prepare(config) + if err != nil { + t.Fatalf("should not have error: %s", err) + } + + // Test bad + config["event_delay"] = "tubes" + b = Builder{} + err = b.Prepare(config) + if err == nil { + t.Fatal("should have error") + } + +} + func TestBuilderPrepare_SnapshotName(t *testing.T) { var b Builder config := testConfig() diff --git a/builder/digitalocean/step_create_droplet.go b/builder/digitalocean/step_create_droplet.go index 36a745de5..a3ffdf9b3 100644 --- a/builder/digitalocean/step_create_droplet.go +++ b/builder/digitalocean/step_create_droplet.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/mitchellh/multistep" "github.com/mitchellh/packer/packer" + "log" "time" ) @@ -49,6 +50,7 @@ func (s *stepCreateDroplet) Cleanup(state map[string]interface{}) { client := state["client"].(*DigitalOceanClient) ui := state["ui"].(packer.Ui) + c := state["config"].(config) // Destroy the droplet we just created ui.Say("Destroying droplet...") @@ -56,7 +58,8 @@ func (s *stepCreateDroplet) Cleanup(state map[string]interface{}) { // Sleep arbitrarily before sending destroy request // Otherwise we get "pending event" errors, even though there isn't // one. - time.Sleep(5 * time.Second) + log.Printf("Sleeping for %v, event_delay", c.RawEventDelay) + time.Sleep(c.EventDelay) err := client.DestroyDroplet(s.dropletId) diff --git a/builder/digitalocean/step_power_off.go b/builder/digitalocean/step_power_off.go index b487f7779..87bab6bb1 100644 --- a/builder/digitalocean/step_power_off.go +++ b/builder/digitalocean/step_power_off.go @@ -3,6 +3,7 @@ package digitalocean import ( "github.com/mitchellh/multistep" "github.com/mitchellh/packer/packer" + "log" "time" ) @@ -10,13 +11,15 @@ type stepPowerOff struct{} func (s *stepPowerOff) Run(state map[string]interface{}) multistep.StepAction { client := state["client"].(*DigitalOceanClient) + c := state["config"].(config) ui := state["ui"].(packer.Ui) dropletId := state["droplet_id"].(uint) // Sleep arbitrarily before sending power off request // Otherwise we get "pending event" errors, even though there isn't // one. - time.Sleep(3 * time.Second) + log.Printf("Sleeping for %v, event_delay", c.RawEventDelay) + time.Sleep(c.EventDelay) // Poweroff the droplet so it can be snapshot err := client.PowerOffDroplet(dropletId) From 477ac8cd3185cb259acd07e4c2b4d7a8d5d3baad Mon Sep 17 00:00:00 2001 From: Jack Pearkes Date: Mon, 17 Jun 2013 14:21:15 +0200 Subject: [PATCH 4/6] builder/digitalocean: use text/template for the snapshot name --- builder/digitalocean/builder.go | 27 +++++++++++++++++++----- builder/digitalocean/builder_test.go | 30 +++++++++++++++++++++++---- builder/digitalocean/step_snapshot.go | 3 ++- 3 files changed, 50 insertions(+), 10 deletions(-) diff --git a/builder/digitalocean/builder.go b/builder/digitalocean/builder.go index 5cb4cd6a2..d8469d752 100644 --- a/builder/digitalocean/builder.go +++ b/builder/digitalocean/builder.go @@ -4,6 +4,7 @@ package digitalocean import ( + "bytes" "errors" "fmt" "github.com/mitchellh/mapstructure" @@ -11,12 +12,18 @@ import ( "github.com/mitchellh/packer/builder/common" "github.com/mitchellh/packer/packer" "log" + "strconv" + "text/template" "time" ) // The unique id for the builder const BuilderId = "pearkes.digitalocean" +type snapshotNameData struct { + CreateTime string +} + // Configuration tells the builder the credentials // to use while communicating with DO and describes the image // you are creating @@ -27,7 +34,7 @@ type config struct { SizeID uint `mapstructure:"size_id"` ImageID uint `mapstructure:"image_id"` - SnapshotName string `mapstructure:"snapshot_name"` + SnapshotName string SSHUsername string `mapstructure:"ssh_username"` SSHPort uint `mapstructure:"ssh_port"` SSHTimeout time.Duration @@ -35,8 +42,9 @@ type config struct { PackerDebug bool `mapstructure:"packer_debug"` - RawSSHTimeout string `mapstructure:"ssh_timeout"` - RawEventDelay string `mapstructure:"event_delay"` + RawSnapshotName string `mapstructure:"snapshot_name"` + RawSSHTimeout string `mapstructure:"ssh_timeout"` + RawEventDelay string `mapstructure:"event_delay"` } type Builder struct { @@ -80,9 +88,9 @@ func (b *Builder) Prepare(raws ...interface{}) error { b.config.SSHPort = 22 } - if b.config.SnapshotName == "" { + if b.config.RawSnapshotName == "" { // Default to packer-{{ unix timestamp (utc) }} - b.config.SnapshotName = "packer-{{.CreateTime}}" + b.config.RawSnapshotName = "packer-{{.CreateTime}}" } if b.config.RawSSHTimeout == "" { @@ -121,6 +129,15 @@ func (b *Builder) Prepare(raws ...interface{}) error { } b.config.EventDelay = delay + // Parse the name of the snapshot + snapNameBuf := new(bytes.Buffer) + tData := snapshotNameData{ + strconv.FormatInt(time.Now().UTC().Unix(), 10), + } + t := template.Must(template.New("snapshot").Parse(b.config.RawSnapshotName)) + t.Execute(snapNameBuf, tData) + b.config.SnapshotName = snapNameBuf.String() + if len(errs) > 0 { return &packer.MultiError{errs} } diff --git a/builder/digitalocean/builder_test.go b/builder/digitalocean/builder_test.go index aba0bb1b7..907d5e7b4 100644 --- a/builder/digitalocean/builder_test.go +++ b/builder/digitalocean/builder_test.go @@ -2,6 +2,7 @@ package digitalocean import ( "github.com/mitchellh/packer/packer" + "strconv" "testing" ) @@ -256,14 +257,35 @@ func TestBuilderPrepare_SnapshotName(t *testing.T) { var b Builder config := testConfig() - // Test set - config["snapshot_name"] = "foo" + // Test default err := b.Prepare(config) if err != nil { t.Fatalf("should not have error: %s", err) } - if b.config.SnapshotName != "foo" { - t.Errorf("invalid: %s", b.config.SnapshotName) + if b.config.RawSnapshotName != "packer-{{.CreateTime}}" { + t.Errorf("invalid: %d", b.config.RawSnapshotName) } + + // Test set + config["snapshot_name"] = "foobarbaz" + b = Builder{} + err = b.Prepare(config) + if err != nil { + t.Fatalf("should not have error: %s", err) + } + + // Test set with template + config["snapshot_name"] = "{{.CreateTime}}" + b = Builder{} + err = b.Prepare(config) + if err != nil { + t.Fatalf("should not have error: %s", err) + } + + _, err = strconv.ParseInt(b.config.SnapshotName, 0, 0) + if err != nil { + t.Fatalf("failed to parse int in template: %s", err) + } + } diff --git a/builder/digitalocean/step_snapshot.go b/builder/digitalocean/step_snapshot.go index 93e450cf6..1abeb67b0 100644 --- a/builder/digitalocean/step_snapshot.go +++ b/builder/digitalocean/step_snapshot.go @@ -1,6 +1,7 @@ package digitalocean import ( + "fmt" "github.com/mitchellh/multistep" "github.com/mitchellh/packer/packer" ) @@ -13,7 +14,7 @@ func (s *stepSnapshot) Run(state map[string]interface{}) multistep.StepAction { c := state["config"].(config) dropletId := state["droplet_id"].(uint) - ui.Say("Creating snapshot...") + ui.Say(fmt.Sprintf("Creating snapshot: %v", c.SnapshotName)) err := client.CreateSnapshot(dropletId, c.SnapshotName) From 341cfb2c2dbab77a23ac3193c7602ea4a0920f33 Mon Sep 17 00:00:00 2001 From: Jack Pearkes Date: Mon, 17 Jun 2013 14:22:29 +0200 Subject: [PATCH 5/6] builder/digitalocean: improve error messages from DO api --- builder/digitalocean/api.go | 8 ++++++-- builder/digitalocean/step_create_droplet.go | 5 ++++- builder/digitalocean/step_create_ssh_key.go | 7 ++++++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/builder/digitalocean/api.go b/builder/digitalocean/api.go index 85c3c63a9..72e4752cc 100644 --- a/builder/digitalocean/api.go +++ b/builder/digitalocean/api.go @@ -156,7 +156,7 @@ func NewRequest(d DigitalOceanClient, path string, params string) (map[string]in // Catch all non-200 status and return an error if resp.StatusCode != 200 { - err = errors.New(fmt.Sprintf("recieved non-200 status from digitalocean: %d", resp.StatusCode)) + err = errors.New(fmt.Sprintf("Recieved non-200 HTTP status from DigitalOcean: %d", resp.StatusCode)) log.Printf("response from digital ocean: %v", decodedResponse) return decodedResponse, err } @@ -170,7 +170,11 @@ func NewRequest(d DigitalOceanClient, path string, params string) (map[string]in // Catch all non-OK statuses from DO and return an error status := decodedResponse["status"] if status != "OK" { - err = errors.New(fmt.Sprintf("recieved non-OK status from digitalocean: %d", status)) + // Get the actual error message if there is one + if status == "ERROR" { + status = decodedResponse["error_message"] + } + err = errors.New(fmt.Sprintf("Recieved bad status from DigitalOcean: %v", status)) log.Printf("response from digital ocean: %v", decodedResponse) return decodedResponse, err } diff --git a/builder/digitalocean/step_create_droplet.go b/builder/digitalocean/step_create_droplet.go index a3ffdf9b3..1a2dff3e3 100644 --- a/builder/digitalocean/step_create_droplet.go +++ b/builder/digitalocean/step_create_droplet.go @@ -63,8 +63,11 @@ func (s *stepCreateDroplet) Cleanup(state map[string]interface{}) { err := client.DestroyDroplet(s.dropletId) + curlstr := fmt.Sprintf("curl '%v/droplets/%v/destroy?client_id=%v&api_key=%v'", + DIGITALOCEAN_API_URL, s.dropletId, c.ClientID, c.APIKey) + if err != nil { ui.Error(fmt.Sprintf( - "Error destroying droplet. Please destroy it manually: %v", s.dropletId)) + "Error destroying droplet. Please destroy it manually: %v", curlstr)) } } diff --git a/builder/digitalocean/step_create_ssh_key.go b/builder/digitalocean/step_create_ssh_key.go index 6046f3e58..6f0025a92 100644 --- a/builder/digitalocean/step_create_ssh_key.go +++ b/builder/digitalocean/step_create_ssh_key.go @@ -71,12 +71,17 @@ func (s *stepCreateSSHKey) Cleanup(state map[string]interface{}) { client := state["client"].(*DigitalOceanClient) ui := state["ui"].(packer.Ui) + c := state["config"].(config) ui.Say("Deleting temporary ssh key...") err := client.DestroyKey(s.keyId) + + curlstr := fmt.Sprintf("curl '%v/ssh_keys/%v/destroy?client_id=%v&api_key=%v'", + DIGITALOCEAN_API_URL, s.keyId, c.ClientID, c.APIKey) + 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)) + "Error cleaning up ssh key. Please delete the key manually: %v", curlstr)) } } From 6eb0568f07fb2f223bc16d62336d2e7a7a60c0e6 Mon Sep 17 00:00:00 2001 From: Jack Pearkes Date: Mon, 17 Jun 2013 14:31:47 +0200 Subject: [PATCH 6/6] builder/digitalocean: print bad status code as string --- builder/digitalocean/api.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/builder/digitalocean/api.go b/builder/digitalocean/api.go index 72e4752cc..e04e86d08 100644 --- a/builder/digitalocean/api.go +++ b/builder/digitalocean/api.go @@ -154,15 +154,14 @@ func NewRequest(d DigitalOceanClient, path string, params string) (map[string]in err = json.Unmarshal(body, &decodedResponse) + log.Printf("response from digitalocean: %v", decodedResponse) + // Catch all non-200 status and return an error if resp.StatusCode != 200 { - err = errors.New(fmt.Sprintf("Recieved non-200 HTTP status from DigitalOcean: %d", resp.StatusCode)) - log.Printf("response from digital ocean: %v", decodedResponse) + err = errors.New(fmt.Sprintf("Received non-200 HTTP status from DigitalOcean: %v", resp.StatusCode)) return decodedResponse, err } - log.Printf("response from digital ocean: %v", decodedResponse) - if err != nil { return decodedResponse, err } @@ -174,8 +173,7 @@ func NewRequest(d DigitalOceanClient, path string, params string) (map[string]in if status == "ERROR" { status = decodedResponse["error_message"] } - err = errors.New(fmt.Sprintf("Recieved bad status from DigitalOcean: %v", status)) - log.Printf("response from digital ocean: %v", decodedResponse) + err = errors.New(fmt.Sprintf("Received bad status from DigitalOcean: %v", status)) return decodedResponse, err }