Solve later PR conflict for list cmd

This PR will solve the later PR conflict for the list cmd.
This commit is contained in:
JRGTH
2025-07-08 19:25:06 -04:00
parent 9561f83edf
commit c52e559533

View File

@@ -34,20 +34,30 @@
usage() { 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 "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 cat << EOF
Options: Options:
-d | --down List stopped jails only. -d | --down List stopped jails only.
-j | --json List jails or sub-arg(s) in json format. -j | --json List jails or sub-arg(s) in json format.
-p | --pretty Print JSON in columns. -p | --pretty Print JSON in columns.
-u | --up List running jails only. -s | --sort VALUE Print info in VALUE order.
-x | --debug Enable debug mode. -u | --up List running jails only.
-x | --debug Enable debug mode.
EOF EOF
exit 1 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() { pretty_json() {
sed -e 's/^ {/ {\n /g' \ sed -e 's/^ {/ {\n /g' \
-e 's/,"/,\n "/g' \ -e 's/,"/,\n "/g' \
@@ -56,6 +66,16 @@ pretty_json() {
-e 's/^\[\(.*\)\]$/[\n\1\n]/' -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() { get_max_lengths() {
if [ -d "${bastille_jailsdir}" ]; then if [ -d "${bastille_jailsdir}" ]; then
@@ -279,13 +299,7 @@ list_bastille(){
_tmp_list= _tmp_list=
get_max_lengths get_max_lengths
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
# Print header # 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))" "" 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 done
wait wait
# Print jails in order print_info
for _file in $(echo ${_tmp_list} | sort); do
cat ${_file}
rm -f ${_file}
done
} }
list_all(){ list_all(){
@@ -342,13 +352,7 @@ list_all(){
_tmp_list= _tmp_list=
get_max_lengths get_max_lengths
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
# Print header # 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))" "" 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 done
wait wait
# Print jails in order print_info
for _file in $(echo ${_tmp_list} | sort); do
cat ${_file}
rm -f ${_file}
done
} }
list_ips() { list_ips() {
@@ -405,13 +405,7 @@ list_ips() {
_tmp_list= _tmp_list=
get_max_lengths get_max_lengths
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
# Print header # Print header
printf " JID%*sName%*sIP Address\n" "$((${MAX_LENGTH_JID} + ${SPACER} - 3))" "" "$((${MAX_LENGTH_JAIL_NAME} + ${SPACER} - 4))" "" printf " JID%*sName%*sIP Address\n" "$((${MAX_LENGTH_JID} + ${SPACER} - 3))" "" "$((${MAX_LENGTH_JAIL_NAME} + ${SPACER} - 4))" ""
@@ -438,27 +432,15 @@ list_ips() {
done done
wait wait
# Print jails in order print_info
for _file in $(echo ${_tmp_list} | sort); do
cat ${_file}
rm -f ${_file}
done
} }
list_paths() { list_paths() {
_tmp_list= _tmp_list=
get_max_lengths get_max_lengths
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
# Print header # Print header
printf " JID%*sName%*sPath\n" "$((${MAX_LENGTH_JID} + ${SPACER} - 3))" "" "$((${MAX_LENGTH_JAIL_NAME} + ${SPACER} - 4))" "" printf " JID%*sName%*sPath\n" "$((${MAX_LENGTH_JID} + ${SPACER} - 3))" "" "$((${MAX_LENGTH_JAIL_NAME} + ${SPACER} - 4))" ""
@@ -485,12 +467,7 @@ list_paths() {
done done
wait wait
# Print jails in order print_info
for _file in $(echo ${_tmp_list} | sort); do
cat ${_file}
rm -f ${_file}
done
} }
list_ports() { list_ports() {
@@ -498,13 +475,7 @@ list_ports() {
_tmp_list= _tmp_list=
get_max_lengths get_max_lengths
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
# Print header # Print header
printf " JID%*sName%*sPublished Ports\n" "$((${MAX_LENGTH_JID} + ${SPACER} - 3))" "" "$((${MAX_LENGTH_JAIL_NAME} + ${SPACER} - 4))" "" printf " JID%*sName%*sPublished Ports\n" "$((${MAX_LENGTH_JID} + ${SPACER} - 3))" "" "$((${MAX_LENGTH_JAIL_NAME} + ${SPACER} - 4))" ""
@@ -531,12 +502,7 @@ list_ports() {
done done
wait wait
# Print jails in order print_info
for _file in $(echo ${_tmp_list} | sort); do
cat ${_file}
rm -f ${_file}
done
} }
list_state() { list_state() {
@@ -544,13 +510,7 @@ list_state() {
_tmp_list= _tmp_list=
get_max_lengths get_max_lengths
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
# Print header # Print header
printf " JID%*sName%*sState\n" "$((${MAX_LENGTH_JID} + ${SPACER} - 3))" "" "$((${MAX_LENGTH_JAIL_NAME} + ${SPACER} - 4))" "" printf " JID%*sName%*sState\n" "$((${MAX_LENGTH_JID} + ${SPACER} - 3))" "" "$((${MAX_LENGTH_JAIL_NAME} + ${SPACER} - 4))" ""
@@ -577,12 +537,42 @@ list_state() {
done done
wait wait
# Print jails in order print_info
for _file in $(echo ${_tmp_list} | sort); do }
cat ${_file}
rm -f ${_file}
done
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. # TODO: Check the correct usage or arguments here. See SC2120.
@@ -607,7 +597,7 @@ list_release(){
} }
list_snapshot(){ 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. # List snapshots with its usage data for valid bastille jails only.
if [ -d "${bastille_jailsdir}" ]; then if [ -d "${bastille_jailsdir}" ]; then
JAIL_LIST=$(ls --color=never "${bastille_jailsdir}" | sed "s/\n//g") JAIL_LIST=$(ls --color=never "${bastille_jailsdir}" | sed "s/\n//g")
@@ -656,6 +646,7 @@ TARGET=""
OPT_JSON=0 OPT_JSON=0
OPT_PRETTY=0 OPT_PRETTY=0
OPT_STATE="all" OPT_STATE="all"
OPT_SORT="-k2"
while [ "$#" -gt 0 ]; do while [ "$#" -gt 0 ]; do
case "${1}" in case "${1}" in
-h|--help|help) -h|--help|help)
@@ -673,13 +664,32 @@ while [ "$#" -gt 0 ]; do
OPT_PRETTY=1 OPT_PRETTY=1
shift 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) -u|--up)
OPT_STATE="Up" OPT_STATE="Up"
shift shift
;; ;;
-x|--debug) -x|--debug)
enable_debug enable_debug
shift shift
;; ;;
-*) -*)
for _opt in $(echo ${1} | sed 's/-//g' | fold -w1); do for _opt in $(echo ${1} | sed 's/-//g' | fold -w1); do
@@ -724,6 +734,7 @@ fi
if [ "$#" -eq 1 ]; then if [ "$#" -eq 1 ]; then
case "${1}" in case "${1}" in
-a|--all|all) -a|--all|all)
OPT_SORT="-k2"
if [ "${OPT_JSON}" -eq 1 ]; then if [ "${OPT_JSON}" -eq 1 ]; then
if [ "${OPT_PRETTY}" -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 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 fi
;; ;;
ip|ips) ip|ips)
OPT_SORT="-k3 -n"
if [ "${OPT_JSON}" -eq 1 ]; then if [ "${OPT_JSON}" -eq 1 ]; then
if [ "${OPT_PRETTY}" -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 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 fi
;; ;;
path|paths) path|paths)
OPT_SORT="-k3"
if [ "${OPT_JSON}" -eq 1 ]; then if [ "${OPT_JSON}" -eq 1 ]; then
if [ "${OPT_PRETTY}" -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 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 fi
;; ;;
rdr|port|ports) rdr|port|ports)
OPT_SORT="-k3 -n"
if [ "${OPT_JSON}" -eq 1 ]; then if [ "${OPT_JSON}" -eq 1 ]; then
if [ "${OPT_PRETTY}" -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 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 fi
;; ;;
state|status) state|status)
OPT_SORT="-k3"
if [ "${OPT_JSON}" -eq 1 ]; then if [ "${OPT_JSON}" -eq 1 ]; then
if [ "${OPT_PRETTY}" -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 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 list_state
fi 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) release|releases)
list_release "${2}" list_release "${2}"
;; ;;
snapshot|snapshots) snap|snapshot|snapshots)
list_snapshot list_snapshot
exit 0 exit 0
;; ;;