-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvmctl.sh
More file actions
executable file
·261 lines (223 loc) · 5.53 KB
/
vmctl.sh
File metadata and controls
executable file
·261 lines (223 loc) · 5.53 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
#!/bin/bash
set -e
echo_ok() {
echo -n "$1"
echo -en "\\033[55G"
echo -e "\033[32m[SUCCESS]\\033[0m"
}
echo_err() {
echo -n "$1"
echo -en "\\033[55G"
echo -e "\033[31m[FAILED]\\033[0m"
}
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
CONFIG_FILE="$SCRIPT_DIR/config.ini"
if [ ! -f "$CONFIG_FILE" ]; then
echo "Configuration file $CONFIG_FILE not found."
exit 1
fi
source "$CONFIG_FILE"
if [ ! -f "$SRC_VMX" ]; then
echo "Source vmx does not exist."
exit 1
fi
list_all_vms() {
find "$BASE_DIR" -name "*.vmx" | awk -F / '{print $(NF-1)}'
}
list_vm() {
vmrun list | awk -F'\' 'NR>1 {print $NF}' | sed 's/\.vmx//'
}
clone_vm() {
local vm_name="$1"
vmrun -T ws clone "$SRC_VMX" "$BASE_DIR\\$vm_name\\$vm_name.vmx" linked -snapshot=$SNAPSHOT -cloneName=$vm_name
if [ $? -ne 0 ]; then
echo_err "Domain '$vm_name' create"
exit 1
fi
vmrun -T ws start "$BASE_DIR\\$vm_name\\$vm_name.vmx" gui
sleep 3
vmrun -T ws -gu root -gp "$ROOTPASS" runScriptInGuest "$BASE_DIR\\$vm_name\\$vm_name.vmx" "bin/bash" \
"hostnamectl set-hostname $vm_name"
if [ $? -eq 0 ]; then
echo_ok "Domain '$vm_name' create"
fi
}
remove_vm() {
local vm_name="$1"
local ip_addr
ip_addr=$(awk -v name="$vm_name" '$1 == name {print $2}' ~/.vmctl/hosts)
if [ -n "$ip_addr" ]; then
ssh-keygen -R "$ip_addr" &> /dev/null
fi
sed -i "/^$vm_name /d" ~/.vmctl/hosts 2> /dev/null
if vmrun -T ws list | grep -q "$1.vmx"; then
vmrun -T ws stop "$BASE_DIR\\$vm_name\\$vm_name.vmx" &> /dev/null
fi
rm -rf "$BASE_DIR\\$vm_name\\$vm_name.vmx.lck"
vmrun -T ws deleteVM "$BASE_DIR\\$vm_name\\$vm_name.vmx"
if [ $? -eq 0 ]; then
echo_ok "Domain '$vm_name' remove"
else
echo_err "Domain '$vm_name' remove"
exit 1
fi
}
start_vm() {
local vm_name="$1"
vmrun -T ws start "$BASE_DIR\\$vm_name\\$vm_name.vmx" gui
if [ $? -eq 0 ]; then
echo_ok "Domain '$vm_name' start"
else
echo_err "Domain '$vm_name' start"
exit 1
fi
}
stop_vm() {
local vm_name="$1"
vmrun -T ws stop "$BASE_DIR\\$vm_name\\$vm_name.vmx"
if [ $? -eq 0 ]; then
echo_ok "Domain '$vm_name' stop"
else
echo_err "Domain '$vm_name' stop"
exit 1
fi
}
set_host_ip() {
local vm_name="$1"
local ip_addr="$2"
local key
vmrun -T ws -gu root -gp "$ROOTPASS" runScriptInGuest "$BASE_DIR\\$vm_name\\$vm_name.vmx" "bin/bash" \
"nmcli connection modify '$CONNECTION' ipv4.method manual ipv4.addresses $ip_addr/24 autoconnect yes && nmcli connection up '$CONNECTION'"
if [ $? -eq 0 ]; then
echo_ok "Domain '$vm_name' set ip to '$ip_addr'"
mkdir -p ~/.vmctl
touch ~/.vmctl/hosts
sed -i "/^$vm_name /d" ~/.vmctl/hosts 2> /dev/null
echo "$vm_name $ip_addr" >> ~/.vmctl/hosts
else
echo_err "Domain '$vm_name' set ip"
exit 1
fi
echo "Deploying SSH key..."
ip_addr=$(awk -v name="$vm_name" '$1 == name {print $2}' ~/.vmctl/hosts)
if [ ! -f "$HOME/.ssh/id_rsa.pub" ]; then
ssh-keygen -t rsa -b 4096 -f "$HOME/.ssh/id_rsa" -q -N ""
fi
key=$(cat ~/.ssh/id_rsa.pub)
vmrun -T ws -gu root -gp "$ROOTPASS" runScriptInGuest "$BASE_DIR\\$vm_name\\$vm_name.vmx" "bin/bash" \
"mkdir -p /root/.ssh && echo $key > /root/.ssh/authorized_keys && chmod 600 /root/.ssh/authorized_keys && chmod 700 /root/.ssh"
if ssh -o StrictHostKeyChecking=accept-new root@$ip_addr exit 2> /dev/null; then
echo_ok "SSH key deployed"
else
echo_err "SSH key deployment failed"
fi
}
ssh_vm() {
local vm_name="$1"
local ip_addr
ip_addr=$(awk -v name="$vm_name" '$1 == name {print $2}' ~/.vmctl/hosts)
if [ -z "$ip_addr" ]; then
echo_err "IP address of '$vm_name' not found."
exit 1
else
ssh -o StrictHostKeyChecking=no -o ForwardX11=no root@$ip_addr
fi
}
usage() {
cat << EOF
Usage: vm [command] [arguments]
Available commands:
list List running or all VMs vm list [--all]
clone Clone one or more VMs vm clone <vm_name> [...]
remove Remove one or more VMs vm remove <vm_name> [...]
start Start one or more VMs vm start <vm_name> [...]
stop Stop one or more VMs vm stop <vm_name> [...]
restart Restart one or more VMs vm restart <vm_name> [...]
setip Set IP address of a VM vm setip <vm_name> <ip_addr>
ssh SSH into a VM vm ssh <vm_name>
For example:
vm clone web1 web2
vm setip web1 192.168.88.100
EOF
exit 1
}
case $1 in
"list")
shift
if [ -z "$1" ]; then
list_vm
exit 0
elif [ "$1" == '--all' ]; then
list_all_vms
exit 0
else
usage
fi
;;
"clone")
if [ $# -lt 2 ]; then
usage
fi
shift
for vm_name in "$@"; do
clone_vm "$vm_name"
done
;;
"remove")
if [ $# -lt 2 ]; then
usage
fi
shift
for vm_name in "$@"; do
remove_vm "$vm_name"
done
;;
"start")
if [ $# -lt 2 ]; then
usage
fi
shift
for vm_name in "$@"; do
start_vm "$vm_name"
done
;;
"stop")
if [ $# -lt 2 ]; then
usage
fi
shift
for vm_name in "$@"; do
stop_vm "$vm_name"
done
;;
"restart")
if [ $# -lt 2 ]; then
usage
fi
shift
for vm_name in "$@"; do
stop_vm "$vm_name"
sleep 3
start_vm "$vm_name"
done
;;
"setip")
if [ $# -ne 3 ]; then
usage
fi
shift
name=$(echo "$*" | awk '{print $1}')
ip=$(echo "$*" | awk '{print $2}')
set_host_ip "$name" "$ip"
;;
"ssh")
if [ $# -ne 2 ]; then
usage
fi
shift
ssh_vm "$1"
;;
*)
usage
;;
esac