Skip to content

Commit

Permalink
Add run-webapp command. (#651)
Browse files Browse the repository at this point in the history
  • Loading branch information
telackey authored Nov 15, 2023
1 parent 638fa01 commit 2059d67
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 3 additions & 2 deletions stack_orchestrator/deploy/compose/deploy_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ def logs(self, services, tail, follow, stream):
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)

Expand Down
2 changes: 1 addition & 1 deletion stack_orchestrator/deploy/deployer.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def logs(self, services, tail, follow, stream):
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


Expand Down
2 changes: 1 addition & 1 deletion stack_orchestrator/deploy/k8s/deploy_k8s.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def logs(self, services, tail, follow, stream):
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

Expand Down
59 changes: 59 additions & 0 deletions stack_orchestrator/deploy/run_webapp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# 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 <http:#www.gnu.org/licenses/>.

# 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'''

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 changes: 2 additions & 0 deletions stack_orchestrator/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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
Expand Down Expand Up @@ -50,6 +51,7 @@ def cli(ctx, stack, quiet, verbose, dry_run, local_stack, debug, continue_on_err
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")
Expand Down

0 comments on commit 2059d67

Please sign in to comment.