示例#1
0
    def from_xml(cls, element):
        validation = {}

        name = element.get(cls.NAME_LABEL)
        source = element.get(cls.SOURCE_LABEL)
        dest = element.get(cls.DEST_LABEL)
        type = element.get(cls.TYPE_LABEL)
        path = None
        args = None

        # supported source formats are a local file path that starts with a
        # leading slash, or URI strings that start with 'http', 'file', or 'ftp
        if "://" not in source and not source.startswith("file:/"):
            # source is a file path:
            if not os.path.exists(source):
                raise ParsingError("Invalid element specified in "
                                   "the %s section " % cls.NAME_LABEL +
                                   "of the manifest.  "
                                   "source does not exist: %s" % source)
        else:
            try:
                fileobj = urllib.urlopen(source)
            except (IOError), e:
                raise ParsingError("Invalid element specified in "
                                   "the %s section " % cls.NAME_LABEL +
                                   "of the manifest.  "
                                   "Unable to open source (%s): %s" % \
                                   (source, e))
示例#2
0
    def from_xml(cls, element):
        name = element.get("name")
        action = element.get("action")
        use = element.get("use")

        zvol = Zvol(name)

        size = element.find("size")
        if size is not None:
            if size.get("val") is None:
                raise ParsingError("Size element must contain a 'val' " + \
                                   "attribute")

            # 'max' is allowed only for swap zvol
            if size.get("val") == "max":
                if use is None or use != "swap":
                    raise ParsingError("'max' value for 'val' attribute is "
                                       "applicable only for swap zvol.")
            zvol.size = size.get("val")
        else:
            raise ParsingError("Zvol element must contain a size subelement")

        zvol.action = action
        if use is not None:
            zvol.use = use
        return zvol
示例#3
0
    def from_xml(cls, element):
        noswap = element.get("noswap")
        nodump = element.get("nodump")

        logical = Logical("logical")
        if noswap is not None:
            try:
                logical.noswap = {"true": True, "false": False}[noswap.lower()]
            except KeyError:
                raise ParsingError("Logical  element's noswap attribute " +
                                   "must be either 'true' or 'false'")

        if nodump is not None:
            try:
                logical.nodump = {"true": True, "false": False}[nodump.lower()]
            except KeyError:
                raise ParsingError("Logical  element's nodump attribute " +
                                   "must be either 'true' or 'false'")

        return logical
示例#4
0
    def from_xml(cls, element):
        name = element.get("name")
        action = element.get("action")
        use = element.get("use")

        zvol = Zvol(name)

        size = element.find("size")
        if size is not None:
            if size.get("val") is None:
                raise ParsingError("Size element must contain a 'val' " + \
                                   "attribute")
            zvol.size = size.get("val")
        else:
            raise ParsingError("Zvol element must contain a size subelement")

        zvol.action = action
        if use is not None:
            zvol.use = use
        return zvol
示例#5
0
    def from_xml(cls, element):
        name = element.get(cls.NAME_LABEL).strip()
        if not name:
            raise ParsingError("distribution name must not be blank")

        add_timestamp = \
            element.get(cls.ADD_TIMESTAMP_LABEL)
        http_proxy = element.get(cls.HTTP_PROXY_LABEL)

        # mkisofs(8) requires the volume ID be no longer than 32 characters in
        # length (specified by the -V option to mkisofs)
        if len(name) > 32:
            raise ParsingError("distribution name must be less than 32 "
                               "characters:  %s" % name)

        distro = Distro(name)
        if add_timestamp is not None:
            distro.add_timestamp = add_timestamp
        if http_proxy is not None:
            distro.http_proxy = http_proxy

        return distro
示例#6
0
    def from_xml(cls, element):
        name = element.get("name")
        action = element.get("action")
        mountpoint = element.get("mountpoint")
        in_be = element.get("in_be")

        filesystem = Filesystem(name)
        if action is not None:
            filesystem.action = action
        if mountpoint is not None:
            filesystem.mountpoint = mountpoint
        else:
            # Recursively strip the dataset_path until the mountpoint is
            # found.
            stripped_entries = []
            dataset_list = name.split("/")

            # add the parent's name (the zpool name) to the list
            dataset_list.insert(0, element.getparent().get("name"))

            while dataset_list:
                try:
                    test_dataset = Filesystem("/".join(dataset_list))
                    test_dataset_mp = test_dataset.get("mountpoint")
                except CalledProcessError:
                    # strip off the last element and save it for later
                    stripped_entries.append(dataset_list[-1])
                    dataset_list = dataset_list[:-1]
                    continue
                else:
                    # the mountpoint was found so add the stripped entries
                    # (in reverse) to generate the proper path.
                    filesystem.mountpoint = os.path.join(test_dataset_mp,
                        "/".join(reversed(stripped_entries)))
                    break
            else:
                # set the mountpoint to None
                filesystem.mountpoint = None

        if in_be is not None:
            if in_be.lower() == "true":
                filesystem.in_be = True
            elif in_be.lower() == "false":
                filesystem.in_be = False
            else:
                raise ParsingError("Filesystem element's in_be attribute " +
                                   "must be either 'true' or 'false'")

        return filesystem
