def _get_client(project_id): user = settings.KEYSTONE_USER password = settings.KEYSTONE_PASSWORD auth_url = settings.OPENSTACK_KEYSTONE_URL keystone = ksclient.Client( auth_url=auth_url, username=user, password=password, tenant_name=project_id) token = keystone.auth_ref['token']['id'] for catalog in keystone.auth_ref['serviceCatalog']: if catalog['name'] == 'glance': for endpoint in catalog['endpoints']: epoint = endpoint['publicURL'] c = client.Client("1", endpoint=epoint, token=token) return c
def glance(context): if glanceclient is None: return None return glanceclient.Client('1', service_type='image', session=context.session)
def authenticate(): """ This function returns authenticated nova and glance objects """ try: auth = v3.Password( username=os.environ['OS_USERNAME'], password=os.environ['OS_PASSWORD'], auth_url=os.environ['OS_AUTH_URL'], project_name=os.environ['OS_TENANT_NAME'], user_domain_name='Default', project_domain_name='Default', ) except Exception as e: print('Auth token failed. ' + str(e)) sys.exit(2) try: sess = session.Session(auth=auth) except: print('session failed.') sys.exit(3) try: glance = glance_client.Client('2', session=sess) except: print('glance failed') sys.exit(4) return glance
def test_do_details(self): class Image(): id = 'test' filters = {} limit = 18 marker = False sort_key = 'test' kwarg = 'name' sort_dir = 'test' name = 'test' disk_format = 'ovi' container_format = 'ovi' size = 1024 is_public = True protected = False status = 'active' min_ram = 512 min_disk = 512 properties = {} created_at = '12.12.12' args = Image() gc = client.Client('1', 'http://is.invalid') with mock.patch.object(gc.images, 'list') as mocked_list: mocked_list.return_value = [Image(), Image()] actual = test_shell.do_details(gc, args)
def setUpClass(cls): super(ImageTestCase, cls).setUpClass() glance_endpoint = cls.service_catalog.url_for(service_type='image') cls.glance = gclient.Client('1', endpoint=glance_endpoint, session=cls.keystone_client.session) cls.image_title = 'New Image ' + str(time.time()) cls.image = cls.upload_image(cls.image_title)
def __init__(self, ks_session, network_ks_session=None, nova_api=None, nova_version=NOVA_VERSION, neutron_api=None, glance_api=None, glance_version=GLANCE_VERSION): # This is the keystone session that we use for spawning instances, # aka our "service tenant" user. self._ks_session = ks_session # And this is the keystone session that we use for finding the network # that we are going to plumb into, aka the "end user". if network_ks_session is not None: self._network_ks_session = network_ks_session else: self._network_ks_session = ks_session # Yes, we really want both of these to use the "service tenant". self._nova_api = nova_api or nova_client.Client( nova_version, session=self._ks_session) self._neutron_api = neutron_api or neutron_client.Client( NEUTRON_VERSION, session=self._ks_session) self._glance_api = glance_api or glance_client.Client( glance_version, session=self._ks_session)
def test_do_update_dry_run_false(self): class Image(): fields = ['id=test_updated', 'status=active', 'is_public=True', 'name=new_name', 'protected=False', 'is_public=True'] dry_run = False id = 'test' verbose = True is_public = True protected = False status = 'active' size = 1024 min_ram = 512 min_disk = 512 properties = {'property': 'test'} created_at = '12.09.2013' args = Image() gc = client.Client('1', 'http://is.invalid') with mock.patch.object(gc.images, 'update') as mocked_update: mocked_update.return_value = Image() actual = test_shell.do_update(gc, args) self.assertEqual(0, actual)
def __init__(self, testcase, template_file, distribution, arch, jeos_type, stack_paramstr, stackname=DEFAULT_STACKNAME): self.testcase = testcase self.stackname = stackname self.template_file = template_file self.distribution = distribution self.stack_paramstr = stack_paramstr self.stack_id_re = re.compile("^arn:openstack:heat::[0-9a-z]{32}:" + "stacks/" + self.stackname + "/[0-9]*$") self.creds = dict(username=os.environ['OS_USERNAME'], password=os.environ['OS_PASSWORD'], tenant=os.environ['OS_TENANT_NAME'], auth_url=os.environ['OS_AUTH_URL'], strategy=os.environ['OS_AUTH_STRATEGY']) self.dbusername = '******' self.testcase.assertEqual(os.environ['OS_AUTH_STRATEGY'], 'keystone', 'keystone authentication required') self.glanceclient = glance_client.Client(host="0.0.0.0", port=9292, use_ssl=False, auth_tok=None, creds=self.creds) self.prepare_jeos(distribution, arch, jeos_type) self.novaclient = nova_client.Client(self.creds['username'], self.creds['password'], self.creds['tenant'], self.creds['auth_url'], service_type='compute') self.heatclient = self._create_heat_client()
def gen_os_clients(self): try: output = {} # Grab handles to each client type keystone = kclient.Client(session=self.os['sess']) output['keystone'] = {} output['keystone']['client'] = keystone glance = gclient.Client(2, session=self.os['sess']) output['glance'] = {} output['glance']['client'] = glance nova = nclient.Client(2, session=self.os['sess']) output['nova'] = {} output['nova']['client'] = nova neutron = neuclient.Client(2, session=self.os['sess']) output['neutron'] = {} output['neutron']['client'] = neutron return 1, output except: logging.error("return_keystone_sess: Issue getting Openstack \ session object") return 0, {}
def volume_from_image(image_id, creds, glance_url, volume_size=None): k = keystone_client.Client(username=creds['username'], password=creds['password'], tenant_name=creds['tenant'], auth_url=creds['auth_url']) if not k.authenticate(): raise Exception("Could not authenticate into keystone") glance = glance_client.Client("1", endpoint=glance_url, token=k.auth_token) cinder = cinder_client.Client('1', creds['username'], creds['password'], creds['tenant'], creds['auth_url']) try: image = glance.images.get(image_id) except: raise Exception("Could not find Glance image with id" % (image_id)) # Unclear if this is strictly needed # If size is not explicitly set then set it based on the image size # TODO: Check if we even have to set a size when pulling from an image if not volume_size: # Gigabytes rounded up volume_size = int(image.size / (1024 * 1024 * 1024) + 1) print "Starting asyncronous copying to Cinder" volume = cinder.volumes.create(volume_size, display_name=image.name, imageRef=image.id) while (volume.status != 'available'): print "Waiting for volume to be ready ... current status (%s)" % ( volume.status) sleep(5) volume = cinder.volumes.get(volume.id) if (volume.status == 'error'): raise Exception('Error converting image to volume') return volume
def run(self, args): # Here is the core of the plugin. # After doing your verifications, escape by doing: # self.exit(return_code, 'return_message', *performance_data) perfdata = [] try: # Get a keystone token keystone = keystone_client.Client( username=args['username'], tenant_name=args['tenant'], password=args['password'], auth_url=args['auth_url'], ) # Auth with glance start_time = datetime.datetime.now() client = glance_client.Client( auth_url=args['auth_url'], username=args['username'], tenant=args['tenant'], endpoint=args['endpoint'], host=args.get('host'), token=keystone.auth_token, ) end_time = datetime.datetime.now() perfdata.append( PerfData('auth_time', ((end_time - start_time).total_seconds() / 1000), min_='0', unit='ms')) except Exception as e: self.exit(STATES.UNKNOWN, str(e)) # Get the images images = [image for image in client.images.list()] # Get the image count image_count = len(images) perfdata.append( PerfData('image_count', image_count, min_=(args.get('req_count')))) # Check the count of images if args.get('req_count') and image_count < args.get('req_count'): self.exit( STATES.CRITICAL, 'Not enough images (%s < %s)' % (image_count, args.get('req_count')), *perfdata) # Check the required images missing_images = [] if args.get('req_images'): for image in args.get('req_images'): if image not in images: missing_images.append(image) if len(missing_images) > 0: self.exit(STATES.CRITICAL, 'Images: %s are missing' % ' '.join(missing_images), *perfdata) self.exit(STATES.OK, 'OK - %s images found' % image_count, *perfdata)
def glance_upload( image_filename=None, image_url=None, creds={ 'auth_url': None, 'password': None, 'strategy': 'noauth', 'tenant': None, 'username': None }, glance_url=None, token=None, name='Factory Test Image', disk_format='raw'): k = keystone_client.Client(username=creds['username'], password=creds['password'], tenant_name=creds['tenant'], auth_url=creds['auth_url']) if (k.authenticate()): #Connect to glance to upload the image glance = glance_client.Client("1", endpoint=glance_url, token=k.auth_token) image_meta = { 'container_format': 'bare', 'disk_format': disk_format, 'is_public': True, 'min_disk': 0, 'min_ram': 0, 'name': name, 'properties': { 'distro': 'rhel' } } try: image = glance.images.create(name=name) if image_filename: image_data = open(image_filename, "r") image_meta['data'] = image_data print "Uploading to Glance" image.update(**image_meta) elif image_url: image_meta['copy_from'] = image_url image.update(**image_meta) print "Waiting for Glance to finish creating image from URL: %s" % ( image_url) while (image.status != 'active'): if image.status == 'killed': raise Exception( "Glance error while waiting for image to generate from URL" ) print '.', sys.stdout.flush() sleep(10) image = glance.images.get(image.id) return image except Exception, e: raise
def mock_glance(self): glance_patcher = mock.patch('glanceclient.client.Client') with mock.patch('glanceclient.v2.schemas.Controller'): glance = mock.create_autospec(glanceclient.Client(endpoint='v2')) glance_patcher.start().return_value = glance self.addCleanup(glance_patcher.stop) return glance
def create_client(keystone_client, os_region_name, cacert): image_api_url = keystone_client.service_catalog.url_for( service_type='image', region_name=os_region_name) return glance_client_pkg.Client(GLANCE_VERSION, endpoint=image_api_url, token=keystone_client.auth_token, cacert=cacert)
def server_create(): if not request.json: abort(400) test_id = request.args.get('test_id') db = Session() sess = _create_session(int(test_id)) nova = nova_cli.Client(session=sess, version='2.1') glance = glance_cli.Client('2', session=sess) neutron = neutron_cli.Client(session=sess) flavor = nova.flavors.find(name=request.json['flavor']) image = list( glance.images.list(filters={"name": request.json['image']}))[0] network = neutron.find_resource('network', request.json['network']) server = nova.servers.create(name=request.json['name'], flavor=flavor.id, image=image.id, network=network['id']) if server: server_dao = _server_dao_create( db, server, test_id, image=_image_dao_create(db, image, test_id, True), flavor=_flavor_dao_create(db, flavor, test_id, True), network=_network_dao_create(db, network, test_id, True)) _create_test_log(db, test_id, 'Server CREATE %s' % repr(server_dao)) db.commit() return jsonify(_os_server_to_json(server)), 201 else: abort(401)
def list(host=None, last_changed=None, availability_zone=None, column=None, aggregate=None, status=None, sort_by=None, project=None, allocation_home=None, exclude_availability_zone=None, exclude_host=None, exclude_aggregate=None): """List all nova instances with given parameters.""" session = get_session() nova = nova_client.Client(2, session=session) glance = glance_client.Client(2, session=session) keystone = keystone_client.Client(session=session) allocation = allocation_client.Client(1, session=session) instances = _list_instances(nova, glance, keystone, allocation, aggregate, availability_zone, host, status, project, allocation_home, exclude_availability_zone, exclude_host, exclude_aggregate) # INSTAN = instances # embed() if not column: sort_by = 'OS-EXT-SRV-ATTR:host' _render_table_instances(instances, column, sort_by)
def server_rebuild(): test_id = request.args.get('test_id') if not request.json: abort(401) server_name = request.json['server'] image_name = request.json['image'] sess = _create_session(int(test_id)) nova = nova_cli.Client('2.1', session=sess) servers = nova.servers.list(search_opts={"name": server_name}) glance = glance_cli.Client('2', session=sess) images = list(glance.images.list(filters={"name": image_name})) if servers and len(servers) and images and len(images): server = servers[0] image = images[0] db = Session() image_dao = _image_dao_create(db, image, test_id, True) server_dao = db.query(OSServer).filter( OSServer.uid == server.id).first() if not server_dao: abort(404) server_dao.image = image_dao server.rebuild(image) db.add(server_dao) db.commit() return jsonify(success=True) else: abort(401)
def stat(host=None, last_changed=None, availability_zone=None, aggregate=None, status=None, project=None, allocation_home=None, exclude_availability_zone=None, exclude_host=None, exclude_aggregate=None, detail=None): """Gather statistic of all nova instances with given parameters.""" session = get_session() nova = nova_client.Client(2, session=session) glance = glance_client.Client(2, session=session) keystone = keystone_client.Client(session=session) allocation = allocation_client.Client(1, session=session) instances = _list_instances(nova, glance, keystone, allocation, aggregate, availability_zone, host, status, project, allocation_home, exclude_availability_zone, exclude_host, exclude_aggregate) # Summary table table = PrettyTable(['Name', 'Value']) data = {'instances': 0, 'vcpus': 0, 'ram': 0} table.align = 'l' projects_counter = Counter() # Detail table dt = PrettyTable(['Project', 'Instances', 'vCPUs', 'RAM']) projects = defaultdict(lambda: defaultdict(int)) dt.align = 'l' for ins in instances: project_name = ins._info['project'].name projects[project_name]['instances'] += 1 data['instances'] += 1 projects_counter[ins._info['project'].name] += 1 if not ins._info['flavor']['vcpus']: continue data['vcpus'] += int(ins._info['flavor']['vcpus']) data['ram'] += int(ins._info['flavor']['ram']) projects[project_name]['vcpus'] += int(ins._info['flavor']['vcpus']) projects[project_name]['ram'] += int(ins._info['flavor']['ram']) data['common'] = ',\n'.join('%s: %d' % (k, v) for k, v in projects_counter.most_common(3)) # Convert the data to bytes for humanization data['ram'] = data['ram'] * 1024 * 1024 table.add_row(['Total instances', data['instances']]) table.add_row(['vCPUs used', data['vcpus']]) table.add_row(['RAM used', humanize.naturalsize(data['ram'], binary=True)]) table.add_row(['Total projects affected', len(projects_counter.keys())]) table.add_row(['Top projects affected', data['common']]) click.echo(table) if detail: for name, p in projects.items(): dt.add_row([ name, p['instances'], p['vcpus'], humanize.naturalsize(p['ram'] * 1024 * 1024, binary=True) ]) click.echo(dt)
def test_do_add_without_dry_run(self): gc = client.Client('1', 'http://is.invalid') class FakeImage(): fields = ['name=test', 'status=active', 'is_public=True', 'id=test', 'protected=False', 'min_disk=10', 'container_format=ovi', 'status=active', 'size=256', 'location=test', 'checksum=1024', 'owner=test_user'] dry_run = False id = 'test' verbose = False test_args = FakeImage() with mock.patch.object(gc.images, 'create') as mocked_create: mocked_create.return_value = FakeImage() actual = test_shell.do_add(gc, test_args) self.assertEqual(0, actual)
def loginTenant(request, tenant_name): """ Create keystone, nova, and glance clients for tenant; on tenant selection """ username = request.session['username'] password = request.session['password'] auth_url = 'https://engage1.massopencloud.org:5000/v3/' print 'lucas-test-auth-loginTenant' unscoped_auth = v3.Password(auth_url=auth_url, username=username, password=password, user_domain_name="Default", unscoped=True) unscoped_sess = session.Session(auth=unscoped_auth) unscoped_token = unscoped_sess.get_token() auth = v3.Token(auth_url=auth_url, token=unscoped_token) sess = session.Session(auth=auth) #scoped_token=sess.get_token() keystone = ksclient.Client(session=sess) # keystone = ksclient.Client(auth_url = 'https://engage1.massopencloud.org:5000/v2.0/', username = username, # password = password, tenant_name = tenant_name) print 'lucas-test-auth-loginTenant-succesfully' # nova = nvclient.Client('2', auth_url = 'https://engage1.massopencloud.org:5000/v2.0/', # username = username, # api_key = password, # project_id = tenant_name) nova = nvclient.Client('2', session=sess) glance = glclient.Client('2', session=sess) return {'keystone': keystone, 'nova': nova, 'glance': glance}
def test_show(self): class Image(): fields = ['id=test_updated', 'status=active', 'is_public=True', 'name=new_name', 'protected=False'] id = 'test_show' name = 'fake_image' is_public = False protected = False status = 'active' size = '1024' min_ram = 512 min_disk = 10 properties = {'a': 'b', 'c': 'd'} created_at = '04.03.2013' owner = 'test' updated_at = '04.03.2013' gc = client.Client('1', 'http://is.invalid') with mock.patch.object(gc.images, 'get') as mocked_get: mocked_get.return_value = Image() actual = test_shell.do_show(gc, Image()) self.assertEqual(0, actual)
def connect(config): # The Glance client is not able to query Keystone # for the endpoint, neither authenticate itself ksclient = keystone.Client(username=config['username'], tenant_name=config['tenant'], password=config['password'], auth_url=config['auth_url']) endpoint = ksclient.service_catalog.url_for( service_type='image', endpoint_type=config['endpoint_type'] ) # Strip version from the last component of endpoint if present # Get rid of trailing '/' if present if endpoint.endswith('/'): endpoint = endpoint[:-1] url_bits = endpoint.split('/') # regex to match 'v1' or 'v2.0' etc if re.match('v\d+\.?\d*', url_bits[-1]): endpoint = '/'.join(url_bits[:-1]) client = glance.Client('2', endpoint=endpoint, token=ksclient.auth_token) config['util'] = OpenstackUtils(client)
def wrapper(self, *args, **kwargs): """Wrapper around methods calls. :param image_href: href that describes the location of an image """ if self.client: return func(self, *args, **kwargs) image_href = kwargs.get('image_href') (image_id, self.glance_host, self.glance_port, use_ssl) = service_utils.parse_image_ref(image_href) if use_ssl: scheme = 'https' else: scheme = 'http' params = {} params['insecure'] = CONF.glance.glance_api_insecure if CONF.glance.auth_strategy == 'keystone': params['token'] = self.context.auth_token endpoint = '%s://%s:%s' % (scheme, self.glance_host, self.glance_port) self.client = client.Client(self.version, endpoint, **params) return func(self, *args, **kwargs)
def get_glance_client(cls, region, service_name=None, endpoint=None, endpoint_type='publicURL', insecure=False, cacert=None): """Create glance client object. :param region: The region of the service :param service_name: The name of the glance service in the catalog :param endpoint: The endpoint of the service :param endpoint_type: The endpoint_type of the service :param insecure: Turn off certificate validation :param cacert: CA Cert file path :return: a Glance Client object. :raises Exception: if the client cannot be created """ ksession = keystone.KeystoneSession() if not cls.glance_client: kwargs = {'region_name': region, 'session': ksession.get_session(), 'interface': endpoint_type} if service_name: kwargs['service_name'] = service_name if endpoint: kwargs['endpoint'] = endpoint if endpoint.startswith("https"): kwargs['insecure'] = insecure kwargs['cacert'] = cacert try: cls.glance_client = glance_client.Client( GLANCE_VERSION, **kwargs) except Exception: with excutils.save_and_reraise_exception(): LOG.exception("Error creating Glance client.") return cls.glance_client
def connect(self, config): ksclient = keystone.Client(username=config['username'], tenant_name=config['tenant'], password=config['password'], auth_url=config['auth_url']) compute_endpoint = ksclient.service_catalog.url_for( service_type='compute', endpoint_type=config['endpoint_type'] ) image_endpoint = ksclient.service_catalog.url_for( service_type='image', endpoint_type=config['endpoint_type'] ) nova_client = nova.Client('1.1', username=config['username'], auth_url=config['auth_url'], api_key='', project_id=config['tenant'], bypass_url=compute_endpoint, auth_token=ksclient.auth_token) glance_client = glance.Client('1', endpoint=image_endpoint, token=ksclient.auth_token) return nova_client, glance_client
def glance(self, context, version=1): """Instantiate a new glanceclient.Client object.""" params = {} params['token'] = context.auth_token params['identity_headers'] = self.generate_identity_headers(context) endpoint = CONF.glance_url return glance.Client(str(version), endpoint, **params)
def wrapper(self, *args, **kwargs): """Wrapper around methods calls. :param image_href: href that describes the location of an image """ if self.client: return func(self, *args, **kwargs) global _GLANCE_SESSION if not _GLANCE_SESSION: _GLANCE_SESSION = keystone.get_session('glance') # NOTE(pas-ha) glanceclient uses Adapter-based SessionClient, # so we can pass session and auth separately, makes things easier service_auth = keystone.get_auth('glance') self.endpoint = keystone.get_endpoint('glance', session=_GLANCE_SESSION, auth=service_auth) user_auth = None # NOTE(pas-ha) our ContextHook removes context.auth_token in noauth # case, so when ironic is in noauth but glance is not, we will not # enter the next if-block and use auth from [glance] config section if self.context.auth_token: user_auth = keystone.get_service_auth(self.context, self.endpoint, service_auth) self.client = client.Client(2, session=_GLANCE_SESSION, auth=user_auth or service_auth, endpoint_override=self.endpoint, global_request_id=self.context.global_id) return func(self, *args, **kwargs)
def setUp(self): super(ApiTestCase, self).setUp() neutron_patcher = mock.patch('neutronclient.v2_0.client.Client', autospec=True) self.neutron = neutron_patcher.start().return_value self.addCleanup(neutron_patcher.stop) nova_patcher = mock.patch('novaclient.client.Client') self.nova = mock.create_autospec(self.NOVACLIENT_SPEC_OBJ) self.novaclient_getter = nova_patcher.start() self.novaclient_getter.return_value = self.nova self.addCleanup(nova_patcher.stop) glance_patcher = mock.patch('glanceclient.client.Client') self.glance = mock.create_autospec(glanceclient.Client(endpoint='/v1')) glance_patcher.start().return_value = self.glance self.addCleanup(glance_patcher.stop) cinder_patcher = mock.patch('cinderclient.client.Client') self.cinder = mock.create_autospec(cinderclient.Client('1')) cinder_patcher.start().return_value = self.cinder self.addCleanup(cinder_patcher.stop) db_api_patcher = mock.patch('ec2api.db.api.IMPL', autospec=ec2api.db.sqlalchemy.api) self.db_api = db_api_patcher.start() self.addCleanup(db_api_patcher.stop) isotime_patcher = mock.patch('oslo_utils.timeutils.isotime') self.isotime = isotime_patcher.start() self.addCleanup(isotime_patcher.stop) self._conf = self.useFixture(config_fixture.Config()) self.configure(fatal_exception_format_errors=True)
def do_setup(self, context): """Any initialization the destination driver does while starting.""" super(OpenStackDestinationDriver, self).do_setup(context) auth_url = self.configuration.auth_url if auth_url is None: raise ValueError(_("Cannot authenticate without an auth_url")) username = self.configuration.username password = self.configuration.password tenant_name = self.configuration.tenant_name project_id = self.configuration.project_id user_domain_name = self.configuration.user_domain_name nova_api_version = self.configuration.nova_api_version cinder_api_version = self.configuration.cinder_api_version glance_api_version = self.configuration.glance_api_version keystone_version = self.configuration.keystone_version if keystone_version == 'v3': auth = v3.Password(auth_url=auth_url, username=username, password=password, project_id=project_id, user_domain_name=user_domain_name) sess = v3_session.Session(auth=auth) elif keystone_version == 'v2': auth = v2.Password(auth_url, username=username, password=password, tenant_name=tenant_name) sess = v2_session.Session(auth=auth) self.nova = nova_client.Client(nova_api_version, session=sess) self.cinder = cinder_client.Client(cinder_api_version, session=sess) self.glance = glance_client.Client(glance_api_version, session=sess) self._initialized = True
def glance(self): if "image" not in self._clients: con = self.context endpoint_type = self._get_client_option('glance', 'endpoint_type') endpoint = self.url_for(service_type='image', endpoint_type=endpoint_type, region_name=cfg.CONF.region_name) # Rackspace service catalog includes a tenant scoped glance # endpoint so we have to munge the url a bit glance_url = urlparse.urlparse(endpoint) # remove the tenant and following from the url endpoint = "%s://%s" % (glance_url.scheme, glance_url.hostname) args = { 'auth_url': con.auth_url, 'service_type': 'image', 'project_id': con.tenant, 'token': self.auth_token, 'endpoint_type': endpoint_type, 'ca_file': self._get_client_option('glance', 'ca_file'), 'cert_file': self._get_client_option('glance', 'cert_file'), 'key_file': self._get_client_option('glance', 'key_file'), 'insecure': self._get_client_option('glance', 'insecure') } client = glanceclient.Client('2', endpoint, **args) self._clients["image"] = client return self._clients["image"]