示例#1
0
 def test_find_by_group_name(self):
     display_manager = FakeDisplayManager(None)
     output = utils.find_resource(display_manager,
                                  'entity_three',
                                  is_group=True,
                                  list_volume=True)
     self.assertEqual(display_manager.get('4242', list_volume=True), output)
def find_volume_snapshot(cs, snapshot):
    """Gets a volume snapshot by name or ID."""
    return utils.find_resource(cs.volume_snapshots, snapshot)
def find_qos_specs(cs, qos_specs):
    """Gets a qos specs by ID."""
    return utils.find_resource(cs.qos_specs, qos_specs)
示例#4
0
 def test_find_by_str_id(self):
     output = utils.find_resource(self.manager, "1234")
     self.assertEqual(output, self.manager.get("1234"))
示例#5
0
 def test_find_by_str_name(self):
     output = utils.find_resource(self.manager, 'entity_one')
     self.assertEqual(self.manager.get('1234'), output)
def _find_qos_specs(cs, qos_specs):
    """Gets a qos specs by ID."""
    return utils.find_resource(cs.qos_specs, qos_specs)
 def test_find_by_str_displayname(self):
     output = utils.find_resource(self.manager, "entity_three")
     self.assertEqual(self.manager.get("4242"), output)
示例#8
0
def _find_volume(cs, volume):
    """Get a volume by ID."""
    return utils.find_resource(cs.volumes, volume)
示例#9
0
def _find_backup(cs, backup):
    """Get a backup by ID."""
    return utils.find_resource(cs.backups, backup)
示例#10
0
def _find_volume_snapshot(cs, snapshot):
    """Get a volume snapshot by ID."""
    return utils.find_resource(cs.volume_snapshots, snapshot)
示例#11
0
def _find_volume(cs, volume):
    """Get a volume by ID."""
    return utils.find_resource(cs.volumes, volume)
示例#12
0
 def test_find_by_str_id(self):
     output = utils.find_resource(self.manager, '1234')
     self.assertEqual(output, self.manager.get('1234'))
示例#13
0
 def test_find_by_group_name(self):
     display_manager = FakeDisplayManager(None)
     output = utils.find_resource(display_manager, 'entity_three',
                                  is_group=True, list_volume=True)
     self.assertEqual(display_manager.get('4242', list_volume=True),
                      output)
示例#14
0
 def test_find_by_group_id(self):
     output = utils.find_resource(self.manager, 1234, is_group=True,
                                  list_volume=True)
     self.assertEqual(self.manager.get('1234', list_volume=True), output)
示例#15
0
def find_gtype(cs, gtype):
    """Gets a group type by name or ID."""
    return utils.find_resource(cs.group_types, gtype)
示例#16
0
def find_consistencygroup(cs, consistencygroup):
    """Gets a consistency group by name or ID."""
    return utils.find_resource(cs.consistencygroups, consistencygroup)
示例#17
0
def _find_transfer(cs, transfer):
    """Get a transfer by ID."""
    return utils.find_resource(cs.transfers, transfer)
 def test_find_by_uuid(self):
     output = utils.find_resource(self.manager, UUID)
     self.assertEqual(self.manager.get(UUID), output)
示例#19
0
def find_group(cs, group):
    """Gets a group by name or ID."""
    return utils.find_resource(cs.groups, group)
def _find_backup(cs, backup):
    """Gets a backup by name or ID."""
    return utils.find_resource(cs.backups, backup)
def find_group_snapshot(cs, group_snapshot):
    """Gets a group_snapshot by name or ID."""
    return utils.find_resource(cs.group_snapshots, group_snapshot)
示例#22
0
 def test_find_by_integer_id(self):
     output = utils.find_resource(self.manager, 1234)
     self.assertEqual(self.manager.get('1234'), output)
def find_message(cs, message):
    """Gets a message by ID."""
    return utils.find_resource(cs.messages, message)
示例#24
0
 def test_find_by_group_id(self):
     output = utils.find_resource(self.manager,
                                  1234,
                                  is_group=True,
                                  list_volume=True)
     self.assertEqual(self.manager.get('1234', list_volume=True), output)
def find_gtype(cs, gtype):
    """Gets a group type by name or ID."""
    return utils.find_resource(cs.group_types, gtype)
