From 5407961613d210f0188566df01c9f4172bf8c8ea Mon Sep 17 00:00:00 2001 From: Thomas E Lackey Date: Wed, 15 Nov 2023 01:39:56 -0600 Subject: [PATCH 1/4] Add run-webapp command. --- stack_orchestrator/deploy/compose/deploy_docker.py | 5 +++-- stack_orchestrator/deploy/deployer.py | 2 +- stack_orchestrator/deploy/k8s/deploy_k8s.py | 2 +- stack_orchestrator/main.py | 2 ++ 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/stack_orchestrator/deploy/compose/deploy_docker.py b/stack_orchestrator/deploy/compose/deploy_docker.py index 79ab1482..b37f3cf0 100644 --- a/stack_orchestrator/deploy/compose/deploy_docker.py +++ b/stack_orchestrator/deploy/compose/deploy_docker.py @@ -61,9 +61,10 @@ class DockerDeployer(Deployer): except DockerException as e: raise DeployerException(e) - def run(self, image, command, user, volumes, entrypoint=None): + def run(self, image: str, command=None, user=None, volumes=None, entrypoint=None, env={}, detach=False): try: - return self.docker.run(image=image, command=command, user=user, volumes=volumes, entrypoint=entrypoint) + return self.docker.run(image=image, command=command, user=user, volumes=volumes, + entrypoint=entrypoint, envs=env, detach=detach, publish_all=True) except DockerException as e: raise DeployerException(e) diff --git a/stack_orchestrator/deploy/deployer.py b/stack_orchestrator/deploy/deployer.py index 68b0088a..79379c3d 100644 --- a/stack_orchestrator/deploy/deployer.py +++ b/stack_orchestrator/deploy/deployer.py @@ -44,7 +44,7 @@ class Deployer(ABC): pass @abstractmethod - def run(self, image, command, user, volumes, entrypoint): + def run(self, image: str, command=None, user=None, volumes=None, entrypoint=None, env={}, detach=False): pass diff --git a/stack_orchestrator/deploy/k8s/deploy_k8s.py b/stack_orchestrator/deploy/k8s/deploy_k8s.py index bc256b6b..627d6e0b 100644 --- a/stack_orchestrator/deploy/k8s/deploy_k8s.py +++ b/stack_orchestrator/deploy/k8s/deploy_k8s.py @@ -120,7 +120,7 @@ class K8sDeployer(Deployer): log_data = self.core_api.read_namespaced_pod_log(k8s_pod_name, namespace="default", container="test") return log_stream_from_string(log_data) - def run(self, image, command, user, volumes, entrypoint=None): + def run(self, image: str, command=None, user=None, volumes=None, entrypoint=None, env={}, detach=False): # We need to figure out how to do this -- check why we're being called first pass diff --git a/stack_orchestrator/main.py b/stack_orchestrator/main.py index 0b0585e0..8ee8ae61 100644 --- a/stack_orchestrator/main.py +++ b/stack_orchestrator/main.py @@ -20,6 +20,7 @@ from stack_orchestrator.repos import setup_repositories from stack_orchestrator.build import build_containers from stack_orchestrator.build import build_npms from stack_orchestrator.build import build_webapp +from stack_orchestrator.deploy import run_webapp from stack_orchestrator.deploy import deploy from stack_orchestrator import version from stack_orchestrator.deploy import deployment @@ -50,6 +51,7 @@ cli.add_command(setup_repositories.command, "setup-repositories") cli.add_command(build_containers.command, "build-containers") cli.add_command(build_npms.command, "build-npms") cli.add_command(build_webapp.command, "build-webapp") +cli.add_command(run_webapp.command, "run-webapp") cli.add_command(deploy.command, "deploy") # deploy is an alias for deploy-system cli.add_command(deploy.command, "deploy-system") cli.add_command(deployment.command, "deployment") -- 2.45.2 From c54db5f326343d1408298b5297d766daeff975a7 Mon Sep 17 00:00:00 2001 From: Thomas E Lackey Date: Wed, 15 Nov 2023 01:40:56 -0600 Subject: [PATCH 2/4] missing file --- stack_orchestrator/deploy/run_webapp.py | 67 +++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 stack_orchestrator/deploy/run_webapp.py diff --git a/stack_orchestrator/deploy/run_webapp.py b/stack_orchestrator/deploy/run_webapp.py new file mode 100644 index 00000000..a8f52c1a --- /dev/null +++ b/stack_orchestrator/deploy/run_webapp.py @@ -0,0 +1,67 @@ +# Copyright © 2022, 2023 Vulcanize + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. + +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +# Builds webapp containers + +# env vars: +# CERC_REPO_BASE_DIR defaults to ~/cerc + +# TODO: display the available list of containers; allow re-build of either all or specific containers + +import hashlib +import click + +from dotenv import dotenv_values +from stack_orchestrator.deploy.deployer_factory import getDeployer + + +@click.command() +@click.option("--image", help="image to deploy", required=True) +@click.option("--deploy-to", default="compose", help="deployment type ([Docker] 'compose' or 'k8s')") +@click.option("--env-file", help="environment file for webapp") +@click.pass_context +def command(ctx, image, deploy_to, env_file): + '''build the specified webapp container''' + + quiet = ctx.obj.quiet + verbose = ctx.obj.verbose + dry_run = ctx.obj.dry_run + debug = ctx.obj.debug + local_stack = ctx.obj.local_stack + stack = ctx.obj.stack + continue_on_error = ctx.obj.continue_on_error + + env = {} + if env_file: + env = dotenv_values(env_file) + + unique_cluster_descriptor = f"{image},{env}" + hash = hashlib.md5(unique_cluster_descriptor.encode()).hexdigest() + cluster = f"laconic-webapp-{hash}" + + deployer = getDeployer(deploy_to, + deployment_dir=None, + compose_files=None, + compose_project_name=cluster, + compose_env_file=None) + + container = deployer.run(image, command=[], user=None, volumes=[], entrypoint=None, env=env, detach=True) + + # Make configurable? + webappPort = "3000/tcp" + # TODO: This assumes a Docker container object... + if webappPort in container.network_settings.ports: + mapping = container.network_settings.ports[webappPort][0] + print(f"""Image: {image}\nID: {container.id}\nURL: http://localhost:{mapping['HostPort']}""") -- 2.45.2 From 49cc8974eed6e3258c397a9da12f1b44719e8611 Mon Sep 17 00:00:00 2001 From: Thomas E Lackey Date: Wed, 15 Nov 2023 01:45:34 -0600 Subject: [PATCH 3/4] Update instructions --- .../data/container-build/cerc-nextjs-base/build.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stack_orchestrator/data/container-build/cerc-nextjs-base/build.sh b/stack_orchestrator/data/container-build/cerc-nextjs-base/build.sh index 342dd3cd..cca8d64b 100755 --- a/stack_orchestrator/data/container-build/cerc-nextjs-base/build.sh +++ b/stack_orchestrator/data/container-build/cerc-nextjs-base/build.sh @@ -23,7 +23,7 @@ Built host container for $CERC_CONTAINER_BUILD_WORK_DIR with tag: To test locally run: - docker run -p 3000:3000 --env-file /path/to/environment.env $CERC_CONTAINER_BUILD_TAG + laconic-so run-webapp --image $CERC_CONTAINER_BUILD_TAG --env-file /path/to/environment.env EOF fi -- 2.45.2 From bd91a6014490ec3f0d96f599fd142b787697cd74 Mon Sep 17 00:00:00 2001 From: David Boreham Date: Wed, 15 Nov 2023 10:39:06 -0700 Subject: [PATCH 4/4] Fix lint errors --- stack_orchestrator/deploy/run_webapp.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/stack_orchestrator/deploy/run_webapp.py b/stack_orchestrator/deploy/run_webapp.py index a8f52c1a..8b1073b1 100644 --- a/stack_orchestrator/deploy/run_webapp.py +++ b/stack_orchestrator/deploy/run_webapp.py @@ -35,14 +35,6 @@ from stack_orchestrator.deploy.deployer_factory import getDeployer def command(ctx, image, deploy_to, env_file): '''build the specified webapp container''' - quiet = ctx.obj.quiet - verbose = ctx.obj.verbose - dry_run = ctx.obj.dry_run - debug = ctx.obj.debug - local_stack = ctx.obj.local_stack - stack = ctx.obj.stack - continue_on_error = ctx.obj.continue_on_error - env = {} if env_file: env = dotenv_values(env_file) -- 2.45.2