Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mac Support + refactor psql wait #13

Merged
merged 7 commits into from
Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions run-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/bin/bash -x -e -u -o pipefail

wait_for_dhis2() {
echo "Wait for Dhis2 instance"
while ! curl -u admin:district -sS http://localhost:8080/api/me.json 2>/dev/null | jq 2>/dev/null; do
sleep 1;
done
}

expect_equal() { local actual=$1 expected=$2
if test "$actual" != "$expected"; then
echo "Error:"
echo " expected = $expected"
echo " action = $actual"
return 1;
fi
}

wget -c "https://releases.dhis2.org/2.30/dhis.war"
wget -c "https://github.com/dhis2/dhis2-demo-db/raw/master/sierra-leone/dev/dhis2-db-sierra-leone.sql.gz"
mkdir -p apps/
echo "select * from users;" > get-users.sql
echo "update userinfo set firstname = 'John2' where uid = 'xE7jOejl9FI';" > set-name.sql

d2-docker create core tokland/dhis2-core:2.30 --war=dhis.war
d2-docker create data tokland/dhis2-data:2.30-sierra \
--sql=dhis2-db-sierra-leone.sql.gz --apps-dir=apps/
d2-docker list
d2-docker start -d tokland/dhis2-data:2.30-sierra
wait_for_dhis2

d2-docker list | grep "tokland/dhis2-data:2.30-sierra" | grep "RUNNING\[port=8080\]"
d2-docker logs
name=$(curl -sS -u admin:district 'http://localhost:8080/api/me' | jq -r '.displayName')
expect_equal "$name" "John Traore"
d2-docker run-sql -i tokland/dhis2-data:2.30-sierra get-users.sql
d2-docker commit
#d2-docker push # too slow
d2-docker export image.tgz

d2-docker stop tokland/dhis2-data:2.30-sierra
d2-docker copy tokland/dhis2-data:2.30-sierra data-sierra tokland/dhis2-data:2.30-sierra2
d2-docker copy data-sierra tokland/dhis2-data:2.30-sierra3
d2-docker stop tokland/dhis2-data:2.30-sierra
d2-docker import image.tgz
d2-docker start -d --run-sql=set-name.sql image.tgz
wait_for_dhis2

name=$(curl -sS -u admin:district 'http://localhost:8080/api/me' | jq -r '.displayName')
expect_equal "$name" "John2 Traore"
d2-docker stop tokland/dhis2-data:2.30-sierra

echo "Tests passed!"
Empty file.
2 changes: 1 addition & 1 deletion src/d2_docker/config/dhis2-core-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ if [ "$(id -u)" = "0" ]; then
chown -R tomcat:tomcat $TOMCATDIR/temp \
$TOMCATDIR/work \
$TOMCATDIR/logs

chown -R tomcat:tomcat $DHIS2HOME
chmod +x "$0" || true
exec su-exec tomcat "$0" "$@"
fi
fi
Expand Down
10 changes: 5 additions & 5 deletions src/d2_docker/config/dhis2-core-start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ dhis2_url="http://localhost:8080"
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"
scripts_dir="/config/scripts"
root_db_path="/config/data/db"
post_db_path="/config/data/db/post"
source_apps_path="/config/data/apps"
scripts_dir="/data/scripts"
root_db_path="/data/db"
post_db_path="/data/db/post"
source_apps_path="/data/apps"
dest_apps_path="/DHIS2_home/files/"
warfile="/usr/local/tomcat/webapps/ROOT.war"
tomcatdir="/usr/local/tomcat"
Expand Down Expand Up @@ -72,7 +72,7 @@ setup_tomcat() {

wait_for_postgres() {
debug "Waiting for postgres: ${host}:${psql_port}"
while ! nc -z $host $psql_port; do
while ! echo "select 1;" | $psql_cmd; do
sleep 1
done
}
Expand Down
6 changes: 3 additions & 3 deletions src/d2_docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ services:
- "com.eyeseetea.image-name=${DHIS2_DATA_IMAGE}"
volumes:
- home:/DHIS2_home
- data:/config/data
- ./config:/config
- "${SCRIPTS_DIR}:/config/scripts"
- "${POST_SQL_DIR}:/config/data/db/post"
- data:/data
- "${SCRIPTS_DIR}:/data/scripts"
- "${POST_SQL_DIR}:/data/db/post"
environment:
CATALINA_OPTS: "-Dcontext.path="
JAVA_OPTS: "-Xmx7500m -Xms4000m"
Expand Down
21 changes: 13 additions & 8 deletions src/d2_docker/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def get_running_image_name():
"ps",
"--filter",
"label=" + IMAGE_NAME_LABEL,
'--format={{.Label "com.eyeseetea.image-name"}}',
'--format={{.Label "%s"}}' % IMAGE_NAME_LABEL,
],
capture_output=True,
)
Expand Down Expand Up @@ -123,19 +123,23 @@ def get_image_status(image_name, first_port=8080):
final_image_name = image_name or get_running_image_name()
project_name = get_project_name(final_image_name)
output_lines = run_docker_ps(
["--filter", "label=" + IMAGE_NAME_LABEL, "--format={{.Names}} {{.Ports}}"]
[
"--filter",
"label=" + IMAGE_NAME_LABEL,
'--format={{.Label "%s"}} {{.Names}} {{.Ports}}' % IMAGE_NAME_LABEL,
]
)

containers = {}
port = None

for line in output_lines:
parts = line.split(None, 1)
if len(parts) != 2:
parts = line.split(None, 2)
if len(parts) != 3:
continue
container_name, ports = parts
image_name_part, container_name, ports = parts
indexed_service = container_name.split("_")[-2:]
if indexed_service:
if image_name_part == final_image_name and indexed_service:
service = indexed_service[0]
containers[service] = container_name
if service == "gateway":
Expand Down Expand Up @@ -223,10 +227,11 @@ def run_docker_compose(
return run(["docker-compose", "-f", yaml_path, "-p", project_name, *args], env=env, **kwargs)


def get_absdir_for_docker_volume(directory, default="empty"):
def get_absdir_for_docker_volume(directory):
"""Return absolute path for given directory, with default fallback."""
if not directory:
return default
empty_directory = os.path.join(os.path.dirname(__file__), ".empty")
return empty_directory
elif not os.path.isdir(directory):
raise D2DockerError("Should be a directory: {}".format(directory))
else:
Expand Down