Exemplo n.º 1
0
    def createSnapshot(self, domain=None, xmlDesc=None):
        retval = False

        if domain is not None:
            parent_snapshot_name = self.getCurrentSnapshotName(domain)

            if xmlDesc is None:
                xml = "<domainsnapshot/>"

            else: # validate xml file
                try:
                    doc = XMLParse(xmlDesc)
                    name = XMLXpath(doc, '/domainsnapshot/name/text()')
                    if name is not None:
                        xml = xmlDesc
                except:
                    pass
            try:
                xml
                guest = self.kvc.search_guests(domain)[0]
                snapshot = libvirtmod.virDomainSnapshotCreateXML(guest._o,xml,0)
                if snapshot is not False:
                    retval = libvirtmod.virDomainSnapshotGetXMLDesc(snapshot, 0)
            except:
                pass

        if retval is not False:
            kvg_guest = self.kvc.search_kvg_guests(domain)[0]
            id = self.getCurrentSnapshotName(domain)
            kvg_guest.set_current_snapshot(id)

            # ここにsnapshotのxmlファイルに親のsnapshotの情報を書き込む処理
            try:
                xml_path = self.getSnapshotXMLPath(id)

                # <parent/>が設定されてない場合
                # かつ、snapshot実行前に<currentSnapshot/>が設定されていた場合
                if self.getParentName(id) is None and parent_snapshot_name is not None:
                    if os.path.exists(xml_path):
                        doc = XMLParse(xml_path)
                        parent = doc.createElement("parent")
                        name   = doc.createElement("name")
                        txt = doc.createTextNode(str(parent_snapshot_name))
                        name.appendChild(txt)
                        parent.appendChild(name)
                        doc.childNodes[0].appendChild(parent)
                        xmlDesc = self.generateXML(doc)

                        ConfigFile(xml_path).write(xmlDesc)

                if os.path.exists(xml_path):
                    if os.getuid() == 0:
                        r_chgrp(xml_path,KARESANSUI_GROUP)
                        r_chmod(xml_path,"g+rw")
                        r_chmod(xml_path,"o-rwx")
            except:
                pass

        return retval
Exemplo n.º 2
0
    def createSnapshot(self, domain=None, xmlDesc=None):
        retval = False

        if domain is not None:
            parent_snapshot_name = self.getCurrentSnapshotName(domain)

            if xmlDesc is None:
                xml = "<domainsnapshot/>"

            else:  # validate xml file
                try:
                    doc = XMLParse(xmlDesc)
                    name = XMLXpath(doc, '/domainsnapshot/name/text()')
                    if name is not None:
                        xml = xmlDesc
                except:
                    pass
            try:
                xml
                guest = self.kvc.search_guests(domain)[0]
                snapshot = libvirtmod.virDomainSnapshotCreateXML(
                    guest._o, xml, 0)
                if snapshot is not False:
                    retval = libvirtmod.virDomainSnapshotGetXMLDesc(
                        snapshot, 0)
            except:
                pass

        if retval is not False:
            kvg_guest = self.kvc.search_kvg_guests(domain)[0]
            id = self.getCurrentSnapshotName(domain)
            kvg_guest.set_current_snapshot(id)

            # ここにsnapshotのxmlファイルに親のsnapshotの情報を書き込む処理
            try:
                xml_path = self.getSnapshotXMLPath(id)

                # <parent/>が設定されてない場合
                # かつ、snapshot実行前に<currentSnapshot/>が設定されていた場合
                if self.getParentName(
                        id) is None and parent_snapshot_name is not None:
                    if os.path.exists(xml_path):
                        doc = XMLParse(xml_path)
                        parent = doc.createElement("parent")
                        name = doc.createElement("name")
                        txt = doc.createTextNode(str(parent_snapshot_name))
                        name.appendChild(txt)
                        parent.appendChild(name)
                        doc.childNodes[0].appendChild(parent)
                        xmlDesc = self.generateXML(doc)

                        ConfigFile(xml_path).write(xmlDesc)

                if os.path.exists(xml_path):
                    if os.getuid() == 0:
                        r_chgrp(xml_path, KARESANSUI_GROUP)
                        r_chmod(xml_path, "g+rw")
                        r_chmod(xml_path, "o-rwx")
            except:
                pass

        return retval
