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

Add ability to convert "hook" files to Bastillefile format. #285

Merged
merged 1 commit into from
Dec 12, 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
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ Note: SYSRC requires NO quotes or that quotes (`"`) be escaped. ie; `\"`)
Any name provided in the ARG file can be used as a variable in the other hooks.
For example, `name=value` in the ARG file will cause instances of `${name}`
to be replaced with `value`. The `RENDER` hook can be used to specify existing files or
directories inside the jail whose contents should have the variables replaced. Values can be
directories inside the jail whose contents should have the variables replaced. Values can be
specified either through the command line when applying the template or as a default in the ARG
file.

Expand Down Expand Up @@ -748,6 +748,11 @@ CMD hostname > /usr/local/www/nginx-dist/hostname.txt
RDR tcp 80 80
```

Use the following command to convert a hook-based template into the Bastillefile format:
```shell
bastille template --convert my-template
```

Applying Templates
------------------

Expand Down
3 changes: 3 additions & 0 deletions usr/local/bin/bastille
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ clone|cmd|console|convert|cp|edit|export|htop|limits|mount|pkg|rename|service|st
JAILS="${JAILS} ${_jail}"
fi
done
elif [ "${CMD}" = 'template' ] && [ "${TARGET}" = '--convert' ]; then
# This command does not act on a jail, so we are temporarily bypassing the presence/started
# checks. The command will simply convert a template from hooks to a Bastillefile. -- cwells
else
JAILS=$(jls name | awk "/^${TARGET}$/")

Expand Down
65 changes: 57 additions & 8 deletions usr/local/share/bastille/template.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
. /usr/local/etc/bastille/bastille.conf

bastille_usage() {
error_exit "Usage: bastille template TARGET project/template"
error_exit "Usage: bastille template TARGET|--convert project/template"
}

post_command_hook() {
Expand Down Expand Up @@ -116,7 +116,61 @@ if [ $# -lt 1 ]; then
bastille_usage
fi

## global variables
TEMPLATE="${1}"
bastille_template=${bastille_templatesdir}/${TEMPLATE}
if [ -z "${HOOKS}" ]; then
HOOKS='LIMITS INCLUDE PRE FSTAB PF PKG OVERLAY CONFIG SYSRC SERVICE CMD RENDER'
fi

# Special case conversion of hook-style template files into a Bastillefile. -- cwells
if [ "${TARGET}" = '--convert' ]; then
if [ -d "${TEMPLATE}" ]; then # A relative path was provided. -- cwells
cd "${TEMPLATE}"
elif [ -d "${bastille_template}" ]; then
cd "${bastille_template}"
else
error_exit "Template not found: ${TEMPLATE}"
fi

echo "Converting template: ${TEMPLATE}"

HOOKS="ARG ${HOOKS}"
for _hook in ${HOOKS}; do
if [ -s "${_hook}" ]; then
# Default command is the hook name and default args are the line from the file. -- cwells
_cmd="${_hook}"
_args_template='${_line}'

# Replace old hook names with Bastille command names. -- cwells
case ${_hook} in
CONFIG|OVERLAY)
_cmd='CP'
_args_template='${_line} /'
;;
FSTAB)
_cmd='MOUNT' ;;
PF)
_cmd='RDR' ;;
PRE)
_cmd='CMD' ;;
esac

while read _line; do
if [ -z "${_line}" ]; then
continue
fi
eval "_args=\"${_args_template}\""
echo "${_cmd} ${_args}" >> Bastillefile
done < "${_hook}"
echo '' >> Bastillefile
rm "${_hook}"
fi
done

info "Template converted: ${TEMPLATE}"
exit 0
fi

case ${TEMPLATE} in
http?://github.com/*/*|http?://gitlab.com/*/*)
Expand All @@ -128,6 +182,7 @@ case ${TEMPLATE} in
fi
fi
TEMPLATE="${TEMPLATE_DIR}"
bastille_template=${bastille_templatesdir}/${TEMPLATE}
;;
*/*)
if [ ! -d "${bastille_templatesdir}/${TEMPLATE}" ]; then
Expand All @@ -142,10 +197,6 @@ if [ -z "${JAILS}" ]; then
error_exit "Container ${TARGET} is not running."
fi

if [ -z "${HOOKS}" ]; then
HOOKS='LIMITS INCLUDE PRE FSTAB PF PKG OVERLAY CONFIG SYSRC SERVICE CMD RENDER'
fi

# Check for an --arg-file parameter. -- cwells
for _script_arg in "$@"; do
case ${_script_arg} in
Expand All @@ -166,8 +217,6 @@ if [ -n "${ARG_FILE}" ] && [ ! -f "${ARG_FILE}" ]; then
error_exit "File not found: ${ARG_FILE}"
fi

## global variables
bastille_template=${bastille_templatesdir}/${TEMPLATE}
for _jail in ${JAILS}; do
## jail-specific variables.
bastille_jail_path=$(jls -j "${_jail}" path)
Expand Down Expand Up @@ -322,6 +371,6 @@ for _jail in ${JAILS}; do
fi
done

info "Template complete."
info "Template applied: ${TEMPLATE}"
echo
done