Skip to content

Commit 326b473

Browse files
committed
Updated install script
1 parent 2645236 commit 326b473

File tree

1 file changed

+46
-24
lines changed

1 file changed

+46
-24
lines changed

install.sh

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -88,26 +88,36 @@ if ! [ -x $(command -v curl) ]; then
8888
fi
8989

9090
if [[ -z $DOMAIN_NAME ]]; then
91-
92-
echo "Enter the domain name for your new EmailEngine installation."
93-
echo "(ex. example.com or test.example.com)"
94-
echo "Leave empty to autogenerate a domain name."
95-
96-
while [ -z "$DOMAIN_NAME" ]
97-
do
98-
#echo -en "\n"
99-
read -p "Domain/Subdomain name: " DOMAIN_NAME
100-
101-
if [ -z "$DOMAIN_NAME" ]
102-
then
103-
DOMAIN_NAME=$(curl --silent --fail -XPOST "https://api.nodemailer.com/autoassign" -H "Content-Type: application/json" -d '{
104-
"prefix": "engine",
105-
"version": "1",
106-
"requestor": "install"
107-
}')
91+
# For upgrades, try to extract existing domain from Caddyfile
92+
if [ "$IS_UPGRADE" = true ] && [ -f /etc/caddy/Caddyfile ]; then
93+
EXISTING_DOMAIN=$(grep -E '^[a-zA-Z0-9][a-zA-Z0-9.-]+[a-zA-Z0-9] \{' /etc/caddy/Caddyfile 2>/dev/null | head -1 | cut -d' ' -f1 || echo "")
94+
if [ -n "$EXISTING_DOMAIN" ]; then
95+
DOMAIN_NAME="$EXISTING_DOMAIN"
96+
echo "Using existing domain: $DOMAIN_NAME"
10897
fi
109-
done
110-
98+
fi
99+
100+
# Only prompt for domain if not found (new installation or upgrade without existing domain)
101+
if [ -z "$DOMAIN_NAME" ]; then
102+
echo "Enter the domain name for your new EmailEngine installation."
103+
echo "(ex. example.com or test.example.com)"
104+
echo "Leave empty to autogenerate a domain name."
105+
106+
while [ -z "$DOMAIN_NAME" ]
107+
do
108+
#echo -en "\n"
109+
read -p "Domain/Subdomain name: " DOMAIN_NAME
110+
111+
if [ -z "$DOMAIN_NAME" ]
112+
then
113+
DOMAIN_NAME=$(curl --silent --fail -XPOST "https://api.nodemailer.com/autoassign" -H "Content-Type: application/json" -d '{
114+
"prefix": "engine",
115+
"version": "1",
116+
"requestor": "install"
117+
}')
118+
fi
119+
done
120+
fi
111121
fi
112122

113123
echo "Using the domain name \"${DOMAIN_NAME}\" for this installation."
@@ -243,8 +253,22 @@ systemctl restart emailengine || { echo "Failed to start EmailEngine service"; e
243253
# Log installation details
244254
echo "$(date): EmailEngine ${VERSION:-latest} installed/upgraded for $DOMAIN_NAME" >> /var/log/emailengine-install.log
245255

246-
# Create Caddyfile with security headers (skip if upgrading and file exists)
247-
if [ "$IS_UPGRADE" = false ] || [ ! -f /etc/caddy/Caddyfile ]; then
256+
# Check if domain exists in Caddyfile
257+
DOMAIN_EXISTS_IN_CADDY=false
258+
if [ -f /etc/caddy/Caddyfile ]; then
259+
if grep -q "^${DOMAIN_NAME} {" /etc/caddy/Caddyfile 2>/dev/null; then
260+
DOMAIN_EXISTS_IN_CADDY=true
261+
fi
262+
fi
263+
264+
# Create or update Caddyfile only if needed
265+
if [ "$IS_UPGRADE" = false ] || [ ! -f /etc/caddy/Caddyfile ] || [ "$DOMAIN_EXISTS_IN_CADDY" = false ]; then
266+
if [ "$DOMAIN_EXISTS_IN_CADDY" = false ] && [ -f /etc/caddy/Caddyfile ]; then
267+
echo "Domain ${DOMAIN_NAME} not found in Caddy configuration, adding it..."
268+
# Backup existing Caddyfile
269+
cp /etc/caddy/Caddyfile /etc/caddy/Caddyfile.backup.$(date +%Y%m%d%H%M%S)
270+
fi
271+
248272
echo "
249273
:80 {
250274
redir https://${DOMAIN_NAME}{uri}
@@ -253,8 +277,6 @@ if [ "$IS_UPGRADE" = false ] || [ ! -f /etc/caddy/Caddyfile ]; then
253277
${DOMAIN_NAME} {
254278
reverse_proxy localhost:3000 {
255279
header_up X-Real-IP {remote_host}
256-
header_up X-Forwarded-For {remote_host}
257-
header_up X-Forwarded-Proto {scheme}
258280
}
259281
260282
header {
@@ -275,7 +297,7 @@ ${DOMAIN_NAME} {
275297
caddy validate --config /etc/caddy/Caddyfile || { echo "Invalid Caddy configuration"; exit 1; }
276298
echo "Caddy configuration validated successfully"
277299
else
278-
echo "Using existing Caddy configuration"
300+
echo "Domain ${DOMAIN_NAME} already configured in Caddy, keeping existing configuration"
279301
fi
280302

281303
# Create upgrade script

0 commit comments

Comments
 (0)