mirror of
https://github.com/hackacad/bastille.git
synced 2025-12-22 02:00:08 +01:00
Add ability to convert "hook" files to Bastillefile format.
This commit is contained in:
@@ -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.
|
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}`
|
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
|
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
|
specified either through the command line when applying the template or as a default in the ARG
|
||||||
file.
|
file.
|
||||||
|
|
||||||
@@ -748,6 +748,11 @@ CMD hostname > /usr/local/www/nginx-dist/hostname.txt
|
|||||||
RDR tcp 80 80
|
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
|
Applying Templates
|
||||||
------------------
|
------------------
|
||||||
|
|
||||||
|
|||||||
@@ -153,6 +153,9 @@ clone|cmd|console|convert|cp|edit|export|htop|limits|mount|pkg|rename|service|st
|
|||||||
JAILS="${JAILS} ${_jail}"
|
JAILS="${JAILS} ${_jail}"
|
||||||
fi
|
fi
|
||||||
done
|
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
|
else
|
||||||
JAILS=$(jls name | awk "/^${TARGET}$/")
|
JAILS=$(jls name | awk "/^${TARGET}$/")
|
||||||
|
|
||||||
|
|||||||
@@ -32,7 +32,7 @@
|
|||||||
. /usr/local/etc/bastille/bastille.conf
|
. /usr/local/etc/bastille/bastille.conf
|
||||||
|
|
||||||
bastille_usage() {
|
bastille_usage() {
|
||||||
error_exit "Usage: bastille template TARGET project/template"
|
error_exit "Usage: bastille template TARGET|--convert project/template"
|
||||||
}
|
}
|
||||||
|
|
||||||
post_command_hook() {
|
post_command_hook() {
|
||||||
@@ -116,7 +116,61 @@ if [ $# -lt 1 ]; then
|
|||||||
bastille_usage
|
bastille_usage
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
## global variables
|
||||||
TEMPLATE="${1}"
|
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
|
case ${TEMPLATE} in
|
||||||
http?://github.com/*/*|http?://gitlab.com/*/*)
|
http?://github.com/*/*|http?://gitlab.com/*/*)
|
||||||
@@ -128,6 +182,7 @@ case ${TEMPLATE} in
|
|||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
TEMPLATE="${TEMPLATE_DIR}"
|
TEMPLATE="${TEMPLATE_DIR}"
|
||||||
|
bastille_template=${bastille_templatesdir}/${TEMPLATE}
|
||||||
;;
|
;;
|
||||||
*/*)
|
*/*)
|
||||||
if [ ! -d "${bastille_templatesdir}/${TEMPLATE}" ]; then
|
if [ ! -d "${bastille_templatesdir}/${TEMPLATE}" ]; then
|
||||||
@@ -142,10 +197,6 @@ if [ -z "${JAILS}" ]; then
|
|||||||
error_exit "Container ${TARGET} is not running."
|
error_exit "Container ${TARGET} is not running."
|
||||||
fi
|
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
|
# Check for an --arg-file parameter. -- cwells
|
||||||
for _script_arg in "$@"; do
|
for _script_arg in "$@"; do
|
||||||
case ${_script_arg} in
|
case ${_script_arg} in
|
||||||
@@ -166,8 +217,6 @@ if [ -n "${ARG_FILE}" ] && [ ! -f "${ARG_FILE}" ]; then
|
|||||||
error_exit "File not found: ${ARG_FILE}"
|
error_exit "File not found: ${ARG_FILE}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
## global variables
|
|
||||||
bastille_template=${bastille_templatesdir}/${TEMPLATE}
|
|
||||||
for _jail in ${JAILS}; do
|
for _jail in ${JAILS}; do
|
||||||
## jail-specific variables.
|
## jail-specific variables.
|
||||||
bastille_jail_path=$(jls -j "${_jail}" path)
|
bastille_jail_path=$(jls -j "${_jail}" path)
|
||||||
@@ -322,6 +371,6 @@ for _jail in ${JAILS}; do
|
|||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
info "Template complete."
|
info "Template applied: ${TEMPLATE}"
|
||||||
echo
|
echo
|
||||||
done
|
done
|
||||||
|
|||||||
Reference in New Issue
Block a user