forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathproxmox.sh
More file actions
executable file
·516 lines (442 loc) · 15.5 KB
/
proxmox.sh
File metadata and controls
executable file
·516 lines (442 loc) · 15.5 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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
#!/usr/bin/env bash
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
parse_json() {
local json_string="$1"
echo "$json_string" | jq '.' > /dev/null || { echo '{"status": "error", "error": "Invalid JSON input"}'; exit 1; }
local -A details
while IFS="=" read -r key value; do
details[$key]="$value"
done < <(echo "$json_string" | jq -r '{
"extension_url": (.externaldetails.extension.url // ""),
"extension_user": (.externaldetails.extension.user // ""),
"extension_token": (.externaldetails.extension.token // ""),
"extension_secret": (.externaldetails.extension.secret // ""),
"host_url": (.externaldetails.host.url // ""),
"host_user": (.externaldetails.host.user // ""),
"host_token": (.externaldetails.host.token // ""),
"host_secret": (.externaldetails.host.secret // ""),
"node": (.externaldetails.host.node // ""),
"network_bridge": (.externaldetails.host.network_bridge // ""),
"verify_tls_certificate": (.externaldetails.host.verify_tls_certificate // "true"),
"vm_name": (.externaldetails.virtualmachine.vm_name // ""),
"template_id": (.externaldetails.virtualmachine.template_id // ""),
"template_type": (.externaldetails.virtualmachine.template_type // ""),
"iso_path": (.externaldetails.virtualmachine.iso_path // ""),
"iso_os_type": (.externaldetails.virtualmachine.iso_os_type // "l26"),
"disk_size_gb": (.externaldetails.virtualmachine.disk_size_gb // "64"),
"storage": (.externaldetails.virtualmachine.storage // "local-lvm"),
"is_full_clone": (.externaldetails.virtualmachine.is_full_clone // "false"),
"snap_name": (.parameters.snap_name // ""),
"snap_description": (.parameters.snap_description // ""),
"snap_save_memory": (.parameters.snap_save_memory // ""),
"vmid": (."cloudstack.vm.details".details.proxmox_vmid // ""),
"vm_internal_name": (."cloudstack.vm.details".name // ""),
"vmmemory": (."cloudstack.vm.details".minRam // ""),
"vmcpus": (."cloudstack.vm.details".cpus // ""),
"vlans": ([."cloudstack.vm.details".nics[]?.broadcastUri // "" | sub("vlan://"; "")] | join(",")),
"mac_addresses": ([."cloudstack.vm.details".nics[]?.mac // ""] | join(","))
} | to_entries | .[] | "\(.key)=\(.value)"')
for key in "${!details[@]}"; do
declare -g "$key=${details[$key]}"
done
# set url, user, token, secret to host values if present, otherwise use extension values
url="${host_url:-$extension_url}"
user="${host_user:-$extension_user}"
token="${host_token:-$extension_token}"
secret="${host_secret:-$extension_secret}"
check_required_fields vm_internal_name url user token secret node
}
urlencode() {
encoded_data=$(python3 -c "import urllib.parse; print(urllib.parse.quote('''$1'''))")
echo "$encoded_data"
}
check_required_fields() {
local missing=()
for varname in "$@"; do
local value="${!varname}"
if [[ -z "$value" ]]; then
missing+=("$varname")
fi
done
if [[ ${#missing[@]} -gt 0 ]]; then
echo "{\"error\":\"Missing required fields: ${missing[*]}\"}"
exit 1
fi
}
validate_name() {
local entity="$1"
local name="$2"
if [[ ! "$name" =~ ^[a-zA-Z0-9-]+$ ]]; then
echo "{\"error\":\"Invalid $entity name '$name'. Only alphanumeric characters and dashes (-) are allowed.\"}"
exit 1
fi
}
call_proxmox_api() {
local method=$1
local path=$2
local data=$3
curl_opts=(
-s
--fail
-X "$method"
-H "Authorization: PVEAPIToken=${user}!${token}=${secret}"
)
if [[ "$verify_tls_certificate" == "false" ]]; then
curl_opts+=(-k)
fi
if [[ -n "$data" ]]; then
curl_opts+=(-d "$data")
fi
response=$(curl "${curl_opts[@]}" "https://${url}:8006/api2/json${path}")
local status=$?
if [[ $status -ne 0 ]]; then
echo "{\"errors\":{\"curl\":\"API call failed with status $status: $(echo "$response" | jq -Rsa . | jq -r .)\"}}"
return $status
fi
echo "$response"
return 0
}
wait_for_proxmox_task() {
local upid="$1"
local timeout="${2:-$wait_time}"
local interval="${3:-1}"
local start_time
start_time=$(date +%s)
while true; do
local now
now=$(date +%s)
if (( now - start_time > timeout )); then
echo '{"status": "error", "error":"Timeout while waiting for async task"}'
exit 1
fi
local status_response
status_response=$(call_proxmox_api GET "/nodes/${node}/tasks/$(urlencode "$upid")/status")
if [[ -z "$status_response" || "$status_response" == *'"errors":'* ]]; then
local msg
msg=$(echo "$status_response" | jq -r '.message // "Unknown error"')
echo "{\"status\": \"error\", \"error\": \"$msg\"}"
exit 1
fi
local task_status
task_status=$(echo "$status_response" | jq -r '.data.status')
if [[ "$task_status" == "stopped" ]]; then
local exit_status
exit_status=$(echo "$status_response" | jq -r '.data.exitstatus')
if [[ "$exit_status" != "OK" ]]; then
echo "{\"error\":\"Task failed with exit status: $exit_status\"}"
exit 1
fi
return 0
fi
sleep "$interval"
done
}
execute_and_wait() {
local method="$1"
local path="$2"
local data="$3"
local response upid msg
response=$(call_proxmox_api "$method" "$path" "$data")
upid=$(echo "$response" | jq -r '.data // ""')
if [[ -z "$upid" ]]; then
msg=$(echo "$response" | jq -r '.message // "Unknown error"')
echo "{\"error\":\"Failed to execute API or retrieve UPID. Message: $msg\"}"
exit 1
fi
wait_for_proxmox_task "$upid"
}
vm_not_present() {
response=$(call_proxmox_api GET "/cluster/nextid?vmid=$vmid")
vmid_result=$(echo "$response" | jq -r '.data // empty')
if [[ "$vmid_result" == "$vmid" ]]; then
return 0
else
return 1
fi
}
prepare() {
response=$(call_proxmox_api GET "/cluster/nextid")
vmid=$(echo "$response" | jq -r '.data // ""')
echo "{\"details\":{\"proxmox_vmid\": \"$vmid\"}}"
}
create() {
if [[ -z "$vm_name" ]]; then
vm_name="$vm_internal_name"
fi
validate_name "VM" "$vm_name"
check_required_fields vmid network_bridge vmcpus vmmemory
if [[ "${template_type^^}" == "ISO" ]]; then
check_required_fields iso_path
local data="vmid=$vmid"
data+="&name=$vm_name"
data+="&ide2=$(urlencode "$iso_path,media=cdrom")"
data+="&ostype=$iso_os_type"
data+="&scsihw=virtio-scsi-single"
data+="&scsi0=$(urlencode "$storage:$disk_size_gb,iothread=on")"
data+="&sockets=1"
data+="&cores=$vmcpus"
data+="&numa=0"
data+="&cpu=x86-64-v2-AES"
data+="&memory=$((vmmemory / 1024 / 1024))"
execute_and_wait POST "/nodes/${node}/qemu/" "$data"
cleanup_vm=1
else
check_required_fields template_id
local data="newid=$vmid"
data+="&name=$vm_name"
clone_flag=$(( is_full_clone == "true" ))
data+="&storage=$storage&full=$clone_flag"
execute_and_wait POST "/nodes/${node}/qemu/${template_id}/clone" "$data"
cleanup_vm=1
data="cores=$vmcpus"
data+="&memory=$((vmmemory / 1024 / 1024))"
execute_and_wait POST "/nodes/${node}/qemu/${vmid}/config" "$data"
fi
IFS=',' read -ra vlan_array <<< "$vlans"
IFS=',' read -ra mac_array <<< "$mac_addresses"
for i in "${!vlan_array[@]}"; do
network="net${i}=$(urlencode "virtio=${mac_array[i]},bridge=${network_bridge},tag=${vlan_array[i]},firewall=0")"
call_proxmox_api PUT "/nodes/${node}/qemu/${vmid}/config/" "$network" > /dev/null
done
execute_and_wait POST "/nodes/${node}/qemu/${vmid}/status/start"
cleanup_vm=0
echo '{"status": "success", "message": "Instance created"}'
}
start() {
execute_and_wait POST "/nodes/${node}/qemu/${vmid}/status/start"
echo '{"status": "success", "message": "Instance started"}'
}
delete() {
if vm_not_present; then
echo '{"status": "success", "message": "Instance deleted"}'
return 0
fi
execute_and_wait DELETE "/nodes/${node}/qemu/${vmid}"
echo '{"status": "success", "message": "Instance deleted"}'
}
stop() {
if vm_not_present; then
echo '{"status": "success", "message": "Instance stopped"}'
return 0
fi
execute_and_wait POST "/nodes/${node}/qemu/${vmid}/status/stop"
echo '{"status": "success", "message": "Instance stopped"}'
}
reboot() {
execute_and_wait POST "/nodes/${node}/qemu/${vmid}/status/reboot"
echo '{"status": "success", "message": "Instance rebooted"}'
}
status() {
local status_response vm_status powerstate
status_response=$(call_proxmox_api GET "/nodes/${node}/qemu/${vmid}/status/current")
vm_status=$(echo "$status_response" | jq -r '.data.status')
case "$vm_status" in
running) powerstate="poweron" ;;
stopped) powerstate="poweroff" ;;
*) powerstate="unknown" ;;
esac
echo "{\"status\": \"success\", \"power_state\": \"$powerstate\"}"
}
get_node_host() {
check_required_fields node
local net_json host
if ! net_json="$(call_proxmox_api GET "/nodes/${node}/network")"; then
echo ""
return 1
fi
# Prefer a static non-bridge IP
host="$(echo "$net_json" | jq -r '
.data
| map(select(
(.type // "") != "bridge" and
(.type // "") != "bond" and
(.method // "") == "static" and
((.address // .cidr // "") != "")
))
| map(.address // (.cidr | split("/")[0]))
| .[0] // empty
' 2>/dev/null)"
# Fallback: first interface with a CIDR
if [[ -z "$host" ]]; then
host="$(echo "$net_json" | jq -r '
.data
| map(select((.cidr // "") != ""))
| map(.cidr | split("/")[0])
| .[0] // empty
' 2>/dev/null)"
fi
echo "$host"
}
get_console() {
check_required_fields node vmid
local api_resp port ticket
if ! api_resp="$(call_proxmox_api POST "/nodes/${node}/qemu/${vmid}/vncproxy")"; then
echo "$api_resp" | jq -c '{status:"error", error:(.errors.curl // (.errors|tostring))}'
exit 1
fi
port="$(echo "$api_resp" | jq -re '.data.port // empty' 2>/dev/null || true)"
ticket="$(echo "$api_resp" | jq -re '.data.ticket // empty' 2>/dev/null || true)"
if [[ -z "$port" || -z "$ticket" ]]; then
jq -n --arg raw "$api_resp" \
'{status:"error", error:"Proxmox response missing port/ticket", upstream:$raw}'
exit 1
fi
# Derive host from node’s network info
local host
host="$(get_node_host)"
if [[ -z "$host" ]]; then
jq -n --arg msg "Could not determine host IP for node $node" \
'{status:"error", error:$msg}'
exit 1
fi
jq -n \
--arg host "$host" \
--arg port "$port" \
--arg password "$ticket" \
--argjson passwordonetimeuseonly true \
'{
status: "success",
message: "Console retrieved",
console: {
host: $host,
port: $port,
password: $password,
passwordonetimeuseonly: $passwordonetimeuseonly,
protocol: "vnc"
}
}'
}
list_snapshots() {
snapshot_response=$(call_proxmox_api GET "/nodes/${node}/qemu/${vmid}/snapshot")
echo "$snapshot_response" | jq '
def to_date:
if . == "-" then "-"
elif . == null then "-"
else (. | tonumber | strftime("%Y-%m-%d %H:%M:%S"))
end;
{
status: "success",
printmessage: "true",
message: [.data[] | {
name: .name,
snaptime: ((.snaptime // "-") | to_date),
description: .description,
parent: (.parent // "-"),
vmstate: (.vmstate // "-")
}]
}
'
}
create_snapshot() {
check_required_fields snap_name
validate_name "Snapshot" "$snap_name"
local data vmstate
data="snapname=$snap_name"
if [[ -n "$snap_description" ]]; then
data+="&description=$snap_description"
fi
if [[ -n "$snap_save_memory" && "$snap_save_memory" == "true" ]]; then
vmstate="1"
else
vmstate="0"
fi
data+="&vmstate=$vmstate"
execute_and_wait POST "/nodes/${node}/qemu/${vmid}/snapshot" "$data"
echo '{"status": "success", "message": "Instance Snapshot created"}'
}
restore_snapshot() {
check_required_fields snap_name
validate_name "Snapshot" "$snap_name"
execute_and_wait POST "/nodes/${node}/qemu/${vmid}/snapshot/${snap_name}/rollback"
status_response=$(call_proxmox_api GET "/nodes/${node}/qemu/${vmid}/status/current")
vm_status=$(echo "$status_response" | jq -r '.data.status')
if [ "$vm_status" = "stopped" ];then
execute_and_wait POST "/nodes/${node}/qemu/${vmid}/status/start"
fi
echo '{"status": "success", "message": "Instance Snapshot restored"}'
}
delete_snapshot() {
check_required_fields snap_name
validate_name "Snapshot" "$snap_name"
execute_and_wait DELETE "/nodes/${node}/qemu/${vmid}/snapshot/${snap_name}"
echo '{"status": "success", "message": "Instance Snapshot deleted"}'
}
action=$1
parameters_file="$2"
wait_time=$3
if [[ -z "$action" || -z "$parameters_file" ]]; then
echo '{"status": "error", "error": "Missing required arguments"}'
exit 1
fi
if [[ ! -r "$parameters_file" ]]; then
echo '{"status": "error", "error": "File not found or unreadable"}'
exit 1
fi
# Read file content as parameters (assumes space-separated arguments)
parameters=$(<"$parameters_file")
parse_json "$parameters" || exit 1
cleanup_vm=0
cleanup() {
if (( cleanup_vm == 1 )); then
execute_and_wait DELETE "/nodes/${node}/qemu/${vmid}"
fi
}
trap cleanup EXIT
case $action in
prepare)
prepare
;;
create)
create
;;
delete)
delete
;;
start)
start
;;
stop)
stop
;;
reboot)
reboot
;;
status)
status
;;
getconsole)
get_console
;;
ListSnapshots)
list_snapshots
;;
CreateSnapshot)
create_snapshot
;;
RestoreSnapshot)
restore_snapshot
;;
DeleteSnapshot)
delete_snapshot
;;
*)
echo '{"status": "error", "error": "Invalid action"}'
exit 1
;;
esac
exit 0