From c18ef73f684a5a999f51302b8b9b9cd50991a7ee Mon Sep 17 00:00:00 2001 From: tschettervictor <85497460+tschettervictor@users.noreply.github.com> Date: Wed, 2 Jul 2025 12:37:56 -0600 Subject: [PATCH 1/5] list: List jails according to priority --- usr/local/share/bastille/list.sh | 58 +++++++++----------------------- 1 file changed, 16 insertions(+), 42 deletions(-) diff --git a/usr/local/share/bastille/list.sh b/usr/local/share/bastille/list.sh index 387c420e..6c92a334 100644 --- a/usr/local/share/bastille/list.sh +++ b/usr/local/share/bastille/list.sh @@ -56,6 +56,16 @@ pretty_json() { -e 's/^\[\(.*\)\]$/[\n\1\n]/' } +get_jail_list() { + + # Check if we want only a single jail, or all jails + if [ -n "${TARGET}" ]; then + JAIL_LIST="$(list_jail_priority ${TARGET} | sort -k2 -n | awk '{print $1}')" + else + JAIL_LIST="$(list_jail_priority "$(ls --color=never "${bastille_jailsdir}" | sed "s/\n//g")" | sort -k2 -n | awk '{print $1}')" + fi +} + get_max_lengths() { if [ -d "${bastille_jailsdir}" ]; then @@ -279,13 +289,7 @@ list_bastille(){ _tmp_list= get_max_lengths - - # Check if we want only a single jail, or all jails - if [ -n "${TARGET}" ]; then - JAIL_LIST="${TARGET}" - else - JAIL_LIST=$(ls --color=never "${bastille_jailsdir}" | sed "s/\n//g") - fi + get_jail_list # Print header printf " JID%*sName%*sBoot%*sPrio%*sState%*sType%*sIP Address%*sPublished Ports%*sRelease%*sTags\n" "$((${MAX_LENGTH_JID} + ${SPACER} - 3))" "" "$((${MAX_LENGTH_JAIL_NAME} + ${SPACER} - 4))" "" "$((${SPACER}))" "" "$((${SPACER}))" "" "$((${SPACER}))" "" "$((${MAX_LENGTH_JAIL_TYPE} + ${SPACER} - 4))" "" "$((${MAX_LENGTH_JAIL_IP} + ${SPACER} - 10))" "" "$((${MAX_LENGTH_JAIL_PORTS} + ${SPACER} - 15))" "" "$((${MAX_LENGTH_JAIL_RELEASE} + ${SPACER} - 7))" "" @@ -342,13 +346,7 @@ list_all(){ _tmp_list= get_max_lengths - - # Check if we want only a single jail, or all jails - if [ -n "${TARGET}" ]; then - JAIL_LIST="${TARGET}" - else - JAIL_LIST=$(ls --color=never "${bastille_jailsdir}" | sed "s/\n//g") - fi + get_jail_list # Print header printf " JID%*sBoot%*sPrio%*sState%*sIP Address%*sPublished Ports%*sHostname%*sRelease%*sPath\n" "$((${MAX_LENGTH_JID} + ${SPACER} - 3))" "" "$((${SPACER}))" "" "$((${SPACER}))" "" "$((${SPACER}))" "" "$((${MAX_LENGTH_JAIL_IP} + ${SPACER} - 10))" "" "$((${MAX_LENGTH_JAIL_PORTS} + ${SPACER} - 15))" "" "$((${MAX_LENGTH_JAIL_HOSTNAME} + ${SPACER} - 8))" "" "$((${MAX_LENGTH_JAIL_RELEASE} + ${SPACER} - 7))" "" @@ -405,13 +403,7 @@ list_ips() { _tmp_list= get_max_lengths - - # Check if we want only a single jail, or all jails - if [ -n "${TARGET}" ]; then - JAIL_LIST="${TARGET}" - else - JAIL_LIST=$(ls --color=never "${bastille_jailsdir}" | sed "s/\n//g") - fi + get_jail_list # Print header printf " JID%*sName%*sIP Address\n" "$((${MAX_LENGTH_JID} + ${SPACER} - 3))" "" "$((${MAX_LENGTH_JAIL_NAME} + ${SPACER} - 4))" "" @@ -452,13 +444,7 @@ list_paths() { _tmp_list= get_max_lengths - - # Check if we want only a single jail, or all jails - if [ -n "${TARGET}" ]; then - JAIL_LIST="${TARGET}" - else - JAIL_LIST=$(ls --color=never "${bastille_jailsdir}" | sed "s/\n//g") - fi + get_jail_list # Print header printf " JID%*sName%*sPath\n" "$((${MAX_LENGTH_JID} + ${SPACER} - 3))" "" "$((${MAX_LENGTH_JAIL_NAME} + ${SPACER} - 4))" "" @@ -498,13 +484,7 @@ list_ports() { _tmp_list= get_max_lengths - - # Check if we want only a single jail, or all jails - if [ -n "${TARGET}" ]; then - JAIL_LIST="${TARGET}" - else - JAIL_LIST=$(ls --color=never "${bastille_jailsdir}" | sed "s/\n//g") - fi + get_jail_list # Print header printf " JID%*sName%*sPublished Ports\n" "$((${MAX_LENGTH_JID} + ${SPACER} - 3))" "" "$((${MAX_LENGTH_JAIL_NAME} + ${SPACER} - 4))" "" @@ -544,13 +524,7 @@ list_state() { _tmp_list= get_max_lengths - - # Check if we want only a single jail, or all jails - if [ -n "${TARGET}" ]; then - JAIL_LIST="${TARGET}" - else - JAIL_LIST=$(ls --color=never "${bastille_jailsdir}" | sed "s/\n//g") - fi + get_jail_list # Print header printf " JID%*sName%*sState\n" "$((${MAX_LENGTH_JID} + ${SPACER} - 3))" "" "$((${MAX_LENGTH_JAIL_NAME} + ${SPACER} - 4))" "" From bb92dc329810a0913898df1d1e9c0756fe4e27e0 Mon Sep 17 00:00:00 2001 From: tschettervictor <85497460+tschettervictor@users.noreply.github.com> Date: Fri, 4 Jul 2025 19:52:44 -0600 Subject: [PATCH 2/5] list: Allow listing by additional args --- usr/local/share/bastille/list.sh | 86 ++++++++++++++++---------------- 1 file changed, 44 insertions(+), 42 deletions(-) diff --git a/usr/local/share/bastille/list.sh b/usr/local/share/bastille/list.sh index 6c92a334..0c332a67 100644 --- a/usr/local/share/bastille/list.sh +++ b/usr/local/share/bastille/list.sh @@ -38,16 +38,26 @@ usage() { cat << EOF Options: - -d | --down List stopped jails only. - -j | --json List jails or sub-arg(s) in json format. - -p | --pretty Print JSON in columns. - -u | --up List running jails only. - -x | --debug Enable debug mode. + -d | --down List stopped jails only. + -j | --json List jails or sub-arg(s) in json format. + -p | --pretty Print JSON in columns. + -s | --sort VALUE Print info in VALUE order. + -u | --up List running jails only. + -x | --debug Enable debug mode. EOF exit 1 } +print_info() { + + # Print jails in given order + for _file in $(echo ${_tmp_list} | sort); do + cat ${_file} + rm -f ${_file} + done | sort -n -k${OPT_COLUMN} +} + pretty_json() { sed -e 's/^ {/ {\n /g' \ -e 's/,"/,\n "/g' \ @@ -60,9 +70,9 @@ get_jail_list() { # Check if we want only a single jail, or all jails if [ -n "${TARGET}" ]; then - JAIL_LIST="$(list_jail_priority ${TARGET} | sort -k2 -n | awk '{print $1}')" + JAIL_LIST="${TARGET}" else - JAIL_LIST="$(list_jail_priority "$(ls --color=never "${bastille_jailsdir}" | sed "s/\n//g")" | sort -k2 -n | awk '{print $1}')" + JAIL_LIST="$(ls --color=never "${bastille_jailsdir}" | sed "s/\n//g")" fi } @@ -334,11 +344,7 @@ list_bastille(){ done wait - # Print jails in order - for _file in $(echo ${_tmp_list} | sort); do - cat ${_file} - rm -f ${_file} - done + print_info } list_all(){ @@ -391,11 +397,7 @@ list_all(){ done wait - # Print jails in order - for _file in $(echo ${_tmp_list} | sort); do - cat ${_file} - rm -f ${_file} - done + print_info } list_ips() { @@ -430,12 +432,7 @@ list_ips() { done wait - # Print jails in order - for _file in $(echo ${_tmp_list} | sort); do - cat ${_file} - rm -f ${_file} - done - + print_info } @@ -471,12 +468,7 @@ list_paths() { done wait - # Print jails in order - for _file in $(echo ${_tmp_list} | sort); do - cat ${_file} - rm -f ${_file} - done - + print_info } list_ports() { @@ -511,12 +503,7 @@ list_ports() { done wait - # Print jails in order - for _file in $(echo ${_tmp_list} | sort); do - cat ${_file} - rm -f ${_file} - done - + print_info } list_state() { @@ -551,12 +538,7 @@ list_state() { done wait - # Print jails in order - for _file in $(echo ${_tmp_list} | sort); do - cat ${_file} - rm -f ${_file} - done - + print_info } # TODO: Check the correct usage or arguments here. See SC2120. @@ -616,6 +598,7 @@ TARGET="" OPT_JSON=0 OPT_PRETTY=0 OPT_STATE="all" +OPT_COLUMN="2" while [ "$#" -gt 0 ]; do case "${1}" in -h|--help|help) @@ -633,13 +616,32 @@ while [ "$#" -gt 0 ]; do OPT_PRETTY=1 shift ;; + -s|--sort) + if [ -n "${3}" ]; then + error_exit "[ERROR]: [-s|--sort] can only be used with 'bastille list'." + fi + case "${2}" in + jid) OPT_COLUMN="1" ;; + name) OPT_COLUMN="2" ;; + boot) OPT_COLUMN="3" ;; + prio|priority) OPT_COLUMN="4" ;; + state) OPT_COLUMN="5" ;; + type|jailtype) OPT_COLUMN="6" ;; + ip) OPT_COLUMN="7" ;; + ports) OPT_COLUMN="8" ;; + release) OPT_COLUMN="9" ;; + tags) OPT_COLUMN="10" ;; + *) error_exit "Invalid sort option: \"${2}\"" ;; + esac + shift 2 + ;; -u|--up) OPT_STATE="Up" shift ;; -x|--debug) enable_debug - shift + shift ;; -*) for _opt in $(echo ${1} | sed 's/-//g' | fold -w1); do From d69b86e54204b6efcbe7d11d76e3e41de6941965 Mon Sep 17 00:00:00 2001 From: tschettervictor <85497460+tschettervictor@users.noreply.github.com> Date: Sun, 6 Jul 2025 15:05:53 -0600 Subject: [PATCH 3/5] list: Allow listing jail type separately --- usr/local/share/bastille/list.sh | 47 +++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/usr/local/share/bastille/list.sh b/usr/local/share/bastille/list.sh index 0c332a67..12caae74 100644 --- a/usr/local/share/bastille/list.sh +++ b/usr/local/share/bastille/list.sh @@ -435,7 +435,6 @@ list_ips() { print_info } - list_paths() { _tmp_list= @@ -541,6 +540,41 @@ list_state() { print_info } +list_type() { + + _tmp_list= + + get_max_lengths + get_jail_list + + # Print header + printf " JID%*sName%*sType\n" "$((${MAX_LENGTH_JID} + ${SPACER} - 3))" "" "$((${MAX_LENGTH_JAIL_NAME} + ${SPACER} - 4))" "" + + for _jail in ${JAIL_LIST}; do + + # Validate jail.conf existence + if [ -f "${bastille_jailsdir}/${_jail}/jail.conf" ]; then + _tmp_jail=$(mktemp /tmp/bastille-list-${_jail}) + else + continue + fi + + ( + + get_jail_info "${_jail}" + + printf " ${JID}%*s${JAIL_NAME}%*s${JAIL_TYPE}\n" "$((${MAX_LENGTH_JID} - ${#JID} + ${SPACER}))" "" "$((${MAX_LENGTH_JAIL_NAME} - ${#JAIL_NAME} + ${SPACER}))" "" + + ) > "${_tmp_jail}" & + + _tmp_list="$(printf "%s\n%s" "${_tmp_list}" "${_tmp_jail}")" + + done + wait + + print_info +} + # TODO: Check the correct usage or arguments here. See SC2120. # shellcheck disable=SC2120 list_release(){ @@ -740,6 +774,17 @@ if [ "$#" -eq 1 ]; then list_state fi ;; + type) + if [ "${OPT_JSON}" -eq 1 ]; then + if [ "${OPT_PRETTY}" -eq 1 ]; then + list_type | awk 'BEGIN{print "["} NR>1{if(NR>2)print ","; printf " {\"JID\":\"%s\",\"Name\":\"%s\",\"Type\":\"%s\"}",$1,$2,$3} END{print "\n]"}' | pretty_json + else + list_type | awk 'BEGIN{print "["} NR>1{if(NR>2)print ","; printf " {\"JID\":\"%s\",\"Name\":\"%s\",\"Type\":\"%s\"}",$1,$2,$3} END{print "\n]"}' + fi + else + list_type + fi + ;; release|releases) list_release "${2}" ;; From 3dec44ae57a018bc8f8b79c89feeed534383df2d Mon Sep 17 00:00:00 2001 From: tschettervictor <85497460+tschettervictor@users.noreply.github.com> Date: Sun, 6 Jul 2025 15:06:42 -0600 Subject: [PATCH 4/5] Update list.sh --- usr/local/share/bastille/list.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/usr/local/share/bastille/list.sh b/usr/local/share/bastille/list.sh index 12caae74..4d268e78 100644 --- a/usr/local/share/bastille/list.sh +++ b/usr/local/share/bastille/list.sh @@ -34,7 +34,7 @@ usage() { error_notify "Usage: bastille list [option(s)] [RELEASE (-p)] [all] [backup(s)] [export(s)] [import(s)] [ip(s)] [jail(s)] [limit(s)] [log(s)]" - error_notify " [path(s)] [port(s)] [prio|priority] [release(s)] [state(s)] [template(s)]" + error_notify " [path(s)] [port(s)] [prio|priority] [release(s)] [state(s)] [template(s)] [type]" cat << EOF Options: From de6bf1a18af5976633746af7e0db2a7d3e7931a3 Mon Sep 17 00:00:00 2001 From: tschettervictor <85497460+tschettervictor@users.noreply.github.com> Date: Sun, 6 Jul 2025 15:16:37 -0600 Subject: [PATCH 5/5] docs: lost: Update for -s --- docs/chapters/subcommands/list.rst | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/docs/chapters/subcommands/list.rst b/docs/chapters/subcommands/list.rst index b5f7e85b..96678bd4 100644 --- a/docs/chapters/subcommands/list.rst +++ b/docs/chapters/subcommands/list.rst @@ -13,11 +13,12 @@ to print in columns instead of rows. ishmael ~ # bastille list help Usage: bastille list [option(s)] [RELEASE (-p)] [all] [backup(s)] [export(s)] [import(s)] [ip(s)] [jail(s)] [limit(s)] [log(s)] - [path(s)] [port(s)] [prio|priority] [release(s)] [state(s)] [template(s)] + [path(s)] [port(s)] [prio|priority] [release(s)] [state(s)] [template(s)] [type] Options: - -d | --down List stopped jails only. - -j | --json List jails or sub-arg(s) in json format. - -p | --pretty Print JSON in columns. - -u | --up List running jails only. - -x | --debug Enable debug mode. \ No newline at end of file + -d | --down List stopped jails only. + -j | --json List jails or sub-arg(s) in json format. + -p | --pretty Print JSON in columns. + -s | --sort VALUE Print info in VALUE order. + -u | --up List running jails only. + -x | --debug Enable debug mode. \ No newline at end of file