github.com/daniellockard/packer@v0.7.6-0.20141210173435-5a9390934716/builder/digitalocean/step_snapshot.go (about) 1 package digitalocean 2 3 import ( 4 "errors" 5 "fmt" 6 "log" 7 8 "github.com/mitchellh/multistep" 9 "github.com/mitchellh/packer/packer" 10 ) 11 12 type stepSnapshot struct{} 13 14 func (s *stepSnapshot) 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 dropletId := state.Get("droplet_id").(uint) 19 20 ui.Say(fmt.Sprintf("Creating snapshot: %v", c.SnapshotName)) 21 err := client.CreateSnapshot(dropletId, c.SnapshotName) 22 if err != nil { 23 err := fmt.Errorf("Error creating snapshot: %s", err) 24 state.Put("error", err) 25 ui.Error(err.Error()) 26 return multistep.ActionHalt 27 } 28 29 ui.Say("Waiting for snapshot to complete...") 30 err = waitForDropletState("active", dropletId, client, c.stateTimeout) 31 if err != nil { 32 err := fmt.Errorf("Error waiting for snapshot to complete: %s", err) 33 state.Put("error", err) 34 ui.Error(err.Error()) 35 return multistep.ActionHalt 36 } 37 38 log.Printf("Looking up snapshot ID for snapshot: %s", c.SnapshotName) 39 images, err := client.Images() 40 if err != nil { 41 err := fmt.Errorf("Error looking up snapshot ID: %s", err) 42 state.Put("error", err) 43 ui.Error(err.Error()) 44 return multistep.ActionHalt 45 } 46 47 var imageId uint 48 for _, image := range images { 49 if image.Name == c.SnapshotName { 50 imageId = image.Id 51 break 52 } 53 } 54 55 if imageId == 0 { 56 err := errors.New("Couldn't find snapshot to get the image ID. Bug?") 57 state.Put("error", err) 58 ui.Error(err.Error()) 59 return multistep.ActionHalt 60 } 61 62 log.Printf("Snapshot image ID: %d", imageId) 63 64 state.Put("snapshot_image_id", imageId) 65 state.Put("snapshot_name", c.SnapshotName) 66 state.Put("region", c.Region) 67 68 return multistep.ActionContinue 69 } 70 71 func (s *stepSnapshot) Cleanup(state multistep.StateBag) { 72 // no cleanup 73 }