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