Files
spark/tools/py/deploy.py
T
John Colvin 44f9400499 Incorporate env files into docker compose
In order to support local (ephemeral) config. For example, to run sparkd
with replay md to troubleshoot an issue.
2024-11-29 15:05:54 +00:00

127 lines
3.8 KiB
Python

import argparse
import subprocess
import publish_api_server
import publish_data_stream
import publish_sparkd
from core.argparse_utils import add_toggle_opt
from core.file_utils import SparkPaths
def _get_arg_parser():
parser = argparse.ArgumentParser(
description="Deploy sparkd, pubsub, md-publisher, webapp, and api server"
)
parser.add_argument(
"--cmake-jobs",
type=int,
help="The number of cmake parallel jobs",
)
parser.add_argument("--compose-file", help="The docker compose (yaml) file to use")
parser.add_argument(
"--clean",
action="store_true",
help="Perform a clean build (remove artifacts from previous build)",
)
parser.add_argument(
"--env-files", action="append", help=".env files to pass to docker compose"
)
parser.add_argument(
"--verbosity",
metavar="{0-5}",
choices=range(1, 6),
type=int,
help="The output verbosity level",
)
add_toggle_opt(parser, "build", help="Whether or not to build the services first")
parser.add_argument(
"--dry-run",
action="store_true",
help="Preview the build commands without running them",
)
all_services = ["sparkd", "api-server", "pubsub", "md-publisher", "webapp"]
parser.add_argument("services", nargs="*")
parser.set_defaults(
configuration="release",
services=all_services,
build=True,
clean=False,
verbosity=2,
)
return parser
def _get_env_files():
root = SparkPaths.root()
files = [root.joinpath(".env"), root.joinpath(".env.local")]
return list(filter(lambda p: p.exists(), files))
def deploy(args):
def run(cmd):
print(cmd)
if args.dry_run:
return
subprocess.run(cmd, shell=True).check_returncode()
services: list[str] = args.services or []
if args.build:
build_all = len(services) == 0
if build_all or "sparkd" in services:
publish_sparkd.publish(
publish_sparkd.PublishArgs(
configuration="release",
cmake_jobs=args.cmake_jobs,
clean=args.clean,
verbosity=args.verbosity,
dry_run=args.dry_run,
)
)
if build_all or any(
[x in services for x in ["pubsub", "md-publisher", "webapp"]]
):
publish_data_stream.publish(
publish_data_stream.PublishArgs(
configuration="release",
cmake_jobs=args.cmake_jobs,
clean=args.clean,
verbosity=args.verbosity,
dry_run=args.dry_run,
)
)
if build_all or "api-server" in services:
publish_api_server.publish(
publish_api_server.PublishArgs(
configuration="release",
cmake_jobs=args.cmake_jobs,
clean=args.clean,
verbosity=args.verbosity,
dry_run=args.dry_run,
)
)
service_args = " ".join(services)
compose_cmd = "sudo docker compose"
if args.compose_file:
compose_cmd += f" -f {args.compose_file}"
env_files = _get_env_files()
if len(env_files) > 0:
compose_cmd += " " + " ".join([f"--env-file {f}" for f in env_files])
run(f"{compose_cmd} down {service_args}")
run(f"{compose_cmd} build {service_args}")
run(f"{compose_cmd} up {service_args} -d")
run(f"{compose_cmd} logs {service_args} --follow")
def _main():
parser = _get_arg_parser()
args = parser.parse_args()
deploy(args)
if __name__ == "__main__":
try:
_main()
except Exception as e:
print(f"deploy.py failed: {e}")
exit(1)