Skip to content

Commit

Permalink
Merge pull request #38 from ixcat/alyx-docker-202101
Browse files Browse the repository at this point in the history
ingest-entrypoint.sh: adjust mkdb/rotatedb for 2 database usage
  • Loading branch information
shenshan authored Jan 23, 2021
2 parents be1c456 + 74d44d5 commit 579ee26
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 26 deletions.
6 changes: 5 additions & 1 deletion Dockerfile.ingest
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ RUN cd /src \
&& git clone https://github.com/cortex-lab/alyx.git \
&& cd alyx && pip install -r requirements.txt

# alyx runtime env
ENV PYTHONPATH=/src/alyx/alyx
ENV DJANGO_SETTINGS_MODULE=alyx.settings

EXPOSE 8000/tcp
COPY ingest-entrypoint.sh /
ENTRYPOINT ["/ingest-entrypoint.sh" ]
CMD [ "run" ]
CMD [ "www" ]

1 change: 1 addition & 0 deletions docker-compose-ingest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ services:
- "8888:8888"
networks:
- ibl
command: [ "dev" ]

networks:
ibl:
Expand Down
128 changes: 103 additions & 25 deletions ingest-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# -------

dbdump="/tmp/dump.sql.gz"
dbcreated="/src/alyx/alyx/db_created"
dbloaded="/src/alyx/alyx/db_loaded"
sucreated="/src/alyx/alyx/superuser_created"

Expand Down Expand Up @@ -37,36 +38,34 @@ fetchdump() {
# ----

mkdb() {
if [ -f "${dbloaded}" ]; then
if [ -f "${dbcreated}" ]; then
echo '# => using existing database';
else
echo '#==> creating database';
echo '# ==> creating database';

[ ! -f "${dbdump}" ] \
&& err_exit ".. no database dump in $dbdump";

createdb alyx_old;
createdb alyx;

touch ${dbcreated};
fi
}

# loaddb
# ------

loaddb() {
echo "# => loading database ${dbdump}"
gzip -dc ${dbdump} |psql -d alyx;
touch ${dbloaded};
}

# rotatedb
# --------

rotatedb() {
echo "# => rotating databases";
echo "# ==> NOT IMPLEMENTED";
if [ -f "${dbloaded}" ]; then
echo '# => database loaded - skipping load.';
else
echo "# => loading database ${dbdump}"
gzip -dc ${dbdump} |psql -d alyx;
touch ${dbloaded};
fi
}


# configure alyx/django
# ---------------------

Expand All @@ -76,15 +75,47 @@ alyxcfg() {
if [ ! -f "$sucreated" ]; then

echo '# ==> configuring settings_secret.py'


# custom settings_secret for multiple DBs
# see also :alyx/alyx/alyx/settings_secret_template.py

cnf="/src/alyx/alyx/alyx/settings_secret.py";
sed \
-e "s/%SECRET_KEY%/0xdeadbeef/" \
-e "s/%DBNAME%/alyx/" \
-e "s/%DBUSER%/$PGUSER/" \
-e "s/%DBPASSWORD%/$PGPASSWORD/" \
-e "s/127.0.0.1/$PGHOST/" \
< /src/alyx/alyx/alyx/settings_secret_template.py \
> /src/alyx/alyx/alyx/settings_secret.py
> $cnf <<-EOF
SECRET_KEY = '%SECRET_KEY%'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': '%DBNAME%',
'USER': '%DBUSER%',
'PASSWORD': '%DBPASSWORD%',
'HOST': '127.0.0.1',
'PORT': '5432',
},
'old': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': '%DBNAME%_old',
'USER': '%DBUSER%',
'PASSWORD': '%DBPASSWORD%',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
EMAIL_HOST = 'mail.superserver.net'
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = 'UnbreakablePassword'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EOF

echo '# ==> creating alyx superuser'

Expand Down Expand Up @@ -113,7 +144,7 @@ alyxcfg() {
admin.save()
exit()
EOF

touch ${sucreated};
fi

Expand All @@ -128,7 +159,6 @@ alyxprep() {
/src/alyx/alyx/manage.py migrate;
}


# alyxstart
# ---------

Expand All @@ -137,28 +167,76 @@ alyxstart() {
/src/alyx/alyx/manage.py runserver --insecure 0.0.0.0:8888;
}

# run
# ---
# create/load/etc full-command
# renamedb
# --------

renamedb() {
echo "# => renaming databases:";

echo "# ==> ... dropping alyx_old";
dropdb alyx_old || err_exit "couldn't drop alyx_old";

echo "# ==> ... renaming alyx to alyx_old";
psql -c 'alter database alyx rename to alyx_old;' \
> /dev/null \
|| err_exit "couldn't rename alyx to alyx_old";

echo "# ==> ... creating new alyx";
createdb alyx || err_exit "couldn't rename alyx to alyx_old";

rm -f ${dbloaded};
rm -f ${sucreated};

echo "# => ok.";
}


run() {
# init
# ----
# perform all initialization steps

init() {
fetchdump;
mkdb;
loaddb;
alyxcfg;
alyxprep;
}


# www
# ---
# initialize environment and run alyx web

www() {
init;
alyxstart;
}

# dev
# ---
# initialize environment and wait indefinitely

dev() {
init;
exec tail -f /dev/null;
}

# _start:

case "$1" in
"fetchdump") fetchdump;;
"createdb") createdb;;
"mkdb") mkdb;;
"loaddb") loaddb;;
"alyxcfg") alyxcfg;;
"alyxprep") alyxprep;;
"alyxstart") alyxstart;;
"renamedb") renamedb;;
"www") www;;
"dev") dev;;
"sh") exec /bin/sh -c "$*";;
*) run;;
"help") \
echo "usage: `basename $0` [fetchdump|mkdb|loaddb|alyxcfg|alyxprep|alyxstart|renamedb|www|dev|sh]";;
*) ;; # ... sourceable
esac

0 comments on commit 579ee26

Please sign in to comment.