Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require target to be running for specific commands #251

Merged
merged 2 commits into from
Sep 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 52 additions & 33 deletions usr/local/bin/bastille
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,14 @@

PATH=${PATH}:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin

bastille_colors_pre() {
## so we can make it colorful
. /usr/local/share/bastille/colors.pre.sh
}
. /usr/local/share/bastille/common.sh

## root check first.
bastille_root_check() {
if [ "$(id -u)" -ne 0 ]; then
bastille_colors_pre
## permission denied
echo -e "${COLOR_RED}Bastille: Permission Denied${COLOR_RESET}" 1>&2
echo -e "${COLOR_RED}root / sudo / doas required${COLOR_RESET}" 1>&2
exit 1
error_notify "Bastille: Permission Denied"
error_exit "root / sudo / doas required"
fi
}

Expand All @@ -51,9 +46,7 @@ bastille_root_check
## check for config existance
bastille_conf_check() {
if [ ! -r "/usr/local/etc/bastille/bastille.conf" ]; then
bastille_colors_pre
echo -e "${COLOR_RED}Missing Configuration${COLOR_RESET}" 1>&2
exit 1
error_exit "Missing Configuration"
fi
}

Expand All @@ -68,11 +61,8 @@ bastille_perms_check() {
if [ -d "${bastille_prefix}" ]; then
BASTILLE_PREFIX_PERMS=$(stat -f "%Op" "${bastille_prefix}")
if [ "${BASTILLE_PREFIX_PERMS}" != 40750 ]; then
bastille_colors_pre
echo -e "${COLOR_RED}Insecure permissions on ${bastille_prefix}${COLOR_RESET}" 1>&2
echo -e "${COLOR_RED}Try: chmod 0750 ${bastille_prefix}${COLOR_RESET}" 1>&2
echo
exit 1
error_notify "Insecure permissions on ${bastille_prefix}"
error_exit "Try: chmod 0750 ${bastille_prefix}"
fi
fi
}
Expand Down Expand Up @@ -137,27 +127,54 @@ shift
# Handle special-case commands first.
case "${CMD}" in
version|-v|--version)
bastille_colors_pre
echo -e "${COLOR_GREEN}${BASTILLE_VERSION}${COLOR_RESET}"
exit 0
;;
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|create|destroy|import|list|rdr|restart|start|update|upgrade|verify)
# Nothing "extra" to do for these commands. -- cwells
;;
template|top|update|upgrade|verify|zfs)
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

case "${CMD}" in
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
;;
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
;;
esac
fi
export TARGET
export JAILS
fi
;;
*)
usage
*) # Filter out all non-commands
usage
;;
esac

Expand All @@ -168,9 +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
bastille_colors_pre
echo -e "${COLOR_RED}${SCRIPTPATH} not found.${COLOR_RESET}" 1>&2
exit 1
error_exit "${SCRIPTPATH} not found."
fi
49 changes: 18 additions & 31 deletions usr/local/share/bastille/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,11 @@
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

. /usr/local/share/bastille/colors.pre.sh
. /usr/local/share/bastille/common.sh
. /usr/local/etc/bastille/bastille.conf

usage() {
echo -e "${COLOR_RED}Usage: bastille bootstrap [release|template] [update|arch].${COLOR_RESET}"
exit 1
error_exit "Usage: bastille bootstrap [release|template] [update|arch]"
}

# Handle special-case commands first.
Expand All @@ -47,21 +46,17 @@ esac
if [ "${bastille_zfs_enable}" = "YES" ]; then
## check for the ZFS pool and bastille prefix
if [ -z "${bastille_zfs_zpool}" ]; then
echo -e "${COLOR_RED}ERROR: Missing ZFS parameters, see bastille_zfs_zpool.${COLOR_RESET}"
exit 1
error_exit "ERROR: Missing ZFS parameters. See bastille_zfs_zpool."
elif [ -z "${bastille_zfs_prefix}" ]; then
echo -e "${COLOR_RED}ERROR: Missing ZFS parameters, see bastille_zfs_prefix.${COLOR_RESET}"
exit 1
error_exit "ERROR: Missing ZFS parameters. See bastille_zfs_prefix."
elif ! zfs list "${bastille_zfs_zpool}" > /dev/null 2>&1; then
echo -e "${COLOR_RED}ERROR: ${bastille_zfs_zpool} is not a ZFS pool.${COLOR_RESET}"
exit 1
error_exit "ERROR: ${bastille_zfs_zpool} is not a ZFS pool."
fi

