Ejemplo n.º 1
0
    def load(self, dbg, uri):
        log.debug("%s: xcpng.libsbd.meta.MetaDBOpeations.load: uri: %s" %
                  (dbg, uri))

        cluster = ceph_cluster(dbg, get_cluster_name_by_uri(dbg, uri))

        try:
            cluster.connect()
            length = unpack(
                '!I',
                rbd_read(dbg, cluster, get_sr_name_by_uri(dbg, uri),
                         '__meta__', 0, 4))[0]
            data = unpack(
                '!%ss' % length,
                rbd_read(dbg, cluster, get_sr_name_by_uri(dbg, uri),
                         '__meta__', 4, length))[0]
            return data
        except Exception as e:
            log.error(
                "%s: xcpng.librbd.meta.MetaDBOpeations.load: Failed to load MetaDB: uri: %s"
                % (dbg, uri))
            log.error(traceback.format_exc())
            raise Exception(e)
        finally:
            cluster.shutdown()
Ejemplo n.º 2
0
    def lock(self, dbg, uri, timeout=10):
        log.debug("%s: xcpng.libsbd.meta.MetaDBOpeations.lock: uri: %s" % (dbg, uri))

        if self.lh is not None:
            raise Exception("MetaDB has been already locked by another client")

        start_time = time()
        self.lh = [None, None, None]
        self.lh[0] = ceph_cluster(dbg, get_cluster_name_by_uri(dbg, uri))

        try:
            while True:
                try:
                    self.lh[0].connect()
                    self.lh[1], self.lh[2] = rbd_lock(dbg,
                                                      self.lh[0],
                                                      get_sr_name_by_uri(dbg, uri),
                                                      '__lock__')
                    break
                except Exception as e:
                    if time() - start_time >= timeout:
                        log.debug("%s: xcpng.libsbd.meta.MetaDBOpeations.lock: Failed to lock MetaDB: uri: %s" % (dbg, uri))
                        raise Exception(e)
                    pass
        except Exception as e:
            log.debug("%s: xcpng.librbd.meta.MetaDBOpeations.lock: Failed to lock MetaDB: uri: %s"
                      % (dbg, uri))
            self.lh[0].shutdown()
            raise Exception(e)
Ejemplo n.º 3
0
    def lock(self, dbg, uri, timeout=10):
        log.debug("%s: xcpng.librbd.meta.MetaDBOpeations.lock: uri: %s timeout: %s" % (dbg, uri, timeout))

        sr_uuid = get_sr_uuid_by_uri(dbg, uri)
        vdi_uuid = get_vdi_uuid_by_uri(dbg, uri)
        pool_name = get_sr_name_by_uri(dbg, uri)

        if vdi_uuid is not None:
            lock_uuid = vdi_uuid
            image_name = get_vdi_name_by_uri(dbg, uri)
        else:
            lock_uuid = sr_uuid
            image_name = '__lock__'

        start_time = time()

        lh = [None, None, None]
        lh[0] = ceph_cluster(dbg, get_cluster_name_by_uri(dbg, uri))

        if is_locked(dbg, lh[0], pool_name, '__lock__'):
            # SR is locked
            raise ImageBusy

        try:
            while True:
                try:
                    if lock_uuid in self.__lhs:
                        raise ImageExists

                    lh[0].connect()
                    lh[1], lh[2] = rbd_lock(dbg,
                                            lh[0],
                                            pool_name,
                                            image_name)
                    self.__lhs[lock_uuid] = lh
                    break
                except Exception as e:
                    if time() - start_time >= timeout:
                        log.error("%s: xcpng.librbd.meta.MetaDBOpeations.lock: Failed to lock: uri: %s" % (dbg, uri))
                        raise Exception(e)
                    sleep(1)
                    pass
        except Exception as e:
            log.error("%s: xcpng.librbd.meta.MetaDBOpeations.lock: Failed to lock: uri: %s"
                      % (dbg, uri))
            log.error(traceback.format_exc())
            lh[0].shutdown()
            raise Exception(e)
Ejemplo n.º 4
0
    def destroy(self, dbg, uri):
        log.debug("%s: xcpng.librbd.meta.MetaDBOpeations.destroy: uri: %s" % (dbg, uri))

        cluster = ceph_cluster(dbg, get_cluster_name_by_uri(dbg, uri))

        try:
            cluster.connect()
            rbd_remove(dbg, cluster, get_sr_name_by_uri(dbg, uri), '__meta__')
            rbd_remove(dbg, cluster, get_sr_name_by_uri(dbg, uri), '__lock__')

        except Exception as e:
            log.debug("%s: xcpng.librbd.meta.MetaDBOpeations.destroy: Failed to destroy MetaDB: uri: %s"
                      % (dbg, uri))
            raise Exception(e)
        finally:
            cluster.shutdown()
