def get_glance_client(self): if not self.glance_client_v1 or not self.glance_client_v2: auth = v3.Password(auth_url=self.conf['auth_url'], username=self.conf['admin_user'], password=self.conf['admin_pwd'], user_domain_name=self.conf['user_domain'], project_name=self.conf['admin_tenant'], project_domain_name=self.conf['project_domain']) self.glance_client_v1 = Client( '1', session=ksc_session.Session(auth=auth)) self.glance_client_v2 = Client( '2', session=ksc_session.Session(auth=auth)) return self.glance_client_v1, self.glance_client_v2
def __init__(self): # FIXME: Replace group name session = ks_loading.load_session_from_conf_options(cfg.CONF, group='nova') auth = ks_loading.load_auth_from_conf_options(cfg.CONF, group='nova') self.glance = Client('2', session=session, auth=auth)
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.tenant } else: endpoint_url = get_endpoint(context.service_catalog, service_type=CONF.glance_service_type, endpoint_region=region_name or CONF.os_region_name, endpoint_type=CONF.glance_endpoint_type) auth = v3.Token(CONF.trove_auth_url, context.auth_token) session = ka_session.Session(auth=auth) return Client(CONF.glance_client_version, endpoint=endpoint_url, session=session)
def __init__(self, region, context): """Create Glance Object. :param region: Region to which glance object has to be created. :param context: context object. """ try: LOG.info('Creating Glance Object') self.keystone_client = KeystoneClient() service_id = [ service.id for service in self.keystone_client.services_list if service.type == 'image' ][0] glance_endpoint = [ endpoint.url for endpoint in self.keystone_client.endpoints_list if endpoint.service_id == service_id and endpoint.region == region and endpoint.interface == 'public' ][0] params = dict() params['identity_headers'] = self.generate_identity_headers( context) self.glance_client = Client(str(API_VERSION), glance_endpoint, **params) except exceptions.ServiceUnavailable: raise
def upload_to_glance(self): api_version = '2' glance_data = cfme_data['template_upload'][self.glance_server] creds_key = glance_data['credentials'] loader = loading.get_plugin_loader('password') auth = loader.load_from_options( auth_url=glance_data['auth_url'], username=credentials[creds_key]['username'], password=credentials[creds_key]['password'], tenant_name=credentials[creds_key]['tenant']) glance_session = session.Session(auth=auth) glance = Client(version=api_version, session=glance_session) for img in glance.images.list(): if img.name == self.template_name: logger.info( "(template-upload) [%s:%s:%s] Template already exists on Glance.", self.log_name, self.provider, self.template_name) return True glance_image = glance.images.create(name=self.template_name, container_format='bare', disk_format='qcow2', visibility="public") glance.images.add_location(glance_image.id, self.image_url, {}) if glance_image: logger.info("{}:{} Successfully uploaded template: {}".format( self.log_name, self.provider, self.template_name)) return True
def test_v2_requests_bad_cert(self, __): """Test VerifiedHTTPSConnection: absence of SSL key file.""" port = self.port url = 'https://0.0.0.0:%d' % port cert_file = os.path.join(TEST_VAR_DIR, 'badcert.crt') cacert = os.path.join(TEST_VAR_DIR, 'ca.crt') try: gc = Client('2', url, insecure=False, ssl_compression=False, cert_file=cert_file, cacert=cacert) gc.images.get('image123') except exc.CommunicationError as e: # NOTE(dsariel) # starting from python 2.7.8 the way to handle loading private # keys into the SSL_CTX was changed and error message become # similar to the one in 3.X if (six.PY2 and 'PrivateKey' not in e.message and 'PEM lib' not in e.message or six.PY3 and 'PEM lib' not in e.message): self.skipTest('Skipped by Debian') except Exception as e: self.fail('Unexpected exception has been raised')
def del_images(sess, image_name): glance = Client('2', session=sess) images = glance.images.list() for image in images: if image.name == image_name: glance.images.delete(image.id) LOG.info('Image %s has been deleted' % image_name)
def upload_to_glance(image, image_name_in_glance, provider, disk_format): """ Upload iso/qcow2/ova images to Glance. """ api_version = '2' # python-glanceclient API version provider_dict = cfme_data['management_systems'][provider] creds_key = provider_dict['credentials'] loader = loading.get_plugin_loader('password') auth = loader.load_from_options( auth_url=provider_dict['auth_url'], username=credentials[creds_key]['username'], password=credentials[creds_key]['password'], tenant_name=credentials[creds_key]['tenant']) glance_session = session.Session(auth=auth) glance = Client(api_version, session=glance_session) # Two images on Glance could have the same name since Glance assigns them different IDS. # So, we are running a check to make sure an image with the same name doesn't already exist. for img in glance.images.list(): if img.name == image_name_in_glance: print("Image already exists on Glance server") sys.exit(127) glance_img = glance.images.create(name=image_name_in_glance) # Update image properties before uploading the image. glance.images.update(glance_img.id, container_format="bare") glance.images.update(glance_img.id, disk_format=disk_format) glance.images.update(glance_img.id, visibility="public") glance.images.upload(glance_img.id, open(image, 'rb'))
def post(self, request, imageid): loader = loading.get_plugin_loader('password') auth = loader.load_from_options( auth_url = AUTH_URL, username = USERNAME, password = PASSWORD, project_id = PROJECT_ID ) try: keystone_session = session.Session(auth = auth) except Exception as e: return Response(data={'error': str(e)}, status=e.http_status) client = Client('2', session = keystone_session) img = client.images.data(imageid) image_detail = client.images.get(imageid) file_name = "/Users/bins/Documents/project/%s.%s" % (image_detail.name, image_detail.disk_format) image_file = open(file_name, 'w+') for chunk in img: image_file.write(chunk) image_file.close() return Response(data={'status': 'OK'}, status=status.HTTP_200_OK)
def glance_upload(self): """Push template to glance server if session is true, use keystone auth session from self.mgmt if session is false, use endpoint directly: 1. download template to NFS mounted share on glance server via ssh+wget 2. create image record in glance's db 3. update image record with the infra-truenas webdav URL """ if self.provider_type == 'openstack': # This means its a full openstack provider, and we should use its mgmt session client_kwargs = dict(session=self.mgmt.session) else: # standalone glance server indirectly hosting image to be templatized client_kwargs = dict(endpoint=self.from_template_upload(self.glance_key).get('url')) client = Client(version='2', **client_kwargs) if self.image_name in [i.name for i in client.images.list()]: logger.info('Image "%s" already exists on %s, skipping glance_upload', self.image_name, self.provider_key) return True glance_image = client.images.create( name=self.template_name if self.provider_type == 'openstack' else self.image_name, container_format='bare', disk_format='qcow2', visibility='public') if self.template_upload_data.get('remote_location'): # add location for image on standalone glance client.images.add_location(glance_image.id, self.raw_image_url, {}) else: if self.download_image(): client.images.upload(glance_image.id, open(self.local_file_path, 'rb')) else: return False return True
def __init__(self, session, region, avail_zone): self.session = session self.avail_zone = avail_zone self.tmp_dir = helpers.make_tmp_dir() self.glance = Client("2", session=session, region_name=region)
def init_glance_client(endpoint, token, version='1'): global glance try: glance = Client(version, endpoint=endpoint, token=token) LOG.info("glanceclient init succeed.") except Exception as e: LOG.error("glanceclient init failed.%s" % e) return
def list_images(sess): LOG.info('Listing images:') glance = Client('2', session=sess) images = glance.images.list() for image in images: LOG.info(('+ {name} container_format:{container_format} ' 'disk_format:{disk_format} visibility:{visibility} ' 'file:{file}').format(**image))
def make_image_public(username, image_id, region_name): sess = admin_session(region_name) glance = Client('2', session=sess) logger.info( ('User: {} requesting visibility=public for image id: {} in region: {}' .format(username, image_id, region_name))) glance.images.update(image_id, visibility='public') logger.info(('User: {} image status after update: {}'.format( username, glance.images.get(image_id))))
def __init__(self, version): self.keystone_cl = keystone.create_keystone() if version not in ["1", "2"]: raise ArgumentError("Invalid glance version choice") url_for = self.keystone_cl.service_catalog.url_for glance_endpt = url_for(service_type="image", endpoint_type="publicURL") + "/v" + version self.glance_session = GlanceFactory(endpoint=glance_endpt, token=self.keystone_cl.auth_token)
def __init__(self, label, **kwargs): loader = loading.get_plugin_loader('password') auth = loader.load_from_options(**kwargs) session2 = session.Session(auth=auth) LOG.info('Creating Glance client for %s using keystone: %s', label, kwargs.pop('auth_url')) self.glance = Client('2', session=session2)
def get_glance_client(): loader = loading.get_plugin_loader('password') auth = loader.load_from_options(auth_url=AUTH_URL, username=USERNAME, password=PASSWORD, project_name=PROJECT_NAME, user_domain_name=DOMAIN_NAME, project_domain_name=DOMAIN_NAME) sess = session.Session(auth=auth) return Client('2', endpoint=GLANCE_URL, session=sess)
def get_glance_client(): """get OpenStack glance client""" loader = loading.get_plugin_loader('password') auth = loader.load_from_options(auth_url=OS_AUTH_URL, username=OS_USERNAME, password=OS_PASSWORD, project_name=OS_PROJECT_NAME, user_domain_id=OS_USER_DOMAIN_NAME, project_domain_id=OS_PROJECT_DOMAIN_NAME) sess = session.Session(auth=auth) return Client('2', session=sess)
def add_image(sess, image_name, vm_mode, image_file): glance = Client('2', session=sess) image = glance.images.create(name=image_name, container_format="ovf", disk_format="vhd", visibility="public", vm_mode=vm_mode) with open(image_file, 'rb') as f: glance.images.upload(image.id, f) LOG.info('Image %s (mode: %s, file: %s) has been added' % (image_name, vm_mode, image_file))
def glance_session(auth_url, tenant_id, username, password, version=2): loader = loading.get_plugin_loader('password') auth = loader.load_from_options(auth_url=auth_url, tenant_id=tenant_id, username=username, password=password) mysession = session.Session(auth=auth) # the Python API is not fully compatible with v2 yet, # e.g. update() is spotty return Client(version, session=mysession)
def get_glance_client(auth_info): loader = loading.get_plugin_loader('password') auth = loader.load_from_options( auth_url=auth_info.get('auth_url'), username=auth_info.get('username'), password=auth_info.get('password'), project_name=auth_info.get('project_name'), user_domain_name=auth_info.get('user_domain_name'), project_domain_name=auth_info.get('project_domain_name')) sess = session.Session(auth=auth) return Client('2', session=sess)
def __init__(self, username=None, password=None, project_name=None, token=None, admin=False): super().__init__(username, password, project_name, token, admin) self.nova = client.Client(2, session=self.sess) self.neutron = client_neutron.Client(session=self.sess) self.cinder = client_cinder.Client(3, session=self.sess) self.glance = Client(2, session=self.sess) try: self.services = self.nova.services.list() self.hypervisors = self.nova.hypervisors.list() except Exception as e: logger.warning(e)
def glanceAuth(): # Authentication & Session Creation auth = identity.V3Password(auth_url=url, username=usern, user_domain_name='Default', password=passw, project_name=proj, project_domain_name='Default') sess = session.Session(auth=auth) #sk = client.Client(session=sess) glance = Client('2', session=sess) return glance
def glance_client(): auth = identity.Password(auth_url='http://' + str(YOUR_IP) + ':' + KEYSTONE_ADMIN_PORT + '/v2.0', username=ADMIN_USER, password=ADMIN_PASS, tenant_name=TENANT_NAME) sess = session.Session(auth=auth) token = auth.get_token(sess) return Client('2', endpoint='http://' + YOUR_IP + ':' + GLANCE_PORT, token=token)
def test_v1_requests_cert_verification(self): """v1 regression test for bug 115260.""" port = self.port url = 'https://0.0.0.0:%d' % port try: client = Client('1', url, insecure=False, ssl_compression=True) client.images.get('image123') self.fail('No SSL exception raised') except exc.CommunicationError as e: if 'certificate verify failed' not in e.message: self.fail('No certificate failure message received') except Exception as e: self.fail('Unexpected exception raised')
def __init__(self, label, auth_url, username, password, project_name): loader = loading.get_plugin_loader('password') auth = loader.load_from_options(auth_url=auth_url, username=username, password=password, project_name=project_name) session2 = session.Session(auth=auth) LOG.info( 'Creating Glance client for %s with parameters: ' 'keystone_endpoint: %s, username: %s, project_name: %s', label, auth_url, username, project_name) self.glance = Client('2', session=session2)
def get_glance(): """ get glance - create the glance object @ params none @ returns - glance object """ kscreds = get_keystone_creds() keystone = keystoneclient.v2_0.client.Client(**kscreds) auth_token = keystone.auth_token image_url = keystone.service_catalog.url_for(service_type='image', endpoint_type='publicURL') image_url = re.sub(r'/v2/$', '', image_url, flags=re.IGNORECASE) glance = Client('2', endpoint=image_url, token=auth_token) return glance
def test_v2_requests_cert_verification_no_compression(self): """v2 regression test for bug 115260.""" self._skip_python3() port = self.port url = 'https://0.0.0.0:%d' % port try: gc = Client('2', url, insecure=False, ssl_compression=False) gc.images.get('image123') self.fail('No SSL exception raised') except SSL.Error as e: if 'certificate verify failed' not in str(e): self.fail('No certificate failure message received') except Exception as e: self.fail('Unexpected exception raised')
def upload_template(self): version = '1' try: glance = Client(version=version, session=self.mgmt.session) glance_image = glance.images.create(name=self.template_name, container_format='bare', disk_format='qcow2', is_public=True, copy_from=self.image_url) if glance_image: return True except: return False
def cluster_connect(self, target_cluster): """ Method to connect to the openstack cluster """ cluster = self.clusters[target_cluster] os.environ["REQUESTS_CA_BUNDLE"] = cluster["ca_cert"] loader = loading.get_plugin_loader('password') auth = loader.load_from_options( auth_url=cluster["auth_url"], username=cluster["username"], password=cluster["password"], project_name=cluster["project_name"], user_domain_id=cluster["user_domain_id"], project_domain_id=cluster["project_domain_id"]) sess = session.Session(auth=auth) glance = Client('2', session=sess) return glance