2021-09-01 05:32:08 +00:00
#!/usr/bin/env bash
set -e
2023-06-14 09:40:41 +00:00
2024-06-10 03:53:43 +00:00
tlog " Running as: $( id) "
2023-11-15 05:58:25 +00:00
2024-06-17 07:23:34 +00:00
# Temporary, remove after this change goes into `base.dockerfile`.
export PATH = " /usr/lib/postgresql/13/bin: ${ PATH } "
2023-06-14 09:40:41 +00:00
stacks_path = /appsmith-stacks
2023-09-14 09:13:06 +00:00
export SUPERVISORD_CONF_TARGET = " $TMP /supervisor-conf.d/ " # export for use in supervisord.conf
2023-09-11 07:24:50 +00:00
export MONGODB_TMP_KEY_PATH = " $TMP /mongodb-key " # export for use in supervisor process mongodb.conf
2023-12-05 05:17:36 +00:00
mkdir -pv " $SUPERVISORD_CONF_TARGET " " $WWW_PATH "
2023-09-14 09:13:06 +00:00
2024-05-10 11:47:29 +00:00
setup_proxy_variables( ) {
export NO_PROXY = " ${ NO_PROXY -localhost,127.0.0.1 } "
2023-06-09 04:54:43 +00:00
2024-05-10 11:47:29 +00:00
# Ensure `localhost` and `127.0.0.1` are in always present in `NO_PROXY`.
local no_proxy_lines
no_proxy_lines = " $( echo " $NO_PROXY " | tr , \\ n) "
if ! echo " $no_proxy_lines " | grep -q '^localhost$' ; then
export NO_PROXY = " localhost, $NO_PROXY "
fi
if ! echo " $no_proxy_lines " | grep -q '^127.0.0.1$' ; then
export NO_PROXY = " 127.0.0.1, $NO_PROXY "
fi
2023-06-09 04:54:43 +00:00
2024-05-10 11:47:29 +00:00
# If one of HTTPS_PROXY or https_proxy are set, copy it to the other. If both are set, prefer HTTPS_PROXY.
if [ [ -n ${ HTTPS_PROXY - } ] ] ; then
export https_proxy = " $HTTPS_PROXY "
elif [ [ -n ${ https_proxy - } ] ] ; then
export HTTPS_PROXY = " $https_proxy "
fi
2022-06-01 05:44:27 +00:00
2024-05-10 11:47:29 +00:00
# If one of HTTP_PROXY or http_proxy are set, copy it to the other. If both are set, prefer HTTP_PROXY.
if [ [ -n ${ HTTP_PROXY - } ] ] ; then
export http_proxy = " $HTTP_PROXY "
elif [ [ -n ${ http_proxy - } ] ] ; then
export HTTP_PROXY = " $http_proxy "
fi
2022-03-24 07:47:36 +00:00
}
2022-01-31 07:57:01 +00:00
init_env_file( ) {
CONF_PATH = "/appsmith-stacks/configuration"
ENV_PATH = " $CONF_PATH /docker.env "
TEMPLATES_PATH = "/opt/appsmith/templates"
2022-04-05 14:52:52 +00:00
2024-06-12 12:15:19 +00:00
if [ [ -n " $APPSMITH_MONGODB_URI " ] ] ; then
export APPSMITH_DB_URL = " $APPSMITH_MONGODB_URI "
unset APPSMITH_MONGODB_URI
fi
2022-04-05 14:52:52 +00:00
# Build an env file with current env variables. We single-quote the values, as well as escaping any single-quote characters.
2023-11-15 05:58:25 +00:00
printenv | grep -E '^APPSMITH_|^MONGO_' | sed " s/'/'\\\''/g; s/=/='/; s/ $/'/ " > " $TMP /pre-define.env "
2022-06-01 05:44:27 +00:00
2024-06-10 03:53:43 +00:00
tlog "Initialize .env file"
2022-01-31 07:57:01 +00:00
if ! [ [ -e " $ENV_PATH " ] ] ; then
# Generate new docker.env file when initializing container for first time or in Heroku which does not have persistent volume
2024-06-11 14:40:33 +00:00
tlog "Generating default configuration file"
2022-01-31 07:57:01 +00:00
mkdir -p " $CONF_PATH "
2022-04-15 12:14:08 +00:00
local default_appsmith_mongodb_user = "appsmith"
local generated_appsmith_mongodb_password = $(
2022-01-31 07:57:01 +00:00
tr -dc A-Za-z0-9 </dev/urandom | head -c 13
2022-03-03 03:10:29 +00:00
echo ""
2022-01-31 07:57:01 +00:00
)
2022-04-15 12:14:08 +00:00
local generated_appsmith_encryption_password = $(
2022-01-31 07:57:01 +00:00
tr -dc A-Za-z0-9 </dev/urandom | head -c 13
2022-03-03 03:10:29 +00:00
echo ""
2022-01-31 07:57:01 +00:00
)
2022-04-15 12:14:08 +00:00
local generated_appsmith_encription_salt = $(
2022-01-31 07:57:01 +00:00
tr -dc A-Za-z0-9 </dev/urandom | head -c 13
2022-03-03 03:10:29 +00:00
echo ""
2022-01-31 07:57:01 +00:00
)
2022-04-15 12:14:08 +00:00
local generated_appsmith_supervisor_password = $(
2022-03-24 07:47:36 +00:00
tr -dc A-Za-z0-9 </dev/urandom | head -c 13
echo ''
)
2022-04-15 12:14:08 +00:00
bash " $TEMPLATES_PATH /docker.env.sh " " $default_appsmith_mongodb_user " " $generated_appsmith_mongodb_password " " $generated_appsmith_encryption_password " " $generated_appsmith_encription_salt " " $generated_appsmith_supervisor_password " > " $ENV_PATH "
2022-01-31 07:57:01 +00:00
fi
2022-03-03 03:10:29 +00:00
2024-06-10 03:53:43 +00:00
tlog "Load environment configuration"
2024-06-12 12:15:19 +00:00
# Load the ones in `docker.env` in the stacks folder.
2022-01-31 07:57:01 +00:00
set -o allexport
. " $ENV_PATH "
2024-06-12 12:15:19 +00:00
set +o allexport
if [ [ -n " $APPSMITH_MONGODB_URI " ] ] ; then
export APPSMITH_DB_URL = " $APPSMITH_MONGODB_URI "
unset APPSMITH_MONGODB_URI
fi
# Load the ones set from outside, should take precedence, and so will overwrite anything from `docker.env` above.
set -o allexport
2023-11-15 05:58:25 +00:00
. " $TMP /pre-define.env "
2022-01-31 07:57:01 +00:00
set +o allexport
}
2024-05-10 11:47:29 +00:00
init_env_file
setup_proxy_variables
2022-06-01 05:44:27 +00:00
2024-05-10 11:47:29 +00:00
# ip is a reserved keyword for tracking events in Mixpanel. Instead of showing the ip as is Mixpanel provides derived properties.
# As we want derived props alongwith the ip address we are sharing the ip address in separate keys
# https://help.mixpanel.com/hc/en-us/articles/360001355266-Event-Properties
if [ [ -n ${ APPSMITH_SEGMENT_CE_KEY - } ] ] ; then
ip = " $( set -o pipefail; curl --connect-timeout 5 -sS https://cs.appsmith.com/api/v1/ip | grep -Eo '\d+(\.\d+){3}' || echo "unknown" ) "
curl \
--connect-timeout 5 \
--user " $APPSMITH_SEGMENT_CE_KEY : " \
--header 'Content-Type: application/json' \
--data ' {
"userId" :"'" $ip "'" ,
"event" :"Instance Start" ,
"properties" : {
"ip" : "'" $ip "'" ,
"ipAddress" : "'" $ip "'"
}
} ' \
https://api.segment.io/v1/track \
|| true
fi
2024-01-13 00:54:02 +00:00
2024-05-10 11:47:29 +00:00
if [ [ -n " ${ FILESTORE_IP_ADDRESS - } " ] ] ; then
2022-06-01 05:44:27 +00:00
2024-05-10 11:47:29 +00:00
## Trim APPSMITH_FILESTORE_IP and FILE_SHARE_NAME
FILESTORE_IP_ADDRESS = " $( echo " $FILESTORE_IP_ADDRESS " | xargs) "
FILE_SHARE_NAME = " $( echo " $FILE_SHARE_NAME " | xargs) "
2024-06-10 03:53:43 +00:00
tlog "Running appsmith for cloudRun"
tlog "creating mount point"
2024-05-10 11:47:29 +00:00
mkdir -p " $stacks_path "
2024-06-10 03:53:43 +00:00
tlog "Mounting File Sytem"
2024-05-10 11:47:29 +00:00
mount -t nfs -o nolock " $FILESTORE_IP_ADDRESS :/ $FILE_SHARE_NAME " /appsmith-stacks
2024-06-10 03:53:43 +00:00
tlog "Mounted File Sytem"
tlog "Setting HOSTNAME for Cloudrun"
2024-05-10 11:47:29 +00:00
export HOSTNAME = "cloudrun"
fi
function get_maximum_heap( ) {
resource = $( ulimit -u)
2024-06-10 03:53:43 +00:00
tlog " Resource : $resource "
2024-05-10 11:47:29 +00:00
if [ [ " $resource " -le 256 ] ] ; then
maximum_heap = 128
elif [ [ " $resource " -le 512 ] ] ; then
maximum_heap = 256
fi
}
function setup_backend_heap_arg( ) {
if [ [ ! -z ${ maximum_heap } ] ] ; then
export APPSMITH_JAVA_HEAP_ARG = " -Xmx ${ maximum_heap } m "
fi
2022-06-01 05:44:27 +00:00
}
2022-01-31 07:57:01 +00:00
unset_unused_variables( ) {
# Check for enviroment vairalbes
2024-06-10 03:53:43 +00:00
tlog "Checking environment configuration"
2022-01-31 07:57:01 +00:00
if [ [ -z " ${ APPSMITH_MAIL_ENABLED } " ] ] ; then
unset APPSMITH_MAIL_ENABLED # If this field is empty is might cause application crash
fi
if [ [ -z " ${ APPSMITH_OAUTH2_GITHUB_CLIENT_ID } " ] ] || [ [ -z " ${ APPSMITH_OAUTH2_GITHUB_CLIENT_SECRET } " ] ] ; then
unset APPSMITH_OAUTH2_GITHUB_CLIENT_ID # If this field is empty is might cause application crash
unset APPSMITH_OAUTH2_GITHUB_CLIENT_SECRET
fi
if [ [ -z " ${ APPSMITH_OAUTH2_GOOGLE_CLIENT_ID } " ] ] || [ [ -z " ${ APPSMITH_OAUTH2_GOOGLE_CLIENT_SECRET } " ] ] ; then
unset APPSMITH_OAUTH2_GOOGLE_CLIENT_ID # If this field is empty is might cause application crash
unset APPSMITH_OAUTH2_GOOGLE_CLIENT_SECRET
fi
if [ [ -z " ${ APPSMITH_RECAPTCHA_SITE_KEY } " ] ] || [ [ -z " ${ APPSMITH_RECAPTCHA_SECRET_KEY } " ] ] || [ [ -z " ${ APPSMITH_RECAPTCHA_ENABLED } " ] ] ; then
unset APPSMITH_RECAPTCHA_SITE_KEY # If this field is empty is might cause application crash
unset APPSMITH_RECAPTCHA_SECRET_KEY
unset APPSMITH_RECAPTCHA_ENABLED
fi
2023-10-18 02:28:56 +00:00
export APPSMITH_SUPERVISOR_USER = " ${ APPSMITH_SUPERVISOR_USER :- appsmith } "
if [ [ -z " ${ APPSMITH_SUPERVISOR_PASSWORD - } " ] ] ; then
APPSMITH_SUPERVISOR_PASSWORD = " $( tr -dc A-Za-z0-9 </dev/urandom | head -c 13) "
export APPSMITH_SUPERVISOR_PASSWORD
fi
2022-01-31 07:57:01 +00:00
}
2024-05-22 10:25:20 +00:00
configure_database_connection_url( ) {
2024-06-10 03:53:43 +00:00
tlog "Configuring database connection URL"
2024-05-22 10:25:20 +00:00
isPostgresUrl = 0
isMongoUrl = 0
if [ [ " ${ APPSMITH_DB_URL } " = = "postgresql:" * ] ] ; then
isPostgresUrl = 1
elif [ [ " ${ APPSMITH_DB_URL } " = = "mongodb" * ] ] ; then
isMongoUrl = 1
fi
}
check_db_uri( ) {
2024-06-10 03:53:43 +00:00
tlog "Checking APPSMITH_DB_URL"
2022-02-09 09:02:46 +00:00
isUriLocal = 1
2024-05-22 10:25:20 +00:00
if [ [ $APPSMITH_DB_URL = = *"localhost" * || $APPSMITH_DB_URL = = *"127.0.0.1" * ] ] ; then
2024-06-10 03:53:43 +00:00
tlog "Detected local DB"
2022-02-09 09:02:46 +00:00
isUriLocal = 0
fi
}
init_mongodb( ) {
if [ [ $isUriLocal -eq 0 ] ] ; then
2024-06-10 03:53:43 +00:00
tlog "Initializing local database"
2022-07-20 15:12:23 +00:00
MONGO_DB_PATH = " $stacks_path /data/mongodb "
2022-02-09 09:02:46 +00:00
MONGO_LOG_PATH = " $MONGO_DB_PATH /log "
MONGO_DB_KEY = " $MONGO_DB_PATH /key "
mkdir -p " $MONGO_DB_PATH "
touch " $MONGO_LOG_PATH "
2022-07-20 15:12:23 +00:00
if [ [ ! -f " $MONGO_DB_KEY " ] ] ; then
openssl rand -base64 756 > " $MONGO_DB_KEY "
2022-02-09 09:02:46 +00:00
fi
2022-09-19 08:19:27 +00:00
use-mongodb-key " $MONGO_DB_KEY "
2023-10-17 00:58:45 +00:00
./mongodb-fixer.sh &
2022-02-09 09:02:46 +00:00
fi
}
init_replica_set( ) {
2024-06-10 03:53:43 +00:00
tlog "Checking initialized database"
2021-11-16 11:02:28 +00:00
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
2022-02-09 09:02:46 +00:00
break
2021-11-16 11:02:28 +00:00
fi
done
2022-03-10 04:46:16 +00:00
2023-03-30 00:30:35 +00:00
if [ [ $isUriLocal -gt 0 && -f /proc/cpuinfo ] ] && ! grep --quiet avx /proc/cpuinfo; then
2024-06-10 03:53:43 +00:00
tlog "====================================================================================================" >& 2
tlog "==" >& 2
tlog "== AVX instruction not found in your CPU. Appsmith's embedded MongoDB may not start. Please use an external MongoDB instance instead." >& 2
tlog "== See https://docs.appsmith.com/getting-started/setup/instance-configuration/custom-mongodb-redis#custom-mongodb for instructions." >& 2
tlog "==" >& 2
tlog "====================================================================================================" >& 2
2023-03-30 00:30:35 +00:00
fi
2022-02-09 09:02:46 +00:00
if [ [ $shouldPerformInitdb -gt 0 && $isUriLocal -eq 0 ] ] ; then
2024-06-10 03:53:43 +00:00
tlog "Initializing Replica Set for local database"
2021-11-16 11:02:28 +00:00
# Start installed MongoDB service - Dependencies Layer
mongod --fork --port 27017 --dbpath " $MONGO_DB_PATH " --logpath " $MONGO_LOG_PATH "
2024-06-10 03:53:43 +00:00
tlog "Waiting 10s for MongoDB to start"
2021-11-16 11:02:28 +00:00
sleep 10
2024-06-10 03:53:43 +00:00
tlog "Creating MongoDB user"
2023-01-27 10:35:00 +00:00
mongosh "127.0.0.1/appsmith" --eval " db.createUser({
user: '$APPSMITH_MONGODB_USER' ,
pwd: '$APPSMITH_MONGODB_PASSWORD' ,
roles: [ {
role: 'root' ,
db: 'admin'
} , 'readWrite' ]
}
) "
2024-06-10 03:53:43 +00:00
tlog "Enabling Replica Set"
2021-11-16 11:02:28 +00:00
mongod --dbpath " $MONGO_DB_PATH " --shutdown || true
2023-09-11 07:24:50 +00:00
mongod --fork --port 27017 --dbpath " $MONGO_DB_PATH " --logpath " $MONGO_LOG_PATH " --replSet mr1 --keyFile " $MONGODB_TMP_KEY_PATH " --bind_ip localhost
2024-06-10 03:53:43 +00:00
tlog "Waiting 10s for MongoDB to start with Replica Set"
2021-11-16 11:02:28 +00:00
sleep 10
2024-05-22 10:25:20 +00:00
mongosh " $APPSMITH_DB_URL " --eval 'rs.initiate()'
2021-11-16 11:02:28 +00:00
mongod --dbpath " $MONGO_DB_PATH " --shutdown || true
fi
2022-02-09 09:02:46 +00:00
if [ [ $isUriLocal -gt 0 ] ] ; then
2024-06-10 03:53:43 +00:00
tlog "Checking Replica Set of external MongoDB"
2022-02-09 09:02:46 +00:00
fix: Fix replicaset check to not require ClusterMonitor role (#19997)
In the `entrypoint.sh` script, we check if the MongoDB in use, has
replicaSet initiated or not. This is usually done with a `rs.initiate()`
on the cluster.
We need the replicaSet to be enabled on MongoDB, since the backend
server relies on MongoDB `changeStream`s, which is a feature, only
available if replicaSet is enabled.
However, to use the `changeStream` APIs, having the `read` or
`readWrite` role on MongoDB is enough. But the check we do in
`entrypoint.sh`, runs `rs.status()` to see if `replicaSet` is initiated.
This `rs.status()` call, unfortunately, requires the `ClusterMonitor`
role, unlike the `changeStream` API.
To tackle this, we created the `appsmithctl check_replica_set` command.
This command would attempt to use the `changeStream` API, and report
success or failure. But this failed on certain configurations, where
MongoDB was running as a single-node-cluster, on localhost, or a
local/internal network. This was an edge case.
That edge case is solved by this PR. With this, we can use `appsmithctl
check-replica-set` in the `entrypoint.sh` again.
2023-02-07 00:08:37 +00:00
if appsmithctl check-replica-set; then
2024-06-10 03:53:43 +00:00
tlog "MongoDB ReplicaSet is enabled"
2022-02-09 09:02:46 +00:00
else
fix: Fix replicaset check to not require ClusterMonitor role (#19997)
In the `entrypoint.sh` script, we check if the MongoDB in use, has
replicaSet initiated or not. This is usually done with a `rs.initiate()`
on the cluster.
We need the replicaSet to be enabled on MongoDB, since the backend
server relies on MongoDB `changeStream`s, which is a feature, only
available if replicaSet is enabled.
However, to use the `changeStream` APIs, having the `read` or
`readWrite` role on MongoDB is enough. But the check we do in
`entrypoint.sh`, runs `rs.status()` to see if `replicaSet` is initiated.
This `rs.status()` call, unfortunately, requires the `ClusterMonitor`
role, unlike the `changeStream` API.
To tackle this, we created the `appsmithctl check_replica_set` command.
This command would attempt to use the `changeStream` API, and report
success or failure. But this failed on certain configurations, where
MongoDB was running as a single-node-cluster, on localhost, or a
local/internal network. This was an edge case.
That edge case is solved by this PR. With this, we can use `appsmithctl
check-replica-set` in the `entrypoint.sh` again.
2023-02-07 00:08:37 +00:00
echo -e "\033[0;31m***************************************************************************************\033[0m"
echo -e "\033[0;31m* MongoDB Replica Set is not enabled *\033[0m"
2023-03-20 07:48:52 +00:00
echo -e "\033[0;31m* Please ensure the credentials provided for MongoDB, has 'readWrite' role. *\033[0m"
fix: Fix replicaset check to not require ClusterMonitor role (#19997)
In the `entrypoint.sh` script, we check if the MongoDB in use, has
replicaSet initiated or not. This is usually done with a `rs.initiate()`
on the cluster.
We need the replicaSet to be enabled on MongoDB, since the backend
server relies on MongoDB `changeStream`s, which is a feature, only
available if replicaSet is enabled.
However, to use the `changeStream` APIs, having the `read` or
`readWrite` role on MongoDB is enough. But the check we do in
`entrypoint.sh`, runs `rs.status()` to see if `replicaSet` is initiated.
This `rs.status()` call, unfortunately, requires the `ClusterMonitor`
role, unlike the `changeStream` API.
To tackle this, we created the `appsmithctl check_replica_set` command.
This command would attempt to use the `changeStream` API, and report
success or failure. But this failed on certain configurations, where
MongoDB was running as a single-node-cluster, on localhost, or a
local/internal network. This was an edge case.
That edge case is solved by this PR. With this, we can use `appsmithctl
check-replica-set` in the `entrypoint.sh` again.
2023-02-07 00:08:37 +00:00
echo -e "\033[0;31m***************************************************************************************\033[0m"
2022-02-09 09:02:46 +00:00
exit 1
fi
fi
2021-09-01 05:32:08 +00:00
}
2022-09-19 08:19:27 +00:00
use-mongodb-key( ) {
2023-09-11 07:24:50 +00:00
# We copy the MongoDB key file to `$MONGODB_TMP_KEY_PATH`, so that we can reliably set its permissions to 600.
# Why? When the host machine of this Docker container is Windows, file permissions cannot be set on files in volumes.
2023-01-27 10:35:00 +00:00
# So the key file should be somewhere inside the container, and not in a volume.
2023-09-11 07:24:50 +00:00
mkdir -pv " $( dirname " $MONGODB_TMP_KEY_PATH " ) "
cp -v " $1 " " $MONGODB_TMP_KEY_PATH "
chmod 600 " $MONGODB_TMP_KEY_PATH "
2022-01-10 11:49:29 +00:00
}
2022-06-01 05:44:27 +00:00
is_empty_directory( ) {
[ [ -d $1 && -z " $( ls -A " $1 " ) " ] ]
}
check_setup_custom_ca_certificates( ) {
2023-10-06 14:08:19 +00:00
# old, deprecated, should be removed.
2022-06-01 05:44:27 +00:00
local stacks_ca_certs_path
stacks_ca_certs_path = " $stacks_path /ca-certs "
local container_ca_certs_path
container_ca_certs_path = "/usr/local/share/ca-certificates"
if [ [ -d $stacks_ca_certs_path ] ] ; then
if [ [ ! -L $container_ca_certs_path ] ] ; then
if is_empty_directory " $container_ca_certs_path " ; then
rmdir -v " $container_ca_certs_path "
else
2024-06-10 03:53:43 +00:00
tlog "The 'ca-certificates' directory inside the container is not empty. Please clear it and restart to use certs from 'stacks/ca-certs' directory." >& 2
2022-06-01 05:44:27 +00:00
return
fi
fi
ln --verbose --force --symbolic --no-target-directory " $stacks_ca_certs_path " " $container_ca_certs_path "
elif [ [ ! -e $container_ca_certs_path ] ] ; then
rm -vf " $container_ca_certs_path " # If it exists as a broken symlink, this will be needed.
mkdir -v " $container_ca_certs_path "
fi
2023-10-06 14:08:19 +00:00
update-ca-certificates --fresh
}
setup-custom-ca-certificates( ) (
local stacks_ca_certs_path = " $stacks_path /ca-certs "
local store = " $TMP /cacerts "
local opts_file = " $TMP /java-cacerts-opts "
rm -f " $store " " $opts_file "
2022-06-01 05:44:27 +00:00
if [ [ -n " $( ls " $stacks_ca_certs_path " /*.pem 2>/dev/null) " ] ] ; then
2024-06-10 03:53:43 +00:00
tlog "Looks like you have some '.pem' files in your 'ca-certs' folder. Please rename them to '.crt' to be picked up automatically." .
2022-06-01 05:44:27 +00:00
fi
2023-10-06 14:08:19 +00:00
if ! [ [ -d " $stacks_ca_certs_path " && " $( find " $stacks_ca_certs_path " -maxdepth 1 -type f -name '*.crt' | wc -l) " -gt 0 ] ] ; then
2024-06-10 03:53:43 +00:00
tlog "No custom CA certificates found."
2023-10-06 14:08:19 +00:00
return
fi
# Import the system CA certificates into the store.
keytool -importkeystore \
-srckeystore /opt/java/lib/security/cacerts \
-destkeystore " $store " \
-srcstorepass changeit \
-deststorepass changeit
# Add the custom CA certificates to the store.
2024-04-04 09:18:39 +00:00
find -L " $stacks_ca_certs_path " -maxdepth 1 -type f -name '*.crt' \
2023-11-16 13:49:04 +00:00
-print \
-exec keytool -import -alias '{}' -noprompt -keystore " $store " -file '{}' -storepass changeit ';'
2023-10-06 14:08:19 +00:00
{
echo " -Djavax.net.ssl.trustStore= $store "
echo "-Djavax.net.ssl.trustStorePassword=changeit"
} > " $opts_file "
)
2022-06-01 05:44:27 +00:00
2021-09-01 05:32:08 +00:00
configure_supervisord( ) {
2023-09-14 09:13:06 +00:00
local supervisord_conf_source = "/opt/appsmith/templates/supervisord"
if [ [ -n " $( ls -A " $SUPERVISORD_CONF_TARGET " ) " ] ] ; then
rm -f " $SUPERVISORD_CONF_TARGET " /*
2021-11-16 11:02:28 +00:00
fi
2023-09-14 09:13:06 +00:00
cp -f " $supervisord_conf_source " /application_process/*.conf " $SUPERVISORD_CONF_TARGET "
2021-11-16 11:02:28 +00:00
# Disable services based on configuration
2022-03-24 07:47:36 +00:00
if [ [ -z " ${ DYNO } " ] ] ; then
if [ [ $isUriLocal -eq 0 ] ] ; then
2023-09-14 09:13:06 +00:00
cp " $supervisord_conf_source /mongodb.conf " " $SUPERVISORD_CONF_TARGET "
2022-03-24 07:47:36 +00:00
fi
if [ [ $APPSMITH_REDIS_URL = = *"localhost" * || $APPSMITH_REDIS_URL = = *"127.0.0.1" * ] ] ; then
2023-09-14 09:13:06 +00:00
cp " $supervisord_conf_source /redis.conf " " $SUPERVISORD_CONF_TARGET "
2023-09-05 07:08:51 +00:00
mkdir -p " $stacks_path /data/redis "
2022-03-24 07:47:36 +00:00
fi
2023-03-07 09:52:41 +00:00
if [ [ $runEmbeddedPostgres -eq 1 ] ] ; then
2023-09-14 09:13:06 +00:00
cp " $supervisord_conf_source /postgres.conf " " $SUPERVISORD_CONF_TARGET "
2023-03-07 09:52:41 +00:00
fi
2021-11-16 11:02:28 +00:00
fi
2023-09-20 10:22:35 +00:00
2021-09-01 05:32:08 +00:00
}
2023-10-05 04:56:39 +00:00
# This is a workaround to get Redis working on different memory pagesize
2022-04-01 06:58:03 +00:00
# https://github.com/appsmithorg/appsmith/issues/11773
check_redis_compatible_page_size( ) {
local page_size
page_size = " $( getconf PAGE_SIZE) "
if [ [ $page_size -gt 4096 ] ] ; then
2023-10-05 04:56:39 +00:00
curl \
2024-05-10 11:47:29 +00:00
--connect-timeout 5 \
2023-10-05 04:56:39 +00:00
--silent \
--user " $APPSMITH_SEGMENT_CE_KEY : " \
--header 'Content-Type: application/json' \
--data '{ "userId": "' " $HOSTNAME " '", "event":"RedisCompile" }' \
https://api.segment.io/v1/track \
|| true
2024-06-10 03:53:43 +00:00
tlog " Compile Redis stable with page size of $page_size "
2023-10-05 04:56:39 +00:00
apt-get update
apt-get install --yes build-essential
2024-05-10 11:47:29 +00:00
curl --connect-timeout 5 --location https://download.redis.io/redis-stable.tar.gz | tar -xz -C /tmp
2023-10-05 04:56:39 +00:00
pushd /tmp/redis-stable
make
make install
popd
rm -rf /tmp/redis-stable
2022-04-01 06:58:03 +00:00
else
2024-06-10 03:53:43 +00:00
tlog " Redis is compatible with page size of $page_size "
2022-04-01 06:58:03 +00:00
fi
}
2023-03-07 09:52:41 +00:00
init_postgres( ) {
# Initialize embedded postgres by default; set APPSMITH_ENABLE_EMBEDDED_DB to 0, to use existing cloud postgres mockdb instance
if [ [ ${ APPSMITH_ENABLE_EMBEDDED_DB : -1 } != 0 ] ] ; then
2024-06-10 03:53:43 +00:00
tlog "Checking initialized local postgres"
2023-03-07 09:52:41 +00:00
POSTGRES_DB_PATH = " $stacks_path /data/postgres/main "
2024-01-23 05:12:18 +00:00
mkdir -p " $POSTGRES_DB_PATH " " $TMP /pg-runtime "
# Postgres does not allow it's server to be run with super user access, we use user postgres and the file system owner also needs to be the same user postgres
chown -R postgres:postgres " $POSTGRES_DB_PATH " " $TMP /pg-runtime "
2024-06-17 07:15:49 +00:00
if [ [ ! -e " $POSTGRES_DB_PATH /PG_VERSION " ] ] ; then
tlog "Initializing local Postgres data folder"
2024-06-17 07:23:34 +00:00
su postgres -c " initdb -D $POSTGRES_DB_PATH "
2023-03-07 09:52:41 +00:00
fi
else
2023-03-21 08:28:18 +00:00
runEmbeddedPostgres = 0
2023-03-07 09:52:41 +00:00
fi
2023-03-21 08:28:18 +00:00
2023-03-07 09:52:41 +00:00
}
safe_init_postgres( ) {
runEmbeddedPostgres = 1
# fail safe to prevent entrypoint from exiting, and prevent postgres from starting
2023-03-21 08:28:18 +00:00
init_postgres || runEmbeddedPostgres = 0
2023-03-07 09:52:41 +00:00
}
2024-05-24 07:41:56 +00:00
setup_caddy( ) {
if [ [ " $APPSMITH_RATE_LIMIT " = = "disabled" ] ] ; then
export _APPSMITH_CADDY = "/opt/caddy/caddy_vanilla"
else
export _APPSMITH_CADDY = "/opt/caddy/caddy"
fi
}
2023-04-19 12:50:59 +00:00
init_loading_pages( ) {
2023-12-05 05:17:36 +00:00
export XDG_DATA_HOME = /appsmith-stacks/data # so that caddy saves tls certs and other data under stacks/data/caddy
export XDG_CONFIG_HOME = /appsmith-stacks/configuration
mkdir -p " $XDG_DATA_HOME " " $XDG_CONFIG_HOME "
cp templates/loading.html " $WWW_PATH "
node caddy-reconfigure.mjs
2024-05-24 07:41:56 +00:00
" $_APPSMITH_CADDY " start --config " $TMP /Caddyfile "
2023-04-19 12:50:59 +00:00
}
2023-12-07 04:21:29 +00:00
function setup_auto_heal( ) {
if [ [ ${ APPSMITH_AUTO_HEAL - } = 1 ] ] ; then
# By default APPSMITH_AUTO_HEAL=0
# To enable auto heal set APPSMITH_AUTO_HEAL=1
2024-03-20 14:15:48 +00:00
bash /opt/appsmith/auto_heal.sh $APPSMITH_AUTO_HEAL_CURL_TIMEOUT >> " $APPSMITH_LOG_DIR " /cron/auto_heal.log 2>& 1 &
2023-12-07 04:21:29 +00:00
fi
}
2024-04-22 04:05:14 +00:00
print_appsmith_info( ) {
tr '\n' ' ' < /opt/appsmith/info.json
}
2024-04-26 13:26:45 +00:00
function capture_infra_details( ) {
bash /opt/appsmith/generate-infra-details.sh || true
}
2021-09-01 05:32:08 +00:00
# Main Section
2024-04-22 04:05:14 +00:00
print_appsmith_info
2024-05-24 07:41:56 +00:00
setup_caddy
2023-04-19 12:50:59 +00:00
init_loading_pages
2022-01-31 07:57:01 +00:00
unset_unused_variables
2022-06-01 05:44:27 +00:00
2024-05-22 10:25:20 +00:00
configure_database_connection_url
check_db_uri
# Don't run MongoDB if running in a Heroku dyno.
2022-03-24 07:47:36 +00:00
if [ [ -z " ${ DYNO } " ] ] ; then
2024-05-22 10:25:20 +00:00
if [ [ $isMongoUrl -eq 1 ] ] ; then
# Setup MongoDB and initialize replica set
2024-06-10 03:53:43 +00:00
tlog "Initializing MongoDB"
2024-05-22 10:25:20 +00:00
init_mongodb
init_replica_set
elif [ [ $isPostgresUrl -eq 1 ] ] ; then
2024-06-10 03:53:43 +00:00
tlog "Initializing Postgres"
2024-05-22 10:25:20 +00:00
# init_postgres
fi
2022-06-01 05:44:27 +00:00
else
2022-04-08 13:18:23 +00:00
# These functions are used to limit heap size for Backend process when deployed on Heroku
get_maximum_heap
setup_backend_heap_arg
2022-09-29 11:49:24 +00:00
# set the hostname for heroku dyno
export HOSTNAME = "heroku_dyno"
2022-03-24 07:47:36 +00:00
fi
2022-06-01 05:44:27 +00:00
check_setup_custom_ca_certificates
2023-10-06 14:08:19 +00:00
setup-custom-ca-certificates
2022-04-01 06:58:03 +00:00
check_redis_compatible_page_size
2022-06-01 05:44:27 +00:00
2023-03-07 09:52:41 +00:00
safe_init_postgres
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.
2024-01-12 01:58:13 +00:00
mkdir -p /appsmith-stacks/data/{ backup,restore} /appsmith-stacks/ssl
2021-09-14 13:31:06 +00:00
2021-10-19 07:29:55 +00:00
# Create sub-directory to store services log in the container mounting folder
2024-03-20 14:15:48 +00:00
export APPSMITH_LOG_DIR = " ${ APPSMITH_LOG_DIR :- /appsmith-stacks/logs } "
mkdir -p " $APPSMITH_LOG_DIR " /{ supervisor,backend,cron,editor,rts,mongodb,redis,postgres,appsmithctl}
2023-04-19 12:50:59 +00:00
2023-12-07 04:21:29 +00:00
setup_auto_heal
2024-04-26 13:26:45 +00:00
capture_infra_details
2023-12-07 04:21:29 +00:00
2021-09-01 05:32:08 +00:00
# Handle CMD command
2021-09-14 13:31:06 +00:00
exec " $@ "