示例#1
0
def _get_barbican_secret_payload(ctxt, secret_ref):
    session = keystone.create_keystone_session(ctxt)
    barbican = barbican_client.Client(session=session)
    sec = utils.retry_on_error()(barbican.secrets.get)(secret_ref)
    # NOTE: accessing `payload` leads to another API call being made:
    payload = utils.retry_on_error()(getattr)(sec, "payload")
    return payload
示例#2
0
    def import_instance(self, ctxt, connection_info, target_environment,
                        instance_name, export_info):
        session = keystone.create_keystone_session(ctxt, connection_info)

        nova = nova_client.Client(NOVA_API_VERSION, session=session)
        glance = glance_client.Client(GLANCE_API_VERSION, session=session)
        neutron = neutron_client.Client(NEUTRON_API_VERSION, session=session)
        cinder = cinder_client.Client(CINDER_API_VERSION, session=session)

        glance_upload = target_environment.get(
            "glance_upload", CONF.openstack_migration_provider.glance_upload)
        target_disk_format = target_environment.get(
            "disk_format", CONF.openstack_migration_provider.disk_format)
        container_format = target_environment.get(
            "container_format",
            CONF.openstack_migration_provider.container_format)
        hypervisor_type = target_environment.get(
            "hypervisor_type",
            CONF.openstack_migration_provider.hypervisor_type)
        fip_pool_name = target_environment.get(
            "fip_pool_name", CONF.openstack_migration_provider.fip_pool_name)
        network_map = target_environment.get("network_map", {})
        keypair_name = target_environment.get("keypair_name")

        migr_image_name = target_environment.get(
            "migr_image_name",
            CONF.openstack_migration_provider.migr_image_name)
        migr_flavor_name = target_environment.get(
            "migr_flavor_name",
            CONF.openstack_migration_provider.migr_flavor_name)

        migr_fip_pool_name = target_environment.get(
            "migr_fip_pool_name",
            CONF.openstack_migration_provider.fip_pool_name or fip_pool_name)
        migr_network_name = target_environment.get(
            "migr_network_name",
            CONF.openstack_migration_provider.migr_network_name)

        flavor_name = target_environment.get("flavor_name", migr_flavor_name)

        if not migr_network_name:
            if len(network_map) != 1:
                raise exception.CoriolisException(
                    'If "migr_network_name" is not provided, "network_map" '
                    'must contain exactly one entry')
            migr_network_name = network_map.values()[0]

        disks_info = export_info["devices"]["disks"]

        images = []
        volumes = []
        volume_devs = []

        if glance_upload:
            for disk_info in disks_info:
                disk_path = disk_info["path"]
                disk_file_info = utils.get_disk_info(disk_path)

                # if target_disk_format == disk_file_info["format"]:
                #    target_disk_path = disk_path
                # else:
                #    target_disk_path = (
                #        "%s.%s" % (os.path.splitext(disk_path)[0],
                #                   target_disk_format))
                #    utils.convert_disk_format(disk_path, target_disk_path,
                #                              target_disk_format)

                self._event_manager.progress_update("Uploading Glance image")

                disk_format = disk_file_info["format"]
                image = self._create_image(
                    glance, self._get_unique_name(),
                    disk_path, disk_format,
                    container_format, hypervisor_type)
                images.append(image)

                virtual_disk_size = disk_file_info["virtual-size"]
                if disk_format != constants.DISK_FORMAT_RAW:
                    virtual_disk_size += DISK_HEADER_SIZE

                self._event_manager.progress_update("Creating Cinder volume")

                volume_size_gb = math.ceil(virtual_disk_size / units.Gi)
                volume = nova.volumes.create(
                    size=volume_size_gb,
                    display_name=self._get_unique_name(),
                    imageRef=image.id)
                volumes.append(volume)

        migr_resources = self._deploy_migration_resources(
            nova, glance, neutron, migr_image_name, migr_flavor_name,
            migr_network_name, migr_fip_pool_name)

        nics_info = export_info["devices"].get("nics", [])

        try:
            for i, volume in enumerate(volumes):
                self._wait_for_volume(nova, volume, 'available')

                self._event_manager.progress_update("Deleting Glance image")

                glance.images.delete(images[i].id)

                self._event_manager.progress_update(
                    "Attaching volume to worker instance")

                # TODO: improve device assignment
                if hypervisor_type == constants.HYPERVISOR_HYPERV:
                    volume_dev_base = "/dev/sd%s"
                else:
                    volume_dev_base = "/dev/vd%s"

                volume_dev = volume_dev_base % chr(ord('a') + i + 1)
                volume_devs.append(volume_dev)
                self._attach_volume(nova, migr_resources.get_instance(),
                                    volume, volume_dev)

                guest_conn_info = migr_resources.get_guest_connection_info()

            self._event_manager.progress_update(
                "Preparing instance for target platform")
            osmorphing_manager.morph_image(guest_conn_info,
                                           hypervisor_type,
                                           constants.PLATFORM_OPENSTACK,
                                           volume_devs,
                                           nics_info,
                                           self._event_manager)
        finally:
            self._event_manager.progress_update(
                "Removing worker instance resources")

            migr_resources.delete()

        self._event_manager.progress_update("Renaming volumes")

        for i, volume in enumerate(volumes):
            new_volume_name = "%s %s" % (instance_name, i + 1)
            cinder.volumes.update(volume.id, name=new_volume_name)

        ports = []
        for nic_info in nics_info:
            self._event_manager.progress_update(
                "Creating Neutron port for migrated instance")

            origin_network_name = nic_info.get("network_name")
            if not origin_network_name:
                self._warn("Origin network name not provided for for nic: %s, "
                           "skipping", nic_info.get("name"))
                continue

            network_name = network_map.get(origin_network_name)
            if not network_name:
                raise exception.CoriolisException(
                    "Network not mapped in network_map: %s" %
                    origin_network_name)

            ports.append(self._create_neutron_port(
                neutron, network_name, nic_info.get("mac_address")))

        self._event_manager.progress_update(
            "Creating migrated instance")

        instance = self._create_target_instance(
            nova, flavor_name, instance_name, keypair_name, ports, volumes,
            migr_image_name)
