2021-09-01 05:32:08 +00:00
#!/usr/bin/env bash
set -e
check_initialized_db( ) {
2021-11-16 11:02:28 +00:00
echo 'Check initialized database'
shouldPerformInitdb = 1
for path in \
" $MONGO_DB_PATH /WiredTiger " \
" $MONGO_DB_PATH /journal " \
" $MONGO_DB_PATH /local.0 " \
" $MONGO_DB_PATH /storage.bson " ; do
if [ -e " $path " ] ; then
shouldPerformInitdb = 0
return
fi
done
echo "Should initialize database"
2021-09-01 05:32:08 +00:00
}
init_mongodb( ) {
2021-11-16 11:02:28 +00:00
echo "Init database"
MONGO_DB_PATH = "/appsmith-stacks/data/mongodb"
MONGO_LOG_PATH = " $MONGO_DB_PATH /log "
MONGO_DB_KEY = " $MONGO_DB_PATH /key "
mkdir -p " $MONGO_DB_PATH "
touch " $MONGO_LOG_PATH "
check_initialized_db
if [ [ $shouldPerformInitdb -gt 0 ] ] ; then
# Start installed MongoDB service - Dependencies Layer
mongod --fork --port 27017 --dbpath " $MONGO_DB_PATH " --logpath " $MONGO_LOG_PATH "
echo "Waiting 10s for mongodb init"
sleep 10
bash "/opt/appsmith/templates/mongo-init.js.sh" " $MONGO_INITDB_ROOT_USERNAME " " $MONGO_INITDB_ROOT_PASSWORD " >"/appsmith-stacks/configuration/mongo-init.js"
mongo " 127.0.0.1/ ${ MONGO_INITDB_DATABASE } " /appsmith-stacks/configuration/mongo-init.js
echo "Seeding db done"
echo "Enable replica set"
mongod --dbpath " $MONGO_DB_PATH " --shutdown || true
echo "Fork process"
openssl rand -base64 756 >" $MONGO_DB_KEY "
chmod go-rwx,u-wx " $MONGO_DB_KEY "
mongod --fork --port 27017 --dbpath " $MONGO_DB_PATH " --logpath " $MONGO_LOG_PATH " --replSet mr1 --keyFile " $MONGO_DB_KEY " --bind_ip localhost
echo "Waiting 10s for mongodb init with replica set"
sleep 10
mongo " $APPSMITH_MONGODB_URI " --eval 'rs.initiate()'
mongod --dbpath " $MONGO_DB_PATH " --shutdown || true
fi
2021-09-01 05:32:08 +00:00
}
2021-11-23 04:09:13 +00:00
# Keep Let's Encrypt directory persistent
mount_letsencrypt_directory( ) {
echo "Mounting Let's encrypt directory"
2021-11-16 11:02:28 +00:00
rm -rf /etc/letsencrypt
mkdir -p /appsmith-stacks/{ letsencrypt,ssl}
ln -s /appsmith-stacks/letsencrypt /etc/letsencrypt
2021-09-01 05:32:08 +00:00
}
configure_supervisord( ) {
2021-11-16 11:02:28 +00:00
SUPERVISORD_CONF_PATH = "/opt/appsmith/templates/supervisord"
2021-11-23 06:43:10 +00:00
if [ [ -n " $( ls -A /etc/supervisor/conf.d) " ] ] ; then
2021-11-23 04:09:13 +00:00
rm -f "/etc/supervisor/conf.d/" *
2021-11-16 11:02:28 +00:00
fi
cp -f " $SUPERVISORD_CONF_PATH /application_process/ " *.conf /etc/supervisor/conf.d
# Disable services based on configuration
if [ [ " $APPSMITH_MONGODB_URI " = " mongodb://appsmith: $MONGO_INITDB_ROOT_PASSWORD @localhost/appsmith " ] ] ; then
cp " $SUPERVISORD_CONF_PATH /mongodb.conf " /etc/supervisor/conf.d/
fi
if [ [ " $APPSMITH_REDIS_URL " = "redis://127.0.0.1:6379" ] ] ; then
cp " $SUPERVISORD_CONF_PATH /redis.conf " /etc/supervisor/conf.d/
2021-11-26 10:10:16 +00:00
# Enable saving Redis session data to disk more often, so recent sessions aren't cleared on restart.
sed -i 's/^# save 60 10000$/save 60 1/g' /etc/redis/redis.conf
2021-11-16 11:02:28 +00:00
fi
if ! [ [ -e "/appsmith-stacks/ssl/fullchain.pem" ] ] || ! [ [ -e "/appsmith-stacks/ssl/privkey.pem" ] ] ; then
cp " $SUPERVISORD_CONF_PATH /cron.conf " /etc/supervisor/conf.d/
fi
2021-09-01 05:32:08 +00:00
}
echo 'Checking configuration file'
CONF_PATH = "/appsmith-stacks/configuration"
ENV_PATH = " $CONF_PATH /docker.env "
if ! [ [ -e " $ENV_PATH " ] ] ; then
2021-11-16 11:02:28 +00:00
echo "Generating default configuration file"
mkdir -p " $CONF_PATH "
AUTO_GEN_MONGO_PASSWORD = $(
tr -dc A-Za-z0-9 </dev/urandom | head -c 13
echo ''
)
AUTO_GEN_ENCRYPTION_PASSWORD = $(
tr -dc A-Za-z0-9 </dev/urandom | head -c 13
echo ''
)
AUTO_GEN_ENCRYPTION_SALT = $(
tr -dc A-Za-z0-9 </dev/urandom | head -c 13
echo ''
)
bash "/opt/appsmith/templates/docker.env.sh" " $AUTO_GEN_MONGO_PASSWORD " " $AUTO_GEN_ENCRYPTION_PASSWORD " " $AUTO_GEN_ENCRYPTION_SALT " >" $ENV_PATH "
2021-09-01 05:32:08 +00:00
fi
2021-10-05 15:19:22 +00:00
if [ [ -f " $ENV_PATH " ] ] ; then
2021-11-16 11:02:28 +00:00
sed -i 's/APPSMITH_MONGO_USERNAME/MONGO_INITDB_ROOT_USERNAME/; s/APPSMITH_MONGO_PASSWORD/MONGO_INITDB_ROOT_PASSWORD/; s/APPSMITH_MONGO_DATABASE/MONGO_INITDB_DATABASE/' " $ENV_PATH "
echo 'Load environment configuration'
set -o allexport
. " $ENV_PATH "
set +o allexport
2021-09-01 05:32:08 +00:00
fi
# Check for enviroment vairalbes
echo 'Checking environment configuration'
if [ [ -z " ${ APPSMITH_MAIL_ENABLED } " ] ] ; then
2021-11-16 11:02:28 +00:00
unset APPSMITH_MAIL_ENABLED # If this field is empty is might cause application crash
2021-09-01 05:32:08 +00:00
fi
if [ [ -z " ${ APPSMITH_OAUTH2_GITHUB_CLIENT_ID } " ] ] || [ [ -z " ${ APPSMITH_OAUTH2_GITHUB_CLIENT_SECRET } " ] ] ; then
2021-11-16 11:02:28 +00:00
unset APPSMITH_OAUTH2_GITHUB_CLIENT_ID # If this field is empty is might cause application crash
unset APPSMITH_OAUTH2_GITHUB_CLIENT_SECRET
2021-09-01 05:32:08 +00:00
fi
if [ [ -z " ${ APPSMITH_OAUTH2_GOOGLE_CLIENT_ID } " ] ] || [ [ -z " ${ APPSMITH_OAUTH2_GOOGLE_CLIENT_SECRET } " ] ] ; then
2021-11-16 11:02:28 +00:00
unset APPSMITH_OAUTH2_GOOGLE_CLIENT_ID # If this field is empty is might cause application crash
unset APPSMITH_OAUTH2_GOOGLE_CLIENT_SECRET
2021-09-01 05:32:08 +00:00
fi
if [ [ -z " ${ APPSMITH_GOOGLE_MAPS_API_KEY } " ] ] ; then
2021-11-16 11:02:28 +00:00
unset APPSMITH_GOOGLE_MAPS_API_KEY
2021-09-01 05:32:08 +00:00
fi
if [ [ -z " ${ APPSMITH_RECAPTCHA_SITE_KEY } " ] ] || [ [ -z " ${ APPSMITH_RECAPTCHA_SECRET_KEY } " ] ] || [ [ -z " ${ APPSMITH_RECAPTCHA_ENABLED } " ] ] ; then
2021-11-16 11:02:28 +00:00
unset APPSMITH_RECAPTCHA_SITE_KEY # If this field is empty is might cause application crash
unset APPSMITH_RECAPTCHA_SECRET_KEY
unset APPSMITH_RECAPTCHA_ENABLED
2021-09-01 05:32:08 +00:00
fi
# Main Section
init_mongodb
2021-11-23 04:09:13 +00:00
mount_letsencrypt_directory
2021-09-01 05:32:08 +00:00
configure_supervisord
2021-09-14 13:31:06 +00:00
# Ensure the restore path exists in the container, so an archive can be copied to it, if need be.
mkdir -p /appsmith-stacks/data/{ backup,restore}
2021-10-19 07:29:55 +00:00
# Create sub-directory to store services log in the container mounting folder
mkdir -p /appsmith-stacks/logs/{ backend,cron,editor,rts,mongodb,redis}
2021-09-01 05:32:08 +00:00
# Handle CMD command
2021-09-14 13:31:06 +00:00
exec " $@ "