Exemplo n.º 3
0
    def read_firewall_xml(self, path=None):

        config = {}

        if path is None:
            path = self.firewall_xml_file

        if not os.path.exists(path) or os.path.getsize(path) == 0:
            raise KaresansuiIpTablesException("no such file: %s" % path)

        document = XMLParse(path)

        table_num = XMLXpathNum(document, '/firewall/table')
        for tbl in range(1, table_num + 1):
            table_name = XMLXpath(document,
                                  '/firewall/table[%i]/@name' % (tbl, ))
            if table_name is None:
                table_name = 'filter'

            chain = {}
            chain_num = XMLXpathNum(document,
                                    '/firewall/table[%i]/chain' % (tbl, ))
            for chn in range(1, chain_num + 1):
                chain_name = XMLXpath(
                    document, '/firewall/table[%i]/chain[%i]/@name' % (
                        tbl,
                        chn,
                    ))
                chain_policy = XMLXpath(
                    document, '/firewall/table[%i]/chain[%i]/@policy' % (
                        tbl,
                        chn,
                    ))

                rule = []
                rule_num = XMLXpathNum(
                    document, '/firewall/table[%i]/chain[%i]/rule' % (
                        tbl,
                        chn,
                    ))
                for rl in range(1, rule_num + 1):
                    rule_id = XMLXpath(
                        document,
                        '/firewall/table[%i]/chain[%i]/rule[%i]/@id' % (
                            tbl,
                            chn,
                            rl,
                        ))

                    target = XMLXpath(
                        document,
                        '/firewall/table[%i]/chain[%i]/rule[%i]/target/text()'
                        % (
                            tbl,
                            chn,
                            rl,
                        ))
                    if target is None:
                        target = ''
                    else:
                        target = target.strip()

                    protocol = XMLXpath(
                        document,
                        '/firewall/table[%i]/chain[%i]/rule[%i]/protocol/text()'
                        % (
                            tbl,
                            chn,
                            rl,
                        ))
                    if protocol is None:
                        protocol = ''
                    else:
                        protocol = protocol.strip()
                        if protocol == "50":
                            protocol = "esp"
                        if protocol == "51":
                            protocol = "ah"

                    source = XMLXpath(
                        document,
                        '/firewall/table[%i]/chain[%i]/rule[%i]/source/text()'
                        % (
                            tbl,
                            chn,
                            rl,
                        ))
                    if source is None:
                        source = ''
                    else:
                        source = source.strip()

                    destination = XMLXpath(
                        document,
                        '/firewall/table[%i]/chain[%i]/rule[%i]/destination/text()'
                        % (
                            tbl,
                            chn,
                            rl,
                        ))
                    if destination is None:
                        destination = ''
                    else:
                        destination = destination.strip()

                    sport = XMLXpath(
                        document,
                        '/firewall/table[%i]/chain[%i]/rule[%i]/source-port/text()'
                        % (
                            tbl,
                            chn,
                            rl,
                        ))
                    if sport is None:
                        sport = ''
                    else:
                        sport = sport.strip()

                    dport = XMLXpath(
                        document,
                        '/firewall/table[%i]/chain[%i]/rule[%i]/destination-port/text()'
                        % (
                            tbl,
                            chn,
                            rl,
                        ))
                    if dport is None:
                        dport = ''
                    else:
                        dport = dport.strip()

                    inif = XMLXpath(
                        document,
                        '/firewall/table[%i]/chain[%i]/rule[%i]/in-interface/text()'
                        % (
                            tbl,
                            chn,
                            rl,
                        ))
                    if inif is None:
                        inif = ''
                    else:
                        inif = inif.strip()

                    outif = XMLXpath(
                        document,
                        '/firewall/table[%i]/chain[%i]/rule[%i]/out-interface/text()'
                        % (
                            tbl,
                            chn,
                            rl,
                        ))
                    if outif is None:
                        outif = ''
                    else:
                        outif = outif.strip()

                    option = XMLXpath(
                        document,
                        '/firewall/table[%i]/chain[%i]/rule[%i]/option/text()'
                        % (
                            tbl,
                            chn,
                            rl,
                        ))
                    if option is None:
                        option = ''
                    else:
                        option = option.strip()

                    rule_info = {
                        "id": rule_id,
                        "target": target,
                        "protocol": protocol,
                        "source": source,
                        "destination": destination,
                        "source-port": sport,
                        "destination-port": dport,
                        "in-interface": inif,
                        "out-interface": outif,
                        "option": option,
                    }

                    rule.append(rule_info)

                chain_info = {
                    "policy": chain_policy,
                    "rule": rule,
                }
                chain[chain_name] = chain_info

            config[table_name] = chain

        return config
