-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstep_snapshot.go
55 lines (48 loc) · 1.51 KB
/
step_snapshot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"fmt"
"github.com/mitchellh/multistep"
"github.com/mitchellh/packer/packer"
"log"
"github.com/askholme/vultr"
"time"
)
type stepSnapshot struct{}
func (s *stepSnapshot) Run(state multistep.StateBag) multistep.StepAction {
client := state.Get("client").(*vultr.Client)
ui := state.Get("ui").(packer.Ui)
c := state.Get("config").(config)
serverId := state.Get("server_id").(string)
ui.Say(fmt.Sprintf("Creating snapshot: %v", c.SnapshotName))
_, err := client.CreateSnapshot(serverId, c.SnapshotName)
if err != nil {
err := fmt.Errorf("Error creating snapshot: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
snapshot, err := client.GetSnapshotByLabel(c.SnapshotName)
if err != nil {
err := fmt.Errorf("Error getting snapshot id: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
ui.Say(fmt.Sprintf("Waiting for snapshot %s to complete...",snapshot.Id))
time.Sleep(2*time.Second)
err = waitForSnapshotState("complete", snapshot.Id, client, c.stateTimeout)
if err != nil {
err := fmt.Errorf("Error waiting for snapshot to complete: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
log.Printf("Snapshot image ID: %d", snapshot.Id)
state.Put("snapshot_id", snapshot.Id)
state.Put("snapshot_name", c.SnapshotName)
state.Put("region", c.Region)
return multistep.ActionContinue
}
func (s *stepSnapshot) Cleanup(state multistep.StateBag) {
// no cleanup
}