Commit fb00e970 authored by nimrod's avatar nimrod
Browse files

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.
parent bb0b533a
Loading
Loading
Loading
Loading

.bash_completion.d/wb

0 → 100644
+19 −0
Original line number Diff line number Diff line
# 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
+1 −10
Original line number Diff line number Diff line
@@ -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

Documents/bin/update-wb

deleted100755 → 0
+0 −41
Original line number Diff line number Diff line
#!/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

Documents/bin/wb

0 → 100755
+119 −0
Original line number Diff line number Diff line
#!/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