Exemplo n.º 1
0
    def _GET(self, *param, **params):
        (host_id, guest_id) = self.chk_guestby1(param)
        if guest_id is None: return web.notfound()

        if is_int(param[2]) is False:
            return web.badrequest()

        disk_id = int(param[2])

        model = findbyguest1(self.orm, guest_id)

        # virt
        kvc = KaresansuiVirtConnection()

        try:
            domname = kvc.uuid_to_domname(model.uniq_key)
            if not domname: return web.notfound()

            virt = kvc.search_kvg_guests(domname)[0]

            guest = MergeGuest(model, virt)
            self.view.guest = guest
            self.view.disk_info = virt.get_disk_info()[disk_id]
        finally:
            kvc.close()

        return True
Exemplo n.º 2
0
    def _DELETE(self, *param, **params):
        (host_id, guest_id) = self.chk_guestby1(param)
        if guest_id is None: return web.notfound()

        if is_int(param[2]) is False:
            return web.notfound()       

        nic_id = int(param[2])

        model = findbyguest1(self.orm, guest_id)
        
        # virt
        kvc = KaresansuiVirtConnection()
        try:
            domname = kvc.uuid_to_domname(model.uniq_key)
            if not domname: return web.conflict(web.ctx.path)
            virt = kvc.search_kvg_guests(domname)[0]
            guest = MergeGuest(model, virt)
            nic_info = virt.get_interface_info()[nic_id]
        finally:
            kvc.close()

        mac = nic_info["mac"]["address"]
        self.logger.debug('spinning off delete_nic_job dom=%s, mac=%s' % (domname, mac))
        if delete_nic_job(self,model,domname,mac) is True:
            return web.accepted()
        else:
            return False
Exemplo n.º 3
0
    def _GET(self, *param, **params):
        """<comment-ja>
        virDomainState
         - VIR_DOMAIN_NOSTATE = 0
         - VIR_DOMAIN_RUNNING = 1
         - VIR_DOMAIN_BLOCKED = 2
         - VIR_DOMAIN_PAUSED = 3
         - VIR_DOMAIN_SHUTDOWN = 4
         - VIR_DOMAIN_SHUTOFF = 5
         - VIR_DOMAIN_CRASHED = 6
        </comment-ja>
        <comment-en>
        TODO: English Comment
        </comment-en>
        """

        host_id =  param[0]
        host_id = self.chk_hostby1(param)
        if host_id is None:
            return web.notfound()

        uri_id =  param[1]
        if uri_id is None:
            return web.notfound()

        model = findbyhost1(self.orm, host_id)

        if self.is_mode_input() is False:
            if model.attribute == 2:
                info = {}
                segs = uri_split(model.hostname)
                uri = uri_join(segs, without_auth=True)
                creds = ''
                if segs["user"] is not None:
                    creds += segs["user"]
                    if segs["passwd"] is not None:
                        creds += ':' + segs["passwd"]
                self.kvc = KaresansuiVirtConnectionAuth(uri,creds)

                try:
                    host = MergeHost(self.kvc, model)
                    for guest in host.guests:
                        _virt = self.kvc.search_kvg_guests(guest.info["model"].name)
                        if 0 < len(_virt):
                            for _v in _virt:
                                info = _v.get_info()
                                if info["uuid"] == uri_id or (uri[0:5] == "test:"):
                                    __guest = MergeGuest(guest.info["model"],_v)
                                    status          = _v.status()
                                    break

                    if self.is_json() is True:
                        self.view.status = json_dumps(status)
                    else:
                        self.view.status = status
                finally:
                    self.kvc.close()

        return True
Exemplo n.º 4
0
    def _PUT(self, *param, **params):

        (host_id, guest_id) = self.chk_guestby1(param)
        if guest_id is None: return web.notfound()

        if is_int(param[2]) is False:
            return web.notfound()       
        nic_id = int(param[2])


        if not validates_nicby1(self):
            return web.badrequest(self.view.alert)

        model = findbyguest1(self.orm, guest_id)
        # virt
        kvc = KaresansuiVirtConnection()
        try:
            domname = kvc.uuid_to_domname(model.uniq_key)
            if not domname: return web.conflict(web.ctx.path)
            virt = kvc.search_kvg_guests(domname)[0]
            guest = MergeGuest(model, virt)
            old_mac = virt.get_interface_info()[nic_id]['mac']['address']
            nic_info = virt.get_interface_info()
        finally:
            kvc.close()

        new_mac = self.input.mac_address

        if old_mac != new_mac:
            f_chk = True
            for x in nic_info:
                if x['mac']['address'] == new_mac:
                    f_chk = False
                    break
            if f_chk is False:
                return web.badrequest(_('Specified MAC address is already defined.'))

            self.logger.debug('spinning off change_mac_job dom=%s, from_mac=%s, to_mac=%s' % (domname, old_mac, new_mac))
            if change_mac_job(self, model, domname, old_mac, new_mac) is True:
                return web.accepted(url=web.ctx.path)
            else:
                return False

        else:
            return web.accepted(url=web.ctx.path)
