Skip to content

Commit a481e89

Browse files
michelroegl-brunnerbvdberg01Rögl-Brunner Michel
authored
[API] Add API to vms (#2021)
* add API to debian-vm.sh * [API] Add API calls to VMs * [API] Add API calls to VMs * [API] Add API calls to VMs * [API] Add API calls to VMs * [API] Add API calls to VMs * debian-vm.sh Co-authored-by: bvdberg01 <[email protected]> * updates --------- Co-authored-by: bvdberg01 <[email protected]> Co-authored-by: Rögl-Brunner Michel <[email protected]>
1 parent b9094e3 commit a481e89

File tree

13 files changed

+215
-17
lines changed

13 files changed

+215
-17
lines changed

misc/api.func

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ EOF
4646

4747
post_to_api_vm() {
4848

49+
if [[ ! -f /usr/local/community-scripts/diagnostics ]]; then
50+
return
51+
fi
4952
DIAGNOSTICS=$(grep -i "^DIAGNOSTICS=" /usr/local/community-scripts/diagnostics | awk -F'=' '{print $2}')
5053
if ! command -v curl &> /dev/null; then
5154
return
@@ -55,7 +58,6 @@ post_to_api_vm() {
5558
return
5659
fi
5760

58-
5961
if [ -z "$RANDOM_UUID" ]; then
6062
return
6163
fi
@@ -114,8 +116,8 @@ EOF
114116
)
115117

116118
RESPONSE=$(curl -s -o response.txt -w "%{http_code}" -L -X POST "$API_URL" --post301 --post302 \
117-
-H "Content-Type: application/json" \
118-
-d "$JSON_PAYLOAD") || true
119+
-H "Content-Type: application/json" \
120+
-d "$JSON_PAYLOAD") || true
119121

120122
POST_UPDATE_DONE=true
121123
}

vm/archlinux-vm.sh

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
# Author: MickLesk (CanbiZ)
55
# License: MIT | https://github.com/community-scripts/ProxmoxVE/raw/main/LICENSE
66

7+
source /dev/stdin <<< $(wget -qLO - https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/misc/api.func)
8+
9+
710
function header_info {
811
clear
912
cat <<"EOF"
@@ -17,6 +20,12 @@ EOF
1720
}
1821
header_info
1922
echo -e "\n Loading..."
23+
#API VARIABLES
24+
RANDOM_UUID="$(cat /proc/sys/kernel/random/uuid)"
25+
METHOD=""
26+
NSAPP="arch-linux-vm"
27+
var_os="arch-linux"
28+
var_version=" "
2029
GEN_MAC=02:$(openssl rand -hex 5 | awk '{print toupper($0)}' | sed 's/\(..\)/\1:/g; s/.$//')
2130
NEXTID=$(pvesh get /cluster/nextid)
2231

