Python libvirt.open() Examples

The following are 30 code examples for showing how to use libvirt.open(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module libvirt , or try the search function .

Example 1
Project: InsightAgent   Author: insightfinder   File: getmetrics_kvm.py    License: Apache License 2.0 6 votes vote down vote up
def updateDataForHost(currentDataList):
    # Open connection with the Hypervisor
    libvertConn = libvirt.open('qemu:///system')
    if libvertConn == None:
        print('Failed to open connection to '
              '', file=sys.stderr)
        exit(1)
    currentTimeStamp = str(int(time.time() * 1000))
    CPUDataMap = libvertConn.getCPUMap()
    mem = libvertConn.getFreeMemory()
    stats = libvertConn.getCPUStats(-1)
    currentDataList.append(currentTimeStamp)
    currentDataList.append(str(round(mem / 1048576., 2)))
    currentDataList.append(str(CPUDataMap[0]))
    currentDataList.append(str(stats['kernel'] / 10 ** 9))
    currentDataList.append(str(stats['idle'] / 10 ** 9))
    currentDataList.append(str(stats['user'] / 10 ** 9))
    currentDataList.append(str(stats['iowait'] / 10 ** 9))
    libvertConn.close() 
Example 2
Project: InsightAgent   Author: insightfinder   File: getmetrics_kvm.py    License: Apache License 2.0 6 votes vote down vote up
def getVMDomains():
    # Open connection with the Hypervisor
    libvertConn = libvirt.open('qemu:///system')
    if libvertConn == None:
        print('Failed to open connection to '
              '', file=sys.stderr)
        exit(1)
    # Get the information about the various guest VMs
    vmIdentities = libvertConn.listDomainsID()
    vmDomains = []
    if len(vmIdentities) == 1:
        vmDomains.append(libvertConn.lookupByID(vmIdentities[0]))
        if len(vmDomains) == 0:
            print('Failed to find the domain ', file=sys.stderr)
            exit(1)
    else:
        # Handle for multiple domains
        for id in vmIdentities:
            vmDomains.append(libvertConn.lookupByID(id))
    return vmDomains 
Example 3
Project: InsightAgent   Author: insightfinder   File: getmetrics_kvm.py    License: Apache License 2.0 6 votes vote down vote up
def checkNewVMs(vmDomains):
    newVMNames = []
    vmMetaDataFilePath = os.path.join(homePath, dataDirectory + "totalVMs.json")
    for vmDomain in vmDomains:
        newVMNames.append(vmDomain.name())
    if os.path.isfile(vmMetaDataFilePath) == False:
        towritePreviousVM = {}
        towritePreviousVM["allVM"] = newVMNames
        with open(vmMetaDataFilePath, 'w') as vmMetaDataFile:
            json.dump(towritePreviousVM, vmMetaDataFile)
    else:
        with open(vmMetaDataFilePath, 'r') as vmMetaDataFile:
            oldVMDomains = json.load(vmMetaDataFile)["allVM"]
        if cmp(newVMNames, oldVMDomains) != 0:
            towritePreviousVM = {}
            towritePreviousVM["allVM"] = newVMNames
            with open(vmMetaDataFilePath, 'w') as vmMetaDataFile:
                json.dump(towritePreviousVM, vmMetaDataFile)
            if os.path.isfile(os.path.join(homePath, dataDirectory + date + ".csv")) == True:
                oldFile = os.path.join(homePath, dataDirectory + date + ".csv")
                newFile = os.path.join(homePath, dataDirectory + date + "." + time.strftime("%Y%m%d%H%M%S") + ".csv")
                os.rename(oldFile, newFile) 
Example 4
Project: qubes-core-admin   Author: QubesOS   File: app.py    License: GNU Lesser General Public License v2.1 6 votes vote down vote up
def cpu_family_model(self):
        """Get CPU family and model"""
        if self._cpu_family is None or self._cpu_model is None:
            family = None
            model = None
            with open('/proc/cpuinfo') as cpuinfo:
                for line in cpuinfo.readlines():
                    line = line.strip()
                    if not line:
                        # take info from the first core
                        break
                    field, value = line.split(':', 1)
                    if field.strip() == 'model':
                        model = int(value.strip())
                    elif field.strip() == 'cpu family':
                        family = int(value.strip())
            self._cpu_family = family
            self._cpu_model = model
        return self._cpu_family, self._cpu_model 
Example 5
Project: see   Author: F-Secure   File: libvirt_pool.py    License: Apache License 2.0 6 votes vote down vote up
def image(self):
        path = "%s/%s" % (self.configuration.get(
            'storage_pool_path').rstrip('/'), self.name.lstrip('/'))

        if not os.path.exists(path):
            raise FileNotFoundError(path)

        hypervisor = libvirt.open(
            self.configuration.get('hypervisor', 'qemu:///system'))

        try:
            volume = hypervisor.storageVolLookupByPath(path)
            return volume.path()
        except libvirt.libvirtError:
            pool = hypervisor.storagePoolDefineXML(POOL_CONFIG_XML.format(
                self.configuration.get('storage_pool_path')))
            pool.setAutostart(True)
            pool.create()
            pool.refresh()
            return pool.storageVolLookupByName(self.name).path() 
Example 6
Project: see   Author: F-Secure   File: lxc.py    License: Apache License 2.0 6 votes vote down vote up
def domain_create(hypervisor, identifier, configuration, network_name=None):
    """libvirt Domain definition.

    @raise: ConfigError, IOError, libvirt.libvirtError.

    """
    mounts = []

    with open(configuration['configuration']) as config_file:
        domain_config = config_file.read()

    if 'filesystem' in configuration:
        if isinstance(configuration['filesystem'], (list, tuple)):
            for mount in configuration['filesystem']:
                mounts.append(mountpoint(mount, identifier))
        else:
            mounts.append(mountpoint(configuration['filesystem'], identifier))

    xml_config = domain_xml(identifier, domain_config, tuple(mounts), network_name=network_name)

    return hypervisor.defineXML(xml_config) 
Example 7
Project: see   Author: F-Secure   File: lxc.py    License: Apache License 2.0 6 votes vote down vote up
def allocate(self):
        """Initializes libvirt resources."""
        network_name = None

        self._hypervisor = libvirt.open(
            self.configuration.get('hypervisor', 'lxc:///'))

        if 'network' in self.configuration:
            self._network = network.create(self._hypervisor, self.identifier,
                                           self.configuration['network'])
            network_name = self._network.name()

        self._domain = domain_create(self._hypervisor, self.identifier,
                                     self.configuration['domain'],
                                     network_name=network_name)
        if self._network is None:
            self._network = network.lookup(self._domain) 
Example 8
Project: virtualbmc   Author: openstack   File: utils.py    License: Apache License 2.0 6 votes vote down vote up
def __enter__(self):
        try:
            if self.sasl_username and self.sasl_password:

                def request_cred(credentials, user_data):
                    for credential in credentials:
                        if credential[0] == libvirt.VIR_CRED_AUTHNAME:
                            credential[4] = self.sasl_username
                        elif credential[0] == libvirt.VIR_CRED_PASSPHRASE:
                            credential[4] = self.sasl_password
                    return 0

                auth = [[libvirt.VIR_CRED_AUTHNAME,
                         libvirt.VIR_CRED_PASSPHRASE], request_cred, None]
                flags = libvirt.VIR_CONNECT_RO if self.readonly else 0
                self.conn = libvirt.openAuth(self.uri, auth, flags)
            elif self.readonly:
                self.conn = libvirt.openReadOnly(self.uri)
            else:
                self.conn = libvirt.open(self.uri)

            return self.conn

        except libvirt.libvirtError as e:
            raise exception.LibvirtConnectionOpenError(uri=self.uri, error=e) 
Example 9
Project: virtualbmc   Author: openstack   File: utils.py    License: Apache License 2.0 6 votes vote down vote up
def __enter__(self):
        pid = self._fork(parent_exits=False)
        if pid > 0:
            return pid

        os.setsid()

        self._fork(parent_exits=True)

        self._change_root_directory()
        self._change_file_creation_mask()

        sys.stdout.flush()
        sys.stderr.flush()

        si = open(os.devnull, 'r')
        so = open(os.devnull, 'a+')
        se = open(os.devnull, 'a+')

        os.dup2(si.fileno(), sys.stdin.fileno())
        os.dup2(so.fileno(), sys.stdout.fileno())
        os.dup2(se.fileno(), sys.stderr.fileno())

        return pid 
Example 10
Project: dwarf   Author: juergh   File: virt.py    License: Apache License 2.0 6 votes vote down vote up
def _create_net_xml():
    """
    Create a libvirt XML for the network bridge
    """
    with open(os.path.join(os.path.dirname(__file__),
                           'libvirt-net.xml'), 'r') as fh:
        xml_template = fh.read()

    xml_info = {
        'uuid': str(uuid.uuid4()),
        'network_name': CONF.libvirt_network_name,
        'bridge': CONF.libvirt_bridge_name,
        'ip': CONF.libvirt_bridge_ip,
        'dhcp_start': '.'.join(CONF.libvirt_bridge_ip.split('.')[0:3] + ['2']),
        'dhcp_end': '.'.join(CONF.libvirt_bridge_ip.split('.')[0:3] + ['254']),
    }

    return Template(xml_template).substitute(xml_info) 
Example 11
Project: bubble-toolkit   Author: MissionCriticalCloud   File: kvm_local_deploy_v2.py    License: Apache License 2.0 6 votes vote down vote up
def __init__(self, debug=0, dryrun=0, force=0, marvin_config=''):
        self.config_section_name = None
        self.DEBUG = debug
        self.DRYRUN = dryrun
        self.FORCE = force
        self.marvin_config = marvin_config
        self.marvin_data = False
        # we can run as a user in the libvirt group
        # self.check_root()
        self.configfile = os.path.dirname(os.path.realpath(__file__)) + '/config'
        self.config_data = { }
        try:
            self.conn = libvirt.open('qemu:///system')
        except Exception as e:
            print("ERROR: Could not connect to Qemu!")
            print(e)
            sys.exit(1)

        self.print_welcome()
        self.read_config_file(self.configfile)

    # Check for root permissions 
Example 12
Project: bubble-toolkit   Author: MissionCriticalCloud   File: kvm_local_deploy_v2.py    License: Apache License 2.0 6 votes vote down vote up
def load_marvin_json(self):
        try:
            print("Note: Processing Marvin config '" + self.marvin_config + "'")
            config_lines = []
            with open(self.marvin_config) as file_pointer:
                for line in file_pointer:
                    ws = line.strip()
                    if not ws.startswith("#"):
                        config_lines.append(ws)
            self.marvin_data = json.loads("\n".join(config_lines))
            return True
        except:
            print("Error: loading Marvin failed")
            return False

    # Get Marvin json 
Example 13
Project: bubble-toolkit   Author: MissionCriticalCloud   File: kvm_local_deploy.py    License: Apache License 2.0 6 votes vote down vote up
def __init__(self, debug=0, dryrun=0, force=0, marvin_config=''):
        self.config_section_name = None
        self.DEBUG = debug
        self.DRYRUN = dryrun
        self.FORCE = force
        self.marvin_config = marvin_config
        self.marvin_data = False
        # we can run as a user in the libvirt group
        # self.check_root()
        self.configfile = os.path.dirname(os.path.realpath(__file__)) + '/config'
        self.config_data = { }
        try:
            self.conn = libvirt.open('qemu:///system')
        except Exception as e:
            print("ERROR: Could not connect to Qemu!")
            print(e)
            sys.exit(1)

        self.print_welcome()
        self.read_config_file(self.configfile)

    # Check for root permissions 
Example 14
Project: libvirt-test-API   Author: libvirt   File: create_xml_with_files.py    License: GNU General Public License v2.0 6 votes vote down vote up
def create_xml_with_files_clean(params):
    logger = params['logger']
    guestname = params['guestname']

    for i in range(len(files)):
        ret = utils.del_file("/tmp/libvirt-test-api-create-file-%d" % i, logger)
    ret = utils.del_file("/tmp/libvirt_passfile_check", logger)

    conn = libvirt.open("lxc:///")
    dom = conn.lookupByName(guestname)
    guest_state = dom.info()[0]
    if guest_state == libvirt.VIR_DOMAIN_RUNNING:
        logger.debug("destroy guest: %s." % guestname)
        time.sleep(5)
        dom.destroyFlags()
        define_list = conn.listDefinedDomains()
        if guestname in define_list:
            time.sleep(3)
            dom.undefine()
            time.sleep(3)
    elif guest_state == libvirt.VIR_DOMAIN_SHUTOFF:
        time.sleep(5)
        dom.undefine()
        time.sleep(3) 
Example 15
Project: libvirt-test-API   Author: libvirt   File: create_with_files.py    License: GNU General Public License v2.0 6 votes vote down vote up
def create_with_files_clean(params):
    logger = params['logger']
    for i in range(len(files)):
        ret = utils.del_file("/tmp/libvirt-test-api-create-file-%d" % i, logger)
    ret = utils.del_file("/tmp/libvirt_passfile_check", logger)

    conn = libvirt.open("lxc:///")
    dom = conn.lookupByName(guestname)
    guest_state = dom.info()[0]
    if guest_state == libvirt.VIR_DOMAIN_RUNNING:
        logger.debug("destroy guest: %s." % guestname)
        time.sleep(5)
        dom.destroyFlags()
        define_list = conn.listDefinedDomains()
        if guestname in define_list:
            time.sleep(3)
            dom.undefine()
            time.sleep(3)
    elif guest_state == libvirt.VIR_DOMAIN_SHUTOFF:
        time.sleep(5)
        dom.undefine()
        time.sleep(3) 
Example 16
Project: libvirt-test-API   Author: libvirt   File: checkpoint_lookup.py    License: GNU General Public License v2.0 6 votes vote down vote up
def checkpoint_lookup(params):
    logger = params['logger']
    guestname = params['guestname']
    checkpoint_name = params.get('checkpoint_name', None)

    logger.info("Checkpoint name: %s" % checkpoint_name)
    if not utils.version_compare('libvirt-python', 5, 6, 0, logger):
        logger.info("Current libvirt-python don't support checkpointLookupByName().")
        return 0

    try:
        conn = libvirt.open()
        dom = conn.lookupByName(guestname)
        cp = dom.checkpointLookupByName(checkpoint_name, 0)
    except libvirtError as err:
        logger.error("API error message: %s" % err.get_error_message())
        return 1

    # check checkpoint
    if cp.getName() == checkpoint_name:
        logger.info("PASS: check checkpoint name successful.")
        return 0
    else:
        logger.error("FAIL: check checkpoint name failed.")
        return 1 
Example 17
Project: libvirt-test-API   Author: libvirt   File: list_all_children.py    License: GNU General Public License v2.0 6 votes vote down vote up
def list_all_children(params):
    logger = params['logger']
    guestname = params['guestname']
    checkpoint_name = params['checkpoint_name']
    flag = utils.parse_flags(params)

    if not utils.version_compare('libvirt-python', 5, 6, 0, logger):
        logger.info("Current libvirt-python don't support listAllChildren().")
        return 0

    logger.info("Checkpoint name: %s" % checkpoint_name)
    logger.info("flag: %s" % flag)

    try:
        conn = libvirt.open()
        dom = conn.lookupByName(guestname)
        cp = dom.checkpointLookupByName(checkpoint_name)
        cp_lists = cp.listAllChildren(flag)
        for cp_list in cp_lists:
            logger.info("Checkpoint children list: %s" % cp_list.getName())
    except libvirtError as err:
        logger.error("API error message: %s" % err.get_error_message())
        return 1

    return 0 
Example 18
Project: libvirt-test-API   Author: libvirt   File: install_suse_ppc.py    License: GNU General Public License v2.0 6 votes vote down vote up
def install_suse_ppc_clean(params):
    """ clean testing environment """
    logger = params['logger']
    guestname = params.get('guestname')

    diskpath = params.get('diskpath', '/var/lib/libvirt/images/libvirt-test-api')
    conn = libvirt.open()
    domain_common.guest_clean(conn, guestname, logger)

    if os.path.exists(diskpath + '/' + guestname):
        os.remove(diskpath + '/' + guestname)

    envfile = os.path.join(HOME_PATH, 'global.cfg')
    envparser = env_parser.Envparser(envfile)
    ostree_search = params.get('guestos') + "_" + params.get('guestarch') + "_iso"
    ostree = envparser.get_value("guest", ostree_search)
    cache_folder = envparser.get_value("variables", "domain_cache_folder") + "/" +\
        ostree.split("/")[-1].split(".iso")[0]
    if os.path.exists(cache_folder):
        shutil.rmtree(cache_folder)

    guest_dir = os.path.join(HOME_PATH, guestname)
    if os.path.exists(guest_dir):
        shutil.rmtree(guest_dir) 
Example 19
Project: libvirt-test-API   Author: libvirt   File: install_common.py    License: GNU General Public License v2.0 6 votes vote down vote up
def clean_guest(guestname, logger):
    conn = libvirt.open(None)
    running_guests = []
    ids = conn.listDomainsID()
    for id in ids:
        obj = conn.lookupByID(id)
        running_guests.append(obj.name())

    if guestname in running_guests:
        logger.info("Destroy guest: %s" % guestname)
        domobj = conn.lookupByName(guestname)
        domobj.destroy()

    define_guests = conn.listDefinedDomains()
    if guestname in define_guests:
        logger.info("Undefine guest: %s" % guestname)
        domobj = conn.lookupByName(guestname)
        domobj.undefine()
    conn.close() 
Example 20
Project: libvirt-test-API   Author: libvirt   File: install_linux_cdrom.py    License: GNU General Public License v2.0 6 votes vote down vote up
def install_linux_cdrom_clean(params):
    """ clean testing environment """
    logger = params['logger']
    guestname = params.get('guestname')

    diskpath = params.get('diskpath', '/var/lib/libvirt/images/libvirt-test-api')
    conn = libvirt.open()
    domain_common.guest_clean(conn, guestname, logger)
    if os.path.exists(diskpath):
        os.remove(diskpath)
    envfile = os.path.join(HOME_PATH, 'usr/share/libvirt-test-api/config', 'global.cfg')
    envparser = env_parser.Envparser(envfile)
    cache_folder = envparser.get_value("variables", "domain_cache_folder")

    if os.path.exists(cache_folder + '/' + guestname + "_folder"):
        shutil.rmtree(cache_folder + '/' + guestname + "_folder")

    guest_dir = os.path.join(HOME_PATH, guestname)
    if os.path.exists(guest_dir):
        shutil.rmtree(guest_dir) 
Example 21
def connection_getMemoryParameters(params):
    """
       test API for getMemoryParameters in class virConnect
    """
    logger = params['logger']
    uri = params.get("uri", None)
    fail = 0

    try:
        conn = libvirt.open(uri)
        logger.info("get connection to libvirtd")
        param_dict = conn.getMemoryParameters()

        for n in node_memory:
            fail = check_memory_parameter(param_dict, n, logger)

    except libvirtError as e:
        logger.error("API error message: %s" % e.get_error_message())
        fail = 1
    return fail 
Example 22
def connection_security_model(params):
    """test API for getSecurityModel"""

    logger = params['logger']
    uri = params.get("uri", None)

    if 'uri' in params:
        conn = libvirt.open(uri)
    else:
        conn = sharedmod.libvirtobj['conn']

    try:
        model = conn.getSecurityModel()
        logger.info("model : %s" % model)
        driver = get_security_driver(logger)
        if driver == model[0]:
            logger.info("Pass : get security model successful.")
            return 0
        else:
            logger.error("Fail : get security model failed.")
            return 1

    except libvirtError as e:
        logger.error("API error message: %s" % e.get_error_message())
        return 1 
Example 23
def connection_cpu_features(params):
    """test libvirt connection functions related to cpu features
    """
    logger = params['logger']

    try:
        if 'conn' in params:
            conn = libvirt.open(params['conn'])
        else:
            conn = libvirt.open()
        host_cpu = get_host_cpu(conn)
        logger.debug("Host cpu xml: " + str(host_cpu))
        logger.info("Host cpu features: " + str(get_cpu_feature_set(host_cpu)))
        if baseline_test(conn, host_cpu, logger):
            return 1
        if compare_test(conn, host_cpu, logger):
            return 1

    except libvirtError as e:
        logger.error("API error message: %s, error code is %s" %
                     (e.get_error_message(), e.get_error_code()))
        logger.error("start failed")
        return 1

    return 0 
Example 24
def prepare_shutoff_daemon(logger):
    if not os.path.exists(hooks_dir):
        os.makedirs(hooks_dir)
    if os.path.exists(hooks_file):
        os.remove(hooks_file)
    with open(hooks_file, 'w') as f:
        f.write(hooks_str)
    if not os.access(hooks_file, os.X_OK):
        st = os.stat(hooks_file)
        os.chmod(hooks_file, st.st_mode | stat.S_IEXEC | stat.S_IXGRP | stat.S_IXOTH)

    cmd = "service libvirtd restart"
    ret, out = utils.exec_cmd(cmd, shell=True)
    if ret:
        logger.error("Restart libvirtd failed: %s" % out)
        return 1
    return 0 
Example 25
Project: drydock   Author: airshipit   File: oob.py    License: Apache License 2.0 5 votes vote down vote up
def init_session(self, node):
        """Initialize a Libvirt session to the node hypervisor.

        :param node: instance of objects.BaremetalNode
        """
        if node.oob_type != 'libvirt':
            raise errors.DriverError(
                "Node OOB type %s is not 'libvirt'" % node.oob_type)

        virsh_url = node.oob_parameters.get('libvirt_uri', None)

        if not virsh_url:
            raise errors.DriverError(
                "Node %s has no 'libvirt_url' defined" % (node.name))

        url_parts = urlparse(virsh_url)

        if url_parts.scheme != "qemu+ssh":
            raise errors.DriverError(
                "Node %s has invalid libvirt URL scheme %s. "
                "Only 'qemu+ssh' supported." % (node.name, url_parts.scheme))

        self.logger.debug(
            "Starting libvirt session to hypervisor %s " % (virsh_url))
        virsh_ses = libvirt.open(virsh_url)

        if not virsh_ses:
            raise errors.DriverError(
                "Unable to establish libvirt session to %s." % virsh_url)

        return virsh_ses 
Example 26
Project: InsightAgent   Author: insightfinder   File: getmetrics_kvm.py    License: Apache License 2.0 5 votes vote down vote up
def writeDataToFile(headerList, currentDataList):
    # write data to csv file
    with open(os.path.join(homePath, dataDirectory, date + ".csv"), 'a+') as csvDataFile:
        dataFileLines = len(csvDataFile.readlines())
        if (dataFileLines < 1):
            csvDataFile.write(listToCSVRow(headerList))
            csvDataFile.write("\n")
        csvDataFile.write(listToCSVRow(currentDataList))
        csvDataFile.write("\n")
        csvDataFile.close() 
Example 27
Project: openmano   Author: nfvlabs   File: host_thread.py    License: Apache License 2.0 5 votes vote down vote up
def restore_iface(self, name, mac, lib_conn=None):
        ''' make an ifdown, ifup to restore default parameter of na interface
            Params:
                mac: mac address of the interface
                lib_conn: connection to the libvirt, if None a new connection is created
            Return 0,None if ok, -1,text if fails
        ''' 
        conn=None
        ret = 0
        error_text=None
        if self.test:
            print self.name, ": restore_iface '%s' %s" % (name, mac)
            return 0, None
        try:
            if not lib_conn:
                conn = libvirt.open("qemu+ssh://"+self.user+"@"+self.host+"/system")
            else:
                conn = lib_conn
                
            #wait to the pending VM deletion
            #TODO.Revise  self.server_forceoff(True)

            iface = conn.interfaceLookupByMACString(mac)
            iface.destroy()
            iface.create()
            print self.name, ": restore_iface '%s' %s" % (name, mac)
        except libvirt.libvirtError as e:   
            error_text = e.get_error_message()
            print self.name, ": restore_iface '%s' '%s' libvirt exception: %s" %(name, mac, error_text) 
            ret=-1
        finally:
            if lib_conn is None and conn is not None:
                conn.close
        return ret, error_text 
Example 28
Project: qubes-core-admin   Author: QubesOS   File: app.py    License: GNU Lesser General Public License v2.1 5 votes vote down vote up
def __init__(self, uri, reconnect_cb=None):
        self._conn = libvirt.open(uri)
        self._reconnect_cb = reconnect_cb 
Example 29
Project: qubes-core-admin   Author: QubesOS   File: app.py    License: GNU Lesser General Public License v2.1 5 votes vote down vote up
def _reconnect_if_dead(self):
        is_dead = not self._conn.isAlive()
        if is_dead:
            uri = self._conn.getURI()
            old_conn = self._conn
            self._conn = libvirt.open(uri)
            if callable(self._reconnect_cb):
                self._reconnect_cb(old_conn)
            old_conn.close()
        return is_dead 
Example 30
Project: qubes-core-admin   Author: QubesOS   File: app.py    License: GNU Lesser General Public License v2.1 5 votes vote down vote up
def _acquire_lock(self, for_save=False):
        assert self.__locked_fh is None, 'double lock'

        while True:
            try:
                fd = os.open(self._store,
                             os.O_RDWR | (os.O_CREAT * int(for_save)))
            except FileNotFoundError:
                if not for_save:
                    raise qubes.exc.QubesException(
                        'Qubes XML store {!r} is missing; '
                        'use qubes-create tool'.format(self._store))
                raise

            # While we were waiting for lock, someone could have unlink()ed
            # (or rename()d) our file out of the filesystem. We have to
            # ensure we got lock on something linked to filesystem.
            # If not, try again.
            if os.fstat(fd) != os.stat(self._store):
                os.close(fd)
                continue

            if self.__load_timestamp and \
                    os.path.getmtime(self._store) != self.__load_timestamp:
                os.close(fd)
                raise qubes.exc.QubesException(
                    'Someone else modified qubes.xml in the meantime')

            break

        if os.name == 'posix':
            fcntl.lockf(fd, fcntl.LOCK_EX)
        elif os.name == 'nt':
            # pylint: disable=protected-access
            overlapped = pywintypes.OVERLAPPED()
            win32file.LockFileEx(
                win32file._get_osfhandle(fd),
                win32con.LOCKFILE_EXCLUSIVE_LOCK, 0, -0x10000, overlapped)

        self.__locked_fh = os.fdopen(fd, 'r+b')
        return self.__locked_fh