Skip to content

Commit

Permalink
refactor(BASH): prefix all imported functions
Browse files Browse the repository at this point in the history
  • Loading branch information
niall-byrne committed Nov 19, 2023
1 parent 40eb25d commit 356de21
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 33 deletions.
23 changes: 11 additions & 12 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

set -eo pipefail

trap terminate SIGINT SIGTERM ERR EXIT
trap _fn_terminate SIGINT SIGTERM ERR EXIT

# shellcheck disable=SC1091
source /usr/local/share/certs/scripts/import.bash

env_file() {
_fn_env_file() {
if [[ -n "${ENV_FILE}" ]]; then
set -a
# shellcheck disable=SC1090
Expand All @@ -16,18 +16,17 @@ env_file() {
fi
}

terminate() {
_fn_terminate() {
ERROR_CODE="$?"
echo "CONTAINER > ERROR CODE: ${ERROR_CODE}"
exit "${ERROR_CODE}"
}

main() {
_fn_env_file

env_file

import /usr/local/share/certs/providers "DNS Provider"
import /usr/local/share/certs/scripts "Script Library"
_fn_import /usr/local/share/certs/providers "DNS Provider"
_fn_import /usr/local/share/certs/scripts "Script Library"

# shellcheck disable=SC2034
if [[ "${TEST_MODE}" == "1" ]]; then
Expand All @@ -36,11 +35,11 @@ main() {
TEST_MODE="-q"
fi

create # Create initial certificates
users # Configure users and passwords
renew & # Start certificate renewal process
relay & # Start deferred relay server configuration
dkim & # Start deferred dkim update process
_fn_create # Create initial certificates
_fn_users # Configure users and passwords
_fn_renew & # Start certificate renewal process
_fn_relay & # Start deferred relay server configuration
_fn_dkim & # Start deferred dkim update process

echo "CONTAINER > Starting postfix ..."
./docker-entrypoint.sh "$@"
Expand Down
4 changes: 2 additions & 2 deletions hooks/deploy.bash
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ source /usr/local/share/certs/scripts/import.bash

main() {
echo "CONTAINER > 'deploy' hook has been called."
import /usr/local/share/certs/scripts "Script Library"
install_certificates
_fn_import /usr/local/share/certs/scripts "Script Library"
_fn_install_certificates
echo "CONTAINER > Reloading dovecot and postfix ..."
dovecot reload
postfix reload
Expand Down
22 changes: 11 additions & 11 deletions providers/cloudflare.bash
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

provider_create() {
write_credential_file
_fn_write_credential_file
certbot certonly "${TEST_MODE}" --dns-cloudflare --dns-cloudflare-credentials /tmp/cloudflare --dns-cloudflare-propagation-seconds "${DNS_PROPAGATION_DELAY}" -d "*.${PRIMARY_DOMAIN}" -m "${CONTACT_EMAIL}" --agree-tos --no-eff-email
}

Expand All @@ -21,7 +21,7 @@ provider_dkim() {
local PAYLOAD
local RESPONSE

dkim_create() {
_fn_dkim_create() {
local CURL_RESPONSE

CURL_RESPONSE="$(
Expand All @@ -36,7 +36,7 @@ provider_dkim() {
echo "${CURL_RESPONSE}"
}

dkim_get() {
_fn_dkim_get() {
local CURL_RESPONSE

CURL_RESPONSE="$(
Expand All @@ -51,7 +51,7 @@ provider_dkim() {
echo "${CURL_RESPONSE}"
}

dkim_update() {
_fn_dkim_update() {
# $1: Record ID

local CURL_RESPONSE
Expand All @@ -68,24 +68,24 @@ provider_dkim() {
echo "${CURL_RESPONSE}"
}

dkim_select_method() {
METHOD="dkim_create"
_fn_dkim_select_method() {
METHOD="_fn_dkim_create"
while read -r LINE; do
# shellcheck disable=SC2001
PARSED_NAME=$(sed "s/^\([^\t]*\)\t\(.*\)$/\1/" <<< "${LINE}")
# shellcheck disable=SC2001
PARSED_ID=$(sed "s/^\([^\t]*\)\t\(.*\)$/\2/" <<< "${LINE}")
if [[ "${PARSED_NAME}" == "mail._domainkey.${PRIMARY_DOMAIN}" ]]; then
METHOD="dkim_update"
METHOD="_fn_dkim_update"
break
fi
done < <(jq -r '.result[] | .name + "\t" + .id' <<< "$(dkim_get)")
done < <(jq -r '.result[] | .name + "\t" + .id' <<< "$(_fn_dkim_get)")
}

DKIM_CONTENT="$(cut -d"(" -f2 "/etc/opendkim/keys/${PRIMARY_DOMAIN}/mail.txt" | cut -d")" -f1 | tr -d ' "\n\t')"
PAYLOAD=$(jq -r ".name = \"mail._domainkey.${PRIMARY_DOMAIN}\" | .content = \"${DKIM_CONTENT}\" | .type = \"TXT\"" <<< '{}')

dkim_select_method
_fn_dkim_select_method

RESPONSE="$(eval "${METHOD}" "${PARSED_ID}")"

Expand All @@ -97,11 +97,11 @@ provider_dkim() {
}

provider_renew() {
write_credential_file
_fn_write_credential_file
certbot renew "${TEST_MODE}" --dns_cloudflare --dns-cloudflare-credentials /tmp/cloudflare --dns-cloudflare-propagation-seconds "${DNS_PROPAGATION_DELAY}" --deploy-hook=/usr/local/share/certs/hooks/deploy.bash
}

write_credential_file() {
_fn_write_credential_file() {
echo "dns_cloudflare_api_token = ${CLOUDFLARE_API_TOKEN}" >> /tmp/cloudflare
chmod 600 /tmp/cloudflare
}
4 changes: 2 additions & 2 deletions scripts/create.bash
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/bin/bash

function create() {
function _fn_create() {
echo "CONTAINER > 'create' function has been called."
if [[ ! -e "/etc/letsencrypt/live/${PRIMARY_DOMAIN}" ]]; then
pushd "certbot" > /dev/null || exit 127
echo "CONTAINER > Attempting to create certificates ..."
provider_create
popd > /dev/null || exit 127
fi
install_certificates
_fn_install_certificates
}
2 changes: 1 addition & 1 deletion scripts/dkim.bash
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

function dkim() {
function _fn_dkim() {
echo "CONTAINER > 'dkim' function has been called."
echo "CONTAINER > 'dkim' is waiting ${DKIM_DELAY} seconds before attempting to update the dkim TXT record ..."
sleep "${DKIM_DELAY}"
Expand Down
2 changes: 1 addition & 1 deletion scripts/import.bash
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

import() {
_fn_import() {
# $1 - path to scripts
# $2 - description of import
for SCRIPT in "${1}"/*.bash; do
Expand Down
2 changes: 1 addition & 1 deletion scripts/install.bash
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

function install_certificates() {
function _fn_install_certificates() {
echo "CONTAINER > 'install_certificates' function has been called."
echo "CONTAINER > Attempting to install certificates ..."
cp -v /etc/letsencrypt/live/"${PRIMARY_DOMAIN}"/fullchain.pem /run/secrets/fullchain.pem
Expand Down
2 changes: 1 addition & 1 deletion scripts/relay.bash
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

SASL_PASSWORD_FILE="/etc/postfix/sasl/sasl_passwd"

function relay() {
function _fn_relay() {
echo "CONTAINER > 'relay' function has been called."
if [[ ! -e "${SASL_PASSWORD_FILE}" ]] && [[ -n "${RELAY_SERVER}" ]]; then
echo "CONTAINER > 'relay' is waiting ${CONFIG_DELAY} seconds to update the postfix configuration ..."
Expand Down
2 changes: 1 addition & 1 deletion scripts/renew.bash
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

function renew() {
function _fn_renew() {
echo "CONTAINER > 'renew' function has been called."
while true; do
echo "CONTAINER > 'renew' is waiting ${RENEW_INTERVAL_IN_DAYS} days before attempting the next certificate renewal ..."
Expand Down
2 changes: 1 addition & 1 deletion scripts/users.bash
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

function users() {
function _fn_users() {
echo "CONTAINER > 'users' function has been called."
echo -e "${USER_LIST}" > /run/secrets/users.txt
}

0 comments on commit 356de21

Please sign in to comment.