Skip to content

Commit

Permalink
add verify ssh
Browse files Browse the repository at this point in the history
  • Loading branch information
kthare10 committed May 14, 2024
1 parent b099a00 commit 3067ae0
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 195 deletions.
3 changes: 0 additions & 3 deletions fabric_am/config/switch_handler_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
#
#
# Author: Komal Thareja ([email protected])
ansible:
ansible_python_interpreter: /usr/bin/python3.6
runtime:
playbooks:
location: /etc/fabric/actor/playbooks
inventory_location: /etc/fabric/actor/playbooks/inventory
Expand Down
107 changes: 29 additions & 78 deletions fabric_am/handlers/switch_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

from fabric_am.util.am_constants import AmConstants
from fabric_am.util.ansible_helper import AnsibleHelper
from fabric_am.util.utils import Utils


class SwitchHandlerException(Exception):
Expand Down Expand Up @@ -71,8 +72,8 @@ def clean_restart(self):
cleanup_playbook = f"{playbook_path}/{cleanup_section[AmConstants.CLEAN_ALL]}"
inventory_path = self.get_config()[AmConstants.PLAYBOOK_SECTION][AmConstants.PB_INVENTORY]
extra_vars = {AmConstants.OPERATION: AmConstants.OP_DELETE_ALL}
self.__execute_ansible(inventory_path=inventory_path, playbook_path=cleanup_playbook,
extra_vars=extra_vars)
Utils.execute_ansible(inventory_path=inventory_path, playbook_path=cleanup_playbook,
extra_vars=extra_vars, logger=self.get_logger())
except Exception as e:
self.get_logger().error(f"Failure to clean up existing VMs: {e}")
self.get_logger().error(traceback.format_exc())
Expand Down Expand Up @@ -121,11 +122,31 @@ def create(self, unit: ConfigToken) -> Tuple[dict, ConfigToken]:

# create switch
extra_vars = {
AmConstants.OPERATION: AmConstants.OP_CREATE,
AmConstants.OPERATION: AmConstants.OP_CREATE
}

Utils.execute_ansible(inventory_path=inventory_path, playbook_path=f"{playbook_path}/{playbook}",
extra_vars=extra_vars, logger=self.get_logger())

from ansible.inventory.manager import InventoryManager
from ansible.parsing.dataloader import DataLoader
data_loader = DataLoader()
inventory = InventoryManager(loader=data_loader,
sources=[inventory_path])
host = inventory.get_host(hostname=f"{sliver.get_site()}-p4.fabric-testbed.net")
ansible_host = host.get_vars().get('ansible_host')
ansible_ssh_user = host.get_vars().get('ansible_ssh_user')
ansible_ssh_pwd = host.get_vars().get('ansible_ssh_pwd')

Utils.verify_ssh(mgmt_ip=ansible_host, user=ansible_ssh_user, pwd=ansible_ssh_pwd,
logger=self.get_logger(), retry=10)
extra_vars = {
AmConstants.OPERATION: AmConstants.OP_CONFIG,
AmConstants.SSH_KEY: ssh_key
}
self.__execute_ansible(inventory_path=inventory_path, playbook_path=f"{playbook_path}/{playbook}",
extra_vars=extra_vars)
Utils.execute_ansible(inventory_path=inventory_path, playbook_path=f"{playbook_path}/{playbook}",
extra_vars=extra_vars, logger=self.get_logger())

except Exception as e:
self.get_logger().error(e)
self.get_logger().error(traceback.format_exc())
Expand Down Expand Up @@ -163,9 +184,7 @@ def delete(self, unit: ConfigToken) -> Tuple[dict, ConfigToken]:
f"resource_type: {sliver.get_type()}")

