From c52e55953399657ecc50e873b01394aa3ac1e316 Mon Sep 17 00:00:00 2001 From: JRGTH Date: Tue, 8 Jul 2025 19:25:06 -0400 Subject: [PATCH] Solve later PR conflict for list cmd This PR will solve the later PR conflict for the list cmd. --- usr/local/share/bastille/list.sh | 197 ++++++++++++++++++------------- 1 file changed, 112 insertions(+), 85 deletions(-) diff --git a/usr/local/share/bastille/list.sh b/usr/local/share/bastille/list.sh index 25c1ddb4..b3450fd6 100644 --- a/usr/local/share/bastille/list.sh +++ b/usr/local/share/bastille/list.sh @@ -34,20 +34,30 @@ 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)] [snapshot(s)] [state(s)] [template(s)]" + error_notify " [path(s)] [port(s)] [prio|priority] [release(s)] [snapshot(s)] [state(s)] [template(s)] [type]" 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}); do + cat ${_file} + rm -f ${_file} + done | sort ${OPT_SORT} +} + pretty_json() { sed -e 's/^ {/ {\n /g' \ -e 's/,"/,\n "/g' \ @@ -56,6 +66,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="${TARGET}" + else + JAIL_LIST="$(ls --color=never "${bastille_jailsdir}" | sed "s/\n//g")" + fi +} + get_max_lengths() { if [ -d "${bastille_jailsdir}" ]; then @@ -279,13 +299,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))" "" @@ -330,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(){ @@ -342,13 +352,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))" "" @@ -393,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() { @@ -405,13 +405,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))" "" @@ -438,27 +432,15 @@ list_ips() { done wait - # Print jails in order - for _file in $(echo ${_tmp_list} | sort); do - cat ${_file} - rm -f ${_file} - done - + print_info } - 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))" "" @@ -485,12 +467,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() { @@ -498,13 +475,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))" "" @@ -531,12 +502,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() { @@ -544,13 +510,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))" "" @@ -577,12 +537,42 @@ list_state() { done wait - # Print jails in order - for _file in $(echo ${_tmp_list} | sort); do - cat ${_file} - rm -f ${_file} - done + 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. @@ -607,7 +597,7 @@ list_release(){ } list_snapshot(){ - # TODO: Avility to list snapshot data for a single target. + # TODO: Ability to list snapshot data for a single target. # List snapshots with its usage data for valid bastille jails only. if [ -d "${bastille_jailsdir}" ]; then JAIL_LIST=$(ls --color=never "${bastille_jailsdir}" | sed "s/\n//g") @@ -656,6 +646,7 @@ TARGET="" OPT_JSON=0 OPT_PRETTY=0 OPT_STATE="all" +OPT_SORT="-k2" while [ "$#" -gt 0 ]; do case "${1}" in -h|--help|help) @@ -673,13 +664,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_SORT="-k1 -n" ;; + name) OPT_SORT="-k2" ;; + boot) OPT_SORT="-k3" ;; + prio|priority) OPT_SORT="-k4 -n" ;; + state) OPT_SORT="-k5" ;; + type|jailtype) OPT_SORT="-k6" ;; + ip) OPT_SORT="-k7 -n" ;; + ports) OPT_SORT="-k8 -n" ;; + release) OPT_SORT="-k9" ;; + tags) OPT_SORT="-k10" ;; + *) 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 @@ -724,6 +734,7 @@ fi if [ "$#" -eq 1 ]; then case "${1}" in -a|--all|all) + OPT_SORT="-k2" if [ "${OPT_JSON}" -eq 1 ]; then if [ "${OPT_PRETTY}" -eq 1 ]; then list_all | awk 'BEGIN{print "["} NR>1{if(NR>2)print ","; printf " {\"JID\":\"%s\",\"Boot\":\"%s\",\"Prio\":\"%s\",\"State\":\"%s\",\"IP Address\":\"%s\",\"Published Ports\":\"%s\",\"Hostname\":\"%s\",\"Release\":\"%s\",\"Path\":\"%s\"}",$1,$2,$3,$4,$5,$6,$7,$8,$9} END{print "\n]"}' | pretty_json @@ -735,6 +746,7 @@ if [ "$#" -eq 1 ]; then fi ;; ip|ips) + OPT_SORT="-k3 -n" if [ "${OPT_JSON}" -eq 1 ]; then if [ "${OPT_PRETTY}" -eq 1 ]; then list_ips | awk 'BEGIN{print "["} NR>1{if(NR>2)print ","; printf " {\"JID\":\"%s\",\"Name\":\"%s\",\"IP Address\":\"%s\"}",$1,$2,$3} END{print "\n]"}' | pretty_json @@ -746,6 +758,7 @@ if [ "$#" -eq 1 ]; then fi ;; path|paths) + OPT_SORT="-k3" if [ "${OPT_JSON}" -eq 1 ]; then if [ "${OPT_PRETTY}" -eq 1 ]; then list_paths | awk 'BEGIN{print "["} NR>1{if(NR>2)print ","; printf " {\"JID\":\"%s\",\"Name\":\"%s\",\"Path\":\"%s\"}",$1,$2,$3} END{print "\n]"}' | pretty_json @@ -757,6 +770,7 @@ if [ "$#" -eq 1 ]; then fi ;; rdr|port|ports) + OPT_SORT="-k3 -n" if [ "${OPT_JSON}" -eq 1 ]; then if [ "${OPT_PRETTY}" -eq 1 ]; then list_ports | awk 'BEGIN{print "["} NR>1{if(NR>2)print ","; printf " {\"JID\":\"%s\",\"Name\":\"%s\",\"Published Ports\":\"%s\"}",$1,$2,$3} END{print "\n]"}' | pretty_json @@ -768,6 +782,7 @@ if [ "$#" -eq 1 ]; then fi ;; state|status) + OPT_SORT="-k3" if [ "${OPT_JSON}" -eq 1 ]; then if [ "${OPT_PRETTY}" -eq 1 ]; then list_state | awk 'BEGIN{print "["} NR>1{if(NR>2)print ","; printf " {\"JID\":\"%s\",\"Name\":\"%s\",\"State\":\"%s\"}",$1,$2,$3} END{print "\n]"}' | pretty_json @@ -778,10 +793,22 @@ if [ "$#" -eq 1 ]; then list_state fi ;; + type|jailtype) + OPT_SORT="-k3" + 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}" ;; - snapshot|snapshots) + snap|snapshot|snapshots) list_snapshot exit 0 ;;