def upload_image(self, image_id, image_name, scratch_dir, disk_format=None, container_format=None): if image_id is not None: #this is the 2nd part of a transfer not a direct upload try: glance = glanceclient.Client('2', session=self.sess) except Exception as exc: logging.error("Unable to create glance object:") logging.error(exc) return False images = glance.images.list() file_path = scratch_dir + image_name glance.images.upload(image_id, open(file_path, 'rb')) return image_id else: #this is a straight upload not part of a transfer try: glance = glanceclient.Client('2', session=self.sess) except Exception as exc: logging.error("Unable to create glance object:") logging.error(exc) return False image = glance.images.create(name=image_name, disk_format=disk_format, container_format=container_format) glance.images.upload(image.id, open(scratch_dir, 'rb')) logger.info("Upload complete") return image.id
def get_glanceclient(version='1', **kwargs): # newer versions of the glanceclient will do this 'strip_version' for # us, but older versions do not. kwargs['endpoint'] = _strip_version(kwargs['endpoint']) pt = ('endpoint', 'token', 'insecure', 'cacert') kskw = {k: kwargs.get(k) for k in pt if k in kwargs} if kwargs.get('session'): sess = kwargs.get('session') return glanceclient.Client(version, session=sess) else: return glanceclient.Client(version, **kskw)
def _initialize_tests(self): """Perform final initialization before tests get run.""" # Access the sentries for inspecting service units self.pxc_sentry = self.d.sentry['percona-cluster'][0] self.keystone_sentry = self.d.sentry['keystone'][0] self.rabbitmq_sentry = self.d.sentry['rabbitmq-server'][0] self.compute_sentry = self.d.sentry['nova-compute-proxy'][0] self.nova_cc_sentry = self.d.sentry['nova-cloud-controller'][0] self.glance_sentry = self.d.sentry['glance'][0] u.log.debug('openstack release val: {}'.format( self._get_openstack_release())) u.log.debug('openstack release str: {}'.format( self._get_openstack_release_string())) # Authenticate admin with keystone self.keystone_session, self.keystone = u.get_default_keystone_session( self.keystone_sentry, openstack_release=self._get_openstack_release()) # Authenticate admin with glance endpoint self.glance = glanceclient.Client('1', session=self.keystone_session) # Authenticate admin with nova endpoint self.nova = nova_client.Client(2, session=self.keystone_session)
def _get_versioned_client(self, api_version, args): endpoint = self._get_image_url(args) auth_token = args.os_auth_token if endpoint and auth_token: kwargs = { 'token': auth_token, 'insecure': args.insecure, 'timeout': args.timeout, 'cacert': args.os_cacert, 'cert': args.os_cert, 'key': args.os_key, } else: ks_session = loading.load_session_from_argparse_arguments(args) auth_plugin_kwargs = self._get_kwargs_to_create_auth_plugin(args) ks_session.auth = self._get_keystone_auth_plugin( ks_session=ks_session, **auth_plugin_kwargs) kwargs = {'session': ks_session} if endpoint is None: endpoint_type = args.os_endpoint_type or 'public' service_type = args.os_service_type or 'image' endpoint = ks_session.get_endpoint( service_type=service_type, interface=endpoint_type, region_name=args.os_region_name) return glanceclient.Client(api_version, endpoint, **kwargs)
def _get_versioned_client(self, api_version, args): endpoint = self._get_image_url(args) auth_token = args.os_auth_token auth_req = (hasattr(args, 'func') and utils.is_authentication_required(args.func)) if not auth_req or (endpoint and auth_token): kwargs = { 'token': auth_token, 'insecure': args.insecure, 'timeout': args.timeout, 'cacert': args.os_cacert, 'cert': args.os_cert, 'key': args.os_key, 'ssl_compression': args.ssl_compression } else: ks_session = session.Session.load_from_cli_options(args) auth_plugin_kwargs = self._get_kwargs_to_create_auth_plugin(args) ks_session.auth = self._get_keystone_auth_plugin( ks_session=ks_session, **auth_plugin_kwargs) kwargs = {'session': ks_session} if endpoint is None: endpoint_type = args.os_endpoint_type or 'public' service_type = args.os_service_type or 'image' endpoint = ks_session.get_endpoint( service_type=service_type, interface=endpoint_type, region_name=args.os_region_name) return glanceclient.Client(api_version, endpoint, **kwargs)
def get_hostname(self): auth = v3.Password( auth_url=self.auth_url, username=self.username, password=self.password, project_name=self.project_name, user_domain_id='default', project_domain_id='default', ) sess = session.Session(auth=auth) nova = novaclient.client.Client('2.1', session=sess) glance = glanceclient.Client('1', session=sess) image_name = glance.images.get( nova.servers.list(search_opts={'name': 'sandbox-nik'}) [0].image['id']).to_dict()['name'] if 'CentOS' in image_name: hostname = 'sandbox-centos' elif 'Oracle' in image_name: hostname = 'sandbox-oel' return hostname
def load_auth_clients(): auth_fields = { 'auth_url': os.environ['OS_AUTH_URL'], 'username': os.environ['OS_USERNAME'], 'password': os.environ['OS_PASSWORD'], 'project_name': os.environ['OS_PROJECT_NAME'], 'user_domain_name': os.environ['OS_USER_DOMAIN_NAME'], 'project_domain_name': os.environ['OS_PROJECT_DOMAIN_NAME'] } v3_auth = v3.Password(**auth_fields) ks_sess = keystone_session.Session(auth=v3_auth, verify=False) ks_client = keystone_client.Client(session=ks_sess) CLIENTS['keystone'] = ks_client gl_client = glanceclient.Client('2', session=ks_sess) CLIENTS['glance'] = gl_client nv_client = nova_client.Client(2, session=ks_sess) CLIENTS['nova'] = nv_client ir_client = ironic_client.get_client(1, insecure=True, os_ironic_api_version='1.46', **auth_fields) CLIENTS['ironic'] = ir_client ins_client = ironic_inspector_client.ClientV1(session=ks_sess) CLIENTS['ironic-inspector'] = ins_client
def _get_clients(os_project_name=None): ''' Returns all the clients objects for the specified project ''' project_name = os_project_name or os.environ['OS_PROJECT_NAME'] try: return __clients[project_name] except KeyError: auth = keystoneauth1.identity.v3.Password( user_domain_name=os.environ['OS_USER_DOMAIN_NAME'], username=os.environ['OS_USERNAME'], password=os.environ['OS_PASSWORD'], project_domain_name=os.environ['OS_PROJECT_DOMAIN_ID'], project_name=project_name, auth_url=os.environ['OS_AUTH_URL']) session = keystoneauth1.session.Session(auth=auth) keystone = keystoneclient.v3.client.Client(session=session) glance = glanceclient.Client('2', session=session) openstack = openstack_client.connect( auth_url=os.environ['OS_AUTH_URL'], project_name=project_name, username=os.environ['OS_USERNAME'], password=os.environ['OS_PASSWORD'], region_name=os.environ['OS_REGION_NAME'], app_name='invoke') __clients[project_name] = openstack, keystone, glance return __clients[project_name]
def __init__(self): """Initialize a nova client object.""" conf = cfg.CONF.service_credentials logger = None if cfg.CONF.nova_http_log_debug: logger = log.getLogger("novaclient-debug") logger.logger.setLevel(log.DEBUG) ks_session = keystone_client.get_session() self.nova_client = nova_client.Client( version=api_versions.APIVersion('2.1'), session=ks_session, # nova adapter options region_name=conf.region_name, interface=conf.interface, service_type=cfg.CONF.service_types.nova, logger=logger) self.glance_client = glanceclient.Client( version='2', session=ks_session, region_name=conf.region_name, interface=conf.interface, service_type=cfg.CONF.service_types.glance)
def _create_glance_client(context: context.RequestContext, netloc: str, use_ssl: bool) -> glanceclient.Client: """Instantiate a new glanceclient.Client object.""" params = {'global_request_id': context.global_id} if use_ssl and CONF.auth_strategy == 'noauth': params = {'insecure': CONF.glance_api_insecure, 'cacert': CONF.glance_ca_certificates_file, 'timeout': CONF.glance_request_timeout, 'split_loggers': CONF.split_loggers } if CONF.auth_strategy == 'keystone': global _SESSION if not _SESSION: config_options = {'insecure': CONF.glance_api_insecure, 'cacert': CONF.glance_ca_certificates_file, 'timeout': CONF.glance_request_timeout, 'cert': CONF.glance_certfile, 'key': CONF.glance_keyfile, 'split_loggers': CONF.split_loggers } _SESSION = ks_session.Session().load_from_options(**config_options) auth = service_auth.get_auth_plugin(context) params['auth'] = auth params['session'] = _SESSION scheme = 'https' if use_ssl else 'http' endpoint = '%s://%s' % (scheme, netloc) return glanceclient.Client('2', endpoint, **params)
def __init__(self, version, endpoint, token, cacert=None, *args, **kwds): Client.__init__(self, *args, **kwds) if has_openstack: self.client = glanceclient.Client(version, endpoint=endpoint, token=token, cacert=cacert)
def _get_image_client(self): """Get a Glance v1 client.""" if not self._g_client: self._g_client = glanceclient.Client('1', self._get_image_endpoint(), token=self._get_auth_token()) return self._g_client
def __init__(self, config): """ Initializes the service. :param config: `tempest.config.Config` object """ self.config = config # Determine the Images API version self.api_version = int(config.images.api_version) # We load the client class specific to the API version... if self.api_version == 1: import glanceclient import keystoneclient.v2_0.client auth_url = self.config.identity.auth_url.rstrip('tokens') keystone = keystoneclient.v2_0.client.Client( username=config.images.username, password=config.images.password, tenant_name=config.images.tenant_name, auth_url=auth_url) token = keystone.auth_token endpoint = keystone.service_catalog.url_for( service_type='image', endpoint_type='publicURL') self._client = glanceclient.Client('1', endpoint=endpoint, token=token) else: raise NotImplementedError
def _create_glance_client(context, host, port, use_ssl, version=1): """Instantiate a new glanceclient.Client object.""" params = {} if use_ssl: scheme = 'https' # https specific params params['insecure'] = CONF.glance_api_insecure params['ssl_compression'] = False else: scheme = 'http' if CONF.auth_strategy == 'keystone': # NOTE(isethi): Glanceclient <= 0.9.0.49 accepts only # keyword 'token', but later versions accept both the # header 'X-Auth-Token' and 'token' params['token'] = context.auth_token identity_headers = { 'X-Auth-Token': context.auth_token, 'X-User-Id': context.user, 'X-Tenant-Id': context.tenant, 'X-Roles': ','.join(context.roles), 'X-Identity-Status': 'Confirmed', 'X-Service-Catalog': json.dumps(context.service_catalog), } params['identity_headers'] = identity_headers endpoint = '%s://%s:%s' % (scheme, host, port) return glanceclient.Client(str(version), endpoint, **params)
def upload_image(self, image_name, image_checksum, image_hdl, set_image_property=None): endpoint = "http://127.0.0.1:9292" glance_client = glanceclient.Client('1', endpoint, token="asdf") try: for image in itertools.chain( glance_client.images.list(is_public=False), glance_client.images.list(is_public=True), ): if image.name == image_name and image_checksum == image_checksum: self._log.debug( "Found existing image in catalog, not re-uploading") return self._log.debug( 'Uploading image to catalog: {}'.format(image_name)) image = glance_client.images.create(name=image_name, data=image_hdl, is_public="False", disk_format="qcow2", container_format="bare", checksum=image_checksum, properties=set_image_property) self._log.debug('Image upload complete: %s', image) except Exception as e: raise ImageUploadError("Failed to upload image to catalog: %s" % str(e)) from e
def get_glance_client(ksclient, endpoint): # hard-code v1 glance API version selection while v2 API matures service_credentials = cfg.CONF.service_credentials return glanceclient.Client('1', endpoint, token=ksclient.auth_token, cacert=service_credentials.os_cacert, insecure=service_credentials.insecure)
def image_info(self): """Return image ids for the user-defined image names @returns {'image_id' id1, 'image_alt_id': id2} """ self.init_keystone_client() image_info = {} try: glance_endpoint = self.kc.service_catalog.url_for( service_type='image', endpoint_type='publicURL') glance_client = glanceclient.Client('2', glance_endpoint, token=self.kc.auth_token) for image in glance_client.images.list(): if self.uconfig.get('glance-image-name') == image.name: image_info['image_id'] = image.id if self.uconfig.get('image-ssh-user'): image_info['image_ssh_user'] = \ self.uconfig.get('image-ssh-user') if self.uconfig.get('glance-alt-image-name') == image.name: image_info['image_alt_id'] = image.id if self.uconfig.get('image-alt-ssh-user'): image_info['image_alt_ssh_user'] = \ self.uconfig.get('image-alt-ssh-user') except: hookenv.log("Glance is not ready, deferring glance query") return image_info
def __init__(self): if not (CONF.glance.username or CONF.glance.user_id): raise exception.GlanceMissingConfiguration( flags=["username", "user_id"]) elif not (CONF.glance.tenant_name or CONF.glance.tenant_id): raise exception.GlanceMissingConfiguration( flags=["tenant_name", "tenant_id"]) elif not (CONF.glance.endpoint or CONF.glance.auth_url): raise exception.GlanceMissingConfiguration( flags=["endpoint", "auth_url"]) self.ks_session = None self.ks_client = None self.token, self.endpoint = self._get_token_and_endpoint() self.client = glanceclient.Client('2', endpoint=self.endpoint, token=self.token, insecure=CONF.glance.insecure) try: self.json_mapping = json.loads( open(CONF.glance.mapping_file).read()) except ValueError: raise exception.GlanceInvalidMappingFIle( file=CONF.glance.mapping_file, reason="Bad JSON.") # Format is not defined in the spec. What is format? Maybe it is the # container format? Or is it the image format? Try to do some ugly # magic and infer what is this for... # This makes me sad :-( LOG.debug("The image spec is broken and I will try to guess what " "the container and the image format are. I cannot " "promise anything.")
def glance_client(context, region_name=None): # We should allow glance to get the endpoint from the service # catalog, but to do so we would need to be able to specify # the endpoint_filter on the API calls, but glance # doesn't currently allow that. As a result, we must # specify the endpoint explicitly. if CONF.glance_url: endpoint_url = '%(url)s%(tenant)s' % { 'url': normalize_url(CONF.glance_url), 'tenant': context.project_id } else: region = region_name or CONF.service_credentials.region_name endpoint_url = get_endpoint(context.service_catalog, service_type=CONF.glance_service_type, endpoint_region=region, endpoint_type=CONF.glance_endpoint_type) auth = v3.Token(CONF.service_credentials.auth_url, context.auth_token) session = ka_session.Session(auth=auth) return glanceclient.Client(CONF.glance_client_version, endpoint=endpoint_url, session=session)
def _connect_to_glance_by_auth(*args, **kwargs): """ Use this for new Openstack Clouds """ version = '2' glance = glanceclient.Client(version, **kwargs) return glance
def _create_glance_client(context, host, port): """Instantiate a new glanceclient.Client object""" params = {} if FLAGS.auth_strategy == 'keystone': params['token'] = context.auth_token endpoint = 'http://%s:%s' % (host, port) return glanceclient.Client('1', endpoint, **params)
def __init__(self, session): self._session = session self._img_cli = glanceclient.Client(session=self._session, version=2) self._obj_cli = swiftclient.Connection(session=self._session, cacert=SYSTEM_CA_BUNDLE) self._ks = keystoneclient.v3.Client(session=session) self._stores = None
def get_glance_client(ksclient): endpoint = ksclient.service_catalog.url_for( service_type='image', endpoint_type=cfg.CONF.service_credentials.os_endpoint_type) # hard-code v1 glance API version selection while v2 API matures return glanceclient.Client('1', endpoint, token=ksclient.auth_token)
def activate_image(dest_session, image_id, deactivate=False): token = dest_session.get_token() glanceendpoint = dest_session.get_endpoint(service_type='image') gclient = glanceclient.Client('1', glanceendpoint, token=token) image = gclient.images.get(image_id) # Because the glance devs can't be bothered to update their python # bindings when new features are added, we have to do this the # old-fashioned way. if deactivate: action = 'deactivate' if image.status == 'deactivated': # Nothing to do return False print("deactivating image %s" % image_id) else: action = 'reactivate' if image.status == 'active': # Nothing to do return False print("activating image %s" % image_id) url = "%s/v2/images/%s/actions/%s" % (glanceendpoint, image_id, action) resp = requests.post(url, headers={'X-Auth-Token': token}) if resp: return True else: raise Exception("Image manipulation got status: " + resp.status_code)
def _get_images(self): try: glance = glanceclient.Client('2', session=self.sess) except Exception as exc: logger.warning("unable to create glance client") logger.warning(exc) return None image_list = () # Do things with images try: for image in glance.images.list(): img_checksum = "No Checksum" img_id = image['id'] img_name = image['name'] img_disk_format = image['disk_format'] img_containter_format = image['container_format'] img_visibility = image['visibility'] if image['checksum'] is not None: img_checksum = image['checksum'] image_list += ((self.project, img_name, img_id, img_disk_format, \ img_containter_format, img_visibility, img_checksum, self.alias),) return image_list except Exception as exc: logger.warning( "Error proccessing images via glance client, bad url or connection failure:" ) logger.warning(exc) return None
def _create_glance_client(context, host, port, use_ssl, version=1): """Instantiate a new glanceclient.Client object.""" params = {} if use_ssl: scheme = 'https' # https specific params params['insecure'] = CONF.glance.api_insecure params['ssl_compression'] = False sslutils.is_enabled(CONF) if CONF.ssl.cert_file: params['cert_file'] = CONF.ssl.cert_file if CONF.ssl.key_file: params['key_file'] = CONF.ssl.key_file if CONF.ssl.ca_file: params['cacert'] = CONF.ssl.ca_file else: scheme = 'http' if CONF.auth_strategy == 'keystone': # NOTE(isethi): Glanceclient <= 0.9.0.49 accepts only # keyword 'token', but later versions accept both the # header 'X-Auth-Token' and 'token' params['token'] = context.auth_token params['identity_headers'] = generate_identity_headers(context) if netutils.is_valid_ipv6(host): # if so, it is ipv6 address, need to wrap it with '[]' host = '[%s]' % host endpoint = '%s://%s:%s' % (scheme, host, port) return glanceclient.Client(str(version), endpoint, **params)
def _get_versioned_client(api_version, args): endpoint = _get_image_url(args) auth_token = args.os_auth_token description = "glance-v%s" % (api_version) ks_session = None if endpoint and auth_token: kwargs = { 'token': auth_token, 'insecure': args.insecure, 'timeout': args.timeout, 'cacert': args.os_cacert, 'cert': args.os_cert, 'key': args.os_key, 'ssl_compression': args.ssl_compression } description += " using auth_token" else: kwargs = _get_kwargs_for_create_session(args) ks_session, ks_desc = _get_keystone_session(**kwargs) kwargs = {'session': ks_session} description += ks_desc if endpoint is None: endpoint_type = args.os_endpoint_type or 'public' service_type = args.os_service_type or 'image' endpoint = ks_session.get_endpoint(service_type=service_type, interface=endpoint_type, region_name=args.os_region_name) return glanceclient.Client(api_version, endpoint, **kwargs), description
def __init__(self, conf): """Initialize a nova client object.""" creds = conf.service_credentials logger = None if conf.nova_http_log_debug: logger = log.getLogger("novaclient-debug") logger.logger.setLevel(log.DEBUG) ks_session = keystone_client.get_session(conf) self.nova_client = nova_client.Client( version=api_versions.APIVersion('2.1'), session=ks_session, # nova adapter options region_name=_get_region_name(conf, conf.service_types.nova), endpoint_type=creds.interface, service_type=conf.service_types.nova, logger=logger) self.glance_client = glanceclient.Client( version='2', session=ks_session, region_name=_get_region_name(conf, conf.service_types.glance), interface=creds.interface, service_type=conf.service_types.glance) # TO DO (dbadea): remove condition after updating nova_client if hasattr(self.nova_client, 'set_timings_max_len'): self.nova_client.set_timings_max_len( conf.nova_client.max_timing_buffer)
def _create_glance_client(self): token = flask.request.headers.get('X-Meta-Auth-Token') endpoint = flask.request.headers.get('X-Meta-Glance-Endpoint') if not token: token = self._get_auth_token() if not endpoint: endpoint = self._get_endpoint() return glanceclient.Client('1', endpoint=endpoint, token=token)
def _glanceclient_from_endpoint(context, endpoint, version): sess, auth = _session_and_auth(context) return glanceclient.Client(version, session=sess, auth=auth, endpoint_override=endpoint, global_request_id=context.global_id)