2022-07-08 19:33:07 +02:00
#!/bin/bash
2022-08-11 13:08:56 +02:00
declare -a ENV_VARS = ( "COUCHDB_USER" "COUCHDB_PASSWORD" "DATA_DIR" "MINIO_ACCESS_KEY" "MINIO_SECRET_KEY" "INTERNAL_API_KEY" "JWT_SECRET" "REDIS_PASSWORD" )
2022-09-23 14:40:00 +02:00
declare -a DOCKER_VARS = ( "APP_PORT" "APPS_URL" "ARCHITECTURE" "BUDIBASE_ENVIRONMENT" "CLUSTER_PORT" "DEPLOYMENT_ENVIRONMENT" "MINIO_URL" "NODE_ENV" "POSTHOG_TOKEN" "REDIS_URL" "SELF_HOSTED" "WORKER_PORT" "WORKER_URL" "TENANT_FEATURE_FLAGS" "ACCOUNT_PORTAL_URL" )
2022-09-13 17:08:54 +02:00
# Check the env vars set in Dockerfile have come through, AAS seems to drop them
[ [ -z " ${ APP_PORT } " ] ] && export APP_PORT = 4001
[ [ -z " ${ ARCHITECTURE } " ] ] && export ARCHITECTURE = amd
[ [ -z " ${ BUDIBASE_ENVIRONMENT } " ] ] && export BUDIBASE_ENVIRONMENT = PRODUCTION
[ [ -z " ${ CLUSTER_PORT } " ] ] && export CLUSTER_PORT = 80
[ [ -z " ${ DEPLOYMENT_ENVIRONMENT } " ] ] && export DEPLOYMENT_ENVIRONMENT = docker
2023-12-19 16:58:08 +01:00
[ [ -z " ${ MINIO_URL } " ] ] && [ [ -z " ${ USE_S3 } " ] ] && export MINIO_URL = http://127.0.0.1:9000
2022-09-13 17:08:54 +02:00
[ [ -z " ${ NODE_ENV } " ] ] && export NODE_ENV = production
[ [ -z " ${ POSTHOG_TOKEN } " ] ] && export POSTHOG_TOKEN = phc_bIjZL7oh2GEUd2vqvTBH8WvrX0fWTFQMs6H5KQxiUxU
2023-02-01 15:12:43 +01:00
[ [ -z " ${ TENANT_FEATURE_FLAGS } " ] ] && export TENANT_FEATURE_FLAGS = "*:LICENSING,*:USER_GROUPS,*:ONBOARDING_TOUR"
2022-09-23 14:40:00 +02:00
[ [ -z " ${ ACCOUNT_PORTAL_URL } " ] ] && export ACCOUNT_PORTAL_URL = https://account.budibase.app
2023-10-16 15:51:37 +02:00
[ [ -z " ${ REDIS_URL } " ] ] && export REDIS_URL = 127.0.0.1:6379
2022-09-13 17:08:54 +02:00
[ [ -z " ${ SELF_HOSTED } " ] ] && export SELF_HOSTED = 1
[ [ -z " ${ WORKER_PORT } " ] ] && export WORKER_PORT = 4002
2023-10-16 15:51:37 +02:00
[ [ -z " ${ WORKER_URL } " ] ] && export WORKER_URL = http://127.0.0.1:4002
[ [ -z " ${ APPS_URL } " ] ] && export APPS_URL = http://127.0.0.1:4001
2023-06-07 17:35:36 +02:00
[ [ -z " ${ SERVER_TOP_LEVEL_PATH } " ] ] && export SERVER_TOP_LEVEL_PATH = /app
2022-09-14 14:24:11 +02:00
# export CUSTOM_DOMAIN=budi001.custom.com
2022-10-13 12:57:00 +02:00
2022-08-11 13:08:56 +02:00
# Azure App Service customisations
if [ [ " ${ TARGETBUILD } " = "aas" ] ] ; then
2023-11-24 16:31:43 +01:00
export DATA_DIR = " ${ DATA_DIR :- /home } "
2022-09-30 12:43:42 +02:00
WEBSITES_ENABLE_APP_SERVICE_STORAGE = true
2022-08-11 13:08:56 +02:00
/etc/init.d/ssh start
else
2023-11-24 16:31:43 +01:00
export DATA_DIR = ${ DATA_DIR :- /data }
2022-08-11 13:08:56 +02:00
fi
2022-11-13 18:53:32 +01:00
mkdir -p ${ DATA_DIR }
2022-10-13 12:57:00 +02:00
# Mount NFS or GCP Filestore if env vars exist for it
2022-11-13 18:53:32 +01:00
if [ [ ! -z ${ FILESHARE_IP } && ! -z ${ FILESHARE_NAME } ] ] ; then
echo "Mounting NFS share"
apt update && apt install -y nfs-common nfs-kernel-server
2022-10-13 12:57:00 +02:00
echo " Mount file share ${ FILESHARE_IP } :/ ${ FILESHARE_NAME } to ${ DATA_DIR } "
mount -o nolock ${ FILESHARE_IP } :/${ FILESHARE_NAME } ${ DATA_DIR }
2022-11-13 18:53:32 +01:00
echo " Mounting result: $? "
2022-10-13 12:57:00 +02:00
fi
2022-08-11 13:08:56 +02:00
if [ -f " ${ DATA_DIR } /.env " ] ; then
2022-09-13 17:08:54 +02:00
# Read in the .env file and export the variables
for LINE in $( cat ${ DATA_DIR } /.env) ; do export $LINE ; done
2022-07-08 19:33:07 +02:00
fi
2022-09-13 17:08:54 +02:00
# randomise any unset environment variables
2022-07-08 19:33:07 +02:00
for ENV_VAR in " ${ ENV_VARS [@] } "
do
temp = $( eval " echo \$ $ENV_VAR " )
if [ [ -z " ${ temp } " ] ] ; then
eval " export $ENV_VAR = $( uuidgen | sed -e 's/-//g' ) "
fi
done
if [ [ -z " ${ COUCH_DB_URL } " ] ] ; then
2023-10-16 15:51:37 +02:00
export COUCH_DB_URL = http://$COUCHDB_USER :$COUCHDB_PASSWORD @127.0.0.1:5984
2022-07-08 19:33:07 +02:00
fi
2022-08-11 13:08:56 +02:00
if [ ! -f " ${ DATA_DIR } /.env " ] ; then
touch ${ DATA_DIR } /.env
2022-07-08 19:33:07 +02:00
for ENV_VAR in " ${ ENV_VARS [@] } "
do
temp = $( eval " echo \$ $ENV_VAR " )
2022-08-11 13:08:56 +02:00
echo " $ENV_VAR = $temp " >> ${ DATA_DIR } /.env
2022-07-08 19:33:07 +02:00
done
2022-09-13 17:08:54 +02:00
for ENV_VAR in " ${ DOCKER_VARS [@] } "
do
temp = $( eval " echo \$ $ENV_VAR " )
echo " $ENV_VAR = $temp " >> ${ DATA_DIR } /.env
done
2022-08-11 13:08:56 +02:00
echo " COUCH_DB_URL= ${ COUCH_DB_URL } " >> ${ DATA_DIR } /.env
2022-07-08 19:33:07 +02:00
fi
2022-09-13 17:08:54 +02:00
# Read in the .env file and export the variables
for LINE in $( cat ${ DATA_DIR } /.env) ; do export $LINE ; done
2023-10-03 09:34:35 +02:00
ln -s ${ DATA_DIR } /.env /app/.env
ln -s ${ DATA_DIR } /.env /worker/.env
2022-07-08 19:33:07 +02:00
# make these directories in runner, incase of mount
2022-08-11 13:08:56 +02:00
mkdir -p ${ DATA_DIR } /minio
2022-09-07 11:51:53 +02:00
chown -R couchdb:couchdb ${ DATA_DIR } /couch
2022-11-13 18:53:32 +01:00
redis-server --requirepass $REDIS_PASSWORD > /dev/stdout 2>& 1 &
2022-12-01 18:08:49 +01:00
/bbcouch-runner.sh &
2023-11-04 12:53:14 +01:00
/minio/minio server --console-address ":9001" ${ DATA_DIR } /minio > /dev/stdout 2>& 1 &
2022-06-22 14:38:33 +02:00
/etc/init.d/nginx restart
if [ [ ! -z " ${ CUSTOM_DOMAIN } " ] ] ; then
2022-06-22 14:57:46 +02:00
# Add monthly cron job to renew certbot certificate
echo -n " * * 2 * * root exec /app/letsencrypt/certificate-renew.sh ${ CUSTOM_DOMAIN } " >> /etc/cron.d/certificate-renew
chmod +x /etc/cron.d/certificate-renew
# Request the certbot certificate
2022-06-22 14:38:33 +02:00
/app/letsencrypt/certificate-request.sh ${ CUSTOM_DOMAIN }
2022-11-13 18:53:32 +01:00
/etc/init.d/nginx restart
2022-06-22 14:38:33 +02:00
fi
2022-12-01 18:08:49 +01:00
# wait for backend services to start
sleep 10
2022-04-01 02:00:52 +02:00
pushd app
2022-11-13 18:53:32 +01:00
pm2 start -l /dev/stdout --name app "yarn run:docker"
2022-04-01 02:00:52 +02:00
popd
pushd worker
2022-11-13 18:53:32 +01:00
pm2 start -l /dev/stdout --name worker "yarn run:docker"
2022-04-01 02:00:52 +02:00
popd
2022-11-13 18:53:32 +01:00
echo "end of runner.sh, sleeping ..."
2022-06-22 15:38:45 +02:00
sleep infinity