forked from hcloud-k8s/terraform-hcloud-kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvswitch.tf
More file actions
481 lines (411 loc) · 17.8 KB
/
Copy pathvswitch.tf
File metadata and controls
481 lines (411 loc) · 17.8 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
resource "terraform_data" "vswitch" {
count = local.bare_metal_enabled && var.hcloud_robot_user != null && var.hcloud_vswitch_id == null ? 1 : 0
triggers_replace = {
name = var.cluster_name
vlan = var.hcloud_vswitch_vlan_id
robot_password = var.hcloud_robot_password
}
input = {
robot_api_url = var.hcloud_robot_api_url
robot_user = var.hcloud_robot_user
name = var.cluster_name
vlan = var.hcloud_vswitch_vlan_id
}
provisioner "local-exec" {
quiet = true
command = <<-EOT
set -eu
response_file=$(mktemp)
cleanup() { rm -f "$response_file"; }
trap 'cleanup' EXIT
trap 'exit 1' HUP INT TERM QUIT PIPE
status=$(curl -sS -o "$response_file" -w '%%{http_code}' \
-u "$ROBOT_USER:$ROBOT_PASSWORD" \
"${self.input.robot_api_url}/vswitch" || true)
case "$status" in
200)
;;
404)
printf '[]' > "$response_file"
;;
*)
printf 'Unexpected Robot API status while listing vSwitches: %s\n' "$status" >&2
exit 1
;;
esac
vswitch_id=$(
jq -r --arg name "${self.input.name}" --argjson vlan "${self.input.vlan}" '
map(select(.name == $name and .vlan == $vlan and .cancelled == false)) |
if length == 0 then ""
elif length == 1 then .[0].id
else error("multiple matching vSwitches found")
end
' "$response_file"
)
if [ -n "$vswitch_id" ]; then
exit 0
fi
curl -sSf \
-u "$ROBOT_USER:$ROBOT_PASSWORD" \
--data-urlencode "name=${self.input.name}" \
--data-urlencode "vlan=${self.input.vlan}" \
"${self.input.robot_api_url}/vswitch" >/dev/null
EOT
environment = {
ROBOT_USER = self.input.robot_user
ROBOT_PASSWORD = nonsensitive(self.triggers_replace.robot_password)
}
}
provisioner "local-exec" {
when = destroy
quiet = true
command = <<-EOT
set -eu
response_file=$(mktemp)
cleanup() { rm -f "$response_file"; }
trap 'cleanup' EXIT
trap 'exit 1' HUP INT TERM QUIT PIPE
status=$(curl -sS -o "$response_file" -w '%%{http_code}' \
-u "$ROBOT_USER:$ROBOT_PASSWORD" \
"${self.input.robot_api_url}/vswitch" || true)
case "$status" in
200)
;;
404)
printf '[]' > "$response_file"
;;
*)
printf 'Unexpected Robot API status while listing vSwitches: %s\n' "$status" >&2
exit 1
;;
esac
vswitch_id=$(
jq -r --arg name "${self.input.name}" --argjson vlan "${self.input.vlan}" '
map(select(.name == $name and .vlan == $vlan and .cancelled == false)) |
if length == 0 then ""
elif length == 1 then .[0].id
else error("multiple matching vSwitches found")
end
' "$response_file"
)
if [ -z "$vswitch_id" ]; then
exit 0
fi
status=$(curl -sS -o /dev/null -w '%%{http_code}' \
-u "$ROBOT_USER:$ROBOT_PASSWORD" \
-X DELETE \
--data-urlencode "cancellation_date=now" \
"${self.input.robot_api_url}/vswitch/$vswitch_id" || true)
case "$status" in
200|204|404|409)
exit 0
;;
*)
printf 'Unexpected Robot API status while deleting vSwitch %s: %s\n' "$vswitch_id" "$status" >&2
exit 1
;;
esac
EOT
environment = {
ROBOT_USER = self.input.robot_user
ROBOT_PASSWORD = nonsensitive(self.triggers_replace.robot_password)
}
}
}
data "external" "vswitch" {
count = local.bare_metal_enabled && var.hcloud_robot_user != null ? 1 : 0
depends_on = [terraform_data.vswitch]
program = [
"sh", "-c", <<-EOT
set -eu
input=$(cat)
robot_api_url=$(printf '%s' "$input" | jq -r '.robot_api_url')
robot_user=$(printf '%s' "$input" | jq -r '.robot_user')
robot_password=$(printf '%s' "$input" | jq -r '.robot_password')
id=$(printf '%s' "$input" | jq -r '.id')
name=$(printf '%s' "$input" | jq -r '.name')
vlan=$(printf '%s' "$input" | jq -r '.vlan')
response_file=$(mktemp)
cleanup() { rm -f "$response_file"; }
trap 'cleanup' EXIT
trap 'exit 1' HUP INT TERM QUIT PIPE
status=$(curl -sS -o "$response_file" -w '%%{http_code}' \
-u "$robot_user:$robot_password" \
"$robot_api_url/vswitch" || true)
case "$status" in
200)
;;
404)
printf '[]' > "$response_file"
;;
*)
printf 'Unexpected Robot API status while looking up vSwitch: %s\n' "$status" >&2
exit 1
;;
esac
if [ "$id" != "null" ]; then
vswitch=$(
jq -r --arg id "$id" '
map(select((.id | tostring) == $id and .cancelled == false)) |
if length == 1 then .[0]
elif length == 0 then error("vSwitch not found")
else error("multiple matching vSwitches found")
end
' "$response_file"
)
else
vswitch=$(
jq -r --arg name "$name" --argjson vlan "$vlan" '
map(select(.name == $name and .vlan == $vlan and .cancelled == false)) |
if length == 1 then .[0]
elif length == 0 then error("vSwitch not found")
else error("multiple matching vSwitches found")
end
' "$response_file"
)
fi
printf '%s' "$vswitch" | jq '{id: (.id | tostring), vlan: (.vlan | tostring)}'
EOT
]
query = {
robot_api_url = var.hcloud_robot_api_url
robot_user = var.hcloud_robot_user
robot_password = var.hcloud_robot_password
id = var.hcloud_vswitch_id != null ? tostring(var.hcloud_vswitch_id) : null
name = var.cluster_name
vlan = var.hcloud_vswitch_vlan_id != null ? tostring(var.hcloud_vswitch_vlan_id) : null
}
}
locals {
hcloud_vswitch_id = var.hcloud_vswitch_id != null ? var.hcloud_vswitch_id : (
local.bare_metal_enabled && var.hcloud_robot_user != null ? tonumber(nonsensitive(data.external.vswitch[0].result.id)) : null
)
hcloud_vswitch_vlan_id = local.bare_metal_enabled && var.hcloud_robot_user != null ? tonumber(nonsensitive(data.external.vswitch[0].result.vlan)) : var.hcloud_vswitch_vlan_id
}
resource "terraform_data" "vswitch_attachment" {
count = local.bare_metal_enabled && var.hcloud_robot_user != null ? 1 : 0
triggers_replace = {
vswitch_id = local.hcloud_vswitch_id
server_numbers = sort([for server in local.bare_metal_servers : tostring(server.number)])
}
provisioner "local-exec" {
quiet = true
command = <<-EOT
set -eu
printf 'Checking vSwitch %s for bare metal servers to attach\n' "${self.triggers_replace.vswitch_id}"
response_file=$(mktemp)
curl_config=$(mktemp)
cleanup() { rm -f "$response_file" "$curl_config"; }
trap 'cleanup' EXIT
trap 'exit 1' HUP INT TERM QUIT PIPE
status=$(curl -sS -o "$response_file" -w '%%{http_code}' \
-u "$ROBOT_USER:$ROBOT_PASSWORD" \
"${var.hcloud_robot_api_url}/vswitch/${self.triggers_replace.vswitch_id}" || true)
case "$status" in
200)
;;
*)
printf 'Unexpected Robot API status while checking vSwitch %s server attachments: %s\n' "${self.triggers_replace.vswitch_id}" "$status" >&2
cat "$response_file" >&2
exit 1
;;
esac
server_numbers_to_attach=$(jq -c --argjson server_numbers "$SERVER_NUMBERS" '
[$server_numbers[] as $server_number | select(([.server[]? | (.server_number | tostring)] | index($server_number) | not)) | $server_number]
' "$response_file")
server_numbers_to_attach_label=$(printf '%s' "$server_numbers_to_attach" | jq -r 'join(", ")')
if [ "$server_numbers_to_attach" = "[]" ]; then
printf 'No bare metal servers to attach to vSwitch %s\n' "${self.triggers_replace.vswitch_id}"
else
printf 'Attaching bare metal servers %s to vSwitch %s\n' "$server_numbers_to_attach_label" "${self.triggers_replace.vswitch_id}"
printf '%s' "$server_numbers_to_attach" | jq -r '.[] | "data-urlencode = \"server[]=" + . + "\""' > "$curl_config"
for attempt in $(seq 1 100); do
status=$(curl -sS -o "$response_file" -w '%%{http_code}' \
-u "$ROBOT_USER:$ROBOT_PASSWORD" \
--config "$curl_config" \
"${var.hcloud_robot_api_url}/vswitch/${self.triggers_replace.vswitch_id}/server" || true)
case "$status" in
200|201|202|204)
break
;;
409)
printf 'vSwitch %s server attachment for servers %s is still in process, retrying (%s/100)\n' "${self.triggers_replace.vswitch_id}" "$server_numbers_to_attach_label" "$attempt"
sleep 10
;;
*)
printf 'Unexpected Robot API status while attaching bare metal servers %s to vSwitch %s: %s\n' "$server_numbers_to_attach_label" "${self.triggers_replace.vswitch_id}" "$status" >&2
cat "$response_file" >&2
exit 1
;;
esac
done
if [ "$status" != "200" ] && [ "$status" != "201" ] && [ "$status" != "202" ] && [ "$status" != "204" ]; then
printf 'Attaching bare metal servers %s to vSwitch %s did not complete after retries, last status: %s\n' "$server_numbers_to_attach_label" "${self.triggers_replace.vswitch_id}" "$status" >&2
exit 1
fi
printf 'Waiting 60 seconds for vSwitch %s server attachment to apply\n' "${self.triggers_replace.vswitch_id}"
sleep 60
fi
for attempt in $(seq 1 100); do
status=$(curl -sS -o "$response_file" -w '%%{http_code}' \
-u "$ROBOT_USER:$ROBOT_PASSWORD" \
"${var.hcloud_robot_api_url}/vswitch/${self.triggers_replace.vswitch_id}" || true)
case "$status" in
200)
attachment_status=$(jq -r --argjson server_numbers "$SERVER_NUMBERS" '
[.server[]? | select((.server_number | tostring) as $server_number | $server_numbers | index($server_number)) | .status] as $statuses |
if ($statuses | length) != ($server_numbers | length) then "missing"
elif any($statuses[]; . == "failed") then "failed"
elif all($statuses[]; . == "ready") then "ready"
else "in process"
end
' "$response_file")
case "$attachment_status" in
ready)
break
;;
"in process"|missing)
printf 'vSwitch %s server attachment for servers %s is not ready yet (%s), waiting (%s/100)\n' "${self.triggers_replace.vswitch_id}" "$SERVER_NUMBERS_LABEL" "$attachment_status" "$attempt"
sleep 10
;;
failed)
printf 'vSwitch %s server attachment failed for servers %s\n' "${self.triggers_replace.vswitch_id}" "$SERVER_NUMBERS_LABEL" >&2
exit 1
;;
*)
printf 'Unexpected vSwitch %s server attachment status for servers %s: %s\n' "${self.triggers_replace.vswitch_id}" "$SERVER_NUMBERS_LABEL" "$attachment_status" >&2
exit 1
;;
esac
;;
*)
printf 'Unexpected Robot API status while checking vSwitch %s server attachment for servers %s: %s\n' "${self.triggers_replace.vswitch_id}" "$SERVER_NUMBERS_LABEL" "$status" >&2
cat "$response_file" >&2
exit 1
;;
esac
done
if [ "$attachment_status" != "ready" ]; then
printf 'vSwitch %s server attachment for servers %s did not become ready after retries, last status: %s\n' "${self.triggers_replace.vswitch_id}" "$SERVER_NUMBERS_LABEL" "$attachment_status" >&2
exit 1
fi
printf 'Bare metal servers %s attached to vSwitch %s\n' "$SERVER_NUMBERS_LABEL" "${self.triggers_replace.vswitch_id}"
EOT
environment = {
ROBOT_USER = var.hcloud_robot_user
ROBOT_PASSWORD = nonsensitive(var.hcloud_robot_password)
SERVER_NUMBERS = jsonencode(self.triggers_replace.server_numbers)
SERVER_NUMBERS_LABEL = join(", ", self.triggers_replace.server_numbers)
}
}
provisioner "local-exec" {
quiet = true
command = <<-EOT
set -eu
printf 'Checking vSwitch %s for bare metal servers to detach\n' "${self.triggers_replace.vswitch_id}"
response_file=$(mktemp)
curl_config=$(mktemp)
cleanup() { rm -f "$response_file" "$curl_config"; }
trap 'cleanup' EXIT
trap 'exit 1' HUP INT TERM QUIT PIPE
status=$(curl -sS -o "$response_file" -w '%%{http_code}' \
-u "$ROBOT_USER:$ROBOT_PASSWORD" \
"${var.hcloud_robot_api_url}/vswitch/${self.triggers_replace.vswitch_id}" || true)
case "$status" in
200)
;;
*)
printf 'Unexpected Robot API status while checking vSwitch %s server attachments: %s\n' "${self.triggers_replace.vswitch_id}" "$status" >&2
cat "$response_file" >&2
exit 1
;;
esac
server_numbers_to_detach=$(jq -c --argjson server_numbers "$SERVER_NUMBERS" '
[.server[]? | (.server_number | tostring) as $server_number | select(($server_numbers | index($server_number) | not)) | $server_number]
' "$response_file")
server_numbers_to_detach_label=$(printf '%s' "$server_numbers_to_detach" | jq -r 'join(", ")')
if [ "$server_numbers_to_detach" = "[]" ]; then
printf 'No bare metal servers to detach from vSwitch %s\n' "${self.triggers_replace.vswitch_id}"
exit 0
fi
printf 'Detaching bare metal servers %s from vSwitch %s\n' "$server_numbers_to_detach_label" "${self.triggers_replace.vswitch_id}"
printf '%s' "$server_numbers_to_detach" | jq -r '.[] | "data-urlencode = \"server[]=" + . + "\""' > "$curl_config"
for attempt in $(seq 1 100); do
status=$(curl -sS -o "$response_file" -w '%%{http_code}' \
-u "$ROBOT_USER:$ROBOT_PASSWORD" \
-X DELETE \
--config "$curl_config" \
"${var.hcloud_robot_api_url}/vswitch/${self.triggers_replace.vswitch_id}/server" || true)
case "$status" in
200|202|204)
break
;;
409)
printf 'vSwitch %s server cleanup for servers %s is still in process, retrying (%s/100)\n' "${self.triggers_replace.vswitch_id}" "$server_numbers_to_detach_label" "$attempt"
sleep 10
;;
*)
printf 'Unexpected Robot API status while detaching bare metal servers %s from vSwitch %s: %s\n' "$server_numbers_to_detach_label" "${self.triggers_replace.vswitch_id}" "$status" >&2
cat "$response_file" >&2
exit 1
;;
esac
done
if [ "$status" != "200" ] && [ "$status" != "202" ] && [ "$status" != "204" ]; then
printf 'Detaching bare metal servers %s from vSwitch %s did not complete after retries, last status: %s\n' "$server_numbers_to_detach_label" "${self.triggers_replace.vswitch_id}" "$status" >&2
exit 1
fi
printf 'Waiting 60 seconds for vSwitch %s server cleanup to apply\n' "${self.triggers_replace.vswitch_id}"
sleep 60
for attempt in $(seq 1 100); do
status=$(curl -sS -o "$response_file" -w '%%{http_code}' \
-u "$ROBOT_USER:$ROBOT_PASSWORD" \
"${var.hcloud_robot_api_url}/vswitch/${self.triggers_replace.vswitch_id}" || true)
case "$status" in
200)
cleanup_status=$(jq -r --argjson server_numbers_to_detach "$server_numbers_to_detach" '
[.server[]? | select((.server_number | tostring) as $server_number | ($server_numbers_to_detach | index($server_number))) | .status] as $statuses |
if ($statuses | length) == 0 then "removed"
elif any($statuses[]; . == "failed") then "failed"
else "in process"
end
' "$response_file")
case "$cleanup_status" in
removed)
break
;;
"in process")
printf 'vSwitch %s server cleanup for servers %s is not done yet, waiting (%s/100)\n' "${self.triggers_replace.vswitch_id}" "$server_numbers_to_detach_label" "$attempt"
sleep 10
;;
failed)
printf 'vSwitch %s server cleanup failed for servers %s\n' "${self.triggers_replace.vswitch_id}" "$server_numbers_to_detach_label" >&2
exit 1
;;
*)
printf 'Unexpected vSwitch %s server cleanup status for servers %s: %s\n' "${self.triggers_replace.vswitch_id}" "$server_numbers_to_detach_label" "$cleanup_status" >&2
exit 1
;;
esac
;;
*)
printf 'Unexpected Robot API status while checking vSwitch %s server cleanup for servers %s: %s\n' "${self.triggers_replace.vswitch_id}" "$server_numbers_to_detach_label" "$status" >&2
cat "$response_file" >&2
exit 1
;;
esac
done
if [ "$cleanup_status" != "removed" ]; then
printf 'vSwitch %s server cleanup for servers %s did not complete after retries, last status: %s\n' "${self.triggers_replace.vswitch_id}" "$server_numbers_to_detach_label" "$cleanup_status" >&2
exit 1
fi
printf 'Bare metal servers %s detached from vSwitch %s\n' "$server_numbers_to_detach_label" "${self.triggers_replace.vswitch_id}"
EOT
environment = {
ROBOT_USER = var.hcloud_robot_user
ROBOT_PASSWORD = nonsensitive(var.hcloud_robot_password)
SERVER_NUMBERS = jsonencode(self.triggers_replace.server_numbers)
}
}
depends_on = [data.external.vswitch]
}