⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xenapi_create.py

📁 xen 3.2.2 源码
💻 PY
📖 第 1 页 / 共 2 页
字号:
                                 for ref, record in                                 server.xenapi.network.get_all_records().items()])                if network_name in networks.keys():                    network_uuid = networks[network_name]                else:                    raise OptionError("Network %s doesn't exist"                                  % vif.attributes["network"].value)        else:            network_uuid = self._get_network_ref()        vif_record = {            "device":                vif.attributes["device"].value,            "network":                network_uuid,            "VM":                vm_ref,            "MAC":                vif.attributes["mac"].value,            "MTU":                vif.attributes["mtu"].value,            "qos_algorithm_type":                vif.attributes["qos_algorithm_type"].value,            "qos_algorithm_params":                get_child_nodes_as_dict(vif,                    "qos_algorithm_param", "key", "value"),            "security_label":                vif.attributes["security_label"].value        }        return server.xenapi.VIF.create(vif_record)    _network_refs = []    def _get_network_ref(self):        try:            return self._network_refs.pop(0)        except IndexError:            self._network_refs = server.xenapi.network.get_all()            return self._network_refs.pop(0)    def create_vtpms(self, vm_ref, vtpms):        if len(vtpms) > 1:            vtpms = [ vtpms[0] ]        log(DEBUG, "create_vtpms")        return map(lambda vtpm: self.create_vtpm(vm_ref, vtpm), vtpms)    def create_vtpm(self, vm_ref, vtpm):        vtpm_record = {            "VM":                vm_ref,            "backend":                vtpm.attributes["backend"].value        }        return server.xenapi.VTPM.create(vtpm_record)    def create_consoles(self, vm_ref, consoles):        log(DEBUG, "create_consoles")        return map(lambda console: self.create_console(vm_ref, console),                   consoles)    def create_console(self, vm_ref, console):        log(DEBUG, "create_consoles")        console_record = {            "VM":                vm_ref,            "protocol":                console.attributes["protocol"].value,            "other_params":                get_child_nodes_as_dict(console,                  "other_param", "key", "value")            }        return server.xenapi.console.create(console_record)def get_child_by_name(exp, childname, default = None):    try:        return [child for child in sxp.children(exp)                if child[0] == childname][0][1]    except:        return default# Convert old sxp into new xmlclass sxp2xml:    def convert_sxp_to_xml(self, config, transient=False):               devices = [child for child in sxp.children(config)                   if len(child) > 0 and child[0] == "device"]                           vbds_sxp = map(lambda x: x[1], [device for device in devices                                        if device[1][0] == "vbd"])        vifs_sxp = map(lambda x: x[1], [device for device in devices                                        if device[1][0] == "vif"])        vtpms_sxp = map(lambda x: x[1], [device for device in devices                                         if device[1][0] == "vtpm"])        # Create XML Document                impl = getDOMImplementation()        document = impl.createDocument(None, "xm", None)        # Lets make the VM tag..        vm = document.createElement("vm")        # Some string compatibility        actions_after_shutdown \            = get_child_by_name(config, "on_poweroff", "destroy")        actions_after_reboot \            = get_child_by_name(config, "on_reboot", "restart")        actions_after_crash \            = get_child_by_name(config, "on_crash", "restart")        def conv_chk(val, vals):            val.replace("-", "_")            if val not in vals:                raise "Invalid value: " + val            else:                return val        actions_after_shutdown = conv_chk(actions_after_shutdown,\                                          XEN_API_ON_NORMAL_EXIT)        actions_after_reboot   = conv_chk(actions_after_reboot, \                                          XEN_API_ON_NORMAL_EXIT)        actions_after_crash    = conv_chk(actions_after_crash, \                                          XEN_API_ON_CRASH_BEHAVIOUR)        # Flesh out tag attributes                    vm.attributes["is_a_template"] = "false"        vm.attributes["auto_power_on"] = "false"        vm.attributes["actions_after_shutdown"] \            = actions_after_shutdown                      vm.attributes["actions_after_reboot"] \            = actions_after_reboot        vm.attributes["actions_after_crash"] \            = actions_after_crash        vm.attributes["PCI_bus"] = ""        vm.attributes["vcpus_max"] \            = str(get_child_by_name(config, "vcpus", 1))        vm.attributes["vcpus_at_startup"] \            = str(get_child_by_name(config, "vcpus", 1))        sec_data = get_child_by_name(config, "security")        if sec_data:            try :                vm.attributes['security_label'] = \                                    security.set_security_label(sec_data[0][1][1],sec_data[0][2][1])            except Exception, e:                raise "Invalid security data format: %s" % str(sec_data)        # Make the name tag        vm.appendChild(self.make_name_tag(            get_child_by_name(config, "name"), document))        # Make version tag        version = document.createElement("version")        version.appendChild(document.createTextNode("0"))        vm.appendChild(version)                # Make pv or hvm tag        image = get_child_by_name(config, "image")        if image[0] == "linux":            pv = document.createElement("pv")            pv.attributes["kernel"] \                = get_child_by_name(image, "kernel", "")            pv.attributes["bootloader"] = ""            pv.attributes["ramdisk"] \                = get_child_by_name(image, "ramdisk", "")            pv.attributes["args"] \                = "root=" + get_child_by_name(image, "root", "") \                + " " + get_child_by_name(image, "args", "")            pv.attributes["bootloader_args"] = ""            vm.appendChild(pv)        elif image[0] == "hvm":            hvm = document.createElement("hvm")            hvm.attributes["boot_policy"] = "BIOS order"            boot_order = document.createElement("boot_param")            boot_order.attributes["key"] = "order"            boot_order.attributes["value"] \                = get_child_by_name(image, "boot", "abcd")            hvm.appendChild            vm.appendChild(hvm)        # Make memory tag        memory = document.createElement("memory")        memory_str = str(int(            get_child_by_name(config, "memory"))*1024*1024)        memory.attributes["static_min"] = str(0)        memory.attributes["static_max"] = memory_str        memory.attributes["dynamic_min"] = memory_str        memory.attributes["dynamic_max"] = memory_str        if get_child_by_name(config, "maxmem"):            memory.attributes["static_max"] = \               str(int(get_child_by_name(config, "maxmem")*1024*1024))        vm.appendChild(memory)        # And now the vbds        vbds = map(lambda vbd: self.extract_vbd(vbd, document), vbds_sxp)        map(vm.appendChild, vbds)        # And now the vifs        vifs = map(lambda vif: self.extract_vif(vif, document), vifs_sxp)        map(vm.appendChild, vifs)        # And now the vTPMs        vtpms = map(lambda vtpm: self.extract_vtpm(vtpm, document), vtpms_sxp)        map(vm.appendChild, vtpms)        # Last but not least the consoles...        consoles = self.extract_consoles(image, document)        map(vm.appendChild, consoles)        # Platform variables...        platform = self.extract_platform(image, document)        map(vm.appendChild, platform)        # transient?        if transient:            other_config = document.createElement("other_config")            other_config.attributes["key"] = "transient"            other_config.attributes["value"] = "True"            vm.appendChild(other_config)                # Add it to doc_root        document.documentElement.appendChild(vm)                # We want to pull out vdis        vdis = map(lambda vdb: self.extract_vdi(vdb, document), vbds_sxp)        map(document.documentElement.appendChild, vdis)        return document    def make_name_tag(self, label_text, document):        name = document.createElement("name")        label = document.createElement("label")        label.appendChild(document.createTextNode(str(label_text)))        name.appendChild(label)        description = document.createElement("description")        description.appendChild(document.createTextNode(" "))        name.appendChild(description)        return name    def extract_vbd(self, vbd_sxp, document):        src = get_child_by_name(vbd_sxp, "uname")        name = str(src.__hash__())        vbd = document.createElement("vbd")        vbd.attributes["name"] = "vdb" + name        vbd.attributes["vdi"] = "vdi" + name        vbd.attributes["mode"] \            = get_child_by_name(vbd_sxp, "mode") != "w" \              and "RO" or "RW"        vbd.attributes["device"] \            = get_child_by_name(vbd_sxp, "dev")        vbd.attributes["bootable"] = "1"        vbd.attributes["type"] = "disk"        vbd.attributes["qos_algorithm_type"] = ""        return vbd    def extract_vdi(self, vbd_sxp, document):        src = get_child_by_name(vbd_sxp, "uname")        name = "vdi" + str(src.__hash__())        vdi = document.createElement("vdi")        vdi.attributes["src"] = src        vdi.attributes["read_only"] \            = (get_child_by_name(vbd_sxp, "mode") != "w") \               and "True" or "False"        vdi.attributes["size"] = '-1'        vdi.attributes["type"] = "system"        vdi.attributes["sharable"] = "False"        vdi.attributes["name"] = name        vdi.appendChild(self.make_name_tag(name, document))        return vdi    def extract_vif(self, vif_sxp, document):        vif = document.createElement("vif")        dev = get_child_by_name(vif_sxp, "vifname", None)        if dev is None:            dev = self.getFreshEthDevice()        vif.attributes["name"] \            = "vif" + str(dev.__hash__())        vif.attributes["mac"] \            = get_child_by_name(vif_sxp, "mac", "")                       vif.attributes["mtu"] \            = get_child_by_name(vif_sxp, "mtu", "")          vif.attributes["device"] = dev        vif.attributes["qos_algorithm_type"] = ""        policy = get_child_by_name(vif_sxp, "policy")        label = get_child_by_name(vif_sxp, "label")        vif.attributes["security_label"] = security.set_security_label(policy, label)        if get_child_by_name(vif_sxp, "bridge") is not None:            vif.attributes["network"] \                = get_child_by_name(vif_sxp, "bridge")                return vif    def extract_vtpm(self, vtpm_sxp, document):        vtpm = document.createElement("vtpm")        vtpm.attributes["backend"] \             = get_child_by_name(vtpm_sxp, "backend", "0")        return vtpm    _eths = -1    def mk_other_config(self, key, value, document):        other_config = document.createElement("other_config")        other_config.attributes["key"] = key        other_config.attributes["value"] = value        return other_config    def extract_consoles(self, image, document):        consoles = []        if int(get_child_by_name(image, "nographic", "1")) == 1:            return consoles                if int(get_child_by_name(image, "vnc", "0")) == 1:            console = document.createElement("console")            console.attributes["protocol"] = "rfb"            console.appendChild(self.mk_other_config(                "vncunused", str(get_child_by_name(image, "vncunused", "0")),                document))            console.appendChild(self.mk_other_config(                "vnclisten",                get_child_by_name(image, "vnclisten", "127.0.0.1"),                document))            console.appendChild(self.mk_other_config(                "vncpasswd", get_child_by_name(image, "vncpasswd", ""),                document))            consoles.append(console)                  if int(get_child_by_name(image, "sdl", "0")) == 1:            console = document.createElement("console")            console.attributes["protocol"] = "sdl"            console.appendChild(self.mk_other_config(                "display", get_child_by_name(image, "display", ""),                document))            console.appendChild(self.mk_other_config(                "xauthority",                get_child_by_name(image, "vxauthority", "127.0.0.1"),                document))            console.appendChild(self.mk_other_config(                "vncpasswd", get_child_by_name(image, "vncpasswd", ""),                document))            consoles.append(console)                    return consoles    def extract_platform(self, image, document):        platform_keys = ['acpi', 'apic', 'pae', 'vhpt', 'timer_mode']        def extract_platform_key(key):            platform = document.createElement("platform")            platform.attributes["key"] = key            platform.attributes["value"] \                = str(get_child_by_name(image, key, "1"))            return platform                return map(extract_platform_key, platform_keys)        def getFreshEthDevice(self):        self._eths += 1        return "eth%i" % self._eths

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -