Refactor docker-entrypoint.sh to use ENV Variables

- This provides a setup which is more consistens with other Docker images
 - Provides the possibility to deploy configuration files with configuration management tool
 - Throws an error when the MAILTRAIN_SETTINGS is still used
This commit is contained in:
Markus Opolka 2019-11-21 14:09:34 +01:00
parent 5ffdfa7c96
commit b21005df78

View file

@ -1,186 +1,60 @@
#!/bin/bash #!/bin/bash
# Entrypoint for Docker Container
set -e set -e
function printHelp { URL_BASE_TRUSTED=${URL_BASE_TRUSTED:-'http://localhost:3000'}
cat <<EOF URL_BASE_SANDBOX=${URL_BASE_SANDBOX:-'http://localhost:3003'}
URL_BASE_PUBLIC=${URL_BASE_PUBLIC:-'http://localhost:3004'}
Optional parameters: WWW_PROXY=${WWW_PROXY:-'false'}
--trustedUrlBase XXX - sets the trusted url of the instance (default: http://localhost:3000) WITH_LDAP=${WITH_LDAP:-'false'}
--sandboxUrlBase XXX - sets the sandbox url of the instance (default: http://localhost:3003) LDAP_HOST=${LDAP_HOST:-'ldap'}
--publicUrlBase XXX - sets the public url of the instance (default: http://localhost:3004) LDAP_PORT=${LDAP_PORT:-'389'}
--withProxy - use if Mailtrain is behind an http reverse proxy LDAP_SECURE=${LDAP_SECURE:-'false'}
--mongoHost XXX - sets mongo host (default: mongo) LDAP_BIND_USER=${LDAP_BIND_USER:-}
--redisHost XXX - sets redis host (default: redis) LDAP_BIND_PASS=${LDAP_BIND_PASS:-}
--mySqlHost XXX - sets mysql host (default: mysql) LDAP_FILTER=${LDAP_FILTER:-}
--mySqlDatabase XXX - sets mysql database (default: mailtrain) LDAP_BASEDN=${LDAP_BASEDN:-}
--mySqlUser XXX - sets mysql user (default: mailtrain) LDAP_UIDTAG=${LDAP_UIDTAG:-}
--mySqlPassword XXX - sets mysql password (default: mailtrain) MONGO_HOST=${MONG_HOST:-'mongo'}
--withLdap - use if you want to enable LDAP authentication REDIS_HOST=${REDIS_HOST:-'redis'}
--ldapHost XXX - LDAP Host for authentication (default: ldap) MYSQL_HOST=${MYSQL_HOST:-'mysql'}
--ldapPort XXX - LDAP port (default: 389) MYSQL_DATABASE=${MYSQL_DATABASE:-'mailtrain'}
--ldapSecure - use if you want to use LDAP with ldaps protocol MYSQL_USER=${MYSQL_USER:-'mailtrain'}
--ldapBindUser XXX - User for LDAP connexion MYSQL_PASSWORD=${MYSQL_PASSWORD=:-'mailtrain'}
--ldapBindPass XXX - Password for LDAP connexion
--ldapFilter XXX - LDAP filter
--ldapBaseDN XXX - LDAP base DN
--ldapUidTag XXX - LDAP UID tag (e.g. uid/cn/username)
EOF
# Warning for users that already rely on the MAILTRAIN_SETTING variable
# Can probably be removed in the future.
MAILTRAIN_SETTING=${MAILTRAIN_SETTINGS:-}
if [ ! -z "$MAILTRAIN_SETTING" ]; then
echo 'Error: MAILTRAIN_SETTINGS is no longer supported. See README'
exit 1 exit 1
}
urlBaseTrusted=http://localhost:3000
urlBaseSandbox=http://localhost:3003
urlBasePublic=http://localhost:3004
wwwProxy=false
withLdap=false
ldapHost=ldap
ldapPort=389
ldapSecure=false
ldapBindUser=""
ldapBindPass=""
ldapFilter=""
ldapBaseDN=""
ldapUidTag=""
mongoHost=mongo
redisHost=redis
mySqlHost=mysql
mySqlDatabase=mailtrain
mySqlUser=mailtrain
mySqlPassword=mailtrain
while [ $# -gt 0 ]; do
case "$1" in
--help)
printHelp
;;
--trustedUrlBase)
urlBaseTrusted="$2"
shift 2
;;
--sandboxUrlBase)
urlBaseSandbox="$2"
shift 2
;;
--publicUrlBase)
urlBasePublic="$2"
shift 2
;;
--withProxy)
wwwProxy=true
shift 1
;;
--mongoHost)
mongoHost="$2"
shift 2
;;
--redisHost)
redisHost="$2"
shift 2
;;
--mySqlHost)
mySqlHost="$2"
shift 2
;;
--mySqlDatabase)
mySqlDatabase="$2"
shift 2
;;
--mySqlUser)
mySqlUser="$2"
shift 2
;;
--mySqlPassword)
mySqlPassword="$2"
shift 2
;;
--withLdap)
withLdap=true
shift 1
;;
--ldapHost)
ldapHost="$2"
shift 2
;;
--ldapPort)
ldapPort="$2"
shift 2
;;
--ldapSecure)
ldapSecure=true
shift 1
;;
--ldapBindUser)
ldapBindUser="$2"
shift 2
;;
--ldapBindPass)
ldapBindPass="$2"
shift 2
;;
--ldapFilter)
ldapFilter="$2"
shift 2
;;
--ldapBaseDN)
ldapBaseDN="$2"
shift 2
;;
--ldapUidTag)
ldapUidTag="$2"
shift 2
;;
*)
echo "Error: unrecognized option $1."
printHelp
esac
done
if [ "$ldapBindUser" == "" ]; then
ldapBindUserLine=""
else
ldapBindUserLine="bindUser: $ldapBindUser"
fi
if [ "$ldapBindPass" == "" ]; then
ldapBindPassLine=""
else
ldapBindPassLine="bindPassword: $ldapBindPass"
fi
if [ "$ldapFilter" == "" ]; then
ldapFilterLine=""
else
ldapFilterLine="filter: $ldapFilter"
fi
if [ "$ldapBaseDN" == "" ]; then
ldapBaseDNLine=""
else
ldapBaseDNLine="baseDN: $ldapBaseDN"
fi
if [ "$ldapUidTag" == "" ]; then
ldapUidTagLine=""
else
ldapUidTagLine="uidTag: $ldapUidTag"
fi fi
if [ -f application/config/config.php ]; then
echo 'Info: application/production.yaml already provisioned'
else
echo 'Info: Generating application/production.yaml'
# Basic configuration
cat > server/config/production.yaml <<EOT cat > server/config/production.yaml <<EOT
www: www:
host: 0.0.0.0 host: 0.0.0.0
proxy: $wwwProxy proxy: $WWW_PROXY
secret: "`pwgen -1`" secret: "`pwgen -1`"
trustedUrlBase: $urlBaseTrusted trustedUrlBase: $URL_BASE_TRUSTED
sandboxUrlBase: $urlBaseSandbox sandboxUrlBase: $URL_BASE_SANDBOX
publicUrlBase: $urlBasePublic publicUrlBase: $URL_BASE_PUBLIC
mysql: mysql:
host: $mySqlHost host: $MYSQL_HOST
database: $mySqlDatabase database: $MYSQL_DATABASE
user: $mySqlUser user: $MYSQL_USER
password: $mySqlPassword password: $MYSQL_PASSWORD
redis: redis:
enabled: true enabled: true
host: $redisHost host: $REDIS_HOST
log: log:
level: info level: info
@ -188,36 +62,59 @@ log:
builtinZoneMTA: builtinZoneMTA:
log: log:
level: warn level: warn
mongo: mongodb://${mongoHost}:27017/zone-mta mongo: mongodb://${MONGO_HOST}:27017/zone-mta
redis: redis://${redisHost}:6379/2 redis: redis://${REDIS_HOST}:6379/2
queue: queue:
processes: 5 processes: 5
ldap:
enabled: $withLdap
host: $ldapHost
port: $ldapPort
secure: $ldapSecure
$ldapBindUserLine
$ldapBindPassLine
$ldapFilterLine
$ldapBaseDNLine
$ldapUidTagLine
EOT EOT
# Manage LDAP if enabled
if [ "$WITH_LDAP" = "true" ]; then
echo 'Info: LDAP enabled'
cat >> server/config/production.yaml <<EOT
ldap:
enabled: true
host: $LDAP_HOST
port: $LDAP_PORT
secure: $LDAP_SECURE
bindUser: $LDAP_BIND_USER
bindPasswort: $LDAP_BIND_PASS
filter: $LDAP_FILTER
baseDN: $LDAP_BASEDN
uidTag: $LDAP_UIDTAG
EOT
else
echo 'Info: LDAP not enabled'
cat >> server/config/production.yaml <<EOT
ldap:
enabled: false
EOT
fi
fi
if [ -f server/services/workers/reports/config/production.yaml ]; then
echo 'Info: server/production.yaml already provisioned'
else
echo 'Info: Generating server/production.yaml'
cat > server/services/workers/reports/config/production.yaml <<EOT cat > server/services/workers/reports/config/production.yaml <<EOT
mysql: mysql:
host: $mySqlHost host: $MYSQL_HOST
log: log:
level: warn level: warn
EOT EOT
fi
# Wait for the other services to start # Wait for the other services to start
while ! nc -z $mySqlHost 3306; do sleep 1; done echo 'Info: Waiting for MySQL Server'
while ! nc -z $redisHost 6379; do sleep 1; done while ! nc -z $MYSQL_HOST 3306; do sleep 1; done
while ! nc -z $mongoHost 27017; do sleep 1; done
echo 'Info: Waiting for Redis Server'
while ! nc -z $REDIS_HOST 6379; do sleep 1; done
echo 'Info: Waiting for MongoDB Server'
while ! nc -z $MONGO_HOST 27017; do sleep 1; done
cd server cd server
NODE_ENV=production node index.js NODE_ENV=production node index.js