#!/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##*/}"}" # The runtime state is per user, so two users (or two instances) do not # overwrite each other's list of supported systems. tmpdir="${TMPDIR:-/tmp}/qrun-${USER:-$(id -un)}" mkdir -p "${tmpdir}" #configdir="$HOME/.config/$progname" version='0.3' 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 if command -v qget >/dev/null 2>&1; then QUICKGET=$(command -v qget) QUICKGET_QGET=yes elif command -v quickget >/dev/null 2>&1; then QUICKGET=$(command -v quickget) QUICKGET_QGET=no else gum style --foreground 1 "You are missing qget and quickget!" exit 2 fi #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 "${tmpdir}/distros" mkdir -p "${tmpdir}/distros" if [ "${QUICKGET_QGET}" = "yes" ]; then "$QUICKGET" --list-os > "${tmpdir}/supported" else "$QUICKGET" | awk 'NR==2,/zorin/' | cut -d':' -f2 | grep -o '[^ ]*' > "${tmpdir}/supported" fi while read -r get_name; do if [ "${QUICKGET_QGET}" = "yes" ]; then supported=$("$QUICKGET" --show --plain "$get_name" | sed 1d) else supported=$("$QUICKGET" "$get_name" | sed 1d) fi echo "$get_name" echo "$supported" echo "$supported" > "${tmpdir}/distros/${get_name}" done < "${tmpdir}/supported" } _if_needed() { if [ ! -f "${tmpdir}/supported" ]; then _generate_supported fi } show_vms() { if [ ${#vms[@]} -eq 0 ]; then gum style --foreground 1 "No VMs!" else echo "${vms[@]%.*}" | tr " " "\n" fi } show_headers() { if [ -f "${tmpdir}/icons" ]; then icons=yes else icons="" fi color2=$((RANDOM % 255 + 1)) 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 "${tmpdir}/supported") tip_distro=$(gum style "try $distro") tip_temp=$(gum join "$tip_header" "$tip_distro") tip_distro_info=$("$QUICKGET" "${distro}" | sed 1d | sed '/^$/q') tip=$(gum join --vertical --align top "$tip_temp" "$tip_distro_info") 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 < "${tmpdir}/supported") choices=$(cat "${tmpdir}/distros/$os") } gum_filter_release() { release=$(echo "$choices" | grep 'Releases:' | cut -d':' -f2 | grep -o '[^ ]*' | gum filter) } gum_filter_edition() { edition="" if echo "$choices" | grep -q 'Editions:'; then edition=$(printf '%s\n' "$choices" | sed -n '/^EditionsList:$/,$p' | sed '1d; /^$/d' | gum filter) fi } qrun_host_arch() { case "$(uname -m)" in x86_64) echo amd64 ;; aarch64) echo arm64 ;; ppc64le) echo ppc64el ;; *) uname -m ;; esac } gum_filter_arch() { local architectures architectures=$(echo "$choices" | sed -n 's/^Archs:[[:space:]]*//p') [ -z "$architectures" ] && architectures="$(qrun_host_arch)" if [ "$(echo "$architectures" | wc -w)" -gt 1 ]; then ARCH=$(printf '%s\n' $architectures | gum filter) [ -z "$ARCH" ] && { printf 'Architecture selection cancelled.\n' >&2; return 1; } else ARCH="$architectures" fi return 0 } 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 return 0 fi clear if [ "${QUICKGET_QGET}" = "yes" ]; then choices=$("$QUICKGET" --show --plain "$os") || return 1 gum_filter_arch || return 0 choices=$("$QUICKGET" --show --plain --arch "$ARCH" "$os") || return 1 gum_filter_release [ -z "$release" ] && return 0 choices=$("$QUICKGET" --show --plain --arch "$ARCH" --release "$release" "$os") || return 1 if echo "$choices" | grep -q 'Editions:'; then gum_filter_edition [ -z "$edition" ] && return 0 "$QUICKGET" --arch "$ARCH" "$os" "$release" "$edition" else "$QUICKGET" --arch "$ARCH" "$os" "$release" fi else if echo "$choices" | grep -q 'Editions:'; then gum_filter_release [ -z "$release" ] && return 0 gum_filter_edition [ -z "$edition" ] && return 0 "$QUICKGET" "$os" "$release" "$edition" else gum_filter_release [ -n "$release" ] && "$QUICKGET" "$os" "$release" fi fi show_headers } run_VM() { quickemu "$@" -vm "$chosen.conf" show_headers } snapshot_run() { local output status=0 output=$(quickemu "$@" 2>&1) || status=$? printf '%s\n' "$output" | gum pager return "$status" } snapshot_VM() { gum_choose_VM [ -n "$chosen" ] || return 0 local vm_conf="${chosen}.conf" action snapshot_tag safety_tag status=0 if [ ! -f "$vm_conf" ]; then gum style --foreground 1 "VM configuration not found: $vm_conf" return 1 fi gum style "Snapshots affect the configured primary disk only. Stop the VM before managing snapshots." action=$(printf 'list\ncreate\nrestore\ndelete\n' | gum choose) [ -n "$action" ] || return 0 case "$action" in list) snapshot_run --snapshot info -vm "$vm_conf" || status=$? ;; create) snapshot_tag=$(gum input --prompt "Snapshot tag: ") [ -n "$snapshot_tag" ] || return 0 snapshot_run --snapshot create "$snapshot_tag" -vm "$vm_conf" || status=$? ;; restore) snapshot_tag=$(gum input --prompt "Snapshot tag to restore: ") [ -n "$snapshot_tag" ] || return 0 gum confirm "Restore '$snapshot_tag'? A safety snapshot will be created first." || return 0 safety_tag="before-restore-$(date +%Y%m%d-%H%M%S)-${RANDOM}" if ! snapshot_run --snapshot create "$safety_tag" -vm "$vm_conf"; then show_headers return 1 fi snapshot_run --snapshot apply "$snapshot_tag" -vm "$vm_conf" || status=$? ;; delete) snapshot_tag=$(gum input --prompt "Snapshot tag to delete: ") [ -n "$snapshot_tag" ] || return 0 gum confirm "Delete snapshot '$snapshot_tag'? This cannot be undone." || return 0 snapshot_run --snapshot delete "$snapshot_tag" -vm "$vm_conf" || status=$? ;; *) return 0 ;; esac show_headers return "$status" } 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" > "${tmpdir}/icons" || rm -f "${tmpdir}/icons" 2>/dev/null show_headers } # MENU _show_menu() { while true; do if [ -f "${tmpdir}/icons" ]; then icons=yes else icons="" fi if [ "$icons" == yes ]; then start=$(echo " create 󰜎 run 󰖟 run offline 󰐕 snapshots 󰖟 homepages  ssh into  kill 󰆳 delete 󱌝 icons 󰩈 exit" | gum filter --height 13) case $start in ' create') create_VM;; '󰜎 run') gum_choose_VM && run_VM;; '󰖟 run offline') gum_choose_VM && run_VM --offline;; '󰐕 snapshots') snapshot_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 run offline homepage snapshots ssh into kill delete icons exit $progname" | gum filter --height 13) case $start in create) create_VM;; run) gum_choose_VM && run_VM;; 'run offline') gum_choose_VM && run_VM --offline;; 'ssh into') ssh_into;; snapshots) snapshot_VM;; 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 if [[ "${1:-}" == "--help" || "${1:-}" == "-h" ]]; then cat <<'EOF' Usage: qrun [--help] Interactive quickemu VM manager. For multi-architecture guests, select the architecture before release and edition. The UI shows options supported by that architecture. If only upstream quickget is available, the fallback does not provide architecture selection. EOF exit 0 fi _define_variables _if_needed show_headers _show_menu