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

Update setup-services.py to include images & volumes subtleties #651

Merged
merged 4 commits into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .ci_helpers/docker/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ services:
- 9000:9000
networks:
- echopypenetwork
volumes:
- echopype_miniodata:/data
httpserver:
# NOTE ====================================
# For httpserver test data,
Expand All @@ -20,3 +22,6 @@ services:
- echopypenetwork
networks:
echopypenetwork:

volumes:
echopype_miniodata:
106 changes: 84 additions & 22 deletions .ci_helpers/docker/setup-services.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@

"""
import argparse
import os
import logging
import shutil
import subprocess
import sys
from pathlib import Path
from typing import Dict, List

logger = logging.getLogger("setup-services")
streamHandler = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter("%(message)s")
streamHandler.setFormatter(formatter)
logger.addHandler(streamHandler)
logger.setLevel(level=logging.INFO)

HERE = Path(".").absolute()
BASE = Path(__file__).parent.absolute()
Expand All @@ -18,17 +27,44 @@
def parse_args():
parser = argparse.ArgumentParser(description="Setup services for testing")
parser.add_argument("--deploy", action="store_true", help="Flag to setup docker services")
parser.add_argument(
"--no-pull",
action="store_true",
help="Optional flag to skip pulling the latest images from dockerhub",
)
parser.add_argument(
"--tear-down",
action="store_true",
help="Flag to tear down docker services",
)
parser.add_argument(
"--images",
action="store_true",
help="Optional flag to remove images also during tear down",
)

return parser.parse_args()


def run_commands(commands: List[Dict]) -> None:
for idx, command in enumerate(commands, start=1):
msg = command.get("msg")
cmd = command.get("cmd")
args = command.get("args", None)
logger.info(f"{idx}) {msg}")
if cmd is None:
continue
elif isinstance(cmd, list):
subprocess.run(cmd)
elif callable(cmd):
cmd(args)
else:
raise ValueError(f"command of {type(cmd)} is invalid.")


if __name__ == "__main__":
args = parse_args()
commands = []
if all([args.deploy, args.tear_down]):
print("Cannot have both --deploy and --tear-down. Exiting.")
sys.exit(1)
Expand All @@ -38,29 +74,55 @@ def parse_args():
sys.exit(0)

if args.deploy:
print("1) Starting test services deployment.")

print("2) Pulling latest images.")
os.system(f"docker-compose -f {COMPOSE_FILE} pull")

print("3) Bringing up services.")
os.system(f"docker-compose -f {COMPOSE_FILE} up -d --remove-orphans --force-recreate")
commands.append({"msg": "Starting test services deployment ...", "cmd": None})
if not args.no_pull:
commands.append(
{
"msg": "Pulling latest images ...",
"cmd": ["docker-compose", "-f", COMPOSE_FILE, "pull"],
}
)
commands.append(
{
"msg": "Bringing up services ...",
"cmd": [
"docker-compose",
"-f",
COMPOSE_FILE,
"up",
"-d",
"--remove-orphans",
"--force-recreate",
],
}
)

print(f"4) Deleting old test folder at {TEST_DATA_PATH}")
if TEST_DATA_PATH.exists():
print("SKIPPED.")
shutil.rmtree(TEST_DATA_PATH)

print("5) Copying new test folder from http service")
os.system(
f"docker cp -L docker_httpserver_1:/usr/local/apache2/htdocs/data {TEST_DATA_PATH}"
commands.append(
{
"msg": f"Deleting old test folder at {TEST_DATA_PATH} ...",
"cmd": shutil.rmtree,
"args": TEST_DATA_PATH,
}
)

commands.append(
{
"msg": "Copying new test folder from http service ...",
"cmd": [
"docker",
"cp",
"-L",
"docker_httpserver_1:/usr/local/apache2/htdocs/data",
TEST_DATA_PATH,
],
}
)

print("6) Done.")
os.system("docker ps --last 2")

if args.tear_down:
print("1) Stopping test services deployment.")
os.system(f"docker-compose -f {COMPOSE_FILE} down --remove-orphans")
print("2) Done.")
os.system("docker ps --last 2")
command = ["docker-compose", "-f", COMPOSE_FILE, "down", "--remove-orphans", "--volumes"]
if args.images:
command = command + ["--rmi", "all"]
commands.append({"msg": "Stopping test services deployment ...", "cmd": command})
commands.append({"msg": "Done.", "cmd": ["docker", "ps", "--last", "2"]})
run_commands(commands)