2013-06-13 12:48:19 -04:00
|
|
|
package digitalocean
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-08-28 14:24:31 -04:00
|
|
|
|
2013-06-13 12:48:19 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stepCreateDroplet struct {
|
|
|
|
dropletId uint
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:25:08 -04:00
|
|
|
func (s *stepCreateDroplet) Run(state multistep.StateBag) multistep.StepAction {
|
2014-09-03 17:37:33 -04:00
|
|
|
client := state.Get("client").(DigitalOceanClient)
|
2013-08-31 15:25:08 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2015-05-27 15:50:43 -04:00
|
|
|
c := state.Get("config").(Config)
|
2013-08-31 15:25:08 -04:00
|
|
|
sshKeyId := state.Get("ssh_key_id").(uint)
|
2013-06-13 12:48:19 -04:00
|
|
|
|
|
|
|
ui.Say("Creating droplet...")
|
|
|
|
|
|
|
|
// Create the droplet based on configuration
|
2014-04-29 23:33:31 -04:00
|
|
|
dropletId, err := client.CreateDroplet(c.DropletName, c.Size, c.Image, c.Region, sshKeyId, c.PrivateNetworking)
|
2013-11-03 11:35:58 -05:00
|
|
|
|
2013-06-13 12:48:19 -04:00
|
|
|
if err != nil {
|
2013-06-20 00:00:51 -04:00
|
|
|
err := fmt.Errorf("Error creating droplet: %s", err)
|
2013-08-31 15:25:08 -04:00
|
|
|
state.Put("error", err)
|
2013-06-13 12:48:19 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// We use this in cleanup
|
|
|
|
s.dropletId = dropletId
|
|
|
|
|
|
|
|
// Store the droplet id for later
|
2013-08-31 15:25:08 -04:00
|
|
|
state.Put("droplet_id", dropletId)
|
2013-06-13 12:48:19 -04:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:25:08 -04:00
|
|
|
func (s *stepCreateDroplet) Cleanup(state multistep.StateBag) {
|
2013-06-13 12:48:19 -04:00
|
|
|
// If the dropletid isn't there, we probably never created it
|
|
|
|
if s.dropletId == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-09-03 17:37:33 -04:00
|
|
|
client := state.Get("client").(DigitalOceanClient)
|
2013-08-31 15:25:08 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2015-05-27 15:50:43 -04:00
|
|
|
c := state.Get("config").(Config)
|
2013-06-13 12:48:19 -04:00
|
|
|
|
|
|
|
// Destroy the droplet we just created
|
|
|
|
ui.Say("Destroying droplet...")
|
2013-06-14 09:26:03 -04:00
|
|
|
|
2013-09-05 02:01:37 -04:00
|
|
|
err := client.DestroyDroplet(s.dropletId)
|
2013-06-13 12:48:19 -04:00
|
|
|
if err != nil {
|
2013-09-01 00:32:12 -04:00
|
|
|
curlstr := fmt.Sprintf("curl '%v/droplets/%v/destroy?client_id=%v&api_key=%v'",
|
2014-08-28 14:24:31 -04:00
|
|
|
c.APIURL, s.dropletId, c.ClientID, c.APIKey)
|
2013-09-01 00:32:12 -04:00
|
|
|
|
2013-06-13 12:48:19 -04:00
|
|
|
ui.Error(fmt.Sprintf(
|
2013-06-17 08:22:29 -04:00
|
|
|
"Error destroying droplet. Please destroy it manually: %v", curlstr))
|
2013-06-13 12:48:19 -04:00
|
|
|
}
|
|
|
|
}
|