## check for the ZFS dataset prefix if already exist
if [ -d "/${bastille_zfs_zpool}/${bastille_zfs_prefix}" ]; then
if ! zfs list "${bastille_zfs_zpool}/${bastille_zfs_prefix}" > /dev/null 2>&1; then
echo -e "${COLOR_RED}ERROR: ${bastille_zfs_zpool}/${bastille_zfs_prefix} is not a ZFS dataset.${COLOR_RESET}"
exit 1
error_exit "ERROR: ${bastille_zfs_zpool}/${bastille_zfs_prefix} is not a ZFS dataset."
fi
fi
fi
Expand All @@ -71,8 +66,7 @@ validate_release_url() {
if [ -n "${NAME_VERIFY}" ]; then
RELEASE="${NAME_VERIFY}"
if ! fetch -qo /dev/null "${UPSTREAM_URL}/MANIFEST" 2>/dev/null; then
echo -e "${COLOR_RED}Unable to fetch MANIFEST, See 'bootstrap urls'.${COLOR_RESET}"
exit 1
error_exit "Unable to fetch MANIFEST. See 'bootstrap urls'."
fi
echo -e "${COLOR_GREEN}Bootstrapping ${PLATFORM_OS} distfiles...${COLOR_RESET}"

Expand Down Expand Up @@ -207,8 +201,7 @@ bootstrap_release() {

## check if release already bootstrapped, else continue bootstrapping
if [ -z "${bastille_bootstrap_archives}" ]; then
echo -e "${COLOR_RED}Bootstrap appears complete.${COLOR_RESET}"
exit 1
error_exit "Bootstrap appears complete."
else
echo -e "${COLOR_GREEN}Bootstrapping additional distfiles...${COLOR_RESET}"
fi
Expand All @@ -224,8 +217,7 @@ bootstrap_release() {
touch "${bastille_releasesdir}/${RELEASE}/root/.hushlogin"
touch "${bastille_releasesdir}/${RELEASE}/usr/share/skel/dot.hushlogin"
else
echo -e "${COLOR_RED}Failed to extract ${_archive}.txz.${COLOR_RESET}"
exit 1
error_exit "Failed to extract ${_archive}.txz."
fi
else
## get the manifest for dist files checksum validation
Expand Down Expand Up @@ -255,16 +247,15 @@ bootstrap_release() {
rm -rf "${bastille_releasesdir}/${RELEASE}"
fi
fi
echo -e "${COLOR_RED}Bootstrap failed.${COLOR_RESET}"
exit 1
error_exit "Bootstrap failed."
fi

## fetch for missing dist files
if [ ! -f "${bastille_cachedir}/${RELEASE}/${_archive}.txz" ]; then
fetch "${UPSTREAM_URL}/${_archive}.txz" -o "${bastille_cachedir}/${RELEASE}/${_archive}.txz"
if [ "$?" -ne 0 ]; then
## alert only if unable to fetch additional dist files
echo -e "${COLOR_RED}Failed to fetch ${_archive}.txz.${COLOR_RESET}"
error_notify "Failed to fetch ${_archive}.txz."
fi
fi

Expand All @@ -273,9 +264,8 @@ bootstrap_release() {
SHA256_DIST=$(grep -w "${_archive}.txz" "${bastille_cachedir}/${RELEASE}/MANIFEST" | awk '{print $2}')
SHA256_FILE=$(sha256 -q "${bastille_cachedir}/${RELEASE}/${_archive}.txz")
if [ "${SHA256_FILE}" != "${SHA256_DIST}" ]; then
echo -e "${COLOR_RED}Failed validation for ${_archive}.txz, please retry bootstrap!${COLOR_RESET}"
rm "${bastille_cachedir}/${RELEASE}/${_archive}.txz"
exit 1
error_exit "Failed validation for ${_archive}.txz. Please retry bootstrap!"
else
echo -e "${COLOR_GREEN}Validated checksum for ${RELEASE}:${_archive}.txz.${COLOR_RESET}"
echo -e "${COLOR_GREEN}MANIFEST:${SHA256_DIST}${COLOR_RESET}"
Expand All @@ -291,8 +281,7 @@ bootstrap_release() {
touch "${bastille_releasesdir}/${RELEASE}/root/.hushlogin"
touch "${bastille_releasesdir}/${RELEASE}/usr/share/skel/dot.hushlogin"
else
echo -e "${COLOR_RED}Failed to extract ${_archive}.txz.${COLOR_RESET}"
exit 1
error_exit "Failed to extract ${_archive}.txz."
fi
fi
fi
Expand Down Expand Up @@ -325,16 +314,15 @@ bootstrap_template() {

## support for non-git
if [ ! -x "$(which git)" ]; then
echo -e "${COLOR_RED}Git not found.${COLOR_RESET}"
echo -e "${COLOR_RED}Not yet implemented.${COLOR_RESET}"
exit 1
error_notify "Git not found."
error_exit "Not yet implemented."
elif [ -x "$(which git)" ]; then
if [ ! -d "${_template}/.git" ]; then
$(which git) clone "${_url}" "${_template}" ||\
echo -e "${COLOR_RED}Clone unsuccessful.${COLOR_RESET}"
error_notify "Clone unsuccessful."
elif [ -d "${_template}/.git" ]; then
cd "${_template}" && $(which git) pull ||\
echo -e "${COLOR_RED}Template update unsuccessful.${COLOR_RESET}"
error_notify "Template update unsuccessful."
fi
fi

Expand All @@ -353,8 +341,7 @@ if [ -n "${OPTION}" ] && [ "${OPTION}" != "${HW_MACHINE}" ] && [ "${OPTION}" !=
HW_MACHINE="i386"
HW_MACHINE_ARCH="i386"
else
echo -e "${COLOR_RED}Unsupported architecture.${COLOR_RESET}"
exit 1
error_exit "Unsupported architecture."
fi
fi

Expand Down
Loading