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

Fixed manual validation for ssh connection #5260

Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 4 additions & 2 deletions deployability/modules/allocation/allocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
1 change: 1 addition & 0 deletions deployability/modules/allocation/generic/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
31 changes: 20 additions & 11 deletions deployability/modules/allocation/vagrant/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.")
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion deployability/modules/allocation/vagrant/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}\""
Expand Down