Skip to content

Commit

Permalink
Ensure target is specified and exists in 1 place instead of many.
Browse files Browse the repository at this point in the history
Require target to be running for commands that need it.
Closes #239.
  • Loading branch information
chriswells0 committed Aug 30, 2020
1 parent aaffc88 commit 62b7e3f
Show file tree
Hide file tree
Showing 18 changed files with 159 additions and 293 deletions.
58 changes: 45 additions & 13 deletions usr/local/bin/bastille
Original file line number Diff line number Diff line change
Expand Up @@ -133,20 +133,48 @@ version|-v|--version)
help|-h|--help)
usage
;;
esac

# Filter out all non-commands
case "${CMD}" in
bootstrap|clone|cmd|console|convert|cp|create)
;;
destroy|edit|export|htop|import|limits|list|mount)
;;
pkg|rdr|rename|restart|service|start|stop|sysrc|umount)
bootstrap|convert|create|destroy|import|list|rdr|restart|start|stop|update|upgrade|verify)
# Nothing "extra" to do for these commands. -- cwells
;;
template|top|update|upgrade|verify|zfs)
clone|cmd|console|cp|edit|export|htop|limits|mount|pkg|rename|service|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

case "${CMD}" in
cmd|console|htop|pkg|service|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
;;
rename)
# Require the target to be stopped. -- cwells
if [ "$(jls name | awk "/^${TARGET}$/")" ]; then
error_exit "${TARGET} is running. See 'bastille stop ${TARGET}'."
fi
;;
esac
fi
export TARGET
export JAILS
fi
;;
*)
usage
*) # Filter out all non-commands
usage
;;
esac

Expand All @@ -157,7 +185,11 @@ if [ -f "${SCRIPTPATH}" ]; then

: "${SH:=sh}"

exec "${SH}" "${SCRIPTPATH}" "$@"
if [ -n "${PARAMS}" ]; then
exec "${SH}" "${SCRIPTPATH}" "${PARAMS}"
else
exec "${SH}" "${SCRIPTPATH}" "$@"
fi
else
error_exit "${SCRIPTPATH} not found."
fi
62 changes: 28 additions & 34 deletions usr/local/share/bastille/clone.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,12 @@ help|-h|--help)
;;
esac

