Skip to content

Commit

Permalink
update script and env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
aarongundel committed Oct 31, 2024
1 parent 3154f44 commit 2fe205d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 288 deletions.
13 changes: 8 additions & 5 deletions arches_lingo/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,14 @@ def get_optional_env_variable(var_name, default=None) -> str:
SECRETS_MODE = get_optional_env_variable("ARCHES_SECRETS_MODE", "ENV")

APP_ROOT = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
DB_NAME = get_optional_env_variable("ARCHES_DB_NAME", APP_NAME)
DB_USER = get_optional_env_variable("ARCHES_PGUSERNAME", "postgres")
DB_PASSWORD = get_optional_env_variable("ARCHES_PGPASSWORD", "postgis")
DB_HOST = get_optional_env_variable("ARCHES_PGHOST", "localhost")
DB_PORT = get_optional_env_variable("ARCHES_PGPORT", "5432")

# environment variable names for postgres are built-ins for the pg client, do not modify
DB_NAME = get_optional_env_variable("PGDATABASE", APP_NAME)
DB_USER = get_optional_env_variable("PGUSER", "postgres")
DB_PASSWORD = get_optional_env_variable("PGPASSWORD", "postgis")
DB_HOST = get_optional_env_variable("PGHOST", "localhost")
DB_PORT = get_optional_env_variable("PGPORT", "5432")

ES_USER = get_optional_env_variable("ARCHES_ESUSER", "elastic")
ES_PASSWORD = get_optional_env_variable("ARCHES_ESPASSWORD", "E1asticSearchforArche5")
ES_HOST = get_optional_env_variable("ARCHES_ESHOST", "localhost")
Expand Down
99 changes: 48 additions & 51 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,10 @@ if [[ -z ${ARCHES_PROJECT} ]]; then
APP_FOLDER=${ARCHES_ROOT}
PACKAGE_JSON_FOLDER=${ARCHES_ROOT}
else
APP_FOLDER=${WEB_ROOT}/${ARCHES_PROJECT_ROOT_DIRECTORY}
APP_FOLDER=${WEB_ROOT}/${ARCHES_PROJECT}
PACKAGE_JSON_FOLDER=${ARCHES_ROOT}
fi

# npm_MODULES_FOLDER=${PACKAGE_JSON_FOLDER}/$(awk \
# -F '--install.modules-folder' '{print $2}' ${PACKAGE_JSON_FOLDER}/.npmrc \
# | awk '{print $1}' \
# | tr -d $'\r' \
# | tr -d '"' \
# | sed -e "s/^\.\///g")

# Environmental Variables
export DJANGO_PORT=${DJANGO_PORT:-8000}