Ejemplo n.º 5
0
    def destroy(self, dbg, uri):
        log.debug("%s: xcpng.librbd.sr.SROperations.destroy: uri: %s" %
                  (dbg, uri))

        cluster = ceph_cluster(dbg, get_cluster_name_by_uri(dbg, uri))

        try:
            cluster.connect()
            cluster.delete_pool(get_sr_name_by_uri(dbg, uri))
        except Exception as e:
            log.debug(
                "%s: xcpng.librbd.sr.SROperations.destory: Failed to destroy SR: uri: %s"
                % dbg, uri)
            raise Exception(e)
        finally:
            cluster.shutdown()
Ejemplo n.º 6
0
    def create(self, dbg, uri, db, size=8388608):
        log.debug("%s: xcpng.librbd.meta.MetaDBOpeations.create: uri: %s" % (dbg, uri))

        cluster = ceph_cluster(dbg, get_cluster_name_by_uri(dbg, uri))

        try:
            cluster.connect()
            rbd_create(dbg, cluster, get_sr_name_by_uri(dbg, uri), '__meta__', size)  # default size = 8388608 = 8Mb
            rbd_create(dbg, cluster, get_sr_name_by_uri(dbg, uri), '__lock__', 0)
            length = len(db)
            rbd_write(dbg, cluster, get_sr_name_by_uri(dbg, uri), '__meta__', pack("!I%ss" % length, length, db), 0, length+4)
        except Exception as e:
            log.error("%s: xcpng.librbd.meta.MetaDBOpeations.create: Failed to create MetaDB: uri: %s"
                      % (dbg, uri))
            raise Exception(e)
        finally:
            cluster.shutdown()
Ejemplo n.º 7
0
    def get_size(self, dbg, uri):
        log.debug("%s: xcpng.librbd.sr.SROperations.sr_size: uri: %s" %
                  (dbg, uri))

        cluster = ceph_cluster(dbg, get_cluster_name_by_uri(dbg, uri))

        try:
            cluster.connect()
            stats = cluster.get_cluster_stats()
            return stats['kb'] * 1024
        except Exception as e:
            log.debug(
                "%s: xcpng.librbd.sr.SROperations.get_size: uri: Failed to get size: uri: %s"
                % dbg, uri)
            raise Exception(e)
        finally:
            cluster.shutdown()
Ejemplo n.º 8
0
    def get_free_space(self, dbg, uri):
        log.debug("%s: xcpng.librbd.sr.SROperations.get_free_space: uri: %s" %
                  (dbg, uri))

        cluster = ceph_cluster(dbg, get_cluster_name_by_uri(dbg, uri))

        try:
            cluster.connect()
            stats = cluster.get_cluster_stats()
            return stats['kb_avail'] * 1024
        except Exception as e:
            log.debug(
                "%s: xcpng.librbd.sr.SROperations.get_free_space: uri: Failed to get free space: uri: %s"
                % dbg, uri)
            log.error(traceback.format_exc())
            raise Exception(e)
        finally:
            cluster.shutdown()
Ejemplo n.º 9
0
    def get_sr_list(self, dbg, uri, configuration):
        log.debug(
            "%s: xcpng.librbd.sr.SROperations.get_sr_list: uri: %s configuration %s"
            % (dbg, uri, configuration))

        srs = []
        uris = []

        cluster_in_uri = get_cluster_name_by_uri(dbg, uri)

        log.debug("%s: xcpng.librbd.sr.SROperations.get_sr_list: uris: %s" %
                  (dbg, uris))

        if cluster_in_uri == '':
            for cluster in get_config_files_list(dbg):
                uris.append("%s/%s" % (uri[:-1], cluster))
        else:
            uris = [uri]

        log.debug("%s: xcpng.librbd.sr.SROperations.get_sr_list: uris: %s" %
                  (dbg, uris))

        for _uri_ in uris:
            cluster_name = get_cluster_name_by_uri(dbg, _uri_)
            cluster = ceph_cluster(dbg, cluster_name)
            try:
                cluster.connect()
                for pool in pool_list(dbg, cluster):
                    if pool.startswith(
                            "%s%s" %
                        (get_sr_type_by_uri(dbg, uri), POOL_PREFIX)):
                        srs.append("%s/%s" %
                                   (_uri_, get_sr_uuid_by_name(dbg, pool)))
            except Exception as e:
                log.debug(
                    "%s: xcpng.librbd.sr.SROperations.get_sr_list: uri: Failed to get SRs list: uri: %s"
                    % dbg, uri)
                log.error(traceback.format_exc())
                raise Exception(e)
            finally:
                cluster.shutdown()
        log.debug("%s: xcpng.librbd.sr.SROperations.get_sr_list: srs: %s" %
                  (dbg, srs))
        return srs
