def __load(self, config_path): item_name = self.__chassis_name logger_config = self.get_logger(item_name) try: with open(config_path, 'r') as fp: chassis_info = YAMLLoader(fp).get_data() except IOError: logger_config.exception( "Cannot find config {}".format(config_path)) raise InfraSimError("Cannot find config {}".format(config_path)) if not isinstance(chassis_info, dict): logger_config.exception( "Config {} is an invalid yaml file.".format(config_path)) raise InfraSimError( "Config {} is an invalid yaml file.".format(config_path)) nodes = chassis_info.get("nodes") if not nodes: raise InfraSimError( "Config {} has no [nodes].".format(chassis_info)) for node in nodes: if not node.get('name'): node['name'] = "{}_node_{}".format(item_name, nodes.index(node)) return chassis_info
def read_packages_info(): version_yml = os.path.join(config.get_infrasim_root(), "components-version/packages.yml") package_list = None with open(version_yml, "r") as fp: package_list = YAMLLoader(fp).get_data() return package_list
def list(self): """ List all mapping in the map folder """ logger.info("request rev: list") try: self.load() except DirectoryNotFound as e: print e.value logger.exception(e.value) table = Texttable() table.set_deco(Texttable.HEADER) rows = [] rows.append(["name", "type"]) for node_name in self.__name_list: node_type = "" with open( os.path.join(self.__mapping_folder, "{}.yml".format(node_name)), 'r') as fp: node_info = YAMLLoader(fp).get_data() node_type = node_info['type'] rows.append([node_name, node_type]) table.add_rows(rows) print table.draw() logger.info("request res: list OK")
def _get_map_manager(self, name, config_file=None): is_chassis = False has_node = nm.in_map_folder(name) has_chassis = cm.in_map_folder(name) if config_file: with open(config_file, 'r') as fp: info = YAMLLoader(fp).get_data() if info.get("nodes"): is_chassis = True if (is_chassis and has_node) or (is_chassis is False and has_chassis): raise InfraSimError("Type from config file is not match with name") else: is_chassis = has_chassis return cm if is_chassis else nm
def get_node_info(): """ Get runtime node information """ runtime_yml_path = os.path.join(config.infrasim_home, env.node_name, "etc", "infrasim.yml") with open(runtime_yml_path, 'r') as fp: node_info = YAMLLoader(fp).get_data() return node_info
def update(self, item_name, config_path): """ Update mapping configure for this item """ logger_config = self.get_logger(item_name) logger_config.info("request rev: update item {0} with file {1}".format( item_name, config_path)) try: self.load() except DirectoryNotFound as e: print e.value logger_config.exception(e.value) if item_name not in self.__name_list: logger_config.exception( "Item {0}'s configuration is not in InfraSIM mapping.".format( item_name)) raise InfraSimError( "Item {0}'s configuration is not in InfraSIM mapping.\n" "Please add it to mapping folder with command:\n" " infrasim item add {0} {1}".format(item_name, config_path)) try: with open(config_path, 'r') as fp: node_info = YAMLLoader(fp).get_data() if not isinstance(node_info, dict): logger_config.exception( "Config {} is an invalid yaml file.".format( config_path)) raise InfraSimError( "Config {} is an invalid yaml file.".format( config_path)) logger_config.info("Item {}'s yaml file: {}".format( item_name, node_info)) except IOError: logger_config.exception( "Cannot find config {}".format(config_path)) raise InfraSimError("Cannot find config {}".format(config_path)) dst = os.path.join(self.__mapping_folder, "{}.yml".format(item_name)) try: node_info["name"] = item_name with open(dst, 'w') as fp: yaml.dump(node_info, fp, default_flow_style=False) os.chmod(dst, 0o664) except IOError: logger_config.exception( "Item {}'s configuration failed to be updated.".format( item_name)) raise InfraSimError( "Item {}'s configuration failed to be updated. Check file mode of {}." .format(item_name, dst)) print "Item {}'s configuration mapping is updated".format(item_name) logger_config.info( "request res: Item {}'s configuration mapping is updated".format( item_name))
def get_environment(): cpu_info = run_command('lscpu') logger_env.info("cpu information: \n{}".format(cpu_info[1])) version_info = version() logger_env.info("infrasim version information: \n{}".format(version_info)) try: with open(infrasim_default_config, 'r') as fp: node_info = YAMLLoader(fp).get_data() logger_env.info("infrasim default configuration " "information: \n{}\n".format(node_info)) except Exception: pass
def __load(self, config_path): item_name = self.__chassis_name logger_config = self.get_logger(item_name) try: with open(config_path, 'r') as fp: chassis_info = YAMLLoader(fp).get_data() except IOError: logger_config.exception("Cannot find config {}".format(config_path)) raise InfraSimError("Cannot find config {}".format(config_path)) if not isinstance(chassis_info, dict): logger_config.exception("Config {} is an invalid yaml file.".format(config_path)) raise InfraSimError("Config {} is an invalid yaml file.".format(config_path)) nodes = chassis_info.get("nodes") if not nodes: raise InfraSimError("Config {} has no [nodes].".format(chassis_info)) for node in nodes: if not node.get('name'): node['name'] = "{}_node_{}".format(item_name, nodes.index(node)) return chassis_info
def add(self, item_name, config_path): logger_config = self.get_logger(item_name) """ Create a mapping for this item, by writing config to <item_name>.yml in mapping folder. """ logger_config.info("request rev: add item {0} with file {1}".format( item_name, config_path)) try: self.load() except DirectoryNotFound as e: print e.value logger_config.exception(e.value) if item_name in self.__name_list: logger_config.exception( "Item {0}'s configuration already in InfraSIM mapping.".format( item_name)) raise InfraSimError( "Item {0}'s configuration already in InfraSIM mapping.\n" "If you want to update the configuration, please run this command:\n" " infrasim config update {0} {1}".format( item_name, config_path)) try: with open(config_path, 'r') as fp: node_info = YAMLLoader(fp).get_data() if not isinstance(node_info, dict): logger_config.exception( "Config {} is an invalid yaml file.".format( config_path)) raise InfraSimError( "Config {} is an invalid yaml file.".format( config_path)) node_info["name"] = item_name logger_config.info("Item {}'s yaml file: {}".format( item_name, node_info)) except IOError: logger_config.exception( "Cannot find config {}".format(config_path)) raise InfraSimError("Cannot find config {}".format(config_path)) dst = os.path.join(self.__mapping_folder, "{}.yml".format(item_name)) with open(dst, 'w') as fp: yaml.dump(node_info, fp, default_flow_style=False, indent=2) os.chmod(dst, 0o664) self.__name_list.append(item_name) print "Item {}'s configuration mapping added.".format(item_name) logger_config.info( "request res: Item {}'s configuration mapping added.".format( item_name))
def get_item_info(self, item_name): logger_config = self.get_logger(item_name) src = os.path.join(self.__mapping_folder, "{}.yml".format(item_name)) if not os.path.exists(src): logger_config.exception( "Item {0}'s configuration is not defined.".format(item_name)) raise InfraSimError( "Item {0}'s configuration is not defined.\n" "Please add config mapping with command:\n" " infrasim config add {0} [your_config_path]".format( item_name)) with open(src, 'r') as fp: node_info = YAMLLoader(fp).get_data() return node_info
def test_update_config(self): self.nm.add(self.test_name, self.init_config) self.assertTrue(os.path.exists(self.test_config)) new_info = FakeConfig().get_node_info() new_info["name"] = "didi" new_info["type"] = "dell_r730" with open(self.tmp_config, "w") as fp: yaml.dump(new_info, fp, default_flow_style=False) self.nm.update(self.test_name, self.tmp_config) with open(self.test_config) as fp: node_info = YAMLLoader(fp).get_data() assert node_info["type"] == "dell_r730" assert node_info["name"] == "test" os.remove(self.tmp_config)
class NodeMap(object): """ This is a class manages infrasim mapping. """ def __init__(self): self.__mapping_folder = config.infrasim_node_config_map self.__name_list = [] def load(self): self.__name_list = [] if not os.path.exists(self.__mapping_folder): raise DirectoryNotFound( "InfraSIM MapManager failed to init due to {} folder not found.\n" "Please run this command to init:\n" " infrasim init".format(self.__mapping_folder)) node_list = os.listdir(self.__mapping_folder) for node in node_list: if node.endswith(".yml"): self.__name_list.append(node[:-4]) def add(self, node_name, config_path): logger_config = infrasim_log.get_logger(LoggerType.config.value, node_name) """ Create a mapping for this node, by writing config to <node_name>.yml in mapping folder. """ logger_config.info("request rev: add node {0} with file {1}".format( node_name, config_path)) try: self.load() except DirectoryNotFound, e: print e.value logger_config.exception(e.value) if node_name in self.__name_list: logger_config.exception( "Node {0}'s configuration already in InfraSIM mapping.".format( node_name)) raise InfraSimError( "Node {0}'s configuration already in InfraSIM mapping.\n" "If you want to update the configuration, please run this command:\n" " infrasim config update {0} {1}".format( node_name, config_path)) try: with open(config_path, 'r') as fp: node_info = YAMLLoader(fp).get_data() if not isinstance(node_info, dict): logger_config.exception( "Config {} is an invalid yaml file.".format( config_path)) raise InfraSimError( "Config {} is an invalid yaml file.".format( config_path)) node_info["name"] = node_name logger_config.info("Node {}'s yaml file: {}".format( node_name, node_info)) except IOError: logger_config.exception( "Cannot find config {}".format(config_path)) raise InfraSimError("Cannot find config {}".format(config_path)) dst = os.path.join(self.__mapping_folder, "{}.yml".format(node_name)) with open(dst, 'w') as fp: yaml.dump(node_info, fp, default_flow_style=False) os.chmod(dst, 0664) self.__name_list.append(node_name) print "Node {}'s configuration mapping added.".format(node_name) logger_config.info( "request res: Node {}'s configuration mapping added.".format( node_name))
self.load() except DirectoryNotFound, e: print e.value logger_config.exception(e.value) if node_name not in self.__name_list: logger_config.exception( "Node {0}'s configuration is not in InfraSIM mapping.".format( node_name)) raise InfraSimError( "Node {0}'s configuration is not in InfraSIM mapping.\n" "Please add it to mapping folder with command:\n" " infrasim node add {0} {1}".format(node_name, config_path)) try: with open(config_path, 'r') as fp: node_info = YAMLLoader(fp).get_data() if not isinstance(node_info, dict): logger_config.exception( "Config {} is an invalid yaml file.".format( config_path)) raise InfraSimError( "Config {} is an invalid yaml file.".format( config_path)) logger_config.info("Node {}'s yaml file: {}".format( node_name, node_info)) except IOError: logger_config.exception( "Cannot find config {}".format(config_path)) raise InfraSimError("Cannot find config {}".format(config_path)) dst = os.path.join(self.__mapping_folder, "{}.yml".format(node_name))
def test_add_config(self): self.nm.add(self.test_name, self.init_config) with open(self.test_config, 'r') as fp: node_info = YAMLLoader(fp).get_data() assert node_info["name"] == "test"