From 0883d308d9c70d99981d79d25a3c4e130eecdab6 Mon Sep 17 00:00:00 2001 From: c-bordon Date: Mon, 6 May 2024 14:43:44 -0300 Subject: [PATCH 1/5] Force instance name to be defined in vagrant deploy --- .../modules/allocation/aws/provider.py | 19 ------------ .../modules/allocation/generic/provider.py | 20 ++++++++++++ .../modules/allocation/vagrant/provider.py | 31 ++++++++++++++++--- 3 files changed, 46 insertions(+), 24 deletions(-) diff --git a/deployability/modules/allocation/aws/provider.py b/deployability/modules/allocation/aws/provider.py index 66e393cce0..05ba003b88 100644 --- a/deployability/modules/allocation/aws/provider.py +++ b/deployability/modules/allocation/aws/provider.py @@ -354,22 +354,3 @@ def _release_dedicated_host(host_identifier: str) -> str: logger.warning(f"{message}") else: logger.info(f"Dedicated host released: {host_identifier}") - - @staticmethod - def generate_repository_name(repository: str) -> str: - """ - Generate a repository name for the instance. - - Args: - repository (str): Repository name. - - Returns: - str: Repository name for the instance. - """ - matches = re.findall(r'(\w+)', repository) - if len(matches) == 3: - return ''.join([c[0] for c in matches]) - elif len(matches) == 2: - return matches[1] - else: - return repository diff --git a/deployability/modules/allocation/generic/provider.py b/deployability/modules/allocation/generic/provider.py index 651e61494a..6551beb563 100644 --- a/deployability/modules/allocation/generic/provider.py +++ b/deployability/modules/allocation/generic/provider.py @@ -5,6 +5,7 @@ import shutil import uuid import yaml +import re from abc import ABC, abstractmethod from pathlib import Path @@ -202,3 +203,22 @@ def _get_misc_specs(cls) -> dict: """ with open(cls.MISC_PATH, "r") as f: return yaml.safe_load(f).get(cls.provider_name) + + @staticmethod + def generate_repository_name(repository: str) -> str: + """ + Generate a repository name for the instance. + + Args: + repository (str): Repository name. + + Returns: + str: Repository name for the instance. + """ + matches = re.findall(r'(\w+)', repository) + if len(matches) == 3: + return ''.join([c[0] for c in matches]) + elif len(matches) == 2: + return matches[1] + else: + return repository diff --git a/deployability/modules/allocation/vagrant/provider.py b/deployability/modules/allocation/vagrant/provider.py index 38f64678e1..640d2c1227 100644 --- a/deployability/modules/allocation/vagrant/provider.py +++ b/deployability/modules/allocation/vagrant/provider.py @@ -7,6 +7,7 @@ import subprocess import boto3 import random +import re from jinja2 import Environment, FileSystemLoader from pathlib import Path @@ -46,12 +47,28 @@ def _create_instance(cls, base_dir: Path, params: CreationPayload, config: Vagra VagrantInstance: The created Vagrant instance. """ if params.instance_name: - instance_id = params.instance_name + name = params.instance_name + elif params.label_issue: + issue = params.label_issue + url_regex = "(https:\/\/|http:\/\/)?[github]{2,}(\.[com]{2,})?\/wazuh\/[a-zA-Z0-9_-]+(?:-[a-zA-Z0-9_-]+)?\/issues\/[0-9]{2,}" + if not re.match(url_regex, issue): + raise ValueError(f"The issue label was not provided or is of incorrect format, example: https://github.com/wazuh//issues/") + issue_name= re.search(r'github\.com\/wazuh\/([^\/]+)\/issues', issue) + repository = cls.generate_repository_name(str(issue_name.group(1))) + name = repository + "-" + str(re.search(r'(\d+)$', issue).group(1)) + "-" + str(params.composite_name.split("-")[1]) + "-" + str(params.composite_name.split("-")[2]) else: - instance_id = cls._generate_instance_id(cls.provider_name) + raise ValueError("Instance name or issue label is required.") + + instance_id = name + "-" + str(random.randint(1000, 9999)) + # Create the instance directory. instance_dir = base_dir / instance_id - instance_dir.mkdir(parents=True, exist_ok=True) + try: + instance_dir.mkdir(parents=True) + except FileExistsError: + instance_id = name + "-" + str(random.randint(1000, 9999)) + instance_dir = base_dir / instance_id + instance_dir.mkdir(parents=True) platform = str(params.composite_name.split("-")[0]) host_instance_dir = None host_identifier = None @@ -60,10 +77,14 @@ def _create_instance(cls, base_dir: Path, params: CreationPayload, config: Vagra # Used for macOS deployments if platform == 'macos': host_instance_dir = "/Users/jenkins/testing/" + instance_id - logger.debug(f"Creating instance directory on remote host") - cmd = f"mkdir {host_instance_dir}" remote_host_parameters = cls.__remote_host(arch, 'create') host_identifier = remote_host_parameters['host_provider'] + logger.debug(f"Checking if instance directory exists on remote host") + cmd = f"ls {host_instance_dir} > /dev/null 2>&1" + if VagrantUtils.remote_command(cmd, remote_host_parameters): + raise ValueError(f"Instance directory {host_instance_dir} already exists on remote host, please delete it before creating a new instance.") + logger.debug(f"Creating instance directory on remote host") + cmd = f"mkdir {host_instance_dir}" VagrantUtils.remote_command(cmd, remote_host_parameters) credentials = VagrantCredentials() if arch == 'ppc64': From b962c4ed3cb0bcbaa1660ae4c3a731615b99df7f Mon Sep 17 00:00:00 2001 From: c-bordon Date: Mon, 6 May 2024 15:00:53 -0300 Subject: [PATCH 2/5] Added debug message --- deployability/modules/allocation/vagrant/provider.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/deployability/modules/allocation/vagrant/provider.py b/deployability/modules/allocation/vagrant/provider.py index 640d2c1227..9899904003 100644 --- a/deployability/modules/allocation/vagrant/provider.py +++ b/deployability/modules/allocation/vagrant/provider.py @@ -59,14 +59,15 @@ def _create_instance(cls, base_dir: Path, params: CreationPayload, config: Vagra else: raise ValueError("Instance name or issue label is required.") - instance_id = name + "-" + str(random.randint(1000, 9999)) + instance_id = name + "-" + str(random.randint(0000, 9999)) # Create the instance directory. instance_dir = base_dir / instance_id try: instance_dir.mkdir(parents=True) except FileExistsError: - instance_id = name + "-" + str(random.randint(1000, 9999)) + logger.debug(f"Instance directory {instance_dir} already exists. Assigning new suffix.") + instance_id = name + "-" + str(random.randint(0000, 9999)) instance_dir = base_dir / instance_id instance_dir.mkdir(parents=True) platform = str(params.composite_name.split("-")[0]) From a7d727d88a854f74bd78fe01a155758d295271c2 Mon Sep 17 00:00:00 2001 From: c-bordon Date: Mon, 6 May 2024 15:38:25 -0300 Subject: [PATCH 3/5] Added loop for dir validation --- .../modules/allocation/vagrant/provider.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/deployability/modules/allocation/vagrant/provider.py b/deployability/modules/allocation/vagrant/provider.py index 9899904003..b29bc032ae 100644 --- a/deployability/modules/allocation/vagrant/provider.py +++ b/deployability/modules/allocation/vagrant/provider.py @@ -60,16 +60,15 @@ def _create_instance(cls, base_dir: Path, params: CreationPayload, config: Vagra raise ValueError("Instance name or issue label is required.") instance_id = name + "-" + str(random.randint(0000, 9999)) - # Create the instance directory. - instance_dir = base_dir / instance_id - try: - instance_dir.mkdir(parents=True) - except FileExistsError: - logger.debug(f"Instance directory {instance_dir} already exists. Assigning new suffix.") - instance_id = name + "-" + str(random.randint(0000, 9999)) - instance_dir = base_dir / instance_id - instance_dir.mkdir(parents=True) + while True: + try: + instance_dir = base_dir / instance_id + instance_dir.mkdir(parents=True) + break + except FileExistsError: + logger.debug(f"Instance directory {instance_dir} already exists. Assigning new suffix.") + instance_id = name + "-" + str(random.randint(0000, 9999)) platform = str(params.composite_name.split("-")[0]) host_instance_dir = None host_identifier = None From 6c5698eb83faeb7b7da03d049216f7310ebbb34e Mon Sep 17 00:00:00 2001 From: c-bordon Date: Mon, 6 May 2024 16:34:12 -0300 Subject: [PATCH 4/5] enhancement on the remote host message --- deployability/modules/allocation/vagrant/provider.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deployability/modules/allocation/vagrant/provider.py b/deployability/modules/allocation/vagrant/provider.py index 4603dafcd2..dd325bba7f 100644 --- a/deployability/modules/allocation/vagrant/provider.py +++ b/deployability/modules/allocation/vagrant/provider.py @@ -83,7 +83,7 @@ def _create_instance(cls, base_dir: Path, params: CreationPayload, config: Vagra logger.debug(f"Checking if instance directory exists on remote host") cmd = f"ls {host_instance_dir} > /dev/null 2>&1" if VagrantUtils.remote_command(cmd, remote_host_parameters): - raise ValueError(f"Instance directory {host_instance_dir} already exists on remote host, please delete it before creating a new instance.") + raise ValueError(f"Instance directory {host_instance_dir} already exists on remote host, Check if it is possible to delete the directory on the remote host.") logger.debug(f"Creating instance directory on remote host") cmd = f"mkdir {host_instance_dir}" VagrantUtils.remote_command(cmd, remote_host_parameters) From 8e1fa9b5eb76908279f65a0333e891de8b06e88a Mon Sep 17 00:00:00 2001 From: c-bordon Date: Mon, 6 May 2024 16:36:39 -0300 Subject: [PATCH 5/5] Changed , to . --- deployability/modules/allocation/vagrant/provider.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deployability/modules/allocation/vagrant/provider.py b/deployability/modules/allocation/vagrant/provider.py index dd325bba7f..2b0e3436ba 100644 --- a/deployability/modules/allocation/vagrant/provider.py +++ b/deployability/modules/allocation/vagrant/provider.py @@ -83,7 +83,7 @@ def _create_instance(cls, base_dir: Path, params: CreationPayload, config: Vagra logger.debug(f"Checking if instance directory exists on remote host") cmd = f"ls {host_instance_dir} > /dev/null 2>&1" if VagrantUtils.remote_command(cmd, remote_host_parameters): - raise ValueError(f"Instance directory {host_instance_dir} already exists on remote host, Check if it is possible to delete the directory on the remote host.") + raise ValueError(f"Instance directory {host_instance_dir} already exists on remote host. Check if it is possible to delete the directory on the remote host.") logger.debug(f"Creating instance directory on remote host") cmd = f"mkdir {host_instance_dir}" VagrantUtils.remote_command(cmd, remote_host_parameters)