示例#26
0
def find_transfer(cs, transfer):
    """Gets a transfer by name or ID."""
    return utils.find_resource(cs.transfers, transfer)
def find_consistencygroup(cs, consistencygroup):
    """Gets a consistency group by name or ID."""
    return utils.find_resource(cs.consistencygroups, consistencygroup)
示例#28
0
def find_message(cs, message):
    """Gets a message by ID."""
    return utils.find_resource(cs.messages, message)
def find_group(cs, group, **kwargs):
    """Gets a group by name or ID."""
    kwargs['is_group'] = True
    return utils.find_resource(cs.groups, group, **kwargs)
示例#30
0
def find_vtype(cs, vtype):
    """Gets a volume type by name or ID."""
    return utils.find_resource(cs.volume_types, vtype)
def find_cgsnapshot(cs, cgsnapshot):
    """Gets a cgsnapshot by name or ID."""
    return utils.find_resource(cs.cgsnapshots, cgsnapshot)
示例#32
0
def find_backup(cs, backup):
    """Gets a backup by name or ID."""
    return utils.find_resource(cs.backups, backup)
 def test_find_by_str_name(self):
     output = utils.find_resource(self.manager, 'entity_one')
     self.assertEqual(output, self.manager.get('1234'))
示例#34
0
def find_group(cs, group, **kwargs):
    """Gets a group by name or ID."""
    kwargs['is_group'] = True
    return utils.find_resource(cs.groups, group, **kwargs)
 def test_find_by_str_displayname(self):
     output = utils.find_resource(self.manager, 'entity_three')
     self.assertEqual(output, self.manager.get('4242'))
 def test_find_by_integer_id(self):
     output = utils.find_resource(self.manager, 1234)
     self.assertEqual(self.manager.get("1234"), output)
示例#37
0
def find_group(cs, group):
    """Gets a group by name or ID."""
    return utils.find_resource(cs.groups, group)
 def test_find_by_str_name(self):
     output = utils.find_resource(self.manager, "entity_one")
     self.assertEqual(self.manager.get("1234"), output)
 def take_action(self, parsed_args):
     volume_client = self.app.client_manager.volume
     snapshot = cinder_utils.find_resource(volume_client.volume_snapshots,
                                           parsed_args.snapshot)
     volume_client.volume_snapshots.export(snapshot)
def _find_volume_snapshot(cs, snapshot):
    """Gets a volume snapshot by name or ID."""
    return utils.find_resource(cs.volume_snapshots, snapshot)
 def test_find_by_str_displayname(self):
     output = utils.find_resource(self.manager, 'entity_three')
     self.assertEqual(self.manager.get('4242'), output)
def _find_transfer(cs, transfer):
    """Gets a transfer by name or ID."""
    return utils.find_resource(cs.transfers, transfer)