示例#7
0
    def from_xml(cls, element):
        '''Convert XML tag to a DataObject.

           If there is a name attribute in the XML tag, then we will use that
           as the name of the object for insertion in the the Data Object
           Cache.
        '''

        if element.tag != cls.TAG_NAME:
            raise ParsingError("Tag name %s != %s" %
                               (element.tag, cls.TAG_NAME))

        # See if there is a name attribute, if so use it.
        name = element.get("name")
        if not name:
            name = cls.TAG_NAME

        return cls(name)
示例#8
0
    def from_xml(cls, element):
        name = element.get("name")
        action = element.get("action")
        is_root = element.get("is_root")
        mountpoint = element.get("mountpoint")

        zpool = Zpool(name)
        if action is not None:
            zpool.action = action
        if is_root is not None:
            try:
                zpool.is_root = {"true": True, "false": False}[is_root.lower()]
            except KeyError:
                raise ParsingError("Zpool element's is_root attribute must " +
                                   "be either 'true' or 'false'")

        if mountpoint is not None:
            zpool.mountpoint = mountpoint
        return zpool
示例#9
0
    def from_xml(cls, element):
        name = element.get("name")
        action = element.get("action")
        mountpoint = element.get("mountpoint")
        in_be = element.get("in_be")

        filesystem = Filesystem(name)
        if action is not None:
            filesystem.action = action
        if mountpoint is not None:
            filesystem.mountpoint = mountpoint

        if in_be is not None:
            try:
                filesystem.in_be = {"true": True,
                                    "false": False}[in_be.lower()]
            except KeyError:
                raise ParsingError("Filesystem element's in_be attribute " +
                                   "must be either 'true' or 'false'")

        return filesystem
示例#10
0
文件: info.py 项目: alhazred/caiman
    def from_xml(cls, element):
        '''Method to create the DOC software element'''
        chkpt_obj = Software(element.get(Software.SOFTWARE_NAME_LABEL),
                             element.get(Software.SOFTWARE_TYPE_LABEL))

        # The software type label determines whether the transfer is
        # IPS, CPIO or SVR4
        val = element.get(Software.SOFTWARE_TYPE_LABEL)
        if val is None:
            raise ParsingError(
                "No type specified for software node in the manifest")

        # The software_data element holds the items that will be
        # transferred. The val variable identifies what type of transfer
        # object to create.  The action is either to install or uninstall.
        # The contents is a list of items that will be installed or
        # uninstalled. TODO: This could be improved by making it a "def"
        sub_element = element.getchildren()
        for sub in sub_element:
            if sub.tag == SOFTWARE_DATA:
                if val == "IPS":
                    transfer_obj = IPSSpec()
                    action = sub.get(IPSSpec.IPS_ACTION_LABEL)
                    if action is None:
                        action = IPSSpec.INSTALL

                    pkg_list = list()
                    reject_list = list()
                    for child in sub.getchildren():
                        if child.tag == IPSSpec.IPS_NAME_LABEL:
                            pkg_list.append(child.text.strip('"\n\t '))
                        elif child.tag == IPSSpec.IPS_REJECT_LABEL:
                            reject_list.append(child.text.strip('"\n\t '))

                    transfer_obj.action = action
                    transfer_obj.contents = pkg_list
                    transfer_obj.reject_list = reject_list

                elif val == "CPIO":
                    transfer_obj = CPIOSpec()
                    action = sub.get(CPIOSpec.CPIOSPEC_ACTION_LABEL)
                    size = sub.get(CPIOSpec.CPIOSPEC_SIZE_LABEL)

                    if action is None:
                        action = CPIOSpec.INSTALL

                    # A file_list transfer is specified in the manifest.
                    file_list = list()
                    for name in sub.iterchildren(tag="name"):
                        file_list.append(name.text.strip('"\n\t '))

                    transfer_obj.contents = file_list
                    transfer_obj.action = action
                    if size is not None:
                        transfer_obj.size = int(size)

                elif val == "SVR4":
                    transfer_obj = SVR4Spec()
                    action = sub.get(SVR4Spec.SVR4_ACTION_LABEL)

                    if action is None:
                        action = SVR4Spec.INSTALL

                    pkg_list = list()
                    for name in sub.iterchildren(tag="name"):
                        pkg_list.append(name.text.strip('"\n\t '))

                    transfer_obj.action = action
                    transfer_obj.contents = pkg_list

                # Insert the transfer object as a child of the
                # software checkpoint
                chkpt_obj.insert_children([transfer_obj])

        return chkpt_obj