unit_id = str(unit.get_reservation_id())
unit_properties = unit.get_properties()
project_id = unit_properties.get(Constants.PROJECT_ID, None)
self.__cleanup(sliver=sliver, unit_id=unit_id, project_id=project_id)
self.__cleanup(sliver=sliver, unit_id=unit_id)
except Exception as e:
result = {Constants.PROPERTY_TARGET_NAME: Constants.TARGET_DELETE,
Constants.PROPERTY_TARGET_RESULT_CODE: Constants.RESULT_CODE_EXCEPTION,
Expand Down Expand Up @@ -232,7 +251,6 @@ def __cleanup(self, *, sliver: NodeSliver, unit_id: str, raise_exception: bool =
Cleanup VM and detach PCI devices
:param sliver: Sliver
:param unit_id: Unit Id
:param project_id: Project Id
:param raise_exception: Raise exception if raise_exception flag is True
:return:
"""
Expand All @@ -246,79 +264,12 @@ def __cleanup(self, *, sliver: NodeSliver, unit_id: str, raise_exception: bool =
extra_vars = {
AmConstants.OPERATION: AmConstants.OP_DELETE
}
self.__execute_ansible(inventory_path=inventory_path, playbook_path=f"{playbook_path}/{playbook}",
extra_vars=extra_vars)
Utils.execute_ansible(inventory_path=inventory_path, playbook_path=f"{playbook_path}/{playbook}",
extra_vars=extra_vars, logger=self.get_logger())

except Exception as e:
self.get_logger().error(f"Exception occurred in cleanup {unit_id} error: {e}")
self.get_logger().error(traceback.format_exc())
if raise_exception:
raise e

def __execute_command(self, *, mgmt_ip: str, user: str, command: str, timeout: int = 60, retry: int = 3):
"""
Execute a command on the VM
:param mgmt_ip Management IP to access the VM
:param user Default Linux user to use for SSH/Ansible
:param command Command to execute
:param timeout Timeout in seconds
:param retry Number of retries
:return:
"""
for i in range(retry):
try:
# Construct the SSH client
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
key_file = self.get_config()[AmConstants.PLAYBOOK_SECTION][AmConstants.ADMIN_SSH_KEY]
pkey = paramiko.RSAKey.from_private_key_file(key_file)
ssh.connect(mgmt_ip, username=user, timeout=timeout, pkey=pkey)

# Execute the command
stdin, stdout, stderr = ssh.exec_command(command)
output = stdout.readlines()
ssh.close()
return output
except Exception as e:
self.get_logger().error(f"Exception : {e}")
self.get_logger().error(traceback.format_exc())
if i < retry - 1:
time.sleep(timeout)
self.get_logger().info(f"Retrying command {command} on VM {mgmt_ip}")
else:
self.get_logger().error(f"Failed to execute command {command} on VM {mgmt_ip}")
raise e

def __verify_ssh(self, *, mgmt_ip: str, user: str, timeout: int = 60, retry: int = 10):
"""
Verify that the VM is accessible via SSH
:param mgmt_ip Management IP to access the VM
:param user Default Linux user to use for SSH/Ansible
:param retry Number of retries
:param retry_interval Timeout in seconds
"""
command = f"echo test ssh from {mgmt_ip} > /tmp/fabric_execute_script.sh; " \
f"chmod +x /tmp/fabric_execute_script.sh; /tmp/fabric_execute_script.sh"

try:
output = self.__execute_command(mgmt_ip=mgmt_ip, user=user, command=command,
timeout=timeout, retry=retry)
self.get_logger().info(f"Output: {output}")
except Exception as e:
pass

def __execute_ansible(self, *, inventory_path: str, playbook_path: str, extra_vars: dict, sources: str = None,
private_key_file: str = None, host_vars: dict = None, host: str = None, user: str = None):
ansible_helper = AnsibleHelper(inventory_path=inventory_path, logger=self.get_logger(),
sources=sources)

ansible_helper.set_extra_vars(extra_vars=extra_vars)

if host is not None and host_vars is not None and len(host_vars) > 0:
for key, value in host_vars.items():
ansible_helper.add_vars(host=host, var_name=key, value=value)

self.get_logger().info(f"Executing playbook {playbook_path} extra_vars: {extra_vars} host_vars: {host_vars}")
ansible_helper.run_playbook(playbook_path=playbook_path, private_key_file=private_key_file, user=user)
return ansible_helper.get_result_callback().get_json_result_ok()
Loading

0 comments on commit 3067ae0

Please sign in to comment.