Add support for persisting webapp data

This commit is contained in:
Prathamesh Musale 2025-02-07 11:20:59 +05:30
parent 873a6d472c
commit 5689eb8695
4 changed files with 33 additions and 4 deletions

View File

@ -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:

View File

@ -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

View File

@ -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)

View File

@ -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)