Skip to content

Commit

Permalink
Merge pull request #5327 from wazuh/change/5226-dtt1-iteration-3-allo…
Browse files Browse the repository at this point in the history
…cation-module-update-host-for-macos-intel-with-the-new-macstadium-host

Support for macOS Intel host added
  • Loading branch information
teddytpc1 authored May 8, 2024
2 parents 7eb886b + b43a25b commit 58ecd99
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 36 deletions.
13 changes: 5 additions & 8 deletions deployability/modules/allocation/static/specs/os.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,6 @@ vagrant:
box: generic/rocky9
box_version: 4.3.12
# Macos
macos-bigsur-11.0-amd64:
box: development/macos-big-sur
box_version: 0.0.0
macos-catalina-10.15.1-amd64:
box: development/macos-catalina
box_version: 0.0.0
macos-highsierra-10.13.6-amd64:
box: development/macos-high-sierra
box_version: 0.0.0
Expand All @@ -123,12 +117,15 @@ vagrant:
macos-monterey-12.6-arm64:
box: macos-12
box_version: 0.0.0
macos-monterey-12.0.1-amd64:
box: development/macos-monterey
macos-ventura-13.4.1-amd64:
box: development/macos-ventura
box_version: 0.0.0
macos-ventura-13.4.1-arm64:
box: macos-13
box_version: 0.0.0
macos-sonoma-14.4.1-amd64:
box: development/macos-sonoma
box_version: 0.0.0
macos-sonoma-14.0-arm64:
box: macos-14
box_version: 0.0.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ Vagrant.configure("2") do |config|
config.vm.network :private_network, type: "dhcp"
config.vm.network "forwarded_port", guest: 22, host: "{{ config.port }}"

config.vm.provision "file", source: "~/.ssh/vagrant_rsa.pub", destination: "~/.ssh/vagrant_rsa.pub"
config.vm.provision "file", source: "~/.ssh/authorized_keys", destination: "~/.ssh/authorized_keys"


config.vm.synced_folder ".", "/vagrant", disabled: true

config.vm.provider "virtualbox" do |vb|
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Vagrant.configure("2") do |config|
# Box image settings
config.vm.box = "{{ config.box }}"

# VirtualBox specific settings
config.vm.provider "parallels" do |v|
v.memory = "4096"
v.cpus = "2"
v.name = "{{ config.name }}"
v.linked_clone = false
end

# Network settings
config.ssh.forward_agent = true
config.vm.network "forwarded_port", guest: 22, host: "{{ config.port }}"
config.vm.synced_folder ".", "/vagrant", disabled: true
end
23 changes: 23 additions & 0 deletions deployability/modules/allocation/vagrant/helpers/vagrant_script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash

# Set the path to Vagrant directory
VAGRANT_DIR="/usr/local/bin"
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"

# Add Vagrant directory to PATH
export PATH=$PATH:$VAGRANT_DIR

