Skip to content

Commit

Permalink
add init from s3 and bump versions
Browse files Browse the repository at this point in the history
  • Loading branch information
eltorio committed Mar 1, 2024
1 parent 33377e7 commit f383b9c
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 12 deletions.
5 changes: 4 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ RUN set -ex; \

# Final Image
FROM node:20-alpine
LABEL maintainer="Ronan Le Meillat <ronan@parapente.cf>"
LABEL maintainer="Ronan Le Meillat <ronan@parapente.eu.org>"
WORKDIR /app/

# Install system dependencies
Expand Down Expand Up @@ -88,5 +88,8 @@ RUN set -ex; \
apk add --update --no-cache \
mysql-client mariadb-connector-c-dev

RUN curl -L https://dl.min.io/client/mc/release/linux-$(dpkg --print-architecture)/mc > /usr/local/bin/mc && chmod +x /usr/local/bin/mc
COPY --chmod=755 scripts/init-from-s3.sh /app/init-from-s3.sh

EXPOSE 3000 3003 3004
ENTRYPOINT ["bash", "/app/docker-entrypoint.sh"]
2 changes: 2 additions & 0 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ if [ "$WITH_LDAP" = "true" ]; then
fi
fi

/app/init-from-s3.sh

NODE_ENV=production node setup/docker-entrypoint-db-setup.js "$ADMIN_PASSWORD" "$ADMIN_ACCESS_TOKEN"

NODE_ENV=production node index.js
6 changes: 6 additions & 0 deletions scripts/autobackup
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
#!/bin/bash
# Script to backup the database and files
# Run only if BACKUP_TO and BACKUP_FROM are set
if [ -z "$BACKUP_TO" ] || [ -z "$BACKUP_FROM" ]; then
echo "BACKUP_TO and BACKUP_FROM must be set"
exit 1
fi
MAILUSER=`cat /app/zone-mta/config/builtin-zonemta.json |sed -n 's/^.*\"user\":.*\"\(.*\)\",$/\1/p'| head -n1`
MAILPASSWD=`cat /app/zone-mta/config/builtin-zonemta.json |sed -n 's/^.*\"pass\":.*\"\(.*\)\"$/\1/p'| head -n1`
NOW=`date -I`
Expand Down
41 changes: 41 additions & 0 deletions scripts/init-from-s3.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash
MAILTRAIN_DATABASE=${MAILTRIAN_DATABASE:-'mailtrain'}
# test if variables S3_BUCKET, S3_ACCESS_KEY, S3_SECRET_KEY, S3_ENDPOINT, S3_REGION, S3_PATH are set
if [ -z "${S3_BUCKET}" ] || [ -z "${S3_ACCESS_KEY}" ] || [ -z "${S3_SECRET_KEY}" ] || [ -z "${S3_ENDPOINT}" ] || [ -z "${S3_PATH}" ]; then
echo "S3_BUCKET, S3_ACCESS_KEY, S3_SECRET_KEY, S3_ENDPOINT, S3_REGION, S3_PATH are not set"
exit 1
fi

# test is mysql variables are set
if [ -z "${MYSQL_HOST}" ] || [ -z "${MYSQL_ROOT_PASSWORD}" ]; then
echo "MYSQL_HOST and MYSQL_ROOT_PASSWORD must be set"
exit 1
fi

# Check if database need to be restored
if [ -z "$(mysql -h $MYSQL_HOST --password="$MYSQL_ROOT_PASSWORD" -e "SHOW DATABASES LIKE '${MAILTRAIN_DATABASE}'")" -a "$INIT_FROM_S3" = "1" ]; then
echo "Database ${MAILTRAIN_DATABASE} does not exist restoring it from s3"
# test if mc is installed
if [ -z "$(which mc)" ]; then
echo "mc is not installed"
exit 1
fi

# Test if mc alias s3backup exists
if [ -z "$(mc alias list | grep s3backup)" ]; then
echo "s3backup alias not found"
echo "create s3backup alias"
mc alias set s3backup ${S3_ENDPOINT} ${S3_ACCESS_KEY} ${S3_SECRET_KEY}
fi

# Find latest backup file in s3
mc ls s3backup/${S3_BUCKET}/${S3_PATH} | sort -r | head -n 1 | awk '{print $6}' | xargs -I {} mc cp s3backup/${S3_BUCKET}/${S3_PATH}/{} /tmp/backup.tar.xz

# Restore backup
tar -xvf /tmp/backup.tar.xz -C /
rm /tmp/backup.tar.xz

# Restore the mailtrain database
sed -n -e "/^-- Current Database: \`${MAILTRAIN_DATABASE}\`/,/^-- Current Database: \`/p" /app/server/files/backup.sql >/app/server/files/${MAILTRAIN_DATABASE}.sql
mysql -h $MYSQL_HOST --password="$MYSQL_ROOT_PASSWORD" </app/server/files/mailtrain.sql
fi
20 changes: 10 additions & 10 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@
"posix": "^4.2.0"
},
"dependencies": {
"@aws-sdk/client-ses": "^3.496.0",
"@aws-sdk/client-ses": "^3.525.0",
"bcrypt-nodejs": "0.0.3",
"bluebird": "^3.7.2",
"body-parser": "^1.20.2",
"bounce-handler": "7.3.2-fork.3",
"capitalize": "^2.0.4",
"compression": "^1.7.4",
"config": "^3.3.10",
"config": "^3.3.11",
"connect-flash": "^0.1.1",
"connect-redis": "^7.1.0",
"connect-redis": "^7.1.1",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"crypto": "^1.0.1",
Expand All @@ -62,9 +62,9 @@
"device": "^0.3.12",
"escape-html": "^1.0.3",
"escape-string-regexp": "^1.0.5",
"express": "^4.18.2",
"express": "^4.18.3",
"express-locale": "^2.0.2",
"express-session": "^1.17.3",
"express-session": "^1.18.0",
"faker": "^4.1.0",
"feedparser-promised": "^2.0.1",
"fs-extra": "^11.2.0",
Expand All @@ -88,7 +88,7 @@
"memory-cache": "^0.2.0",
"mjml": "^4.14.1",
"moment": "^2.30.1",
"moment-timezone": "^0.5.41",
"moment-timezone": "^0.5.45",
"morgan": "^1.10.0",
"multer": "^1.4.5-lts.1",
"mysql2": "^3.2.0",
Expand All @@ -97,15 +97,15 @@
"node-mocks-http": "^1.8.1",
"node-object-hash": "^1.4.1",
"nodeify": "^1.0.1",
"nodemailer": "^6.9.8",
"nodemailer": "^6.9.11",
"nodemailer-openpgp": "^2.1.0",
"npmlog": "^7.0.1",
"openpgp": "^4.7.2",
"passport": "^0.4.1",
"passport-local": "^1.0.0",
"redis": "^4.6.12",
"request": "^2.88.0",
"request-promise": "^4.2.5",
"redis": "^4.6.13",
"request": "^2.88.2",
"request-promise": "^4.2.6",
"serve-favicon": "^2.5.0",
"slugify": "^1.3.6",
"smtp-server": "^3.4.7",
Expand Down
2 changes: 1 addition & 1 deletion zone-mta/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"node": ">=16.0.0"
},
"dependencies": {
"zone-mta": "^3.9.0",
"zone-mta": "^3.9.4",
"zonemta-delivery-counters": "^1.0.1",
"zonemta-limiter": "^1.0.0",
"zonemta-loop-breaker": "^1.0.2"
Expand Down

0 comments on commit f383b9c

Please sign in to comment.