def glanceclient(request): o = urlparse.urlparse(url_for(request, 'image')) LOG.debug('glanceclient connection created for host "%s:%d"' % (o.hostname, o.port)) return glance_client.Client(o.hostname, o.port, auth_tok=request.user.token)
def glance_upload( image_filename, creds={ 'auth_url': None, 'password': None, 'strategy': 'noauth', 'tenant': None, 'username': None }, host="0.0.0.0", port="9292", token=None): image_meta = { 'container_format': 'bare', 'disk_format': 'qcow2', 'is_public': True, 'min_disk': 0, 'min_ram': 0, 'name': 'Factory Test Image', 'properties': { 'distro': 'rhel' } } c = glance_client.Client(host=host, port=port, auth_tok=token, creds=creds) image_data = open(image_filename, "r") image_meta = c.add_image(image_meta, image_data) image_data.close() return image_meta['id']
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) if self.api_version == 1: # We load the client class specific to the API version... from glance import client creds = { 'username': config.images.username, 'password': config.images.password, 'tenant': config.images.tenant_name, # rstrip() is necessary here because Glance client # automatically adds the tokens/ part... 'auth_url': config.identity.auth_url.rstrip('/tokens'), 'strategy': config.identity.strategy } self._client = client.Client(config.images.host, config.images.port, creds=creds, configure_via_auth=False) else: raise NotImplementedError
def register_with_openstack(self, username): self.keydir = os.path.join(self.conf.dbdir, self.deployment, 'novacreds') print ' Registering assembly image with nova ...' creds = dict(username=os.getenv('OS_AUTH_USER'), password=os.getenv('OS_AUTH_KEY'), tenant=os.getenv('OS_AUTH_TENANT'), auth_url=os.getenv('OS_AUTH_URL'), strategy=os.getenv('OS_AUTH_STRATEGY', 'noauth')) c = glance_client.Client(host="0.0.0.0", port=9292, use_ssl=False, auth_tok=None, creds=creds) parameters = { "filters": {}, "limit": 10, } image_meta = { 'name': self.name, 'is_public': True, 'disk_format': 'qcow2', 'min_disk': 0, 'min_ram': 0, 'location': 'file://%s' % (self.image), 'owner': self.username, 'container_format': 'bare' } images = c.get_images(**parameters) for image in images: if image['name'] == self.name: print ' *** image already in glance: %s > %s' % (image['name'], image['id']) return try: with open(self.image) as ifile: image_meta = c.add_image(image_meta, ifile) image_id = image_meta['id'] print " Added new image with ID: %s" % image_id print " Returned the following metadata for the new image:" for k, v in sorted(image_meta.items()): print " %(k)30s => %(v)s" % locals() except exception.ClientConnectionError, e: print( " Failed to connect to the Glance API server." " Is the server running?" % locals()) pieces = unicode(e).split('\n') for piece in pieces: print piece return
def novaclient(request): LOG.debug('novaclient connection created using token "%s"' ' and url "%s"' % (request.user.token, url_for(request, 'compute'))) c = client.Client(username=request.user.username, api_key=request.user.token, project_id=request.user.tenant_id, auth_url=url_for(request, 'compute')) c.client.auth_token = request.user.token c.client.management_url=url_for(request, 'compute') return c
def test_get_glance_api(self): self.mox.StubOutClassWithMocks(glance_client, 'Client') client_instance = glance_client.Client(TEST_HOSTNAME, TEST_PORT, auth_tok=TEST_TOKEN) # Normally ``auth_tok`` is set in ``Client.__init__``, but mox doesn't # duplicate that behavior so we set it manually. client_instance.auth_tok = TEST_TOKEN self.mox.StubOutWithMock(api.glance, 'url_for') api.glance.url_for(IsA(http.HttpRequest), 'image').AndReturn(TEST_URL) self.mox.ReplayAll() ret_val = api.glance.glance_api(self.request) self.assertIsNotNone(ret_val) self.assertEqual(ret_val.auth_tok, TEST_TOKEN)
def _get_glance_image(image, instance, **kwargs): """Download image from the glance image server.""" LOG.debug(_("Downloading image %s from glance image server") % image) glance_client = client.Client(FLAGS.glance_host, FLAGS.glance_port) metadata, read_iter = glance_client.get_image(image) read_file_handle = read_write_util.GlanceFileRead(read_iter) file_size = int(metadata['size']) write_file_handle = read_write_util.VMWareHTTPWriteFile( kwargs.get("host"), kwargs.get("data_center_name"), kwargs.get("datastore_name"), kwargs.get("cookies"), kwargs.get("file_path"), file_size) start_transfer(read_file_handle, file_size, write_file_handle=write_file_handle) LOG.debug(_("Downloaded image %s from glance image server") % image)
def get_vmdk_size_and_properties(image, instance): """ Get size of the vmdk file that is to be downloaded for attach in spawn. Need this to create the dummy virtual disk for the meta-data file. The geometry of the disk created depends on the size. """ LOG.debug(_("Getting image size for the image %s") % image) if FLAGS.image_service == "nova.image.glance.GlanceImageService": glance_client = client.Client(FLAGS.glance_host, FLAGS.glance_port) meta_data = glance_client.get_image_meta(image) size, properties = meta_data["size"], meta_data["properties"] elif FLAGS.image_service == "nova.image.s3.S3ImageService": raise NotImplementedError elif FLAGS.image_service == "nova.image.local.LocalImageService": raise NotImplementedError LOG.debug(_("Got image size of %(size)s for the image %(image)s") % locals()) return size, properties
def deregister_with_openstack(self): print ' deregistering assembly image from glance ...' parameters = { "filters": {}, "limit": 10, } creds = dict(username=os.getenv('OS_AUTH_USER'), password=os.getenv('OS_AUTH_KEY'), tenant=os.getenv('OS_AUTH_TENANT'), auth_url=os.getenv('OS_AUTH_URL'), strategy=os.getenv('OS_AUTH_STRATEGY', 'noauth')) c = glance_client.Client(host="0.0.0.0", port=9292, use_ssl=False, auth_tok=None, creds=creds) images = c.get_images(**parameters) image_id = None for image in images: if image['name'] == self.name: image_id = image['id'] break if image_id == None: print " *** No image with name %s was found in glance" % self.name else: try: c.delete_image(image_id) print " Deleted image %s (%s)" % (self.name, image_id) except exception.NotFound: print " *** No image with ID %s was found" % image_id return except exception.NotAuthorized: print " *** Glance can't delete the image (%s, %s)" % ( image_id, self.name) return
def _put_glance_image(image, instance, **kwargs): """Upload the snapshotted vm disk file to Glance image server.""" LOG.debug(_("Uploading image %s to the Glance image server") % image) read_file_handle = read_write_util.VmWareHTTPReadFile( kwargs.get("host"), kwargs.get("data_center_name"), kwargs.get("datastore_name"), kwargs.get("cookies"), kwargs.get("file_path")) file_size = read_file_handle.get_size() glance_client = client.Client(FLAGS.glance_host, FLAGS.glance_port) # The properties and other fields that we need to set for the image. image_metadata = {"is_public": True, "disk_format": "vmdk", "container_format": "bare", "type": "vmdk", "properties": {"vmware_adaptertype": kwargs.get("adapter_type"), "vmware_ostype": kwargs.get("os_type"), "vmware_image_version": kwargs.get("image_version")}} start_transfer(read_file_handle, file_size, glance_client=glance_client, image_id=image, image_meta=image_metadata) LOG.debug(_("Uploaded image %s to the Glance image server") % image)
def _get_client(self): return client.Client("localhost", self.api_port)
def test_client_handles_versions(self): api_client = client.Client("0.0.0.0", doc_root="") self.assertRaises(exception.MultipleChoices, api_client.get_images)