Skip to content

Commit

Permalink
Make docker compose more seamless (#7693)
Browse files Browse the repository at this point in the history
- Add docker compose files to https://github.com/cBioPortal/cbioportal-docker-compose, initial work by @nr23730 & the Hyve - thanks!  I figured it might be cleaner to keep the docker compose out of this repo, since it is using containers from various other repos (e.g. session-service)
- Update docs to recommend to use that docker compose repo to set up a local instance
- Update entrypoint for web-and-data image to include a check for database liveness and migrate the db automatically
  • Loading branch information
inodb authored Jul 31, 2020
1 parent b01c5b3 commit 8955836
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 452 deletions.
4 changes: 3 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
.DS_Store
.github
target
docker
docs
*/target/*
docker
# don't ignore entrypoint scripts
!docker/web-and-data/docker-entrypoint.sh
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ See [https://docs.cbioportal.org](https://docs.cbioportal.org)
See [LICENSE](./LICENSE)

## 💻 Run Backend
cBioPortal consists of several components, please read the [Architecture docs](https://docs.cbioportal.org/2.1-deployment/architecture-overview) to figure out what repo would be relevant to edit. If you e.g. only want to make frontend changes, one can directly edit [the frontend repo](https://github.com/cbioportal/cbioportal-frontend) instead. Read the instructions in that repo for more info on how to do frontend development. This repo only contains the backend part. Before editing the backend, it's good to read the [backend code organization](docs/Backend-Code-Organization.md). For development of the backend repo one should first set up a database. Please follow the [Docker deployment documentation](https://docs.cbioportal.org/2.1.1-deploy-with-docker-recommended/docker) to do so. In step 6 instead of using the `latest` image, you can create your own image and use that:
cBioPortal consists of several components, please read the [Architecture docs](https://docs.cbioportal.org/2.1-deployment/architecture-overview) to figure out what repo would be relevant to edit. If you e.g. only want to make frontend changes, one can directly edit [the frontend repo](https://github.com/cbioportal/cbioportal-frontend) instead. Read the instructions in that repo for more info on how to do frontend development. This repo only contains the backend part. Before editing the backend, it's good to read the [backend code organization](docs/Backend-Code-Organization.md). For development of the backend repo one should first set up a database. Please follow the [Docker deployment documentation](https://docs.cbioportal.org/2.1.1-deploy-with-docker-recommended/docker) to do so. Change the [docker-compose](https://github.com/cBioPortal/cbioportal-docker-compose/blob/5da068f0eb9b4f42db52ab5e91321b26a1826d7a/docker-compose.yml#L6) file to use your image instead:

```
docker build -t cbioportal/cbioportal:my-dev-cbioportal-image -f docker/web-and-data/Dockerfile .
Expand Down
4 changes: 4 additions & 0 deletions docker/web-and-data/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,7 @@ ENV PORTAL_HOME=/cbioportal

RUN mkdir -p $PORTAL_WEB_HOME
RUN unzip /app.war -d $PORTAL_WEB_HOME

# add entrypoint
COPY --from=build /cbioportal/docker/web-and-data/docker-entrypoint.sh /usr/local/bin/
ENTRYPOINT ["docker-entrypoint.sh"]
86 changes: 86 additions & 0 deletions docker/web-and-data/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/usr/bin/env bash
set -eo pipefail
shopt -s nullglob

BAKED_IN_WAR_CONFIG_FILE=/cbioportal-webapp/WEB-INF/classes/portal.properties
CUSTOM_PROPERTIES_FILE=/cbioportal/portal.properties

# check to see if this file is being run or sourced from another script
_is_sourced() {
# https://unix.stackexchange.com/a/215279
[ "${#FUNCNAME[@]}" -ge 2 ] \
&& [ "${FUNCNAME[0]}" = '_is_sourced' ] \
&& [ "${FUNCNAME[1]}" = 'source' ]
}

parse_db_params_from_command_line() {
echo $@ | sed 's/-D/\n-D/g' | grep -- '-Ddb' | sed 's/-D//g' | grep db.
}

parse_db_params_from_config_and_command_line() {
if [[ -f $CUSTOM_PROPERTIES_FILE ]]; then
PROPERTIES_FILE=$CUSTOM_PROPERTIES_FILE
else
PROPERTIES_FILE=$BAKED_IN_WAR_CONFIG_FILE
fi

for param in db.host db.user db.portal_db_name db.password; do
if $(parse_db_params_from_command_line $@ | grep -q $param); then
parse_db_params_from_command_line $@ | grep "^$param"
else
grep -v '^#' $PROPERTIES_FILE | grep "^$param"
fi
done
}

check_db_connection() {
eval "$(parse_db_params_from_config_and_command_line $@ | sed 's/^db\./db_/g')"
POTENTIAL_DB_PARAMS=$@

while ! mysqladmin ping -s -h$(echo ${db_host} | cut -d: -f1) -u${db_user} -p${db_password};
do
sleep 5s;
if [ -n "$SHOW_DEBUG_INFO" ] && [ "$SHOW_DEBUG_INFO" != "false" ]; then
echo mysqladmin ping -s -h$(echo ${db_host} | cut -d: -f1) -u${db_user} -p${db_password}
fi
echo "Database not available yet (first time can take a few minutes to load seed database)... Attempting reconnect..."
done
echo "Database connection success"
}

migrate_db() {
echo "Migrating database if necessary..."
POTENTIAL_DB_PARAMS=$@

if [[ -f $CUSTOM_PROPERTIES_FILE ]]; then
python3 /cbioportal/core/src/main/scripts/migrate_db.py -y -p $CUSTOM_PROPERTIES_FILE -s /cbioportal/db-scripts/src/main/resources/migration.sql
else
python3 /cbioportal/core/src/main/scripts/migrate_db.py -y -p <(parse_db_params_from_config_and_command_line $POTENTIAL_DB_PARAMS) -s /cbioportal/db-scripts/src/main/resources/migration.sql
fi
}

_main() {
# when running the webapp, check db and do migration first
# check if command is something like "java -jar webapp-runner.jar"
if [[ "$@" == *java* ]] && [[ "$@" == *-jar* ]] && [[ "$@" == *webapp-runner.jar* ]]; then
# Parse database config. Use command line parameters (e.g. -Ddb.host) if
# available, otherwise use portal.properties
if [ -n "$SHOW_DEBUG_INFO" ] && [ "$SHOW_DEBUG_INFO" != "false" ]; then
echo "Using database config:"
parse_db_params_from_config_and_command_line $@
fi

check_db_connection $@
migrate_db $@

if [ -n "$SHOW_DEBUG_INFO" ] && [ "$SHOW_DEBUG_INFO" != "false" ]; then
echo Running: "$@"
fi
fi
exec "$@"
}

# If we are sourced from elsewhere, don't perform any further actions
if ! _is_sourced; then
_main "$@"
fi
171 changes: 0 additions & 171 deletions docs/Deploy-Using-Docker.md

This file was deleted.

Loading

0 comments on commit 8955836

Please sign in to comment.