@@ -56,10 +65,13 @@ THIN="discard=on,ssd=1,"
5665
set -e
5766
trap 'error_handler $LINENO "$BASH_COMMAND"' ERR
5867
trap cleanup EXIT
68+
trap 'post_update_to_api "failed" "INTERRUPTED"' SIGINT
69+
trap 'post_update_to_api "failed" "TERMINATED"' SIGTERM
5970
function error_handler() {
6071
local exit_code="$?"
6172
local line_number="$1"
6273
local command="$2"
74+
post_update_to_api "failed" "${commad}"
6375
local error_message="${RD}[ERROR]${CL} in line ${RD}$line_number${CL}: exit code ${RD}$exit_code${CL}: while executing command ${YW}$command${CL}"
6476
echo -e "\n$error_message\n"
6577
cleanup_vmid
@@ -111,7 +123,7 @@ function check_root() {
111123
}
112124

113125
function pve_check() {
114-
if ! pveversion | grep -Eq "pve-manager/8\.[1-3](\.[0-9]+)*"; then
126+
if ! pveversion | grep -Eq "pve-manager/8\.[1-3](\.[0-9]+)*"; then
115127
msg_error "${CROSS}${RD}This version of Proxmox Virtual Environment is not supported"
116128
echo -e "Requires Proxmox Virtual Environment Version 8.1 or later."
117129
echo -e "Exiting..."
@@ -164,6 +176,7 @@ function default_settings() {
164176
VLAN=""
165177
MTU=""
166178
START_VM="yes"
179+
METHOD="default"
167180
echo -e "${CONTAINERID}${BOLD}${DGN}Virtual Machine ID: ${BGN}${VMID}${CL}"
168181
echo -e "${CONTAINERTYPE}${BOLD}${DGN}Machine Type: ${BGN}i440fx${CL}"
169182
echo -e "${DISKSIZE}${BOLD}${DGN}Disk Size: ${BGN}${DISK_SIZE}${CL}"
@@ -181,6 +194,7 @@ function default_settings() {
181194
}
182195

183196
function advanced_settings() {
197+
METHOD="advanced"
184198
while true; do
185199
if VMID=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set Virtual Machine ID" 8 58 $NEXTID --title "VIRTUAL MACHINE ID" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
186200
if [ -z "$VMID" ]; then
@@ -377,6 +391,7 @@ arch_check
377391
pve_check
378392
ssh_check
379393
start_script
394+
post_to_api_vm
380395

381396
msg_info "Validating Storage"
382397
while read -r line; do
@@ -493,5 +508,6 @@ if [ "$START_VM" == "yes" ]; then
493508
qm start $VMID
494509
msg_ok "Started Arch Linux VM"
495510
fi
511+
post_update_to_api "done" "none"
496512

497513
msg_ok "Completed Successfully!\n"

vm/debian-vm.sh

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
# Author: MickLesk (CanbiZ)
55
# License: MIT | https://github.com/community-scripts/ProxmoxVE/raw/main/LICENSE
66

7+
source /dev/stdin <<< $(wget -qLO - https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/misc/api.func)
8+
79
function header_info {
810
clear
911
cat <<"EOF"
@@ -19,6 +21,11 @@ header_info
1921
echo -e "\n Loading..."
2022
GEN_MAC=02:$(openssl rand -hex 5 | awk '{print toupper($0)}' | sed 's/\(..\)/\1:/g; s/.$//')
2123
NEXTID=$(pvesh get /cluster/nextid)
24+
RANDOM_UUID="$(cat /proc/sys/kernel/random/uuid)"
25+
METHOD=""
26+
NSAPP="debian12vm"
27+
var_os="debian"
28+
var_version="12"
2229

2330
YW=$(echo "\033[33m")
2431
BL=$(echo "\033[36m")
@@ -56,11 +63,14 @@ THIN="discard=on,ssd=1,"
5663
set -e
5764
trap 'error_handler $LINENO "$BASH_COMMAND"' ERR
5865
trap cleanup EXIT
66+
trap 'post_update_to_api "failed" "INTERRUPTED"' SIGINT
67+
trap 'post_update_to_api "failed" "TERMINATED"' SIGTERM
5968
function error_handler() {
6069
local exit_code="$?"
6170
local line_number="$1"
6271
local command="$2"
6372
local error_message="${RD}[ERROR]${CL} in line ${RD}$line_number${CL}: exit code ${RD}$exit_code${CL}: while executing command ${YW}$command${CL}"
73+
post_update_to_api "failed" "${command}"
6474
echo -e "\n$error_message\n"
6575
cleanup_vmid
6676
}
@@ -74,6 +84,7 @@ function cleanup_vmid() {
7484

7585
function cleanup() {
7686
popd >/dev/null
87+
post_update_to_api "done" "none"
7788
rm -rf $TEMP_DIR
7889
}
7990

@@ -111,7 +122,7 @@ function check_root() {
111122
}
112123

113124
function pve_check() {
114-
if ! pveversion | grep -Eq "pve-manager/8\.[1-3](\.[0-9]+)*"; then
125+
if ! pveversion | grep -Eq "pve-manager/8\.[1-3](\.[0-9]+)*"; then
115126
msg_error "${CROSS}${RD}This version of Proxmox Virtual Environment is not supported"
116127
echo -e "Requires Proxmox Virtual Environment Version 8.1 or later."
117128
echo -e "Exiting..."
@@ -164,6 +175,7 @@ function default_settings() {
164175
VLAN=""
165176
MTU=""
166177
START_VM="yes"
178+
METHOD="default"
167179
echo -e "${CONTAINERID}${BOLD}${DGN}Virtual Machine ID: ${BGN}${VMID}${CL}"
168180
echo -e "${CONTAINERTYPE}${BOLD}${DGN}Machine Type: ${BGN}i440fx${CL}"
169181
echo -e "${DISKSIZE}${BOLD}${DGN}Disk Size: ${BGN}${DISK_SIZE}${CL}"
@@ -181,6 +193,7 @@ function default_settings() {
181193
}
182194

183195
function advanced_settings() {
196+
METHOD="advanced"
184197
while true; do
185198
if VMID=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set Virtual Machine ID" 8 58 $NEXTID --title "VIRTUAL MACHINE ID" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
186199
if [ -z "$VMID" ]; then
@@ -378,6 +391,8 @@ pve_check
378391
ssh_check
379392
start_script
380393

394+
post_to_api_vm
395+
381396
msg_info "Validating Storage"
382397
while read -r line; do
383398
TAG=$(echo $line | awk '{print $1}')
@@ -439,7 +454,7 @@ done
439454

440455
msg_info "Creating a Debian 12 VM"
441456
qm create $VMID -agent 1${MACHINE} -tablet 0 -localtime 1 -bios ovmf${CPU_TYPE} -cores $CORE_COUNT -memory $RAM_SIZE \
442-
-name $HN -tags proxmox-helper-scripts -net0 virtio,bridge=$BRG,macaddr=$MAC$VLAN$MTU -onboot 1 -ostype l26 -scsihw virtio-scsi-pci
457+
-name $HN -tags community-script -net0 virtio,bridge=$BRG,macaddr=$MAC$VLAN$MTU -onboot 1 -ostype l26 -scsihw virtio-scsi-pci
443458
pvesm alloc $STORAGE $VMID $DISK0 4M 1>&/dev/null
444459
qm importdisk $VMID ${FILE} $STORAGE ${DISK_IMPORT:-} 1>&/dev/null
445460
qm set $VMID \

vm/docker-vm.sh

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
# Author: thost96 (thost96)
55
# License: MIT | https://github.com/community-scripts/ProxmoxVE/raw/main/LICENSE
66

7+
source /dev/stdin <<< $(wget -qLO - https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/misc/api.func)
8+
79
function header_info {
810
clear
911
cat <<"EOF"
@@ -19,6 +21,12 @@ header_info
1921
echo -e "\n Loading..."
2022
GEN_MAC=02:$(openssl rand -hex 5 | awk '{print toupper($0)}' | sed 's/\(..\)/\1:/g; s/.$//')
2123
NEXTID=$(pvesh get /cluster/nextid)
24+
RANDOM_UUID="$(cat /proc/sys/kernel/random/uuid)"
25+
METHOD=""
26+
NSAPP="debian12vm"
27+
var_os="debian"
28+
var_version="12"
29+
DISK_SIZE="8G"
2230

2331
YW=$(echo "\033[33m")
2432
BL=$(echo "\033[36m")
@@ -36,10 +44,13 @@ THIN="discard=on,ssd=1,"
3644
set -e
3745
trap 'error_handler $LINENO "$BASH_COMMAND"' ERR
3846
trap cleanup EXIT
47+
trap 'post_update_to_api "failed" "INTERRUPTED"' SIGINT
48+
trap 'post_update_to_api "failed" "TERMINATED"' SIGTERM
3949
function error_handler() {
4050
local exit_code="$?"
4151
local line_number="$1"
4252
local command="$2"
53+
post_update_to_api "failed" "${command}"
4354
local error_message="${RD}[ERROR]${CL} in line ${RD}$line_number${CL}: exit code ${RD}$exit_code${CL}: while executing command ${YW}$command${CL}"
4455
echo -e "\n$error_message\n"
4556
cleanup_vmid
@@ -91,7 +102,7 @@ function check_root() {
91102
}
92103

93104
function pve_check() {
94-
if ! pveversion | grep -Eq "pve-manager/8\.[1-3](\.[0-9]+)*"; then
105+
if ! pveversion | grep -Eq "pve-manager/8\.[1-3](\.[0-9]+)*"; then
95106
msg_error "This version of Proxmox Virtual Environment is not supported"
96107
echo -e "Requires Proxmox Virtual Environment Version 8.1 or later."
97108
echo -e "Exiting..."
@@ -144,6 +155,7 @@ function default_settings() {
144155
VLAN=""
145156
MTU=""
146157
START_VM="yes"
158+
METHOD="default"
147159
echo -e "${DGN}Using Virtual Machine ID: ${BGN}${VMID}${CL}"
148160
echo -e "${DGN}Using Machine Type: ${BGN}i440fx${CL}"
149161
echo -e "${DGN}Using Disk Cache: ${BGN}None${CL}"
@@ -160,6 +172,7 @@ function default_settings() {
160172
}
161173

162174
function advanced_settings() {
175+
METHOD="advanced"
163176
while true; do
164177
if VMID=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set Virtual Machine ID" 8 58 $NEXTID --title "VIRTUAL MACHINE ID" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
165178
if [ -z "$VMID" ]; then
@@ -341,6 +354,7 @@ arch_check
341354
pve_check
342355
ssh_check
343356
start_script
357+
post_to_api_vm
344358

345359
msg_info "Validating Storage"
346360
while read -r line; do
@@ -417,7 +431,7 @@ msg_ok "Added Docker and Docker Compose Plugin to Debian 12 Qcow2 Disk Image suc
417431

418432
msg_info "Creating a Docker VM"
419433
qm create $VMID -agent 1${MACHINE} -tablet 0 -localtime 1 -bios ovmf${CPU_TYPE} -cores $CORE_COUNT -memory $RAM_SIZE \
420-
-name $HN -tags proxmox-helper-scripts,debian12,docker -net0 virtio,bridge=$BRG,macaddr=$MAC$VLAN$MTU -onboot 1 -ostype l26 -scsihw virtio-scsi-pci
434+
-name $HN -tags community-script,debian12,docker -net0 virtio,bridge=$BRG,macaddr=$MAC$VLAN$MTU -onboot 1 -ostype l26 -scsihw virtio-scsi-pci
421435
pvesm alloc $STORAGE $VMID $DISK0 4M 1>&/dev/null
422436
qm importdisk $VMID ${FILE} $STORAGE ${DISK_IMPORT:-} 1>&/dev/null
423437
qm set $VMID \
@@ -465,4 +479,5 @@ if [ "$START_VM" == "yes" ]; then
465479
qm start $VMID
466480
msg_ok "Started Docker VM"
467481
fi
482+
post_update_to_api "done" "none"
468483
msg_ok "Completed Successfully!\n"

vm/haos-vm.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
# License: MIT
66
# https://github.com/community-scripts/ProxmoxVE/raw/main/LICENSE
77

8+
source /dev/stdin <<< $(wget -qLO - https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/misc/api.func)
9+
10+
811
function header_info {
912
clear
1013
cat <<"EOF"
@@ -21,6 +24,13 @@ echo -e "\n Loading..."
2124
GEN_MAC=02:$(openssl rand -hex 5 | awk '{print toupper($0)}' | sed 's/\(..\)/\1:/g; s/.$//')
2225
NEXTID=$(pvesh get /cluster/nextid)
2326
VERSIONS=(stable beta dev)
27+
#API VARIABLES
28+
RANDOM_UUID="$(cat /proc/sys/kernel/random/uuid)"
29+
METHOD=""
30+
NSAPP="homeassistant-os"
31+
var_os="homeassistant"
32+
DISK_SIZE="32G"
33+
#
2434
for version in "${VERSIONS[@]}"; do
2535
eval "$version=$(curl -s https://raw.githubusercontent.com/home-assistant/version/master/$version.json | grep "ova" | cut -d '"' -f 4)"
2636
done
@@ -41,13 +51,16 @@ SPINNER_PID=""
4151
set -Eeuo pipefail
4252
trap 'error_handler $LINENO "$BASH_COMMAND"' ERR
4353
trap cleanup EXIT
54+
trap 'post_update_to_api "failed" "INTERRUPTED"' SIGINT
55+
trap 'post_update_to_api "failed" "TERMINATED"' SIGTERM
4456

4557
function error_handler() {
4658
if [ -n "$SPINNER_PID" ] && ps -p $SPINNER_PID > /dev/null; then kill $SPINNER_PID > /dev/null; fi
4759
printf "\e[?25h"
4860
local exit_code="$?"
4961
local line_number="$1"
5062
local command="$2"
63+
post_update_to_api "failed" "${command}"
5164
local error_message="${RD}[ERROR]${CL} in line ${RD}$line_number${CL}: exit code ${RD}$exit_code${CL}: while executing command ${YW}$command${CL}"
5265
echo -e "\n$error_message\n"
5366
cleanup_vmid
@@ -167,6 +180,8 @@ function default_settings() {
167180
VLAN=""
168181
MTU=""
169182
START_VM="yes"
183+
METHOD="default"
184+
var_version="${stable}"
170185
echo -e "${DGN}Using HAOS Version: ${BGN}${BRANCH}${CL}"
171186
echo -e "${DGN}Using Virtual Machine ID: ${BGN}${VMID}${CL}"
172187
echo -e "${DGN}Using Machine Type: ${BGN}i440fx${CL}"
@@ -184,11 +199,13 @@ function default_settings() {
184199
}
185200

186201
function advanced_settings() {
202+
METHOD="advanced"
187203
if BRANCH=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "HAOS VERSION" --radiolist "Choose Version" --cancel-button Exit-Script 10 58 3 \
188204
"$stable" "Stable " ON \
189205
"$beta" "Beta " OFF \
190206
"$dev" "Dev " OFF \
191207
3>&1 1>&2 2>&3); then
208+
var_version="${BRANCH}"
192209
echo -e "${DGN}Using HAOS Version: ${BGN}$BRANCH${CL}"
193210
else
194211
exit-script
@@ -376,6 +393,9 @@ pve_check
376393
ssh_check
377394
start_script
378395

396+
397+
post_to_api_vm
398+
379399
msg_info "Validating Storage"
380400
while read -r line; do
381401
TAG=$(echo $line | awk '{print $1}')
@@ -463,4 +483,7 @@ if [ "$START_VM" == "yes" ]; then
463483
qm start $VMID
464484
msg_ok "Started Home Assistant OS VM"
465485
fi
486+
post_update_to_api "done" "none"
466487
msg_ok "Completed Successfully!\n"
488+
489+

0 commit comments

Comments
 (0)