Exemplo n.º 5
0
    def _DELETE(self, *param, **params):
        (host_id, guest_id) = self.chk_guestby1(param)
        if guest_id is None: return web.notfound()

        if is_int(param[2]) is False:
            return web.badrequest()

        disk_id = int(param[2])

        model = findbyguest1(self.orm, guest_id)
        if not model: return web.notfound()

        # virt
        kvc = KaresansuiVirtConnection()
        try:
            domname = kvc.uuid_to_domname(model.uniq_key)
            if not domname: return web.conflict(web.ctx.path)

            virt = kvc.search_kvg_guests(domname)[0]
            guest = MergeGuest(model, virt)
            disk_info = virt.get_disk_info()[disk_id]

            if disk_info['source'].has_key('file'):
                pool_type = 'file'
                volume_rpath = disk_info['source']['file']
            elif disk_info['source'].has_key('dev'):
                pool_type = 'iscsi'
                volume_rpath = disk_info['source']['dev']

            disk_type = disk_info['type']
            disk_device = disk_info['device']

            if disk_device != "cdrom":
                pool_name = kvc.get_storage_pool_name_byimage(volume_rpath)
                if not pool_name:
                    return web.badrequest(_("Storage pool not found."))
                else:
                    pool_name = pool_name[0]
                pool_type = kvc.get_storage_pool_type(pool_name)

            order = 0
            volume_job = None
            if pool_type != 'iscsi' and disk_type != "block":
                disk_volumes = kvc.get_storage_volume_bydomain(
                    domname, 'disk', 'key')
                volume = None

                for key in disk_volumes.keys():
                    if volume_rpath == os.path.realpath(disk_volumes[key]):
                        volume = key

                if volume is None:
                    return web.badrequest(
                        _("Storage volume can not be found."))

                volume_job = delete_storage_volume(self,
                                                   key,
                                                   pool_name,
                                                   order,
                                                   use=DISK_USES["DISK"])
                order += 1
        finally:
            kvc.close()

        target = disk_info["target"]["dev"]
        self.logger.debug('spinning off delete_disk_job dom=%s, target=%s' %
                          (domname, target))

        disk_job = delete_disk_job(self, domname, target, order)
        if setexec_delete_disk(self, model, disk_job, volume_job) is True:
            return web.accepted()
        else:
            return False
