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