Ensure target is specified and exists in 1 place instead of many.

Require target to be running for commands that need it.
Closes #239.
This commit is contained in:
Chris Wells
2020-08-30 15:37:54 -04:00
parent aaffc882f9
commit c6aded8d0a
20 changed files with 162 additions and 320 deletions
+40 -8
View File
@@ -133,19 +133,47 @@ version|-v|--version)
help|-h|--help) help|-h|--help)
usage usage
;; ;;
esac bootstrap|create|destroy|import|list|rdr|restart|start|update|upgrade|verify)
# Nothing "extra" to do for these commands. -- cwells
;;
clone|cmd|console|convert|cp|edit|export|htop|limits|mount|pkg|rename|service|stop|sysrc|template|top|umount|zfs)
# Parse the target and ensure it exists. -- cwells
if [ $# -eq 0 ]; then # No target was given, so show the command's help. -- cwells
PARAMS='help'
elif [ "${1}" != 'help' ] && [ "${1}" != '-h' ] && [ "${1}" != '--help' ]; then
TARGET="${1}"
shift
if [ "${TARGET}" = 'ALL' ]; then
JAILS=$(jls name)
else
JAILS=$(jls name | awk "/^${TARGET}$/")
# Ensure the target exists. -- cwells
if [ ! -d "${bastille_jailsdir}/${TARGET}" ]; then
error_exit "[${TARGET}]: Not found."
fi
# Filter out all non-commands
case "${CMD}" in case "${CMD}" in
bootstrap|clone|cmd|console|convert|cp|create) cmd|console|htop|pkg|service|stop|sysrc|template|top)
# Require the target to be running. -- cwells
if [ ! "$(jls name | awk "/^${TARGET}$/")" ]; then
error_exit "[${TARGET}]: Not started. See 'bastille start ${TARGET}'."
fi
;; ;;
destroy|edit|export|htop|import|limits|list|mount) convert|rename)
# Require the target to be stopped. -- cwells
if [ "$(jls name | awk "/^${TARGET}$/")" ]; then
error_exit "${TARGET} is running. See 'bastille stop ${TARGET}'."
fi
;; ;;
pkg|rdr|rename|restart|service|start|stop|sysrc|umount) esac
fi
export TARGET
export JAILS
fi
;; ;;
template|top|update|upgrade|verify|zfs) *) # Filter out all non-commands
;;
*)
usage usage
;; ;;
esac esac
@@ -157,7 +185,11 @@ if [ -f "${SCRIPTPATH}" ]; then
: "${SH:=sh}" : "${SH:=sh}"
if [ -n "${PARAMS}" ]; then
exec "${SH}" "${SCRIPTPATH}" "${PARAMS}"
else
exec "${SH}" "${SCRIPTPATH}" "$@" exec "${SH}" "${SCRIPTPATH}" "$@"
fi
else else
error_exit "${SCRIPTPATH} not found." error_exit "${SCRIPTPATH} not found."
fi fi
+4 -10
View File
@@ -42,14 +42,12 @@ help|-h|--help)
;; ;;
esac esac
if [ $# -ne 3 ]; then if [ $# -ne 2 ]; then
usage usage
fi fi
TARGET="${1}" NEWNAME="${1}"
NEWNAME="${2}" IP="${2}"
IP="${3}"
shift
validate_ip() { validate_ip() {
IPX_ADDR="ip4.addr" IPX_ADDR="ip4.addr"
@@ -146,7 +144,6 @@ update_fstab() {
clone_jail() { clone_jail() {
# Attempt container clone # Attempt container clone
if [ -d "${bastille_jailsdir}/${TARGET}" ]; then
echo -e "${COLOR_GREEN}Attempting to clone '${TARGET}' to ${NEWNAME}...${COLOR_RESET}" echo -e "${COLOR_GREEN}Attempting to clone '${TARGET}' to ${NEWNAME}...${COLOR_RESET}"
if ! [ -d "${bastille_jailsdir}/${NEWNAME}" ]; then if ! [ -d "${bastille_jailsdir}/${NEWNAME}" ]; then
if [ "${bastille_zfs_enable}" = "YES" ]; then if [ "${bastille_zfs_enable}" = "YES" ]; then
@@ -168,7 +165,7 @@ clone_jail() {
# Just clone the jail directory # Just clone the jail directory
# Check if container is running # Check if container is running
if [ -n "$(jls name | awk "/^${TARGET}$/")" ]; then if [ -n "$(jls name | awk "/^${TARGET}$/")" ]; then
error_exit "${TARGET} is running, See 'bastille stop ${TARGET}'." error_exit "${TARGET} is running. See 'bastille stop ${TARGET}'."
fi fi
# Perform container file copy(archive mode) # Perform container file copy(archive mode)
@@ -177,9 +174,6 @@ clone_jail() {
else else
error_exit "${NEWNAME} already exists." error_exit "${NEWNAME} already exists."
fi fi
else
error_exit "${TARGET} not found. See bootstrap."
fi
# Generate jail configuration files # Generate jail configuration files
update_jailconf update_jailconf
+1 -11
View File
@@ -41,20 +41,10 @@ help|-h|--help)
;; ;;
esac esac
if [ $# -lt 2 ]; then if [ $# -eq 0 ]; then
usage usage
fi fi
TARGET="${1}"
shift
if [ "${TARGET}" = 'ALL' ]; then
JAILS=$(jls name)
fi
if [ "${TARGET}" != 'ALL' ]; then
JAILS=$(jls name | awk "/^${TARGET}$/")
fi
for _jail in ${JAILS}; do for _jail in ${JAILS}; do
echo -e "${COLOR_GREEN}[${_jail}]:${COLOR_RESET}" echo -e "${COLOR_GREEN}[${_jail}]:${COLOR_RESET}"
jexec -l "${_jail}" "$@" jexec -l "${_jail}" "$@"
+2 -11
View File
@@ -31,7 +31,7 @@
. /usr/local/share/bastille/common.sh . /usr/local/share/bastille/common.sh
usage() { usage() {
error_exit "Usage: bastille console TARGET [user]'." error_exit "Usage: bastille console TARGET [user]'"
} }
# Handle special-case commands first. # Handle special-case commands first.
@@ -41,21 +41,12 @@ help|-h|--help)
;; ;;
esac esac
if [ $# -gt 2 ] || [ $# -lt 1 ]; then if [ $# -gt 1 ]; then
usage usage
fi fi
TARGET="${1}"
shift
USER="${1}" USER="${1}"
if [ "${TARGET}" = 'ALL' ]; then
JAILS=$(jls name)
fi
if [ "${TARGET}" != 'ALL' ]; then
JAILS=$(jls name | awk "/^${TARGET}$/")
fi
validate_user() { validate_user() {
if jexec -l "${_jail}" id "${USER}" >/dev/null 2>&1; then if jexec -l "${_jail}" id "${USER}" >/dev/null 2>&1; then
USER_SHELL="$(jexec -l "${_jail}" getent passwd "${USER}" | cut -d: -f7)" USER_SHELL="$(jexec -l "${_jail}" getent passwd "${USER}" | cut -d: -f7)"
+2 -10
View File
@@ -32,7 +32,7 @@
. /usr/local/etc/bastille/bastille.conf . /usr/local/etc/bastille/bastille.conf
usage() { usage() {
error_exit "Usage: bastille convert TARGET." error_exit "Usage: bastille convert TARGET"
} }
# Handle special-case commands first. # Handle special-case commands first.
@@ -42,13 +42,10 @@ help|-h|--help)
;; ;;
esac esac
if [ $# -gt 1 ] || [ $# -lt 1 ]; then if [ $# -ne 0 ]; then
usage usage
fi fi
TARGET="${1}"
shift
convert_symlinks() { convert_symlinks() {
# Work with the symlinks, revert on first cp error # Work with the symlinks, revert on first cp error
if [ -d "${bastille_releasesdir}/${RELEASE}" ]; then if [ -d "${bastille_releasesdir}/${RELEASE}" ]; then
@@ -131,11 +128,6 @@ start_convert() {
fi fi
} }
# Check if container is running
if [ -n "$(jls name | awk "/^${TARGET}$/")" ]; then
error_exit "${TARGET} is running. See 'bastille stop'."
fi
# Check if is a thin container # Check if is a thin container
if [ ! -d "${bastille_jailsdir}/${TARGET}/root/.bastille" ]; then if [ ! -d "${bastille_jailsdir}/${TARGET}/root/.bastille" ]; then
error_exit "${TARGET} is not a thin container." error_exit "${TARGET} is not a thin container."
+3 -11
View File
@@ -42,20 +42,12 @@ help|-h|--help)
;; ;;
esac esac
if [ $# -gt 3 ] || [ $# -lt 3 ]; then if [ $# -ne 2 ]; then
usage usage
fi fi
TARGET="${1}" CPSOURCE="${1}"
CPSOURCE="${2}" CPDEST="${2}"
CPDEST="${3}"
if [ "${TARGET}" = 'ALL' ]; then
JAILS=$(jls name)
fi
if [ "${TARGET}" != 'ALL' ]; then
JAILS=$(jls name | awk "/^${TARGET}$/")
fi
for _jail in ${JAILS}; do for _jail in ${JAILS}; do
bastille_jail_path="$(jls -j "${_jail}" path)" bastille_jail_path="$(jls -j "${_jail}" path)"
+3 -13
View File
@@ -42,26 +42,16 @@ help|-h|--help)
;; ;;
esac esac
if [ $# -gt 2 ] || [ $# -lt 1 ]; then if [ $# -gt 1 ]; then
usage usage
fi elif [ $# -eq 1 ]; then
TARGET_FILENAME="${1}"
TARGET="${1}"
if [ $# == 2 ]; then
TARGET_FILENAME="${2}"
fi fi
if [ -z "${EDITOR}" ]; then if [ -z "${EDITOR}" ]; then
EDITOR=vi EDITOR=vi
fi fi
if [ "${TARGET}" = 'ALL' ]; then
JAILS=$(bastille list jails)
fi
if [ "${TARGET}" != 'ALL' ]; then
JAILS=$(bastille list jails | awk "/^${TARGET}$/")
fi
for _jail in ${JAILS}; do for _jail in ${JAILS}; do
if [ -n "${TARGET_FILENAME}" ]; then if [ -n "${TARGET_FILENAME}" ]; then
"${EDITOR}" "${bastille_jailsdir}/${_jail}/${TARGET_FILENAME}" "${EDITOR}" "${bastille_jailsdir}/${_jail}/${TARGET_FILENAME}"
+1 -8
View File
@@ -42,18 +42,14 @@ help|-h|--help)
;; ;;
esac esac
if [ $# -gt 1 ] || [ $# -lt 1 ]; then if [ $# -ne 0 ]; then
usage usage
fi fi
TARGET="${1}"
shift
jail_export() jail_export()
{ {
# Attempt to export the container # Attempt to export the container
DATE=$(date +%F-%H%M%S) DATE=$(date +%F-%H%M%S)
if [ -d "${bastille_jailsdir}/${TARGET}" ]; then
if [ "${bastille_zfs_enable}" = "YES" ]; then if [ "${bastille_zfs_enable}" = "YES" ]; then
if [ -n "${bastille_zfs_zpool}" ]; then if [ -n "${bastille_zfs_zpool}" ]; then
FILE_EXT="xz" FILE_EXT="xz"
@@ -84,9 +80,6 @@ jail_export()
echo -e "${COLOR_GREEN}Exported '${bastille_backupsdir}/${TARGET}_${DATE}.${FILE_EXT}' successfully.${COLOR_RESET}" echo -e "${COLOR_GREEN}Exported '${bastille_backupsdir}/${TARGET}_${DATE}.${FILE_EXT}' successfully.${COLOR_RESET}"
exit 0 exit 0
fi fi
else
error_exit "Container '${TARGET}' does not exist."
fi
} }
# Check for user specified file location # Check for user specified file location
+1 -11
View File
@@ -42,20 +42,10 @@ help|-h|--help)
;; ;;
esac esac
if [ $# -gt 1 ] || [ $# -lt 1 ]; then if [ $# -ne 0 ]; then
usage usage
fi fi
TARGET="${1}"
shift
if [ "${TARGET}" = 'ALL' ]; then
JAILS=$(jls name)
fi
if [ "${TARGET}" != 'ALL' ]; then
JAILS=$(jls name | awk "/^${TARGET}$/")
fi
for _jail in ${JAILS}; do for _jail in ${JAILS}; do
bastille_jail_path=$(jls -j "${_jail}" path) bastille_jail_path=$(jls -j "${_jail}" path)
if [ ! -x "${bastille_jail_path}/usr/local/bin/htop" ]; then if [ ! -x "${bastille_jail_path}/usr/local/bin/htop" ]; then
+3 -13
View File
@@ -51,22 +51,12 @@ help|-h|--help)
;; ;;
esac esac
if [ $# -lt 3 ]; then if [ $# -ne 2 ]; then
usage usage
fi fi
TARGET="${1}" OPTION="${1}"
OPTION="${2}" VALUE="${2}"
VALUE="${3}"
shift
if [ "${TARGET}" = 'ALL' ]; then
JAILS=$(jls name)
fi
if [ "${TARGET}" != 'ALL' ]; then
JAILS=$(jls name | awk "/^${TARGET}$/")
fi
for _jail in ${JAILS}; do for _jail in ${JAILS}; do
echo -e "${COLOR_GREEN}[${_jail}]:${COLOR_RESET}" echo -e "${COLOR_GREEN}[${_jail}]:${COLOR_RESET}"
+1 -12
View File
@@ -44,18 +44,7 @@ esac
if [ $# -lt 2 ]; then if [ $# -lt 2 ]; then
usage usage
fi elif [ $# -eq 2 ]; then
TARGET=$1
shift
if [ "${TARGET}" = 'ALL' ]; then
JAILS=$(jls name)
else
JAILS=$(jls name | awk "/^${TARGET}$/")
fi
if [ $# -eq 2 ]; then
_fstab="$@ nullfs ro 0 0" _fstab="$@ nullfs ro 0 0"
else else
_fstab="$@" _fstab="$@"
+1 -11
View File
@@ -41,20 +41,10 @@ help|-h|--help)
;; ;;
esac esac
if [ $# -lt 2 ]; then if [ $# -lt 1 ]; then
usage usage
fi fi
TARGET="${1}"
shift
if [ "${TARGET}" = 'ALL' ]; then
JAILS=$(jls name)
fi
if [ "${TARGET}" != 'ALL' ]; then
JAILS=$(jls name | awk "/^${TARGET}$/")
fi
for _jail in ${JAILS}; do for _jail in ${JAILS}; do
echo -e "${COLOR_GREEN}[${_jail}]:${COLOR_RESET}" echo -e "${COLOR_GREEN}[${_jail}]:${COLOR_RESET}"
jexec -l "${_jail}" /usr/sbin/pkg "$@" jexec -l "${_jail}" /usr/sbin/pkg "$@"
+8 -16
View File
@@ -32,7 +32,7 @@
. /usr/local/etc/bastille/bastille.conf . /usr/local/etc/bastille/bastille.conf
usage() { usage() {
error_exit "Usage: bastille rename [TARGET] [NEW_NAME]" error_exit "Usage: bastille rename TARGET NEW_NAME"
} }
validate_name() { validate_name() {
@@ -50,13 +50,11 @@ help|-h|--help)
;; ;;
esac esac
if [ $# -gt 2 ] || [ $# -lt 2 ]; then if [ $# -ne 1 ]; then
usage usage
fi fi
TARGET="${1}" NEWNAME="${1}"
NEWNAME="${2}"
shift
update_jailconf() { update_jailconf() {
# Update jail.conf # Update jail.conf
@@ -90,7 +88,6 @@ update_fstab() {
change_name() { change_name() {
# Attempt container name change # Attempt container name change
if [ -d "${bastille_jailsdir}/${TARGET}" ]; then
echo -e "${COLOR_GREEN}Attempting to rename '${TARGET}' to ${NEWNAME}...${COLOR_RESET}" echo -e "${COLOR_GREEN}Attempting to rename '${TARGET}' to ${NEWNAME}...${COLOR_RESET}"
if [ "${bastille_zfs_enable}" = "YES" ]; then if [ "${bastille_zfs_enable}" = "YES" ]; then
if [ -n "${bastille_zfs_zpool}" ] && [ -n "${bastille_zfs_prefix}" ]; then if [ -n "${bastille_zfs_zpool}" ] && [ -n "${bastille_zfs_prefix}" ]; then
@@ -125,9 +122,6 @@ change_name() {
mv "${bastille_jailsdir}/${TARGET}" "${bastille_jailsdir}/${NEWNAME}" mv "${bastille_jailsdir}/${TARGET}" "${bastille_jailsdir}/${NEWNAME}"
fi fi
fi fi
else
error_exit "${TARGET} not found. See 'bastille bootstrap'."
fi
# Update jail configuration files accordingly # Update jail configuration files accordingly
update_jailconf update_jailconf
@@ -141,16 +135,14 @@ change_name() {
fi fi
} }
## check if a running jail matches name or already exist
if [ "$(jls name | awk "/^${TARGET}$/")" ]; then
error_exit "Warning: ${TARGET} is running."
elif [ -d "${bastille_jailsdir}/${NEWNAME}" ]; then
error_exit "Jail: ${NEWNAME} already exists."
fi
## validate jail name ## validate jail name
if [ -n "${NEWNAME}" ]; then if [ -n "${NEWNAME}" ]; then
validate_name validate_name
fi fi
## check if a jail already exists with the new name
if [ -d "${bastille_jailsdir}/${NEWNAME}" ]; then
error_exit "Jail: ${NEWNAME} already exists."
fi
change_name change_name
+1 -12
View File
@@ -41,21 +41,10 @@ help|-h|--help)
;; ;;
esac esac
if [ $# -lt 2 ]; then if [ $# -ne 2 ]; then
usage usage
fi fi
TARGET=$1
shift
if [ "${TARGET}" = 'ALL' ]; then
JAILS=$(jls name)
fi
if [ "${TARGET}" != 'ALL' ]; then
JAILS=$(jls name | awk "/^${TARGET}$/")
fi
for _jail in ${JAILS}; do for _jail in ${JAILS}; do
echo -e "${COLOR_GREEN}[${_jail}]:${COLOR_RESET}" echo -e "${COLOR_GREEN}[${_jail}]:${COLOR_RESET}"
jexec -l "${_jail}" /usr/sbin/service "$@" jexec -l "${_jail}" /usr/sbin/service "$@"
+1 -17
View File
@@ -42,26 +42,10 @@ help|-h|--help)
;; ;;
esac esac
if [ $# -gt 1 ] || [ $# -lt 1 ]; then if [ $# -ne 0 ]; then
usage usage
fi fi
TARGET="${1}"
shift
if [ "${TARGET}" = 'ALL' ]; then
JAILS=$(jls name)
fi
if [ "${TARGET}" != 'ALL' ]; then
JAILS=$(jls name | awk "/^${TARGET}$/")
## check if exist or not running
if [ ! -d "${bastille_jailsdir}/${TARGET}" ]; then
error_exit "[${TARGET}]: Not found."
elif [ ! "$(jls name | awk "/^${TARGET}$/")" ]; then
error_exit "[${TARGET}]: Not started."
fi
fi
for _jail in ${JAILS}; do for _jail in ${JAILS}; do
## test if running ## test if running
if [ "$(jls name | awk "/^${_jail}$/")" ]; then if [ "$(jls name | awk "/^${_jail}$/")" ]; then
+1 -12
View File
@@ -41,21 +41,10 @@ help|-h|--help)
;; ;;
esac esac
if [ $# -lt 2 ]; then if [ $# -lt 1 ]; then
usage usage
fi fi
TARGET="${1}"
shift
if [ "${TARGET}" = 'ALL' ]; then
JAILS=$(jls name)
fi
if [ "${TARGET}" != 'ALL' ]; then
JAILS=$(jls name | awk "/^${TARGET}$/")
fi
for _jail in ${JAILS}; do for _jail in ${JAILS}; do
echo -e "${COLOR_GREEN}[${_jail}]:${COLOR_RESET}" echo -e "${COLOR_GREEN}[${_jail}]:${COLOR_RESET}"
jexec -l "${_jail}" /usr/sbin/sysrc "$@" jexec -l "${_jail}" /usr/sbin/sysrc "$@"
+1 -12
View File
@@ -42,22 +42,11 @@ help|-h|--help)
;; ;;
esac esac
if [ $# -gt 2 ] || [ $# -lt 2 ]; then if [ $# -ne 1 ]; then
bastille_usage bastille_usage
fi fi
TARGET="${1}"
shift
if [ "${TARGET}" = 'ALL' ]; then
JAILS=$(jls name)
fi
if [ "${TARGET}" != 'ALL' ]; then
JAILS=$(jls name | awk "/^${TARGET}$/")
fi
TEMPLATE="${1}" TEMPLATE="${1}"
shift
case ${TEMPLATE} in case ${TEMPLATE} in
http?://github.com/*/*|http?://gitlab.com/*/*) http?://github.com/*/*|http?://gitlab.com/*/*)
+1 -12
View File
@@ -41,21 +41,10 @@ help|-h|--help)
;; ;;
esac esac
if [ $# -gt 1 ] || [ $# -lt 1 ]; then if [ $# -ne 0 ]; then
usage usage
fi fi
TARGET="${1}"
shift
if [ "${TARGET}" = 'ALL' ]; then
JAILS=$(jls name)
fi
if [ "${TARGET}" != 'ALL' ]; then
JAILS=$(jls name | awk "/^${TARGET}$/")
fi
for _jail in ${JAILS}; do for _jail in ${JAILS}; do
echo -e "${COLOR_GREEN}[${_jail}]:${COLOR_RESET}" echo -e "${COLOR_GREEN}[${_jail}]:${COLOR_RESET}"
jexec -l "${_jail}" /usr/bin/top jexec -l "${_jail}" /usr/bin/top
+1 -11
View File
@@ -42,21 +42,11 @@ help|-h|--help)
;; ;;
esac esac
if [ $# -ne 2 ]; then if [ $# -ne 1 ]; then
usage usage
fi fi
TARGET=$1
shift
MOUNT_PATH=$1 MOUNT_PATH=$1
shift
if [ "${TARGET}" = 'ALL' ]; then
JAILS=$(jls name)
else
JAILS=$(jls name | awk "/^${TARGET}$/")
fi
for _jail in ${JAILS}; do for _jail in ${JAILS}; do
echo -e "${COLOR_GREEN}[${_jail}]:${COLOR_RESET}" echo -e "${COLOR_GREEN}[${_jail}]:${COLOR_RESET}"
+5 -18
View File
@@ -84,34 +84,21 @@ if [ -z "${bastille_zfs_zpool}" ]; then
error_exit "ZFS zpool not defined." error_exit "ZFS zpool not defined."
fi fi
if [ $# -lt 2 ]; then if [ $# -lt 1 ]; then
usage usage
fi fi
TARGET="${1}" case "$1" in
if [ "${TARGET}" = 'ALL' ]; then
JAILS=$(jls name)
fi
if [ "${TARGET}" != 'ALL' ]; then
JAILS=$(jls name | awk "/^${TARGET}$/")
fi
case "$2" in
set) set)
ATTRIBUTE=$3 ATTRIBUTE=$2
JAILS=${JAILS}
zfs_set_value zfs_set_value
;; ;;
get) get)
ATTRIBUTE=$3 ATTRIBUTE=$2
JAILS=${JAILS}
zfs_get_value zfs_get_value
;; ;;
snap|snapshot) snap|snapshot)
TAG=$3 TAG=$2
JAILS=${JAILS}
zfs_snapshot zfs_snapshot
;; ;;
df|usage) df|usage)