示例#43
0
def tidy_storage(result_file):
    """
    Search Glance images DB and rbd images pool for any discrepancy
    between the two.
      - If an image is in Glance images DB but not in rbd images pool,
        list the image and suggested actions to take in a log file.
      - If an image is in rbd images pool but not in Glance images DB,
        create a Glance image in Glance images DB to associate with the
        backend data. List the image and suggested actions to take in a log
        file.

    Search Cinder volumes DB and rbd cinder-volumes pool for any discrepancy
    between the two.
       - If a volume is in Cinder volumes DB but not in rbd cinder-volumes
         pool, set the volume state to "error". List the volume and suggested
         actions to take in a log file.
       - If a volume is in rbd cinder-volumes pool but not in Cinder volumes
         DB, remove any snapshot(s) assoicated with this volume in rbd pool and
         create a volume in Cinder volumes DB to associate with the backend
         data. List the volume and suggested actions to take in a log file.
       - If a volume is in both Cinder volumes DB and rbd cinder-volumes pool
         and it has snapshot(s) in the rbd pool, re-create the snapshot in
         Cinder if it doesn't exist.

    Clean up Cinder snapshots DB if the snapshot doesn't have backend data.

    """
    with OpenStack() as client:
        # Check Glance images
        print("Scanning Glance images in DB and rbd images pool...\n")
        try:
            g_client_v1, g_client_v2 = client.get_glance_client
            image_l = g_client_v2.images.list()
            image_id_l = [image['id'].encode('utf-8') for image in image_l]

            output = subprocess.check_output(["rbd", "ls", "--pool", "images"],
                                             stderr=subprocess.STDOUT)
        except subprocess.CalledProcessError:
            LOG.error("Failed to access rbd images pool")
            raise TidyStorageFail("Failed to access rbd images pool")
        except Exception as e:
            LOG.exception(e)
            raise TidyStorageFail("Failed to list Glance images")

        rbd_image_l = [i for i in output.split('\n') if i != ""]

        print("Images in Glance images DB: %s \n" % image_id_l)
        print("Images in rbd images pool:  %s \n" % rbd_image_l)

        in_glance_only = np.setdiff1d(image_id_l, rbd_image_l)
        in_rbd_image_only = np.setdiff1d(rbd_image_l, image_id_l)

        print("Images in Glance images DB only: %s \n" % in_glance_only)
        print("Images in rbd images pool only:  %s \n" % in_rbd_image_only)

        if in_rbd_image_only.size != 0:
            output = subprocess.check_output(
                ["grep", "fsid", "/etc/ceph/ceph.conf"],
                stderr=subprocess.STDOUT)

            ceph_cluster = [
                i.strip() for i in output.split('=') if i.find('fsid') == -1
            ]

        fields = dict()
        for image in in_rbd_image_only:
            try:
                img_file = 'rbd:images/{}'.format(image)
                output = subprocess.check_output(
                    ["qemu-img", "info", img_file], stderr=subprocess.STDOUT)

                fields['disk_format'] = 'qcow2'
                for line in output.split('\n'):
                    if 'file format:' in line:
                        fields['disk_format'] = line.split(':')[1].strip()
                        break

                fields['name'] = 'found-image-%s' % image
                fields['id'] = image
                fields['container_format'] = 'bare'
                fields['location'] = \
                    'rbd://{}/images/{}/snap'.format(ceph_cluster[0],
                                                     image)

                print("Creating a Glance image %s ...\n " % fields['name'])
                g_client_v1.images.create(**fields)
            except subprocess.CalledProcessError:
                LOG.error("Failed to access rbd image %s" % image)
                raise TidyStorageFail("Failed to access rbd image")
            except Exception as e:
                LOG.exception(e)
                raise TidyStorageFail("Failed to create glance image")

        # Check cinder volume snapshots. Do it before "cinder manage"
        # operation as "cinder manage" does not support keeping the same
        # volume id.
        print("Scanning Cinder snapshots in DB and rbd cinder-volumes "
              "pool...\n")
        try:
            c_client = client.get_cinder_client
            snap_l = c_client.volume_snapshots.list(search_opts=search_opts)
        except Exception as e:
            LOG.exception(e)
            raise TidyStorageFail("Failed to get Cinder snapshots")

        snaps_no_backend_vol = []
        for snap in snap_l:
            print("Check if volume snapshot %s has backend " % snap.name)
            try:
                output = subprocess.check_output(
                    ["rbd", "ls", "--pool", "cinder-volumes"],
                    stderr=subprocess.STDOUT)
            except subprocess.CalledProcessError:
                LOG.error("Failed to access rbd cinder-volumes pool")
                raise TidyStorageFail(
                    "Failed to access rbd cinder-volumes pool")

            found_vol = False
            for line in output.split('\n'):
                if snap.volume_id in line:
                    found_vol = True
                    break

            if found_vol:
                volume = 'cinder-volumes/volume-{}'.format(snap.volume_id)
                try:
                    output = subprocess.check_output(
                        ["rbd", "snap", "list", volume],
                        stderr=subprocess.STDOUT)

                    keep_snap = False
                    for line in output.split('\n'):
                        if snap.id in line:
                            keep_snap = True
                            break
                except subprocess.CalledProcessError:
                    LOG.info("Failed to list snapshots for volume %s in "
                             "rbd cinder-volumes pool" % snap.volume_id)
                    raise TidyStorageFail("Failed to list snapshots in rbd.")

                if not keep_snap:
                    try:
                        print("Volume snapshot %s has no backend data. "
                              "Deleting it from Cinder...\n" % snap.name)

                        c_client.volume_snapshots.delete(c_utils.find_resource(
                            c_client.volume_snapshots, snap.id),
                                                         force=True)

                    except Exception as e:
                        LOG.exception(e)
                        raise TidyStorageFail(
                            "Failed to delete volume snapshot")

            else:
                # Volume snapshot that doesn't have backend volume cannot
                # be deleted. If the backend volume is restored later, then
                # the snapshot can be deleted. So for now we will add these
                # snapshots in the user action log.
                snaps_no_backend_vol.append(snap)

        # Check Cinder volumes
        print("Scanning Cinder volumes in DB and rbd cinder-volumes pool...\n")
        try:
            volume_l = c_client.volumes.list(search_opts=search_opts)
            v_t_d = c_client.volume_types.default()
            avail_zones = c_client.availability_zones.list()
            pools = c_client.pools.list()
        except Exception as e:
            LOG.exception(e)
            raise TidyStorageFail("Failed to get Cinder volume info")

        if pools:
            host = pools[0].name  # pylint: disable=no-member

        if v_t_d is None:
            v_t_d = 'ceph'
        else:
            v_t_d = v_t_d.name

        cinder_volume_l = [i.id.encode('utf-8') for i in volume_l]

        if avail_zones:
            avail_z = avail_zones[0].zoneName  # pylint: disable=no-member

        try:
            output = subprocess.check_output(
                ["rbd", "ls", "--pool", "cinder-volumes"],
                stderr=subprocess.STDOUT)
        except subprocess.CalledProcessError:
            LOG.error("Failed to access rbd cinder-volumes pool")
            raise TidyStorageFail("Failed to access rbd cinder-volumes pool")

        rbd_volume_l = [i[7:] for i in output.split('\n') if i != ""]

        print("Volumes in Cinder volumes DB: %s \n" % cinder_volume_l)
        print("Volumes in rbd pool: %s \n" % rbd_volume_l)

        in_cinder_only = np.setdiff1d(cinder_volume_l, rbd_volume_l)
        in_rbd_volume_only = np.setdiff1d(rbd_volume_l, cinder_volume_l)
        in_cinder_and_rbd = np.intersect1d(cinder_volume_l, rbd_volume_l)

        print("Volumes in Cinder volumes DB only: %s \n" % in_cinder_only)
        print("Volumes in rbd pool only: %s \n" % in_rbd_volume_only)
        print("Volumes in Cinder volumes DB and rbd pool: %s \n" %
              in_cinder_and_rbd)

        for vol_id in in_rbd_volume_only:
            volume = 'cinder-volumes/volume-{}'.format(vol_id)
            try:
                # Find out if the volume is a bootable one
                output = subprocess.check_output(["rbd", "info", volume],
                                                 stderr=subprocess.STDOUT)

                bootable = False
                for line in output.split('\n'):
                    if 'parent: images/' in line:
                        bootable = True
                        break

                # Find out if the volume has any snapshots.
                print("Checking if volume %s has snapshots...\n" % vol_id)
                output = subprocess.check_output(
                    ["rbd", "snap", "list", volume], stderr=subprocess.STDOUT)

                snap_l = [
                    item.strip() for item in output.split(' ')
                    if item.find('snapshot-') != -1
                ]

                # Returned volume id (vol.id) will be different from vol_id
                try:
                    vol = c_client.volumes.manage(
                        host=host,
                        ref={'source-name': 'volume-%s' % vol_id},
                        name='found-volume-%s' % vol_id,
                        description='manage a volume',
                        volume_type=v_t_d,
                        availability_zone=avail_z,
                        bootable=bootable)

                    print("Creating volume found-volume-%s in Cinder...\n" %
                          vol_id)
                except Exception as e:
                    LOG.exception(e)
                    raise TidyStorageFail("Failed to manage volume")

                try:
                    for snap in snap_l:
                        # Manage a snapshot for a managed volume is not
                        # supported in rbd. So we just remove the snapshot.

                        # Remove the snapshot
                        print(
                            textwrap.fill(
                                "Removing snapshot %s from volume %s "
                                "in rbd...\n" % (snap, vol_id), 76))
                        del_snap = '{}@{}'.format(volume, snap)
                        output = subprocess.check_output(
                            ["rbd", "snap", "unprotect", del_snap],
                            stderr=subprocess.STDOUT)

                        output = subprocess.check_output(
                            ["rbd", "snap", "rm", del_snap],
                            stderr=subprocess.STDOUT)

                except Exception as e:
                    LOG.exception(e)
                    raise TidyStorageFail("Failed to manage volume snapshot")
            except subprocess.CalledProcessError:
                LOG.error("Failed to access volume %s in cinder-volumes pool" %
                          vol_id)
                raise TidyStorageFail("Failed to access rbd image")

        for vol in in_cinder_only:
            try:
                c_client.volumes.reset_state(c_utils.find_volume(
                    c_client, vol),
                                             state='error')
                print("Setting state to error for volume %s \n" % vol)
            except Exception as e:
                LOG.error("Failed to update volume to error state for %s" %
                          vol)
                raise TidyStorageFail("Failed to update volume to error state")

        # For volumes that are in Cinder volumes DB and rbd cinder-volumes
        # pool, we check if any volume snapshot needs to be re-created
        try:
            c_s_l = c_client.volume_snapshots.list(search_opts=search_opts)
            cinder_snap_l = ['snapshot-{}'.format(snap.id) for snap in c_s_l]
        except Exception as e:
            LOG.exception(e)
            raise TidyStorageFail("Failed to get Cinder snapshots")

        for vol_id in in_cinder_and_rbd:
            volume = 'cinder-volumes/volume-{}'.format(vol_id)
            try:
                # Find out if the volume has any snapshots.
                print("Checking if volume %s has snapshots...\n" % vol_id)
                output = subprocess.check_output(
                    ["rbd", "snap", "list", volume], stderr=subprocess.STDOUT)

                snap_l = [
                    item.strip() for item in output.split(' ')
                    if item.find('snapshot-') != -1
                ]

                for snap in snap_l:
                    if snap not in cinder_snap_l:
                        print("Creating volume snapshot found-%s "
                              "in Cinder...\n" % snap)

                        c_client.volume_snapshots.manage(
                            volume_id=vol_id,
                            ref={'source-name': snap},
                            name='found-%s' % snap,
                            description='manage a snapshot')
            except subprocess.CalledProcessError:
                LOG.error("Failed to access snapshot for volume %s" % vol_id)
                raise TidyStorageFail("Failed to access volume snapshot")
            except Exception as e:
                LOG.exception(e)
                raise TidyStorageFail("Failed to manage Cinder snapshot")

        try:
            with open(result_file, 'w') as f:
                f.write('\n%s\n' % ('-' * 80))
                f.write(
                    textwrap.fill(
                        "Following images are found in Ceph images pool but "
                        "not in Glance. These images were created after the "
                        "system backup was done. If you do not want to keep "
                        "them, you can delete them by "
                        "\"glance image-delete <id>\" command.", 80))
                f.write("\n\n")
                f.write('{0[0]:<40}{0[1]:<50}\n'.format(['ID', 'NAME']))
                image_l = g_client_v2.images.list()
                for image in image_l:
                    if image['name'].find("found-image") != -1:
                        f.write('{0[0]:<40}{0[1]:<50}\n'.format(
                            [image['id'].encode('utf-8'), image['name']]))

                f.write("\n")
                f.write('\n%s\n' % ('-' * 80))
                f.write(
                    textwrap.fill(
                        "Following images are found in Glance without backend "
                        "data associated with. These images were deleted after "
                        "the system backup was done. You can delete them by "
                        "\"glance image-delete <id>\" command or follow the B&R "
                        "document to restore the image.", 80))
                f.write("\n\n")
                f.write('{0[0]:<40}{0[1]:<50}\n'.format(['ID', 'NAME']))
                image_l = g_client_v2.images.list()
                for image in image_l:
                    if (in_glance_only.size != 0
                            and image['id'].encode('utf-8') in in_glance_only):
                        f.write('{0[0]:<40}{0[1]:<50}\n'.format(
                            [image['id'].encode('utf-8'), image['name']]))

                f.write("\n")
                f.write('\n%s\n' % ('-' * 80))
                f.write(
                    textwrap.fill(
                        "Following volumes are found in Ceph cinder-volumes "
                        "pool but not in Cinder. These volumes were created "
                        "after the system backup was done. If you do not want "
                        "to keep them you can delete them by "
                        "\"cinder delete <id>\" command.", 80))
                f.write("\n\n")
                f.write('{0[0]:<40}{0[1]:<50}\n'.format(['ID', 'NAME']))
                volume_l = c_client.volumes.list(search_opts=search_opts)
                for volume in volume_l:
                    if volume.name.find("found-") != -1:
                        f.write('{0[0]:<40}{0[1]:<50}\n'.format(
                            [volume.id.encode('utf-8'), volume.name]))

                f.write("\n")
                f.write('\n%s\n' % ('-' * 80))
                f.write(
                    textwrap.fill(
                        "Following volumes are found in Cinder without backend "
                        "data associated with. These volumes were deleted "
                        "after the system backup was done. You can delete them "
                        "by \"cinder delete <id>\" command or follow the B&R "
                        "document to restore the cinder volume.", 80))
                f.write("\n\n")
                f.write('{0[0]:<40}{0[1]:<50}\n'.format(['ID', 'NAME']))
                volume_l = c_client.volumes.list(search_opts=search_opts)
                for volume in volume_l:
                    if (in_cinder_only.size != 0
                            and volume.id in in_cinder_only):
                        f.write('{0[0]:<40}{0[1]:<50}\n'.format(
                            [volume.id.encode('utf-8'), volume.name]))

                f.write("\n")
                f.write('\n%s\n' % ('-' * 80))
                f.write(
                    textwrap.fill(
                        "Following volume snapshots are found in Ceph but not in "
                        "Cinder. These volume snapshots were created after the "
                        "system backup was done. If you do not want to keep them "
                        "you can delete them by \"cinder snapshot-delete <id>\" "
                        "command.", 80))
                f.write("\n\n")
                f.write('{0[0]:<40}{0[1]:<50}\n'.format(['ID', 'NAME']))
                snap_l = c_client.volume_snapshots.list(
                    search_opts=search_opts)
                for snap in snap_l:
                    if snap.name.find("found-") != -1:
                        f.write('{0[0]:<40}{0[1]:<50}\n'.format(
                            [snap.id.encode('utf-8'), snap.name]))

                f.write("\n")
                f.write('\n%s\n' % ('-' * 80))
                f.write(
                    textwrap.fill(
                        "Following volume snapshots are found in Cinder without "
                        "backend volumes. If you want to delete them, you can do "
                        "so by \"cinder snapshot-delete <id>\" after backend "
                        "volumes are restored.", 80))
                f.write("\n\n")
                f.write('{0[0]:<40}{0[1]:<50}\n'.format(['ID', 'NAME']))
                for snap in snaps_no_backend_vol:
                    f.write('{0[0]:<40}{0[1]:<50}\n'.format(
                        [snap.id.encode('utf-8'), snap.name]))

                f.write("\n\n")

        except IOError:
            raise TidyStorageFail("Failed to open file: %s" % result_file)
