Skip to content

Commit

Permalink
invoke playbook from switch handler
Browse files Browse the repository at this point in the history
  • Loading branch information
kthare10 committed May 13, 2024
1 parent 062f629 commit 13169bb
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 85 deletions.
24 changes: 18 additions & 6 deletions fabric_am/handlers/switch_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ def create(self, unit: ConfigToken) -> Tuple[dict, ConfigToken]:

unit_id = None
sliver = None
project_id = None
try:
self.get_logger().info(f"Create invoked for unit: {unit}")
sliver = unit.get_sliver()
Expand All @@ -114,26 +113,30 @@ def create(self, unit: ConfigToken) -> Tuple[dict, ConfigToken]:

unit_properties = unit.get_properties()
ssh_key = unit_properties.get(Constants.USER_SSH_KEY, None)
project_id = unit_properties.get(Constants.PROJECT_ID, None)

resource_type = str(sliver.get_type())
playbook_path = self.get_config()[AmConstants.PLAYBOOK_SECTION][AmConstants.PB_LOCATION]
inventory_path = self.get_config()[AmConstants.PLAYBOOK_SECTION][AmConstants.PB_INVENTORY]

resource_type = str(sliver.get_type())
playbook = self.get_config()[AmConstants.PLAYBOOK_SECTION][resource_type]
if playbook is None or inventory_path is None or playbook_path is None:
raise SwitchHandlerException(f"Missing config parameters playbook: {playbook} "
f"playbook_path: {playbook_path} inventory_path: {inventory_path}")

# create switch

extra_vars = {
AmConstants.OPERATION: AmConstants.OP_CREATE,
AmConstants.SSH_KEY: ssh_key
}
self.__execute_ansible(inventory_path=inventory_path, playbook_path=f"{playbook_path}/{playbook}",
extra_vars=extra_vars)
except Exception as e:
self.get_logger().error(e)
self.get_logger().error(traceback.format_exc())
# Delete VM in case of failure
if sliver is not None and unit_id is not None:
time.sleep(5)
self.__cleanup(sliver=sliver, unit_id=unit_id, project_id=project_id)
self.__cleanup(sliver=sliver, unit_id=unit_id)
unit.get_sliver().label_allocations.instance = None

