Skip to content

Commit

Permalink
Refactored entrypoint for better readability
Browse files Browse the repository at this point in the history
  • Loading branch information
jaydrogers committed Jan 8, 2025
1 parent 58f07aa commit 394a9c9
Showing 1 changed file with 81 additions and 63 deletions.
144 changes: 81 additions & 63 deletions src/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
#!/bin/sh

# Validate required environment variables
for var in CLOUDFLARE_API_TOKEN CERTBOT_DOMAINS CERTBOT_EMAIL CERTBOT_KEY_TYPE; do
if [ -z "$(eval echo \$$var)" ]; then
echo "Error: $var environment variable is not set"
exit 1
fi
done
################################################################################
# Functions
################################################################################

# Permissions must be created after volumes have been mounted; otherwise, windows file system permissions will override
# the permissions set within the container.
mkdir -p /etc/letsencrypt/accounts /var/log/letsencrypt /var/lib/letsencrypt
chmod 755 /etc/letsencrypt /var/lib/letsencrypt
chmod 700 /etc/letsencrypt/accounts /var/log/letsencrypt

cat << "EOF"
____________________
< Certbot, activate! >
--------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
EOF
cleanup() {
echo "Shutdown requested, exiting gracefully..."
exit 0
}

if [ -n "$CERTBOT_DOMAIN" ] && [ -z "$CERTBOT_DOMAINS" ]; then
CERTBOT_DOMAINS=$CERTBOT_DOMAIN
fi
configure_windows_file_permissions() {
# Permissions must be created after volumes have been mounted; otherwise, windows file system permissions will override
# the permissions set within the container.
mkdir -p /etc/letsencrypt/accounts /var/log/letsencrypt /var/lib/letsencrypt
chmod 755 /etc/letsencrypt /var/lib/letsencrypt
chmod 700 /etc/letsencrypt/accounts /var/log/letsencrypt
}

echo "🚀 Let's Get Encrypted! 🚀"
echo "🌐 Domain(s): $CERTBOT_DOMAINS"
echo "📧 Email: $CERTBOT_EMAIL"
echo "🔑 Key Type: $CERTBOT_KEY_TYPE"
echo "⏰ Renewal Interval: $RENEWAL_INTERVAL seconds"
echo "Let's Encrypt, shall we?"
echo "-----------------------------------------------------------"
# Workaround https://github.com/microsoft/wsl/issues/12250 by replacing symlinks with direct copies of the files they
# reference.
replace_symlinks() {
# shellcheck disable=SC3043
local dir="$1"

# Create Cloudflare configuration file
echo "dns_cloudflare_api_token = $CLOUDFLARE_API_TOKEN" > /cloudflare.ini
# Iterate over all items in the directory
for item in "$dir"/*; do
if [ -L "$item" ]; then
# If the item is a symlink
target=$(readlink -f "$item")
if [ -e "$target" ]; then
echo "Replacing symlink $item with a copy of $target"
cp -r "$target" "$item"
else
echo "Warning: target $target of symlink $item does not exist"
fi
elif [ -d "$item" ]; then
# If the item is a directory, process it recursively
replace_symlinks "$item"
fi
done
}

# Function to run certbot with provided arguments
run_certbot() {
certbot certonly \
--dns-cloudflare \
Expand All @@ -62,38 +62,56 @@ run_certbot() {
fi
}

# Workaround https://github.com/microsoft/wsl/issues/12250 by replacing symlinks with direct copies of the files they
# reference.
replace_symlinks() {
# shellcheck disable=SC3043
local dir="$1"

# Iterate over all items in the directory
for item in "$dir"/*; do
if [ -L "$item" ]; then
# If the item is a symlink
target=$(readlink -f "$item")
if [ -e "$target" ]; then
echo "Replacing symlink $item with a copy of $target"
cp -r "$target" "$item"
else
echo "Warning: target $target of symlink $item does not exist"
fi
elif [ -d "$item" ]; then
# If the item is a directory, process it recursively
replace_symlinks "$item"
validate_environment_variables() {
# Validate required environment variables
for var in CLOUDFLARE_API_TOKEN CERTBOT_DOMAINS CERTBOT_EMAIL CERTBOT_KEY_TYPE; do
if [ -z "$(eval echo \$$var)" ]; then
echo "Error: $var environment variable is not set"
exit 1
fi
done
}

cleanup() {
echo "Shutdown requested, exiting gracefully..."
exit 0
}
################################################################################
# Main
################################################################################

trap cleanup TERM INT

validate_environment_variables

if [ "$REPLACE_SYMLINKS" = "true" ]; then
configure_windows_file_permissions
fi

# Ensure backwards compatibility with the old CERTBOT_DOMAIN environment variable
if [ -n "$CERTBOT_DOMAIN" ] && [ -z "$CERTBOT_DOMAINS" ]; then
CERTBOT_DOMAINS=$CERTBOT_DOMAIN
fi

cat << "EOF"
____________________
< Certbot, activate! >
--------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
EOF

trap cleanup SIGTERM SIGINT
echo "🚀 Let's Get Encrypted! 🚀"
echo "🌐 Domain(s): $CERTBOT_DOMAINS"
echo "📧 Email: $CERTBOT_EMAIL"
echo "🔑 Key Type: $CERTBOT_KEY_TYPE"
echo "⏰ Renewal Interval: $RENEWAL_INTERVAL seconds"
echo "Let's Encrypt, shall we?"
echo "-----------------------------------------------------------"

# Create Cloudflare configuration file
echo "dns_cloudflare_api_token = $CLOUDFLARE_API_TOKEN" > /cloudflare.ini

# Run certbot initially
# Run certbot initially to get the certificates
run_certbot

# Infinite loop to keep the container running and periodically check for renewals
Expand All @@ -102,7 +120,7 @@ while true; do
echo "Next certificate renewal check will be at ${next_run}"

# Use wait with timeout to allow for signal interruption
sleep $RENEWAL_INTERVAL &
sleep "$RENEWAL_INTERVAL" &
wait $!

# Check if we received a signal
Expand Down

0 comments on commit 394a9c9

Please sign in to comment.