diff options
Diffstat (limited to 'lib/30-files')
-rw-r--r-- | lib/30-files | 175 |
1 files changed, 175 insertions, 0 deletions
diff --git a/lib/30-files b/lib/30-files new file mode 100644 index 0000000..c7b2000 --- /dev/null +++ b/lib/30-files @@ -0,0 +1,175 @@ +#!/bin/sh + +_boxconf_try_files(){ + # Get the highest precedence file for a given path. + # $1 = target file path + for _bcsf_file in \ + "${BOXCONF_SITE_FILE_DIR}${1}.${BOXCONF_HOSTNAME}" \ + "${BOXCONF_FILE_DIR}${1}.${BOXCONF_HOSTNAME}" \ + "${BOXCONF_SITE_FILE_DIR}${1}.${BOXCONF_HOSTCLASS}.${BOXCONF_OS_DISTRIBUTION}" \ + "${BOXCONF_FILE_DIR}${1}.${BOXCONF_HOSTCLASS}.${BOXCONF_OS_DISTRIBUTION}" \ + "${BOXCONF_SITE_FILE_DIR}${1}.${BOXCONF_OS_DISTRIBUTION}.${BOXCONF_HOSTCLASS}" \ + "${BOXCONF_FILE_DIR}${1}.${BOXCONF_OS_DISTRIBUTION}.${BOXCONF_HOSTCLASS}" \ + "${BOXCONF_SITE_FILE_DIR}${1}.${BOXCONF_HOSTCLASS}.${BOXCONF_OS_FAMILY}" \ + "${BOXCONF_FILE_DIR}${1}.${BOXCONF_HOSTCLASS}.${BOXCONF_OS_FAMILY}" \ + "${BOXCONF_SITE_FILE_DIR}${1}.${BOXCONF_OS_FAMILY}.${BOXCONF_HOSTCLASS}" \ + "${BOXCONF_FILE_DIR}${1}.${BOXCONF_OS_FAMILY}.${BOXCONF_HOSTCLASS}" \ + "${BOXCONF_SITE_FILE_DIR}${1}.${BOXCONF_HOSTCLASS}" \ + "${BOXCONF_FILE_DIR}${1}.${BOXCONF_HOSTCLASS}" \ + "${BOXCONF_SITE_FILE_DIR}${1}.${BOXCONF_OS_DISTRIBUTION}" \ + "${BOXCONF_FILE_DIR}${1}.${BOXCONF_OS_DISTRIBUTION}" \ + "${BOXCONF_SITE_FILE_DIR}${1}.${BOXCONF_OS_FAMILY}" \ + "${BOXCONF_FILE_DIR}${1}.${BOXCONF_OS_FAMILY}" \ + "${BOXCONF_SITE_FILE_DIR}${1}.common" \ + "${BOXCONF_FILE_DIR}${1}.common" + do + if [ -f "$_bcsf_file" ]; then + echo "$_bcsf_file" + return + fi + done + + bug "no source file found for ${1}" +} + +install_file(){ + # Install the files at the given paths into the target system. + # The source file is chosen from the matching file in the boxconf directory with + # the highest-precedence suffix. + # Takes options similar to the `install` command. + _bcif_install_args='-Cv' + _bcif_mode=0644 + + while getopts m:o:g: _bcif_opt; do + case $_bcif_opt in + m) _bcif_mode=$OPTARG ;; + o) _bcif_install_args="${_bcif_install_args} -o ${OPTARG}" ;; + g) _bcif_install_args="${_bcif_install_args} -g ${OPTARG}" ;; + esac + done + shift $((OPTIND - 1)) + + while [ $# -gt 0 ]; do + _bcif_src=$(_boxconf_try_files "$1") + install -m "$_bcif_mode" $_bcif_install_args "$_bcif_src" "$1" + shift + done +} + +install_directory(){ + # Create the specified directories in the target system. + # Takes options similar to the `install` command. + _bcid_install_args='-Cdv' + _bcid_mode=0755 + + while getopts m:o:g: _bcid_opt; do + case $_bcid_opt in + m) _bcid_mode=$OPTARG ;; + o) _bcid_install_args="${_bcid_install_args} -o ${OPTARG}" ;; + g) _bcid_install_args="${_bcid_install_args} -g ${OPTARG}" ;; + esac + done + shift $((OPTIND - 1)) + + while [ $# -gt 0 ]; do + install -m "$_bcid_mode" $_bcid_install_args "$1" + shift + done +} + +install_template(){ + # Install the templatess at the given paths into the target system. + # The source template is chosen from the matching file in the boxconf directory + # with the highest-precedence suffix. Template is rendered as a shell heredoc. + # Takes options similar to the `install` command. + _bcit_install_args='-Cv' + _bcit_mode=0644 + + while getopts m:o:g: _bcit_opt; do + case $_bcit_opt in + m) _bcit_mode=$OPTARG ;; + o) _bcit_install_args="${_bcit_install_args} -o ${OPTARG}" ;; + g) _bcit_install_args="${_bcit_install_args} -g ${OPTARG}" ;; + esac + done + shift $((OPTIND - 1 )) + + while [ $# -gt 0 ]; do + _bcit_src=$(_boxconf_try_files "$1") + + eval "cat <<__BOXCONF_EOF__ >${_bcit_src}.render +$(cat "$_bcit_src") +__BOXCONF_EOF__ +" + [ -s "${_bcit_src}.render" ] || bug "failed to render template: ${_bcit_src}" + install -m "$_bcit_mode" $_bcit_install_args "${_bcit_src}.render" "$1" + shift + done +} + +install_certificate(){ + # Install a certificate from the CA dir into the target system. + # Takes options similar to the `install` command. + # $1 = certificate name + # $2 = target path + _bcic_install_args='-Cv' + _bcic_mode=0644 + + while getopts m:o:g: _bcic_opt; do + case $_bcic_opt in + m) _bcic_mode=$OPTARG ;; + o) _bcic_install_args="${_bcic_install_args} -o ${OPTARG}" ;; + g) _bcic_install_args="${_bcic_install_args} -g ${OPTARG}" ;; + esac + done + shift $((OPTIND - 1)) + + [ -f "${BOXCONF_CA_DIR}/${BOXCONF_HOSTNAME}/${1}.fullchain.crt" ] \ + || bug "no certificate exists for ${BOXCONF_HOSTNAME}/${1}" + + install -m "$_bcic_mode" $_bcic_install_args "${BOXCONF_CA_DIR}/${BOXCONF_HOSTNAME}/${1}.fullchain.crt" "$2" +} + +install_certificate_key(){ + # Install a certificate's private key from the CA dir into the target system. + # Takes options similar to the `install` command. + # $1 = certificate name + # $2 = target path + _bcick_install_args='-Cv' + _bcick_mode=0600 + + while getopts m:o:g: _bcick_opt; do + case $_bcick_opt in + m) _bcick_mode=$OPTARG ;; + o) _bcick_install_args="${_bcick_install_args} -o ${OPTARG}" ;; + g) _bcick_install_args="${_bcick_install_args} -g ${OPTARG}" ;; + esac + done + shift $((OPTIND - 1)) + + [ -f "${BOXCONF_CA_DIR}/${BOXCONF_HOSTNAME}/${1}.key" ] \ + || bug "no key exists for ${BOXCONF_HOSTNAME}/${1}" + + install -m "$_bcick_mode" $_bcick_install_args "${BOXCONF_CA_DIR}/${BOXCONF_HOSTNAME}/${1}.key" "$2" +} + +install_ca_certificate(){ + # Install a the root CA from the CA dir into the target system. + # Takes options similar to the `install` command. + # $1 = target path + _bcicc_install_args='-Cv' + _bcicc_mode=0644 + + while getopts m:o:g: _bcicc_opt; do + case $_bcicc_opt in + m) _bcicc_mode=$OPTARG ;; + o) _bcicc_install_args="${_bcicc_install_args} -o ${OPTARG}" ;; + g) _bcicc_install_args="${_bcicc_install_args} -g ${OPTARG}" ;; + esac + done + shift $((OPTIND - 1)) + + [ -f "${BOXCONF_CA_DIR}/ca.crt" ] || bug 'CA certificate not found' + + install -m "$_bcicc_mode" $_bcicc_install_args "${BOXCONF_CA_DIR}/ca.crt" "$1" +} |