if [ $# -ne 3 ]; then
if [ $# -ne 2 ]; then
usage
fi

TARGET="${1}"
NEWNAME="${2}"
IP="${3}"
shift
NEWNAME="${1}"
IP="${2}"

validate_ip() {
IPX_ADDR="ip4.addr"
Expand Down Expand Up @@ -146,39 +144,35 @@ update_fstab() {

clone_jail() {
# Attempt container clone
if [ -d "${bastille_jailsdir}/${TARGET}" ]; then
echo -e "${COLOR_GREEN}Attempting to clone '${TARGET}' to ${NEWNAME}...${COLOR_RESET}"
if ! [ -d "${bastille_jailsdir}/${NEWNAME}" ]; then
if [ "${bastille_zfs_enable}" = "YES" ]; then
if [ -n "${bastille_zfs_zpool}" ]; then
# Replicate the existing container
DATE=$(date +%F-%H%M%S)
zfs snapshot -r "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}@bastille_clone_${DATE}"
zfs send -R "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}@bastille_clone_${DATE}" | zfs recv "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${NEWNAME}"

# Cleanup source temporary snapshots
zfs destroy "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}/root@bastille_clone_${DATE}"
zfs destroy "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}@bastille_clone_${DATE}"

# Cleanup target temporary snapshots
zfs destroy "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${NEWNAME}/root@bastille_clone_${DATE}"
zfs destroy "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${NEWNAME}@bastille_clone_${DATE}"
fi
else
# Just clone the jail directory
# Check if container is running
if [ -n "$(jls name | awk "/^${TARGET}$/")" ]; then
error_exit "${TARGET} is running, See 'bastille stop ${TARGET}'."
fi

# Perform container file copy(archive mode)
cp -a "${bastille_jailsdir}/${TARGET}" "${bastille_jailsdir}/${NEWNAME}"
echo -e "${COLOR_GREEN}Attempting to clone '${TARGET}' to ${NEWNAME}...${COLOR_RESET}"
if ! [ -d "${bastille_jailsdir}/${NEWNAME}" ]; then
if [ "${bastille_zfs_enable}" = "YES" ]; then
if [ -n "${bastille_zfs_zpool}" ]; then
# Replicate the existing container
DATE=$(date +%F-%H%M%S)
zfs snapshot -r "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}@bastille_clone_${DATE}"
zfs send -R "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}@bastille_clone_${DATE}" | zfs recv "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${NEWNAME}"

# Cleanup source temporary snapshots
zfs destroy "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}/root@bastille_clone_${DATE}"
zfs destroy "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}@bastille_clone_${DATE}"

# Cleanup target temporary snapshots
zfs destroy "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${NEWNAME}/root@bastille_clone_${DATE}"
zfs destroy "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${NEWNAME}@bastille_clone_${DATE}"
fi
else
error_exit "${NEWNAME} already exists."
# Just clone the jail directory
# Check if container is running
if [ -n "$(jls name | awk "/^${TARGET}$/")" ]; then
error_exit "${TARGET} is running. See 'bastille stop ${TARGET}'."
fi

# Perform container file copy(archive mode)
cp -a "${bastille_jailsdir}/${TARGET}" "${bastille_jailsdir}/${NEWNAME}"
fi
else
error_exit "${TARGET} not found. See bootstrap."
error_exit "${NEWNAME} already exists."
fi

# Generate jail configuration files
Expand Down
12 changes: 1 addition & 11 deletions usr/local/share/bastille/cmd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,10 @@ help|-h|--help)
;;
esac

if [ $# -lt 2 ]; then
if [ $# -eq 0 ]; then
usage
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
echo -e "${COLOR_GREEN}[${_jail}]:${COLOR_RESET}"
jexec -l "${_jail}" "$@"
Expand Down
13 changes: 2 additions & 11 deletions usr/local/share/bastille/console.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
. /usr/local/share/bastille/common.sh

usage() {
error_exit "Usage: bastille console TARGET [user]'."
error_exit "Usage: bastille console TARGET [user]'"
}

# Handle special-case commands first.
Expand All @@ -41,21 +41,12 @@ help|-h|--help)
;;
esac

if [ $# -gt 2 ] || [ $# -lt 1 ]; then
if [ $# -gt 1 ]; then
usage
fi

TARGET="${1}"
shift
USER="${1}"

if [ "${TARGET}" = 'ALL' ]; then
JAILS=$(jls name)
fi
if [ "${TARGET}" != 'ALL' ]; then
JAILS=$(jls name | awk "/^${TARGET}$/")
fi

validate_user() {
if jexec -l "${_jail}" id "${USER}" >/dev/null 2>&1; then
USER_SHELL="$(jexec -l "${_jail}" getent passwd "${USER}" | cut -d: -f7)"
Expand Down
14 changes: 3 additions & 11 deletions usr/local/share/bastille/cp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,12 @@ help|-h|--help)
;;
esac

if [ $# -gt 3 ] || [ $# -lt 3 ]; then
if [ $# -ne 2 ]; then
usage
fi

TARGET="${1}"
CPSOURCE="${2}"
CPDEST="${3}"

if [ "${TARGET}" = 'ALL' ]; then
JAILS=$(jls name)
fi
if [ "${TARGET}" != 'ALL' ]; then
JAILS=$(jls name | awk "/^${TARGET}$/")
fi
CPSOURCE="${1}"
CPDEST="${2}"

for _jail in ${JAILS}; do
bastille_jail_path="$(jls -j "${_jail}" path)"
Expand Down
16 changes: 3 additions & 13 deletions usr/local/share/bastille/edit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,16 @@ help|-h|--help)
;;
esac

if [ $# -gt 2 ] || [ $# -lt 1 ]; then
if [ $# -gt 1 ]; then
usage
fi

TARGET="${1}"
if [ $# == 2 ]; then
TARGET_FILENAME="${2}"
elif [ $# -eq 1 ]; then
TARGET_FILENAME="${1}"
fi

if [ -z "${EDITOR}" ]; then
EDITOR=vi
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
if [ -n "${TARGET_FILENAME}" ]; then
"${EDITOR}" "${bastille_jailsdir}/${_jail}/${TARGET_FILENAME}"
Expand Down
59 changes: 26 additions & 33 deletions usr/local/share/bastille/export.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,50 +42,43 @@ help|-h|--help)
;;
esac

if [ $# -gt 1 ] || [ $# -lt 1 ]; then
if [ $# -ne 0 ]; then
usage
fi

TARGET="${1}"
shift

jail_export()
{
# Attempt to export the container
DATE=$(date +%F-%H%M%S)
if [ -d "${bastille_jailsdir}/${TARGET}" ]; then
if [ "${bastille_zfs_enable}" = "YES" ]; then
if [ -n "${bastille_zfs_zpool}" ]; then
FILE_EXT="xz"
echo -e "${COLOR_GREEN}Exporting '${TARGET}' to a compressed .${FILE_EXT} archive.${COLOR_RESET}"
echo -e "${COLOR_GREEN}Sending zfs data stream...${COLOR_RESET}"
# Take a recursive temporary snapshot
zfs snapshot -r "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}@bastille_export_${DATE}"
if [ "${bastille_zfs_enable}" = "YES" ]; then
if [ -n "${bastille_zfs_zpool}" ]; then
FILE_EXT="xz"
echo -e "${COLOR_GREEN}Exporting '${TARGET}' to a compressed .${FILE_EXT} archive.${COLOR_RESET}"
echo -e "${COLOR_GREEN}Sending zfs data stream...${COLOR_RESET}"
# Take a recursive temporary snapshot
zfs snapshot -r "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}@bastille_export_${DATE}"

# Export the container recursively and cleanup temporary snapshots
zfs send -R "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}@bastille_export_${DATE}" | \
xz ${bastille_compress_xz_options} > "${bastille_backupsdir}/${TARGET}_${DATE}.${FILE_EXT}"
zfs destroy "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}/root@bastille_export_${DATE}"
zfs destroy "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}@bastille_export_${DATE}"
fi
else
# Create standard backup archive
FILE_EXT="txz"
echo -e "${COLOR_GREEN}Exporting '${TARGET}' to a compressed .${FILE_EXT} archive...${COLOR_RESET}"
cd "${bastille_jailsdir}" && tar -cf - "${TARGET}" | xz ${bastille_compress_xz_options} > "${bastille_backupsdir}/${TARGET}_${DATE}.${FILE_EXT}"
# Export the container recursively and cleanup temporary snapshots
zfs send -R "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}@bastille_export_${DATE}" | \
xz ${bastille_compress_xz_options} > "${bastille_backupsdir}/${TARGET}_${DATE}.${FILE_EXT}"
zfs destroy "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}/root@bastille_export_${DATE}"
zfs destroy "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}@bastille_export_${DATE}"
fi
else
# Create standard backup archive
FILE_EXT="txz"
echo -e "${COLOR_GREEN}Exporting '${TARGET}' to a compressed .${FILE_EXT} archive...${COLOR_RESET}"
cd "${bastille_jailsdir}" && tar -cf - "${TARGET}" | xz ${bastille_compress_xz_options} > "${bastille_backupsdir}/${TARGET}_${DATE}.${FILE_EXT}"
fi

if [ "$?" -ne 0 ]; then
error_exit "Failed to export '${TARGET}' container."
else
# Generate container checksum file
cd "${bastille_backupsdir}"
sha256 -q "${TARGET}_${DATE}.${FILE_EXT}" > "${TARGET}_${DATE}.sha256"
echo -e "${COLOR_GREEN}Exported '${bastille_backupsdir}/${TARGET}_${DATE}.${FILE_EXT}' successfully.${COLOR_RESET}"
exit 0
fi
if [ "$?" -ne 0 ]; then
error_exit "Failed to export '${TARGET}' container."
else
error_exit "Container '${TARGET}' does not exist."
# Generate container checksum file
cd "${bastille_backupsdir}"
sha256 -q "${TARGET}_${DATE}.${FILE_EXT}" > "${TARGET}_${DATE}.sha256"
echo -e "${COLOR_GREEN}Exported '${bastille_backupsdir}/${TARGET}_${DATE}.${FILE_EXT}' successfully.${COLOR_RESET}"
exit 0
fi
}

Expand Down
12 changes: 1 addition & 11 deletions usr/local/share/bastille/htop.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,10 @@ help|-h|--help)
;;
esac

if [ $# -gt 1 ] || [ $# -lt 1 ]; then
if [ $# -ne 0 ]; then
usage
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
bastille_jail_path=$(jls -j "${_jail}" path)
if [ ! -x "${bastille_jail_path}/usr/local/bin/htop" ]; then
Expand Down
16 changes: 3 additions & 13 deletions usr/local/share/bastille/limits.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,12 @@ help|-h|--help)
;;
esac

if [ $# -lt 3 ]; then
if [ $# -ne 2 ]; then
usage
fi

TARGET="${1}"
OPTION="${2}"
VALUE="${3}"
shift

if [ "${TARGET}" = 'ALL' ]; then
JAILS=$(jls name)
fi

if [ "${TARGET}" != 'ALL' ]; then
JAILS=$(jls name | awk "/^${TARGET}$/")
fi
OPTION="${1}"
VALUE="${2}"

for _jail in ${JAILS}; do
echo -e "${COLOR_GREEN}[${_jail}]:${COLOR_RESET}"
Expand Down
Loading

0 comments on commit 62b7e3f

Please sign in to comment.