-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path.bash_aliases
245 lines (203 loc) · 4.18 KB
/
.bash_aliases
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
#!/bin/bash
unset -v VMCONFIG
declare -A VMCONFIG
echo -ne "\n\n\e[1;34m### extra functions and colored output by https://github.com/morph027/pve-cli-dashboard ###\e[0m\n\n"
##
## _vm_status() just greps for status in "pct/qm list" output (submitted by _prettify())
## return values:
## 0 - running
## 1 - stopped
##
_vm_status() {
local vm_info="$1"
local vm_status=$(echo "$vm_info" | grep -oE 'stopped|running')
case $vm_status in
"running")
return 0
;;
"stopped")
return 1
;;
esac
}
##
## _info() will parse the config file of vm $1 into an associative bash array
## values can then be accessed using ${VMCONFIG[KEY]}, e.g. {VMCONFIG[net0]}
##
_info() {
local VM=$1
local CONFIGFILE=$(find /etc/pve -name ${VM}.conf)
VMTYPE=$(basename $(dirname $CONFIGFILE))
case $VMTYPE in
"qemu-server")
HOSTNAMEATTR="name"
COMMAND="qm"
;;
"lxc")
HOSTNAMEATTR="hostname"
COMMAND="pct"
;;
esac
while read line
do
local KEY="$(printf '%q' ${line%%:*})"
local VALUE="$(printf '%q' ${line#* })"
VMCONFIG[$KEY]="$VALUE"
done < $CONFIGFILE
}
##
## _destroy() will ask for confirmation before destroying
##
_destroy() {
local PCT=$2
_info $PCT
$COMMAND status $PCT >/dev/null 2>&1
if [ $? -eq 0 ]; then
echo -ne "\n\e[1;31mCT $PCT - Destroy\n\n\e[0m"
read -r -p "Please enter the ID to confirm ($PCT - ${VMCONFIG[$HOSTNAMEATTR]}): " answer
if [ "$answer" == "$PCT" ]; then
echo "Destroying $PCT ..."
command $COMMAND "$@"
else
echo "Good thing I asked; I won't destroy $PCT"
fi
fi
}
##
## _prettify() just colors the command $1 by vm status (running/stopped)
##
_prettify() {
local COMMAND="$1"
while read line
do
line=$(echo "$line" | sed 's,\(VMID.*\),\\e[1;37m\1\\e[0m,')
_vm_status "$line" && echo -e "\e[0;32m" || echo -e "\e[0;31m"
echo -ne "$line\e[0m"
done < <(command "$COMMAND" list)
echo -ne "\n\n"
}
##
## _get-id-by-name() gets the lxc VMID of a given name
##
_get-id-by-name() {
local VM_NAME="$1"
VM_CONFIG=$(grep -l 'name: '"$VM_NAME"'' /etc/pve/lxc/*)
if [ ! ${#VM_CONFIG} -eq "0" ]; then
VM_CONFIG=$(basename "$VM_CONFIG")
echo "${VM_CONFIG%%.*}"
else
return 1
fi
}
##
## _handle_by_name() iterate over given VM names
##
_handle_by_name() {
local ACTION="$1"
shift
# shellcheck disable=SC2068
for VM_NAME in $@
do
if VM_ID=$(_get-id-by-name "$VM_NAME"); then
echo "${ACTION} $VM_NAME"
pct "$ACTION" "$VM_ID"
fi
done
}
##
## start-by-name starts the lxc container by given name
##
start-by-name() {
local VM_NAME="$*"
_handle_by_name "start" "$VM_NAME"
}
##
## stop-by-name stops the lxc container by given name
##
stop-by-name() {
local VM_NAME="$*"
_handle_by_name "stop" "$VM_NAME"
}
##
## shutdown-by-name shuts down the lxc container by given name
##
shutdown-by-name() {
local VM_NAME="$*"
_handle_by_name "shutdown" "$VM_NAME"
}
##
## enter-by-name enters the lxc container by given name
##
enter-by-name() {
local VM_NAME="$1"
if VM_ID=$(_get-id-by-name "$VM_NAME"); then
pct enter "$VM_ID"
fi
}
##
## reset-by-name resets the lxc container by given name
##
reset-by-name() {
local VM_NAME="$*"
_handle_by_name "reset" "$VM_NAME"
}
##
## tab completion for commands
##
complete -W "$(grep -hPo '(?<=^hostname: ).*' /etc/pve/lxc/*.conf)" \
start-by-name \
stop-by-name \
shutdown-by-name \
enter-by-name \
reset-by-name
##
## wrapper for real pct command
##
pct() {
case $1 in
"list")
_prettify pct
;;
"destroy")
_destroy "$@"
;;
"reset")
VM=$2; command pct shutdown $VM && sleep 5 && command pct start $VM
;;
*)
command pct "$@"
;;
esac
}
##
## wrapper for real qm command
##
qm() {
case $1 in
"list")
_prettify qm
;;
"destroy")
_destroy "$@"
;;
*)
command qm "$@"
;;
esac
}
##
## motd
##
shopt -s nullglob
lxcfiles=(/etc/pve/lxc/*.conf)
qmfiles=(/etc/pve/qemu-server/*.conf)
shopt -u nullglob
echo "-------"
if [ ! ${#lxcfiles[@]} -eq 0 ]; then
_prettify pct
echo -ne "-------\n"
fi
if [ ! ${#qmfiles[@]} -eq 0 ]; then
_prettify qm
echo -ne "-------\n"
fi