From ef9eda1621fb98ee68abacf0ac778730c203d0f4 Mon Sep 17 00:00:00 2001 From: Josh Washburne Date: Wed, 25 Sep 2024 11:42:11 -0400 Subject: [PATCH] Created script folder. --- scripts/hopon/010_hopon_completion.bash | 22 ++++++++++++++++ scripts/hopon/README.md | 18 +++++++++++++ scripts/hopon/hopon | 34 +++++++++++++++++++++++++ scripts/hopon/install.sh | 4 +++ 4 files changed, 78 insertions(+) create mode 100644 scripts/hopon/010_hopon_completion.bash create mode 100644 scripts/hopon/README.md create mode 100644 scripts/hopon/hopon create mode 100644 scripts/hopon/install.sh diff --git a/scripts/hopon/010_hopon_completion.bash b/scripts/hopon/010_hopon_completion.bash new file mode 100644 index 0000000..495fd47 --- /dev/null +++ b/scripts/hopon/010_hopon_completion.bash @@ -0,0 +1,22 @@ +#/usr/bin/env bash + +_hopon_completions () { + # Check to see if a container/VM manager is installed. + local vm_manager="" + if [ -x "$(command -v incus)" ]; then + vm_manager="incus" + elif [ -x "$(command -v lxc)" ]; then + vm_manager="lxc" + else + return + fi + + # We only want one copy of the found parameter. + if [ "${#COMP_WORDS[@]}" != "2" ]; then + return + fi + + COMPREPLY=($(compgen -W "$($vm_manager list -c n -f csv)" -- "${COMP_WORDS[1]}")) +} + +complete -F _hopon_completions hopon diff --git a/scripts/hopon/README.md b/scripts/hopon/README.md new file mode 100644 index 0000000..5c125e0 --- /dev/null +++ b/scripts/hopon/README.md @@ -0,0 +1,18 @@ +# hopon + +A simple bash script for quickly accessing a linux container/VM. Has tab-completion enabled for Bash shells and can detect different container/VM managers. + +## Requirements + +* Bash +* LXD/LXC or Incus + +## Usage + +``` +# hopon +``` + +## Install + +Run `sudo ./install.sh` \ No newline at end of file diff --git a/scripts/hopon/hopon b/scripts/hopon/hopon new file mode 100644 index 0000000..56b436c --- /dev/null +++ b/scripts/hopon/hopon @@ -0,0 +1,34 @@ +#/usr/bin/env bash + +# Check for the first parameter. +if [ -z "$1" ]; then + echo "No container/VM name specified." + exit 2 +fi + +# Check to see if a container/VM manager is installed. +if [ -x "$(command -v incus)" ]; then + vm_manager="incus" +elif [ -x "$(command -v lxc)" ]; then + vm_manager="lxc" +else + echo "No suitable container/VM manager detected." + echo "Make sure you have \"lxc\" or \"incus\" installed." + exit 2 +fi + +# Look for the specified container/VM. +exists=$("$vm_manager" list -c n -f csv | grep ^$1 -- 2>/dev/null) + +if [ -n "$exists" ]; then + # The shell will be different depending on the Linux distro used. + IMAGEOS=$("$vm_manager" config get "$1" image.os) + if [ $IMAGEOS = "Alpine" ]; then + "$vm_manager" exec "$1" -- /bin/ash + else + "$vm_manager" exec "$1" -- /bin/bash + fi +else + echo "Container/VM with name \"$1\" does not exist." + exit 2 +fi diff --git a/scripts/hopon/install.sh b/scripts/hopon/install.sh new file mode 100644 index 0000000..5ff60c8 --- /dev/null +++ b/scripts/hopon/install.sh @@ -0,0 +1,4 @@ +#/bin/sh + +install -m 755 hopon /usr/bin +install -m 755 010_hopon_completion.bash /etc/bash_completion.d