From aaac82611ed1e93e806eb6db46aa96a185edf840 Mon Sep 17 00:00:00 2001 From: c-bordon Date: Thu, 18 Apr 2024 09:39:48 -0300 Subject: [PATCH] Fixed know_host files for ssh connection --- .../modules/allocation/allocation.py | 6 ++-- .../modules/allocation/generic/models.py | 1 + .../modules/allocation/vagrant/provider.py | 31 ++++++++++++------- .../modules/allocation/vagrant/utils.py | 2 +- 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/deployability/modules/allocation/allocation.py b/deployability/modules/allocation/allocation.py index 802da474b6..644e9b7b7a 100755 --- a/deployability/modules/allocation/allocation.py +++ b/deployability/modules/allocation/allocation.py @@ -140,13 +140,15 @@ def __generate_inventory(instance: Instance, inventory_path: Path) -> None: ansible_user=ssh_config.user, ansible_port=ssh_config.port, ansible_connection='ssh', - ansible_password=ssh_config.password) + ansible_password=ssh_config.password, + ansible_ssh_common_args='-o StrictHostKeyChecking=no') else: inventory = models.InventoryOutput(ansible_host=ssh_config.hostname, ansible_user=ssh_config.user, ansible_port=ssh_config.port, ansible_connection='ssh', - ansible_ssh_private_key_file=str(ssh_config.private_key)) + ansible_ssh_private_key_file=str(ssh_config.private_key), + ansible_ssh_common_args='-o StrictHostKeyChecking=no') with open(inventory_path, 'w') as f: yaml.dump(inventory.model_dump(exclude_none=True), f) logger.info(f"Inventory file generated at {inventory_path}") diff --git a/deployability/modules/allocation/generic/models.py b/deployability/modules/allocation/generic/models.py index fc5274a558..4f809043c3 100644 --- a/deployability/modules/allocation/generic/models.py +++ b/deployability/modules/allocation/generic/models.py @@ -31,6 +31,7 @@ class InventoryOutput(BaseModel): ansible_password: str | None = None ansible_connection: Literal['ssh', 'winrm'] | None = None ansible_winrm_server_cert_validation: Literal['ignore'] | None = None + ansible_ssh_common_args: Literal['-o StrictHostKeyChecking=no'] | None = None class TrackOutput(BaseModel): diff --git a/deployability/modules/allocation/vagrant/provider.py b/deployability/modules/allocation/vagrant/provider.py index 6234cf5a87..d08f5f9d3b 100644 --- a/deployability/modules/allocation/vagrant/provider.py +++ b/deployability/modules/allocation/vagrant/provider.py @@ -294,9 +294,12 @@ def __remote_host(arch: str, action: str, os: str = None, instance_dir: Path = N if conn_ok: if action == 'create': - cmd = "sudo /usr/local/bin/prlctl list -j" - prlctl_output = subprocess.Popen(f"sshpass -p {ssh_password} ssh {ssh_user}@{server_ip} {cmd}", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0].decode('utf-8') - data_list = json.loads(prlctl_output) + try: + cmd = "sudo /usr/local/bin/prlctl list -j" + 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) + '.') uuid_count = 0 for item in data_list: if 'uuid' in item: @@ -330,12 +333,15 @@ def __remote_host(arch: str, action: str, os: str = None, instance_dir: Path = N if conn_ok: if action == 'create': - loadav_command = "\'python3 -c \"import psutil; print(psutil.getloadavg()[0])\"\'" - cpu_command = "\'python3 -c \"import psutil; print(psutil.getloadavg()[0]/ psutil.cpu_count() * 100)\"\'" - memory_command = "\'python3 -c \"import psutil; print(psutil.virtual_memory().percent)\"\'" - load_average = subprocess.Popen(f"sshpass -p {ssh_password} ssh {ssh_user}@{server_ip} {loadav_command}", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0].decode('utf-8') - cpu_usage = subprocess.Popen(f"sshpass -p {ssh_password} ssh {ssh_user}@{server_ip} {cpu_command}", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0].decode('utf-8') - memory_usage = subprocess.Popen(f"sshpass -p {ssh_password} ssh {ssh_user}@{server_ip} {memory_command}", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0].decode('utf-8') + try: + loadav_command = "\'python3 -c \"import psutil; print(psutil.getloadavg()[0])\"\'" + cpu_command = "\'python3 -c \"import psutil; print(psutil.getloadavg()[0]/ psutil.cpu_count() * 100)\"\'" + memory_command = "\'python3 -c \"import psutil; print(psutil.virtual_memory().percent)\"\'" + load_average = subprocess.Popen(f"sshpass -p {ssh_password} ssh -o 'StrictHostKeyChecking no' {ssh_user}@{server_ip} {loadav_command}", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0].decode('utf-8') + cpu_usage = subprocess.Popen(f"sshpass -p {ssh_password} ssh -o 'StrictHostKeyChecking no' {ssh_user}@{server_ip} {cpu_command}", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0].decode('utf-8') + memory_usage = subprocess.Popen(f"sshpass -p {ssh_password} ssh -o 'StrictHostKeyChecking no' {ssh_user}@{server_ip} {memory_command}", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0].decode('utf-8') + except Exception as e: + 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.") @@ -382,8 +388,11 @@ def __remote_host(arch: str, action: str, os: str = None, instance_dir: Path = N if conn_ok: if action == 'create': - cmd = "sudo docker ps -a" - output = subprocess.Popen(f"ssh -i {ssh_key} {ssh_user}@{server_ip} {cmd}", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0].decode('utf-8') + try: + cmd = "sudo docker ps -a" + output = subprocess.Popen(f"ssh -i {ssh_key} {ssh_user}@{server_ip} {cmd}", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0].decode('utf-8') + except Exception as e: + raise ValueError('Could not get docker containers running on ppc64 server: ' + str(e) + '.') if '2222' in output and '8080' in output: raise ValueError(f"ppc64 server has full capacity, cannot host a new container") else: diff --git a/deployability/modules/allocation/vagrant/utils.py b/deployability/modules/allocation/vagrant/utils.py index d27b2dab80..aacd54b56b 100644 --- a/deployability/modules/allocation/vagrant/utils.py +++ b/deployability/modules/allocation/vagrant/utils.py @@ -23,7 +23,7 @@ def remote_command(cls, command: str | list, remote_host_parameters: dict) -> st ssh_user = remote_host_parameters['ssh_user'] if remote_host_parameters.get('ssh_password'): ssh_password = remote_host_parameters['ssh_password'] - ssh_command = f"sshpass -p {ssh_password} ssh {ssh_user}@{server_ip} {command}" + ssh_command = f"sshpass -p {ssh_password} ssh -o 'StrictHostKeyChecking no' {ssh_user}@{server_ip} {command}" if remote_host_parameters.get('ssh_key'): ssh_key = remote_host_parameters['ssh_key'] ssh_command = f"ssh -i {ssh_key} {ssh_user}@{server_ip} \"{command}\""