Exemplo n.º 6
0
    def _GET(self, *param, **params):
        (host_id, guest_id) = self.chk_guestby1(param)

        if self.input.has_key('job_id') is True:
            self.view.job_id = self.input.job_id
        else:
            self.view.job_id = None

        if guest_id is None:
            return web.notfound()

        model = findbyguest1(self.orm, guest_id)

        self.kvc = KaresansuiVirtConnection()
        if self.is_mode_input() is True:
            try:
                domname = self.kvc.uuid_to_domname(model.uniq_key)
                if not domname:
                    return web.notfound()

                virt = self.kvc.search_kvg_guests(domname)[0]
                guest = MergeGuest(model, virt)
                self.view.model = guest.info["model"]
                return True
            except:
                self.kvc.close()
                raise
        else:
            try:
                domname = self.kvc.uuid_to_domname(model.uniq_key)
                if not domname:
                    return web.notfound()

                virt = self.kvc.search_kvg_guests(domname)[0]

                guest = MergeGuest(model, virt)

                guest_info = guest.info["virt"].get_info()
                info = {}
                info['memory'] = guest_info["memory"]
                info['cpu'] = guest_info["cpuTime"]
                info['os'] = guest_info["OSType"]
                info['hypervisor'] = guest_info["hypervisor"]
                info['type'] = guest_info["VMType"]
                info['hv_version'] = guest_info["hv_version"]

                disk_info = guest.info["virt"].get_disk_info()
                interface_info = guest.info["virt"].get_interface_info()
                net_info = guest.info["virt"].get_netinfo()
                graphics_info = guest.info["virt"].get_graphics_info()
                vcpu_info = guest.info["virt"].get_vcpus_info()

                pool_info = []
                pool_vols_info = []
                already_vols = []
                pools = self.kvc.get_storage_pool_name_bydomain(domname)
                if len(pools) > 0:
                    for pool in pools:
                        pool_obj = self.kvc.search_kvn_storage_pools(pool)[0]
                        if pool_obj.is_active() is True:
                            pool_info = pool_obj.get_info()

                            vols_obj = pool_obj.search_kvn_storage_volumes(
                                self.kvc)
                            vols_info = {}

                            for vol_obj in vols_obj:
                                vol_name = vol_obj.get_storage_volume_name()
                                vols_info[vol_name] = vol_obj.get_info()

                            pool_vols_info = vols_info

                if self.__template__["media"] == 'json':
                    json_guest = guest.get_json(self.me.languages)
                    self.view.data = json_dumps({
                        "model":
                        json_guest["model"],
                        "virt":
                        json_guest["virt"],
                        "autostart":
                        guest.info["virt"].autostart(),
                        "info":
                        info,
                        "disk_info":
                        disk_info,
                        "net_info":
                        net_info,
                        "interface_info":
                        interface_info,
                        "pool_info":
                        pool_info,
                        "pool_vols_info":
                        pool_vols_info,
                        "graphics_info":
                        graphics_info,
                        "vcpu_info":
                        vcpu_info,
                    })
                else:
                    self.view.model = guest.info["model"]
                    self.view.virt = guest.info["virt"]
                    self.view.autostart = guest.info["virt"].autostart()
                    self.view.info = info
                    self.view.disk_info = disk_info
                    self.view.net_info = net_info
                    self.view.interface_info = interface_info
                    self.view.pool_info = pool_info
                    self.view.pool_vols_info = pool_vols_info
                    self.view.graphics_info = graphics_info
                    self.view.vcpu_info = vcpu_info
            except:
                self.kvc.close()
                raise

            return True
Exemplo n.º 7
0
    def _GET(self, *param, **params):

        if self.input.has_key('job_id') is True:
            self.view.job_id = self.input.job_id
        else:
            self.view.job_id = None

        host_id =  param[0]
        host_id = self.chk_hostby1(param)
        if host_id is None:
            return web.notfound()

        uri_id =  param[1]
        if uri_id is None:
            return web.notfound()

        model = findbyhost1(self.orm, host_id)

        if self.is_mode_input() is False:
            if model.attribute == 2:
                info = {}
                segs = uri_split(model.hostname)
                uri = uri_join(segs, without_auth=True)
                creds = ''
                if segs["user"] is not None:
                    creds += segs["user"]
                    if segs["passwd"] is not None:
                        creds += ':' + segs["passwd"]
                self.kvc = KaresansuiVirtConnectionAuth(uri,creds)

                try:
                    host = MergeHost(self.kvc, model)
                    for guest in host.guests:
                        if not '__guest' in locals():
                            _virt = self.kvc.search_kvg_guests(guest.info["model"].name)
                            if 0 < len(_virt):
                                for _v in _virt:
                                    info = _v.get_info()
                                    if info["uuid"] == uri_id or (uri[0:5] == "test:"):
                                        __guest = MergeGuest(guest.info["model"],_v)
                                        autostart       = _v.autostart()
                                        status          = _v.status()
                                        is_creatable    = _v.is_creatable()
                                        is_shutdownable = _v.is_shutdownable()
                                        is_suspendable  = _v.is_suspendable()
                                        is_resumable    = _v.is_resumable()
                                        is_destroyable  = _v.is_destroyable()
                                        is_active       = _v.is_active()
                                        break

                    if self.is_json() is True:
                        json_host  = host.get_json(self.me.languages)
                        json_guest = __guest.get_json(self.me.languages)
                        self.view.data = json_dumps(
                            {
                                "parent_model": json_host["model"],
                                "parent_virt": json_host["virt"],
                                "model": json_guest["model"],
                                "virt": json_guest["virt"],
                                "info": info,
                                "autostart": autostart,
                                "status": status,
                                "is_creatable": is_creatable,
                                "is_shutdownable": is_shutdownable,
                                "is_suspendable": is_suspendable,
                                "is_resumable": is_resumable,
                                "is_destroyable": is_destroyable,
                                "is_active": is_active,
                            }
                        )
                    else:
                        self.view.parent_model = host.info["model"]
                        self.view.parent_virt = host.info["virt"]
                        self.view.model = __guest.info["model"]
                        self.view.virt = __guest.info["virt"]
                        self.view.info = info
                        self.view.autostart = autostart
                        self.view.status = status
                        self.view.is_creatable = is_creatable
                        self.view.is_shutdownable = is_shutdownable
                        self.view.is_suspendable = is_suspendable
                        self.view.is_resumable = is_resumable
                        self.view.is_destroyable = is_destroyable
                        self.view.is_active = is_active
                finally:
                    self.kvc.close()

        return True
