-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
qrun
executable file
·312 lines (288 loc) · 7.96 KB
/
qrun
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
#!/usr/bin/bash
# Author: zenobit
# Description: Uses gum to provide a simple VMs runner for quickemu and quickget
# License MIT
_define_variables() {
color=$(( RANDOM % 255 + 1 ))
progname="${progname:="${0##*/}"}"
#configdir="$HOME/.config/$progname"
version='0.2'
vms=(*.conf)
if ! command -v gum >/dev/null 2>&1; then
echo 'You are missing gum! Exiting...' && exit 1
fi
if ! command -v quickemu >/dev/null 2>&1; then
gum style --foreground 1 "You are missing quickemu!"
fi
QUICKGET=$(command -v quickget)
#export BORDER="rounded"
export BORDERS_FOREGROUND="$color"
export GUM_CHOOSE_CURSOR_FOREGROUND="$color"
export GUM_CHOOSE_SELECTED_FOREGROUND="$color"
export GUM_CONFIRM_PROMPT_FOREGROUND=""
export GUM_CONFIRM_SELECTED_FOREGROUND="$color"
export GUM_CONFIRM_UNSELECTED_FOREGROUND=0
export GUM_FILTER_CURSOR_TEXT_FOREGROUND=""
export GUM_FILTER_HEADER_FOREGROUND=""
export GUM_FILTER_INDICATOR_FOREGROUND="$color"
export GUM_FILTER_MATCH_FOREGROUND="$color"
export GUM_FILTER_PROMPT_FOREGROUND="$color"
export GUM_FILTER_SELECTED_PREFIX_FOREGROUND="$color"
export GUM_FILTER_SELECTED_PREFIX_BORDER_FOREGROUND="$color"
}
_generate_supported(){
echo "Extracting OS Editions and Releases..."
rm -rf /tmp/distros
mkdir -p /tmp/distros
"$QUICKGET" | awk 'NR==2,/zorin/' | cut -d':' -f2 | grep -o '[^ ]*' > /tmp/supported
while read -r get_name; do
supported=$($QUICKGET "$get_name" | sed 1d)
echo "$get_name"
echo "$supported"
echo "$supported" > "/tmp/distros/${get_name}"
done < /tmp/supported
}
_if_needed() {
if [ ! -f /tmp/supported ]; then
_generate_supported
fi
}
show_vms() {
if [ ${#vms[@]} -eq 0 ]; then
gum style --foreground 1 "No VMs!"
else
echo "${vms[@]%.*}" | tr " " "\n"
fi
}
color2=$(( RANDOM % 255 + 1 ))
show_headers() {
if [ -f /tmp/icons ]; then
icons=yes
else
icons=""
fi
logo_0=$(gum style " Simple VMs runner")
logo_1=$(gum style --foreground "$color2" "▄▄▄▄ ▄▄▄ ▄ ▄ ▄ ▄
█ █ █ █ █ █ █▄ █
█ █ █▄▀ █ █ █ ▀▄█
█▄▀▄ █ ▀▄ █▄▄█ █ █")
logo_2=$(gum style "v$version")
logo_3=$(gum style --foreground "$color2" "▀")
logo_4=$(gum style " for quickemu")
logo_234=$(gum join "$logo_2" "$logo_3" "$logo_4")
logo=$(gum join --vertical "$logo_0" "$logo_1" "$logo_234")
logo_border=$(gum style --padding "0 1" --border=rounded --border-foreground $color "$logo" )
tip_header=$(gum style --bold "Tip: ")
distro=$(shuf -n 1 /tmp/supported)
tip_distro=$(gum style "try $distro")
tip_temp=$(gum join "$tip_header" "$tip_distro")
homepage=$("$QUICKGET" -s "${distro}")
tip_homepage=$(gum style "$homepage")
tip=$(gum join --vertical --align top "$tip_temp" "$tip_homepage")
tip_border=$(gum style --padding "0 1" --border=rounded --border-foreground $color "$tip")
pid_files=(*/*.pid)
vms=(*.conf)
vms_running=()
vms_not=()
vms_vm=$(gum style --bold "ready:")
vms_run=""
if [ -n "$(find . -name '*.pid')" ]; then
for pid_file in "${pid_files[@]}"; do
instance_name=$(basename "$pid_file" .pid)
vms_running+=("$instance_name")
done
if [ "$icons" == yes ]; then
running_logo=$(gum style --foreground "$color" --bold ".")
else
running_logo=$(gum style --foreground "$color" --bold ">")
fi
for instance in "${vms_running[@]}"; do
vms_run+="$running_logo$instance "
done
fi
mapfile -t vms_not < <(comm -23 <(printf "%s\n" "${vms[@]}" | rev | cut -d'.' -f2-9 | rev | sort) <(printf "%s\n" "${vms_running[@]}" | sort))
vms_not_next=$(gum style < <(printf '%s\n' "${vms_not[@]}"))
if [ -n "$(find . -name '*.pid')" ]; then
vms_run_next=$(echo "$vms_run" | tr " " "\n")
vms_header=$(gum join --vertical "$vms_vm" "$vms_run_next" "$vms_not_next")
else
vms_header=$(gum join --vertical "$vms_vm" "$vms_not_next")
fi
vms_border=$(gum style --padding "0 1" --border=rounded --border-foreground $color "$vms_header")
header=$(gum join --align top "$logo_border" "$vms_border")
gum join --align center --vertical "$tip_border" "$header"
}
gum_filter_os() {
os=$(gum filter < /tmp/supported)
choices=$(cat "/tmp/distros/$os")
}
gum_filter_release() {
release=$(echo "$choices" | grep 'Releases:' | cut -d':' -f2 | grep -o '[^ ]*' | gum filter --sort)
}
gum_filter_edition() {
edition=$(echo "$choices" | grep 'Editions:' | cut -d':' -f2 | grep -o '[^ ]*' | gum filter --sort)
}
gum_choose_VM() {
if find . -maxdepth 1 -name '*.conf' >/dev/null 2>&1 ; then
chosen=$(find . -maxdepth 1 -name '*.conf' | cut -d'/' -f2 | rev | cut -d'.' -f2-9 | rev | gum filter --select-if-one)
else
gum style --foreground 1 "Can't!"
fi
}
create_VM() {
gum_filter_os
if [ -z "$os" ]; then exit 100
elif [ "$(echo "$choices" | wc -l)" = 1 ]; then
clear
gum_filter_release
clear
"$QUICKGET" "$os" "$release"
else
clear
gum_filter_release
clear
gum_filter_edition
clear
"$QUICKGET" "$os" "$release" "$edition"
fi
show_headers
}
run_VM() {
quickemu -vm "$chosen.conf"
show_headers
}
gum_choose_running() {
pid_files=( */*.pid )
if [ ${#pid_files[@]} -gt 0 ]; then
mapfile -t running < <(find . -name '*.pid' -printf '%P\n' | sed 's/\.pid$//')
if [ ${#running[@]} -gt 0 ]; then
selected=$(gum choose --select-if-one "${running[@]}")
else
gum style --foreground 1 "Can't!" && selected=""
fi
else
gum style --foreground 1 "Can't!" && selected=""
fi
}
# test
gum_choose_runnings() {
pid_files=( */*.pid )
if [ ${#pid_files[@]} -gt 0 ]; then
mapfile -t running < <(find . -name '*.pid' -printf '%P\n' | sed 's/\.pid$//')
if [ ${#running[@]} -gt 0 ]; then
selected=$(gum choose --select-if-one "${running[@]}")
else
gum style --foreground 1 "Can't!" && selected=""
fi
else
gum style --foreground 1 "Can't!" && selected=""
fi
}
get_ssh_port() {
port=$(grep 'ssh' < "$selected".ports | cut -d',' -f2)
}
ssh_into() {
gum_choose_running
if [ -n "$selected" ]; then
get_ssh_port
username=$(gum input --prompt "$selected user? ")
ssh "$username"@localhost -o ConnectTimeout=5 -o StrictHostKeyChecking=accept-new -p "$port"
show_headers
fi
}
kill_vm() {
gum_choose_running
if [ -n "$selected" ]; then
echo "${selected}"
gum confirm "Really kill $selected?" && pid=$(cat "$selected".pid) && kill "$pid"
show_headers
fi
}
# test
kill_vms() {
gum_choose_runnings
if [ -n "$selected" ]; then
for vm_name in "${selected[@]}"; do
gum confirm "Really kill $vm_name?"
pid=$(cat "${vm_name}.pid")
kill "$pid"
done
show_headers
fi
}
gum_choose_VM_to_delete() {
if [ -n "$(echo *.conf)" ]; then
chosen=$(echo "${vms[@]%.*}" | tr " " "\n" | gum choose --select-if-one)
gum confirm "Really delete $chosen" && rm -r "$chosen" && rm "$chosen".conf
show_headers
else
gum style --foreground 1 "No VMs!"
fi
}
open_distro_homepage(){
gum_filter_os
"$QUICKGET" -o "${os}" >/dev/null 2>&1 &
}
# shellcheck disable=SC2015
icons_or() {
gum confirm " Use icons?
need Nerd Fonts" && echo "yes" > /tmp/icons || rm /tmp/icons
show_headers
}
# MENU
_show_menu() {
while true
do
if [ -f /tmp/icons ]; then
icons=yes
else
icons=""
fi
if [ "$icons" == yes ]; then
start=$(echo " create
run
homepages
ssh into
kill
delete
icons
exit" | gum choose --selected ' run')
case $start in
' create' ) create_VM;;
' run' ) gum_choose_VM && run_VM;;
' ssh into' ) ssh_into;;
' homepages' ) open_distro_homepage;;
' kill' ) kill_vm;;
' delete' ) gum_choose_VM_to_delete;;
' icons' ) icons_or;;
' exit' ) exit 0;;
esac
else
start=$(echo "create
run
homepage
ssh into
kill
delete
icons
exit $progname" | gum filter --height 6)
# from choose: --selected ' run'
case $start in
create ) create_VM;;
run ) gum_choose_VM && run_VM;;
'ssh into' ) ssh_into;;
homepage ) open_distro_homepage;;
kill ) kill_vm;;
delete ) gum_choose_VM_to_delete;;
icons ) icons_or;;
"exit $progname" ) exit 0;;
esac
fi
done
}
# run
#clear
_define_variables
_if_needed
show_headers
_show_menu