From fb00e970058fdbf43e9df975cbee61c5de39d3c0 Mon Sep 17 00:00:00 2001 From: Adar Nimrod <nimrod@shore.co.il> Date: Thu, 18 Nov 2021 22:16:59 +0200 Subject: [PATCH] Rewrite wb (workbench). - Standalone script. - Incorperate update-wb. - Much more functionality (listing existing sessions, killing sessions, killing the tmux server and container, check if I'm in a toolbox container). - Bash completion. --- .bash_completion.d/wb | 19 +++++++ .bashrc | 11 +--- Documents/bin/update-wb | 41 -------------- Documents/bin/wb | 119 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 139 insertions(+), 51 deletions(-) create mode 100644 .bash_completion.d/wb delete mode 100755 Documents/bin/update-wb create mode 100755 Documents/bin/wb diff --git a/.bash_completion.d/wb b/.bash_completion.d/wb new file mode 100644 index 0000000..a1d2ba7 --- /dev/null +++ b/.bash_completion.d/wb @@ -0,0 +1,19 @@ +# vim: ft=bash + +_wb() { + local cur prev words cword opts + _init_completion || return + opts='-h --help -l --list -d --dry-update -u --update -k --kill -s --kill-server -i --in-workbench' + + if [[ $prev == -k ]] || [[ $prev == --kill ]] + then + COMPREPLY=($(compgen -W "$(wb -l)" -- "$cur")) + elif [[ $cur == -* ]] + then + COMPREPLY=($(compgen -W "$opts" -- "$cur")) + else + COMPREPLY=($(compgen -W "$(wb -l)" -- "$cur")) + fi +} + +complete -F _wb wb diff --git a/.bashrc b/.bashrc index 6c12b18..2399426 100644 --- a/.bashrc +++ b/.bashrc @@ -172,6 +172,7 @@ alias transmission-remote='forward kodi.shore.co.il 9091:localhost:9091 && trans alias unssh="ssh -o \"UserKnownHostsFile /dev/null\" -o \"StrictHostKeyChecking no\"" alias update-requirements='find -name "*requirements*.txt" -exec pur --requirement {} \;' alias venv='python3 -m venv' +alias wbr='ssh -t ns4.shore.co.il wb' alias wifi-portal='curl --silent --fail --write-out "%{redirect_url}" --output /dev/null http://detectportal.firefox.com/success.txt' alias yellow="printf '\e[1;93m%s\e[0m\n'" alias xargs="xargs " @@ -300,16 +301,6 @@ toux () { chmod +x "$@" } -wb () { - toolbox run --container workbench -- \ - tmux -L workbench new-session -As "${1:-workbench}" -} - -wbr () { - ssh -t ns4.shore.co.il toolbox run --container workbench -- \ - tmux -L workbench new-session -As "${1:-workbench}" -} - __prompt () { local exitstatus="$?" local runduration endtime pre_prompt diff --git a/Documents/bin/update-wb b/Documents/bin/update-wb deleted file mode 100755 index ffe0645..0000000 --- a/Documents/bin/update-wb +++ /dev/null @@ -1,41 +0,0 @@ -#!/bin/sh -set -eu - -IMAGE='registry.shore.co.il/workbench' -CONTAINER='workbench' - -is_latest() { - if ! podman image exists "$IMAGE" || - ! toolbox run --container "$CONTAINER" true 2>/dev/null - then - podman image pull "$IMAGE" || exit 1 - return 1 - fi - current="$(podman container inspect "$CONTAINER" --format '{{ .Image }}')" - podman image pull "$IMAGE" > /dev/null - new="$(podman image inspect "$IMAGE" --format '{{.Digest}}')" - [ "$new" = "$current" ] || return 1 -} - -update() { - if [ -S "/tmp/tmux-$(id -u)/workbench" ] && pgrep tmux >/dev/null - then - toolbox run --container workbench -- tmux -L workbench kill-server - fi - toolbox rm --force "$CONTAINER" 2>/dev/null || true - podman image prune --filter 'label=com.github.containers.toolbox=true' --force - yes | toolbox create --image "$IMAGE" -} - -if [ "$(hostname)" = 'toolbox' ] -then - printf '\e[1;91m%s\e[0m\n' \ - 'Cannot update the workbench container from inside the workbench container.' >&2 - exit 1 -elif [ "${1:-}" = "-d" ] || [ "${1:-}" = "--dry-run" ] || [ -n "${DRY_RUN:-}" ] -then - podman image pull "$IMAGE" > /dev/null -elif ! (is_latest) -then - update -fi diff --git a/Documents/bin/wb b/Documents/bin/wb new file mode 100755 index 0000000..d2020f3 --- /dev/null +++ b/Documents/bin/wb @@ -0,0 +1,119 @@ +#!/bin/sh +set -eu + +IMAGE='registry.shore.co.il/workbench' +CONTAINER='workbench' +TMUX_SOCKET='workbench' +DEFAULT_SESSION='workbench' + +usage() { + echo "$(basename "$0"): [-h|--help] [-u|--update] [-d|--dry-update] [-l|--list] [-k|--kill] [-s|--kill-server] [-i|--in-workbench] [SESSION_NAME]" +} + +fail() { + printf '\e[1;91m%s\e[0m\n' "$1" >&2 + exit 1 +} + +not_from_toolbox() { + fail 'This command cannot run from within the workbench container.' +} + +command -v toolbox >/dev/null || fail 'Toolbox is not installed.' +command -v podman >/dev/null || fail 'Podman is not installed.' + +run() { + exec toolbox run --container "$CONTAINER" -- \ + tmux -L "$TMUX_SOCKET" new-session -As "${1:-$DEFAULT_SESSION}" +} + +_kill() { + toolbox run --container "$CONTAINER" -- \ + tmux -L "$TMUX_SOCKET" kill-session -t "${1:-$DEFAULT_SESSION}" +} + +list() { + if in_toolbox + then + tmux -L "$TMUX_SOCKET" list-sessions | awk -F: '{print $1}' + elif container_exists + then + toolbox run --container "$CONTAINER" -- \ + tmux -L "$TMUX_SOCKET" list-sessions | awk -F: '{print $1}' + fi +} + +image_exitst() { + podman image exists "$IMAGE" +} + +container_exists() { + podman container exists "$CONTAINER" +} + +is_latest() { + if ! image_exists || ! container_exists + then + podman image pull "$IMAGE" || exit 1 + return 1 + fi + current="$(podman container inspect "$CONTAINER" --format '{{ .Image }}')" + podman image pull "$IMAGE" > /dev/null + new="$(podman image inspect "$IMAGE" --format '{{.Digest}}')" + [ "$new" = "$current" ] || return 1 +} + +kill_tmux() { + if [ -S "/tmp/tmux-$(id -u)/$TMUX_SOCKET" ] && pgrep tmux >/dev/null + then + toolbox run --container "$CONTAINER" -- \ + tmux -L "$TMUX_SOCKET" kill-server + fi +} + +kill_server() { + if in_toolbox + then + not_from_toolbox + fi + kill_tmux + toolbox rm --f "$CONTAINER" +} + +update() { + if in_toolbox + then + not_from_toolbox + fi + if is_latest + then + exit 0 + fi + kill_server 2>/dev/null || true + podman image prune --filter 'label=com.github.containers.toolbox=true' --force + yes | toolbox create --image "$IMAGE" +} + +dry_update() { + podman image pull "$IMAGE" > /dev/null +} + +in_toolbox() { + [ "$(hostname)" = 'toolbox' ] +} + +if [ "$#" -eq 0 ] +then + run +fi + +case "${1:-}" in + -d|--dry-update) dry_update;; + -u|--update) update;; + -l|--list) list;; + -h|--help) usage;; + -s|--kill-server) kill_server;; + -i|--in-workbench) in_toolbox;; + -k|--kill) _kill "${2:-}";; + *) run "$1";; +esac -- GitLab