From 5689eb86956fa5be5c3ddee363884fd1a3688aa2 Mon Sep 17 00:00:00 2001 From: Prathamesh Musale Date: Fri, 7 Feb 2025 11:20:59 +0530 Subject: [PATCH] Add support for persisting webapp data --- .../docker-compose-webapp-template-persistent.yml | 13 +++++++++++++ .../stacks/webapp-template-persistent/stack.yml | 7 +++++++ stack_orchestrator/deploy/webapp/deploy_webapp.py | 12 ++++++++---- .../deploy/webapp/deploy_webapp_from_registry.py | 5 +++++ 4 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 stack_orchestrator/data/compose/docker-compose-webapp-template-persistent.yml create mode 100644 stack_orchestrator/data/stacks/webapp-template-persistent/stack.yml diff --git a/stack_orchestrator/data/compose/docker-compose-webapp-template-persistent.yml b/stack_orchestrator/data/compose/docker-compose-webapp-template-persistent.yml new file mode 100644 index 00000000..0867ae32 --- /dev/null +++ b/stack_orchestrator/data/compose/docker-compose-webapp-template-persistent.yml @@ -0,0 +1,13 @@ +services: + webapp: + image: cerc/webapp-container:local + restart: always + environment: + CERC_SCRIPT_DEBUG: ${CERC_SCRIPT_DEBUG} + ports: + - "80" + volumes: + - app-data:/data + +volumes: + app-data: diff --git a/stack_orchestrator/data/stacks/webapp-template-persistent/stack.yml b/stack_orchestrator/data/stacks/webapp-template-persistent/stack.yml new file mode 100644 index 00000000..42b0c1c5 --- /dev/null +++ b/stack_orchestrator/data/stacks/webapp-template-persistent/stack.yml @@ -0,0 +1,7 @@ +version: "1.0" +name: webapp-template-persistent +description: "Same as webapp-template stack, but with persistent data" +containers: + - cerc/webapp-template-container +pods: + - webapp-template-persistent diff --git a/stack_orchestrator/deploy/webapp/deploy_webapp.py b/stack_orchestrator/deploy/webapp/deploy_webapp.py index 4c91dec3..e377ee67 100644 --- a/stack_orchestrator/deploy/webapp/deploy_webapp.py +++ b/stack_orchestrator/deploy/webapp/deploy_webapp.py @@ -25,9 +25,9 @@ from stack_orchestrator.deploy.deploy import create_deploy_context from stack_orchestrator.deploy.deploy_types import DeployCommandContext -def _fixup_container_tag(deployment_dir: str, image: str): +def _fixup_container_tag(deployment_dir: str, pod_name: str, image: str): deployment_dir_path = Path(deployment_dir) - compose_file = deployment_dir_path.joinpath("compose", "docker-compose-webapp-template.yml") + compose_file = deployment_dir_path.joinpath("compose", pod_name) # replace "cerc/webapp-container:local" in the file with our image tag with open(compose_file) as rfile: contents = rfile.read() @@ -54,7 +54,7 @@ def _fixup_url_spec(spec_file_name: str, url: str): wfile.write(contents) -def create_deployment(ctx, deployment_dir, image, url, kube_config, image_registry, env_file): +def create_deployment(ctx, deployment_dir, image, url, kube_config, image_registry, env_file, persist_data=False): # Do the equivalent of: # 1. laconic-so --stack webapp-template deploy --deploy-to k8s init --output webapp-spec.yml # --config (eqivalent of the contents of my-config.env) @@ -70,6 +70,10 @@ def create_deployment(ctx, deployment_dir, image, url, kube_config, image_regist spec_file_name = tf.name # Specify the webapp template stack stack = "webapp-template" + pod_file_name = "docker-compose-webapp-template.yml" + if persist_data: + stack = "webapp-template-persistent" + pod_file_name = "docker-compose-webapp-template-persistent.yml" # TODO: support env file deploy_command_context: DeployCommandContext = create_deploy_context( global_options2(ctx), None, stack, None, None, None, env_file, "k8s" @@ -95,7 +99,7 @@ def create_deployment(ctx, deployment_dir, image, url, kube_config, image_regist None ) # Fix up the container tag inside the deployment compose file - _fixup_container_tag(deployment_dir, image) + _fixup_container_tag(deployment_dir, pod_file_name, image) os.remove(spec_file_name) diff --git a/stack_orchestrator/deploy/webapp/deploy_webapp_from_registry.py b/stack_orchestrator/deploy/webapp/deploy_webapp_from_registry.py index 24a529c2..5d55e43c 100644 --- a/stack_orchestrator/deploy/webapp/deploy_webapp_from_registry.py +++ b/stack_orchestrator/deploy/webapp/deploy_webapp_from_registry.py @@ -137,10 +137,14 @@ def process_app_deployment_request( env.update(app_deployment_request.attributes.config.env) env_filename = None + persist_data = False if env: env_filename = tempfile.mktemp() with open(env_filename, "w") as file: for k, v in env.items(): + if k == "__PERSISTENT_VOLUME__": + persist_data = True if v == "true" else False + continue file.write("%s=%s\n" % (k, shlex.quote(str(v)))) # 5. determine new or existing deployment @@ -180,6 +184,7 @@ def process_app_deployment_request( kube_config, image_registry, env_filename, + persist_data ) elif env_filename: shutil.copyfile(env_filename, deployment_config_file)