/
step_archive.go
127 lines (107 loc) · 3.31 KB
/
step_archive.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package p2pub
import (
"context"
"fmt"
"strings"
"time"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
"github.com/iij/p2pubapi"
"github.com/iij/p2pubapi/protocol"
)
// api
func waitForImage(api *p2pubapi.API, gis, iar, imageID string, timeout time.Duration) error {
start := time.Now()
for {
arg := protocol.CustomOSImageGet{
GisServiceCode: gis,
IarServiceCode: iar,
ImageId: imageID,
}
res := protocol.CustomOSImageGetResponse{}
if err := p2pubapi.Call(*api, arg, &res); err != nil {
return err
}
if res.Status == "Created" {
break
}
if time.Since(start) > timeout {
return fmt.Errorf("timeout")
}
time.Sleep(20 * time.Second)
}
return nil
}
type stepArchive struct {
}
func (s *stepArchive) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
api := state.Get("api").(*p2pubapi.API)
config := state.Get("config").(*Config)
ui.Say(fmt.Sprintf("Saving image ..."))
serviceCode := state.Get("StorageServiceCode").(string)
iar := state.Get("IarServiceCode").(string)
if strings.HasPrefix(serviceCode, "iba") {
args := protocol.CustomOSImageCreate{
GisServiceCode: config.GisServiceCode,
IarServiceCode: iar,
IbaServiceCode: serviceCode,
Name: config.Label,
}
resp := protocol.CustomOSImageCreateResponse{}
if err := p2pubapi.Call(*api, args, &resp); err != nil {
ui.Error(fmt.Sprintf("Failed to save image. reason: %s", err.Error()))
state.Put("error", err)
return multistep.ActionHalt
}
state.Put("ImageId", resp.ImageId)
} else {
args := protocol.OnlineBackup{
GisServiceCode: config.GisServiceCode,
StorageServiceCode: serviceCode,
Label: config.Label,
}
resp := protocol.OnlineBackupResponse{}
if err := p2pubapi.Call(*api, args, &resp); err != nil {
ui.Error(fmt.Sprintf("Failed to save image. reason: %s", err.Error()))
state.Put("error", err)
return multistep.ActionHalt
}
listArgs := protocol.CustomOSImageListGet{
GisServiceCode: config.GisServiceCode,
IarServiceCode: iar,
}
listResp := protocol.CustomOSImageListGetResponse{}
if err := p2pubapi.Call(*api, listArgs, &listResp); err != nil {
ui.Error(fmt.Sprintf("Failed to check saved image. reason: %s", err.Error()))
state.Put("error", err)
return multistep.ActionHalt
}
for _, image := range listResp.ImageList {
if image.SrcServiceCode == serviceCode {
state.Put("ImageId", image.ImageId)
break
}
}
if _, ok := state.GetOk("ImageId"); !ok {
ui.Error(fmt.Sprintf("Failed to check saved image"))
state.Put("error", fmt.Errorf("Failed to check saved image"))
return multistep.ActionHalt
}
}
if err := p2pubapi.WaitSystemStorage(api, config.GisServiceCode, serviceCode,
p2pubapi.InService, p2pubapi.Attached, 30*time.Minute); err != nil {
ui.Error(err.Error())
state.Put("error", err)
return multistep.ActionHalt
}
if err := waitForImage(api, config.GisServiceCode, iar, state.Get("ImageId").(string), 30*time.Minute); err != nil {
ui.Error(err.Error())
state.Put("error", err)
return multistep.ActionHalt
}
ui.Say(fmt.Sprintf("Image saved. ID = %s:%s", iar, state.Get("ImageId").(string)))
return multistep.ActionContinue
}
func (s *stepArchive) Cleanup(state multistep.StateBag) {
}