示例#11
0
class Configuration(SimpleXmlHandlerBase):
    TAG_NAME = "configuration"
    NAME_LABEL = "name"
    SOURCE_LABEL = "source"
    TYPE_LABEL = "type"
    DEST_LABEL = "dest"
    VALIDATION_LABEL = "validation"
    PATH_LABEL = "path"
    ARGS_LABEL = "args"
    ON_ERROR_LABEL = "on_error"

    TYPE_VALUE_NETWORK = "network"
    TYPE_VALUE_USER = "******"
    TYPE_VALUE_SYSCONF = "sysconf"
    TYPE_VALUE_ZONE = "zone"

    def __init__(self, name):
        super(Configuration, self).__init__(name)

        self.source = None
        self.dest = None
        self.type = None
        self.validation = None

    def to_xml(self):
        element = etree.Element(Configuration.TAG_NAME)

        element.set(Configuration.NAME_LABEL, self.name)
        element.set(Configuration.SOURCE_LABEL, self.source)

        if self.type is not None:
            element.set(Configuration.TYPE_LABEL, self.type)

        if self.dest is not None:
            element.set(Configuration.DEST_LABEL, self.dest)

        if self.validation is not None:
            validation_element = etree.SubElement(
                element, Configuration.VALIDATION_LABEL)
            for (key, value) in self.validation.items():
                validation_element.set(key, value)
                validation_element.set(key, value)

        return element

    @classmethod
    def can_handle(cls, element):
        '''
        Returns True if element has:
        - the tag 'configuration'
        - a name attribute
        - a source attribute

        Otherwise returns False
        '''
        if element.tag != cls.TAG_NAME:
            return False

        for entry in [cls.NAME_LABEL, cls.SOURCE_LABEL]:
            if element.get(entry) is None:
                return False

        return True

    @classmethod
    def from_xml(cls, element):
        validation = {}

        name = element.get(cls.NAME_LABEL)
        source = element.get(cls.SOURCE_LABEL)
        dest = element.get(cls.DEST_LABEL)
        type = element.get(cls.TYPE_LABEL)
        path = None
        args = None

        # supported source formats are a local file path that starts with a
        # leading slash, or URI strings that start with 'http', 'file', or 'ftp
        if "://" not in source and not source.startswith("file:/"):
            # source is a file path:
            if not os.path.exists(source):
                raise ParsingError("Invalid element specified in "
                                   "the %s section " % cls.NAME_LABEL +
                                   "of the manifest.  "
                                   "source does not exist: %s" % source)
        else:
            try:
                fileobj = urllib.urlopen(source)
            except (IOError), e:
                raise ParsingError("Invalid element specified in "
                                   "the %s section " % cls.NAME_LABEL +
                                   "of the manifest.  "
                                   "Unable to open source (%s): %s" % \
                                   (source, e))

        for subelement in element.iterchildren():
            if subelement.tag == cls.VALIDATION_LABEL:
                path = subelement.get(cls.PATH_LABEL)
                args = subelement.get(cls.ARGS_LABEL)
                on_error = subelement.get(cls.ON_ERROR_LABEL)
                if path is not None:
                    if os.path.exists(path):
                        validation[cls.PATH_LABEL] = path
                    else:
                        raise ParsingError(
                            "Invalid element specified in "
                            "the %s section of " % cls.NAME_LABEL +
                            "the manifest. validation path does not exist: "
                            "%s" % path)
                if args is not None:
                    validation[cls.ARGS_LABEL] = args
                if on_error is not None:
                    validation[cls.ON_ERROR_LABEL] = on_error

        # validate the 'source' if a validation path was specified
        if path is not None:
            try:
                if args is not None:
                    cmd = [path, args, source]
                else:
                    cmd = [path, source]
                subprocess.check_call(cmd, stdout=_NULL, stderr=_NULL)
            except subprocess.CalledProcessError:
                raise ParsingError(
                    "Error reading %s " % cls.NAME_LABEL +
                    "element from the source manifest. source manifest "
                    "specified could not be validated: %s" % source)

        configuration = Configuration(name)
        configuration.source = source
        if dest is not None:
            configuration.dest = dest
        if type is not None:
            configuration.type = type
        configuration.validation = validation

        return configuration