From b9dde022b4b140e6b0fdf7f919ed019b7580e298 Mon Sep 17 00:00:00 2001 From: Ignacio Foche Perez Date: Tue, 26 May 2020 10:39:46 +0200 Subject: [PATCH 01/16] version bumped --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 4b7bc72..4710ee5 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ setuptools.setup( name="d2_docker", - version="1.0.6", + version="1.0.7", description="Dockers for DHIS2 instances", long_description=open("README.md", encoding="utf-8").read(), keywords=["python"], From 2539cc88b55ea8a17f91228f96728ee808b1b5aa Mon Sep 17 00:00:00 2001 From: Arnau Sanchez Date: Tue, 26 May 2020 12:46:03 +0200 Subject: [PATCH 02/16] Use urllib.request instead of requests for wait_for_server --- src/d2_docker/utils.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/d2_docker/utils.py b/src/d2_docker/utils.py index c025fea..9722c13 100644 --- a/src/d2_docker/utils.py +++ b/src/d2_docker/utils.py @@ -3,7 +3,6 @@ import logging import re import os -import requests import shutil import socket import tempfile @@ -424,17 +423,16 @@ def wait_for_server(port): while True: try: logger.debug("wait_for_server:url={}".format(url)) - res = requests.get(url) - status = res.status_code - logger.debug("wait_for_server:status={}".format(status)) - if status == 502 or status == 504: - pass - elif status == 200: - return True - else: + urllib.request.urlopen(url) + logger.debug("wait_for_server:ok") + return True + except urllib.request.URLError as e: + logger.debug("wait_for_server:url-error: {}".format(e.reason)) + except urllib.request.HTTPError as e: + if e.code == 404: return False - except requests.exceptions.ConnectionError: - logger.debug("wait_for_server:connection-error") + else: + logger.debug("wait_for_server:http-error: {}".format(e.code)) time.sleep(5) From 071821ccfa707a2149b8e5c28e9d98b8a39fa29f Mon Sep 17 00:00:00 2001 From: Alexis Rico Date: Wed, 27 May 2020 14:53:05 +0200 Subject: [PATCH 03/16] Escape backslashes --- src/d2_docker/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/d2_docker/utils.py b/src/d2_docker/utils.py index 9722c13..7831ccc 100644 --- a/src/d2_docker/utils.py +++ b/src/d2_docker/utils.py @@ -235,7 +235,7 @@ def get_absdir_for_docker_volume(directory): if not directory: empty_directory = os.path.join(os.path.dirname(__file__), ".empty") return empty_directory - elif not os.path.isdir(directory): + elif not os.path.isdir(directory.replace("\/", "\\")): raise D2DockerError("Should be a directory: {}".format(directory)) else: return os.path.abspath(directory) From 70b734a8020c0539d7f9875d45f7b4a0555f86a3 Mon Sep 17 00:00:00 2001 From: Alexis Rico Date: Wed, 27 May 2020 16:01:48 +0200 Subject: [PATCH 04/16] Change os.path methods to pathlib --- src/d2_docker/utils.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/d2_docker/utils.py b/src/d2_docker/utils.py index 7831ccc..2a2d723 100644 --- a/src/d2_docker/utils.py +++ b/src/d2_docker/utils.py @@ -9,6 +9,7 @@ import time import urllib.request from distutils import dir_util +from pathlib import Path from .image_name import ImageName @@ -235,7 +236,7 @@ def get_absdir_for_docker_volume(directory): if not directory: empty_directory = os.path.join(os.path.dirname(__file__), ".empty") return empty_directory - elif not os.path.isdir(directory.replace("\/", "\\")): + elif not Path(directory).is_dir(): raise D2DockerError("Should be a directory: {}".format(directory)) else: return os.path.abspath(directory) @@ -264,7 +265,7 @@ def get_docker_directory(type, args=None): basedir = args and args.dhis2_docker_images_directory or script_dir docker_dir = os.path.join(basedir, subdir) - if not os.path.isdir(docker_dir): + if not Path(docker_dir).is_dir(): raise D2DockerError("Docker directory not found: {}".format(docker_dir)) else: logger.debug("Docker directory: {}".format(docker_dir)) From 494468f0dfba686ebcb14e9c21d7ba475233b71b Mon Sep 17 00:00:00 2001 From: Arnau Sanchez Date: Mon, 6 Jul 2020 11:32:45 +0200 Subject: [PATCH 05/16] Add command version --- src/d2_docker/cli.py | 4 +++- src/d2_docker/commands/version.py | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 src/d2_docker/commands/version.py diff --git a/src/d2_docker/cli.py b/src/d2_docker/cli.py index 9cc46bb..752318f 100644 --- a/src/d2_docker/cli.py +++ b/src/d2_docker/cli.py @@ -18,6 +18,7 @@ run_sql, create, upgrade, + version ) COMMAND_MODULES = [ @@ -35,6 +36,7 @@ run_sql, create, upgrade, + version, ] def get_parser(): @@ -47,7 +49,7 @@ def get_parser(): parser.add_argument( "--log-level", metavar="NOTSET | DEBUG | INFO | WARNING | ERROR | CRITICAL", - default="DEBUG", + default="INFO", help="Run command with the given log level", ) subparsers = parser.add_subparsers(help="Subcommands", dest="command") diff --git a/src/d2_docker/commands/version.py b/src/d2_docker/commands/version.py new file mode 100644 index 0000000..d1243b9 --- /dev/null +++ b/src/d2_docker/commands/version.py @@ -0,0 +1,20 @@ +from d2_docker import utils +import pkg_resources # part of setuptools + +DESCRIPTION = "Show version" + + +def setup(parser): + pass + + +def run(args): + utils.run(["docker", "-v"]) + utils.run(["docker-compose", "-v"]) + resource = pkg_resources.require("d2-docker") + if resource: + version = pkg_resources.require("d2-docker")[0].version + print("d2-docker version {}".format(version)) + else: + print("Cannot get d2-docker version") + From 9583226faa1f35583b5430109413fed71e26e97a Mon Sep 17 00:00:00 2001 From: Arnau Sanchez Date: Mon, 6 Jul 2020 13:00:53 +0200 Subject: [PATCH 06/16] Add d2-docker start --deploy-path option --- README.md | 17 +++++++++-------- src/d2_docker/commands/start.py | 4 ++++ src/d2_docker/config/dhis2-core-start.sh | 7 ++++--- src/d2_docker/docker-compose.yml | 3 ++- src/d2_docker/utils.py | 4 +++- 5 files changed, 22 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index fa197f0..0c6e477 100644 --- a/README.md +++ b/README.md @@ -76,10 +76,11 @@ Some notes: - By default, the image `dhis2-core` from the same organisation will be used, keeping the first part of the tag (using `-` as separator). For example: `eyeseetea/dhis2-data:2.30-sierra` will use core `eyeseetea/dhis2-core:2.30`. If you need a custom image to be used, use `--core-image= eyeseetea/dhis2-core:2.30-custom`. - Once started, you can connect to the DHIS2 instance (`http://localhost:PORT`) where _PORT_ is the first available port starting from 8080. You can run many images at the same time, but not the same image more than once. You can specify the port with option `-p PORT`. - Use option `--pull` to overwrite the local images with the images in the hub. -- Use option `--detach` to run containers in the background. -- Use option `-k`/`--keep-containers` to re-use existing containers, so data from the previous run will be kept. +- Use option `--detach` to run the container in the background. +- Use option `--deploy-path` to run the container with a deploy path namespace (i.e: `--deploy-path=dhis2` serves `http://localhost:8080/dhis2`) +- Use option `-k`/`--keep-containers` to re-use existing docker containers, so data from the previous run will be kept. - Use option `--run-sql=DIRECTORY` to run SQL files (.sql, .sql.gz or .dump files) after the DB has been initialized. -- Use option `--run-scripts=DIRECTORY` to run shell scripts (.sh) from a directory within the `dhis2-core` container. By default, a script is run **after** postgres starts (`host=db`, `port=5432`) but **before** Tomcat starts; if its filename starts with prefix "post", it will be run **after** Tomcat is available. `curl` and typical shell tools are available on that Alpine Linux environment. Note that the Dhis2 endpoint is always `http://localhost:8080`, regardless of the public port that the instance is exposed to. Of course, this endpoint is only available for post-scripts. +- Use option `--run-scripts=DIRECTORY` to run shell scripts (.sh) from a directory within the `dhis2-core` container. By default, a script is run **after** postgres starts (`host=db`, `port=5432`) but **before** Tomcat starts; if its filename starts with prefix "post", it will be run **after** Tomcat is available. `curl` and typical shell tools are available on that Alpine Linux environment. Note that the Dhis2 endpoint is always `http://localhost:8080/${deployPath}`, regardless of the public port that the instance is exposed to. ### Show logs for running containers @@ -214,11 +215,11 @@ $ d2-docker upgrade \ Migration folder `upgrade-sierra` should then contain data to be used in each intermediate upgrade version. Supported migration data: -- DHIS2 war file: `dhis.war` (if not specified, it will be download from the releases page) -- DHIS2 home files: `dhis2-home/` -- Shell scripts (pre-tomcat): `*.sh` -- Shell scripts (post-tomcat): `post-*.sh` -- SQL files: `*.sql` +- DHIS2 war file: `dhis.war` (if not specified, it will be download from the releases page) +- DHIS2 home files: `dhis2-home/` +- Shell scripts (pre-tomcat): `*.sh` +- Shell scripts (post-tomcat): `post-*.sh` +- SQL files: `*.sql` A full example might look: diff --git a/src/d2_docker/commands/start.py b/src/d2_docker/commands/start.py index f8e367b..0997394 100644 --- a/src/d2_docker/commands/start.py +++ b/src/d2_docker/commands/start.py @@ -25,6 +25,7 @@ def setup(parser): ) parser.add_argument("--pull", action="store_true", help="Force a pull from docker hub") parser.add_argument("-p", "--port", type=int, metavar="N", help="Set Dhis2 instance port") + parser.add_argument("--deploy-path", type=str, help="Set Tomcat context.path") def run(args): @@ -73,6 +74,8 @@ def start(args, image_name): bool, ["--force-recreate" if override_containers else None, "-d" if args.detach else None] ) + deploy_path = ("/" + re.sub("^/*", "", args.deploy_path) if args.deploy_path else "") + with utils.stop_docker_on_interrupt(image_name, core_image): utils.run_docker_compose( ["up", *up_args], @@ -82,6 +85,7 @@ def start(args, image_name): load_from_data=override_containers, post_sql_dir=args.run_sql, scripts_dir=args.run_scripts, + deploy_path=deploy_path ) if args.detach: diff --git a/src/d2_docker/config/dhis2-core-start.sh b/src/d2_docker/config/dhis2-core-start.sh index a448580..422af7e 100755 --- a/src/d2_docker/config/dhis2-core-start.sh +++ b/src/d2_docker/config/dhis2-core-start.sh @@ -9,11 +9,12 @@ set -e -u -o pipefail # - Run post-tomcat shell scripts # -# Global LOAD_FROM_DATA="yes" | "no" +# Global: LOAD_FROM_DATA="yes" | "no" +# Global: DEPLOY_PATH=string export PGPASSWORD="dhis" -dhis2_url="http://localhost:8080" +dhis2_url="http://localhost:8080/$DEPLOY_PATH" psql_cmd="psql -v ON_ERROR_STOP=0 --quiet -h db -U dhis dhis2" pgrestore_cmd="pg_restore -h db -U dhis -d dhis2" configdir="/config" @@ -92,7 +93,7 @@ start_tomcat() { wait_for_tomcat() { debug "Waiting for Tomcat to start: $dhis2_url" - while ! curl -sS -i "$dhis2_url" 2>/dev/null | grep "Location: .*redirect.action"; do + while ! curl -sS -i "$dhis2_url" 2>/dev/null | grep "^Location"; do sleep 1 done } diff --git a/src/d2_docker/docker-compose.yml b/src/d2_docker/docker-compose.yml index c90a685..7af47f8 100644 --- a/src/d2_docker/docker-compose.yml +++ b/src/d2_docker/docker-compose.yml @@ -11,9 +11,10 @@ services: - "${SCRIPTS_DIR}:/data/scripts" - "${POST_SQL_DIR}:/data/db/post" environment: - CATALINA_OPTS: "-Dcontext.path=" + CATALINA_OPTS: "-Dcontext.path=${DEPLOY_PATH}" JAVA_OPTS: "-Xmx7500m -Xms4000m" LOAD_FROM_DATA: "${LOAD_FROM_DATA}" + DEPLOY_PATH: "${DEPLOY_PATH}" entrypoint: sh /config/dhis2-core-entrypoint.sh command: sh /config/dhis2-core-start.sh depends_on: diff --git a/src/d2_docker/utils.py b/src/d2_docker/utils.py index 2a2d723..16f9ea7 100644 --- a/src/d2_docker/utils.py +++ b/src/d2_docker/utils.py @@ -152,7 +152,7 @@ def get_image_status(image_name, first_port=8080): if len(parts) != 3: continue image_name_part, container_name, ports = parts - indexed_service = container_name.split("_")[-2:] + indexed_service = container_name.split("_", 2)[-2:] if image_name_part == final_image_name and indexed_service: service = indexed_service[0] containers[service] = container_name @@ -201,6 +201,7 @@ def run_docker_compose( load_from_data=True, post_sql_dir=None, scripts_dir=None, + deploy_path="", **kwargs, ): """ @@ -224,6 +225,7 @@ def run_docker_compose( # Set default values for directory, required by docker-compose volumes section ("POST_SQL_DIR", post_sql_dir_abs), ("SCRIPTS_DIR", scripts_dir_abs), + ("DEPLOY_PATH", deploy_path), ] env = dict((k, v) for (k, v) in [pair for pair in env_pairs if pair] if v) From 5fcfd210da0bbbc50f2a8c61a382a2da469edb19 Mon Sep 17 00:00:00 2001 From: Arnau Sanchez Date: Mon, 6 Jul 2020 14:32:08 +0200 Subject: [PATCH 07/16] Add option start --tomcat-server-xml=SERVER.XML --- src/d2_docker/commands/start.py | 2 ++ src/d2_docker/config/dhis2-core-start.sh | 5 ++++- src/d2_docker/config/server.xml | 12 +++++++++--- src/d2_docker/docker-compose.yml | 1 + src/d2_docker/utils.py | 12 +++++++++++- 5 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/d2_docker/commands/start.py b/src/d2_docker/commands/start.py index f8e367b..c5deafc 100644 --- a/src/d2_docker/commands/start.py +++ b/src/d2_docker/commands/start.py @@ -17,6 +17,7 @@ def setup(parser): parser.add_argument( "-k", "--keep-containers", action="store_true", help="Keep existing containers" ) + parser.add_argument("--tomcat-server-xml", metavar="FILE", help="Use custom Tomcat server.xml") parser.add_argument("--run-sql", metavar="DIRECTORY", help="Run .sql[.gz] files in directory") parser.add_argument( "--run-scripts", @@ -82,6 +83,7 @@ def start(args, image_name): load_from_data=override_containers, post_sql_dir=args.run_sql, scripts_dir=args.run_scripts, + tomcat_server=args.tomcat_server_xml ) if args.detach: diff --git a/src/d2_docker/config/dhis2-core-start.sh b/src/d2_docker/config/dhis2-core-start.sh index a448580..8d9abcc 100755 --- a/src/d2_docker/config/dhis2-core-start.sh +++ b/src/d2_docker/config/dhis2-core-start.sh @@ -23,6 +23,7 @@ root_db_path="/data/db" post_db_path="/data/db/post" source_apps_path="/data/apps" dest_apps_path="/DHIS2_home/files/" +tomcat_conf_dir="/usr/local/tomcat/conf" debug() { echo "[dhis2-core-start] $*" >&2 @@ -73,9 +74,11 @@ copy_apps() { } setup_tomcat() { + debug "Setup tomcat" cp -v "$configdir/DHIS2_home/dhis.conf" /DHIS2_home/dhis.conf cp -v $homedir/* /DHIS2_home/ || true - cp -v "$configdir/server.xml" /usr/local/tomcat/conf/server.xml + cp -v "$configdir/server.xml" "$tomcat_conf_dir/server.xml" + find "$configdir/override/tomcat/" -maxdepth 1 -type f -size +0 -exec cp -v {} "$tomcat_conf_dir/" \; } wait_for_postgres() { diff --git a/src/d2_docker/config/server.xml b/src/d2_docker/config/server.xml index 86e2951..3e9806e 100644 --- a/src/d2_docker/config/server.xml +++ b/src/d2_docker/config/server.xml @@ -18,14 +18,20 @@ - - + diff --git a/src/d2_docker/docker-compose.yml b/src/d2_docker/docker-compose.yml index c90a685..ef8492b 100644 --- a/src/d2_docker/docker-compose.yml +++ b/src/d2_docker/docker-compose.yml @@ -8,6 +8,7 @@ services: - home:/DHIS2_home - ./config:/config - data:/data + - "${TOMCAT_SERVER}:/config/override/tomcat/server.xml" - "${SCRIPTS_DIR}:/data/scripts" - "${POST_SQL_DIR}:/data/db/post" environment: diff --git a/src/d2_docker/utils.py b/src/d2_docker/utils.py index 2a2d723..ab42292 100644 --- a/src/d2_docker/utils.py +++ b/src/d2_docker/utils.py @@ -201,6 +201,7 @@ def run_docker_compose( load_from_data=True, post_sql_dir=None, scripts_dir=None, + tomcat_server=None, **kwargs, ): """ @@ -224,6 +225,7 @@ def run_docker_compose( # Set default values for directory, required by docker-compose volumes section ("POST_SQL_DIR", post_sql_dir_abs), ("SCRIPTS_DIR", scripts_dir_abs), + ("TOMCAT_SERVER", get_abs_file_for_docker_volume(tomcat_server)), ] env = dict((k, v) for (k, v) in [pair for pair in env_pairs if pair] if v) @@ -232,7 +234,7 @@ def run_docker_compose( def get_absdir_for_docker_volume(directory): - """Return absolute path for given directory, with default fallback.""" + """Return absolute path for given directory, with fallback to empty directory.""" if not directory: empty_directory = os.path.join(os.path.dirname(__file__), ".empty") return empty_directory @@ -242,6 +244,14 @@ def get_absdir_for_docker_volume(directory): return os.path.abspath(directory) +def get_abs_file_for_docker_volume(file_path): + """Return absolute path for given file, with fallback to empty file.""" + if not file_path: + return os.path.join(os.path.dirname(__file__), ".empty", "placeholder") + else: + return os.path.abspath(file_path) + + def get_item_type(name): """ Return "docker-image" if name matches the pattern 'ORG/{DHIS2_DATA_IMAGE}:TAG', From e3e84de25b494190fbb5237a3fafcfe5a91cb9c8 Mon Sep 17 00:00:00 2001 From: Arnau Sanchez Date: Tue, 7 Jul 2020 09:29:20 +0200 Subject: [PATCH 08/16] Add section 'Custom Tomcat server.xml' to readme --- README.md | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index fa197f0..ff9789e 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,36 @@ Some notes: - Use option `--run-sql=DIRECTORY` to run SQL files (.sql, .sql.gz or .dump files) after the DB has been initialized. - Use option `--run-scripts=DIRECTORY` to run shell scripts (.sh) from a directory within the `dhis2-core` container. By default, a script is run **after** postgres starts (`host=db`, `port=5432`) but **before** Tomcat starts; if its filename starts with prefix "post", it will be run **after** Tomcat is available. `curl` and typical shell tools are available on that Alpine Linux environment. Note that the Dhis2 endpoint is always `http://localhost:8080`, regardless of the public port that the instance is exposed to. Of course, this endpoint is only available for post-scripts. +#### Custom Tomcat server.xml + +Copy the default [server.xml](https://github.com/EyeSeeTea/d2-docker/blob/master/src/d2_docker/config/server.xml) and use it as a template to create your own configuration. Then pass it to the `start` command: + +``` +$ d2-docker start --tomcat-server-xml=server-xml.xml ... +``` + +Note that you should not change the catalina connector port (8080, by default). A typical configuration to use _https_ would look like this: + +``` + + ... + + + ... +/> +``` + ### Show logs for running containers Check logs of a running container: @@ -214,11 +244,11 @@ $ d2-docker upgrade \ Migration folder `upgrade-sierra` should then contain data to be used in each intermediate upgrade version. Supported migration data: -- DHIS2 war file: `dhis.war` (if not specified, it will be download from the releases page) -- DHIS2 home files: `dhis2-home/` -- Shell scripts (pre-tomcat): `*.sh` -- Shell scripts (post-tomcat): `post-*.sh` -- SQL files: `*.sql` +- DHIS2 war file: `dhis.war` (if not specified, it will be download from the releases page) +- DHIS2 home files: `dhis2-home/` +- Shell scripts (pre-tomcat): `*.sh` +- Shell scripts (post-tomcat): `post-*.sh` +- SQL files: `*.sql` A full example might look: From c4a3e7cf3fa7c358a3cb419deb8dad6a62cf73e5 Mon Sep 17 00:00:00 2001 From: Arnau Sanchez Date: Wed, 8 Jul 2020 10:13:48 +0200 Subject: [PATCH 09/16] Add d2-docker start --auth=USER:PASS option used in pre/post scripts --- src/d2_docker/commands/start.py | 4 +++- src/d2_docker/config/dhis2-core-start.sh | 6 ++++-- src/d2_docker/docker-compose.yml | 1 + src/d2_docker/utils.py | 2 ++ 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/d2_docker/commands/start.py b/src/d2_docker/commands/start.py index 0997394..f6db112 100644 --- a/src/d2_docker/commands/start.py +++ b/src/d2_docker/commands/start.py @@ -11,6 +11,7 @@ def setup(parser): "image_or_file", metavar="IMAGE_OR_EXPORT_FILE", help="Docker image or exported file" ) utils.add_core_image_arg(parser) + parser.add_argument("--auth", metavar="USER:PASSWORD", help="Dhis2 instance authentication") parser.add_argument( "-d", "--detach", action="store_true", help="Run containers on the background" ) @@ -85,7 +86,8 @@ def start(args, image_name): load_from_data=override_containers, post_sql_dir=args.run_sql, scripts_dir=args.run_scripts, - deploy_path=deploy_path + deploy_path=deploy_path, + dhis2_auth=args.auth ) if args.detach: diff --git a/src/d2_docker/config/dhis2-core-start.sh b/src/d2_docker/config/dhis2-core-start.sh index 422af7e..b4226af 100755 --- a/src/d2_docker/config/dhis2-core-start.sh +++ b/src/d2_docker/config/dhis2-core-start.sh @@ -11,10 +11,12 @@ set -e -u -o pipefail # Global: LOAD_FROM_DATA="yes" | "no" # Global: DEPLOY_PATH=string +# Global: DHIS2_AUTH=string export PGPASSWORD="dhis" dhis2_url="http://localhost:8080/$DEPLOY_PATH" +dhis2_url_with_auth="http://$DHIS2_AUTH@localhost:8080/$DEPLOY_PATH" psql_cmd="psql -v ON_ERROR_STOP=0 --quiet -h db -U dhis dhis2" pgrestore_cmd="pg_restore -h db -U dhis -d dhis2" configdir="/config" @@ -54,14 +56,14 @@ run_sql_files() { run_pre_scripts() { find "$scripts_dir" -type f -name '*.sh' ! \( -name 'post*' \) | sort | while read -r path; do debug "Run pre-tomcat script: $path" - (cd "$(dirname "$path")" && bash "$path") + (cd "$(dirname "$path")" && bash -x "$path" "$dhis2_url_with_auth") done } run_post_scripts() { find "$scripts_dir" -type f -name '*.sh' -name 'post*' | sort | while read -r path; do debug "Run post-tomcat script: $path" - (cd "$(dirname "$path")" && bash "$path") + (cd "$(dirname "$path")" && bash -x "$path" "$dhis2_url_with_auth") done } diff --git a/src/d2_docker/docker-compose.yml b/src/d2_docker/docker-compose.yml index 7af47f8..b2d396f 100644 --- a/src/d2_docker/docker-compose.yml +++ b/src/d2_docker/docker-compose.yml @@ -15,6 +15,7 @@ services: JAVA_OPTS: "-Xmx7500m -Xms4000m" LOAD_FROM_DATA: "${LOAD_FROM_DATA}" DEPLOY_PATH: "${DEPLOY_PATH}" + DHIS2_AUTH: "${DHIS2_AUTH}" entrypoint: sh /config/dhis2-core-entrypoint.sh command: sh /config/dhis2-core-start.sh depends_on: diff --git a/src/d2_docker/utils.py b/src/d2_docker/utils.py index 16f9ea7..de91551 100644 --- a/src/d2_docker/utils.py +++ b/src/d2_docker/utils.py @@ -202,6 +202,7 @@ def run_docker_compose( post_sql_dir=None, scripts_dir=None, deploy_path="", + dhis2_auth=None, **kwargs, ): """ @@ -226,6 +227,7 @@ def run_docker_compose( ("POST_SQL_DIR", post_sql_dir_abs), ("SCRIPTS_DIR", scripts_dir_abs), ("DEPLOY_PATH", deploy_path), + ("DHIS2_AUTH", dhis2_auth), ] env = dict((k, v) for (k, v) in [pair for pair in env_pairs if pair] if v) From eed37ec4338ee144091f00dcda2b4ff061b290cb Mon Sep 17 00:00:00 2001 From: Arnau Sanchez Date: Wed, 8 Jul 2020 10:28:30 +0200 Subject: [PATCH 10/16] Don't pass dhis2 url in pre-tomcat scripts --- src/d2_docker/config/dhis2-core-start.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/d2_docker/config/dhis2-core-start.sh b/src/d2_docker/config/dhis2-core-start.sh index b4226af..59cb500 100755 --- a/src/d2_docker/config/dhis2-core-start.sh +++ b/src/d2_docker/config/dhis2-core-start.sh @@ -56,7 +56,7 @@ run_sql_files() { run_pre_scripts() { find "$scripts_dir" -type f -name '*.sh' ! \( -name 'post*' \) | sort | while read -r path; do debug "Run pre-tomcat script: $path" - (cd "$(dirname "$path")" && bash -x "$path" "$dhis2_url_with_auth") + (cd "$(dirname "$path")" && bash -x "$path") done } From 627fe6c0c73f85613a9828b2884494924243df98 Mon Sep 17 00:00:00 2001 From: Arnau Sanchez Date: Wed, 8 Jul 2020 11:06:16 +0200 Subject: [PATCH 11/16] Add d2-docker start --auth docs --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 0c6e477..eaeb654 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,7 @@ Some notes: - Use option `--detach` to run the container in the background. - Use option `--deploy-path` to run the container with a deploy path namespace (i.e: `--deploy-path=dhis2` serves `http://localhost:8080/dhis2`) - Use option `-k`/`--keep-containers` to re-use existing docker containers, so data from the previous run will be kept. +- Use option `-auth` to pass the instance authentication (`USER:PASS`). It will be used to call post-tomcat scripts. - Use option `--run-sql=DIRECTORY` to run SQL files (.sql, .sql.gz or .dump files) after the DB has been initialized. - Use option `--run-scripts=DIRECTORY` to run shell scripts (.sh) from a directory within the `dhis2-core` container. By default, a script is run **after** postgres starts (`host=db`, `port=5432`) but **before** Tomcat starts; if its filename starts with prefix "post", it will be run **after** Tomcat is available. `curl` and typical shell tools are available on that Alpine Linux environment. Note that the Dhis2 endpoint is always `http://localhost:8080/${deployPath}`, regardless of the public port that the instance is exposed to. From fdeb09dd32911dd0d3283f050b0d1b85292f655f Mon Sep 17 00:00:00 2001 From: Arnau Sanchez Date: Tue, 14 Jul 2020 12:10:52 +0200 Subject: [PATCH 12/16] Show path to template server.xml on d2-docker start --help --- src/d2_docker/commands/start.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/d2_docker/commands/start.py b/src/d2_docker/commands/start.py index c5deafc..8f8e908 100644 --- a/src/d2_docker/commands/start.py +++ b/src/d2_docker/commands/start.py @@ -1,12 +1,17 @@ import os import re +import d2_docker from d2_docker import utils DESCRIPTION = "Start a container from an existing dhis2-data Docker image or from an exported file" def setup(parser): + d2_docker_path = os.path.abspath(d2_docker.__path__[0]) + server_xml_path = os.path.join(d2_docker_path, "config", "server.xml") + server_xml_help = "Use a custom Tomcat server.xml file. Template: {0}".format(server_xml_path) + parser.add_argument( "image_or_file", metavar="IMAGE_OR_EXPORT_FILE", help="Docker image or exported file" ) @@ -17,7 +22,7 @@ def setup(parser): parser.add_argument( "-k", "--keep-containers", action="store_true", help="Keep existing containers" ) - parser.add_argument("--tomcat-server-xml", metavar="FILE", help="Use custom Tomcat server.xml") + parser.add_argument("--tomcat-server-xml", metavar="FILE", help=server_xml_help) parser.add_argument("--run-sql", metavar="DIRECTORY", help="Run .sql[.gz] files in directory") parser.add_argument( "--run-scripts", From 7ac4920e9ae6668abb48c8f454d431e5ce9fb667 Mon Sep 17 00:00:00 2001 From: Ignacio Foche Perez Date: Wed, 15 Jul 2020 09:46:55 +0200 Subject: [PATCH 13/16] merged with development --- src/d2_docker/commands/start.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/d2_docker/commands/start.py b/src/d2_docker/commands/start.py index 1e690c2..f4b37ec 100644 --- a/src/d2_docker/commands/start.py +++ b/src/d2_docker/commands/start.py @@ -93,7 +93,7 @@ def start(args, image_name): post_sql_dir=args.run_sql, scripts_dir=args.run_scripts, deploy_path=deploy_path, - dhis2_auth=args.auth + dhis2_auth=args.auth, tomcat_server=args.tomcat_server_xml ) From 117f669edd071fa8cfe102bc6fc19c797ee993b2 Mon Sep 17 00:00:00 2001 From: Arnau Sanchez Date: Wed, 15 Jul 2020 09:48:39 +0200 Subject: [PATCH 14/16] run_docker_compose pass empty strings --- src/d2_docker/utils.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/d2_docker/utils.py b/src/d2_docker/utils.py index 16f9ea7..12e8201 100644 --- a/src/d2_docker/utils.py +++ b/src/d2_docker/utils.py @@ -201,7 +201,7 @@ def run_docker_compose( load_from_data=True, post_sql_dir=None, scripts_dir=None, - deploy_path="", + deploy_path=None, **kwargs, ): """ @@ -225,9 +225,9 @@ def run_docker_compose( # Set default values for directory, required by docker-compose volumes section ("POST_SQL_DIR", post_sql_dir_abs), ("SCRIPTS_DIR", scripts_dir_abs), - ("DEPLOY_PATH", deploy_path), + ("DEPLOY_PATH", deploy_path or ""), ] - env = dict((k, v) for (k, v) in [pair for pair in env_pairs if pair] if v) + env = dict((k, v) for (k, v) in [pair for pair in env_pairs if pair] if v is not None) yaml_path = os.path.join(os.path.dirname(__file__), "docker-compose.yml") return run(["docker-compose", "-f", yaml_path, "-p", project_name, *args], env=env, **kwargs) From 629edab0eacfac9e5595a86f154934ac0ca1c1e9 Mon Sep 17 00:00:00 2001 From: Ignacio Foche Perez Date: Wed, 15 Jul 2020 10:12:56 +0200 Subject: [PATCH 15/16] version bumped --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 4710ee5..eed93e9 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ setuptools.setup( name="d2_docker", - version="1.0.7", + version="1.0.8", description="Dockers for DHIS2 instances", long_description=open("README.md", encoding="utf-8").read(), keywords=["python"], From 875205102b70d07b94343ca6feb1b4517103007d Mon Sep 17 00:00:00 2001 From: Arnau Sanchez Date: Thu, 16 Jul 2020 15:14:17 +0200 Subject: [PATCH 16/16] Explicit recursive-include in manifest to install .empty directory --- MANIFEST.in | 1 + 1 file changed, 1 insertion(+) diff --git a/MANIFEST.in b/MANIFEST.in index 4e8eb5a..35a95ed 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -4,3 +4,4 @@ include requirements.txt include src/d2_docker/docker-compose.yml recursive-include src/d2_docker/config * recursive-include src/d2_docker/images * +recursive-include src/d2_docker/.empty *