def get_full_vol_name(vmdk_name, datastore, vm_datastore):
    """
    Forms full volume name from vmdk file name an datastore as volume@datastore
    For volumes on vm_datastore, just returns volume name
    """
    vol_name = vmdk_utils.strip_vmdk_extension(vmdk_name)
    logging.debug("get_full_vol_name: %s %s %s", vmdk_name, datastore, vm_datastore)
    if datastore == vm_datastore:
        return vol_name
    return "{0}@{1}".format(vol_name, datastore)
Beispiel #2
0
def get_full_vol_name(vmdk_name, datastore, vm_datastore):
    """
    Forms full volume name from vmdk file name an datastore as volume@datastore
    For volumes on vm_datastore, just returns volume name
    """
    vol_name = vmdk_utils.strip_vmdk_extension(vmdk_name)
    logging.debug("get_full_vol_name: %s %s %s", vmdk_name, datastore, vm_datastore)
    if datastore == vm_datastore:
        return vol_name
    return "{0}@{1}".format(vol_name, datastore)
def generate_ls_rows():
    """ Gather all volume metadata into rows that can be used to format a table """
    rows = []
    for v in vmdk_utils.get_volumes():
        path = os.path.join(v['path'], v['filename'])
        name = vmdk_utils.strip_vmdk_extension(v['filename'])
        metadata = get_metadata(path)
        attached_to = get_attached_to(metadata)
        policy = get_policy(metadata, path)
        size_info = get_vmdk_size_info(path)
        created, created_by = get_creation_info(metadata)
        rows.append([name, v['datastore'], created_by, created, attached_to,
                     policy, size_info['capacity'], size_info['used']])
    return rows
def generate_ls_rows():
    """ Gather all volume metadata into rows that can be used to format a table """
    rows = []
    for v in vmdk_utils.get_volumes():
        path = os.path.join(v['path'], v['filename'])
        name = vmdk_utils.strip_vmdk_extension(v['filename'])
        metadata = get_metadata(path)
        attached_to = get_attached_to(metadata)
        policy = get_policy(metadata, path)
        size_info = get_vmdk_size_info(path)
        created, created_by = get_creation_info(metadata)
        rows.append([name, v['datastore'], created_by, created, attached_to,
                     policy, size_info['capacity'], size_info['used']])
    return rows
def generate_ls_rows(tenant_reg):
    """ Gather all volume metadata into rows that can be used to format a table """
    rows = []
    for v in vmdk_utils.get_volumes(tenant_reg):
        if 'tenant' not in v or v['tenant'] == auth_data_const.ORPHAN_TENANT:
            tenant = 'N/A'
        else:
            tenant = v['tenant']
        path = os.path.join(v['path'], v['filename'])
        name = vmdk_utils.strip_vmdk_extension(v['filename'])
        metadata = get_metadata(path)
        attached_to = get_attached_to(metadata)
        policy = get_policy(metadata, path)
        size_info = get_vmdk_size_info(path)
        created, created_by = get_creation_info(metadata)
        diskformat = get_diskformat(metadata)
        fstype = get_fstype(metadata)
        access = get_access(metadata)
        attach_as = get_attach_as(metadata)
        rows.append([name, v['datastore'], tenant, size_info['capacity'], size_info['used'], fstype, policy,
                     diskformat, attached_to, access, attach_as, created_by, created])

    return rows
    def _remove_volumes_for_tenant(self, tenant_id):
        """ Delete all volumes belongs to this tenant.

            Do not use it outside of removing a tenant.

        """
        try:
            cur = self.conn.execute("SELECT name FROM tenants WHERE id = ?",
                                    (tenant_id, ))
            result = cur.fetchone()
        except sqlite3.Error as e:
            logging.error("Error %s when querying from tenants table", e)
            return str(e)

        error_msg = ""
        if result:
            logging.debug("remove_volumes_for_tenant: %s %s", tenant_id,
                          result)
            tenant_name = result[0]
            vmdks = vmdk_utils.get_volumes(tenant_name)
            # Delete all volumes for this tenant.
            dir_paths = set()
            for vmdk in vmdks:
                vmdk_path = os.path.join(vmdk['path'],
                                         "{0}".format(vmdk['filename']))
                dir_paths.add(vmdk['path'])
                logging.debug("path=%s filename=%s", vmdk['path'],
                              vmdk['filename'])
                logging.debug("Deleting volume path%s", vmdk_path)
                datastore_url = vmdk_utils.get_datastore_url(vmdk['datastore'])
                err = vmdk_ops.removeVMDK(
                    vmdk_path=vmdk_path,
                    vol_name=vmdk_utils.strip_vmdk_extension(vmdk['filename']),
                    vm_name=None,
                    tenant_uuid=tenant_id,
                    datastore_url=datastore_url)
                if err:
                    logging.error("remove vmdk %s failed with error %s",
                                  vmdk_path, err)
                    error_msg += str(err)

            VOL_RM_LOG_PREFIX = "Tenant <name> %s removal: "
            # delete the symlink /vmfs/volume/datastore_name/tenant_name
            # which point to /vmfs/volumes/datastore_name/tenant_uuid
            for (datastore, url, path) in vmdk_utils.get_datastores():
                dockvol_path, tenant_path = get_dockvol_path_tenant_path(
                    datastore_name=datastore, tenant_id=tenant_id)
                logging.debug(
                    VOL_RM_LOG_PREFIX + "try to remove symlink to %s",
                    tenant_name, tenant_path)

                if os.path.isdir(tenant_path):
                    exist_symlink_path = os.path.join(dockvol_path,
                                                      tenant_name)
                    if os.path.isdir(exist_symlink_path):
                        os.remove(exist_symlink_path)
                        logging.debug(
                            VOL_RM_LOG_PREFIX + "removing symlink %s",
                            tenant_name, exist_symlink_path)

            # Delete path /vmfs/volumes/datastore_name/tenant_uuid
            logging.debug("Deleting dir paths %s", dir_paths)
            for path in list(dir_paths):
                try:
                    os.rmdir(path)
                except os.error as e:
                    msg = "remove dir {0} failed with error {1}".format(
                        path, e)
                    logging.error(msg)
                    error_msg += str(err)

        err = self.remove_volumes_from_volumes_table(tenant_id)
        if err:
            logging.error("Failed to remove volumes from database %s", err)
            error_msg += str(err)

        if error_msg:
            return error_msg

        return None
Beispiel #7
0
def listVMDK(path):
    """ returns a list of volume names (note: may be an empty list) """
    vmdks = vmdk_utils.list_vmdks(path)
    # fully qualified path
    return [{u'Name': vmdk_utils.strip_vmdk_extension(x),
             u'Attributes': {}} for x in vmdks]