From 11c1cd4b19aaf9eccfd59d9aa4d3965aca970702 Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Sun, 29 Nov 2020 18:46:50 +0100 Subject: [PATCH] switch to getopts finished. More changes: - every interactive function has now its own help function - added help command which calls these help functions - made 'start' the default mode in jaildk_build() --- jaildk | 228 +++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 158 insertions(+), 70 deletions(-) diff --git a/jaildk b/jaildk index 72f66a6..d10604c 100644 --- a/jaildk +++ b/jaildk @@ -1,6 +1,6 @@ #!/bin/sh -version=1.07 +version=1.08 usage_jaildk() { beg=`tput -T ${TERM:-cons25} md` @@ -19,31 +19,43 @@ fetch - fetch current port collection ${beg}Installing Jails:${end} install [-r function] - install a jail (prepare mounts, devfs etc) -uninstall - uninstall a jail -remove - remove a jail or a jail version -reinstall - stop, remove, install and start a jail +uninstall [-w] - uninstall a jail +remove - remove a jail or a jail version +reinstall - stop, remove, install and start a jail ${beg}Maintaining Jails:${end} -start - start a jail -stop - stop a jail -restart - restart a jail -status - display a jail's status -rc - execute an rc-script inside a jail +start - start a jail +stop - stop a jail +restart - restart a jail +status [] - display a jail's status +rc [-r ] - execute an rc-script inside a jail ${beg}Managing Jails:${end} -login - login into a jail (also available as separate command) -blogin - chroot into a build jail (dito) +login [] - login into a jail +blogin - chroot into a build jail ${beg}Transferring Jails:${end} -freeze [-a -b -v ] - freeze (build an image of) a jail -thaw - thaw (install) an image of a jail +freeze [-a -b -v ] - freeze (build an image of) a jail +thaw - thaw (install) an image of a jail -Run the without arguments to get usage help about the command. +${beg}Getting help:${end} +help - request help on EOF ) - echo "$usage" - exit 1 + echo "$usage" + exit 1 +} + +usage_help() { + command=$1 + + usage="usage_${command}" + if ! type "$usage" > /dev/null 2>&1; then + die "Unknown command $command!" + else + $usage + fi } ex() { @@ -67,6 +79,11 @@ bold() { echo -n "$BOLD_OFF" } +fin() { + echo "$*" >&2 + exit +} + die() { bold "$*" >&2 exit 1 @@ -102,7 +119,11 @@ die_if_not_exist() { } usage_build() { - die "Usage: $0 build [-b ] [-v ]" + fin "Usage: $0 build [] [-b ] [-v ] +Mount to $j/build read-writable for maintenance. Options: +-b Use specified . default: use configured base. +-v Mount of . + One of start, stop or status. default: start." } jaildk_build() { @@ -116,14 +137,14 @@ jaildk_build() { while getopts "b:v:" arg; do case $arg in - b) base=1;; + b) base=${OPTARG};; v) version=${OPTARG};; *) usage_build;; esac done if test -z "$mode"; then - usage_build + mode=start fi die_if_not_exist $jail $version @@ -155,6 +176,7 @@ jaildk_rc_mtree() { base=$3 version=$4 rw=$5 + rcscript=mtree if [ $mode = "start" ]; then if test -n "$rw"; then @@ -359,7 +381,7 @@ jaildk_rc_mount() { } usage_install() { - die "Usage: $0 install [] [-r rc-function] + fin "Usage: $0 install [] [-r rc-function] Install according to its config. Options: Mode can either be start, stop or status. default: start -r Only execute function with parameter. default: all. @@ -379,7 +401,7 @@ jaildk_install() { base='' version='' - while getopts "r:wb:v:" arg; do + while getopts "r:b:v:w" arg; do case $arg in w) rw=1;; b) base=${OPTARG};; @@ -435,14 +457,27 @@ jaildk_install() { done } +usage_uninstall() { + fin "Usage: $0 uninstall [-w] +Uninstall . Options: +-w Uninstall writable build chroot." +} + jaildk_uninstall() { # wrapper around _install jail=$1 - rw=$2 # FIXME !!!!!!!!!! getopt !!!!!!!!!! + shift + rw='' + + while getopts "w" arg; do + case $arg in + w) rw=1;; + *) usage_uninstall;; + esac + done if test -z "$jail"; then - err "Usage: $0 uninstall []" - exit 1 + usage_uninstall fi die_if_not_exist $jail @@ -456,7 +491,8 @@ jaildk_uninstall() { usage_base() { - die "Usage: $0 base -b [-w] + fin "Usage: $0 base -b [-w] +Build a base directory from bsd install media. Options: -b can be the name of a base (e.g. 12.2-RELEASE) or a directory where it shall be created -w Create a writable base, including compiler and other @@ -555,7 +591,7 @@ var/tmp" exit 1 else ex mkdir -p $basedir - bsdinstall jail $basedir + bsdinstall jail $basedir || exit 1 if test -z "$rw"; then # run base @@ -606,7 +642,7 @@ clone() { } usage_clone() { - die "Usage: $0 clone -s -d [-o ] [-n ] + fin "Usage: $0 clone -s -d [-o ] [-n ] -s Source jail to clone from -d Destionation jail to create from source -o Old version @@ -721,7 +757,8 @@ jaildk_clone() { } usage_create() { - die "Usage: $0 create " + fin "Usage: $0 create +Create a new jail from template." } jaildk_create() { @@ -751,12 +788,25 @@ remove() { fi } +usage_remove() { + fin "Usage: $0 remove [-v ] +Remove from disk." +} + jaildk_remove() { jail=$1 - version=$2 + shift + version='' + + while getopts "v:" arg; do + case $arg in + v) version=${OPTARG};; + *) usage_remove;; + esac + done if test -z "$jail"; then - die "Usage: $0 remove []" + usage_remove fi if jls | egrep -q "${jail}"; then @@ -789,19 +839,37 @@ jaildk_remove() { } jaildk_jail_usage() { - die "Usage: $0 | status" + fin "Usage: $0 | status" +} + +usage_start() { + fin "Usage $0 start +Start ." +} + +usage_stop() { + fin "Usage $0 stop +Stop ." +} + +usage_restart() { + fin "Usage $0 restart +Restart ." +} + +usage_status() { + fin "Usage $0 status [] +Show status of . Without , show status of all jails." } jaildk_jail() { - mode=$1 jail=$2 + mode=$1 - if test -z "$mode"; then - jaildk_jail_usage - fi - - if test -z "$jail" -a $mode = "status"; then + if test -z "$jail"; then + usage_$mode + elif test -z "$jail" -a $mode = "status"; then bold "Running jails:" bold " JID IP Address Hostname Path" jls | grep -v JID @@ -817,8 +885,6 @@ jaildk_jail() { jls | grep -v JID | awk '{print $3}' | while read J; do jaildk_rc $J status done - elif test -z "$jail"; then - jaildk_jail_usage else bold "Jail $jail $mode:" case $mode in @@ -846,35 +912,33 @@ get_rc_scripts() { done } -jaildk_rc_usage() { - err "Usage: $0 rc { [] | }" - err "If is all, execute for every rc script" - err " must be a parameter of " - err "if only has been given, execute all scripts" - exit 1 +usage_rc() { + fin "Usage: $0 rc [] [-r with parameter . Options: +-r Execute . default: execute all enabled scripts." } jaildk_rc() { jail=$1 - rc=$2 - mode=$3 + mode=$1 + shift + shift + + rc='' + + while getopts "r:" arg; do + case $arg in + r) rcd=${OPTARG};; + *) usage_rc;; + esac + done if test -z "$rc"; then - jaildk_rc_usage + rc='all' fi - if test -z "$mode"; then - # shift args - case $rc in - start|stop|restart|status) - # shift args - mode=$rc - rc=all - ;; - *) - jaildk_rc_usage - ;; - esac + if test -z "$jail" -o -z "$mode"; then + usage_rc fi if ! jls | egrep -q "${jail}"; then @@ -903,6 +967,14 @@ jaildk_rc() { fi } +usage_blogin() { + err "Usage: $file +Chroot into a build jail. + +Mounted build chroot's:" + mount|egrep "base.*build" | awk '{print $3}' | cut -d/ -f 4 + exit 1 +} jaildk_blogin() { jail=$1 @@ -914,10 +986,7 @@ jaildk_blogin() { else file="$0" fi - echo "Usage: $file []" - echo "mounted build jails:" - mount|egrep "base.*build" | awk '{print $3}' | cut -d/ -f 4 - exit + usage_blogin fi chroot="$j/build/$jail" @@ -941,6 +1010,16 @@ jaildk_blogin() { env - HOME=$home TERM=$term SHELL=$shell PATH=$path chroot $chroot $shell } +usage_login() { + err "Usage: $file [] +Login into a jail by name, ip or domain. If has not been +specified, login as root. + +Available jails:" + jls + exit 1 +} + jaildk_login() { jail=$1 user=$2 @@ -954,10 +1033,7 @@ jaildk_login() { else file="$0" fi - echo "Usage: $file []" - echo "available jails:" - jls - exit + usage_login fi jid="" @@ -995,10 +1071,19 @@ jaildk_login() { env - JAIL=$jail HOME=$home TERM=$term SHELL=$shell PATH=$path $jexec -U $user $jid $shell } +usage_reinstall() { + fin "Usage: $0 reinstall +Stop, uninstall, install and start . +" +} jaildk_reinstall() { jail=$1 + if test -z "$jail"; then + usage_reinstall + fi + die_if_not_exist $jail if jls | egrep -q "${jail}"; then @@ -1023,7 +1108,7 @@ jaildk_setup() { j=$1 if test -z "$j"; then - die "Usage: $0 setup " + fin "Usage: $0 setup " fi bold "preparing directories" @@ -1295,7 +1380,7 @@ thaw_tarball() { } usage_thaw() { - die "Usage: $0 thaw " + fin "Usage: $0 thaw " } jaildk_thaw() { @@ -1396,6 +1481,9 @@ case $runner in setup|reinstall|install|uninstall|build|blogin|login|clone|create|remove|rc|base|fetch|freeze|thaw) jaildk_$runner $* ;; + help) + usage_help $* + ;; *) usage_jaildk $* ;;