Ejemplo n.º 10
0
    def dump(self, dbg, uri, json):
        log.debug("%s: xcpng.libsbd.meta.MetaDBOpeations.dump: uri: %s" %
                  (dbg, uri))

        cluster = ceph_cluster(dbg, get_cluster_name_by_uri(dbg, uri))

        try:
            length = len(json)
            cluster.connect()
            rbd_write(dbg, cluster, get_sr_name_by_uri(dbg, uri), '__meta__',
                      pack("!I%ss" % length, length, json), 0, length + 4)
        except Exception as e:
            log.error(
                "%s: xcpng.librbd.meta.MetaDBOpeations.dump: Failed to dump MetaDB: uri: %s"
                % (dbg, uri))
            log.error(traceback.format_exc())
            raise Exception(e)
        finally:
            cluster.shutdown()
Ejemplo n.º 11
0
    def destroy(self, dbg, uri):
        log.debug("%s: xcpng.librbd.volume.VolumeOperations.destroy: uri: %s" %
                  (dbg, uri))

        volume_meta = self.MetadataHandler.get_vdi_meta(dbg, uri)
        cluster = ceph_cluster(dbg, get_cluster_name_by_uri(dbg, uri))

        try:
            cluster.connect()
            rbd_remove(
                dbg, cluster, get_sr_name_by_uri(dbg, uri),
                "%s%s" % (VDI_PREFIXES[get_vdi_type_by_uri(
                    dbg, uri)], volume_meta[IMAGE_UUID_TAG]))
        except Exception as e:
            log.debug(
                "%s: xcpng.librbd.volume.VolumeOperations.create: Failed to create volume: uri: %s"
                % dbg, uri)
            raise Exception(e)
        finally:
            cluster.shutdown()
Ejemplo n.º 12
0
    def get_vdi_list(self, dbg, uri):
        log.debug("%s: xcpng.librbd.sr.SROperations.get_vdi_list: uri: %s" %
                  (dbg, uri))

        rbds = []
        cluster = ceph_cluster(dbg, get_cluster_name_by_uri(dbg, uri))

        try:
            cluster.connect()
            for rbd in rbd_list(dbg, cluster, get_sr_name_by_uri(dbg, uri)):
                if rbd.startswith(VDI_PREFIXES[get_vdi_type_by_uri(dbg, uri)]):
                    rbds.append(rbd)
            return rbds
        except Exception as e:
            log.debug(
                "%s: xcpng.librbd.sr.SROperations.get_vdi_list: uri: Failed to get VDIs list: uri: %s"
                % dbg, uri)
            raise Exception(e)
        finally:
            cluster.shutdown()
Ejemplo n.º 13
0
    def get_phisical_utilization(self, dbg, uri):
        log.debug(
            "%s: xcpng.librbd.volume.VolumeOperations.get_phisical_utilization: uri: %s"
            % (dbg, uri))

        volume_meta = self.MetadataHandler.get_vdi_meta(dbg, uri)
        cluster = ceph_cluster(dbg, get_cluster_name_by_uri(dbg, uri))

        try:
            cluster.connect()
            return rbd_utilization(
                dbg, cluster, get_sr_name_by_uri(dbg, uri),
                "%s%s" % (VDI_PREFIXES[get_vdi_type_by_uri(
                    dbg, uri)], volume_meta[IMAGE_UUID_TAG]))
        except Exception as e:
            log.debug(
                "%s: xcpng.librbd.volume.VolumeOperations.resize: Failed to create volume: uri: %s new_size: %s"
                % dbg, uri.new_size)
            raise Exception(e)
        finally:
            cluster.shutdown()
Ejemplo n.º 14
0
    def sr_import(self, dbg, uri, configuration):
        log.debug(
            "%s: xcpng.librbd.sr.SROperations.sr_import: uri: %s configuration %s"
            % (dbg, uri, configuration))

        cluster = ceph_cluster(dbg, get_cluster_name_by_uri(dbg, uri))
        pool_name = get_sr_name_by_uri(dbg, uri)

        try:
            cluster.connect()
            if not cluster.pool_exists(pool_name):
                raise Exception("CEPH pool %s doesn\'t exist" % pool_name)
        except Exception as e:
            log.debug(
                "%s: xcpng.librbd.sr.SROperations.get_vdi_list: uri: Failed to destroy SR: uri: %s"
                % dbg, uri)
            raise Exception(e)
        finally:
            cluster.shutdown()

        mkdir_p("%s/%s" % (SR_PATH_PREFIX, get_sr_uuid_by_uri(dbg, uri)))
Ejemplo n.º 15
0
    def create(self, dbg, uri, configuration):
        log.debug(
            "%s: xcpng.librbd.sr.SROperations.create: uri: %s configuration %s"
            % (dbg, uri, configuration))

        if CEPH_CLUSTER_TAG not in configuration:
            raise Exception(
                'Failed to connect to CEPH cluster. Parameter \'cluster\' is not specified'
            )

        cluster = ceph_cluster(dbg, configuration[CEPH_CLUSTER_TAG])

        try:
            cluster.connect()
            cluster.create_pool(get_sr_name_by_uri(dbg, uri))
        except Exception as e:
            log.debug(
                "%s: xcpng.librbd.sr.SROperations.create: uri: Failed to create SR: uri: %s"
                % dbg, uri)
            raise Exception(e)
        finally:
            cluster.shutdown()