github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/builder/digitalocean/step_create_droplet.go (about) 1 package digitalocean 2 3 import ( 4 "fmt" 5 6 "github.com/mitchellh/multistep" 7 "github.com/mitchellh/packer/packer" 8 ) 9 10 type stepCreateDroplet struct { 11 dropletId uint 12 } 13 14 func (s *stepCreateDroplet) Run(state multistep.StateBag) multistep.StepAction { 15 client := state.Get("client").(*DigitalOceanClient) 16 ui := state.Get("ui").(packer.Ui) 17 c := state.Get("config").(config) 18 sshKeyId := state.Get("ssh_key_id").(uint) 19 20 ui.Say("Creating droplet...") 21 22 // Create the droplet based on configuration 23 dropletId, err := client.CreateDroplet(c.DropletName, c.Size, c.Image, c.Region, sshKeyId, c.PrivateNetworking) 24 25 if err != nil { 26 err := fmt.Errorf("Error creating droplet: %s", err) 27 state.Put("error", err) 28 ui.Error(err.Error()) 29 return multistep.ActionHalt 30 } 31 32 // We use this in cleanup 33 s.dropletId = dropletId 34 35 // Store the droplet id for later 36 state.Put("droplet_id", dropletId) 37 38 return multistep.ActionContinue 39 } 40 41 func (s *stepCreateDroplet) Cleanup(state multistep.StateBag) { 42 // If the dropletid isn't there, we probably never created it 43 if s.dropletId == 0 { 44 return 45 } 46 47 client := state.Get("client").(*DigitalOceanClient) 48 ui := state.Get("ui").(packer.Ui) 49 c := state.Get("config").(config) 50 51 // Destroy the droplet we just created 52 ui.Say("Destroying droplet...") 53 54 err := client.DestroyDroplet(s.dropletId) 55 if err != nil { 56 curlstr := fmt.Sprintf("curl '%v/droplets/%v/destroy?client_id=%v&api_key=%v'", 57 c.APIURL, s.dropletId, c.ClientID, c.APIKey) 58 59 ui.Error(fmt.Sprintf( 60 "Error destroying droplet. Please destroy it manually: %v", curlstr)) 61 } 62 }