result = {Constants.PROPERTY_TARGET_NAME: Constants.TARGET_CREATE,
Expand Down Expand Up @@ -229,7 +232,7 @@ def poa(self, unit: ConfigToken, data: dict) -> Tuple[dict, ConfigToken]:
self.get_logger().info(f"POA completed")
return result, unit

def __cleanup(self, *, sliver: NodeSliver, unit_id: str, project_id: str, raise_exception: bool = False):
def __cleanup(self, *, sliver: NodeSliver, unit_id: str, raise_exception: bool = False):
"""
Cleanup VM and detach PCI devices
:param sliver: Sliver
Expand All @@ -241,6 +244,15 @@ def __cleanup(self, *, sliver: NodeSliver, unit_id: str, project_id: str, raise_
try:
playbook_path = self.get_config()[AmConstants.PLAYBOOK_SECTION][AmConstants.PB_LOCATION]
inventory_path = self.get_config()[AmConstants.PLAYBOOK_SECTION][AmConstants.PB_INVENTORY]
resource_type = str(sliver.get_type())
playbook = self.get_config()[AmConstants.PLAYBOOK_SECTION][resource_type]

# reset switch
extra_vars = {
AmConstants.OPERATION: AmConstants.OP_DELETE
}
self.__execute_ansible(inventory_path=inventory_path, playbook_path=f"{playbook_path}/{playbook}",
extra_vars=extra_vars)

except Exception as e:
self.get_logger().error(f"Exception occurred in cleanup {unit_id} error: {e}")
Expand Down
3 changes: 0 additions & 3 deletions fabric_am/playbooks/head_switch_provisioning.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,5 @@

gather_facts: no

vars:
switch_username: rare

roles:
- head_switch_provisioning
14 changes: 11 additions & 3 deletions fabric_am/playbooks/roles/head_switch_provisioning/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,21 @@
args:
executable: /bin/bash
register: efibootmgr_output
when: operation == 'create' or operation == 'delete'

- name: Reboot the switch
reboot:
async: 0
poll: 0
when: operation == 'create' or operation == 'delete'

- name: Add public key to authorized_keys
lineinfile:
path: ~/.ssh/authorized_keys
line: "{{ public_key_string }}"
line: "{{ sshkey }}"
state: present
create: yes
owner: "{{ switch_username }}"
owner: "{{ ansible_ssh_user }}"
group: "{{ ansible_ssh_pass }}"
mode: "0600"
mode: "0600"
when: operation == 'create'
115 changes: 42 additions & 73 deletions test_playbooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from fim.slivers.capacities_labels import Capacities, Labels, CapacityHints
from fim.slivers.network_node import NodeSliver, NodeType

from fabric_am.handlers.switch_handler import SwitchHandler
from fabric_am.handlers.vm_handler import VMHandler
from fabric_am.util.am_constants import AmConstants
from fabric_am.util.ansible_helper import AnsibleHelper
Expand All @@ -49,6 +50,8 @@ class TestPlaybooks:
prop = {AmConstants.CONFIG_PROPERTIES_FILE: 'fabric_am/config/vm_handler_config.yml'}
lock = multiprocessing.Lock()
handler = VMHandler(logger=logger, properties=prop, process_lock=lock)
sw_handler = SwitchHandler(logger=logger, process_lock=lock,
properties={AmConstants.CONFIG_PROPERTIES_FILE: 'fabric_am/config/switch_handler_config.yml'})
from fabric_cf.actor.core.container.globals import GlobalsSingleton
GlobalsSingleton.get().log = logger

Expand Down Expand Up @@ -180,8 +183,8 @@ def test_poa_cpupin(self):
def test_fpga_prov(self):
u = Unit(rid=ID(uid='0a0c2fb9-071a-4a3a-ba94-aa178c237aa2'))
u.properties = {Constants.USER_SSH_KEY:
"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDIxGUVBf24l4gSgUtQQaScP7S604CpXKh66cCMZB1GoXfGqyhRVO1xQUXGA2Oj8MeZf3bo4tjmrPnVeeTVfwTrxkkFNvekwY4QbGX7o8YPNnEFquLWMmkoLn9RFJI47Cj+JHWQN7sEW4WVnmHNITcw5lD3V+yw1bD5M0boUXvh/MnHTu59MEDRyLUyWY+N1FUxHrO0UgSISczRjFS31zF5WY83ssNWq+zxD0NM6GhLWg5Ynzat1J75NRvnMVkuj0VmFcJuHIl3jYCdL9uE7kCw08oh06p/VZBzUIDP6EB0e+H1udu0DvT7SunqBZnobrTCyj1Bma9BJEHPhocIIcPl [email protected],"
"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDIxGUVBf24l4gSgUtQQaScP7S604CpXKh66cCMZB1GoXfGqyhRVO1xQUXGA2Oj8MeZf3bo4tjmrPnVeeTVfwTrxkkFNvekwY4QbGX7o8YPNnEFquLWMmkoLn9RFJI47Cj+JHWQN7sEW4WVnmHNITcw5lD3V+yw1bD5M0boUXvh/MnHTu59MEDRyLUyWY+N1FUxHrO0UgSISczRjFS31zF5WY83ssNWq+zxD0NM6GhLWg5Ynzat1J75NRvnMVkuj0VmFcJuHIl3jYCdL9uE7kCw08oh06p/VZBzUIDP6EB0e+H1udu0DvT7SunqBZnobrTCyj1Bma9BJEHPhocIIcPl [email protected]"}
"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAkoLn9RFJIJEHPhocIIcPl [email protected],"
"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAPNnEFquLWMmkoLEHPhocIIcPl [email protected]"}
sliver = NodeSliver()
cap = Capacities(core=2, ram=8, disk=10)
sliver.set_properties(type=NodeType.VM, site="RENC", capacity_allocations=cap, name="fpga-vm")
Expand All @@ -202,6 +205,15 @@ def test_fpga_prov(self):
u.set_sliver(sliver=sliver)
r, u = self.handler.create(unit=u)

def test_switch(self):
u = Unit(rid=ID(uid='0a0c2fb9-071a-4a3a-ba94-aa178c237aa2'))
u.properties = {Constants.USER_SSH_KEY:
"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDIxGyj1Bma9BJEHPhocIIcPl [email protected]"}
sliver = NodeSliver()
cap = Capacities(unt=1)
sliver.set_properties(type=NodeType.Switch, site="RENC", capacity_allocations=cap, name="p4-sw")
r, u = self.sw_handler.create(unit=u)


def execute_ansible(*, logger, inventory_path: str, playbook_path: str, extra_vars: dict,
ansible_python_interpreter: str, sources: str = None, private_key_file: str = None,
Expand All @@ -221,79 +233,13 @@ def execute_ansible(*, logger, inventory_path: str, playbook_path: str, extra_va
return ansible_helper.get_result_callback()


if __name__ == "__main__":
import time
tpb = TestPlaybooks()
#tpb.test_create_vm_success_no_pci()

#time.sleep(10)
#tpb.test_delete_vm_success_no_pci()
#time.sleep(10)
#u = tpb.test_create_vm_success()

#time.sleep(10)
#tpb.test_delete_vm_success(u=u)
#tpb.test_config_nw_interface_tagged()
#tpb.test_config_nw_interface()

#tpb.test_poa_cpuinfo()
#tpb.test_poa_numainfo()

#tpb.test_poa_numatune()
#tpb.test_poa_cpupin()
#tpb.test_poa_reboot()

tpb.test_fpga_prov()

'''
ansible_helper = AnsibleHelper(inventory_path="/etc/fabric/actor/playbooks//inventory", ansible_python_interpreter='/usr/bin/python3.6',
logger=logging.getLogger())
ansible_helper.set_extra_vars({"operation": "listall"})
ansible_helper.run_playbook(playbook_path="/etc/fabric/actor/playbooks//worker_libvirt_operations.yml")
ansible_helper.get_result_callback()
for host, ok_result in ansible_helper.get_result_callback().host_ok.items():
# Get VMs via Virsh
print(f"host: {host}")
virsh_vms = []
if ok_result and ok_result._result:
virsh_vms = ok_result._result.get('stdout_lines', [])
print(f"List of VMs: {virsh_vms}")
ansible_helper2 = AnsibleHelper(inventory_path="/etc/fabric/actor/playbooks//inventory",
ansible_python_interpreter='/usr/bin/python3.6',
logger=logging.getLogger())
ansible_helper2.set_extra_vars({"operation": "list",
"host": str(host)})
# Get VMs via Openstack
os_vms = {}
ansible_helper2.run_playbook(playbook_path="/etc/fabric/actor/playbooks/head_vm_provisioning.yml")
result = ansible_helper2.get_result_callback().get_json_result_ok()
if result and result.get('openstack_servers'):
servers = result.get('openstack_servers')
for s in servers:
os_vms[s.get('OS-EXT-SRV-ATTR:instance_name')] = s.get('name')
# Find extra VMs on Virsh and delete them
for v in virsh_vms:
if v not in os_vms:
ansible_helper3 = AnsibleHelper(inventory_path="/etc/fabric/actor/playbooks//inventory",
ansible_python_interpreter='/usr/bin/python3.6',
logger=logging.getLogger())
ansible_helper3.set_extra_vars({"operation": "delete",
"host": str(host)})
ansible_helper3.run_playbook(playbook_path="/etc/fabric/actor/playbooks/worker_libvirt_operations.yml")
result = ansible_helper3.get_result_callback().get_json_result_ok()
'''
def test_audit():
logger = logging.getLogger("Audit")
results_1 = execute_ansible(inventory_path="/etc/fabric/actor/playbooks//inventory",
logger=logger,
playbook_path="/etc/fabric/actor/playbooks//worker_libvirt_operations.yml",
extra_vars={"operation": "listall"},
ansible_python_interpreter='/usr/bin/python3.6')
logger=logger,
playbook_path="/etc/fabric/actor/playbooks//worker_libvirt_operations.yml",
extra_vars={"operation": "listall"},
ansible_python_interpreter='/usr/bin/python3.6')

# Dictionary to store OpenStack VMs
os_vms = {}
Expand Down Expand Up @@ -328,6 +274,29 @@ def execute_ansible(*, logger, inventory_path: str, playbook_path: str, extra_va
logger.info(f"Deleting for instance: {v} result: {results_3}")


if __name__ == "__main__":
import time
tpb = TestPlaybooks()
#tpb.test_create_vm_success_no_pci()

#time.sleep(10)
#tpb.test_delete_vm_success_no_pci()
#time.sleep(10)
#u = tpb.test_create_vm_success()

#time.sleep(10)
#tpb.test_delete_vm_success(u=u)
#tpb.test_config_nw_interface_tagged()
#tpb.test_config_nw_interface()

#tpb.test_poa_cpuinfo()
#tpb.test_poa_numainfo()

#tpb.test_poa_numatune()
#tpb.test_poa_cpupin()
#tpb.test_poa_reboot()

#tpb.test_fpga_prov()

#test_audit()

0 comments on commit 13169bb

Please sign in to comment.