From 089f3cd8653479275787c60ade121cf07e15e474 Mon Sep 17 00:00:00 2001 From: Sebastian Urchs Date: Thu, 16 May 2024 12:05:38 -0400 Subject: [PATCH] [FIX] Enable graph setup to run on stack restart (#57) --- docker-compose.yml | 8 +++--- docs/neurobagel_environment_variables.tsv | 2 -- scripts/setup.sh | 33 ++++++++++++++++------- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 5ea4f1d..7071220 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -32,7 +32,7 @@ services: - "local_node_query" - "full_stack" volumes: - - "${NB_GRAPH_ROOT_HOST:-~/graphdb-home}:${NB_GRAPH_ROOT_CONT:-/opt/graphdb/home}" + - "graphdb_home:/opt/graphdb/home" - "./scripts:/usr/src/neurobagel/scripts" - ".env:/usr/src/neurobagel/.env" - "./vocab:/usr/src/neurobagel/vocab" @@ -40,7 +40,6 @@ services: ports: - "${NB_GRAPH_PORT_HOST:-7200}:${NB_GRAPH_PORT:-7200}" environment: - NB_GRAPH_ROOT_CONT: ${NB_GRAPH_ROOT_CONT:-/opt/graphdb/home} NB_GRAPH_USERNAME: ${NB_GRAPH_USERNAME} NB_GRAPH_ADDRESS: ${NB_GRAPH_ADDRESS:-graph} NB_GRAPH_PORT: ${NB_GRAPH_PORT:-7200} @@ -89,4 +88,7 @@ secrets: db_admin_password: environment: "NB_GRAPH_ADMIN_PASSWORD" db_user_password: - environment: "NB_GRAPH_PASSWORD" \ No newline at end of file + environment: "NB_GRAPH_PASSWORD" + +volumes: + graphdb_home: \ No newline at end of file diff --git a/docs/neurobagel_environment_variables.tsv b/docs/neurobagel_environment_variables.tsv index 201db03..3f957c6 100644 --- a/docs/neurobagel_environment_variables.tsv +++ b/docs/neurobagel_environment_variables.tsv @@ -15,8 +15,6 @@ Environment variable Set manually in .env? Description Default value if not set `NB_FAPI_PORT` No Port number on which to run the Neurobagel federation API _in the API container_ `8000` Docker `NB_GRAPH_IMG` No Graph server Docker image `ontotext/graphdb:10.3.1` Docker `NB_GRAPH_ADDRESS` No IP address for the graph database (or container name, if graph is hosted locally) `206.12.99.17 (graph)` ** Docker, Python -`NB_GRAPH_ROOT_HOST` No Path to directory on the _host machine_ to store graph database files and data (directory does not have to exist beforehand). `~/graphdb-home` Docker -`NB_GRAPH_ROOT_CONT` No Path to directory for graph databases in the _graph server container_ `/opt/graphdb/home` * Docker `NB_GRAPH_PORT_HOST` No Port number on the _host machine_ to map the graph server container port to `7200` Docker `NB_GRAPH_PORT` No Port number used by the _graph server container_ `7200` * Docker, Python `NB_QUERY_TAG` No Docker image tag for the query tool `latest` Docker diff --git a/scripts/setup.sh b/scripts/setup.sh index 47d66cf..18cca8f 100755 --- a/scripts/setup.sh +++ b/scripts/setup.sh @@ -1,31 +1,46 @@ #!/bin/bash -/opt/graphdb/dist/bin/graphdb -Dgraphdb.home=${NB_GRAPH_ROOT_CONT} & +/opt/graphdb/dist/bin/graphdb -Dgraphdb.home=/opt/graphdb/home & GRAPHDB_PID=$! +# TODO revisit/test this also once we document how users can change (in addition to the data files being uploaded) the variables to set up a non-tester database after a first-time deployment +export NB_GRAPH_ADMIN_PASSWORD=$(cat /run/secrets/db_admin_password) +export NB_GRAPH_PASSWORD=$(cat /run/secrets/db_user_password) + # Waiting for GraphDB to start -while ! curl --silent "localhost:${NB_GRAPH_PORT}/rest/repositories" | grep '\[\]'; do +while ! curl --silent "localhost:${NB_GRAPH_PORT}/rest/repositories" -u "${NB_GRAPH_USERNAME}:${NB_GRAPH_PASSWORD}" | grep '\['; do : done +# We need to figure out if this is the first time the setup has been run +repo_response=$(curl --silent "localhost:${NB_GRAPH_PORT}/rest/repositories" -u "${NB_GRAPH_USERNAME}:${NB_GRAPH_PASSWORD}") +if [ "${repo_response}" = "[]" ]; then + export FIRST_TIME_SETUP="on" +else + export FIRST_TIME_SETUP="off" +fi + +echo "First time setup: ${FIRST_TIME_SETUP}" + # TODO: Do we also want to use this elsewhere in the script or stick to ./? SCRIPT_DIR=$(dirname "$0") mkdir -p ${SCRIPT_DIR}/logs -export NB_GRAPH_ADMIN_PASSWORD=$(cat /run/secrets/db_admin_password) -export NB_GRAPH_PASSWORD=$(cat /run/secrets/db_user_password) - # Logic for main setup main() { echo "Setting up a Neurobagel graph backend..." echo -e "(The GraphDB server is being accessed inside the GraphDB container at http://localhost:${NB_GRAPH_PORT}.)\n" - echo "Setting up GraphDB server..." - ./graphdb_setup.sh --env-file-path /usr/src/neurobagel/.env "${NB_GRAPH_ADMIN_PASSWORD}" - echo "Finished server setup." + if [ "${FIRST_TIME_SETUP}" = "on" ]; then + echo "Setting up GraphDB server..." + ./graphdb_setup.sh --env-file-path /usr/src/neurobagel/.env "${NB_GRAPH_ADMIN_PASSWORD}" + echo "Finished server setup."export FIRST_TIME_SETUP="on" + else + echo "GraphDB server already set up, skipping setup." + fi echo "Adding datasets to the database..." - ./add_data_to_graph.sh /data localhost:${NB_GRAPH_PORT} ${NB_GRAPH_DB} "${NB_GRAPH_USERNAME}" "${NB_GRAPH_PASSWORD}" + ./add_data_to_graph.sh /data localhost:${NB_GRAPH_PORT} ${NB_GRAPH_DB} "${NB_GRAPH_USERNAME}" "${NB_GRAPH_PASSWORD}" --clear-data echo "Finished adding datasets to databases." echo "Adding Neurobagel vocabulary to the database"