Exemplo n.º 8
0
    def _GET(self, *param, **params):
        (host_id, guest_id) = self.chk_guestby1(param)

        if self.input.has_key('job_id') is True:
            self.view.job_id = self.input.job_id
        else:
            self.view.job_id = None

        if guest_id is None:
            return web.notfound()

        model = findbyguest1(self.orm, guest_id)

        self.kvc = KaresansuiVirtConnection()
        if self.is_mode_input() is True:
            try:
                domname = self.kvc.uuid_to_domname(model.uniq_key)
                if not domname:
                    return web.notfound()

                virt = self.kvc.search_kvg_guests(domname)[0]
                guest = MergeGuest(model, virt)
                self.view.model = guest.info["model"]
                return True
            except:
                self.kvc.close()
                raise
        else:
            try:
                domname = self.kvc.uuid_to_domname(model.uniq_key)
                if not domname:
                    return web.notfound()

                virt = self.kvc.search_kvg_guests(domname)[0]

                guest = MergeGuest(model, virt)

                guest_info = guest.info["virt"].get_info()
                info = {}
                info['memory'] = guest_info["memory"]
                info['cpu'] = guest_info["cpuTime"]
                info['os'] = guest_info["OSType"]
                info['hypervisor'] = guest_info["hypervisor"]
                info['type'] = guest_info["VMType"]
                info['hv_version'] = guest_info["hv_version"]

                disk_info = guest.info["virt"].get_disk_info()
                interface_info = guest.info["virt"].get_interface_info()
                net_info = guest.info["virt"].get_netinfo()
                graphics_info = guest.info["virt"].get_graphics_info()
                vcpu_info = guest.info["virt"].get_vcpus_info()

                pool_info = []
                pool_vols_info = []
                already_vols = []
                pools = self.kvc.get_storage_pool_name_bydomain(domname)
                if len(pools) > 0:
                    for pool in pools:
                        pool_obj = self.kvc.search_kvn_storage_pools(pool)[0]
                        if pool_obj.is_active() is True:
                            pool_info = pool_obj.get_info()

                            vols_obj = pool_obj.search_kvn_storage_volumes(self.kvc)
                            vols_info = {}

                            for vol_obj in vols_obj:
                                vol_name = vol_obj.get_storage_volume_name()
                                vols_info[vol_name] = vol_obj.get_info()

                            pool_vols_info = vols_info

                if self.__template__["media"] == 'json':
                    json_guest = guest.get_json(self.me.languages)
                    self.view.data = json_dumps(
                        {
                            "model": json_guest["model"],
                            "virt": json_guest["virt"],
                            "autostart": guest.info["virt"].autostart(),
                            "info": info,
                            "disk_info": disk_info,
                            "net_info": net_info,
                            "interface_info": interface_info,
                            "pool_info": pool_info,
                            "pool_vols_info": pool_vols_info,
                            "graphics_info": graphics_info,
                            "vcpu_info": vcpu_info,
                        }
                    )
                else:
                    self.view.model = guest.info["model"]
                    self.view.virt = guest.info["virt"]
                    self.view.autostart = guest.info["virt"].autostart()
                    self.view.info = info
                    self.view.disk_info = disk_info
                    self.view.net_info = net_info
                    self.view.interface_info = interface_info
                    self.view.pool_info = pool_info
                    self.view.pool_vols_info = pool_vols_info
                    self.view.graphics_info = graphics_info
                    self.view.vcpu_info = vcpu_info
            except:
                self.kvc.close()
                raise

            return True
