Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force instance name to be defined in vagrant deploy #5342

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 0 additions & 19 deletions deployability/modules/allocation/aws/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,25 +357,6 @@ def _release_dedicated_host(host_identifier: str) -> str:
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

@staticmethod
def validate_dependencies():
"""
Expand Down
20 changes: 20 additions & 0 deletions deployability/modules/allocation/generic/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import shutil
import uuid
import yaml
import re

from abc import ABC, abstractmethod
from pathlib import Path
Expand Down Expand Up @@ -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
33 changes: 27 additions & 6 deletions deployability/modules/allocation/vagrant/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import subprocess
import boto3
import random
import re

from jinja2 import Environment, FileSystemLoader
from pathlib import Path
Expand Down Expand Up @@ -47,12 +48,28 @@ def _create_instance(cls, base_dir: Path, params: CreationPayload, config: Vagra
"""
cls.validate_dependencies(params.composite_name)
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/<repository>/issues/<issue-number>")
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(0000, 9999))
# Create the instance directory.
instance_dir = base_dir / instance_id
instance_dir.mkdir(parents=True, exist_ok=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
Expand All @@ -61,10 +78,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, 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)
credentials = VagrantCredentials()
if arch == 'ppc64':
Expand Down