Skip to content

Commit

Permalink
Merge pull request #285 from chriswells0/hook-conversion
Browse files Browse the repository at this point in the history
Add ability to convert "hook" files to Bastillefile format.
  • Loading branch information
cedwards authored Dec 12, 2020
2 parents 4e843be + 554f229 commit be6b1ad
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 8 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,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 @@ -155,6 +155,9 @@ clone|config|cmd|console|convert|cp|edit|export|htop|limits|mount|pkg|rename|ser
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="${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 @@ -119,7 +119,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 @@ -131,6 +185,7 @@ case ${TEMPLATE} in
fi
fi
TEMPLATE="${TEMPLATE_DIR}"
bastille_template=${bastille_templatesdir}/${TEMPLATE}
;;
*/*)
if [ ! -d "${bastille_templatesdir}/${TEMPLATE}" ]; then
Expand All @@ -145,10 +200,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 @@ -169,8 +220,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
info "[${_jail}]:"
info "Applying template: ${TEMPLATE}..."
Expand Down Expand Up @@ -331,6 +380,6 @@ for _jail in ${JAILS}; do
fi
done

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

0 comments on commit be6b1ad

Please sign in to comment.