-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgovc.sh
executable file
·384 lines (348 loc) · 9.46 KB
/
govc.sh
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
#!/bin/sh -efu
. shell-error
. shell-quote
. shell-args
. shell-ini-config
PROG="${0##*/}"
PROG_VERSION='0.3'
# VSphere insecure by default ((
vs_insecure=1
vs_json=
vs_dc=
vs_srv=
vs_snap=
vs_mem=
vs_dsize=
vs_dmode=
vs_config="$HOME/.terraformware.conf"
vs_dc_config="$HOME/.vs-dc.ini"
govc="$HOME/bin/govc"
cmd=
[ -x "$govc" ] || fatal "govc binary not found on $govc location"
# we rely on terraformware settings
[ -s "$vs_config" ] || fatal "No VSphere credentials found in $vs_config, exiting"
vs_username="$(grep -F vsphere_username $HOME/.terraformware.conf)"
vs_username="${vs_username##*= }"
vs_password="$(grep -F vsphere_password $HOME/.terraformware.conf)"
vs_password="${vs_password##*= }"
vs_password=$(echo "$vs_password"|base64 -d)
[ -n "$vs_username" ] && [ -n "$vs_password" ] || fatal 'no vsphere credentials found'
show_help() {
cat <<EOF
Usage: Usage: $PROG -l <DC> [options] <cmd> <vm>
Options:
-l, --location=<DC> VSphere DC location (for example BRQ or AMS);
-V, --version print program version and exit;
-h, --help show this text and exit;
-j, --json use json output.
Command shortcuts (takes VM as last argument):
poweron
poweroff
suspend
pstate Shows current VM power state
info
disk_info
disk_shrink
disk_extend <size>
disk_change <mode>
memory_extend <size>Mb
memory_hotadd_check
memory_hotadd_enable
cpu_add <nr>
cpu_remove <nr>
cpu_hotadd_check
cpu_hotadd_enable
cpu_hotremove_check
cpu_hotremove_enable
ls_snapshot
create_snapshot
delete_snapshot <name> Delete defined snapshot
revert_snapshot <name> Revert to defined snapshot
Advanced usage:
$PROG [options] -- <govc direct cmd>
EOF
exit
}
print_version() {
cat <<EOF
$PROG version $PROG_VERSION
Written by Konstantin Lepikhov <[email protected]>
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
EOF
exit
}
get_vm_path() {
local vm="$1"; shift
vm=$($govc find vm -name "$vm"*)
[ -n "$vm" ] && printf '%s\n' "$vm" \
|| return 1
}
get_vm_pstate() {
local vm=$1; shift
local pstate=
[ -n "$vm" ] || fatal 'VM name is missing'
local pstate=$($govc object.collect -s $(get_vm_path "$vm") runtime.powerState)
[ -n "$pstate" ] && printf '%s\n' "$pstate" || return 1
}
vm_on() {
local vm=$1; shift
local state=$(get_vm_pstate "$vm")
case "$state" in
poweredOn)
return 0
;;
*)
return 1
;;
esac
}
vm_off() {
local vm=$1; shift
local state=$(get_vm_pstate "$vm")
case "$state" in
poweredOff)
return 0
;;
*)
return 1
;;
esac
}
get_vm_disk_path() {
local vm=$1; shift
local path=
local ds=
[ -n "$vm" ] || fatal 'VM name is missing'
vm_off "$vm" || fatal 'VM should be OFF to perform!'
path=$($govc device.info -vm "$vm" -json disk-*|jq -r .Devices[].Backing.FileName)
ds=${path%% *}
path="${path##* }"
[ -n "$ds" ] && path="-ds $ds $path"
[ -n "$path" ] && printf '%s\n' "$path" || return 1
}
get_vm_option_bul() {
local vm=$1; shift
local opt=$1; shift
output=
[ -n "$vm" ] && [ -n "$opt" ] || fatal 'Not enought args to operate!'
vm=$(get_vm_path "$vm")
output=$($govc object.collect -s "$vm" "$opt")
[ -n "$output" -a "$output" = true ] && return 0 \
|| return 1
}
get_vm_option_val() {
local vm=$1; shift
local opt=$1; shift
output=
[ -n "$vm" ] && [ -n "$opt" ] || fatal 'Not enought args to operate!'
vm=$(get_vm_path "$vm")
output=$($govc object.collect -s "$vm" "$opt")
[ -n "$output" ] && printf '%s\n' "$output" || return 1
}
vm_extend_disk() {
local disk_size="$1"; shift
local vm="$1"; shift
local disk_name=
local cur_size=
local output=
shell_var_is_number "$disk_size" || fatal 'Disk size should be a number'
# Does it works for multiple disks?
cur_size=$($govc device.info -vm "$vm" -json disk-*|jq -r .Devices[].CapacityInBytes)
cur_size=$(($cur_size/(1024*1024)))
[ "$disk_size" -gt "$cur_size" ] || fatal "New disk size $disk_size < $cur_size"
disk_name=$($govc device.info -vm "$vm" -json disk-*|jq -r .Devices[].Name)
[ -n "$disk_name" ] || return 1
output="vm.disk.change -vm $vm -disk.name=$disk_name -size ${disk_size}M"
[ -n "$output" ] && printf '%s\n' "$output" || return 1
}
vm_change_disk() {
local disk_mode="$1"; shift
local vm="$1"; shift
local disk_path=
local cur_mode=
local output=
cur_mode=$($govc device.info -vm "$vm" -json disk-*|jq -r .Devices[].Backing.DiskMode)
[ -n "$cur_mode" ] || fatal 'Unable to get current disk mode'
case "$disk_mode" in
show)
disk_mode="$cur_mode"
;;
persistent|nonpersistent|undoable|independent_persistent|independent_nonpersistent|append)
;;
*) fatal "Mode $disk_mode unsupported"
;;
esac
if [ "$cur_mode" = "$disk_mode" ]; then
printf '%s\n' "$disk_mode"
return $?
fi
# Does it works for multiple disks?
disk_path=$($govc device.info -vm "$vm" -json disk-*|jq -r .Devices[].Backing.FileName)
[ -n "$disk_path" ] || return 1
printf 'vm.disk.change -vm %s -disk.filePath \"%s\" -mode %s' "$vm" "$disk_path" "$disk_mode" || return 1
}
vm_change_mem() {
local mem_size="$1"; shift
local vm="$1"; shift
local cur_size=
local mem_limit=
shell_var_is_number "$mem_size" || fatal 'Memory size should be a number!'
cur_size=$(get_vm_option_val "$vm" config.hardware.memoryMB)
# Handle the case where hotplug memory limit is missing
mem_limit=$(get_vm_option_val "$vm" config.hotPlugMemoryLimit) ||:
[ "$mem_size" -gt "$cur_size" ] &&
message "Changing memory size $cur_size->$mem_size MB.." || \
fatal "Memory requested size is lower than current $cur_size, exiting.."
if [ -n "$mem_limit" ]; then
[ "$mem_size" -gt "$mem_limit" ] && \
fatal "Cannot add more memory than hotadd limit $mem_limit"
fi
printf '%s\n' "vm.change -m $mem_size"
}
vm_change_cpu() {
local nr_cpu="$1"; shift
local action="$1"; shift
local vm="$1"; shift
local nr_cur=
shell_var_is_number "$nr_cpu" || fatal 'CPU count should be a number!'
nr_cur=$(get_vm_option_val "$vm" config.hardware.numCPU)
case "$action" in
cpu_add) nr_cpu=$(($nr_cur+$nr_cpu))
;;
cpu_remove)
nr_cpu=$(($nr_cur-$nr_cpu))
[ -n "$nr_cpu" -a "$nr_cpu" -gt 0 ] || \
fatal 'resulting CPU amount should be >= 0'
# see https://bugzilla.redhat.com/show_bug.cgi?id=1417938
vm_off "$vm" || fatal 'Cannot remove CPUs from a running VM'
;;
esac
[ -n "$nr_cpu" -a -n "$nr_cur" ] && message "Changing CPU count $nr_cur->$nr_cpu .."
printf '%s\n' "vm.change -c $nr_cpu"
}
TEMP=`getopt -n $PROG -o 'l:,H:,h,V,j' -l 'location:,fqdn:,help,version,json' -- "$@"` ||
show_usage
eval set -- "$TEMP"
while :; do
case "$1" in
-l|--location) shift; vs_dc="$1"
;;
-h|--help) show_help
;;
-V|--version) print_version
;;
-j|--json) vs_json=1
;;
--) shift; break
;;
esac
shift
done
[ -n "$vs_dc" ] || vs_dc="$(ini_config_get "$vs_dc_config" global location)"
[ -n "$vs_dc" ] || show_usage
vs_dc=$(printf '%s\n' "$vs_dc" | tr '[:upper:]' '[:lower:]')
vs_srv=$(ini_config_get "$vs_dc_config" "$vs_dc" url)
vs_dc=$(ini_config_get "$vs_dc_config" "$vs_dc" dc)
[ -n "$vs_insecure" ] && export GOVC_INSECURE='true'
export GOVC_URL="https://${vs_username}:${vs_password}@$vs_srv"
export GOVC_DATACENTER="$vs_dc"
cmd="$1"; shift
case "$cmd" in
poweroff) cmd="vm.power -s $1"
;;
poweron) cmd="vm.power -on $1"
;;
suspend) cmd="vm.power -suspend $1"
;;
pstate) get_vm_pstate "$1"
exit $?
;;
info) cmd="vm.info -e $1"
;;
disk_info) cmd='device.info disk-*'
;;
disk_shrink)
vs_json=
cmd="datastore.disk.shrink $(get_vm_disk_path $1)"
;;
disk_extend)
vs_dsize="$1"; shift
cmd=$(vm_extend_disk "$vs_dsize" "$1")
;;
disk_change)
vs_dmode="$1"; shift
cmd=$(vm_change_disk "$vs_dmode" "$1")
if [ "$vs_dmode" = "show" ]; then
printf '%s\n' "$cmd"
exit $?
else
vm_off "$1" || fatal "VM $1 should be OFF to operate!"
fi
;;
memory_extend)
vs_mem="$1"; shift
cmd=$(vm_change_mem "$vs_mem" "$1")
;;
memory_hotadd_check)
get_vm_option_bul "$@" config.memoryHotAddEnabled && \
message 'Enabled' || \
message 'Disabled'
exit $?
;;
memory_hotadd_enable)
vm_off "$1" || fatal "VM $1 should be OFF to operate!"
cmd='vm.change -e mem.hotadd=true'
;;
cpu_add|cpu_remove)
vs_cpu="$1"; shift
cmd=$(vm_change_cpu "$vs_cpu" "$cmd" "$1")
;;
cpu_hotadd_check)
get_vm_option_bul "$1" config.cpuHotAddEnabled && \
message 'Enabled' || \
message 'Disabled'
exit $?
;;
cpu_hotadd_enable)
vm_off "$1" || fatal "VM $1 should be OFF to operate!"
cmd='vm.change -e vcpu.hotadd=true'
;;
cpu_hotremove_check)
get_vm_option_bul "$1" config.cpuHotRemoveEnabled && \
message 'Enabled' || \
message 'Disabled'
exit $?
;;
cpu_hotremove_enable)
vm_off "$1" || fatal "VM $1 should be OFF to operate!"
cmd='vm.change -e vcpu.hotremove=true'
;;
ls_snapshot)
vs_json=
cmd='snapshot.tree'
;;
create_snapshot)
cmd="snapshot.create $(LC_ALL=POSIX date +'%Y_%b_%d_%H.%M')"
;;
delete_snapshot|revert_snapshot)
vs_snap="$1"; shift
[ -n "$vs_snap" ] || fatal 'Cmd requires snapshot name to operate, exiting'
case "$cmd" in
delete_*) cmd='snapshot.remove'
;;
revert_*) cmd='snapshot.revert'
;;
esac
cmd="$cmd $vs_snap"
;;
*) fatal "$cmd: Unsupported!"
;;
esac
[ -n "$vs_json" ] && cmd=$(printf '%s\n' "$cmd" |sed -ne 's,\(^[a-z]\+\?\.[a-z]\+\?\)\ ,\1 -json\ ,gp')
if [ "$#" -eq 1 ]; then
export GOVC_VM="$*"
eval "$govc" "$cmd"
else
$govc "$cmd" "$*"
fi