added remove command

This commit is contained in:
Lorenzo Venerandi
2025-01-30 14:49:52 +01:00
parent 5668295be3
commit 0b0e4a408c
6 changed files with 126 additions and 12 deletions

View File

@@ -1,9 +1,12 @@
import os
from dotenv import load_dotenv
import src.code_generator.generator as code_generator
import src.wasm_builder.build as wasm_builder
import src.component_deploy.deploy as deployer
import src.component_deploy.remove as remover
import time
class Pelato:
def __init__(self):
@@ -11,6 +14,8 @@ class Pelato:
def setup_vars(self):
load_dotenv()
self.registry_url = os.getenv('REGISTRY_URL')
self.reg_user = os.getenv('REGISTRY_USER')
self.reg_pass = os.getenv('REGISTRY_PASSWORD')
@@ -26,6 +31,9 @@ class Pelato:
def deploy(self, project_dir):
deployer.deploy_components(project_dir, self.nats_host, self.nats_port, self.detached)
def remove(self, project_dir):
remover.remove_components(project_dir, self.nats_host, self.nats_port, self.detached)
def all(self, project_dir):

View File

@@ -46,7 +46,7 @@ def deploy_components(project_dir, nats_host, nats_port, detached):
logging.error(f"Error deploying project: {e}")
return
print("Project built successfully")
print("Project deployed successfully")
def __deploy_wadm(task_dir, client, nats_host, nats_port, detached, wait_list):

View File

@@ -0,0 +1,23 @@
FROM ubuntu:24.04 AS wash-deploy-image
# Install dependencies and tools
RUN apt-get update && apt-get install -y \
curl \
wget \
tar \
git \
build-essential \
&& rm -rf /var/lib/apt/lists/*
# ----------------- Install WasmCloud -----------------
RUN curl -s "https://packagecloud.io/install/repositories/wasmcloud/core/script.deb.sh" | bash && \
apt-get install -y wash
# ----------------- Deploy the WasmCloud module -----------------
FROM wash-deploy-image
RUN mkdir /app
WORKDIR /app
# Deploy the WasmCloud module
CMD ["sh", "-c", "wash app delete wadm.yaml"]

View File

@@ -0,0 +1,82 @@
import docker
import os
import logging
import yaml
def remove_components(project_dir, nats_host, nats_port, detached):
# Check if the project directory is valid
if not os.path.exists(f"{project_dir}/gen"):
logging.error(f"Project directory is not valid")
return
print('Removing WASM components')
# Docker client
client = docker.from_env()
# Build the images for the project if they don't exist
try:
client.images.get("wash-remove-image:latest")
except docker.errors.ImageNotFound:
print(' - Building wash-remove-image from Dockerfile...')
client.images.build(
path="src/component_deploy/docker",
dockerfile="remove.Dockerfile",
tag="wash-remove-image:latest"
)
try:
wait_list = []
for task in os.listdir(f"{project_dir}/gen"):
__remove_wadm(f"{project_dir}/gen/{task}", client, nats_host, nats_port, detached, wait_list)
if detached == 'True':
print('Waiting for component remove...')
for container in wait_list:
try:
client.containers.get(container).wait()
except Exception:
continue
except Exception as e:
logging.error(f"Error removing components: {e}")
return
def __remove_wadm(task_dir, client, nats_host, nats_port, detached, wait_list):
wadm = __parse_yaml(f"{task_dir}/wadm.yaml")
path = os.path.abspath(task_dir)
name = wadm['spec']['components'][0]['name'] + '-remove'
# Build the wasm module
print(f" - Removing WASM module {name} from WasmCloud")
container = client.containers.run(
"wash-remove-image:latest",
environment=[f'WASMCLOUD_CTL_HOST={nats_host}',
f'WASMCLOUD_CTL_PORT={nats_port}'],
volumes={path: {'bind': '/app', 'mode': 'rw'}},
remove=True,
detach=True,
name=name
)
if detached == 'False':
container.wait()
else:
wait_list.append(name)
def __parse_yaml(yaml_file):
with open(yaml_file, 'r') as stream:
try:
return yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(exc)
return None