-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeploy-image
executable file
·165 lines (140 loc) · 4.41 KB
/
deploy-image
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
#!/usr/bin/env zsh
setopt ERR_EXIT PIPE_FAIL WARN_CREATE_GLOBAL
# Find required commands first
SSH=$(command -v ssh)
SCP=$(command -v scp)
CAT=$(command -v cat)
MKTEMP=$(command -v mktemp)
RM=$(command -v rm)
for cmd in "ssh:$SSH" "scp:$SCP" "cat:$CAT" "mktemp:$MKTEMP" "rm:$RM"; do
if [[ -z "${cmd#*:}" ]]; then
print -P "%F{red}Error:%f ${cmd%:*} not found in PATH"
exit 1
fi
done
# Default values
typeset PROXMOX_HOST="192.168.20.114"
typeset PROXMOX_USER="root"
typeset PROXMOX_PATH="/var/lib/vz/dump"
typeset IMAGE_PATH="result"
typeset STORAGE="pond"
usage() {
$CAT <<EOF
Usage: $0 [options]
Options:
-h, --host Proxmox host (default: $PROXMOX_HOST)
-u, --user SSH user (default: $PROXMOX_USER)
-p, --path Remote path (default: $PROXMOX_PATH)
-s, --storage Storage location (default: $STORAGE)
-i, --vmid VM ID for restore (will prompt if not provided)
--apply Execute the deployment
--help Show this help message
EOF
}
# Show help if no arguments
if [[ $# -eq 0 ]]; then
usage
exit 0
fi
# Parse arguments
zmodload zsh/zutil
if ! zparseopts -D -E -F -A opts \
h:=host -host:=host \
u:=user -user:=user \
p:=path -path:=path \
i:=vmid -vmid:=vmid \
s:=storage -storage:=storage \
-apply=apply \
-help=help 2>/dev/null; then
print -P "%F{red}Error:%f Invalid option provided"
print
usage
exit 1
fi
# Handle explicit help request
if (( ${#help} )); then
usage
exit 0
fi
if (( ${#apply} == 0 )); then
print -P "%F{yellow}Note:%f Use --apply to execute the deployment"
print
usage
exit 1
fi
[[ -n "${host[-1]}" ]] && PROXMOX_HOST="${host[-1]}"
[[ -n "${user[-1]}" ]] && PROXMOX_USER="${user[-1]}"
[[ -n "${path[-1]}" ]] && PROXMOX_PATH="${path[-1]}"
[[ -n "${storage[-1]}" ]] && STORAGE="${storage[-1]}"
# Find the image file using zsh globbing
setopt NULL_GLOB
image_files=($IMAGE_PATH/vzdump-qemu-*.vma.zst)
if (( ${#image_files} == 0 )); then
print -P "%F{red}Error:%f No image file found in $IMAGE_PATH"
exit 1
fi
IMAGE_FILE=$image_files[1]
# Create temporary directory for SSH control socket
SSH_CONTROL_DIR=$($MKTEMP -d)
SSH_CONTROL_PATH="$SSH_CONTROL_DIR/socket"
ssh_execute() {
local command=$1
local error_message=${2:-"SSH command failed"}
if ! $SSH -o ControlMaster=auto \
-o ControlPath="$SSH_CONTROL_PATH" \
-o ControlPersist=60 \
"$PROXMOX_USER@$PROXMOX_HOST" "$command"; then
print -P "%F{red}Error:%f $error_message"
$RM -rf $SSH_CONTROL_DIR
exit 1
fi
}
print -P "Deploying image %F{blue}${IMAGE_FILE:t}%f to %F{cyan}$PROXMOX_USER%f@%F{yellow}$PROXMOX_HOST%f:%F{green}$PROXMOX_PATH%f using storage %F{magenta}$STORAGE%f"
# Check if we can connect
if ! $SSH -o ControlMaster=yes \
-o ControlPath="$SSH_CONTROL_PATH" \
-o ControlPersist=5m \
-q "$PROXMOX_USER@$PROXMOX_HOST" exit; then
print -P "%F{red}Error:%f Cannot connect to $PROXMOX_USER@$PROXMOX_HOST"
$RM -rf $SSH_CONTROL_DIR
exit 1
fi
# Check if remote directory exists
ssh_execute "[ -d $PROXMOX_PATH ]" "Remote directory $PROXMOX_PATH does not exist"
# Copy the file
print -P "%F{blue}Copying image file...%f"
if $SCP -o ControlMaster=auto \
-o ControlPath="$SSH_CONTROL_PATH" \
-o ControlPersist=60 \
"$IMAGE_FILE" "$PROXMOX_USER@$PROXMOX_HOST:$PROXMOX_PATH/"; then
print -P "%F{green}Successfully copied image to Proxmox%f"
print -P "%F{blue}Image location:%f $PROXMOX_PATH/${IMAGE_FILE:t}"
VM_ID=""
if [[ -n "${vmid[-1]}" ]]; then
VM_ID="${vmid[-1]}"
else
print -P "%F{yellow}Enter VM ID for restore:%f"
read "VM_ID"
fi
if [[ ! "$VM_ID" =~ ^[0-9]+$ ]]; then
print -P "%F{red}Error:%f Invalid VM ID: $VM_ID"
$RM -rf $SSH_CONTROL_DIR
exit 1
fi
print -P "%F{blue}Restoring VM with ID $VM_ID...%f"
ssh_execute "qmrestore $PROXMOX_PATH/${IMAGE_FILE:t} $VM_ID --unique true --storage $STORAGE" "Failed to restore VM"
print -P "%F{green}Successfully restored VM $VM_ID%f"
print -P "%F{blue}Configuring EFI disk...%f"
ssh_execute "
qm set $VM_ID --delete efidisk0 &&
qm set $VM_ID --efidisk0 $STORAGE:1,format=raw,efitype=4m,pre-enrolled-keys=0 &&
qm set $VM_ID --delete unused0 || true
" "Failed to configure EFI disk"
print -P "%F{green}Successfully configured EFI disk%f"
else
print -P "%F{red}Error:%f Failed to copy image to Proxmox"
$RM -rf $SSH_CONTROL_DIR
exit 1
fi
# Clean up
$RM -rf $SSH_CONTROL_DIR