Exemplo n.º 4
0
    def read_firewall_xml(self,path=None):

        config = {}

        if path is None:
            path = self.firewall_xml_file

        if not os.path.exists(path) or os.path.getsize(path) == 0:
            raise KaresansuiIpTablesException("no such file: %s" % path)

        document = XMLParse(path)
        
        table_num = XMLXpathNum(document,'/firewall/table')
        for tbl in range(1, table_num + 1):
            table_name = XMLXpath(document,'/firewall/table[%i]/@name' % (tbl,))
            if table_name is None:
                table_name = 'filter'

            chain = {}
            chain_num = XMLXpathNum(document,'/firewall/table[%i]/chain' % (tbl,))
            for chn in range(1, chain_num + 1):
                chain_name = XMLXpath(document,'/firewall/table[%i]/chain[%i]/@name' % (tbl,chn,))
                chain_policy = XMLXpath(document,'/firewall/table[%i]/chain[%i]/@policy' % (tbl,chn,))

                rule = []
                rule_num = XMLXpathNum(document,'/firewall/table[%i]/chain[%i]/rule' % (tbl,chn,))
                for rl in range(1, rule_num + 1):
                    rule_id = XMLXpath(document,'/firewall/table[%i]/chain[%i]/rule[%i]/@id' % (tbl,chn,rl,))

                    target = XMLXpath(document,'/firewall/table[%i]/chain[%i]/rule[%i]/target/text()' % (tbl,chn,rl,))
                    if target is None:
                        target = ''
                    else:
                        target = target.strip()

                    protocol = XMLXpath(document,'/firewall/table[%i]/chain[%i]/rule[%i]/protocol/text()' % (tbl,chn,rl,))
                    if protocol is None:
                        protocol = ''
                    else:
                        protocol = protocol.strip()
                        if protocol == "50":
                            protocol = "esp"
                        if protocol == "51":
                            protocol = "ah"

                    source = XMLXpath(document,'/firewall/table[%i]/chain[%i]/rule[%i]/source/text()' % (tbl,chn,rl,))
                    if source is None:
                        source = ''
                    else:
                        source = source.strip()

                    destination = XMLXpath(document,'/firewall/table[%i]/chain[%i]/rule[%i]/destination/text()' % (tbl,chn,rl,))
                    if destination is None:
                        destination = ''
                    else:
                        destination = destination.strip()

                    sport = XMLXpath(document,'/firewall/table[%i]/chain[%i]/rule[%i]/source-port/text()' % (tbl,chn,rl,))
                    if sport is None:
                        sport = ''
                    else:
                        sport = sport.strip()

                    dport = XMLXpath(document,'/firewall/table[%i]/chain[%i]/rule[%i]/destination-port/text()' % (tbl,chn,rl,))
                    if dport is None:
                        dport = ''
                    else:
                        dport = dport.strip()

                    inif = XMLXpath(document,'/firewall/table[%i]/chain[%i]/rule[%i]/in-interface/text()' % (tbl,chn,rl,))
                    if inif is None:
                        inif = ''
                    else:
                        inif = inif.strip()

                    outif = XMLXpath(document,'/firewall/table[%i]/chain[%i]/rule[%i]/out-interface/text()' % (tbl,chn,rl,))
                    if outif is None:
                        outif = ''
                    else:
                        outif = outif.strip()

                    option = XMLXpath(document,'/firewall/table[%i]/chain[%i]/rule[%i]/option/text()' % (tbl,chn,rl,))
                    if option is None:
                        option = ''
                    else:
                        option = option.strip()

                    rule_info = {"id": rule_id,
                        "target": target,
                        "protocol": protocol,
                        "source": source,
                        "destination": destination,
                        "source-port": sport,
                        "destination-port": dport,
                        "in-interface": inif,
                        "out-interface": outif,
                        "option": option,
                       }

                    rule.append(rule_info)

                chain_info = {"policy": chain_policy,
                        "rule": rule,
                       }
                chain[chain_name] = chain_info

            config[table_name] = chain

        return config