Exemplo n.º 9
0
    def _GET(self, *param, **params):
        host_id = self.chk_hostby1(param)
        if host_id is None: return web.notfound()

        model = findbyhost1(self.orm, host_id)
        uris = available_virt_uris()

        #import pdb; pdb.set_trace()
        if model.attribute == MACHINE_ATTRIBUTE["URI"]:
            uri_guests = []
            uri_guests_status = {}
            uri_guests_kvg = {}
            uri_guests_info = {}
            uri_guests_name = {}
            segs = uri_split(model.hostname)
            uri = uri_join(segs, without_auth=True)
            creds = ''
            if segs["user"] is not None:
                creds += segs["user"]
                if segs["passwd"] is not None:
                    creds += ':' + segs["passwd"]

            # Output .part
            if self.is_mode_input() is not True:
                try:
                    self.kvc = KaresansuiVirtConnectionAuth(uri, creds)
                    host = MergeHost(self.kvc, model)
                    for guest in host.guests:

                        _virt = self.kvc.search_kvg_guests(
                            guest.info["model"].name)
                        if 0 < len(_virt):
                            for _v in _virt:
                                uuid = _v.get_info()["uuid"]
                                uri_guests_info[uuid] = guest.info
                                uri_guests_kvg[uuid] = _v
                                uri_guests_name[uuid] = guest.info[
                                    "model"].name.encode("utf8")

                    for name in sorted(uri_guests_name.values(),
                                       key=str.lower):
                        for uuid in dict_search(name, uri_guests_name):
                            uri_guests.append(
                                MergeGuest(uri_guests_info[uuid]["model"],
                                           uri_guests_kvg[uuid]))
                            uri_guests_status[uuid] = uri_guests_info[uuid][
                                'virt'].status()

                finally:
                    self.kvc.close()

                # .json
                if self.is_json() is True:
                    guests_json = []
                    for x in uri_guests:
                        guests_json.append(x.get_json(self.me.languages))

                    self.view.uri_guests = json_dumps(guests_json)
                else:
                    self.view.uri_guests = uri_guests
                    self.view.uri_guests_status = uri_guests_status

        self.kvc = KaresansuiVirtConnection()
        try:  # libvirt connection scope -->

            # Storage Pool
            #inactive_pool = self.kvc.list_inactive_storage_pool()
            inactive_pool = []
            active_pool = self.kvc.list_active_storage_pool()
            pools = inactive_pool + active_pool
            pools.sort()

            if not pools:
                return web.badrequest('One can not start a storage pool.')

            # Output .input
            if self.is_mode_input() is True:
                self.view.pools = pools
                pools_info = {}
                pools_vols_info = {}
                pools_iscsi_blocks = {}
                already_vols = []
                guests = []

                guests += self.kvc.list_inactive_guest()
                guests += self.kvc.list_active_guest()
                for guest in guests:
                    already_vol = self.kvc.get_storage_volume_bydomain(
                        domain=guest, image_type=None, attr='path')
                    if already_vol:
                        already_vols += already_vol.keys()

                for pool in pools:
                    pool_obj = self.kvc.search_kvn_storage_pools(pool)[0]
                    if pool_obj.is_active() is True:
                        pools_info[pool] = pool_obj.get_info()

                        blocks = None
                        if pools_info[pool]['type'] == 'iscsi':
                            blocks = self.kvc.get_storage_volume_iscsi_block_bypool(
                                pool)
                            if blocks:
                                pools_iscsi_blocks[pool] = []
                        vols_obj = pool_obj.search_kvn_storage_volumes(
                            self.kvc)
                        vols_info = {}

                        for vol_obj in vols_obj:
                            vol_name = vol_obj.get_storage_volume_name()
                            vols_info[vol_name] = vol_obj.get_info()
                            if blocks:
                                if vol_name in blocks and vol_name not in already_vols:
                                    pools_iscsi_blocks[pool].append(
                                        vol_obj.get_info())

                        pools_vols_info[pool] = vols_info

                self.view.pools_info = pools_info
                self.view.pools_vols_info = pools_vols_info
                self.view.pools_iscsi_blocks = pools_iscsi_blocks

                bridge_prefix = {
                    "XEN": "xenbr",
                    "KVM": KVM_BRIDGE_PREFIX,
                }
                self.view.host_id = host_id
                self.view.DEFAULT_KEYMAP = DEFAULT_KEYMAP
                self.view.DISK_NON_QEMU_FORMAT = DISK_NON_QEMU_FORMAT
                self.view.DISK_QEMU_FORMAT = DISK_QEMU_FORMAT

                self.view.hypervisors = {}
                self.view.mac_address = {}
                self.view.keymaps = {}
                self.view.phydev = {}
                self.view.virnet = {}

                used_ports = {}

                for k, v in MACHINE_HYPERVISOR.iteritems():
                    if k in available_virt_mechs():
                        self.view.hypervisors[k] = v
                        uri = uris[k]
                        mem_info = self.kvc.get_mem_info()
                        active_networks = self.kvc.list_active_network()
                        used_graphics_ports = self.kvc.list_used_graphics_port(
                        )
                        bus_types = self.kvc.bus_types
                        self.view.bus_types = bus_types
                        self.view.max_mem = mem_info['host_max_mem']
                        self.view.free_mem = mem_info['host_free_mem']
                        self.view.alloc_mem = mem_info['guest_alloc_mem']

                        self.view.mac_address[k] = generate_mac_address(k)
                        self.view.keymaps[k] = eval(
                            "get_keymaps(%s_KEYMAP_DIR)" % k)

                        # Physical device
                        phydev = []
                        phydev_regex = re.compile(r"%s" % bridge_prefix[k])
                        for dev, dev_info in get_ifconfig_info().iteritems():
                            try:
                                if phydev_regex.match(dev):
                                    phydev.append(dev)
                            except:
                                pass
                        if len(phydev) == 0:
                            phydev.append("%s0" % bridge_prefix[k])
                        phydev.sort()
                        self.view.phydev[k] = phydev  # Physical device

                        # Virtual device
                        self.view.virnet[k] = sorted(active_networks)
                        used_ports[k] = used_graphics_ports

                exclude_ports = []
                for k, _used_port in used_ports.iteritems():
                    exclude_ports = exclude_ports + _used_port
                    exclude_ports = sorted(exclude_ports)
                    exclude_ports = [
                        p for p, q in zip(exclude_ports, exclude_ports[1:] +
                                          [None]) if p != q
                    ]
                self.view.graphics_port = next_number(GRAPHICS_PORT_MIN_NUMBER,
                                                      PORT_MAX_NUMBER,
                                                      exclude_ports)

            else:  # .part
                models = findbyhost1guestall(self.orm, host_id)
                guests = []
                if models:
                    # Physical Guest Info
                    self.view.hypervisors = {}
                    for model in models:
                        for k, v in MACHINE_HYPERVISOR.iteritems():
                            if k in available_virt_mechs():
                                self.view.hypervisors[k] = v
                                uri = uris[k]
                                if hasattr(self, "kvc") is not True:
                                    self.kvc = KaresansuiVirtConnection(uri)
                                domname = self.kvc.uuid_to_domname(
                                    model.uniq_key)
                                #if not domname: return web.conflict(web.ctx.path)
                                _virt = self.kvc.search_kvg_guests(domname)
                                if 0 < len(_virt):
                                    guests.append(MergeGuest(model, _virt[0]))
                                else:
                                    guests.append(MergeGuest(model, None))

                # Exported Guest Info
                exports = {}
                for pool_name in pools:
                    files = []

                    pool = self.kvc.search_kvn_storage_pools(pool_name)
                    path = pool[0].get_info()["target"]["path"]

                    if os.path.exists(path):
                        for _afile in glob.glob("%s/*/info.dat" % (path, )):
                            param = ExportConfigParam()
                            param.load_xml_config(_afile)

                            _dir = os.path.dirname(_afile)

                            uuid = param.get_uuid()
                            name = param.get_domain()
                            created = param.get_created()
                            title = param.get_title()
                            if title != "":
                                title = re.sub("[\r\n]", "", title)
                            if title == "":
                                title = _('untitled')

                            if created != "":
                                created_str = time.strftime("%Y/%m/%d %H:%M:%S", \
                                                            time.localtime(float(created)))
                            else:
                                created_str = _("N/A")

                            files.append({
                                "dir": _dir,
                                "pool": pool_name,
                                #"b64dir" : base64_encode(_dir),
                                "uuid": uuid,
                                "name": name,
                                "created": int(created),
                                "created_str": created_str,
                                "title": title,
                                "icon": param.get_database()["icon"],
                            })

                    exports[pool_name] = files

                # .json
                if self.is_json() is True:
                    guests_json = []
                    for x in guests:
                        guests_json.append(x.get_json(self.me.languages))

                    self.view.guests = json_dumps(guests_json)
                else:
                    self.view.exports = exports
                    self.view.guests = guests

            return True
        except:
            pass
        finally:
            #self.kvc.close()
            pass  # libvirt connection scope --> Guest#_post()