示例#3
0
    def import_instance(self, ctxt, connection_info, target_environment,
                        instance_name, export_info):
        session = keystone.create_keystone_session(ctxt, connection_info)

        glance_api_version = connection_info.get("image_api_version",
                                                 GLANCE_API_VERSION)

        nova = nova_client.Client(NOVA_API_VERSION, session=session)
        glance = glance_client.Client(glance_api_version, session=session)
        neutron = neutron_client.Client(NEUTRON_API_VERSION, session=session)
        cinder = cinder_client.Client(CINDER_API_VERSION, session=session)

        os_type = export_info.get('os_type')
        LOG.info("os_type: %s", os_type)

        glance_upload = target_environment.get(
            "glance_upload", CONF.openstack_migration_provider.glance_upload)
        target_disk_format = target_environment.get(
            "disk_format", CONF.openstack_migration_provider.disk_format)
        container_format = target_environment.get(
            "container_format",
            CONF.openstack_migration_provider.container_format)
        hypervisor_type = target_environment.get(
            "hypervisor_type",
            CONF.openstack_migration_provider.hypervisor_type)
        fip_pool_name = target_environment.get(
            "fip_pool_name", CONF.openstack_migration_provider.fip_pool_name)
        network_map = target_environment.get("network_map", {})
        keypair_name = target_environment.get("keypair_name")

        migr_image_name = target_environment.get(
            "migr_image_name",
            target_environment.get("migr_image_name_map", {}).get(
                os_type,
                CONF.openstack_migration_provider.migr_image_name_map.get(
                    os_type)))
        migr_flavor_name = target_environment.get(
            "migr_flavor_name",
            CONF.openstack_migration_provider.migr_flavor_name)

        migr_fip_pool_name = target_environment.get(
            "migr_fip_pool_name",
            fip_pool_name or CONF.openstack_migration_provider.fip_pool_name)
        migr_network_name = target_environment.get(
            "migr_network_name",
            CONF.openstack_migration_provider.migr_network_name)

        flavor_name = target_environment.get("flavor_name", migr_flavor_name)

        if not migr_image_name:
            raise exception.CoriolisException(
                "No matching migration image type found")

        LOG.info("Migration image name: %s", migr_image_name)

        if not migr_network_name:
            if len(network_map) != 1:
                raise exception.CoriolisException(
                    'If "migr_network_name" is not provided, "network_map" '
                    'must contain exactly one entry')
            migr_network_name = network_map.values()[0]

        disks_info = export_info["devices"]["disks"]

        images = []
        volumes = []
        ports = []

        try:
            if glance_upload:
                for disk_info in disks_info:
                    disk_path = disk_info["path"]
                    disk_file_info = utils.get_disk_info(disk_path)

                    # if target_disk_format == disk_file_info["format"]:
                    #    target_disk_path = disk_path
                    # else:
                    #    target_disk_path = (
                    #        "%s.%s" % (os.path.splitext(disk_path)[0],
                    #                   target_disk_format))
                    #    utils.convert_disk_format(disk_path, target_disk_path,
                    #                              target_disk_format)

                    self._event_manager.progress_update(
                        "Uploading Glance image")

                    disk_format = disk_file_info["format"]
                    image = self._create_image(
                        glance, self._get_unique_name(),
                        disk_path, disk_format,
                        container_format, hypervisor_type)
                    images.append(image)

                    self._event_manager.progress_update(
                        "Waiting for Glance image to become active")
                    self._wait_for_image(nova, image)

                    virtual_disk_size = disk_file_info["virtual-size"]
                    if disk_format != constants.DISK_FORMAT_RAW:
                        virtual_disk_size += DISK_HEADER_SIZE

                    self._event_manager.progress_update(
                        "Creating Cinder volume")

                    volume_size_gb = math.ceil(virtual_disk_size / units.Gi)
                    volume = nova.volumes.create(
                        size=volume_size_gb,
                        display_name=self._get_unique_name(),
                        imageRef=image.id)
                    volumes.append(volume)

            migr_resources = self._deploy_migration_resources(
                nova, glance, neutron, os_type, migr_image_name,
                migr_flavor_name, migr_network_name, migr_fip_pool_name)

            nics_info = export_info["devices"].get("nics", [])

            try:
                for i, volume in enumerate(volumes):
                    self._wait_for_volume(nova, volume, 'available')

                    self._event_manager.progress_update(
                        "Attaching volume to worker instance")

                    self._attach_volume(nova, migr_resources.get_instance(),
                                        volume)

                    conn_info = migr_resources.get_guest_connection_info()

                osmorphing_hv_type = self._get_osmorphing_hypervisor_type(
                    hypervisor_type)

                self._event_manager.progress_update(
                    "Preparing instance for target platform")
                osmorphing_manager.morph_image(conn_info,
                                               os_type,
                                               osmorphing_hv_type,
                                               constants.PLATFORM_OPENSTACK,
                                               nics_info,
                                               self._event_manager)
            finally:
                self._event_manager.progress_update(
                    "Removing worker instance resources")
                migr_resources.delete()

            self._event_manager.progress_update("Renaming volumes")

            for i, volume in enumerate(volumes):
                new_volume_name = "%s %s" % (instance_name, i + 1)
                cinder.volumes.update(volume.id, name=new_volume_name)

            for nic_info in nics_info:
                self._event_manager.progress_update(
                    "Creating Neutron port for migrated instance")

                origin_network_name = nic_info.get("network_name")
                if not origin_network_name:
                    self._warn("Origin network name not provided for for nic: "
                               "%s, skipping", nic_info.get("name"))
                    continue

                network_name = network_map.get(origin_network_name)
                if not network_name:
                    raise exception.CoriolisException(
                        "Network not mapped in network_map: %s" %
                        origin_network_name)

                ports.append(self._create_neutron_port(
                    neutron, network_name, nic_info.get("mac_address")))

            self._event_manager.progress_update(
                "Creating migrated instance")

            self._create_target_instance(
                nova, flavor_name, instance_name, keypair_name, ports, volumes)
        except Exception:
            self._event_manager.progress_update("Deleting volumes")
            for volume in volumes:
                @utils.ignore_exceptions
                @utils.retry_on_error()
                def _del_volume():
                    volume.delete()
                _del_volume()
            self._event_manager.progress_update("Deleting Neutron ports")
            for port in ports:
                @utils.ignore_exceptions
                @utils.retry_on_error()
                def _del_port():
                    neutron.delete_port(port["id"])
                _del_port()
            raise
        finally:
            self._event_manager.progress_update("Deleting Glance images")
            for image in images:
                @utils.ignore_exceptions
                @utils.retry_on_error()
                def _del_image():
                    image.delete()
                _del_image()
示例#4
0
def get_secret(ctxt, secret_ref):
    session = keystone.create_keystone_session(ctxt)
    barbican = barbican_client.Client(session=session)
    return json.loads(barbican.secrets.get(secret_ref).payload)
示例#5
0
def get_secret(ctxt, secret_ref):
    session = keystone.create_keystone_session(ctxt)
    barbican = barbican_client.Client(session=session)
    return json.loads(barbican.secrets.get(secret_ref).payload)