def get(self, location): """ Takes a `glance.store.location.Location` object that indicates where to find the image file, and returns a generator for reading the image file :param location `glance.store.location.Location` object, supplied from glance.store.location.get_location_from_uri() :raises `glance.exception.NotFound` if image does not exist """ loc = location.store_location swift_conn = self._make_swift_connection( auth_url=loc.swift_auth_url, user=loc.user, key=loc.key) try: (resp_headers, resp_body) = swift_conn.get_object( container=loc.container, obj=loc.obj, resp_chunk_size=self.CHUNKSIZE) except swift_client.ClientException, e: if e.http_status == httplib.NOT_FOUND: uri = location.get_store_uri() raise exception.NotFound(_("Swift could not find image at " "uri %(uri)s") % locals()) else: raise
def get(self, location): """ Takes a `glance.store.location.Location` object that indicates where to find the image file, and returns a tuple of generator (for reading the image file) and image_size :param location `glance.store.location.Location` object, supplied from glance.store.location.get_location_from_uri() :raises `glance.exception.NotFound` if image does not exist """ loc = location.store_location swift_conn = self._swift_connection_for_location(loc) try: (resp_headers, resp_body) = swift_conn.get_object(container=loc.container, obj=loc.obj, resp_chunk_size=self.CHUNKSIZE) except swiftclient.ClientException, e: if e.http_status == httplib.NOT_FOUND: uri = location.get_store_uri() raise exception.NotFound( _("Swift could not find image at " "uri %(uri)s") % locals()) else: raise
def delete(cls, location): """ Takes a `glance.store.location.Location` object that indicates where to find the image file to delete :location `glance.store.location.Location` object, supplied from glance.store.location.get_location_from_uri() """ from swift.common import client as swift_client # TODO(sirp): snet=False for now, however, if the instance of # swift we're talking to is within our same region, we should set # snet=True loc = location.store_location swift_conn = swift_client.Connection( authurl=loc.swift_auth_url, user=loc.user, key=loc.key, snet=False) try: swift_conn.delete_object(loc.container, loc.obj) except swift_client.ClientException, e: if e.http_status == httplib.NOT_FOUND: uri = location.get_store_uri() raise exception.NotFound("Swift could not find image at " "uri %(uri)s" % locals()) else: raise
def get(cls, location, expected_size=None, options=None): """ Takes a `glance.store.location.Location` object that indicates where to find the image file, and returns a generator from Swift provided by Swift client's get_object() method. :location `glance.store.location.Location` object, supplied from glance.store.location.get_location_from_uri() """ from swift.common import client as swift_client # TODO(sirp): snet=False for now, however, if the instance of # swift we're talking to is within our same region, we should set # snet=True loc = location.store_location swift_conn = swift_client.Connection( authurl=loc.swift_auth_url, user=loc.user, key=loc.key, snet=False) try: (resp_headers, resp_body) = swift_conn.get_object( container=loc.container, obj=loc.obj, resp_chunk_size=cls.CHUNKSIZE) except swift_client.ClientException, e: if e.http_status == httplib.NOT_FOUND: uri = location.get_store_uri() raise exception.NotFound("Swift could not find image at " "uri %(uri)s" % locals()) else: raise
def set_acls(self, location, public=False, read_tenants=[], write_tenants=[]): """ Sets the read and write access control list for an image in the backend store. :location `glance.store.location.Location` object, supplied from glance.store.location.get_location_from_uri() :public A boolean indicating whether the image should be public. :read_tenants A list of tenant strings which should be granted read access for an image. :write_tenants A list of tenant strings which should be granted write access for an image. """ if self.multi_tenant: loc = location.store_location swift_conn = self._swift_connection_for_location(loc) headers = {} if public: headers['X-Container-Read'] = ".r:*" elif read_tenants: headers['X-Container-Read'] = ','.join(read_tenants) else: headers['X-Container-Read'] = '' write_tenants.extend(self.admin_tenants) if write_tenants: headers['X-Container-Write'] = ','.join(write_tenants) else: headers['X-Container-Write'] = '' try: swift_conn.post_container(loc.container, headers=headers) except swiftclient.ClientException, e: if e.http_status == httplib.NOT_FOUND: uri = location.get_store_uri() raise exception.NotFound( _("Swift could not find image at " "uri %(uri)s") % locals()) else: raise
def set_acls(self, location, public=False, read_tenants=[], write_tenants=[]): """ Sets the read and write access control list for an image in the backend store. :location `glance.store.location.Location` object, supplied from glance.store.location.get_location_from_uri() :public A boolean indicating whether the image should be public. :read_tenants A list of tenant strings which should be granted read access for an image. :write_tenants A list of tenant strings which should be granted write access for an image. """ if self.multi_tenant: loc = location.store_location swift_conn = self._swift_connection_for_location(loc) headers = {} if public: headers['X-Container-Read'] = ".r:*" elif read_tenants: headers['X-Container-Read'] = ','.join(read_tenants) else: headers['X-Container-Read'] = '' write_tenants.extend(self.admin_tenants) if write_tenants: headers['X-Container-Write'] = ','.join(write_tenants) else: headers['X-Container-Write'] = '' try: swift_conn.post_container(loc.container, headers=headers) except swiftclient.ClientException, e: if e.http_status == httplib.NOT_FOUND: uri = location.get_store_uri() raise exception.NotFound(_("Swift could not find image at " "uri %(uri)s") % locals()) else: raise
def delete(self, location): """ Takes a `glance.store.location.Location` object that indicates where to find the image file to delete :location `glance.store.location.Location` object, supplied from glance.store.location.get_location_from_uri() :raises NotFound if image does not exist """ loc = location.store_location swift_conn = self._make_swift_connection( auth_url=loc.swift_auth_url, user=loc.user, key=loc.key) try: swift_conn.delete_object(loc.container, loc.obj) except swift_client.ClientException, e: if e.http_status == httplib.NOT_FOUND: uri = location.get_store_uri() raise exception.NotFound(_("Swift could not find image at " "uri %(uri)s") % locals()) else: raise
# since we're simply sending off parallelizable requests # to Swift to delete stuff. It's not like we're going to # be hogging up network or file I/O here... swift_conn.delete_object(obj_container, segment['name']) else: swift_conn.delete_object(loc.container, loc.obj) if self.multi_tenant: #NOTE: In multi-tenant mode containers are specific to # each object (Glance image) swift_conn.delete_container(loc.container) except swiftclient.ClientException, e: if e.http_status == httplib.NOT_FOUND: uri = location.get_store_uri() raise exception.NotFound( _("Swift could not find image at " "uri %(uri)s") % locals()) else: raise def set_acls(self, location, public=False, read_tenants=[], write_tenants=[]): """ Sets the read and write access control list for an image in the backend store.
# Delete all the chunks before the object manifest itself obj_container, obj_prefix = manifest.split('/', 1) for segment in swift_conn.get_container(obj_container, prefix=obj_prefix)[1]: # TODO(jaypipes): This would be an easy area to parallelize # since we're simply sending off parallelizable requests # to Swift to delete stuff. It's not like we're going to # be hogging up network or file I/O here... swift_conn.delete_object(obj_container, segment['name']) else: swift_conn.delete_object(loc.container, loc.obj) except swift_client.ClientException, e: if e.http_status == httplib.NOT_FOUND: uri = location.get_store_uri() raise exception.NotFound(_("Swift could not find image at " "uri %(uri)s") % locals()) else: raise class ChunkReader(object): def __init__(self, fd, checksum, total): self.fd = fd self.checksum = checksum self.total = total self.bytes_read = 0 def read(self, i): left = self.total - self.bytes_read