# Check if an argument is provided
if [ $# -eq 0 ]
then
echo "Usage: $0 [up | destroy | status | ...]"
exit 1
fi

if [ $1 == "destroy" ]; then
VAGRANT_CWD=$SCRIPT_DIR vagrant $1 -f
else
VAGRANT_CWD=$SCRIPT_DIR vagrant $1
fi

exit 0
13 changes: 5 additions & 8 deletions deployability/modules/allocation/vagrant/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,10 @@ def delete(self) -> None:
if str(self.host_identifier) == "macstadium":
logger.debug(f"Deleting remote directory {self.host_instance_dir}")
VagrantUtils.remote_command(f"sudo rm -rf {self.host_instance_dir}", self.remote_host_parameters)
logger.debug(f"Killing remote process on port {self.ssh_port}")
proccess = VagrantUtils.remote_command(f"sudo lsof -Pi :{self.ssh_port} -sTCP:LISTEN -t", self.remote_host_parameters)
VagrantUtils.remote_command(f"sudo kill -9 {proccess}", self.remote_host_parameters)
if str(self.host_identifier) == "black_mini":
logger.debug(f"Deleting remote directory {self.host_instance_dir}")
VagrantUtils.remote_command(f"sudo rm -rf {self.host_instance_dir}", self.remote_host_parameters)

if self.arch == 'arm64':
logger.debug(f"Killing remote process on port {self.ssh_port}")
proccess = VagrantUtils.remote_command(f"sudo lsof -Pi :{self.ssh_port} -sTCP:LISTEN -t", self.remote_host_parameters)
VagrantUtils.remote_command(f"sudo kill -9 {proccess}", self.remote_host_parameters)

def status(self) -> str:
"""
Expand Down Expand Up @@ -209,7 +206,7 @@ def __run_vagrant_command(self, command: str | list) -> str:
if isinstance(command, str):
command = [command]
if self.platform == 'macos':
cmd = f"sudo VAGRANT_CWD={self.host_instance_dir} /usr/local/bin/vagrant " + ' '.join(command)
cmd = f"sudo {self.host_instance_dir}/vagrant_script.sh " + ' '.join(command)
output = VagrantUtils.remote_command(cmd, self.remote_host_parameters)
return output
else:
Expand Down
39 changes: 23 additions & 16 deletions deployability/modules/allocation/vagrant/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ def _create_instance(cls, base_dir: Path, params: CreationPayload, config: Vagra
if platform == 'macos':
vagrant_file = str(instance_dir) + '/Vagrantfile'
VagrantUtils.remote_copy(vagrant_file, host_instance_dir, remote_host_parameters)
VagrantUtils.remote_copy(Path(__file__).parent.parent / 'vagrant' / 'helpers' / 'vagrant_script.sh', host_instance_dir, remote_host_parameters)
cmd = f"chmod 700 {host_instance_dir}/vagrant_script.sh"
VagrantUtils.remote_command(cmd, remote_host_parameters)

instance_params = {}
instance_params['instance_dir'] = instance_dir
Expand Down Expand Up @@ -207,10 +210,14 @@ def __render_vagrantfile(cls, config: VagrantConfig) -> str:
"""
environment = Environment(loader=FileSystemLoader(cls.TEMPLATES_DIR))
if config.platform == 'macos':
if config.arch == 'arm64':
template = environment.get_template("vagrant_macStadium.j2")
if config.arch == 'amd64':
virtualbox_boxes = ['development/macos-high-sierra', 'development/macos-mojave', 'development/macos-sierra', 'development/macos-sierra_cmake', 'development/macos-sierra_gcc9']
if config.box not in virtualbox_boxes:
template = environment.get_template("vagrant_parallels_intel.j2")
else:
template = environment.get_template("vagrant_Virtual_box.j2")
else:
template = environment.get_template("vagrant_black_mini.j2")
template = environment.get_template("vagrant_parallels_arm.j2")
else:
template = environment.get_template("vagrant.j2")
return template.render(config=config)
Expand Down Expand Up @@ -301,14 +308,14 @@ def __remote_host(arch: str, action: str, os: str = None, instance_dir: Path = N
ssh_password = client.get_secret_value(SecretId='devops_macstadium_m1_jenkins_password')['SecretString']
ssh_user = client.get_secret_value(SecretId='devops_macstadium_m1_jenkins_user')['SecretString']
except Exception as e:
raise ValueError('Could not get macOS macStadium server IP: ' + str(e) + '.')
raise ValueError('Could not get macOS macStadium ARM server IP: ' + str(e) + '.')

try:
tn = Telnet(server_ip, server_port, timeout)
conn_ok = True
tn.close()
except Exception as e:
raise ValueError('Could not connect to macOS macStadium server: ' + str(e) + '.')
raise ValueError('Could not connect to macOS macStadium ARM server: ' + str(e) + '.')

remote_host_parameters['server_ip'] = server_ip
remote_host_parameters['ssh_password'] = ssh_password
Expand All @@ -322,37 +329,37 @@ def __remote_host(arch: str, action: str, os: str = None, instance_dir: Path = N
prlctl_output = subprocess.Popen(f"sshpass -p {ssh_password} ssh -o 'StrictHostKeyChecking no' {ssh_user}@{server_ip} {cmd}", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0].decode('utf-8')
data_list = json.loads(prlctl_output)
except Exception as e:
raise ValueError('Could not get VMs running on macStadium server: ' + str(e) + '.')
raise ValueError('Could not get VMs running on macStadium ARM server: ' + str(e) + '.')
uuid_count = 0
for item in data_list:
if 'uuid' in item:
uuid_count += 1
if uuid_count < 2:
logger.info(f"macStadium server has less than 2 VMs running, deploying in this host.")
logger.info(f"macStadium ARM server has less than 2 VMs running, deploying in this host.")
return remote_host_parameters
else:
raise ValueError(f"macStadium server is full capacity, use AWS provider.")
raise ValueError(f"macStadium ARM server is full capacity, use AWS provider.")
else:
return remote_host_parameters
if arch == 'amd64':
try:
server_ip = client.get_secret_value(SecretId='devops_black_mini_jenkins_ip')['SecretString']
ssh_password = client.get_secret_value(SecretId='devops_black_mini_jenkins_password')['SecretString']
ssh_user = client.get_secret_value(SecretId='devops_black_mini_jenkins_user')['SecretString']
server_ip = client.get_secret_value(SecretId='devops_macstadium_intel_ip')['SecretString']
ssh_password = client.get_secret_value(SecretId='devops_macstadium_intel_password')['SecretString']
ssh_user = client.get_secret_value(SecretId='devops_macstadium_intel_user')['SecretString']
except Exception as e:
raise ValueError('Could not get macOS Black mini server IP: ' + str(e) + '.')
raise ValueError('Could not get macOS macStadium Intel server IP: ' + str(e) + '.')

try:
tn = Telnet(server_ip, server_port, timeout)
conn_ok = True
tn.close()
except Exception as e:
raise ValueError('Could not connect to macOS Black mini server: ' + str(e) + '.')
raise ValueError('Could not connect to macOS macStadium Intel server: ' + str(e) + '.')

remote_host_parameters['server_ip'] = server_ip
remote_host_parameters['ssh_password'] = ssh_password
remote_host_parameters['ssh_user'] = ssh_user
remote_host_parameters['host_provider'] = 'black_mini'
remote_host_parameters['host_provider'] = 'macstadium'

if conn_ok:
if action == 'create':
Expand All @@ -367,10 +374,10 @@ def __remote_host(arch: str, action: str, os: str = None, instance_dir: Path = N
raise ValueError('Could not get server load average: ' + str(e) + '.')

if float(load_average) <= 10.0 and float(cpu_usage) <= 70.0 and float(memory_usage) <= 75.0:
logger.info(f"Using the black mini server to deploy.")
logger.info(f"Using the macStadium Intel server to deploy.")
return remote_host_parameters
else:
raise ValueError(f"Black mini server is under heavy load, use AWS provider.")
raise ValueError(f"macStadium Intel server is under heavy load, use AWS provider.")
else:
return remote_host_parameters

Expand Down

0 comments on commit 58ecd99

Please sign in to comment.