python类open()的实例源码

main.py 文件源码 项目:nitro 作者: KVM-VMI 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def __init__(self, vm_name, analyze_enabled, output=None):
        self.vm_name = vm_name
        self.analyze_enabled = analyze_enabled
        self.output = output
        # get domain from libvirt
        con = libvirt.open('qemu:///system')
        self.domain = con.lookupByName(vm_name)
        self.events = []
        self.nitro = None
        # define new SIGINT handler, to stop nitro
        signal.signal(signal.SIGINT, self.sigint_handler)
virt_net.py 文件源码 项目:DevOps 作者: YoLoveLife 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def __init__(self, uri, module):

        self.module = module

        conn = libvirt.open(uri)

        if not conn:
            raise Exception("hypervisor connection failure")

        self.conn = conn
virt_pool.py 文件源码 项目:DevOps 作者: YoLoveLife 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def __init__(self, uri, module):

        self.module = module

        conn = libvirt.open(uri)

        if not conn:
            raise Exception("hypervisor connection failure")

        self.conn = conn
driver.py 文件源码 项目:freezer-dr 作者: openstack 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def __init__(self, nodes, fencer_conf):
        super(LibvirtDriver, self).__init__(nodes, fencer_conf)
        self.parser = YamlParser(self.fencer_conf['credentials_file'])
        # initiate libvirt connection
        conn_name = self.fencer_conf.get('name', None)
        self.connection = libvirt.open(name=conn_name)
host.py 文件源码 项目:JimV-N 作者: jamesiter 项目源码 文件源码 阅读 35 收藏 0 点赞 0 评论 0
def init_conn(self):
        self.conn = libvirt.open()

        if self.conn is None:
            raise ConnFailed(u'?????? --> ' + sys.stderr)
libvirt_utils.py 文件源码 项目:daisy 作者: opnfv 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def create_vm(template, name=None, disks=None, physical_bridge=None):
    LI('Begin to create VM %s' % template)

    if name or disks or physical_bridge:
        tree = ET.ElementTree(file=template)
        root = tree.getroot()
        if name:
            modify_vm_name(root, name)
        if disks:
            modify_vm_disk_file(root, disks)
        if physical_bridge:
            modify_vm_bridge(root, physical_bridge)

        temp_file = path_join(WORKSPACE, 'tmp.xml')
        tree.write(temp_file)
        output = commands.getoutput('cat %s' % temp_file)
        os.remove(temp_file)
    else:
        output = commands.getoutput('cat %s' % template)

    conn = libvirt.open('qemu:///system')
    domain = conn.defineXML(output)
    if domain is None:
        err_exit('Failed to define VM %s' % template)
    if domain.create() < 0:
        err_exit('Failed to start VM %s' % template)
    domain.setAutostart(1)
    conn.close()

    LI('VM %s is started' % domain.name())
    return domain
libvirt_utils.py 文件源码 项目:daisy 作者: opnfv 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def reboot_vm(vm_name, boot_devs=None):
    LI('Begin to reboot VM %s', vm_name)
    conn = libvirt.open('qemu:///system')
    try:
        vm = conn.lookupByName(vm_name)
    except libvirt.libvirtError as e:
        LE(e)
        err_exit('VM %s is not found: ' % vm_name)

    if boot_devs:
        if vm.isActive():
            vm.destroy()
            LI('Destroy VM %s' % vm_name)

        root = ET.fromstring(vm.XMLDesc())
        LI('Modify the boot order %s' % boot_devs)
        modify_vm_boot_order(root, boot_devs)

        LI('Re-define and start the VM %s' % vm_name)
        vm.undefine()
        vm = conn.defineXML(ET.tostring(root))
        vm.create()
        vm.setAutostart(1)
    else:
        vm.reset()

    conn.close()
libvirt_utils.py 文件源码 项目:daisy 作者: opnfv 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def delete_vm_and_disk(vm_name):
    LI('Begin to delete VM %s', vm_name)
    conn = libvirt.open('qemu:///system')
    vm = None
    for item in conn.listAllDomains():
        if vm_name == item.name():
            vm = item
            break
    if vm is None:
        conn.close()
        LI('VM %s is not found' % vm_name)
        return

    output = vm.XMLDesc()
    root = ET.fromstring(output)

    if vm.isActive():
        vm.destroy()
        LI('Destroy VM %s' % vm.name())
    vm.undefine()

    for disk_file in get_disk_file(root):
        if os.path.isfile(disk_file):
            status, output = commands.getstatusoutput('rm -f %s' % disk_file)
            if status:
                LW('Failed to delete the VM disk file %s' % disk_file)

    conn.close()
    LI('VM %s is removed' % vm_name)