Expand All @@ -25,7 +18,7 @@ wait_for_db() {
echo "Testing if database server is up..."
while [[ ! ${return_code} == 0 ]]
do
psql --host=${PGHOST} --port=${PGPORT} --user=${PGUSERNAME} --dbname=postgres -c "select 1" >&/dev/null
psql --host=${PGHOST} --port=${PGPORT} --user=${PGUSER} --dbname=postgres -c "select 1" >&/dev/null
return_code=$?
sleep 1
done
Expand All @@ -34,7 +27,7 @@ wait_for_db() {
echo "Testing if Elasticsearch is up..."
while [[ ! ${return_code} == 0 ]]
do
curl -s "http://${ESHOST}:${ESPORT}/_cluster/health?wait_for_status=green&timeout=60s" >&/dev/null
curl -s "http://${ARCHES_ESHOST}:${ARCHES_ESPORT}/_cluster/health?wait_for_status=green&timeout=60s" >&/dev/nullB
return_code=$?
sleep 1
done
Expand All @@ -43,7 +36,7 @@ wait_for_db() {

db_exists() {
echo "Checking if database "${PGDBNAME}" exists..."
count=`psql --host=${PGHOST} --port=${PGPORT} --user=${PGUSERNAME} --dbname=postgres -Atc "SELECT COUNT(*) FROM pg_catalog.pg_database WHERE datname='${PGDBNAME}'"`
count=`psql --host=${PGHOST} --port=${PGPORT} --user=${PGUSER} --dbname=postgres -Atc "SELECT COUNT(*) FROM pg_catalog.pg_database WHERE datname='${PGDBNAME}'"`

# Check if returned value is a number and not some error message
re='^[0-9]+$'
Expand Down Expand Up @@ -94,54 +87,35 @@ init_arches() {
fi
}

# npm
# install_npm_components() {
# if [[ ! -d ${npm_MODULES_FOLDER} ]] || [[ ! "$(ls ${npm_MODULES_FOLDER})" ]]; then
# echo "npm modules do not exist, installing..."
# cd ${PACKAGE_JSON_FOLDER}
# npm install
# fi
# }

#### Misc
copy_settings_local() {
# The settings_local.py in ${ARCHES_ROOT}/arches/ gets ignored if running manage.py from a custom Arches project instead of Arches core app
echo "Copying ${APP_FOLDER}/docker/settings_docker.py to ${APP_FOLDER}/${ARCHES_PROJECT}/settings_docker.py..."
cp ${APP_FOLDER}/docker/settings_docker.py ${APP_FOLDER}/${ARCHES_PROJECT}/settings_docker.py

# Copy settings_local if it does not exist
cp -n ${APP_FOLDER}/docker/settings_local.py ${APP_FOLDER}/${ARCHES_PROJECT}/settings_local.py
}

#### Run commands

start_celery_supervisor() {
cd ${APP_FOLDER}
supervisord -c docker/arches-lingo-supervisor.conf
supervisord -c docker/arches-supervisor.conf
}

run_migrations() {
echo ""
echo "----- RUNNING DATABASE MIGRATIONS -----"
echo ""
cd ${APP_FOLDER}
../ENV/bin/python manage.py migrate
python3 manage.py migrate
}

run_setup_db() {
echo ""
echo "----- RUNNING SETUP_DB -----"
echo ""
cd ${APP_FOLDER}
../ENV/bin/python manage.py setup_db --force
python3 manage.py setup_db --force
}

run_load_package() {
echo ""
echo "----- *** LOADING PACKAGE: ${ARCHES_PROJECT} *** -----"
echo ""
cd ${APP_FOLDER}
../ENV/bin/python manage.py packages -o load_package -s arches_lingo/pkg -db -dev -y
python3 manage.py packages -o load_package -a arches_lingo -db -dev -y
}

# "exec" means that it will finish building???
Expand All @@ -150,25 +124,54 @@ run_django_server() {
echo "----- *** RUNNING DJANGO DEVELOPMENT SERVER *** -----"
echo ""
cd ${APP_FOLDER}
echo "Running Django"
exec /bin/bash -c "source ${WEB_ROOT}/ENV/bin/activate && gunicorn arches_lingo.wsgi"
}

run_dev_server() {
echo ""
echo "----- *** RUNNING DJANGO DEVELOPMENT SERVER *** -----"
echo ""
cd ${APP_FOLDER}
echo "Running Django"
exec /bin/bash -c "source ../ENV/bin/activate && pip3 install debugpy -t /tmp && python -Wdefault /tmp/debugpy --listen 0.0.0.0:5678 manage.py runserver 0.0.0.0:${DJANGO_PORT}"
}

#### Main commands
run_arches() {
init_arches
run_django_server
# "exec" means that it will finish building???
run_gunicorn() {
echo ""
echo "----- *** RUNNING DJANGO PRODUCTION SERVER *** -----"
echo ""
cd ${APP_ROOT}
echo "Running Django"
exec /bin/bash -c "source ../ENV/bin/activate && (/etc/init.d/nginx start&) && gunicorn arches_lingo.wsgi"
}

run_webpack() {

reset_database() {
echo ""
echo "----- *** RUNNING WEBPACK DEVELOPMENT SERVER *** -----"
echo "----- RESETTING DATABASE -----"
echo ""
cd ${APP_FOLDER}
# echo "Running Webpack"
eval `ssh-agent -s` && cat /run/secrets/ssh_passphrase | SSH_ASKPASS=/bin/cat setsid -w ssh-add 2>> /dev/null
exec /bin/bash -c "source ../ENV/bin/activate && cd /web_root/arches-lingo && npm i && wait-for-it arches-lingo:80 -t 1200 && npm start"
cd ${APP_ROOT}
pwd && ../ENV/bin/python --version
(test $(echo "SELECT FROM pg_database WHERE datname = 'template_postgis'" | ../ENV/bin/python manage.py dbshell | grep -c "1 row") = 1 || \
(echo "CREATE DATABASE template_postgis" | ../ENV/bin/python manage.py dbshell --database postgres && \
echo "CREATE EXTENSION postgis" | ../ENV/bin/python manage.py dbshell --database postgres))
../ENV/bin/python manage.py packages -o load_package -a arches_lingo -db -dev -y
../ENV/bin/python manage.py packages -o import_business_data -s tests/fixtures/data/aat_entries_scheme_examples.json -ow overwrite
../ENV/bin/python manage.py packages -o import_business_data -s tests/fixtures/data/aat_entries_concept_examples.json -ow overwrite
../ENV/bin/python manage.py es reindex_database -mp
}

activate_virtualenv() {
. ${WEB_ROOT}/ENV/bin/activate
}

#### Main commands
run_arches() {
run_django_server
}

### Starting point ###

# Use -gt 1 to consume two arguments per pass in the loop
Expand All @@ -193,29 +196,23 @@ do
case ${key} in
run_arches)
start_celery_supervisor
copy_settings_local
wait_for_db
run_arches
;;
setup_arches)
start_celery_supervisor
copy_settings_local
wait_for_db
setup_arches
;;
run_tests)
copy_settings_local
wait_for_db
run_tests
;;
run_migrations)
copy_settings_local
wait_for_db
run_migrations
;;
install_npm_components)
install_npm_components
;;

help|-h)
display_help
;;
Expand Down
2 changes: 1 addition & 1 deletion docker/production/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ WORKDIR ${WEB_ROOT}
# From here, run commands from ARCHES_ROOT
RUN source ${WEB_ROOT}/ENV/bin/activate

COPY docker/production/entrypoint.sh ${WEB_ROOT}/entrypoint.sh
COPY docker/entrypoint.sh ${WEB_ROOT}/entrypoint.sh
RUN chmod -R 700 ${WEB_ROOT}/entrypoint.sh &&\
dos2unix ${WEB_ROOT}/entrypoint.sh

Expand Down
Loading

0 comments on commit 2fe205d

Please sign in to comment.