示例#1
0
    def init(self):
        self.__host_bdf = self.__pci_dev_info.get("host")
        self.__bus = self.__pci_dev_info.get("bus")
        self.__addr = self.__pci_dev_info.get("addr")
        self.__multifunction = self.__pci_dev_info.get("multifunction")
        if self.__host_bdf is None:
            raise ArgsNotCorrect("host should be set.")

        self.__target_driver = self.__pci_dev_info.get("driver", "vfio-pci")
        if self.__target_driver not in ["vfio-pci", "pci-stub"]:
            raise ArgsNotCorrect("{} not supported".format(
                self.__target_driver))

        if self.__target_driver == "vfio-pci":
            os.system("modprobe vfio-pci")
        else:
            os.system("modprobe pci-stub")

        if not re.search(r'\d{4}:[0-9a-fA-F]{2}:\d{2}\.\d+$', self.__host_bdf):
            raise ArgsNotCorrect("BDF should be Domain:Bus:Device.Function")

        target_dev_sysfs_path = "/sys/bus/pci/devices/{}".format(
            self.__host_bdf)

        if not os.path.exists(target_dev_sysfs_path):
            raise InfraSimError("No such device {}".format(self.__host_bdf))

        if not os.path.exists(os.path.join(target_dev_sysfs_path, "iommu")):
            raise InfraSimError(
                "No IOMMU found. Check your hardware and/or linux parameters, use intel_iommu=on"
            )

        self.__get_dev_sysfs_path_in_iommu_group(self.__host_bdf)
        self.bind()
示例#2
0
    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
示例#3
0
    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))
示例#4
0
    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))
示例#5
0
    def delete(self, item_name):
        """
        Delete a mapping for this item, by deleting config
        of <item_name>.yml in mapping folder.
        """
        logger_config = self.get_logger(item_name)
        logger_config.info("request rev: delete item {}".format(item_name))
        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 {}'s configuration is not in InfraSIM mapping.".format(
                    item_name))
            raise InfraSimError(
                "Item {0}'s configuration is not in InfraSIM mapping.".format(
                    item_name))

        os.remove(
            os.path.join(self.__mapping_folder, "{}.yml".format(item_name)))

        self.__name_list.remove(item_name)
        print "Item {}'s configuration mapping removed".format(item_name)
        logger_config.info(
            "request res: Item {}'s configuration mapping removed.".format(
                item_name))
示例#6
0
    def __init__(self, nsname=None, nspath=None, nspid=None):
        self.mypath = get_ns_path(nspid=os.getpid())
        self.targetpath = get_ns_path(nspath,
                                      nsname=nsname,
                                      nspid=nspid)

        if not os.path.exists(self.targetpath):
            raise InfraSimError('invalid namespace {}'.format(nsname))
示例#7
0
 def init(self):
     self.workspace = ChassisWorkspace(self.__chassis)
     nodes = self.__chassis.get("nodes")
     if nodes is None:
         raise InfraSimError("There is no nodes under chassis")
     for node in nodes:
         node_name = node.get("name", "{}_node_{}".format(self.__chassis_name, nodes.index(node)))
         node["name"] = node_name
         node["type"] = self.__chassis["type"]
     self.workspace.init()
     self.__file_name = os.path.join(self.workspace.get_workspace_data(), "shm_data.bin")
     self.__daemon = CChassisDaemon(self.__chassis_name, self.__file_name)
示例#8
0
def fetch_image(url, checksum, dst):
    """
    :param url: Download link of the image file
    :param checksum: MD5 checksum of the image
    :param dst: Location to store the image
    """
    if os.path.exists(dst):
        if hashlib.md5(open(dst, "rb").read()).hexdigest() == checksum:
            return
        else:
            os.remove(dst)
    os.system("wget -c {0} -O {1}".format(url, dst))
    if hashlib.md5(open(dst, "rb").read()).hexdigest() != checksum:
        raise InfraSimError("Fail to download image {}".format(dst.split('/')[-1]))
示例#9
0
 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
示例#10
0
    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
示例#11
0
    def add(self, item_name, config_path):
        self.__chassis_name = item_name
        installed_node = []
        logger_config = self.get_logger(item_name)
        sub_nodes = self.__split_sub_nodes(config_path)
        node_name = ""
        try:
            for node in sub_nodes:
                node_name = node["node_name"]
                self.__nm.add(node_name, node["file"])
                installed_node.append(node_name)
        except InfraSimError:
            for node in installed_node:
                self.__nm.delete(node)
            raise InfraSimError("Node {0} in {1} is already existed.".format(
                node_name, item_name))

        super(ChassisMap, self).add(item_name, config_path)
        print "Chassis {} is added with nodes:{}.".format(
            item_name, installed_node)
        logger_config.info("Chassis {} is added with nodes:{}.".format(
            item_name, installed_node))
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))
        """
        logger_config = infrasim_log.get_logger(LoggerType.config.value,
                                                node_name)
        logger_config.info("request rev: delete node {}".format(node_name))
        try:
            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 {}'s configuration is not in InfraSIM mapping.".format(
                    node_name))
            raise InfraSimError(
                "Node {0}'s configuration is not in InfraSIM mapping.".format(
                    node_name))

        os.remove(
            os.path.join(self.__mapping_folder, "{}.yml".format(node_name)))

        self.__name_list.remove(node_name)
        print "Node {}'s configuration mapping removed".format(node_name)
        logger_config.info(
            "request res: Node {}'s configuration mapping removed.".format(
                node_name))

    def update(self, node_name, config_path):
        """
        Update mapping configure for this node
        """