def _find_volume_type(cs, vtype):
    """Gets a volume type by name or ID."""
    return utils.find_resource(cs.volume_types, vtype)
示例#45
0
 def test_find_by_str_displayname(self):
     display_manager = FakeDisplayManager(None)
     output = utils.find_resource(display_manager, 'entity_three')
     self.assertEqual(display_manager.get('4242'), output)
示例#46
0
 def test_find_by_uuid(self):
     output = utils.find_resource(self.manager, UUID)
     self.assertEqual(self.manager.get(UUID), output)
示例#47
0
 def test_find_by_str_id(self):
     output = utils.find_resource(self.manager, '1234')
     self.assertEqual(self.manager.get('1234'), output)
示例#48
0
 def test_find_by_str_displayname(self):
     display_manager = FakeDisplayManager(None)
     output = utils.find_resource(display_manager, 'entity_three')
     self.assertEqual(display_manager.get('4242'), output)
示例#49
0
def find_cgsnapshot(cs, cgsnapshot):
    """Gets a cgsnapshot by name or ID."""
    return utils.find_resource(cs.cgsnapshots, cgsnapshot)
示例#50
0
def find_group_snapshot(cs, group_snapshot):
    """Gets a group_snapshot by name or ID."""
    return utils.find_resource(cs.group_snapshots, group_snapshot)
 def take_action(self, parsed_args):
     volume_client = self.app.client_manager.volume
     volume = cinder_utils.find_resource(volume_client.volumes,
                                         parsed_args.volume)
     volume_client.volumes.export(volume)