forked from hashicorp/packer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
step_create_snapshot.go
54 lines (47 loc) · 1.45 KB
/
step_create_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
package hcloud
import (
"context"
"fmt"
"github.com/Bourne-ID/packer/packer-plugin-sdk/multistep"
packersdk "github.com/Bourne-ID/packer/packer-plugin-sdk/packer"
"github.com/hetznercloud/hcloud-go/hcloud"
)
type stepCreateSnapshot struct{}
func (s *stepCreateSnapshot) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
client := state.Get("hcloudClient").(*hcloud.Client)
ui := state.Get("ui").(packersdk.Ui)
c := state.Get("config").(*Config)
serverID := state.Get("server_id").(int)
ui.Say("Creating snapshot ...")
ui.Say("This can take some time")
result, _, err := client.Server.CreateImage(ctx, &hcloud.Server{ID: serverID}, &hcloud.ServerCreateImageOpts{
Type: hcloud.ImageTypeSnapshot,
Labels: c.SnapshotLabels,
Description: hcloud.String(c.SnapshotName),
})
if err != nil {
err := fmt.Errorf("Error creating snapshot: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
state.Put("snapshot_id", result.Image.ID)
state.Put("snapshot_name", c.SnapshotName)
_, errCh := client.Action.WatchProgress(ctx, result.Action)
for {
select {
case err1 := <-errCh:
if err1 == nil {
return multistep.ActionContinue
} else {
err := fmt.Errorf("Error creating snapshot: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
}
}
}
func (s *stepCreateSnapshot) Cleanup(state multistep.StateBag) {
// no cleanup
}