libvirt_utils.py 文件源码 项目:daisy 作者: opnfv 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def create_virtual_network(template):
    LI('Begin to create virtual network %s' % template)
    output = commands.getoutput('cat %s' % template)
    conn = libvirt.open('qemu:///system')
    network = conn.networkDefineXML(output)
    if network is None:
        err_exit('Failed to define a virtual network %s' % template)

    network.create()  # set the network active
    network.setAutostart(1)
    conn.close()
    LI('Virtual network %s is created' % network.name())
    return network.name()
functions.py 文件源码 项目:isard 作者: isard-vdi 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def test_hypervisor_conn(uri):
    """ test hypervisor connecton, if fail an error message in log """
    try:
        handle = libvirt.open(uri)
        return handle
    except:
        log.error(sys.exc_info()[1])
        return False
functions.py 文件源码 项目:isard 作者: isard-vdi 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def analize_backing_chains_outputs(array_out_err=[], path_to_write_json=None, path_to_read_json=None):
    if path_to_write_json is not None:
        f = open(path_to_write_json, 'w')
        json.dump(array_out_err, f)
        f.close()

    if path_to_read_json is not None:
        f = open(path_to_read_json, 'r')
        array_out_err = json.load(f)
        log.debug(len(array_out_err))
        f.close()

    domains_ok = 0
    domains_err = 0
    for d in array_out_err:
        id = d['title']
        if len(d['err']) > 0:
            domains_err += 1
            log.info(d['err'])
            update_domain_status('Failed', id, detail=d['err'])
        else:
            log.debug(id)
            domains_ok += 1
            if type(d['out']) is not str:
                out = out.decode('utf-8')
            else:
                out = d['out']
            l = json.loads(out)

            from pprint import pprint
            pprint(l)
            update_disk_backing_chain(id, 0, l[0]['filename'], l)

    return ({'ok': domains_ok, 'err': domains_err})
functions.py 文件源码 项目:isard 作者: isard-vdi 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def test_hypervisor_conn(uri):
    """ test hypervisor connecton, if fail an error message in log """
    try:
        handle = libvirt.open(uri)
        return handle
    except:
        log.error(sys.exc_info()[1])
        return False
libvirt_kvm.py 文件源码 项目:paws 作者: rhpit 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def download(link, file_dst, label):
        """Create HTTP request to download a file"""
        # delimit labels to be at same size and a better visualization
        # when printing the progres bar
        fill_out = 20 - len(label)
        extra = " " * fill_out
        pbar_label = label + extra
        LOG.debug("Starting download %s" % link)
        try:
            req = urllib2.urlopen(link)
            remote_file_size = int(req.headers.get('content-length'))
            CHUNK = 16 * 1024
            with open(file_dst, 'wb') as fp:
                with progressbar(length=remote_file_size,
                                 fill_char=style('#', fg='green'),
                                 empty_char=' ',
                                 label=pbar_label,
                                 show_percent=True) as bar:
                    while True:
                        chunk = req.read(CHUNK)
                        if not chunk: break
                        fp.write(chunk)
                        bar.update(len(chunk))

            LOG.debug("Download complete, file %s saved locally" % file_dst)
        except (HTTPError, RequestException, Exception) as ex:
            raise ex
libvirt.py 文件源码 项目:neos 作者: Aftermath 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def connect(self):
        """Connect to hypervisor and return object."""
        try:
            if self.read_only:
                conn = libvirt.openReadOnly(self.uri)
            else:
                conn = libvirt.open(self.uri)
        except:
            raise
        return conn
vm.py 文件源码 项目:webkvmmgr 作者: crazw 项目源码 文件源码 阅读 35 收藏 0 点赞 0 评论 0
def get_domain(self, uri="qemu:///system"):
        try:
            conn = libvirt.open(uri)
            dom = conn.lookupByName(self.name)
            return dom
            conn.close()
        except Exception, e:
            logger.exception(e)
            return None
vm.py 文件源码 项目:webkvmmgr 作者: crazw 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def change_cur_cpu_number(self, number, uri="qemu:///system"):
        try:
            conn = libvirt.open(uri)
            nodeinfo = conn.getInfo()
        except Exception, e:
            logger.exception(e)
            return None 
        if conn:
            conn.close()

        host_maxcpus = int(nodeinfo[2])
        if number > host_maxcpus or number <= 0:
            logger.warning("invalid cpu number")
            return None

        tree = ET.fromstring(self.xml)
        vcpu_ele = tree.find('vcpu')
        max_vcpu = vcpu_ele.text
        if max_vcpu is not None and int(max_vcpu) < number:
            vcpu_ele.text = str(number)
        vcpu_ele.set('current', str(number))

        topo_ele = tree.find('cpu/topology')
        if topo_ele:
            topo_ele.set('sockets', vcpu_ele.text)
            topo_ele.set('cores', '1')
            topo_ele.set('threads', '1')
        self.xml = ET.tostring(tree)
        return self.xml


问题


面经


文章

微信
公众号

扫码关注公众号