-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
fusion-vmcli
executable file
·226 lines (203 loc) · 6.27 KB
/
fusion-vmcli
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
#!/usr/bin/env bash
#
# Vmware Funsion Operate Linux (Mode: No Gui)
#
# Author: jackliu (Jianqiu Liu)
# Site: https://github.com/ljq
# Blog: https://wdft.com
# Email: stackgo@163.com
# Date: 2020-11-29
# Version: 1.0.0
# script version
CLI_VERSION="1.0.1"
# color green
GREEN_COLOR="\033[32m"
CYAN_COLOR="\033[36m"
YELLOW_COLOR="\033[43;37m"
RED_COLOR="\033[31m"
GREEN_BG_COLOR="\033[47;42m"
CYAN_BG_COLOR="\033[47;46m"
RES="\033[0m"
# help info
USEAGE="[usage]: [-l | list] [start|stop|suspend|pause|unpause <number>] [-h | -help | --help] [-v | -V | --version]"
HELP_INFO=$(cat <<EOF
[helptext]
-h|help : Show help info.
-l|list : default[None parameters] show VMs list and VMs status.
start|stop|suspend|pause|unpause [number] :
Start the corresponding virtual machine [Linux based distribution only].
Notice: default show VMs list.
EOF
)
# ---------------------- Functions -------------------------
# fusion vms source path
# etc conf
function etc_ini(){
item=$1
section="BASE"
ini_file="/usr/local/etc/fusion-vmcli.conf"
if [ ! -f "$ini_file" ]; then
return 0
fi
ini_options=`awk -F '=' '/'$section'/{a=1}a==1&&$1~/'${item}'/{print $2;exit}' ${ini_file}`
ini_options=${ini_options//\"/}
echo ${ini_options}
}
# Show list vm nodes
function show_vms(){
local vms=($1)
local total=$2
local num=1
echo -e "${GREEN_BG_COLOR}"
echo -e "=============== VM List (by Linux distribution) ===============\n"
for dirname in ${vms[@]}
do
#filter Win
dirname=${dirname//.vmwarevm/}
if [[ "${dirname}" =~ "Win" ]]; then
continue
else
# escape filename
esc_dirname=${dirname//#/\\ }
echo -e "[${num}] ${esc_dirname}"
((num=num+1))
fi
done
echo -e "\n"
echo -e "By linux distribution total:${total} vm nodes.\n"
echo -e "======================= VM List End ============================\n"
echo -e "${RES}"
}
# get vm total number
function vm_nodes_total(){
local vms=($1)
local total=0
for dirname in ${vms[@]}
do
#filter Win
if [[ "${dirname}" =~ "Win" ]]; then
continue
else
((total=total+1))
fi
done
echo $total
}
# Task exec
function task_exec(){
# check params
local operate=$1
local serial_num=$2
local vms=($3)
local vm_path=$4
local list_total=$5
if [[ -z $operate || -z $serial_num ]]; then
echo -e "${RED_COLOR}[error] missing parameter: operate or serial_num.${RES}\n"
exit;
fi
local vmrun_params=("start" "stop" "suspend" "pause" "unpause")
if [[ ${vmrun_params[@]/${operate}/} != ${vmrun_params[@]} ]]; then
echo -e "${GREEN_COLOR}----------------------------------------------------------${RES}\n"
else
echo -e "${RED_COLOR}[error] input invalid parameter: operate.${RES}\n"
exit
fi
if [[ $serial_num -le 0 || $serial_num -gt ${list_total} ]]; then
echo -e "${RED_COLOR}[error] serial_num is not range (1-${list_total})${RES}\n"
exit
fi
#vm nodes
local vm_nodes=()
local num=1
for dirname in ${vms[@]}
do
#filter Win
dirname=${dirname//.vmwarevm/}
if [[ "${dirname}" =~ "Win" ]]; then
continue
else
# escape filename
esc_dirname=${dirname//#/\\ }
vm_nodes[$num]=$esc_dirname
((num=num+1))
fi
done
# select node machine
local vm_num_machine=${vm_nodes[${serial_num}]}
echo -e "${GREEN_COLOR}VM machine (${vm_num_machine}) $operate Starting ...${RES}\n"
# task exec
#echo "$(which vmrun) $operate $vm_path/$vm_num_machine.vmwarevm/$vm_num_machine.vmx nogui"
"$(which vmrun)" $operate "${vm_path}/${vm_num_machine}.vmwarevm/${vm_num_machine}.vmx" nogui
echo -e "${GREEN_COLOR}VM machine(${vm_num_machine}) $operate Finished.${RES}\n"
echo -e "${GREEN_COLOR}----------------------------------------------------------${RES}\n"
}
# Show vm nodes running
function vms_runing(){
echo -e "${CYAN_BG_COLOR}"
echo -e "=============== VM Runing List ===============\n"
"$(which vmrun)" list
echo -e "\n"
echo -e "=============== List Runing End ==============="
echo -e "${RES}"
}
# ---------------------- Task Process -------------------------
# check vmware fusion installed status
if which vmrun >/dev/null 2>&1; then
vmrun_version=$(vmrun | grep "vmrun version")
echo -e "${CYAN_COLOR}${vmrun_version}.${RES}"
echo -e "${USEAGE}"
else
echo -e "${YELLOW_COLOR}[Warning] vmrun is not installd.${RES}\n"
fi
# help
case $1 in
"-v"|"-V"|"--version")
echo -e "cli script version:${CLI_VERSION}."
exit
;;
"-h"|"-help"|"--help")
echo -e "${HELP_INFO}"
exit
;;
"-l"|"list")
;;
esac
# virtual machines path config (custom)
vm_path=$(etc_ini "vm_path")
current_user=$(whoami)
vm_path="${vm_path//\$\(whoami\)/${current_user}}"
if [ -z "$vm_path" ]; then
vm_path="/Users/$(whoami)/Documents/Virtual Machines.localized"
# check default vm path
if [ ! -d "$vm_path" ]; then
echo -e "${GREEN_BG_COLOR}[Notice] Sorry! The default path is not found.${RES}"
echo -e "${GREEN_BG_COLOR}The default path is not found.Check to see if it is installed or exists.${RES}"
echo -e "${GREEN_BG_COLOR}The configuration file:${RES}"
echo -e "${GREEN_BG_COLOR} /usr/local/etc/fusion-vmcli.conf${RES}\n"
exit
fi
fi
# list dir and process filename space
list_vms=($(ls "${vm_path}" | tr " " "\#"))
list_total=$(vm_nodes_total "${list_vms[*]}")
# vmrun params exec
if [[ ! -z $1 && ! -z $2 ]]; then
# direct exec
task_exec $1 $2 "${list_vms[*]}" "$vm_path" $list_total
vms_runing
else
# show list
show_vms "${list_vms[*]}" $list_total
vms_runing
# interactive exec
echo -e "${YELLOW_COLOR}"
echo -e "[Notice] Please input your vm operate(start|stop|suspend|pause|unpause) and number(1-${list_total}):"
echo -e "${RES}"
read operate serial_num
# cli interactive
task_exec $operate $serial_num "${list_vms[*]}" "$vm_path" $list_total
vms_runing
fi
echo -e "\n"
echo -e "${GREEN_BG_COLOR}[Notice] Task process is done.${RES}\n"
exit