From fee259081fe08429a6a45fa8d919bbd8efcf447e Mon Sep 17 00:00:00 2001 From: imadbourouche Date: Fri, 19 Jan 2024 11:38:09 +0100 Subject: [PATCH 01/38] Update ontoportal docker to use docker compose of the ui and api using ontoportal script --- .gitignore | 3 + Dockerfile | 23 +++++++ config/deploy.yml | 39 +++++++++++ ontoportal | 137 +++++++++++++++++++++++++++++++++++++ run_api.sh | 169 ++++++++++++++++++++++++++++++++++++++++++++++ run_ui.sh | 84 +++++++++++++++++++++++ 6 files changed, 455 insertions(+) create mode 100644 Dockerfile create mode 100644 config/deploy.yml create mode 100755 ontoportal create mode 100755 run_api.sh create mode 100755 run_ui.sh diff --git a/.gitignore b/.gitignore index 613c8a6..0571a45 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ .env dip.override.yml + + +.kamal/hooks/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..4778993 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,23 @@ +FROM alpine:latest + +RUN apk --update --no-cache add \ + bash \ + curl \ + docker \ + docker-compose \ + git + +COPY ontoportal /app/ontoportal +COPY run_api.sh /app/run_api.sh +COPY run_ui.sh /app/run_ui.sh + +RUN chmod +x /app/ontoportal +RUN chmod +x /app/run_api.sh +RUN chmod +x /app/run_ui.sh + + +WORKDIR /app + +VOLUME /var/run/docker.sock + +CMD ["/app/ontoportal", "start"] diff --git a/config/deploy.yml b/config/deploy.yml new file mode 100644 index 0000000..04d55e0 --- /dev/null +++ b/config/deploy.yml @@ -0,0 +1,39 @@ +# Name of your application. Used to uniquely configure containers. +service: ontoportal_docker + +# Name of the container image. +image: imadpyth/ontoportal_docker + +# Deploy to these servers. +servers: + - 127.0.0.1 + +run_directory: /root/app + +# Credentials for your image host. +registry: + # Specify the registry server, if you're not using Docker Hub + # server: registry.digitalocean.com / ghcr.io / ... + username: imadpyth + + # Always use an access token rather than real password when possible. + password: + - KAMAL_REGISTRY_PASSWORD + + +# Use a different ssh user than root +ssh: + user: root + #log_level: debug + +volumes: + - /var/run/docker.sock:/var/run/docker.sock + +traefik: + host_port: 3000 + +healthcheck: + path: / + port: 3000 + max_attempts: 50 + interval: 20s \ No newline at end of file diff --git a/ontoportal b/ontoportal new file mode 100755 index 0000000..daeb9be --- /dev/null +++ b/ontoportal @@ -0,0 +1,137 @@ +#!/usr/bin/env bash + +run_api(){ + get_files_from_github(){ + echo "[+] Getting compose file from ontologies_api" + curl -L https://raw.githubusercontent.com/ontoportal-lirmm/ontologies_api/development/docker-compose.yml -o docker-compose_api_development.yml + curl -L https://raw.githubusercontent.com/ontoportal-lirmm/ontologies_api/development/.env.sample -o .env_api + } + + update_compose_file(){ + echo "[+] Changing some lines" + file_path="./docker-compose_api_development.yml" + sed -i 's#- \.:\/srv\/ontoportal\/ontologies_api#- app_api:/srv/ontoportal/ontologies_api#' "$file_path" + sed -i 's/\.env/.env_api/' "$file_path" + sed -i 's#./test/solr/configsets:/configsets:ro#./ontologies_linked_data/test/solr/configsets/:/configsets:ro#' "$file_path" + sed -i '/^volumes:/a \ \ app_api:' "$file_path" + #sed -i 's/image: agroportal\/ontologies_api:development/image: agroportal\/ontologies_api:stage/' "$file_path" + } + + update_env_api(){ + file_path=".env_api" + echo >> "$file_path" + echo "REDIS_GOO_CACHE_HOST=redis-ut" >> "$file_path" + echo "REDIS_HTTP_CACHE_HOST=redis-ut" >> "$file_path" + echo "REDIS_PERSISTENT_HOST=redis-ut" >> "$file_path" + } + + run_the_api_service(){ + echo "[+] Running api script" + ./run_api.sh dev --api-url http://api-service:9393 + } + + get_files_from_github + update_compose_file + update_env_api + run_the_api_service + +} + +run_ui(){ + get_files_from_github(){ + echo "[+] Getting compose file from bioportal_web_ui" + curl -L https://raw.githubusercontent.com/ontoportal-lirmm/bioportal_web_ui/development/docker-compose.yml -o docker-compose_ui_development.yml + curl -L https://raw.githubusercontent.com/ontoportal-lirmm/bioportal_web_ui/development/.env.sample -o .env_ui + } + + update_compose_file(){ + echo "[+] Changing some lines" + file_path="./docker-compose_ui_development.yml" + sed -i 's/- ".env"/- .env_ui/' "$file_path" + sed -i 's/- .:\/app/- app_web:\/app/' "$file_path" + sed -i 's/retries: 3/retries: 100/' "$file_path" + sed -i '/^volumes:/a \ \ app_web:' "$file_path" + #sed -i 's/image: agroportal\/ontoportal_web_ui:development/image: agroportal\/ontoportal_web_ui:stage/' "$file_path" + } + + run_the_ui_script(){ + echo "[+] Running ui script" + ./run_ui.sh dev --api-url http://api-service:9393 --api-key 72c72cba-ad45-4785-b94e-483fa55cdddb + } + + get_files_from_github + update_compose_file + run_the_ui_script + +} + + +stop_services(){ + docker container stop api-service + docker container stop ui-service +} + + +show_help(){ + echo "HELP: ontoportal [clean/start/stop]" +} + +clean_containers(){ + echo "[+] Cleaning the containers" + docker stop $(docker ps -aq) 2>/dev/null + docker container rm -f $(docker ps -aq) 2>/dev/null ; docker volume rm $(docker volume ls -q) ; docker buildx prune -f ; docker network prune -f + rm .env* + rm docker-compose* + rm ontologies_linked_data -rf + echo "[+] Finishing cleaning the containers" +} + + +show_running_containers(){ + docker container ls -a +} + +show_logs_api(){ + docker logs -f api-service +} + + +show_logs_ui(){ + docker logs -f ui-service +} + + +case "$1" in + "start") + #clean_containers + run_api + run_ui + ;; + "logs") + case "$2" in + "api") + show_logs_api + ;; + "ui") + show_logs_ui + ;; + *) + show_running_containers + ;; + esac + ;; + "stop") + stop_services + ;; + "clean") + clean_containers + ;; + "help") + show_help + ;; + *) + show_help + exit 1 + ;; +esac + diff --git a/run_api.sh b/run_api.sh new file mode 100755 index 0000000..ef191d9 --- /dev/null +++ b/run_api.sh @@ -0,0 +1,169 @@ +#!/usr/bin/env bash + +update_env_file() { + local api_url="$1" + local old_path="$2" + local goo_path="$3" + local sparql_client_path="$4" + file_content=$(<.env_api) + while IFS= read -r line; do + if [[ "$line" == "API_URL="* && -n "$api_url" ]]; then + echo "API_URL=$api_url" + elif [[ "$line" == "ONTOLOGIES_LINKED_DATA_PATH="* ]]; then + echo "ONTOLOGIES_LINKED_DATA_PATH=$old_path" + elif [[ "$line" == "GOO_PATH="* ]]; then + echo "GOO_PATH=$goo_path" + elif [[ "$line" == "SPARQL_CLIENT_PATH="* ]]; then + echo "SPARQL_CLIENT_PATH=$sparql_client_path" + else + echo "$line" + fi + done <<< "$file_content" > .env_api +} + + +build_docker_run_cmd() { + local custom_command="$1" + local old_path="$2" + local goo_path="$3" + local sparql_client_path="$4" + local bash_cmd="" + + for path_var in "old_path:ontologies_linked_data" "goo_path:goo" "sparql_client_path:sparql-client"; do + IFS=':' read -r path value <<< "$path_var" + + if [ -n "${!path}" ]; then + host_path="$(realpath "$(dirname "${!path}")")/$value" + echo "Run: bundle config local.$value ${!path}" + container_path="/srv/ontoportal/$value" + docker_run_cmd+=" -v $host_path:$container_path" + bash_cmd+="(git config --global --add safe.directory $container_path && bundle config local.$value $container_path) &&" + else + bash_cmd+=" (bundle config unset local.$value) &&" + fi + done + + bash_cmd+=" (bundle check || bundle install || bundle update) && $custom_command" + #docker_run_cmd+="docker compose run --rm -it --name api-service --service-ports api bash -c \"$bash_cmd\"" + docker_run_cmd+="docker compose -f docker-compose_api_development.yml run --rm -d --name api-service --service-ports api bash -c \"$bash_cmd\"" + + echo "----------------------------------" + ls + echo "----------------------------------" + echo + echo '[+] RUN: ' $docker_run_cmd + eval "$docker_run_cmd" +} + + + +run_command() { + local custom_command="$1" + local reset_cache=false + local api_url="" + local old_path="" + local goo_path="" + local sparql_client_path="" + + shift + while [[ "$#" -gt 0 ]]; do + case $1 in + --reset-cache) + reset_cache=true + shift + ;; + --api-url) + api_url="$2" + shift 2 + ;; + --old-path) + old_path="$2" + shift 2 + ;; + --goo-path) + goo_path="$2" + shift 2 + ;; + --sparql-client-path) + sparql_client_path="$2" + shift 2 + ;; + *) + echo "Unknown option: $1" + show_help + exit 1 + ;; + esac + done + + if [ "$reset_cache" = true ]; then + echo "Resetting cache. Running: docker compose down --volumes" + docker compose down --volumes + fi + + update_env_file "$api_url" "$old_path" "$goo_path" "$sparql_client_path" + + + source .env_api + api_url="$API_URL" + old_path="$ONTOLOGIES_LINKED_DATA_PATH" + goo_path="$GOO_PATH" + sparql_client_path="$SPARQL_CLIENT_PATH" + + + if [ -z "$api_url" ] ; then + echo "Error: Missing required arguments. Please provide both --api-url or update them in your .env_api" + exit 1 + fi + + build_docker_run_cmd "$custom_command" "$old_path" "$goo_path" "$sparql_client_path" +} + + +clone_ongologies_linked_data(){ + repo_dir="ontologies_linked_data" + + if [ -d "$repo_dir" ]; then + echo "[+] Directory $repo_dir already exists. Skipping cloning." + else + echo "[+] Cloning ontologies_linked_data repo..." + git clone --depth=1 https://github.com/ontoportal-lirmm/ontologies_linked_data.git + fi + + echo -n "[+] Generating Solr configsets: " + if [ -d "$repo_dir" ]; then + cd ontologies_linked_data || exit 1 + if ! ./test/solr/generate_ncbo_configsets.sh; then + echo "Error: Failed to generate Solr configsets." + exit 1 + else + echo "Success: Generating Solr configsets." + fi + cd .. + else + echo "[-] Error: directory ontologies_linked_data does not exists" + fi +} + + +# Function to handle the "dev" option +dev() { + echo "Starting OntoPortal API development server..." + clone_ongologies_linked_data + + local custom_command="bundle exec rackup --host 0.0.0.0 --env=development --port 9393" + run_command "$custom_command" "$@" +} + +case "$1" in + "dev") + dev "${@:2}" + ;; + "help") + show_help + ;; + *) + show_help + exit 1 + ;; +esac diff --git a/run_ui.sh b/run_ui.sh new file mode 100755 index 0000000..33c8a26 --- /dev/null +++ b/run_ui.sh @@ -0,0 +1,84 @@ +#!/usr/bin/env bash + +update_env_file() { + local api_url="$1" + local api_key="$2" + + file_content=$(<.env_ui) + while IFS= read -r line; do + if [[ "$line" == "API_URL="* ]]; then + echo "API_URL=$api_url" + elif [[ "$line" == "API_KEY="* ]]; then + echo "API_KEY=$api_key" + else + echo "$line" + fi + done <<< "$file_content" > .env_ui +} + +dev() { + echo "Starting Ontoportal Web UI development server..." + + local reset_cache=false + local api_url="" + local api_key="" + + while [[ "$#" -gt 0 ]]; do + case $1 in + --reset-cache) + reset_cache=true + shift + ;; + --api-url) + api_url="$2" + shift 2 + ;; + --api-key) + api_key="$2" + shift 2 + ;; + *) + echo "Unknown option: $1" + show_help + exit 1 + ;; + esac + done + + if [ -n "$api_url" ] && [ -n "$api_key" ]; then + update_env_file "$api_url" "$api_key" + else + source .env_ui + api_url="$API_URL" + api_key="$API_KEY" + fi + + if [ -z "$api_url" ] || [ -z "$api_key" ]; then + echo "Error: Missing required arguments. Please provide both --api-url and --api-key or update them in your .env" + exit 1 + fi + + # Check if --reset-cache is present and execute docker compose down --volumes + if [ "$reset_cache" = true ]; then + echo "Resetting cache. Running: docker compose down --volumes" + docker compose down --volumes + fi + + echo "Run: bundle exec rails s -b 0.0.0.0 -p 3000" + #docker compose run --rm -it --service-ports rails bash -c "(bundle check || bundle install) && bin/rails db:prepare && bundle exec rails s -b 0.0.0.0 -p 3000" + docker compose -f docker-compose_ui_development.yml run --rm -d --name ui-service --service-ports rails bash -c "cp /app/config/bioportal_config_env.rb.sample /app/config/bioportal_config_development.rb && cp /app/config/database.yml.sample /app/config/database.yml && (bundle check || bundle install) && bin/rails db:prepare && bundle exec rails s -b 0.0.0.0 -p 4000" + +} + +case "$1" in + "dev") + dev "${@:2}" + ;; + "help") + show_help + ;; + *) + show_help + exit 1 + ;; +esac From 5661fd543c679d959341684bd385e4bfa46ae1c4 Mon Sep 17 00:00:00 2001 From: imadbourouche Date: Wed, 24 Jan 2024 13:59:23 +0100 Subject: [PATCH 02/38] Make the script more configurable using .env file and simplify the run_api and run_ui scripts --- Dockerfile | 12 +- config/deploy.yml | 42 ++----- ontoportal | 277 +++++++++++++++++++++++++++++++--------------- run_api.sh | 169 ---------------------------- run_ui.sh | 84 -------------- 5 files changed, 201 insertions(+), 383 deletions(-) delete mode 100755 run_api.sh delete mode 100755 run_ui.sh diff --git a/Dockerfile b/Dockerfile index 4778993..9217a98 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,16 +8,8 @@ RUN apk --update --no-cache add \ git COPY ontoportal /app/ontoportal -COPY run_api.sh /app/run_api.sh -COPY run_ui.sh /app/run_ui.sh - +COPY .env /app/.env RUN chmod +x /app/ontoportal -RUN chmod +x /app/run_api.sh -RUN chmod +x /app/run_ui.sh - - WORKDIR /app - VOLUME /var/run/docker.sock - -CMD ["/app/ontoportal", "start"] +CMD ["/bin/sh", "-c", "/app/ontoportal start && tail -f /dev/null"] \ No newline at end of file diff --git a/config/deploy.yml b/config/deploy.yml index 04d55e0..075dbdf 100644 --- a/config/deploy.yml +++ b/config/deploy.yml @@ -1,39 +1,17 @@ -# Name of your application. Used to uniquely configure containers. service: ontoportal_docker - -# Name of the container image. -image: imadpyth/ontoportal_docker - -# Deploy to these servers. +image: ENV["IMAGE_NAME"] servers: - - 127.0.0.1 - -run_directory: /root/app - -# Credentials for your image host. + - ENV["SERVER_IP"] registry: - # Specify the registry server, if you're not using Docker Hub - # server: registry.digitalocean.com / ghcr.io / ... - username: imadpyth - - # Always use an access token rather than real password when possible. + username: + - ENV["DOCKER_REGISTRY_NAME"] password: - - KAMAL_REGISTRY_PASSWORD - - -# Use a different ssh user than root + - ENV["KAMAL_REGISTRY_PASSWORD"] ssh: - user: root - #log_level: debug - -volumes: - - /var/run/docker.sock:/var/run/docker.sock - + user: ENV["SSH_USER"] +run_directory: /root/app +volumes: /var/run/docker.sock:/var/run/docker.sock traefik: - host_port: 3000 - + host_port: 4000 healthcheck: - path: / - port: 3000 - max_attempts: 50 - interval: 20s \ No newline at end of file + cmd: /bin/true \ No newline at end of file diff --git a/ontoportal b/ontoportal index daeb9be..b0c904d 100755 --- a/ontoportal +++ b/ontoportal @@ -1,130 +1,232 @@ #!/usr/bin/env bash run_api(){ - get_files_from_github(){ - echo "[+] Getting compose file from ontologies_api" - curl -L https://raw.githubusercontent.com/ontoportal-lirmm/ontologies_api/development/docker-compose.yml -o docker-compose_api_development.yml - curl -L https://raw.githubusercontent.com/ontoportal-lirmm/ontologies_api/development/.env.sample -o .env_api + clone_ongologies_linked_data(){ + repo_dir="ontologies_linked_data" + + if [ -d "$repo_dir" ]; then + echo "[+] Directory $repo_dir already exists. Skipping cloning." + else + echo "[+] Cloning ontologies_linked_data repo..." + git clone -q --depth=1 https://github.com/ontoportal-lirmm/ontologies_linked_data.git + fi + + echo "[+] Generating Solr configsets ..." + if [ -d "$repo_dir" ]; then + cd ontologies_linked_data || exit 1 + if ! ./test/solr/generate_ncbo_configsets.sh; then + echo "[-] Error: Failed to generate Solr configsets." + exit 1 + else + echo "[+] Success: Generating Solr configsets." + fi + cd .. + else + echo "[-] Error: directory ontologies_linked_data does not exists" + fi } - update_compose_file(){ - echo "[+] Changing some lines" - file_path="./docker-compose_api_development.yml" + clone_ongologies_linked_data + source .env + api_url="$API_URL" + if [ -z "$api_url" ] ; then + echo "[-] Error: Missing required arguments. Please provide both --api-url or update them in your .env" + exit 1 + fi + bash_cmd=" (bundle check || bundle install || bundle update) && bundle exec rackup --host 0.0.0.0 --env=development --port 9393" + docker_run_cmd="docker compose -f docker-compose_api.yml run --rm -d --name api-service --service-ports api bash -c \"$bash_cmd\"" + echo "[+] Starting the API" + eval "$docker_run_cmd" +} + +run_ui(){ + source .env + api_url="$API_URL" + api_key="$API_KEY" + if [ -z "$api_url" ] || [ -z "$api_key" ]; then + echo "[-] Error: Missing required arguments. Please provide both --api-url and --api-key or update them in your .env" + exit 1 + fi + echo "[+] Starting the UI" + bash_cmd="cp /app/config/bioportal_config_env.rb.sample /app/config/bioportal_config_development.rb && cp /app/config/database.yml.sample /app/config/database.yml && (bundle check || bundle install) && bin/rails db:prepare && bundle exec rails s -b 0.0.0.0 -p 3000" + eval "docker compose -f docker-compose_ui.yml run --rm -d --name ui-service --service-ports rails bash -c \"$bash_cmd\"" + +} + +run_default_mode(){ + + run_api_default_mode(){ + echo "[+] Getting compose file for API" + curl -sS -L https://raw.githubusercontent.com/ontoportal-lirmm/ontologies_api/development/docker-compose.yml -o docker-compose_api.yml + echo >> .env + curl -sS -L https://raw.githubusercontent.com/ontoportal-lirmm/ontologies_api/development/.env.sample >> .env + + sed -i 's/^API_URL=http:\/\/localhost:9393$/API_URL=http:\/\/api-service:9393/' .env + + echo "[+] Changing lines in docker-compose_api.yml" + file_path="./docker-compose_api.yml" sed -i 's#- \.:\/srv\/ontoportal\/ontologies_api#- app_api:/srv/ontoportal/ontologies_api#' "$file_path" - sed -i 's/\.env/.env_api/' "$file_path" sed -i 's#./test/solr/configsets:/configsets:ro#./ontologies_linked_data/test/solr/configsets/:/configsets:ro#' "$file_path" sed -i '/^volumes:/a \ \ app_api:' "$file_path" - #sed -i 's/image: agroportal\/ontologies_api:development/image: agroportal\/ontologies_api:stage/' "$file_path" - } - update_env_api(){ - file_path=".env_api" + echo "[+] Adding some envirement variables for the API" + file_path=".env" echo >> "$file_path" echo "REDIS_GOO_CACHE_HOST=redis-ut" >> "$file_path" echo "REDIS_HTTP_CACHE_HOST=redis-ut" >> "$file_path" echo "REDIS_PERSISTENT_HOST=redis-ut" >> "$file_path" - } - - run_the_api_service(){ + echo "[+] Running api script" - ./run_api.sh dev --api-url http://api-service:9393 - } - - get_files_from_github - update_compose_file - update_env_api - run_the_api_service - -} - -run_ui(){ - get_files_from_github(){ - echo "[+] Getting compose file from bioportal_web_ui" - curl -L https://raw.githubusercontent.com/ontoportal-lirmm/bioportal_web_ui/development/docker-compose.yml -o docker-compose_ui_development.yml - curl -L https://raw.githubusercontent.com/ontoportal-lirmm/bioportal_web_ui/development/.env.sample -o .env_ui + + run_api + if [ $? -ne 0 ]; then + echo "[-] Error in run_api function. Exiting..." + exit 1 + fi + + echo "[+] The API is running successfully." } - update_compose_file(){ - echo "[+] Changing some lines" - file_path="./docker-compose_ui_development.yml" - sed -i 's/- ".env"/- .env_ui/' "$file_path" + run_ui_default_mode(){ + echo "[+] Getting compose file for UI" + curl -sS -L https://raw.githubusercontent.com/ontoportal-lirmm/bioportal_web_ui/development/docker-compose.yml -o docker-compose_ui.yml + echo >> .env + curl -sS -L https://raw.githubusercontent.com/ontoportal-lirmm/bioportal_web_ui/development/.env.sample >> .env + + sed -i 's/^API_URL=http:\/\/localhost:9393$/API_URL=http:\/\/api-service:9393/' .env + sed -i 's/^API_KEY=$/API_KEY=72c72cba-ad45-4785-b94e-483fa55cdddb/' .env + + echo "[+] Changing lines in docker-compose_ui.yml" + file_path="./docker-compose_ui.yml" sed -i 's/- .:\/app/- app_web:\/app/' "$file_path" sed -i 's/retries: 3/retries: 100/' "$file_path" sed -i '/^volumes:/a \ \ app_web:' "$file_path" - #sed -i 's/image: agroportal\/ontoportal_web_ui:development/image: agroportal\/ontoportal_web_ui:stage/' "$file_path" - } - - run_the_ui_script(){ + echo "[+] Running ui script" - ./run_ui.sh dev --api-url http://api-service:9393 --api-key 72c72cba-ad45-4785-b94e-483fa55cdddb - } + run_ui + if [ $? -ne 0 ]; then + echo "[-] Error in run_ui function. Exiting..." + exit 1 + fi - get_files_from_github - update_compose_file - run_the_ui_script + echo "[+] The UI is running successfully." + } + run_api_default_mode + run_ui_default_mode } - -stop_services(){ - docker container stop api-service - docker container stop ui-service +run_personlized_mode(){ + source .env + echo "[+] Getting compose file from: $ORGANIZATION_NAME$COMPOSE_API_FILE_PATH" + eval "curl -sS -L https://raw.githubusercontent.com/$ORGANIZATION_NAME$COMPOSE_API_FILE_PATH -o docker-compose_api.yml" + echo "[+] Changing some lines" + file_path="./docker-compose_api.yml" + sed -i 's#- \.:\/srv\/ontoportal\/ontologies_api#- app_api:/srv/ontoportal/ontologies_api#' "$file_path" + sed -i 's#./test/solr/configsets:/configsets:ro#./ontologies_linked_data/test/solr/configsets/:/configsets:ro#' "$file_path" + sed -i '/^volumes:/a \ \ app_api:' "$file_path" + echo "[+] Running api script" + run_api + + + echo "[+] Getting compose file from: $ORGANIZATION_NAME$COMPOSE_UI_FILE_PATH" + eval "curl -sS -L https://raw.githubusercontent.com/$ORGANIZATION_NAME$COMPOSE_UI_FILE_PATH -o docker-compose_ui.yml" + echo "[+] Changing some lines" + file_path="./docker-compose_ui.yml" + sed -i 's/- .:\/app/- app_web:\/app/' "$file_path" + sed -i 's/retries: 3/retries: 100/' "$file_path" + sed -i '/^volumes:/a \ \ app_web:' "$file_path" + echo "[+] Running ui script" + run_ui } - -show_help(){ - echo "HELP: ontoportal [clean/start/stop]" +clean(){ + echo "[+] Cleanning the server" + echo "[+] Removing ontologies_linked_data" + rm -rf ontologies_linked_data + echo "[+] Removing docker compose files" + rm -f docker-compose* + echo "[+] Removing All containers/volumes/networks" + docker container rm -f $(docker ps -aq) 2>/dev/null; docker volume rm $(docker volume ls -q) 2>/dev/null ; docker network prune -f } -clean_containers(){ - echo "[+] Cleaning the containers" - docker stop $(docker ps -aq) 2>/dev/null - docker container rm -f $(docker ps -aq) 2>/dev/null ; docker volume rm $(docker volume ls -q) ; docker buildx prune -f ; docker network prune -f - rm .env* - rm docker-compose* - rm ontologies_linked_data -rf - echo "[+] Finishing cleaning the containers" +start_function() { + if [ -f ".env" ]; then + echo "[+] Starting in personlized mode" + run_personlized_mode + else + echo "[+] Starting the script in default mode" + rm -f .env + clean + run_default_mode + fi } - -show_running_containers(){ - docker container ls -a +stop_function() { + echo "[+] Stoping services" + docker container stop api-service + docker container stop ui-service } -show_logs_api(){ - docker logs -f api-service +deploy(){ + if [ -f ".env" ]; then + echo "[+] Checking for env variables" + source .env + # List of environment variables to check + variables=("IMAGE_NAME" "SERVER_IP" "DOCKER_REGISTRY_NAME" "KAMAL_REGISTRY_PASSWORD" "SSH_USER") + + for variable in "${variables[@]}"; do + if [ -z "${!variable}" ]; then + echo "[-] Error: $variable is not set." + exit + fi + done + + echo "[+] Changing kamal deploy.yml file" + echo "" > config/deploy.yml + echo "service: ontoportal_docker" > config/deploy.yml + echo "image: ${IMAGE_NAME}" >> config/deploy.yml + echo -e "servers:\n - ${SERVER_IP}" >> config/deploy.yml + echo "run_directory: /root/app" >> config/deploy.yml + echo -e "registry:\n username:\n - DOCKER_REGISTRY_NAME\n password:\n - KAMAL_REGISTRY_PASSWORD" >> config/deploy.yml + echo -e "ssh:\n user: ${SSH_USER}" >> config/deploy.yml + echo "volumes: /var/run/docker.sock:/var/run/docker.sock" >> config/deploy.yml + echo -e "traefik:\n host_port: 4000" >> config/deploy.yml + echo -e "healthcheck:\n cmd: /bin/true" >> config/deploy.yml + echo "[+] Starting the deployment" + kamal setup -vv + else + echo "[-] Error: .env file does not exist" + fi } - -show_logs_ui(){ - docker logs -f ui-service +show_help() { + echo "Usage: $0 " + echo + echo "Commands:" + echo " start : Start the ontologies API and UI (Default Mode / Personilized Mode)" + echo " deploy : Deploy the application using Kamal to the server" + echo " stop : Stop the API and UI services" + echo " clean : Clean up the server (remove ontologies_linked_data, docker-compose files, and remove all containers)" + echo " help : Show this help message" + echo + echo "Additional notes:" + echo "- You can provide your own .env file to customize parameters:" + echo " Just put the .env file in this directory and run $0 start" } - case "$1" in "start") - #clean_containers - run_api - run_ui - ;; - "logs") - case "$2" in - "api") - show_logs_api - ;; - "ui") - show_logs_ui - ;; - *) - show_running_containers - ;; - esac - ;; + start_function + ;; + "deploy") + deploy + ;; "stop") - stop_services + stop_function ;; "clean") - clean_containers + clean ;; "help") show_help @@ -133,5 +235,4 @@ case "$1" in show_help exit 1 ;; -esac - +esac \ No newline at end of file diff --git a/run_api.sh b/run_api.sh deleted file mode 100755 index ef191d9..0000000 --- a/run_api.sh +++ /dev/null @@ -1,169 +0,0 @@ -#!/usr/bin/env bash - -update_env_file() { - local api_url="$1" - local old_path="$2" - local goo_path="$3" - local sparql_client_path="$4" - file_content=$(<.env_api) - while IFS= read -r line; do - if [[ "$line" == "API_URL="* && -n "$api_url" ]]; then - echo "API_URL=$api_url" - elif [[ "$line" == "ONTOLOGIES_LINKED_DATA_PATH="* ]]; then - echo "ONTOLOGIES_LINKED_DATA_PATH=$old_path" - elif [[ "$line" == "GOO_PATH="* ]]; then - echo "GOO_PATH=$goo_path" - elif [[ "$line" == "SPARQL_CLIENT_PATH="* ]]; then - echo "SPARQL_CLIENT_PATH=$sparql_client_path" - else - echo "$line" - fi - done <<< "$file_content" > .env_api -} - - -build_docker_run_cmd() { - local custom_command="$1" - local old_path="$2" - local goo_path="$3" - local sparql_client_path="$4" - local bash_cmd="" - - for path_var in "old_path:ontologies_linked_data" "goo_path:goo" "sparql_client_path:sparql-client"; do - IFS=':' read -r path value <<< "$path_var" - - if [ -n "${!path}" ]; then - host_path="$(realpath "$(dirname "${!path}")")/$value" - echo "Run: bundle config local.$value ${!path}" - container_path="/srv/ontoportal/$value" - docker_run_cmd+=" -v $host_path:$container_path" - bash_cmd+="(git config --global --add safe.directory $container_path && bundle config local.$value $container_path) &&" - else - bash_cmd+=" (bundle config unset local.$value) &&" - fi - done - - bash_cmd+=" (bundle check || bundle install || bundle update) && $custom_command" - #docker_run_cmd+="docker compose run --rm -it --name api-service --service-ports api bash -c \"$bash_cmd\"" - docker_run_cmd+="docker compose -f docker-compose_api_development.yml run --rm -d --name api-service --service-ports api bash -c \"$bash_cmd\"" - - echo "----------------------------------" - ls - echo "----------------------------------" - echo - echo '[+] RUN: ' $docker_run_cmd - eval "$docker_run_cmd" -} - - - -run_command() { - local custom_command="$1" - local reset_cache=false - local api_url="" - local old_path="" - local goo_path="" - local sparql_client_path="" - - shift - while [[ "$#" -gt 0 ]]; do - case $1 in - --reset-cache) - reset_cache=true - shift - ;; - --api-url) - api_url="$2" - shift 2 - ;; - --old-path) - old_path="$2" - shift 2 - ;; - --goo-path) - goo_path="$2" - shift 2 - ;; - --sparql-client-path) - sparql_client_path="$2" - shift 2 - ;; - *) - echo "Unknown option: $1" - show_help - exit 1 - ;; - esac - done - - if [ "$reset_cache" = true ]; then - echo "Resetting cache. Running: docker compose down --volumes" - docker compose down --volumes - fi - - update_env_file "$api_url" "$old_path" "$goo_path" "$sparql_client_path" - - - source .env_api - api_url="$API_URL" - old_path="$ONTOLOGIES_LINKED_DATA_PATH" - goo_path="$GOO_PATH" - sparql_client_path="$SPARQL_CLIENT_PATH" - - - if [ -z "$api_url" ] ; then - echo "Error: Missing required arguments. Please provide both --api-url or update them in your .env_api" - exit 1 - fi - - build_docker_run_cmd "$custom_command" "$old_path" "$goo_path" "$sparql_client_path" -} - - -clone_ongologies_linked_data(){ - repo_dir="ontologies_linked_data" - - if [ -d "$repo_dir" ]; then - echo "[+] Directory $repo_dir already exists. Skipping cloning." - else - echo "[+] Cloning ontologies_linked_data repo..." - git clone --depth=1 https://github.com/ontoportal-lirmm/ontologies_linked_data.git - fi - - echo -n "[+] Generating Solr configsets: " - if [ -d "$repo_dir" ]; then - cd ontologies_linked_data || exit 1 - if ! ./test/solr/generate_ncbo_configsets.sh; then - echo "Error: Failed to generate Solr configsets." - exit 1 - else - echo "Success: Generating Solr configsets." - fi - cd .. - else - echo "[-] Error: directory ontologies_linked_data does not exists" - fi -} - - -# Function to handle the "dev" option -dev() { - echo "Starting OntoPortal API development server..." - clone_ongologies_linked_data - - local custom_command="bundle exec rackup --host 0.0.0.0 --env=development --port 9393" - run_command "$custom_command" "$@" -} - -case "$1" in - "dev") - dev "${@:2}" - ;; - "help") - show_help - ;; - *) - show_help - exit 1 - ;; -esac diff --git a/run_ui.sh b/run_ui.sh deleted file mode 100755 index 33c8a26..0000000 --- a/run_ui.sh +++ /dev/null @@ -1,84 +0,0 @@ -#!/usr/bin/env bash - -update_env_file() { - local api_url="$1" - local api_key="$2" - - file_content=$(<.env_ui) - while IFS= read -r line; do - if [[ "$line" == "API_URL="* ]]; then - echo "API_URL=$api_url" - elif [[ "$line" == "API_KEY="* ]]; then - echo "API_KEY=$api_key" - else - echo "$line" - fi - done <<< "$file_content" > .env_ui -} - -dev() { - echo "Starting Ontoportal Web UI development server..." - - local reset_cache=false - local api_url="" - local api_key="" - - while [[ "$#" -gt 0 ]]; do - case $1 in - --reset-cache) - reset_cache=true - shift - ;; - --api-url) - api_url="$2" - shift 2 - ;; - --api-key) - api_key="$2" - shift 2 - ;; - *) - echo "Unknown option: $1" - show_help - exit 1 - ;; - esac - done - - if [ -n "$api_url" ] && [ -n "$api_key" ]; then - update_env_file "$api_url" "$api_key" - else - source .env_ui - api_url="$API_URL" - api_key="$API_KEY" - fi - - if [ -z "$api_url" ] || [ -z "$api_key" ]; then - echo "Error: Missing required arguments. Please provide both --api-url and --api-key or update them in your .env" - exit 1 - fi - - # Check if --reset-cache is present and execute docker compose down --volumes - if [ "$reset_cache" = true ]; then - echo "Resetting cache. Running: docker compose down --volumes" - docker compose down --volumes - fi - - echo "Run: bundle exec rails s -b 0.0.0.0 -p 3000" - #docker compose run --rm -it --service-ports rails bash -c "(bundle check || bundle install) && bin/rails db:prepare && bundle exec rails s -b 0.0.0.0 -p 3000" - docker compose -f docker-compose_ui_development.yml run --rm -d --name ui-service --service-ports rails bash -c "cp /app/config/bioportal_config_env.rb.sample /app/config/bioportal_config_development.rb && cp /app/config/database.yml.sample /app/config/database.yml && (bundle check || bundle install) && bin/rails db:prepare && bundle exec rails s -b 0.0.0.0 -p 4000" - -} - -case "$1" in - "dev") - dev "${@:2}" - ;; - "help") - show_help - ;; - *) - show_help - exit 1 - ;; -esac From 9ddb305cf81d4a11eab47474c3d8fe5a35536b6c Mon Sep 17 00:00:00 2001 From: imadbourouche Date: Wed, 24 Jan 2024 17:54:39 +0100 Subject: [PATCH 03/38] Configure ontologies_linked_data path & remove (bundle check || bundle install || bundle update) from run_api --- .env.sample | 52 ++++++++++++++++++++++++++++++---------------------- ontoportal | 15 ++++++++------- 2 files changed, 38 insertions(+), 29 deletions(-) diff --git a/.env.sample b/.env.sample index f3b870d..8d7f956 100644 --- a/.env.sample +++ b/.env.sample @@ -1,24 +1,32 @@ -BUNDLE_PATH="/srv/ontoportal/bundle" -# default bundle config resolves to /usr/local/bundle/config inside of the container -# we are setting it to local app directory if we need to use 'bundle config local' -BUNDLE_APP_CONFIG="/srv/ontoportal/.bundle" -# API/NCBO configs -REDIS_GOO_CACHE_HOST="redis-goo-cache" -REDIS_HTTP_CACHE_HOST="redis-http-cache" -REDIS_PERSISTENT_HOST="redis-persistent" -REPOSITORY_FOLDER="/srv/ontoportal/data/repository" -REPORT_PATH="/srv/ontoportal/data/reports/ontologies_report.json" -SOLR_TERM_SEARCH_URL="http://solr-term:8983/solr/term_search_core1" -SOLR_PROP_SEARCH_URL="http://solr-prop:8983/solr/prop_search_core1" -MGREP_HOST="mgrep" -MGREP_PORT="55556" -GOO_PORT=9000 -GOO_HOST="4store" +##################### PERSONEL configuration #################### +ORGANIZATION_NAME=ontoportal-lirmm +COMPOSE_API_FILE_PATH=/ontologies_api/development/docker-compose.yml +COMPOSE_UI_FILE_PATH=/bioportal_web_ui/development/docker-compose.yml +ONTOLOGIES_LINKED_DATA_PATH=https://github.com/ontoportal-lirmm/ontologies_linked_data.git -# AllegroGraph related settings -AGRAPH_SUPER_USER="test" -AGRAPH_SUPER_PASSWORD="xyzzy" +##################### API configuration #################### +API_URL=http://api-service:9393 +REDIS_GOO_CACHE_HOST=redis-ut +REDIS_HTTP_CACHE_HOST=redis-ut +REDIS_PERSISTENT_HOST=redis-ut -OP_APIKEY= -OP_API_URL="https://data.bioontology.org" -STARTER_ONTOLOGY="STY" +##################### UI configuration #################### +RAILS_ENV=development +SITE=Ontoportal +ORG_URL=http://www.lirmm.fr +UI_URL=http://localhost:3000 +API_URL=http://api-service:9393 +API_KEY=72c72cba-ad45-4785-b94e-483fa55cdddb +UI_THEME=ontoportal +FAIRNESS_DISABLED=false +NCBO_ANNOTATORPLUS_ENABLED=false +SUPPORT_EMAIL=sifrportal-support@lirmm.fr +RELEASE_VERSION="OntoPortal Appliance 3.0.1" +USE_RECAPTCHA=false + +##################### KAMAL configuration #################### +IMAGE_NAME= +SERVER_IP= +DOCKER_REGISTRY_NAME= +KAMAL_REGISTRY_PASSWORD= +SSH_USER= \ No newline at end of file diff --git a/ontoportal b/ontoportal index b0c904d..bd402af 100755 --- a/ontoportal +++ b/ontoportal @@ -1,14 +1,15 @@ #!/usr/bin/env bash run_api(){ + clone_ongologies_linked_data(){ repo_dir="ontologies_linked_data" if [ -d "$repo_dir" ]; then echo "[+] Directory $repo_dir already exists. Skipping cloning." else - echo "[+] Cloning ontologies_linked_data repo..." - git clone -q --depth=1 https://github.com/ontoportal-lirmm/ontologies_linked_data.git + echo "[+] Cloning ontologies_linked_data repo... from $ontologies_linked_data_path" + git clone -q --depth=1 "$ontologies_linked_data_path" fi echo "[+] Generating Solr configsets ..." @@ -25,15 +26,15 @@ run_api(){ echo "[-] Error: directory ontologies_linked_data does not exists" fi } - - clone_ongologies_linked_data source .env + ontologies_linked_data_path="$ONTOLOGIES_LINKED_DATA_PATH" api_url="$API_URL" - if [ -z "$api_url" ] ; then - echo "[-] Error: Missing required arguments. Please provide both --api-url or update them in your .env" + if [ -z "$api_url" ] || [ -z "$ontologies_linked_data_path" ]; then + echo "[-] Error: Missing required configurations. Please provide both API_URL and ONTOLOGIES_LINKED_DATA_PATH in your .env file" exit 1 fi - bash_cmd=" (bundle check || bundle install || bundle update) && bundle exec rackup --host 0.0.0.0 --env=development --port 9393" + clone_ongologies_linked_data + bash_cmd="bundle exec rackup --host 0.0.0.0 --env=development --port 9393" docker_run_cmd="docker compose -f docker-compose_api.yml run --rm -d --name api-service --service-ports api bash -c \"$bash_cmd\"" echo "[+] Starting the API" eval "$docker_run_cmd" From 11fe11bced9edd5f73e0951e5d0818f812fdc4b7 Mon Sep 17 00:00:00 2001 From: imadbourouche Date: Thu, 25 Jan 2024 15:05:08 +0100 Subject: [PATCH 04/38] Fix the clear methode to clean only the ontoportal_docker running and to not remove the volumes --- ontoportal | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/ontoportal b/ontoportal index bd402af..59ecab1 100755 --- a/ontoportal +++ b/ontoportal @@ -35,7 +35,7 @@ run_api(){ fi clone_ongologies_linked_data bash_cmd="bundle exec rackup --host 0.0.0.0 --env=development --port 9393" - docker_run_cmd="docker compose -f docker-compose_api.yml run --rm -d --name api-service --service-ports api bash -c \"$bash_cmd\"" + docker_run_cmd="docker compose -f docker-compose_api.yml -p ontoportal_docker run --rm -d --name api-service --service-ports api bash -c \"$bash_cmd\"" echo "[+] Starting the API" eval "$docker_run_cmd" } @@ -50,7 +50,7 @@ run_ui(){ fi echo "[+] Starting the UI" bash_cmd="cp /app/config/bioportal_config_env.rb.sample /app/config/bioportal_config_development.rb && cp /app/config/database.yml.sample /app/config/database.yml && (bundle check || bundle install) && bin/rails db:prepare && bundle exec rails s -b 0.0.0.0 -p 3000" - eval "docker compose -f docker-compose_ui.yml run --rm -d --name ui-service --service-ports rails bash -c \"$bash_cmd\"" + eval "docker compose -f docker-compose_ui.yml -p ontoportal_docker run --rm -d --name ui-service --service-ports rails bash -c \"$bash_cmd\"" } @@ -67,7 +67,9 @@ run_default_mode(){ echo "[+] Changing lines in docker-compose_api.yml" file_path="./docker-compose_api.yml" sed -i 's#- \.:\/srv\/ontoportal\/ontologies_api#- app_api:/srv/ontoportal/ontologies_api#' "$file_path" + sed -i 's#- bundle:/srv/ontoportal/bundle#- bundle_api:/srv/ontoportal/bundle#' "$file_path" sed -i 's#./test/solr/configsets:/configsets:ro#./ontologies_linked_data/test/solr/configsets/:/configsets:ro#' "$file_path" + sed -i '/volumes:/,/^$/ s/bundle:/bundle_api:/' "$file_path" sed -i '/^volumes:/a \ \ app_api:' "$file_path" echo "[+] Adding some envirement variables for the API" @@ -124,10 +126,17 @@ run_personlized_mode(){ echo "[+] Changing some lines" file_path="./docker-compose_api.yml" sed -i 's#- \.:\/srv\/ontoportal\/ontologies_api#- app_api:/srv/ontoportal/ontologies_api#' "$file_path" + sed -i 's#- bundle:/srv/ontoportal/bundle#- bundle_api:/srv/ontoportal/bundle#' "$file_path" sed -i 's#./test/solr/configsets:/configsets:ro#./ontologies_linked_data/test/solr/configsets/:/configsets:ro#' "$file_path" + sed -i '/volumes:/,/^$/ s/bundle:/bundle_api:/' "$file_path" sed -i '/^volumes:/a \ \ app_api:' "$file_path" echo "[+] Running api script" run_api + if [ $? -ne 0 ]; then + echo "[-] Error in run_api function. Exiting..." + exit 1 + fi + echo "[+] The API is running successfully." echo "[+] Getting compose file from: $ORGANIZATION_NAME$COMPOSE_UI_FILE_PATH" @@ -139,6 +148,11 @@ run_personlized_mode(){ sed -i '/^volumes:/a \ \ app_web:' "$file_path" echo "[+] Running ui script" run_ui + if [ $? -ne 0 ]; then + echo "[-] Error in run_ui function. Exiting..." + exit 1 + fi + echo "[+] The UI is running successfully." } clean(){ @@ -147,8 +161,8 @@ clean(){ rm -rf ontologies_linked_data echo "[+] Removing docker compose files" rm -f docker-compose* - echo "[+] Removing All containers/volumes/networks" - docker container rm -f $(docker ps -aq) 2>/dev/null; docker volume rm $(docker volume ls -q) 2>/dev/null ; docker network prune -f + echo "[+] Removing All containers" + docker ps -a --format "{{.Names}}" | grep "ontoportal_docker" | awk '{print "docker rm -f " $1}' | sh } start_function() { @@ -166,7 +180,7 @@ start_function() { stop_function() { echo "[+] Stoping services" docker container stop api-service - docker container stop ui-service + docker container stop ui-service } deploy(){ From bd0a166e1f97d217d45f53fec1ab8e5331416f73 Mon Sep 17 00:00:00 2001 From: imadbourouche Date: Thu, 25 Jan 2024 15:10:55 +0100 Subject: [PATCH 05/38] Get the env file by copying .env.sample not by pulling from the repositories --- ontoportal | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/ontoportal b/ontoportal index 59ecab1..553c03c 100755 --- a/ontoportal +++ b/ontoportal @@ -59,10 +59,10 @@ run_default_mode(){ run_api_default_mode(){ echo "[+] Getting compose file for API" curl -sS -L https://raw.githubusercontent.com/ontoportal-lirmm/ontologies_api/development/docker-compose.yml -o docker-compose_api.yml - echo >> .env - curl -sS -L https://raw.githubusercontent.com/ontoportal-lirmm/ontologies_api/development/.env.sample >> .env + #echo >> .env + #curl -sS -L https://raw.githubusercontent.com/ontoportal-lirmm/ontologies_api/development/.env.sample >> .env - sed -i 's/^API_URL=http:\/\/localhost:9393$/API_URL=http:\/\/api-service:9393/' .env + #sed -i 's/^API_URL=http:\/\/localhost:9393$/API_URL=http:\/\/api-service:9393/' .env echo "[+] Changing lines in docker-compose_api.yml" file_path="./docker-compose_api.yml" @@ -72,12 +72,12 @@ run_default_mode(){ sed -i '/volumes:/,/^$/ s/bundle:/bundle_api:/' "$file_path" sed -i '/^volumes:/a \ \ app_api:' "$file_path" - echo "[+] Adding some envirement variables for the API" - file_path=".env" - echo >> "$file_path" - echo "REDIS_GOO_CACHE_HOST=redis-ut" >> "$file_path" - echo "REDIS_HTTP_CACHE_HOST=redis-ut" >> "$file_path" - echo "REDIS_PERSISTENT_HOST=redis-ut" >> "$file_path" + #echo "[+] Adding some envirement variables for the API" + #file_path=".env" + #echo >> "$file_path" + #echo "REDIS_GOO_CACHE_HOST=redis-ut" >> "$file_path" + #echo "REDIS_HTTP_CACHE_HOST=redis-ut" >> "$file_path" + #echo "REDIS_PERSISTENT_HOST=redis-ut" >> "$file_path" echo "[+] Running api script" @@ -93,11 +93,11 @@ run_default_mode(){ run_ui_default_mode(){ echo "[+] Getting compose file for UI" curl -sS -L https://raw.githubusercontent.com/ontoportal-lirmm/bioportal_web_ui/development/docker-compose.yml -o docker-compose_ui.yml - echo >> .env - curl -sS -L https://raw.githubusercontent.com/ontoportal-lirmm/bioportal_web_ui/development/.env.sample >> .env + #echo >> .env + #curl -sS -L https://raw.githubusercontent.com/ontoportal-lirmm/bioportal_web_ui/development/.env.sample >> .env - sed -i 's/^API_URL=http:\/\/localhost:9393$/API_URL=http:\/\/api-service:9393/' .env - sed -i 's/^API_KEY=$/API_KEY=72c72cba-ad45-4785-b94e-483fa55cdddb/' .env + #sed -i 's/^API_URL=http:\/\/localhost:9393$/API_URL=http:\/\/api-service:9393/' .env + #sed -i 's/^API_KEY=$/API_KEY=72c72cba-ad45-4785-b94e-483fa55cdddb/' .env echo "[+] Changing lines in docker-compose_ui.yml" file_path="./docker-compose_ui.yml" @@ -114,7 +114,8 @@ run_default_mode(){ echo "[+] The UI is running successfully." } - + + cp .env.sample .env run_api_default_mode run_ui_default_mode } From 6e15c40f537ed4822dd72df10a001ce4737f7979 Mon Sep 17 00:00:00 2001 From: imadbourouche Date: Thu, 29 Feb 2024 11:08:56 +0100 Subject: [PATCH 06/38] Enhanced script configurability and personalization - Added support for user-defined environment variables - Give the user ability to: - start/deploy the services that he wants (api / ui / ontoportal) - specify the source of his project (docker compose files) - Fixing some functionalities: - fix the clean/stop methods to clean/stop only the services related to our project - Refactored code for better readability and maintainability --- .env.sample | 7 +- ontoportal | 207 +++++++++++++++++++++------------------------------- 2 files changed, 89 insertions(+), 125 deletions(-) diff --git a/.env.sample b/.env.sample index 8d7f956..8d2e178 100644 --- a/.env.sample +++ b/.env.sample @@ -1,8 +1,9 @@ ##################### PERSONEL configuration #################### ORGANIZATION_NAME=ontoportal-lirmm -COMPOSE_API_FILE_PATH=/ontologies_api/development/docker-compose.yml -COMPOSE_UI_FILE_PATH=/bioportal_web_ui/development/docker-compose.yml -ONTOLOGIES_LINKED_DATA_PATH=https://github.com/ontoportal-lirmm/ontologies_linked_data.git +COMPOSE_API_FILE_PATH=/ontologies_api/master/docker-compose.yml +COMPOSE_UI_FILE_PATH=/bioportal_web_ui/master/docker-compose.yml +ONTOLOGIES_LINKED_DATA_LINK=https://github.com/ontoportal-lirmm/ontologies_linked_data.git +SERVICE=ontoportal ##################### API configuration #################### API_URL=http://api-service:9393 diff --git a/ontoportal b/ontoportal index 553c03c..e16ad14 100755 --- a/ontoportal +++ b/ontoportal @@ -3,17 +3,16 @@ run_api(){ clone_ongologies_linked_data(){ - repo_dir="ontologies_linked_data" - if [ -d "$repo_dir" ]; then + if [ -d "ontologies_linked_data" ]; then echo "[+] Directory $repo_dir already exists. Skipping cloning." else - echo "[+] Cloning ontologies_linked_data repo... from $ontologies_linked_data_path" - git clone -q --depth=1 "$ontologies_linked_data_path" + echo "[+] Cloning ontologies_linked_data repo... from $ONTOLOGIES_LINKED_DATA_LINK" + git clone -q --depth=1 "$ONTOLOGIES_LINKED_DATA_LINK" fi echo "[+] Generating Solr configsets ..." - if [ -d "$repo_dir" ]; then + if [ -d "ontologies_linked_data" ]; then cd ontologies_linked_data || exit 1 if ! ./test/solr/generate_ncbo_configsets.sh; then echo "[-] Error: Failed to generate Solr configsets." @@ -27,10 +26,10 @@ run_api(){ fi } source .env - ontologies_linked_data_path="$ONTOLOGIES_LINKED_DATA_PATH" + ontologies_linked_data_link="$ONTOLOGIES_LINKED_DATA_LINK" api_url="$API_URL" - if [ -z "$api_url" ] || [ -z "$ontologies_linked_data_path" ]; then - echo "[-] Error: Missing required configurations. Please provide both API_URL and ONTOLOGIES_LINKED_DATA_PATH in your .env file" + if [ -z "$api_url" ] || [ -z "$ontologies_linked_data_link" ]; then + echo "[-] Error: Missing required configurations. Please provide both API_URL and ONTOLOGIES_LINKED_DATA_LINK in your .env file" exit 1 fi clone_ongologies_linked_data @@ -54,51 +53,33 @@ run_ui(){ } -run_default_mode(){ +start() { - run_api_default_mode(){ + start_api(){ echo "[+] Getting compose file for API" - curl -sS -L https://raw.githubusercontent.com/ontoportal-lirmm/ontologies_api/development/docker-compose.yml -o docker-compose_api.yml - #echo >> .env - #curl -sS -L https://raw.githubusercontent.com/ontoportal-lirmm/ontologies_api/development/.env.sample >> .env - - #sed -i 's/^API_URL=http:\/\/localhost:9393$/API_URL=http:\/\/api-service:9393/' .env - + echo "[+] Getting compose file from: $ORGANIZATION_NAME$COMPOSE_API_FILE_PATH" + eval "curl -sS -L https://raw.githubusercontent.com/$ORGANIZATION_NAME$COMPOSE_API_FILE_PATH -o docker-compose_api.yml" echo "[+] Changing lines in docker-compose_api.yml" file_path="./docker-compose_api.yml" - sed -i 's#- \.:\/srv\/ontoportal\/ontologies_api#- app_api:/srv/ontoportal/ontologies_api#' "$file_path" + sed -i 's#- .:/srv/ontoportal/ontologies_api#- app_api:/srv/ontoportal/ontologies_api#' "$file_path" sed -i 's#- bundle:/srv/ontoportal/bundle#- bundle_api:/srv/ontoportal/bundle#' "$file_path" sed -i 's#./test/solr/configsets:/configsets:ro#./ontologies_linked_data/test/solr/configsets/:/configsets:ro#' "$file_path" sed -i '/volumes:/,/^$/ s/bundle:/bundle_api:/' "$file_path" - sed -i '/^volumes:/a \ \ app_api:' "$file_path" - - #echo "[+] Adding some envirement variables for the API" - #file_path=".env" - #echo >> "$file_path" - #echo "REDIS_GOO_CACHE_HOST=redis-ut" >> "$file_path" - #echo "REDIS_HTTP_CACHE_HOST=redis-ut" >> "$file_path" - #echo "REDIS_PERSISTENT_HOST=redis-ut" >> "$file_path" + sed -i '/^volumes:/a \ \ app_api:' "$file_path" echo "[+] Running api script" - run_api if [ $? -ne 0 ]; then echo "[-] Error in run_api function. Exiting..." exit 1 fi - echo "[+] The API is running successfully." } - run_ui_default_mode(){ - echo "[+] Getting compose file for UI" - curl -sS -L https://raw.githubusercontent.com/ontoportal-lirmm/bioportal_web_ui/development/docker-compose.yml -o docker-compose_ui.yml - #echo >> .env - #curl -sS -L https://raw.githubusercontent.com/ontoportal-lirmm/bioportal_web_ui/development/.env.sample >> .env + start_ui(){ + echo "[+] Getting compose file from: $ORGANIZATION_NAME$COMPOSE_UI_FILE_PATH" + eval "curl -sS -L https://raw.githubusercontent.com/$ORGANIZATION_NAME$COMPOSE_UI_FILE_PATH -o docker-compose_ui.yml" - #sed -i 's/^API_URL=http:\/\/localhost:9393$/API_URL=http:\/\/api-service:9393/' .env - #sed -i 's/^API_KEY=$/API_KEY=72c72cba-ad45-4785-b94e-483fa55cdddb/' .env - echo "[+] Changing lines in docker-compose_ui.yml" file_path="./docker-compose_ui.yml" sed -i 's/- .:\/app/- app_web:\/app/' "$file_path" @@ -114,106 +95,88 @@ run_default_mode(){ echo "[+] The UI is running successfully." } - - cp .env.sample .env - run_api_default_mode - run_ui_default_mode -} -run_personlized_mode(){ - source .env - echo "[+] Getting compose file from: $ORGANIZATION_NAME$COMPOSE_API_FILE_PATH" - eval "curl -sS -L https://raw.githubusercontent.com/$ORGANIZATION_NAME$COMPOSE_API_FILE_PATH -o docker-compose_api.yml" - echo "[+] Changing some lines" - file_path="./docker-compose_api.yml" - sed -i 's#- \.:\/srv\/ontoportal\/ontologies_api#- app_api:/srv/ontoportal/ontologies_api#' "$file_path" - sed -i 's#- bundle:/srv/ontoportal/bundle#- bundle_api:/srv/ontoportal/bundle#' "$file_path" - sed -i 's#./test/solr/configsets:/configsets:ro#./ontologies_linked_data/test/solr/configsets/:/configsets:ro#' "$file_path" - sed -i '/volumes:/,/^$/ s/bundle:/bundle_api:/' "$file_path" - sed -i '/^volumes:/a \ \ app_api:' "$file_path" - echo "[+] Running api script" - run_api - if [ $? -ne 0 ]; then - echo "[-] Error in run_api function. Exiting..." - exit 1 - fi - echo "[+] The API is running successfully." - - - echo "[+] Getting compose file from: $ORGANIZATION_NAME$COMPOSE_UI_FILE_PATH" - eval "curl -sS -L https://raw.githubusercontent.com/$ORGANIZATION_NAME$COMPOSE_UI_FILE_PATH -o docker-compose_ui.yml" - echo "[+] Changing some lines" - file_path="./docker-compose_ui.yml" - sed -i 's/- .:\/app/- app_web:\/app/' "$file_path" - sed -i 's/retries: 3/retries: 100/' "$file_path" - sed -i '/^volumes:/a \ \ app_web:' "$file_path" - echo "[+] Running ui script" - run_ui - if [ $? -ne 0 ]; then - echo "[-] Error in run_ui function. Exiting..." - exit 1 + clean + if [ -f ".env" ]; then + echo "[+] Env file exist" + else + echo "[+] Env file does not exist" + cp .env.sample .env fi - echo "[+] The UI is running successfully." -} - -clean(){ - echo "[+] Cleanning the server" - echo "[+] Removing ontologies_linked_data" - rm -rf ontologies_linked_data - echo "[+] Removing docker compose files" - rm -f docker-compose* - echo "[+] Removing All containers" - docker ps -a --format "{{.Names}}" | grep "ontoportal_docker" | awk '{print "docker rm -f " $1}' | sh -} -start_function() { - if [ -f ".env" ]; then - echo "[+] Starting in personlized mode" - run_personlized_mode + if [ -z "$1" ]; then + sed -i "s/^SERVICE=.*/SERVICE=ontoportal/" .env else - echo "[+] Starting the script in default mode" - rm -f .env - clean - run_default_mode + sed -i "s/^SERVICE=.*/SERVICE=$1/" .env fi + + source .env + if [ "$SERVICE" == "api" ]; then + start_api + elif [ "$SERVICE" == "ui" ]; then + start_ui + else + start_api + start_ui + fi + } -stop_function() { +stop() { echo "[+] Stoping services" docker container stop api-service docker container stop ui-service + docker ps -a --format "{{.Names}}" | grep "ontoportal_docker" | awk '{print "docker stop " $1}' | sh } deploy(){ if [ -f ".env" ]; then - echo "[+] Checking for env variables" - source .env - # List of environment variables to check - variables=("IMAGE_NAME" "SERVER_IP" "DOCKER_REGISTRY_NAME" "KAMAL_REGISTRY_PASSWORD" "SSH_USER") - - for variable in "${variables[@]}"; do - if [ -z "${!variable}" ]; then - echo "[-] Error: $variable is not set." - exit - fi - done - - echo "[+] Changing kamal deploy.yml file" - echo "" > config/deploy.yml - echo "service: ontoportal_docker" > config/deploy.yml - echo "image: ${IMAGE_NAME}" >> config/deploy.yml - echo -e "servers:\n - ${SERVER_IP}" >> config/deploy.yml - echo "run_directory: /root/app" >> config/deploy.yml - echo -e "registry:\n username:\n - DOCKER_REGISTRY_NAME\n password:\n - KAMAL_REGISTRY_PASSWORD" >> config/deploy.yml - echo -e "ssh:\n user: ${SSH_USER}" >> config/deploy.yml - echo "volumes: /var/run/docker.sock:/var/run/docker.sock" >> config/deploy.yml - echo -e "traefik:\n host_port: 4000" >> config/deploy.yml - echo -e "healthcheck:\n cmd: /bin/true" >> config/deploy.yml - echo "[+] Starting the deployment" - kamal setup -vv + echo "[+] Env file exist" else - echo "[-] Error: .env file does not exist" + echo "[+] Env file does not exist" + cp .env.sample .env fi + + if [ -z "$1" ]; then + sed -i "s/^SERVICE=.*/SERVICE=ontoportal/" .env + else + sed -i "s/^SERVICE=.*/SERVICE=$1/" .env + fi + + echo "[+] Checking for env variables" + source .env + variables=("IMAGE_NAME" "SERVER_IP" "DOCKER_REGISTRY_NAME" "KAMAL_REGISTRY_PASSWORD" "SSH_USER") + + for variable in "${variables[@]}"; do + if [ -z "${!variable}" ]; then + echo "[-] Error: $variable is not set." + exit + fi + done + + echo "[+] Changing kamal deploy.yml file" + echo "" > config/deploy.yml + echo "service: ontoportal_docker" > config/deploy.yml + echo "image: ${IMAGE_NAME}" >> config/deploy.yml + echo -e "servers:\n - ${SERVER_IP}" >> config/deploy.yml + echo "run_directory: /root/app" >> config/deploy.yml + echo -e "registry:\n username:\n - DOCKER_REGISTRY_NAME\n password:\n - KAMAL_REGISTRY_PASSWORD" >> config/deploy.yml + echo -e "ssh:\n user: ${SSH_USER}" >> config/deploy.yml + echo "volumes: /var/run/docker.sock:/var/run/docker.sock" >> config/deploy.yml + echo -e "traefik:\n host_port: 4000" >> config/deploy.yml + echo -e "healthcheck:\n cmd: /bin/true" >> config/deploy.yml + echo "[+] Starting the deployment" + kamal setup -vv + +} + +clean(){ + echo "[+] Cleanning the server" + rm -rf ontologies_linked_data + rm -f docker-compose* + docker container rm -f api-service ui-service 2>/dev/null + docker ps -a --format "{{.Names}}" | grep "ontoportal_docker" | awk '{print "docker rm -f " $1}' | sh + docker volume rm ontoportal_docker_rails_cache > /dev/null 2>&1 } show_help() { @@ -233,13 +196,13 @@ show_help() { case "$1" in "start") - start_function + start "$2" ;; "deploy") - deploy + deploy "$2" ;; "stop") - stop_function + stop ;; "clean") clean From 3fe8fba8f53437f1f8bda02c7a800319b7aaeebf Mon Sep 17 00:00:00 2001 From: Syphax Bouazzouni Date: Sat, 8 Jun 2024 10:41:58 +0200 Subject: [PATCH 07/38] remove old docker-compose.yml --- docker-compose.yml | 85 ---------------------------------------------- 1 file changed, 85 deletions(-) delete mode 100644 docker-compose.yml diff --git a/docker-compose.yml b/docker-compose.yml deleted file mode 100644 index f0ccc3f..0000000 --- a/docker-compose.yml +++ /dev/null @@ -1,85 +0,0 @@ -x-app: &api - stdin_open: true - tty: true - env_file: - .env - depends_on: &depends_on - solr: - condition: service_started - redis: - condition: service_healthy - mgrep: - condition: service_started - 4store: - condition: service_started - -services: - api: - <<: *api - image: ${IMAGE_REPOSITORY}/ontologies_api:${IMAGE_TAG} - command: "bundle exec rackup -o 0.0.0.0 --port 9393" - env_file: - .env - volumes: - - history:/usr/local/hist - - repository:/srv/ontoportal/data/repository - - reports:/srv/ontoportal/data/reports - ports: - - 9393:9393 - - ncbo_cron: - <<: *api - image: ${IMAGE_REPOSITORY}/ncbo_cron:${IMAGE_TAG} - command: "bundle exec bin/ncbo_cron" - volumes: - - history:/usr/local/hist - - repository:/srv/ontoportal/data/repository - - reports:/srv/ontoportal/data/reports - - mgrep:/srv/ontoportal/data/mgrep - - logs:/srv/ontoportal/ncbo_cron/logs - - redis: - image: redis - command: ["redis-server", "--save", "", "--maxmemory-policy", "volatile-ttl", "--maxmemory", "128000000"] - volumes: - - redis_data:/data - healthcheck: - test: redis-cli ping - interval: 10s - timeout: 3s - retries: 10 - - 4store: - image: bde2020/4store - platform: linux/amd64 - volumes: - - 4store_data:/var/lib/4store - command: bash -c "4s-backend ontoportal_kb && 4s-httpd -D -s-1 -p 9000 ontoportal_kb" - - solr: - image: solr:9.1 - command: bin/solr start -cloud -f - - mgrep: - image: ontoportal/mgrep:0.0.2 - platform: linux/amd64 - volumes: - - mgrep:/srv/mgrep/dictionary - healthcheck: - test: ["CMD", "nc", "-z", "-v", "localhost", "55556"] - start_period: 3s - interval: 10s - timeout: 5s - retries: 5 - -volumes: - bundle: - 4store_data: - ag_data: - repository: - mgrep: - reports: - redis_data: - history: - logs: - solr_data: From 85aea23e0ddc586932f9b841bf3d5ba4d339d7a8 Mon Sep 17 00:00:00 2001 From: Syphax Bouazzouni Date: Sat, 8 Jun 2024 10:43:02 +0200 Subject: [PATCH 08/38] add bats to run bash tests --- .gitmodules | 9 +++++++++ test/bats | 1 + test/run_tests.sh | 3 +++ test/test_helper/bats-assert | 1 + test/test_helper/bats-support | 1 + test/tests.sh | 25 +++++++++++++++++++++++++ 6 files changed, 40 insertions(+) create mode 100644 .gitmodules create mode 160000 test/bats create mode 100755 test/run_tests.sh create mode 160000 test/test_helper/bats-assert create mode 160000 test/test_helper/bats-support create mode 100644 test/tests.sh diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..b7efcb4 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,9 @@ +[submodule "test/bats"] + path = test/bats + url = https://github.com/bats-core/bats-core.git +[submodule "test/test_helper/bats-support"] + path = test/test_helper/bats-support + url = https://github.com/bats-core/bats-support.git +[submodule "test/test_helper/bats-assert"] + path = test/test_helper/bats-assert + url = https://github.com/bats-core/bats-assert.git diff --git a/test/bats b/test/bats new file mode 160000 index 0000000..902578d --- /dev/null +++ b/test/bats @@ -0,0 +1 @@ +Subproject commit 902578da790fbcb035747d2964747f192f6e1603 diff --git a/test/run_tests.sh b/test/run_tests.sh new file mode 100755 index 0000000..ea9cbdb --- /dev/null +++ b/test/run_tests.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash +./test/bats/bin/bats ./test/tests.sh + diff --git a/test/test_helper/bats-assert b/test/test_helper/bats-assert new file mode 160000 index 0000000..e2d855b --- /dev/null +++ b/test/test_helper/bats-assert @@ -0,0 +1 @@ +Subproject commit e2d855bc78619ee15b0c702b5c30fb074101159f diff --git a/test/test_helper/bats-support b/test/test_helper/bats-support new file mode 160000 index 0000000..9bf10e8 --- /dev/null +++ b/test/test_helper/bats-support @@ -0,0 +1 @@ +Subproject commit 9bf10e876dd6b624fe44423f0b35e064225f7556 diff --git a/test/tests.sh b/test/tests.sh new file mode 100644 index 0000000..f4670d3 --- /dev/null +++ b/test/tests.sh @@ -0,0 +1,25 @@ +# Here you write the tests +# And run them using test/run_tests.sh +setup() { + load 'test_helper/bats-support/load' + load 'test_helper/bats-assert/load' + DIR="$( cd "$( dirname "$BATS_TEST_FILENAME" )" >/dev/null 2>&1 && pwd )" + PATH="$DIR/../src:$PATH" +} + +@test "Running and Stopping API" { + run bin/run_api.sh start + assert_output --partial "[+] Server is up and running!" + refute_output --partial 'error' + refute_output --partial 'ERROR' + + bin/run_api.sh stop + + run docker compose -f docker-compose_api.yml ps + assert_output "NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS" + + bin/run_api.sh clean + + run docker compose -f docker-compose_api.yml ps -a + assert_output "NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS" +} From 4cb164f0dbaa602909f7d8c7158e1ef814d1a42b Mon Sep 17 00:00:00 2001 From: Syphax Bouazzouni Date: Sat, 8 Jun 2024 10:43:54 +0200 Subject: [PATCH 09/38] add loading_animation.sh utility to show a loading animation --- utils/loading_animation.sh | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100755 utils/loading_animation.sh diff --git a/utils/loading_animation.sh b/utils/loading_animation.sh new file mode 100755 index 0000000..879f02a --- /dev/null +++ b/utils/loading_animation.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash + +function loading_icon() { + local loading_message="${1}" + local status_endpoint="${2}" + local load_interval="${3}" + local elapsed=0 + local loading_animation=( '—' "\\" '|' '/' ) + + echo -n "${loading_message} " + + # This part is to make the cursor not blink + # on top of the animation while it lasts + tput civis + trap "tput cnorm" EXIT + + until [ $elapsed -ge $load_interval ] || curl -sSf $status_endpoint > /dev/null 2>&1; do + for frame in "${loading_animation[@]}" ; do + printf "%s\b" "${frame}" + sleep 0.25 + done + elapsed=$(( elapsed + 1 )) + done + printf " \b\n" +} + +loading_icon "$@" \ No newline at end of file From 1a4c0fce6b9184209ee053f679ec3441c289fe2f Mon Sep 17 00:00:00 2001 From: Syphax Bouazzouni Date: Sat, 8 Jun 2024 10:45:52 +0200 Subject: [PATCH 10/38] no more need to get ontologies_linked_data to run the api --- .env.sample | 1 - .gitignore | 6 ++++++ ontoportal | 30 +++--------------------------- 3 files changed, 9 insertions(+), 28 deletions(-) diff --git a/.env.sample b/.env.sample index 8d2e178..0ec8e56 100644 --- a/.env.sample +++ b/.env.sample @@ -2,7 +2,6 @@ ORGANIZATION_NAME=ontoportal-lirmm COMPOSE_API_FILE_PATH=/ontologies_api/master/docker-compose.yml COMPOSE_UI_FILE_PATH=/bioportal_web_ui/master/docker-compose.yml -ONTOLOGIES_LINKED_DATA_LINK=https://github.com/ontoportal-lirmm/ontologies_linked_data.git SERVICE=ontoportal ##################### API configuration #################### diff --git a/.gitignore b/.gitignore index 6302686..8de99c8 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,9 @@ dip.override.yml .kamal/hooks/ + +docker-compose_api.yml + +docker-compose_ui.yml + +*.iml diff --git a/ontoportal b/ontoportal index e16ad14..83dc202 100755 --- a/ontoportal +++ b/ontoportal @@ -1,30 +1,6 @@ #!/usr/bin/env bash run_api(){ - - clone_ongologies_linked_data(){ - - if [ -d "ontologies_linked_data" ]; then - echo "[+] Directory $repo_dir already exists. Skipping cloning." - else - echo "[+] Cloning ontologies_linked_data repo... from $ONTOLOGIES_LINKED_DATA_LINK" - git clone -q --depth=1 "$ONTOLOGIES_LINKED_DATA_LINK" - fi - - echo "[+] Generating Solr configsets ..." - if [ -d "ontologies_linked_data" ]; then - cd ontologies_linked_data || exit 1 - if ! ./test/solr/generate_ncbo_configsets.sh; then - echo "[-] Error: Failed to generate Solr configsets." - exit 1 - else - echo "[+] Success: Generating Solr configsets." - fi - cd .. - else - echo "[-] Error: directory ontologies_linked_data does not exists" - fi - } source .env ontologies_linked_data_link="$ONTOLOGIES_LINKED_DATA_LINK" api_url="$API_URL" @@ -123,7 +99,7 @@ start() { } stop() { - echo "[+] Stoping services" + echo "[+] Stopping services" docker container stop api-service docker container stop ui-service docker ps -a --format "{{.Names}}" | grep "ontoportal_docker" | awk '{print "docker stop " $1}' | sh @@ -171,8 +147,8 @@ deploy(){ } clean(){ - echo "[+] Cleanning the server" - rm -rf ontologies_linked_data + echo "[+] Cleaning the server" + rm -f docker-compose* docker container rm -f api-service ui-service 2>/dev/null docker ps -a --format "{{.Names}}" | grep "ontoportal_docker" | awk '{print "docker rm -f " $1}' | sh From f7db34994afa1555545a502e029072dd575623b1 Mon Sep 17 00:00:00 2001 From: Syphax Bouazzouni Date: Sat, 8 Jun 2024 10:46:46 +0200 Subject: [PATCH 11/38] extract from ontoportal script the file bin/run_api.sh --- bin/run_api.sh | 115 +++++++++++++++++++++++++++++++++++++++++++++++++ ontoportal | 38 +--------------- 2 files changed, 117 insertions(+), 36 deletions(-) create mode 100755 bin/run_api.sh diff --git a/bin/run_api.sh b/bin/run_api.sh new file mode 100755 index 0000000..bdcded2 --- /dev/null +++ b/bin/run_api.sh @@ -0,0 +1,115 @@ +#!/usr/bin/env bash +status_ok() { + curl -sSf http://localhost:9393 > /dev/null 2>&1 +} + +log() { + docker exec -it api-service tail -f log/production.log +} + +clean() { + echo "[+] Cleaning the API containers" + docker container rm -f api-service + docker compose -f docker-compose_api.yml --profile 4store down --volumes +} + +stop() { + echo "[+] Stopping the API" + docker stop api-service + docker compose -f docker-compose_api.yml --profile 4store stop +} + +run(){ + local env_path='.env' + + source "$env_path" + + local api_url="$API_URL" + + if [ -z "$api_url" ]; then + echo "[-] Error: Missing required configurations. Please provide the API_URL in your .env file" + exit 1 + fi + + bash_cmd="rm -fr tmp/pids/unicorn.pid && (bundle check || bundle install) && bundle exec unicorn -c config/unicorn.rb -E production -l 9393" + #bash_cmd="bash" + + docker_run_cmd="docker compose -f docker-compose_api.yml -p ontoportal_docker run --remove-orphans --name api-service --rm -d --service-ports api bash -c \"$bash_cmd\"" + echo "[+] Starting the API" + eval "$docker_run_cmd" + + # Wait for API to be ready (adjust the sleep time accordingly) + if docker ps --format '{{.Names}}' | grep -q "^api-service$"; then + echo "[+] API containers started" + else + eval "$docker_run_cmd > /dev/null 2>&1;" + fi + + source utils/loading_animation.sh "[+] Waiting for the server http://localhost:9393 to be up..." "http://localhost:9393" 300 + + if status_ok; then + echo "[+] Server is up and running!" + else + echo "[x] Timed out waiting for the server to be up." + exit 1 + fi +} + +start(){ + local file_path="docker-compose_api.yml" + local env_path=".env" + if [ -z "$env_path" ]; then + echo "[-] Error: Missing required configurations. Please provide the path to your .env file" + exit 1 + fi + source "$env_path" + + if [ -z "$ORGANIZATION_NAME" ]; [ -z "$COMPOSE_API_FILE_PATH" ]; then + echo "[-] Error: Missing required configurations. Please provide both ORGANIZATION_NAME and COMPOSE_API_FILE_PATH in your .env file" + exit 1 + fi + + echo "[+] Getting compose file for API" + echo "[+] Getting compose file from: $ORGANIZATION_NAME$COMPOSE_API_FILE_PATH" + eval "curl -sS -L https://raw.githubusercontent.com/$ORGANIZATION_NAME$COMPOSE_API_FILE_PATH -o docker-compose_api.yml" + + echo "[+] Running api script" + run "$env_path" +} + +usage() { + echo "Usage: $0