Exemplo n.º 10
0
    def _GET(self, *param, **params):
        (host_id, guest_id) = self.chk_guestby1(param)
        if guest_id is None: return web.notfound()

        bridge_prefix = {
            "XEN": "xenbr",
            "KVM": "br|bondbr",
            #"KVM":"eth|bondbr",
        }

        model = findbyguest1(self.orm, guest_id)

        # virt
        self.kvc = KaresansuiVirtConnection()
        try:
            domname = self.kvc.uuid_to_domname(model.uniq_key)
            if not domname:
                return web.notfound()
            virt = self.kvc.search_kvg_guests(domname)[0]
            guest = MergeGuest(model, virt)
            self.view.guest = guest

            # Output .input
            if self.is_mode_input() is True:
                try:
                    VMType = guest.info["virt"].get_info()["VMType"].upper()
                except:
                    VMType = "KVM"

                self.view.VMType = VMType

                # Network
                phydev = []
                phydev_regex = re.compile(r"%s" % bridge_prefix[VMType])

                for dev, dev_info in get_ifconfig_info().iteritems():
                    try:
                        if phydev_regex.match(dev):
                            phydev.append(dev)
                    except:
                        pass
                if len(phydev) == 0:
                    phydev.append("%s0" % bridge_prefix[VMType])

                phydev.sort()
                self.view.phydev = phydev  # Physical device
                self.view.virnet = sorted(
                    self.kvc.list_active_network())  # Virtual device
                self.view.mac_address = generate_mac_address(
                )  # new mac address

                # Disk
                inactive_pool = []
                active_pool = self.kvc.list_active_storage_pool()
                pools = inactive_pool + active_pool
                pools.sort()

                if not pools:
                    return web.badrequest('One can not start a storage pool.')

                pools_info = {}
                pools_vols_info = {}
                pools_iscsi_blocks = {}
                already_vols = []
                guests = []

                guests += self.kvc.list_inactive_guest()
                guests += self.kvc.list_active_guest()
                for guest in guests:
                    already_vol = self.kvc.get_storage_volume_bydomain(
                        domain=guest, image_type=None, attr='path')
                    if already_vol:
                        already_vols += already_vol.keys()

                for pool in pools:
                    pool_obj = self.kvc.search_kvn_storage_pools(pool)[0]
                    if pool_obj.is_active() is True:
                        pools_info[pool] = pool_obj.get_info()

                        blocks = None
                        if pools_info[pool]['type'] == 'iscsi':
                            blocks = self.kvc.get_storage_volume_iscsi_block_bypool(
                                pool)
                            if blocks:
                                pools_iscsi_blocks[pool] = []
                        vols_obj = pool_obj.search_kvn_storage_volumes(
                            self.kvc)
                        vols_info = {}

                        for vol_obj in vols_obj:
                            vol_name = vol_obj.get_storage_volume_name()
                            vols_info[vol_name] = vol_obj.get_info()
                            if blocks:
                                if vol_name in blocks and vol_name not in already_vols:
                                    pools_iscsi_blocks[pool].append(
                                        vol_obj.get_info())

                        pools_vols_info[pool] = vols_info

                self.view.pools = pools
                self.view.pools_info = pools_info
                self.view.pools_vols_info = pools_vols_info
                self.view.pools_iscsi_blocks = pools_iscsi_blocks

                if VMType == "KVM":
                    self.view.DISK_FORMATS = DISK_QEMU_FORMAT
                else:
                    self.view.DISK_FORMATS = DISK_NON_QEMU_FORMAT

                self.view.bus_types = self.kvc.bus_types

            else:  # .part
                self.view.ifinfo = virt.get_interface_info()  # interface info
                self.view.disk_info = virt.get_disk_info()  # Disk info

        finally:
            self.kvc.close()

        return True