Skip to content

Commit 05c6fe0

Browse files
feat: add skip_create_snapshot config to the builder (#289)
If skip_create_snapshot is set to true, we don't create the snapshot but only run the defined provisioners to ensure all scripts are correct. When set to false, the snapshot is created. Closes #86 Signed-off-by: Dhairya Arora <dhairya.arora@syself.com>
1 parent 1e1c85e commit 05c6fe0

File tree

8 files changed

+105
-26
lines changed

8 files changed

+105
-26
lines changed

.web-docs/components/builder/hcloud/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ builder.
7979
- `server_labels` (map of key/value strings) - Key/value pair labels to
8080
apply to the created server.
8181

82+
- `skip_create_snapshot` (boolean) - Skips snapshot creation.
83+
8284
- `snapshot_name` (string) - The name of the resulting snapshot that will
8385
appear in your account as image description. Defaults to `packer-{{timestamp}}` (see
8486
[configuration templates](/packer/docs/templates/legacy_json_templates/engine) for more info).

builder/hcloud/config.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,13 @@ type Config struct {
4040
Image string `mapstructure:"image"`
4141
ImageFilter *imageFilter `mapstructure:"image_filter"`
4242

43-
SnapshotName string `mapstructure:"snapshot_name"`
44-
SnapshotLabels map[string]string `mapstructure:"snapshot_labels"`
45-
UserData string `mapstructure:"user_data"`
46-
UserDataFile string `mapstructure:"user_data_file"`
47-
SSHKeys []string `mapstructure:"ssh_keys"`
48-
SSHKeysLabels map[string]string `mapstructure:"ssh_keys_labels"`
43+
SkipCreateSnapshot bool `mapstructure:"skip_create_snapshot"`
44+
SnapshotName string `mapstructure:"snapshot_name"`
45+
SnapshotLabels map[string]string `mapstructure:"snapshot_labels"`
46+
UserData string `mapstructure:"user_data"`
47+
UserDataFile string `mapstructure:"user_data_file"`
48+
SSHKeys []string `mapstructure:"ssh_keys"`
49+
SSHKeysLabels map[string]string `mapstructure:"ssh_keys_labels"`
4950

5051
Networks []int64 `mapstructure:"networks"`
5152
PublicIPv4 string `mapstructure:"public_ipv4"`

builder/hcloud/config.hcl2spec.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

builder/hcloud/step_create_snapshot.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ func (s *stepCreateSnapshot) Run(ctx context.Context, state multistep.StateBag)
2020

2121
serverID := state.Get(StateServerID).(int64)
2222

23+
// Skip snapshot creation if skip_create_snapshot is set to true.
24+
if c.SkipCreateSnapshot {
25+
ui.Say("Skipping snapshot creation...")
26+
return multistep.ActionContinue
27+
}
28+
2329
ui.Say("Creating snapshot...")
2430
ui.Say("This can take some time")
2531
result, _, err := client.Server.CreateImage(ctx, &hcloud.Server{ID: serverID}, &hcloud.ServerCreateImageOpts{

builder/hcloud/step_create_snapshot_test.go

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ func TestStepCreateSnapshot(t *testing.T) {
2323
state.Put(StateServerID, int64(8))
2424
},
2525
WantRequests: []mockutil.Request{
26-
{Method: "POST", Path: "/servers/8/actions/create_image",
26+
{
27+
Method: "POST", Path: "/servers/8/actions/create_image",
2728
Want: func(t *testing.T, req *http.Request) {
2829
payload := decodeJSONBody(t, req.Body, &schema.ServerActionCreateImageRequest{})
2930
assert.Equal(t, "dummy-snapshot", *payload.Description)
@@ -35,7 +36,8 @@ func TestStepCreateSnapshot(t *testing.T) {
3536
"action": { "id": 3, "status": "running" }
3637
}`,
3738
},
38-
{Method: "GET", Path: "/actions?id=3&page=1&sort=status&sort=id",
39+
{
40+
Method: "GET", Path: "/actions?id=3&page=1&sort=status&sort=id",
3941
Status: 200,
4042
JSONRaw: `{
4143
"actions": [
@@ -63,7 +65,8 @@ func TestStepCreateSnapshot(t *testing.T) {
6365
state.Put(StateServerID, int64(8))
6466
},
6567
WantRequests: []mockutil.Request{
66-
{Method: "POST", Path: "/servers/8/actions/create_image",
68+
{
69+
Method: "POST", Path: "/servers/8/actions/create_image",
6770
Want: func(t *testing.T, req *http.Request) {
6871
payload := decodeJSONBody(t, req.Body, &schema.ServerActionCreateImageRequest{})
6972
assert.Equal(t, "dummy-snapshot", *payload.Description)
@@ -87,7 +90,8 @@ func TestStepCreateSnapshot(t *testing.T) {
8790
state.Put(StateServerID, int64(8))
8891
},
8992
WantRequests: []mockutil.Request{
90-
{Method: "POST", Path: "/servers/8/actions/create_image",
93+
{
94+
Method: "POST", Path: "/servers/8/actions/create_image",
9195
Want: func(t *testing.T, req *http.Request) {
9296
payload := decodeJSONBody(t, req.Body, &schema.ServerActionCreateImageRequest{})
9397
assert.Equal(t, "dummy-snapshot", *payload.Description)
@@ -99,7 +103,8 @@ func TestStepCreateSnapshot(t *testing.T) {
99103
"action": { "id": 3, "status": "running" }
100104
}`,
101105
},
102-
{Method: "GET", Path: "/actions?id=3&page=1&sort=status&sort=id",
106+
{
107+
Method: "GET", Path: "/actions?id=3&page=1&sort=status&sort=id",
103108
Status: 200,
104109
JSONRaw: `{
105110
"actions": [
@@ -132,7 +137,8 @@ func TestStepCreateSnapshot(t *testing.T) {
132137
state.Put(StateSnapshotIDOld, int64(20))
133138
},
134139
WantRequests: []mockutil.Request{
135-
{Method: "POST", Path: "/servers/8/actions/create_image",
140+
{
141+
Method: "POST", Path: "/servers/8/actions/create_image",
136142
Want: func(t *testing.T, req *http.Request) {
137143
payload := decodeJSONBody(t, req.Body, &schema.ServerActionCreateImageRequest{})
138144
assert.Equal(t, "dummy-snapshot", *payload.Description)
@@ -144,7 +150,8 @@ func TestStepCreateSnapshot(t *testing.T) {
144150
"action": { "id": 3, "status": "running" }
145151
}`,
146152
},
147-
{Method: "GET", Path: "/actions?id=3&page=1&sort=status&sort=id",
153+
{
154+
Method: "GET", Path: "/actions?id=3&page=1&sort=status&sort=id",
148155
Status: 200,
149156
JSONRaw: `{
150157
"actions": [
@@ -153,7 +160,8 @@ func TestStepCreateSnapshot(t *testing.T) {
153160
"meta": { "pagination": { "page": 1 }}
154161
}`,
155162
},
156-
{Method: "DELETE", Path: "/images/20",
163+
{
164+
Method: "DELETE", Path: "/images/20",
157165
Status: 204,
158166
},
159167
},
@@ -176,7 +184,8 @@ func TestStepCreateSnapshot(t *testing.T) {
176184
state.Put(StateSnapshotIDOld, int64(20))
177185
},
178186
WantRequests: []mockutil.Request{
179-
{Method: "POST", Path: "/servers/8/actions/create_image",
187+
{
188+
Method: "POST", Path: "/servers/8/actions/create_image",
180189
Want: func(t *testing.T, req *http.Request) {
181190
payload := decodeJSONBody(t, req.Body, &schema.ServerActionCreateImageRequest{})
182191
assert.Equal(t, "dummy-snapshot", *payload.Description)
@@ -188,7 +197,8 @@ func TestStepCreateSnapshot(t *testing.T) {
188197
"action": { "id": 3, "status": "running" }
189198
}`,
190199
},
191-
{Method: "GET", Path: "/actions?id=3&page=1&sort=status&sort=id",
200+
{
201+
Method: "GET", Path: "/actions?id=3&page=1&sort=status&sort=id",
192202
Status: 200,
193203
JSONRaw: `{
194204
"actions": [
@@ -197,7 +207,8 @@ func TestStepCreateSnapshot(t *testing.T) {
197207
"meta": { "pagination": { "page": 1 }}
198208
}`,
199209
},
200-
{Method: "DELETE", Path: "/images/20",
210+
{
211+
Method: "DELETE", Path: "/images/20",
201212
Status: 400,
202213
},
203214
},
@@ -209,5 +220,14 @@ func TestStepCreateSnapshot(t *testing.T) {
209220
assert.Regexp(t, "Could not delete old snapshot id=20: .*", err.Error())
210221
},
211222
},
223+
{
224+
Name: "skip snapshot creation",
225+
Step: &stepCreateSnapshot{},
226+
SetupStateFunc: func(state multistep.StateBag) {
227+
state.Put(StateServerID, int64(8))
228+
state.Put(StateConfig, &Config{SkipCreateSnapshot: true})
229+
},
230+
WantStepAction: multistep.ActionContinue,
231+
},
212232
})
213233
}

builder/hcloud/step_pre_validate.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ func (s *stepPreValidate) Run(ctx context.Context, state multistep.StateBag) mul
5050
}
5151
}
5252

53+
// Skip snapshot name validation if skip_create_snapshot is set to true.
54+
if c.SkipCreateSnapshot {
55+
return multistep.ActionContinue
56+
}
57+
5358
ui.Say(fmt.Sprintf("Validating snapshot name: %s", s.SnapshotName))
5459

5560
// We would like to ask only for snapshots with a certain name using

builder/hcloud/step_pre_validate_test.go

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,22 @@ func TestStepPreValidate(t *testing.T) {
2525
c.UpgradeServerType = "cpx21"
2626
},
2727
WantRequests: []mockutil.Request{
28-
{Method: "GET", Path: "/server_types?name=cpx11",
28+
{
29+
Method: "GET", Path: "/server_types?name=cpx11",
2930
Status: 200,
3031
JSONRaw: `{
3132
"server_types": [{ "id": 9, "name": "cpx11", "architecture": "x86"}]
3233
}`,
3334
},
34-
{Method: "GET", Path: "/server_types?name=cpx21",
35+
{
36+
Method: "GET", Path: "/server_types?name=cpx21",
3537
Status: 200,
3638
JSONRaw: `{
3739
"server_types": [{ "id": 10, "name": "cpx21", "architecture": "x86"}]
3840
}`,
3941
},
40-
{Method: "GET", Path: "/images?architecture=x86&page=1&type=snapshot",
42+
{
43+
Method: "GET", Path: "/images?architecture=x86&page=1&type=snapshot",
4144
Status: 200,
4245
JSONRaw: `{
4346
"images": []
@@ -64,19 +67,22 @@ func TestStepPreValidate(t *testing.T) {
6467
c.UpgradeServerType = "cpx21"
6568
},
6669
WantRequests: []mockutil.Request{
67-
{Method: "GET", Path: "/server_types?name=cpx11",
70+
{
71+
Method: "GET", Path: "/server_types?name=cpx11",
6872
Status: 200,
6973
JSONRaw: `{
7074
"server_types": [{ "id": 9, "name": "cpx11", "architecture": "x86"}]
7175
}`,
7276
},
73-
{Method: "GET", Path: "/server_types?name=cpx21",
77+
{
78+
Method: "GET", Path: "/server_types?name=cpx21",
7479
Status: 200,
7580
JSONRaw: `{
7681
"server_types": [{ "id": 10, "name": "cpx21", "architecture": "x86"}]
7782
}`,
7883
},
79-
{Method: "GET", Path: "/images?architecture=x86&page=1&type=snapshot",
84+
{
85+
Method: "GET", Path: "/images?architecture=x86&page=1&type=snapshot",
8086
Status: 200,
8187
JSONRaw: `{
8288
"images": [{ "id": 1, "description": "dummy-snapshot"}]
@@ -108,19 +114,22 @@ func TestStepPreValidate(t *testing.T) {
108114
c.UpgradeServerType = "cpx21"
109115
},
110116
WantRequests: []mockutil.Request{
111-
{Method: "GET", Path: "/server_types?name=cpx11",
117+
{
118+
Method: "GET", Path: "/server_types?name=cpx11",
112119
Status: 200,
113120
JSONRaw: `{
114121
"server_types": [{ "id": 9, "name": "cpx11", "architecture": "x86"}]
115122
}`,
116123
},
117-
{Method: "GET", Path: "/server_types?name=cpx21",
124+
{
125+
Method: "GET", Path: "/server_types?name=cpx21",
118126
Status: 200,
119127
JSONRaw: `{
120128
"server_types": [{ "id": 10, "name": "cpx21", "architecture": "x86"}]
121129
}`,
122130
},
123-
{Method: "GET", Path: "/images?architecture=x86&page=1&type=snapshot",
131+
{
132+
Method: "GET", Path: "/images?architecture=x86&page=1&type=snapshot",
124133
Status: 200,
125134
JSONRaw: `{
126135
"images": [{ "id": 1, "description": "dummy-snapshot"}]
@@ -138,5 +147,37 @@ func TestStepPreValidate(t *testing.T) {
138147
assert.Equal(t, int64(1), snapshotIDOld)
139148
},
140149
},
150+
{
151+
Name: "skip snapshot name validation",
152+
Step: &stepPreValidate{
153+
SnapshotName: "dummy-snapshot",
154+
},
155+
SetupConfigFunc: func(c *Config) {
156+
c.UpgradeServerType = "cpx21"
157+
c.SkipCreateSnapshot = true
158+
},
159+
WantRequests: []mockutil.Request{
160+
{
161+
Method: "GET", Path: "/server_types?name=cpx11",
162+
Status: 200,
163+
JSONRaw: `{
164+
"server_types": [{ "id": 9, "name": "cpx11", "architecture": "x86"}]
165+
}`,
166+
},
167+
{
168+
Method: "GET", Path: "/server_types?name=cpx21",
169+
Status: 200,
170+
JSONRaw: `{
171+
"server_types": [{ "id": 10, "name": "cpx21", "architecture": "x86"}]
172+
}`,
173+
},
174+
},
175+
WantStepAction: multistep.ActionContinue,
176+
WantStateFunc: func(t *testing.T, state multistep.StateBag) {
177+
serverType, ok := state.Get(StateServerType).(*hcloud.ServerType)
178+
assert.True(t, ok)
179+
assert.Equal(t, hcloud.ServerType{ID: 9, Name: "cpx11", Architecture: "x86"}, *serverType)
180+
},
181+
},
141182
})
142183
}

docs/builders/hcloud.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ builder.
9292
- `server_labels` (map of key/value strings) - Key/value pair labels to
9393
apply to the created server.
9494

95+
- `skip_create_snapshot` (boolean) - Skips snapshot creation.
96+
9597
- `snapshot_name` (string) - The name of the resulting snapshot that will
9698
appear in your account as image description. Defaults to `packer-{{timestamp}}` (see
9799
[configuration templates](/packer/docs/templates/legacy_json_templates/engine) for more info).

0 commit comments

Comments
 (0)