Skip to content

Commit 394a9c9

Browse files
committed
Refactored entrypoint for better readability
1 parent 58f07aa commit 394a9c9

File tree

1 file changed

+81
-63
lines changed

1 file changed

+81
-63
lines changed

src/entrypoint.sh

+81-63
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,46 @@
11
#!/bin/sh
22

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

11-
# Permissions must be created after volumes have been mounted; otherwise, windows file system permissions will override
12-
# the permissions set within the container.
13-
mkdir -p /etc/letsencrypt/accounts /var/log/letsencrypt /var/lib/letsencrypt
14-
chmod 755 /etc/letsencrypt /var/lib/letsencrypt
15-
chmod 700 /etc/letsencrypt/accounts /var/log/letsencrypt
16-
17-
cat << "EOF"
18-
____________________
19-
< Certbot, activate! >
20-
--------------------
21-
\ ^__^
22-
\ (oo)\_______
23-
(__)\ )\/\
24-
||----w |
25-
|| ||
26-
EOF
7+
cleanup() {
8+
echo "Shutdown requested, exiting gracefully..."
9+
exit 0
10+
}
2711

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

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

40-
# Create Cloudflare configuration file
41-
echo "dns_cloudflare_api_token = $CLOUDFLARE_API_TOKEN" > /cloudflare.ini
26+
# Iterate over all items in the directory
27+
for item in "$dir"/*; do
28+
if [ -L "$item" ]; then
29+
# If the item is a symlink
30+
target=$(readlink -f "$item")
31+
if [ -e "$target" ]; then
32+
echo "Replacing symlink $item with a copy of $target"
33+
cp -r "$target" "$item"
34+
else
35+
echo "Warning: target $target of symlink $item does not exist"
36+
fi
37+
elif [ -d "$item" ]; then
38+
# If the item is a directory, process it recursively
39+
replace_symlinks "$item"
40+
fi
41+
done
42+
}
4243

43-
# Function to run certbot with provided arguments
4444
run_certbot() {
4545
certbot certonly \
4646
--dns-cloudflare \
@@ -62,38 +62,56 @@ run_certbot() {
6262
fi
6363
}
6464

65-
# Workaround https://github.com/microsoft/wsl/issues/12250 by replacing symlinks with direct copies of the files they
66-
# reference.
67-
replace_symlinks() {
68-
# shellcheck disable=SC3043
69-
local dir="$1"
70-
71-
# Iterate over all items in the directory
72-
for item in "$dir"/*; do
73-
if [ -L "$item" ]; then
74-
# If the item is a symlink
75-
target=$(readlink -f "$item")
76-
if [ -e "$target" ]; then
77-
echo "Replacing symlink $item with a copy of $target"
78-
cp -r "$target" "$item"
79-
else
80-
echo "Warning: target $target of symlink $item does not exist"
81-
fi
82-
elif [ -d "$item" ]; then
83-
# If the item is a directory, process it recursively
84-
replace_symlinks "$item"
65+
validate_environment_variables() {
66+
# Validate required environment variables
67+
for var in CLOUDFLARE_API_TOKEN CERTBOT_DOMAINS CERTBOT_EMAIL CERTBOT_KEY_TYPE; do
68+
if [ -z "$(eval echo \$$var)" ]; then
69+
echo "Error: $var environment variable is not set"
70+
exit 1
8571
fi
8672
done
8773
}
8874

89-
cleanup() {
90-
echo "Shutdown requested, exiting gracefully..."
91-
exit 0
92-
}
75+
################################################################################
76+
# Main
77+
################################################################################
78+
79+
trap cleanup TERM INT
80+
81+
validate_environment_variables
82+
83+
if [ "$REPLACE_SYMLINKS" = "true" ]; then
84+
configure_windows_file_permissions
85+
fi
86+
87+
# Ensure backwards compatibility with the old CERTBOT_DOMAIN environment variable
88+
if [ -n "$CERTBOT_DOMAIN" ] && [ -z "$CERTBOT_DOMAINS" ]; then
89+
CERTBOT_DOMAINS=$CERTBOT_DOMAIN
90+
fi
91+
92+
cat << "EOF"
93+
____________________
94+
< Certbot, activate! >
95+
--------------------
96+
\ ^__^
97+
\ (oo)\_______
98+
(__)\ )\/\
99+
||----w |
100+
|| ||
101+
EOF
93102

94-
trap cleanup SIGTERM SIGINT
103+
echo "🚀 Let's Get Encrypted! 🚀"
104+
echo "🌐 Domain(s): $CERTBOT_DOMAINS"
105+
echo "📧 Email: $CERTBOT_EMAIL"
106+
echo "🔑 Key Type: $CERTBOT_KEY_TYPE"
107+
echo "⏰ Renewal Interval: $RENEWAL_INTERVAL seconds"
108+
echo "Let's Encrypt, shall we?"
109+
echo "-----------------------------------------------------------"
110+
111+
# Create Cloudflare configuration file
112+
echo "dns_cloudflare_api_token = $CLOUDFLARE_API_TOKEN" > /cloudflare.ini
95113

96-
# Run certbot initially
114+
# Run certbot initially to get the certificates
97115
run_certbot
98116

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

104122
# Use wait with timeout to allow for signal interruption
105-
sleep $RENEWAL_INTERVAL &
123+
sleep "$RENEWAL_INTERVAL" &
106124
wait $!
107125

108126
# Check if we received a signal

0 commit comments

Comments
 (0)