def destroy(self, node_name): if Workspace.check_workspace_exists(node_name): try: node_info = Workspace.get_node_info_in_workspace(node_name) except InfraSimError, e: print e.value return
def test_start_status_restart_status_destroy_status(self): """ CLI test: start, restart and destroy a node and check status respectively """ output_status = {} output_start = run_command("infrasim node start") self.assertEqual(output_start[0], 0) self.assertTrue(Workspace.check_workspace_exists(self.node_name)) output_status["start"] = run_command("infrasim node status") self.assertEqual(output_status["start"][0], 0) output_restart = run_command("infrasim node restart") self.assertEqual(output_restart[0], 0) self.assertTrue(Workspace.check_workspace_exists(self.node_name)) output_status["restart"] = run_command("infrasim node status") self.assertEqual(output_status["restart"][0], 0) output_destroy = run_command("infrasim node destroy") self.assertEqual(output_destroy[0], 0) self.assertFalse(Workspace.check_workspace_exists(self.node_name)) output_status["destroy"] = run_command("infrasim node status") self.assertEqual(output_status["destroy"][0], 0) assert "{}-socat is running".format(self.node_name) in output_start[1] assert "{}-bmc is running".format(self.node_name) in output_start[1] assert "{}-node is running".format(self.node_name) in output_start[1] assert "{}-socat is running".format( self.node_name) in output_status["start"][1] assert "{}-bmc is running".format( self.node_name) in output_status["start"][1] assert "{}-node is running".format( self.node_name) in output_status["start"][1] assert "{}-socat is stopped".format( self.node_name) in output_restart[1] assert "{}-bmc is stopped".format(self.node_name) in output_restart[1] assert "{}-node is stopped".format(self.node_name) in output_restart[1] assert "{}-socat is running".format( self.node_name) in output_restart[1] assert "{}-bmc is running".format(self.node_name) in output_restart[1] assert "{}-node is running".format(self.node_name) in output_restart[1] assert "{}-socat is running".format( self.node_name) in output_status["restart"][1] assert "{}-bmc is running".format( self.node_name) in output_status["restart"][1] assert "{}-node is running".format( self.node_name) in output_status["restart"][1] assert "Node {} runtime workspace is destroyed".format( self.node_name) in output_destroy[1] assert "Node {} runtime workspace doesn't exist".format( self.node_name) in output_status["destroy"][1]
def start(self, node_name): try: if Workspace.check_workspace_exists(node_name): node_info = Workspace.get_node_info_in_workspace(node_name) else: node_info = nm.get_item_info(node_name) node = model.CNode(node_info) self._node_preinit(node) except InfraSimError as e: print e.value logger_cmd.error("cmd res: {}".format(e.value)) return node.start() vnc_port = node_info["compute"].get("vnc_display", 1) + 5900 # get IP address print "Infrasim service started.\n" \ "Node {} graphic interface accessible via: \n" \ "VNC port: {} \n" \ "Either host IP: {} \n" \ "depending on host in which network VNC viewer is running \n"\ "Node log folder: {}". \ format(node.get_node_name(), vnc_port, helper.ip4_addresses(netns=node_info.get("namespace")), infrasim_log.get_log_path(node_name)) logger_cmd.info("cmd res: start node {} OK".format(node_name))
def test_stop_stop(self): """ CLI test: stop a node will hint it's already stopped """ output_start = run_command("infrasim node start") self.assertEqual(output_start[0], 0) self.assertTrue(Workspace.check_workspace_exists(self.node_name)) output_stop_1 = run_command("infrasim node stop") self.assertEqual(output_stop_1[0], 0) self.assertTrue(Workspace.check_workspace_exists(self.node_name)) output_stop_2 = run_command("infrasim node stop") self.assertEqual(output_stop_2[0], 0) self.assertTrue(Workspace.check_workspace_exists(self.node_name)) assert "{}-socat starts to run".format( self.node_name) in output_start[1] assert "{}-bmc starts to run".format(self.node_name) in output_start[1] assert "{}-node is running".format(self.node_name) in output_start[1] assert "{}-socat stop".format(self.node_name) in output_stop_1[1] assert "{}-bmc stop".format(self.node_name) in output_stop_1[1] assert "{}-node stop".format(self.node_name) in output_stop_1[1] assert "[ ] {}-node is stopped".format( self.node_name) in output_stop_2[1] assert "[ ] {}-bmc is stopped".format( self.node_name) in output_stop_2[1] assert "[ ] {}-socat is stopped".format( self.node_name) in output_stop_2[1]
def test_stop_stop(self): """ CLI test: stop a node will hint it's already stopped """ output_start = run_command("infrasim node start") self.assertEqual(output_start[0], 0) self.assertTrue(Workspace.check_workspace_exists(self.node_name)) output_stop_1 = run_command("infrasim node stop") self.assertEqual(output_stop_1[0], 0) self.assertTrue(Workspace.check_workspace_exists(self.node_name)) output_stop_2 = run_command("infrasim node stop") self.assertEqual(output_stop_2[0], 0) self.assertTrue(Workspace.check_workspace_exists(self.node_name)) assert "{}-socat is running".format(self.node_name) in output_start[1] assert "{}-bmc is running".format(self.node_name) in output_start[1] assert "{}-node is running".format(self.node_name) in output_start[1] assert "{}-socat is stopped".format(self.node_name) in output_stop_1[1] assert "{}-bmc is stopped".format(self.node_name) in output_stop_1[1] assert "{}-node is stopped".format(self.node_name) in output_stop_1[1] assert "{}-node is stopped".format(self.node_name) in output_stop_2[1] assert "{}-bmc is stopped".format(self.node_name) in output_stop_2[1] assert "{}-socat is stopped".format(self.node_name) in output_stop_2[1]
def destroy(self, node_name): if Workspace.check_workspace_exists(node_name): try: node_info = Workspace.get_node_info_in_workspace(node_name) except InfraSimError as e: print e.value logger_cmd.error("cmd res: {}".format(e.value)) return else: print "Node {} runtime workspace is not found, destroy action is not applied.".\ format(node_name) logger_cmd.warning( "cmd res: Node {} runtime workspace is not found, " "destroy action is not applied.".format(node_name)) return node = model.CNode(node_info) try: self._node_preinit(node, ignore_check=True) except InfraSimError as e: print(e.value) logger_cmd.error("cmd res: {}".format(e.value)) except Exception as e: print(e) logger_cmd.error("cmd res: {}".format(e)) try: node.stop() node.terminate_workspace() logger_cmd.info("cmd res: destroy node {} OK".format(node_name)) except Exception as e: print( "You have to destroy node {} manually due to fatal error: {}". format(node_name, e))
def destroy(self, node_name): if Workspace.check_workspace_exists(node_name): try: node_info = Workspace.get_node_info_in_workspace(node_name) except InfraSimError, e: print e.value logger_cmd.error("cmd res: {}".format(e.value)) return
def start(self, node_name): try: if Workspace.check_workspace_exists(node_name): node_info = Workspace.get_node_info_in_workspace(node_name) else: node_info = nm.get_node_info(node_name) node = model.CNode(node_info) self._node_preinit(node) except InfraSimError, e: print e.value return
def start(self, node_name): try: if Workspace.check_workspace_exists(node_name): node_info = Workspace.get_node_info_in_workspace(node_name) else: node_info = nm.get_node_info(node_name) node = model.CNode(node_info) self._node_preinit(node) except InfraSimError, e: print e.value logger_cmd.error("cmd res: {}".format(e.value)) return
def test_start_status_restart_status_destroy_status(self): """ CLI test: start, restart and destroy a node and check status respectively """ output_status = {} output_start = run_command("infrasim node start") self.assertEqual(output_start[0], 0) self.assertTrue(Workspace.check_workspace_exists(self.node_name)) output_status["start"] = run_command("infrasim node status") self.assertEqual(output_status["start"][0], 0) output_restart = run_command("infrasim node restart") self.assertEqual(output_restart[0], 0) self.assertTrue(Workspace.check_workspace_exists(self.node_name)) output_status["restart"] = run_command("infrasim node status") self.assertEqual(output_status["restart"][0], 0) output_destroy = run_command("infrasim node destroy") self.assertEqual(output_destroy[0], 0) self.assertFalse(Workspace.check_workspace_exists(self.node_name)) output_status["destroy"] = run_command("infrasim node status") self.assertEqual(output_status["destroy"][0], 0) assert "{}-socat is running".format(self.node_name) in output_start[1] assert "{}-bmc is running".format(self.node_name) in output_start[1] assert "{}-node is running".format(self.node_name) in output_start[1] assert "{}-socat is running".format(self.node_name) in output_status["start"][1] assert "{}-bmc is running".format(self.node_name) in output_status["start"][1] assert "{}-node is running".format(self.node_name) in output_status["start"][1] assert "{}-socat is stopped".format(self.node_name) in output_restart[1] assert "{}-bmc is stopped".format(self.node_name) in output_restart[1] assert "{}-node is stopped".format(self.node_name) in output_restart[1] assert "{}-socat is running".format(self.node_name) in output_restart[1] assert "{}-bmc is running".format(self.node_name) in output_restart[1] assert "{}-node is running".format(self.node_name) in output_restart[1] assert "{}-socat is running".format(self.node_name) in output_status["restart"][1] assert "{}-bmc is running".format(self.node_name) in output_status["restart"][1] assert "{}-node is running".format(self.node_name) in output_status["restart"][1] assert "Node {} runtime workspace is destroyed".format(self.node_name) in output_destroy[1] assert "Node {} runtime workspace doesn't exist".format(self.node_name) in output_status["destroy"][1]
def test_init(self): """ CLI test: test init "-f" which will remove existing workspace """ output_info = {} output_start = run_command("infrasim node start") self.assertEqual(output_start[0], 0) self.assertTrue(Workspace.check_workspace_exists(self.node_name)) output_info['start'] = run_command("infrasim node info") self.assertEqual(output_info['start'][0], 0) self.assertRaises(CommandRunFailed, run_command, cmd="infrasim init -s", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) run_command("infrasim init -s -f") self.assertEqual(len(os.listdir(config.infrasim_home)), 1) # Verify if it will reinstall packages when user confirmed 'Y' result = run_command_with_user_input("infrasim init", True, subprocess.PIPE, subprocess.PIPE, subprocess.PIPE, "Y\nY\nY\n") assert "downloading Infrasim_Qemu" in result[1] assert "downloading OpenIpmi" in result[1] assert "downloading Seabios" in result[1] result = run_command_with_user_input("infrasim init", True, subprocess.PIPE, subprocess.PIPE, subprocess.PIPE, "Y\nyes\nn\n") assert "downloading Infrasim_Qemu" in result[1] assert "downloading OpenIpmi" in result[1] assert "downloading Seabios" not in result[1] result = run_command_with_user_input("infrasim init", True, subprocess.PIPE, subprocess.PIPE, subprocess.PIPE, "no\nN\nY\n") assert "downloading Infrasim_Qemu" not in result[1] assert "downloading OpenIpmi" not in result[1] assert "downloading Seabios" in result[1]
def check_node_start_workspace(node_name): conf = Workspace.get_node_info_in_workspace(node_name) node_root = os.path.join(config.infrasim_home, conf["name"]) # Check node data folder and files exist node_type = conf["type"] data_folder = os.path.join(node_root, "data") node_emu = os.path.join(data_folder, "{}.emu".format(node_type)) node_bios = os.path.join(data_folder, "{}_smbios.bin".format(node_type)) assert os.path.exists(data_folder) is True assert os.path.exists(node_emu) is True assert os.path.exists(node_bios) is True # Check node script folder and files exist script_folder = os.path.join(node_root, "scripts") script_chassiscontrol = os.path.join(script_folder, "chassiscontrol") script_lancontrol = os.path.join(script_folder, "lancontrol") script_startcmd = os.path.join(script_folder, "startcmd") script_stopcmd = os.path.join(script_folder, "stopcmd") script_resetcmd = os.path.join(script_folder, "resetcmd") assert os.path.exists(script_folder) is True assert os.path.exists(script_chassiscontrol) is True assert os.path.exists(script_lancontrol) is True assert os.path.exists(script_startcmd) is True assert os.path.exists(script_stopcmd) is True assert os.path.exists(script_resetcmd) is True # Check etc folder and files exist etc_folder = os.path.join(node_root, "etc") etc_infrasim_yml = os.path.join(etc_folder, "infrasim.yml") etc_vbmc_conf = os.path.join(etc_folder, "vbmc.conf") assert os.path.exists(etc_folder) is True assert os.path.exists(etc_infrasim_yml) is True assert os.path.exists(etc_vbmc_conf) is True # Check disk image exist node_name = conf["name"] node_drive = conf['compute']['storage_backend'][0]['drives'] # set drive 'file' for i in range(1, len(node_drive) + 1): disk_file = os.path.join(node_root, "sd{0}.img".format(chr(96 + i))) assert os.path.exists(disk_file) is True # Check serial device exist serial_dev = os.path.join(node_root, ".pty0_{}".format(node_name)) assert os.path.exists(serial_dev) is True # Check unix socket file serial = os.path.join(node_root, ".serial") assert os.path.exists(serial) is True # Check node runtime pid file exist node_socat = os.path.join(node_root, ".{}-socat.pid".format(node_name)) node_ipmi = os.path.join(node_root, ".{}-bmc.pid".format(node_name)) node_qemu = os.path.join(node_root, ".{}-node.pid".format(node_name)) assert os.path.exists(node_socat) is True assert os.path.exists(node_ipmi) is True assert os.path.exists(node_qemu) is True
def init(self): node = get_dir_list(infrasim_home) # remove the file whose name starts from "." for nd in node: if nd[0] is not '.' and Workspace.check_node(nd): nd_status = NodeStatus(nd) self.__node_list.append(nd_status)
def info(self, node_name, type=True): try: node_info = Workspace.get_node_info_in_workspace(node_name) node_info_net = node_info['compute']['networks'] node_info_stor = node_info['compute']['storage_backend'] print "{:<20}{}\n" \ "{:<20}{}\n" \ "{:<20}{}\n" \ "{:<20}{}\n" \ "{:<20}{}\n" \ "{:<20}{}" . \ format("node name:", node_name, "type:", node_info['type'], "memory size:", node_info['compute']['memory']['size'], "sol_enable:", node_info['sol_enable'], "cpu quantities:", node_info['compute']['cpu']['quantities'], "cpu type:", node_info['compute']['cpu']['type']) print "{:<20}".format("network(s):") + "-" * 22 table = Texttable() table_type = ~(Texttable.HEADER | Texttable.BORDER | Texttable.HLINES | Texttable.VLINES) table.set_deco(table_type) table.set_cols_align(["l", "l", "l", "l"]) row = [] row.append([" " * 17, "device", "mode", "name"]) for i in range(1, len(node_info_net) + 1): net = node_info_net[i - 1] row.append([ " " * 17, net['device'], net['network_mode'], net['network_name'] ]) table.add_rows(row) print table.draw() print "{:<20}".format("storage backend:") + "-" * 30 table = Texttable() table.set_deco(table_type) table.set_cols_align(["l", "l", "l", "l"]) row = [] row.append([" " * 17, "type", "max drive", "drive size"]) for i in range(1, len(node_info_stor) + 1): stor = node_info_stor[i - 1] node_info_drives = stor['drives'] for j in range(1, len(node_info_drives) + 1): drive = node_info_drives[j - 1] if j % stor['max_drive_per_controller'] == 1: row.append([ " " * 17, stor['type'], stor['max_drive_per_controller'], drive['size'] ]) else: row.append([" " * 17, "", "", drive['size']]) table.add_rows(row) print table.draw() logger_cmd.info("cmd res: get node {} info OK".format(node_name)) except InfraSimError, e: print e.value logger_cmd.error("cmd res: {}".format(e.value)) return
def terminate_workspace(self): if Workspace.check_workspace_exists(self.__node_name): shutil.rmtree(self.workspace.get_workspace()) self.__logger.info( "[Node] Node {} runtime workspcace is destroyed".format( self.__node_name)) print "Node {} runtime workspace is destroyed.".format( self.__node_name)
def wrapper(*args, **kwargs): frame = inspect.currentframe() frame_args, _, _, values = inspect.getargvalues(frame) node_name = values["args"][1] if not Workspace.check_workspace_exists(node_name): print "Node {} runtime workspace doesn't exist".format(node_name) return return func(*args, **kwargs)
def test_start_destroy(self): """ CLI test: start, then destroy the node directly """ output_start = run_command("infrasim node start") self.assertEqual(output_start[0], 0) self.assertTrue(Workspace.check_workspace_exists(self.node_name)) output_destroy = run_command("infrasim node destroy") self.assertEqual(output_destroy[0], 0) self.assertFalse(Workspace.check_workspace_exists(self.node_name)) assert "{}-socat is running".format(self.node_name) in output_start[1] assert "{}-bmc is running".format(self.node_name) in output_start[1] assert "{}-node is running".format(self.node_name) in output_start[1] assert "Node {} runtime workspace is destroyed".format(self.node_name) in output_destroy[1]
def status(self, node_name): try: node_info = Workspace.get_node_info_in_workspace(node_name) node = model.CNode(node_info) self._node_preinit(node, ignore_check=True) node.status() except InfraSimError, e: print e.value
def destroy(self, chassis_name=None): if Workspace.check_workspace_exists(chassis_name): try: chassis_info = ChassisWorkspace.get_chassis_info_in_workspace(chassis_name) except InfraSimError, e: print e.value logger_cmd.error("cmd res: {}".format(e.value)) return
def test_destory_destroy(self): """ CLI test: fail to destroy a destroyed node """ output_start = run_command("infrasim node start") self.assertEqual(output_start[0], 0) self.assertTrue(Workspace.check_workspace_exists(self.node_name)) output_destroy_1 = run_command("infrasim node destroy") self.assertEqual(output_destroy_1[0], 0) self.assertFalse(Workspace.check_workspace_exists(self.node_name)) output_destroy_2 = run_command("infrasim node destroy") self.assertEqual(output_destroy_2[0], 0) self.assertFalse(Workspace.check_workspace_exists(self.node_name)) assert "Node {} runtime workspace is destroyed".format(self.node_name) in output_destroy_1[1] assert "Node {} runtime workspace is not found".format(self.node_name) in output_destroy_2[1]
def wrapper(*args, **kwargs): frame = inspect.currentframe() frame_args, _, _, values = inspect.getargvalues(frame) node_name = values["args"][1] if not Workspace.check_workspace_exists(node_name): print "Node {} runtime workspace doesn't exist".format(node_name) logger_cmd.warning("cmd res: Node {} runtime workspace doesn't exist".format(node_name)) return return func(*args, **kwargs)
def test_start_info_destroy_info(self): """ CLI test: start and destroy a node, and get node_info respectively """ output_info = {} output_start = run_command("infrasim node start") self.assertEqual(output_start[0], 0) self.assertTrue(Workspace.check_workspace_exists(self.node_name)) output_info['start'] = run_command("infrasim node info") self.assertEqual(output_info['start'][0], 0) output_destroy = run_command("infrasim node destroy") self.assertEqual(output_destroy[0], 0) self.assertFalse(Workspace.check_workspace_exists(self.node_name)) output_info['destroy'] = run_command("infrasim node info") self.assertEqual(output_info['destroy'][0], 0) assert "{}-socat starts to run".format( self.node_name) in output_start[1] assert "{}-bmc starts to run".format(self.node_name) in output_start[1] assert "{}-node is running".format(self.node_name) in output_start[1] assert "node name: {}".format( self.node_name) in output_info['start'][1] assert "type:" in output_info['start'][1] assert "memory size:" in output_info['start'][1] assert "sol_enable:" in output_info['start'][1] assert "cpu quantities:" in output_info['start'][1] assert "cpu type:" in output_info['start'][1] assert "network(s):" in output_info['start'][1] assert "device" in output_info['start'][1] assert "mode" in output_info['start'][1] assert "name " in output_info['start'][1] assert "storage backend:" in output_info['start'][1] assert "type " in output_info['start'][1] assert "max drive" in output_info['start'][1] assert "drive size" in output_info['start'][1] assert "Node {} runtime workspace is destroyed".format( self.node_name) in output_destroy[1] assert "Node {} runtime workspace doesn't exist".format( self.node_name) in output_info['destroy'][1]
def status(self, node_name): try: node_info = Workspace.get_node_info_in_workspace(node_name) node = model.CNode(node_info) self._node_preinit(node, ignore_check=True) node.status() logger_cmd.info("cmd res: get node {} status OK".format(node_name)) except InfraSimError, e: print e.value logger_cmd.error("cmd res: {}".format(e.value))
def test_start_start(self): """ CLI test: start a started node will hint it's running """ output_start_1 = run_command("infrasim node start") self.assertEqual(output_start_1[0], 0) self.assertTrue(Workspace.check_workspace_exists(self.node_name)) output_start_2 = run_command("infrasim node start") self.assertEqual(output_start_2[0], 0) self.assertTrue(Workspace.check_workspace_exists(self.node_name)) assert "{}-socat starts to run".format(self.node_name) in output_start_1[1] assert "{}-bmc starts to run".format(self.node_name) in output_start_1[1] assert "{}-node is running".format(self.node_name) in output_start_1[1] assert "{}-socat is already running".format(self.node_name) in output_start_2[1] assert "{}-bmc is already running".format(self.node_name) in output_start_2[1] assert "{}-node is running".format(self.node_name) in output_start_2[1]
def test_start_start(self): """ CLI test: start a started node will hint it's running """ output_start_1 = run_command("infrasim node start") self.assertEqual(output_start_1[0], 0) self.assertTrue(Workspace.check_workspace_exists(self.node_name)) output_start_2 = run_command("infrasim node start") self.assertEqual(output_start_2[0], 0) self.assertTrue(Workspace.check_workspace_exists(self.node_name)) assert "{}-socat is running".format(self.node_name) in output_start_1[1] assert "{}-bmc is running".format(self.node_name) in output_start_1[1] assert "{}-node is running".format(self.node_name) in output_start_1[1] assert "{}-socat is running".format(self.node_name) in output_start_2[1] assert "{}-bmc is running".format(self.node_name) in output_start_2[1] assert "{}-node is running".format(self.node_name) in output_start_2[1]
def destroy_existing_nodes(): nodes = os.listdir(config.infrasim_home) if os.path.exists(config.infrasim_node_config_map): nodes.remove('.node_map') if os.path.exists(config.infrasim_chassis_config_map): nodes.remove('.chassis_map') for node in nodes: if Workspace.check_node(node): os.system("infrasim node destroy {}".format(node)) else: os.system("infrasim chassis destroy {}".format(node))
def info(self, node_name, type=True): try: node_info = Workspace.get_node_info_in_workspace(node_name) node_info_net = node_info['compute']['networks'] node_info_stor = node_info['compute']['storage_backend'] print "{:<20}{}\n" \ "{:<20}{}\n" \ "{:<20}{}\n" \ "{:<20}{}\n" \ "{:<20}{}\n" \ "{:<20}{}" . \ format("node name:", node_name, "type:", node_info['type'], "memory size:", node_info['compute']['memory']['size'], "sol_enable:", node_info['sol_enable'], "cpu quantities:", node_info['compute']['cpu']['quantities'], "cpu type:", node_info['compute']['cpu']['type']) print "{:<20}".format("network(s):") + "-" * 22 table = Texttable() table_type = ~(Texttable.HEADER | Texttable.BORDER | Texttable.HLINES | Texttable.VLINES) table.set_deco(table_type) table.set_cols_align(["l", "l", "l", "l"]) row = [] row.append([" " * 17, "device", "mode", "name"]) for i in range(1, len(node_info_net) + 1): net = node_info_net[i-1] row.append([" " * 17, net['device'], net['network_mode'], net['network_name']]) table.add_rows(row) print table.draw() print "{:<20}".format("storage backend:") + "-" * 30 table = Texttable() table.set_deco(table_type) table.set_cols_align(["l", "l", "l", "l"]) row = [] row.append([" " * 17, "type", "max drive", "drive size"]) for i in range(1, len(node_info_stor) + 1): stor = node_info_stor[i - 1] node_info_drives = stor['controller']['drives'] for j in range(1, len(node_info_drives) + 1): drive = node_info_drives[j - 1] if j == 1: row.append([" " * 17, stor['controller']['type'], stor['controller']['max_drive_per_controller'], drive['size']]) else: row.append([" " * 17, "", "", drive['size']]) table.add_rows(row) print table.draw() except InfraSimError, e: print e.value return
def test_start_info_destroy_info(self): """ CLI test: start and destroy a node, and get node_info respectively """ output_info = {} output_start = run_command("infrasim node start") self.assertEqual(output_start[0], 0) self.assertTrue(Workspace.check_workspace_exists(self.node_name)) output_info['start'] = run_command("infrasim node info") self.assertEqual(output_info['start'][0], 0) output_destroy = run_command("infrasim node destroy") self.assertEqual(output_destroy[0], 0) self.assertFalse(Workspace.check_workspace_exists(self.node_name)) output_info['destroy'] = run_command("infrasim node info") self.assertEqual(output_info['destroy'][0], 0) assert "{}-socat is running".format(self.node_name) in output_start[1] assert "{}-bmc is running".format(self.node_name) in output_start[1] assert "{}-node is running".format(self.node_name) in output_start[1] assert "node name: {}".format(self.node_name) in output_info['start'][1] assert "type:" in output_info['start'][1] assert "memory size:" in output_info['start'][1] assert "sol_enable:" in output_info['start'][1] assert "cpu quantities:" in output_info['start'][1] assert "cpu type:" in output_info['start'][1] assert "network(s):" in output_info['start'][1] assert "device" in output_info['start'][1] assert "mode" in output_info['start'][1] assert "name " in output_info['start'][1] assert "storage backend:" in output_info['start'][1] assert "type " in output_info['start'][1] assert "max drive" in output_info['start'][1] assert "drive size" in output_info['start'][1] assert "Node {} runtime workspace is destroyed".format(self.node_name) in output_destroy[1] assert "Node {} runtime workspace doesn't exist".format(self.node_name) in output_info['destroy'][1]
def get_node_status(self): self.__node_info = Workspace.get_node_info_in_workspace(self.__node_name) self.netns = self.__node_info.get("namespace", None) base_path = os.path.join(infrasim_home, self.__node_name) task_name = ['socat', 'bmc', 'node', 'racadm', 'ipmi_console'] task_list = {} for task in task_name: if task is 'ipmi_console': pid_file = os.path.join(base_path, '.ipmi_console.pid') else: pid_file = os.path.join(base_path, ".{}-{}.pid".format(self.__node_name, task)) if os.path.exists(pid_file): task_pid = get_task_pid(pid_file) if not os.path.exists("/proc/{}".format(task_pid)): os.remove(pid_file) elif task_pid > 0: task_list[task] = "{:<6}".format(task_pid) return task_list
def update(self, node_name, config_file): try: nm.update(node_name, config_file) logger_cmd.info("cmd res: update node {} OK".format(node_name)) except InfraSimError as e: print e.value logger_cmd.error("cmd res: {}".format(e.value)) if Workspace.check_workspace_exists(node_name): print "Node {0} runtime workspace exists.\n" \ "If you want to apply updated configuration, please destroy node runtime workspace first.\n" \ "You can run commands: \n" \ " infrasim node destroy {0}\n" \ " infrasim node start {0}".format(node_name) logger_cmd.warning( "Node {0} runtime workspace exists. " "Need to destroy node runtime workspace first".format( node_name))
def update(self, name, config_file): try: mm = self._get_map_manager(name, config_file) mm.update(name, config_file) logger_cmd.info("cmd res: update {} OK".format(name)) except InfraSimError as e: print e.value logger_cmd.error("cmd res: {}".format(e.value)) if Workspace.check_workspace_exists(name): print "{1} {0} runtime workspace exists.\n" \ "If you want to apply updated configuration, please destroy runtime workspace first.\n" \ "You can run commands: \n" \ " infrasim {1} destroy {0}\n" \ " infrasim {1} start {0}".format(name, mm.get_type()) logger_cmd.warning( "{1} {0} runtime workspace exists. " "Need to destroy {1} runtime workspace first".format( name, mm.get_type()))
def test_init(self): """ CLI test: test init "-f" which will remove existing workspace """ output_info = {} output_start = run_command("infrasim node start") self.assertEqual(output_start[0], 0) self.assertTrue(Workspace.check_workspace_exists(self.node_name)) output_info['start'] = run_command("infrasim node info") self.assertEqual(output_info['start'][0], 0) self.assertRaises(CommandRunFailed, run_command, cmd="infrasim init -s", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) run_command("infrasim init -s -f") self.assertEqual(len(os.listdir(config.infrasim_home)), 1)
def edit(self, chassis_name): if chassis_name not in cm.get_name_list(): print "Fail to find chassis {0} configuration. It is not registered. Check by:\n" \ " infrasim config list".format(chassis_name) logger_cmd.warning("cmd res: Fail to find chassis {0} configuration. It is not registered. Check by:\n" " infrasim config list".format(chassis_name)) return editor = os.environ.get('EDITOR', 'vi') config_path = os.path.join(cm.get_mapping_folder(), "{}.yml".format(chassis_name)) try: os.system("{} {}".format(editor, config_path)) if Workspace.check_workspace_exists(chassis_name): print "Warning: " \ "\033[93mPlease destroy chassis {}\033[0m " \ "before start or restart, " \ "or this edit won't work." .format(chassis_name) logger_cmd.info("cmd res: edit chassis {} OK".format(chassis_name)) except OSError, e: print e logger_cmd.error("cmd res: {}".format(e))
def edit(self, node_name): if node_name not in nm.get_name_list(): print "Fail to find node {0} configuration. It is not registered. Check by:\n" \ " infrasim config list".format(node_name) logger_cmd.warning("cmd res: Fail to find node {0} configuration. It is not registered. Check by:\n" " infrasim config list".format(node_name)) return editor = os.environ.get('EDITOR', 'vi') config_path = os.path.join(nm.get_mapping_folder(), "{}.yml".format(node_name)) try: os.system("{} {}".format(editor, config_path)) if Workspace.check_workspace_exists(node_name): print "Warning: " \ "\033[93mPlease destroy node {}\033[0m " \ "before start or restart, " \ "or this edit won't work." .format(node_name) logger_cmd.info("cmd res: edit node {} OK".format(node_name)) except OSError, e: print e logger_cmd.error("cmd res: {}".format(e))
def destroy(self, chassis_name=None): if Workspace.check_workspace_exists(chassis_name): try: chassis_info = ChassisWorkspace.get_chassis_info_in_workspace( chassis_name) except InfraSimError as e: print e.value logger_cmd.error("cmd res: {}".format(e.value)) return else: print "Chassis {} runtime workspace is not found, destroy action is not applied.".\ format(chassis_name) logger_cmd.warning( "cmd res: Chassis {} runtime workspace is not found, " "destroy action is not applied.".format(chassis_name)) return chassis = model.CChassis(chassis_name, chassis_info) try: chassis.init() chassis.destroy() except InfraSimError as e: print e.value logger_cmd.error("cmd res: {}".format(e.value)) logger_cmd.info("cmd res: destroy chassis {} OK".format(chassis_name))
def test_init(self): """ CLI test: test init "-f" which will remove existing workspace """ output_info = {} output_start = run_command("infrasim node start") self.assertEqual(output_start[0], 0) self.assertTrue(Workspace.check_workspace_exists(self.node_name)) output_info['start'] = run_command("infrasim node info") self.assertEqual(output_info['start'][0], 0) self.assertRaises(CommandRunFailed, run_command, cmd="infrasim init -s", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) run_command("infrasim init -s -f") dirs = [ i for i in os.listdir(config.infrasim_home) if i.startswith('.') is False ] self.assertEqual(len(dirs), 0) # install specific versions package_list = [{ "name": "infrasim-qemu", "version": "2.10.1-ubuntu-xenial-1.0.251" }, { "name": "infrasim-openipmi", "version": "2.0.24-1.4.79ubuntu16.04.1" }] pm = PackageManager(only_upgrade=False, force=True) for pkg in package_list: pm.do_install(pkg.get('name'), version=pkg.get('version')) for pkg in package_list: rc, output = run_command('dpkg -s {} | grep \"^Version:\"'.format( pkg.get('name'))) reobj = re.search(r'^Version:\s?(?P<version>.*)', output.strip()) assert reobj assert pkg.get('version') == reobj.groupdict().get('version') # upgrade to latest version package_list = [{ "name": "infrasim-qemu", "version": "latest" }, { "name": "infrasim-openipmi", "version": "latest" }] for pkg in package_list: pm.do_install(pkg.get('name'), version=pkg.get('version')) for pkg in package_list: rc, output = run_command('dpkg -s {} | grep \"^Version:\"'.format( pkg.get('name'))) reobj = re.search(r'^Version:\s?(?P<version>.*)', output.strip()) assert reobj assert pkg.get('version') != reobj.groupdict().get('version') # downgrade to specific versions package_list = [{ "name": "infrasim-qemu", "version": "2.10.1-ubuntu-xenial-1.0.251" }, { "name": "infrasim-openipmi", "version": "2.0.24-1.4.79ubuntu16.04.1" }] for pkg in package_list: pm.do_install(pkg.get('name'), version=pkg.get('version')) for pkg in package_list: rc, output = run_command('dpkg -s {} | grep \"^Version:\"'.format( pkg.get('name'))) reobj = re.search(r'^Version:\s?(?P<version>.*)', output.strip()) assert reobj assert pkg.get('version') == reobj.groupdict().get('version') # uninstall the versions for pkg in package_list: pm.do_remove(pkg.get('name')) # update cache pm.init() for pkg in package_list: self.assertRaises(CommandRunFailed, run_command, cmd='dpkg -l | grep {}'.format(pkg.get('name')), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # install the packages again for the subsequent tests package_list = [{ "name": "infrasim-qemu", "version": "latest" }, { "name": "infrasim-openipmi", "version": "latest" }, { "name": "infrasim-seabios", "version": "latest" }] for pkg in package_list: pm.do_install(pkg.get('name'), version=pkg.get('version')) for pkg in package_list: rc, output = run_command('dpkg -s {} | grep \"^Version:\"'.format( pkg.get('name'))) reobj = re.search(r'^Version:\s?(?P<version>.*)', output.strip()) # make sure the package is installed assert reobj
def init_env(instance): """ This is to sync ipmi-console with runtime vBMC configuration. Initial version capture infrasim instance name by infrasim-main status, while we have a plan to give instance name to ipmi-console so that it can be attached to target vBMC instance. """ # Threading quit flag initilization env.local_env.quit_flag = False # Verify node instance runtime cur_path = os.environ["PATH"] os.environ["PATH"] = "{}/bin:{}".format(os.environ.get("PYTHONPATH"), cur_path) if not Workspace.check_workspace_exists(instance): logger.error("Warning: there is no node {} workspace. " "Please start node {} first.".format(instance, instance)) raise IpmiError( "Warning: there is no node {} workspace. " "Please start node {} first.".format(instance, instance)) try: with open("{}/{}/.{}-bmc.pid".format( config.infrasim_home, instance, instance), "r") as f: pid = f.readline().strip() if not os.path.exists("/proc/{}".format(pid)): raise Exception except Exception: logger.error("Warning: node {} has not started BMC. " "Please start node {} first.".format(instance, instance)) raise IpmiError( "Warning: node {} has not started BMC. " "Please start node {} first.".format(instance, instance)) logger.info("Init ipmi-console environment for infrasim instance: {}". format(instance)) # Get runtime vbmc.conf vbmc_conf_path = os.path.join(config.infrasim_home, instance, "etc", "vbmc.conf") if not os.path.exists(vbmc_conf_path): msg = "{} vBMC configuration is not defined at {}".format(instance, vbmc_conf_path) logger.error(msg) raise Exception(msg) else: msg = "Target vbmc to attach is: {}".format(vbmc_conf_path) logger.info(msg) # Get runtime infrasim.yml infrasim_yml_path = os.path.join(config.infrasim_home, instance, "etc", "infrasim.yml") if not os.path.exists(infrasim_yml_path): msg = "{} infrasim instance is not defined at {}".format(instance, infrasim_yml_path) logger.error(msg) raise Exception(msg) else: msg = "Target infrasim instance to attach is: {}".format(infrasim_yml_path) logger.info(msg) # Get variable and set to ipmi-console env # - PORT_TELNET_TO_VBMC # - VBMC_IP # - VBMC_PORT with open(vbmc_conf_path, 'r') as fp: conf = fp.read() p_telnet = re.compile(r"^\s*console\s*[\d:\.]+\s+(?P<port_telnet_to_vbmc>\d+)", re.MULTILINE) s_telnet = p_telnet.search(conf) if s_telnet: env.PORT_TELNET_TO_VBMC = int(s_telnet.group("port_telnet_to_vbmc")) logger.info("PORT_TELNET_TO_VBMC: {}".format(env.PORT_TELNET_TO_VBMC)) else: logger.error("PORT_TELNET_TO_VBMC is not found") raise Exception("PORT_TELNET_TO_VBMC is not found") p_vbmc = re.compile(r"^\s*addr\s*(?P<vbmc_ip>[\d:\.]+)\s*(?P<vbmc_port>\d+)", re.MULTILINE) s_vbmc = p_vbmc.search(conf) if s_vbmc: ip = s_vbmc.group("vbmc_ip") if ip == "::" or ip == "0.0.0.0": env.VBMC_IP = "localhost" else: env.VBMC_IP = ip logger.info("VBMC_IP: {}".format(env.VBMC_IP)) env.VBMC_PORT = int(s_vbmc.group("vbmc_port")) logger.info("VBMC_PORT: {}".format(env.VBMC_PORT)) else: logger.error("VBMC_IP and VBMC_PORT is not found") raise Exception("VBMC_IP and VBMC_PORT is not found") # Get variable and set to ipmi-console env # - PORT_SSH_FOR_CLIENT with open(infrasim_yml_path, 'r') as fp: conf = fp.read() p_port = re.compile(r"^\s*ipmi_console_ssh:\s*(?P<port_ssh_for_client>\d+)", re.MULTILINE) s_port = p_port.search(conf) if s_port: env.PORT_SSH_FOR_CLIENT = int(s_port.group("port_ssh_for_client")) else: env.PORT_SSH_FOR_CLIENT = 9300 logger.info("PORT_SSH_FOR_CLIENT: {}".format(env.PORT_SSH_FOR_CLIENT)) # check if ipmi_console_ssh port is in use if helper.check_if_port_in_use("0.0.0.0", env.PORT_SSH_FOR_CLIENT): logger.error("ssh port {} is already in use.".format(env.PORT_SSH_FOR_CLIENT)) raise IpmiError("ssh port {} is already in use.".format(env.PORT_SSH_FOR_CLIENT))
class CNode(object): def __init__(self, node_info=None): self.__tasks_list = [] self.__node = node_info self.__node_name = "" self.workspace = None self.__sol_enabled = None self.__netns = None self.__logger = infrasim_log.get_logger(LoggerType.model.value) @property def netns(self): return self.__netns def get_task_list(self): return self.__tasks_list def get_node_name(self): return self.__node_name def set_node_name(self, name): self.__node_name = name def get_node_info(self): return self.__node @run_in_namespace def precheck(self): if self.__is_running(): return for task in self.__tasks_list:\ task.precheck() def terminate_workspace(self): if Workspace.check_workspace_exists(self.__node_name): shutil.rmtree(self.workspace.get_workspace()) self.__logger.info("[Node] Node {} runtime workspcace is destroyed". format(self.__node_name)) print "Node {} runtime workspace is destroyed.".format(self.__node_name) def init(self): """ 1. Prepare CNode attributes: - self.__node 2. Then use this information to init workspace 3. Use this information to init sub module """ if 'name' in self.__node: self.set_node_name(self.__node['name']) else: raise ArgsNotCorrect("[Node] No node name is given in node information.") self.__logger = infrasim_log.get_logger(LoggerType.model.value, self.__node_name) if self.__node['compute'] is None: raise ArgsNotCorrect("[Node] No compute information") if 'sol_enable' not in self.__node: self.__node['sol_enable'] = True self.__sol_enabled = self.__node['sol_enable'] self.__netns = self.__node.get("namespace") # If user specify "network_mode" as "bridge" but without MAC # address, generate one for this network. for network in self.__node['compute']['networks']: if 'mac' not in network: uuid_val = uuid.uuid4() str1 = str(uuid_val)[-2:] str2 = str(uuid_val)[-4:-2] str3 = str(uuid_val)[-6:-4] network['mac'] = ":".join(["52:54:BE", str1, str2, str3]) # If user specify "nmve" controller(drive) in "storage_backend" # with NO serial, generate one for it, since QEMU now (2.10.1) # treat "serail" as a mandatory attribute for "nvme" for storage in self.__node['compute']['storage_backend']: if storage.get('type') == 'nvme': if not storage.get('serial', ''): storage['serial'] = helper.random_serial() if self.__sol_enabled: socat_obj = CSocat() socat_obj.logger = self.__logger socat_obj.set_priority(0) socat_obj.set_task_name("{}-socat".format(self.__node_name)) self.__tasks_list.append(socat_obj) bmc_info = self.__node.get('bmc', {}) bmc_obj = CBMC(bmc_info) bmc_obj.logger = self.__logger bmc_obj.set_priority(1) bmc_obj.set_task_name("{}-bmc".format(self.__node_name)) bmc_obj.enable_sol(self.__sol_enabled) bmc_obj.set_log_path("/var/log/infrasim/{}/openipmi.log". format(self.__node_name)) bmc_obj.set_node_name(self.__node['name']) self.__tasks_list.append(bmc_obj) compute_obj = CCompute(self.__node['compute']) compute_obj.logger = self.__logger asyncr = bmc_info.get("startnow", True) compute_obj.set_asyncronous(asyncr) compute_obj.enable_sol(self.__sol_enabled) compute_obj.set_priority(2) compute_obj.set_task_name("{}-node".format(self.__node_name)) compute_obj.set_log_path("/var/log/infrasim/{}/qemu.log". format(self.__node_name)) self.__tasks_list.append(compute_obj) if "type" in self.__node and "dell" in self.__node["type"]: racadm_info = self.__node.get("racadm", {}) racadm_obj = CRacadm(racadm_info) racadm_obj.logger = self.__logger racadm_obj.set_priority(3) racadm_obj.set_node_name(self.__node_name) racadm_obj.set_task_name("{}-racadm".format(self.__node_name)) racadm_obj.set_log_path("/var/log/infrasim/{}/racadm.log". format(self.__node_name)) self.__tasks_list.append(racadm_obj) # Set interface if "type" not in self.__node: raise ArgsNotCorrect("[Node] Can't get infrasim type") else: bmc_obj.set_type(self.__node['type']) compute_obj.set_type(self.__node['type']) if self.__sol_enabled: if "sol_device" in self.__node: socat_obj.set_sol_device(self.__node["sol_device"]) bmc_obj.set_sol_device(self.__node["sol_device"]) if "serial_socket" not in self.__node: self.__node["serial_socket"] = os.path.join(config.infrasim_home, self.__node["name"], ".serial") socat_obj.set_socket_serial(self.__node["serial_socket"]) compute_obj.set_socket_serial(self.__node["serial_socket"]) if "ipmi_console_port" in self.__node: bmc_obj.set_port_ipmi_console(self.__node["ipmi_console_port"]) # ipmi-console shall connect to same port with the same conf file if "bmc_connection_port" in self.__node: bmc_obj.set_port_qemu_ipmi(self.__node["bmc_connection_port"]) compute_obj.set_port_qemu_ipmi(self.__node["bmc_connection_port"]) self.workspace = Workspace(self.__node) for task in self.__tasks_list: task.set_workspace(self.workspace.get_workspace()) if not self.__is_running(): self.workspace.init() if self.__netns: for task in self.__tasks_list: task.netns = self.__netns for task in self.__tasks_list: task.init() # Run tasks list as the priority def start(self): # sort the tasks as the priority self.__tasks_list.sort(key=lambda x: x.get_priority(), reverse=False) for task in self.__tasks_list: task.run() def stop(self): # sort the tasks as the priority in reversed sequence self.__tasks_list.sort(key=lambda x: x.get_priority(), reverse=True) for task in self.__tasks_list: task.terminate() def status(self): for task in self.__tasks_list: task.status() def __is_running(self): state = False for task in self.__tasks_list: state |= task._task_is_running() return state
def init(self): """ 1. Prepare CNode attributes: - self.__node 2. Then use this information to init workspace 3. Use this information to init sub module """ if 'name' in self.__node: self.set_node_name(self.__node['name']) else: raise ArgsNotCorrect("[Node] No node name is given in node information.") self.__logger = infrasim_log.get_logger(LoggerType.model.value, self.__node_name) if self.__node['compute'] is None: raise ArgsNotCorrect("[Node] No compute information") if 'sol_enable' not in self.__node: self.__node['sol_enable'] = True self.__sol_enabled = self.__node['sol_enable'] self.__netns = self.__node.get("namespace") # If user specify "network_mode" as "bridge" but without MAC # address, generate one for this network. for network in self.__node['compute']['networks']: if 'mac' not in network: uuid_val = uuid.uuid4() str1 = str(uuid_val)[-2:] str2 = str(uuid_val)[-4:-2] str3 = str(uuid_val)[-6:-4] network['mac'] = ":".join(["52:54:BE", str1, str2, str3]) # If user specify "nmve" controller(drive) in "storage_backend" # with NO serial, generate one for it, since QEMU now (2.10.1) # treat "serail" as a mandatory attribute for "nvme" for storage in self.__node['compute']['storage_backend']: if storage.get('type') == 'nvme': if not storage.get('serial', ''): storage['serial'] = helper.random_serial() if self.__sol_enabled: socat_obj = CSocat() socat_obj.logger = self.__logger socat_obj.set_priority(0) socat_obj.set_task_name("{}-socat".format(self.__node_name)) self.__tasks_list.append(socat_obj) bmc_info = self.__node.get('bmc', {}) bmc_obj = CBMC(bmc_info) bmc_obj.logger = self.__logger bmc_obj.set_priority(1) bmc_obj.set_task_name("{}-bmc".format(self.__node_name)) bmc_obj.enable_sol(self.__sol_enabled) bmc_obj.set_log_path("/var/log/infrasim/{}/openipmi.log". format(self.__node_name)) bmc_obj.set_node_name(self.__node['name']) self.__tasks_list.append(bmc_obj) compute_obj = CCompute(self.__node['compute']) compute_obj.logger = self.__logger asyncr = bmc_info.get("startnow", True) compute_obj.set_asyncronous(asyncr) compute_obj.enable_sol(self.__sol_enabled) compute_obj.set_priority(2) compute_obj.set_task_name("{}-node".format(self.__node_name)) compute_obj.set_log_path("/var/log/infrasim/{}/qemu.log". format(self.__node_name)) self.__tasks_list.append(compute_obj) if "type" in self.__node and "dell" in self.__node["type"]: racadm_info = self.__node.get("racadm", {}) racadm_obj = CRacadm(racadm_info) racadm_obj.logger = self.__logger racadm_obj.set_priority(3) racadm_obj.set_node_name(self.__node_name) racadm_obj.set_task_name("{}-racadm".format(self.__node_name)) racadm_obj.set_log_path("/var/log/infrasim/{}/racadm.log". format(self.__node_name)) self.__tasks_list.append(racadm_obj) # Set interface if "type" not in self.__node: raise ArgsNotCorrect("[Node] Can't get infrasim type") else: bmc_obj.set_type(self.__node['type']) compute_obj.set_type(self.__node['type']) if self.__sol_enabled: if "sol_device" in self.__node: socat_obj.set_sol_device(self.__node["sol_device"]) bmc_obj.set_sol_device(self.__node["sol_device"]) if "serial_socket" not in self.__node: self.__node["serial_socket"] = os.path.join(config.infrasim_home, self.__node["name"], ".serial") socat_obj.set_socket_serial(self.__node["serial_socket"]) compute_obj.set_socket_serial(self.__node["serial_socket"]) if "ipmi_console_port" in self.__node: bmc_obj.set_port_ipmi_console(self.__node["ipmi_console_port"]) # ipmi-console shall connect to same port with the same conf file if "bmc_connection_port" in self.__node: bmc_obj.set_port_qemu_ipmi(self.__node["bmc_connection_port"]) compute_obj.set_port_qemu_ipmi(self.__node["bmc_connection_port"]) # Init monitor task monitor_info = {} if "monitor" not in self.__node: monitor_info = { "enable": True, # Interface and port is for north bound REST service of # infrasim-monitor, not the socket of QEMU monitor "inferface": None, "port": 9005 } else: monitor_info = { "enable": self.__node["monitor"].get("enable", True), "interface": self.__node["monitor"].get("interface", None), "port": self.__node["monitor"].get("port", 9005) } if not isinstance(monitor_info["enable"], bool): raise ArgsNotCorrect("[Monitor] Invalid setting") if monitor_info["enable"]: compute_obj.enable_qemu_monitor() monitor_obj = CMonitor(monitor_info) monitor_obj.logger = self.__logger monitor_obj.set_priority(4) monitor_obj.set_node_name(self.__node_name) monitor_obj.set_task_name("{}-monitor".format(self.__node_name)) monitor_obj.set_log_path("/var/log/infrasim/{}/monitor.log". format(self.__node_name)) self.__tasks_list.append(monitor_obj) self.workspace = Workspace(self.__node) for task in self.__tasks_list: task.set_workspace(self.workspace.get_workspace()) if not self.__is_running(): self.workspace.init() if self.__netns: for task in self.__tasks_list: task.netns = self.__netns for task in self.__tasks_list: task.init()
class CNode(object): def __init__(self, node_info=None): self.__tasks_list = [] self.__node = node_info self.__node_name = "" self.workspace = None self.__sol_enabled = None self.__netns = None self.__logger = infrasim_log.get_logger(LoggerType.model.value) self.__shm_key = None @property def netns(self): return self.__netns def get_task_list(self): return self.__tasks_list def get_node_name(self): return self.__node_name def set_node_name(self, name): self.__node_name = name def get_node_info(self): return self.__node @run_in_namespace def precheck(self): if self.__is_running(): return for task in self.__tasks_list: task.precheck() def terminate_workspace(self): if Workspace.check_workspace_exists(self.__node_name): shutil.rmtree(self.workspace.get_workspace()) self.__logger.info("[Node] Node {} runtime workspcace is destroyed". format(self.__node_name)) print "Node {} runtime workspace is destroyed.".format(self.__node_name) def init(self): """ 1. Prepare CNode attributes: - self.__node 2. Then use this information to init workspace 3. Use this information to init sub module """ if 'name' in self.__node: self.set_node_name(self.__node['name']) else: raise ArgsNotCorrect("[Node] No node name is given in node information.") self.__logger = infrasim_log.get_logger(LoggerType.model.value, self.__node_name) if self.__node['compute'] is None: raise ArgsNotCorrect("[Node] No compute information") if 'sol_enable' not in self.__node: self.__node['sol_enable'] = True self.__sol_enabled = self.__node['sol_enable'] self.__netns = self.__node.get("namespace") if 'uuid' in self.__node["compute"]: uuid_num = self.__node["compute"].get("uuid") else: uuid_num = str(uuid.uuid4()) self.__node["compute"]["uuid"] = uuid_num if 'serial_number' in self.__node["compute"]: serial_number = self.__node["compute"].get("serial_number") else: serial_number = pre_serial_number + ''.join(random.SystemRandom().choice(string.digits) for _ in range(3)) self.__node["compute"]["serial_number"] = serial_number # If user specify "network_mode" as "bridge" but without MAC # address, generate one for this network. for network in self.__node['compute']['networks']: if 'mac' not in network: uuid_val = uuid.uuid4() str1 = str(uuid_val)[-2:] str2 = str(uuid_val)[-4:-2] str3 = str(uuid_val)[-6:-4] network['mac'] = ":".join(["52:54:BE", str1, str2, str3]) # If user specify "nmve" controller(drive) in "storage_backend" # with NO serial, generate one for it, since QEMU now (2.10.1) # treat "serail" as a mandatory attribute for "nvme" for storage in self.__node['compute']['storage_backend']: if storage.get('type') == 'nvme': if not storage.get('serial', ''): storage['serial'] = helper.random_serial() if self.__sol_enabled: socat_obj = CSocat() socat_obj.logger = self.__logger socat_obj.set_priority(0) socat_obj.set_task_name("{}-socat".format(self.__node_name)) socat_obj.set_node_name(self.__node['name']) self.__tasks_list.append(socat_obj) bmc_info = self.__node.get('bmc', {}) bmc_obj = CBMC(bmc_info) bmc_obj.logger = self.__logger bmc_obj.set_priority(1) bmc_obj.set_task_name("{}-bmc".format(self.__node_name)) bmc_obj.enable_sol(self.__sol_enabled) bmc_obj.set_log_path(os.path.join(config.infrasim_log_dir, self.__node_name, "openipmi.log")) bmc_obj.set_node_name(self.__node['name']) self.__tasks_list.append(bmc_obj) compute_obj = CCompute(self.__node['compute']) compute_obj.logger = self.__logger asyncr = bmc_info.get("startnow", True) compute_obj.set_asyncronous(asyncr) compute_obj.enable_sol(self.__sol_enabled) compute_obj.set_priority(2) compute_obj.set_uuid(uuid_num) compute_obj.set_serial_number(self.__node["compute"]["serial_number"]) compute_obj.set_task_name("{}-node".format(self.__node_name)) compute_obj.set_log_path(os.path.join(config.infrasim_log_dir, self.__node_name, "qemu.log")) self.__tasks_list.append(compute_obj) if "type" in self.__node and "dell" in self.__node["type"]: racadm_info = self.__node.get("racadm", {}) racadm_obj = CRacadm(racadm_info) racadm_obj.logger = self.__logger racadm_obj.set_priority(3) racadm_obj.set_node_name(self.__node_name) racadm_obj.set_task_name("{}-racadm".format(self.__node_name)) racadm_obj.set_log_path(os.path.join(config.infrasim_log_dir, self.__node_name, "racadm.log")) self.__tasks_list.append(racadm_obj) # Set interface if "type" not in self.__node: raise ArgsNotCorrect("[Node] Can't get infrasim type") else: bmc_obj.set_type(self.__node['type']) compute_obj.set_type(self.__node['type']) if self.__sol_enabled: if "sol_device" in self.__node: socat_obj.set_sol_device(self.__node["sol_device"]) bmc_obj.set_sol_device(self.__node["sol_device"]) if "serial_socket" not in self.__node: self.__node["serial_socket"] = os.path.join(config.infrasim_home, self.__node["name"], ".serial") socat_obj.set_socket_serial(self.__node["serial_socket"]) compute_obj.set_socket_serial(self.__node["serial_socket"]) if "ipmi_console_port" in self.__node: bmc_obj.set_port_ipmi_console(self.__node["ipmi_console_port"]) # ipmi-console shall connect to same port with the same conf file if "bmc_connection_port" in self.__node: bmc_obj.set_port_qemu_ipmi(self.__node["bmc_connection_port"]) compute_obj.set_port_qemu_ipmi(self.__node["bmc_connection_port"]) # Init monitor task monitor_info = {} if "monitor" not in self.__node: monitor_info = { "enable": True, # Interface and port is for north bound REST service of # infrasim-monitor, not the socket of QEMU monitor "inferface": None, "port": 9005 } else: monitor_info = { "enable": self.__node["monitor"].get("enable", True), "interface": self.__node["monitor"].get("interface", None), "port": self.__node["monitor"].get("port", 9005) } if not isinstance(monitor_info["enable"], bool): raise ArgsNotCorrect("[Monitor] Invalid setting") if monitor_info["enable"]: compute_obj.enable_qemu_monitor() monitor_obj = CMonitor(monitor_info) monitor_obj.logger = self.__logger monitor_obj.set_priority(4) monitor_obj.set_node_name(self.__node_name) monitor_obj.set_task_name("{}-monitor".format(self.__node_name)) monitor_obj.set_log_path(os.path.join(config.infrasim_log_dir, self.__node_name, "monitor.log")) self.__tasks_list.append(monitor_obj) self.workspace = Workspace(self.__node) for task in self.__tasks_list: task.set_workspace(self.workspace.get_workspace()) if not self.__is_running(): self.workspace.init() if self.__netns: for task in self.__tasks_list: task.netns = self.__netns for task in self.__tasks_list: task.init() # Run tasks list as the priority def start(self): # sort the tasks as the priority self.__tasks_list.sort(key=lambda x: x.get_priority(), reverse=False) for task in self.__tasks_list: task.run() def stop(self): # sort the tasks as the priority in reversed sequence self.__tasks_list.sort(key=lambda x: x.get_priority(), reverse=True) for task in self.__tasks_list: task.terminate() def status(self): for task in self.__tasks_list: task.status() def __is_running(self): state = True for task in self.__tasks_list: state &= task.task_is_running() return state def wait_node_up(self, timeout=180): start = time.time() while self.__is_running() is False: if time.time() - start > timeout: return False return True
def get(self, nodename): """ Show workspace details on this InfraSIM host """ node_info = Workspace.get_node_info_in_workspace(nodename) return node_info, 200
def init_env(instance): """ This is to sync ipmi-console with runtime vBMC configuration. Initial version capture infrasim instance name by infrasim-main status, while we have a plan to give instance name to ipmi-console so that it can be attached to target vBMC instance. """ if not Workspace.check_workspace_exists(instance): raise IpmiError("Warning: there is no node {} workspace. Please start node {} first.".format(instance, instance)) output = run_command("infrasim node status") if output[0] == 0 and "{}-bmc is stopped".format(instance) in output[1]: raise IpmiError("Warning: node {} has not started BMC. Please start node {} first.".format(instance, instance)) logger.info("Init ipmi-console environment for infrasim instance: {}". format(instance)) # Get runtime vbmc.conf vbmc_conf_path = os.path.join(os.environ["HOME"], ".infrasim", instance, "etc", "vbmc.conf") if not os.path.exists(vbmc_conf_path): msg = "{} vBMC configuration is not defined at {}".format(instance, vbmc_conf_path) logger.error(msg) raise Exception(msg) else: msg = "Target vbmc to attach is: {}".format(vbmc_conf_path) logger.info(msg) # Get runtime infrasim.yml infrasim_yml_path = os.path.join(os.environ["HOME"], ".infrasim", instance, "etc", "infrasim.yml") if not os.path.exists(infrasim_yml_path): msg = "{} infrasim instance is not defined at {}".format(instance, infrasim_yml_path) logger.error(msg) raise Exception(msg) else: msg = "Target infrasim instance to attach is: {}".format(infrasim_yml_path) logger.info(msg) # Get variable and set to ipmi-console env # - PORT_TELNET_TO_VBMC # - VBMC_IP # - VBMC_PORT with open(vbmc_conf_path, 'r') as fp: conf = fp.read() p_telnet = re.compile(r"^\s*console\s*[\d:\.]+\s+(?P<port_telnet_to_vbmc>\d+)", re.MULTILINE) s_telnet = p_telnet.search(conf) if s_telnet: env.PORT_TELNET_TO_VBMC = int(s_telnet.group("port_telnet_to_vbmc")) logger.info("PORT_TELNET_TO_VBMC: {}".format(env.PORT_TELNET_TO_VBMC)) else: raise Exception("PORT_TELNET_TO_VBMC is not found") p_vbmc = re.compile(r"^\s*addr\s*(?P<vbmc_ip>[\d:\.]+)\s*(?P<vbmc_port>\d+)", re.MULTILINE) s_vbmc = p_vbmc.search(conf) if s_vbmc: ip = s_vbmc.group("vbmc_ip") if ip == "::" or ip == "0.0.0.0": env.VBMC_IP = "localhost" else: env.VBMC_IP = ip logger.info("VBMC_IP: {}".format(env.VBMC_IP)) env.VBMC_PORT = int(s_vbmc.group("vbmc_port")) logger.info("VBMC_PORT: {}".format(env.VBMC_PORT)) else: raise Exception("VBMC_IP and VBMC_PORT is not found") # Get variable and set to ipmi-console env # - PORT_SSH_FOR_CLIENT with open(infrasim_yml_path, 'r') as fp: conf = fp.read() p_port = re.compile(r"^\s*ipmi_console_ssh:\s*(?P<port_ssh_for_client>\d+)", re.MULTILINE) s_port = p_port.search(conf) if s_port: env.PORT_SSH_FOR_CLIENT = int(s_port.group("port_ssh_for_client")) else: env.PORT_SSH_FOR_CLIENT = 9300 logger.info("PORT_SSH_FOR_CLIENT: {}".format(env.PORT_SSH_FOR_CLIENT))
except InfraSimError, e: print e.value logger_cmd.error("cmd res: {}".format(e.value)) @args("node_name", help="Specify node name to update configuration mapping") @args("config_file", help="Node Config File Path") def update(self, node_name, config_file): try: nm.update(node_name, config_file) logger_cmd.info("cmd res: update node {} OK".format(node_name)) except InfraSimError, e: print e.value logger_cmd.error("cmd res: {}".format(e.value)) if Workspace.check_workspace_exists(node_name): print "Node {0} runtime workspace exists.\n" \ "If you want to apply updated configuration, please destroy node runtime workspace first.\n" \ "You can run commands: \n" \ " infrasim node destroy {0}\n" \ " infrasim node start {0}".format(node_name) logger_cmd.warning( "Node {0} runtime workspace exists. " "Need to destroy node runtime workspace first".format( node_name)) @args("node_name", nargs='?', default="default", help="Specify node name to open its configuration in editor") def edit(self, node_name):
@args("node_name", help="Specify node name to delete configuration mapping") def delete(self, node_name): try: nm.delete(node_name) except InfraSimError, e: print e.value @args("node_name", help="Specify node name to update configuration mapping") @args("config_file", help="Node Config File Path") def update(self, node_name, config_file): try: nm.update(node_name, config_file) except InfraSimError, e: print e.value if Workspace.check_workspace_exists(node_name): print "Node {0} runtime workspace exists.\n" \ "If you want to apply updated configuration, please destroy node runtime workspace first.\n" \ "You can run commands: \n" \ " infrasim node destroy {0}\n" \ " infrasim node start {0}".format(node_name) def list(self): try: nm.list() except InfraSimError, e: print e.value class NodeCommands(object):
def check_node_stop_workspace(node_name): conf = Workspace.get_node_info_in_workspace(node_name) node_root = os.path.join(config.infrasim_home, conf["name"]) # Check node data folder and files exist node_type = conf["type"] data_folder = os.path.join(node_root, "data") node_emu = os.path.join(data_folder, "{}.emu".format(node_type)) node_bios = os.path.join(data_folder, "{}_smbios.bin".format(node_type)) assert os.path.exists(data_folder) is True assert os.path.exists(node_emu) is True assert os.path.exists(node_bios) is True # Check node script folder and files exist script_folder = os.path.join(node_root, "scripts") script_chassiscontrol = os.path.join(script_folder, "chassiscontrol") script_lancontrol = os.path.join(script_folder, "lancontrol") script_startcmd = os.path.join(script_folder, "startcmd") script_stopcmd = os.path.join(script_folder, "stopcmd") script_resetcmd = os.path.join(script_folder, "resetcmd") assert os.path.exists(script_folder) is True assert os.path.exists(script_chassiscontrol) is True assert os.path.exists(script_lancontrol) is True assert os.path.exists(script_startcmd) is True assert os.path.exists(script_stopcmd) is True assert os.path.exists(script_resetcmd) is True # Check etc folder and files exist etc_folder = os.path.join(node_root, "etc") etc_infrasim_yml = os.path.join(etc_folder, "infrasim.yml") etc_vbmc_conf = os.path.join(etc_folder, "vbmc.conf") assert os.path.exists(etc_folder) is True assert os.path.exists(etc_infrasim_yml) is True assert os.path.exists(etc_vbmc_conf) is True # Check disk image exist node_name = conf["name"] node_stor = conf['compute']['storage_backend'] disk_index = 0 for stor_control in node_stor: for drive in stor_control["drives"]: disk_file = os.path.join(node_root, "sd{0}.img".format(chr(97 + disk_index))) disk_index += 1 assert os.path.exists(disk_file) is True # Check serial device exist serial_dev = os.path.join(node_root, ".pty0") assert os.path.exists(serial_dev) is False # should __NOT__ check .serial, since it will not be removed automatically # Check unix socket file # serial = os.path.join(node_root, ".serial") # assert os.path.exists(serial) is False # Check node runtime pid file don't exist node_socat = os.path.join(node_root, ".{}-socat.pid".format(node_name)) node_ipmi = os.path.join(node_root, ".{}-bmc.pid".format(node_name)) node_qemu = os.path.join(node_root, ".{}-node.pid".format(node_name)) assert os.path.exists(node_socat) is False assert os.path.exists(node_ipmi) is False assert os.path.exists(node_qemu) is False
def terminate_workspace(self): if Workspace.check_workspace_exists(self.__node_name): shutil.rmtree(self.workspace.get_workspace()) self.__logger.info("[Node] Node {} runtime workspcace is destroyed". format(self.__node_name)) print "Node {} runtime workspace is destroyed.".format(self.__node_name)
def init(self): """ 1. Prepare CNode attributes: - self.__node 2. Then use this information to init workspace 3. Use this information to init sub module """ if 'name' in self.__node: self.set_node_name(self.__node['name']) else: raise ArgsNotCorrect( "[Node] No node name is given in node information.") self.__logger = infrasim_log.get_logger(LoggerType.model.value, self.__node_name) if self.__node['compute'] is None: raise ArgsNotCorrect("[Node] No compute information") if 'sol_enable' not in self.__node: self.__node['sol_enable'] = True self.__sol_enabled = self.__node['sol_enable'] self.__netns = self.__node.get("namespace") if 'uuid' in self.__node["compute"]: uuid_num = self.__node["compute"].get("uuid") else: uuid_num = str(uuid.uuid4()) self.__node["compute"]["uuid"] = uuid_num if 'serial_number' in self.__node["compute"]: serial_number = self.__node["compute"].get("serial_number") else: serial_number = pre_serial_number + ''.join( random.SystemRandom().choice(string.digits) for _ in range(3)) self.__node["compute"]["serial_number"] = serial_number # If user specify "network_mode" as "bridge" but without MAC # address, generate one for this network. for network in self.__node['compute']['networks']: if 'mac' not in network: uuid_val = uuid.uuid4() str1 = str(uuid_val)[-2:] str2 = str(uuid_val)[-4:-2] str3 = str(uuid_val)[-6:-4] network['mac'] = ":".join(["52:54:BE", str1, str2, str3]) # If user specify "nmve" controller(drive) in "storage_backend" # with NO serial, generate one for it, since QEMU now (2.10.1) # treat "serail" as a mandatory attribute for "nvme" for storage in self.__node['compute']['storage_backend']: if storage.get('type') == 'nvme': if not storage.get('serial', ''): storage['serial'] = helper.random_serial() if self.__sol_enabled: socat_obj = CSocat() socat_obj.logger = self.__logger socat_obj.set_priority(0) socat_obj.set_task_name("{}-socat".format(self.__node_name)) socat_obj.set_node_name(self.__node['name']) self.__tasks_list.append(socat_obj) bmc_info = self.__node.get('bmc', {}) bmc_obj = CBMC(bmc_info) bmc_obj.logger = self.__logger bmc_obj.set_priority(1) bmc_obj.set_task_name("{}-bmc".format(self.__node_name)) bmc_obj.enable_sol(self.__sol_enabled) bmc_obj.set_log_path( os.path.join(config.infrasim_log_dir, self.__node_name, "openipmi.log")) bmc_obj.set_node_name(self.__node['name']) self.__tasks_list.append(bmc_obj) compute_obj = CCompute(self.__node['compute']) compute_obj.logger = self.__logger asyncr = bmc_info.get("startnow", True) compute_obj.set_asyncronous(asyncr) compute_obj.enable_sol(self.__sol_enabled) compute_obj.set_priority(2) compute_obj.set_uuid(uuid_num) compute_obj.set_serial_number(self.__node["compute"]["serial_number"]) compute_obj.set_task_name("{}-node".format(self.__node_name)) compute_obj.set_log_path( os.path.join(config.infrasim_log_dir, self.__node_name, "qemu.log")) self.__tasks_list.append(compute_obj) if "type" in self.__node and "dell" in self.__node["type"]: racadm_info = self.__node.get("racadm", {}) racadm_obj = CRacadm(racadm_info) racadm_obj.logger = self.__logger racadm_obj.set_priority(3) racadm_obj.set_node_name(self.__node_name) racadm_obj.set_task_name("{}-racadm".format(self.__node_name)) racadm_obj.set_log_path( os.path.join(config.infrasim_log_dir, self.__node_name, "racadm.log")) self.__tasks_list.append(racadm_obj) # Set interface if "type" not in self.__node: raise ArgsNotCorrect("[Node] Can't get infrasim type") else: bmc_obj.set_type(self.__node['type']) compute_obj.set_type(self.__node['type']) if self.__sol_enabled: if "sol_device" in self.__node: socat_obj.set_sol_device(self.__node["sol_device"]) bmc_obj.set_sol_device(self.__node["sol_device"]) if "serial_socket" not in self.__node: self.__node["serial_socket"] = os.path.join( config.infrasim_home, self.__node["name"], ".serial") socat_obj.set_socket_serial(self.__node["serial_socket"]) compute_obj.set_socket_serial(self.__node["serial_socket"]) if "ipmi_console_port" in self.__node: bmc_obj.set_port_ipmi_console(self.__node["ipmi_console_port"]) # ipmi-console shall connect to same port with the same conf file if "bmc_connection_port" in self.__node: bmc_obj.set_port_qemu_ipmi(self.__node["bmc_connection_port"]) compute_obj.set_port_qemu_ipmi(self.__node["bmc_connection_port"]) # Init monitor task monitor_info = {} if "monitor" not in self.__node: monitor_info = { "enable": True, # Interface and port is for north bound REST service of # infrasim-monitor, not the socket of QEMU monitor "inferface": None, "port": 9005 } else: monitor_info = { "enable": self.__node["monitor"].get("enable", True), "interface": self.__node["monitor"].get("interface", None), "port": self.__node["monitor"].get("port", 9005) } if not isinstance(monitor_info["enable"], bool): raise ArgsNotCorrect("[Monitor] Invalid setting") if monitor_info["enable"]: compute_obj.enable_qemu_monitor() monitor_obj = CMonitor(monitor_info) monitor_obj.logger = self.__logger monitor_obj.set_priority(4) monitor_obj.set_node_name(self.__node_name) monitor_obj.set_task_name("{}-monitor".format(self.__node_name)) monitor_obj.set_log_path( os.path.join(config.infrasim_log_dir, self.__node_name, "monitor.log")) self.__tasks_list.append(monitor_obj) self.workspace = Workspace(self.__node) for task in self.__tasks_list: task.set_workspace(self.workspace.get_workspace()) if not self.__is_running(): self.workspace.init() if self.__netns: for task in self.__tasks_list: task.netns = self.__netns for task in self.__tasks_list: task.init()