Ejemplo n.º 1
0
class Image(livetest.Generic):
    def setUp(self):
        self.now = time.mktime(time.gmtime())
        self.cloud = 'cloud.%s' % self['testcloud']
        aurl, self.token = self[self.cloud, 'url'], self[self.cloud, 'token']
        self.auth_base = AstakosCachedClient(aurl, self.token)
        self.imgname = 'img_%s' % self.now
        url = self.auth_base.get_service_endpoints('image')['publicURL']
        self.token = self.auth_base.token
        self.client = ImageClient(url, self.token)
        cyclades_url = self.auth_base.get_service_endpoints(
            'compute')['publicURL']
        self.cyclades = CycladesClient(cyclades_url, self.token)
        self._imglist = {}
        self._imgdetails = {}

    def test_000(self):
        self._prepare_img()
        super(self.__class__, self).test_000()

    def _prepare_img(self):
        f = open(self['image', 'local_path'], 'rb')
        (token, uuid) = (self.token, self.auth_base.user_term('id'))
        purl = self.auth_base.get_service_endpoints(
            'object-store')['publicURL']
        from kamaki.clients.pithos import PithosClient
        self.pithcli = PithosClient(purl, token, uuid)
        cont = 'cont_%s' % self.now
        self.pithcli.container = cont
        self.obj = 'obj_%s' % self.now
        print('\t- Create container %s on Pithos server' % cont)
        self.pithcli.container_put()
        self.location = 'pithos://%s/%s/%s' % (uuid, cont, self.obj)
        print('\t- Upload an image at %s...\n' % self.location)
        self.pithcli.upload_object(self.obj, f)
        print('\t- ok')
        f.close()

        r = self.client.register(
            self.imgname, self.location, params=dict(is_public=True))
        self._imglist[self.imgname] = dict(
            name=r['name'], id=r['id'])
        self._imgdetails[self.imgname] = r

    def tearDown(self):
        for img in self._imglist.values():
            print('\tDeleting image %s' % img['id'])
            self.cyclades.delete_image(img['id'])
        if hasattr(self, 'pithcli'):
            print('\tDeleting container %s' % self.pithcli.container)
            try:
                self.pithcli.del_container(delimiter='/')
                self.pithcli.purge_container()
            except ClientError:
                pass

    def _get_img_by_name(self, name):
        r = self.cyclades.list_images()
        for img in r:
            if img['name'] == name:
                return img
        return None

    def test_list_public(self):
        """Test list_public"""
        self._test_list_public()

    def _test_list_public(self):
        r = self.client.list_public()
        r0 = self.client.list_public(order='-')
        self.assertTrue(len(r) > 0)
        for img in r:
            for term in (
                    'status',
                    'name',
                    'container_format',
                    'disk_format',
                    'id',
                    'size'):
                self.assertTrue(term in img)
        self.assertTrue(r, r0)
        r0.reverse()
        for i, img in enumerate(r):
            self.assert_dicts_are_equal(img, r0[i])
        r1 = self.client.list_public(detail=True)
        for img in r1:
            for term in (
                    'status',
                    'name',
                    'checksum',
                    'created_at',
                    'disk_format',
                    'updated_at',
                    'id',
                    'location',
                    'container_format',
                    'owner',
                    'is_public',
                    'deleted_at',
                    'properties',
                    'size'):
                self.assertTrue(term in img)
                if len(img['properties']):
                    for interm in ('osfamily', 'root_partition'):
                        self.assertTrue(interm in img['properties'])
        size_max = 1000000000000
        r2 = self.client.list_public(filters=dict(size_max=size_max))
        self.assertTrue(len(r2) <= len(r))
        for img in r2:
            self.assertTrue(int(img['size']) <= size_max)

    def test_get_meta(self):
        """Test get_meta"""
        self._test_get_meta()

    def _test_get_meta(self):
        r = self.client.get_meta(self['image', 'id'])
        self.assertEqual(r['id'], self['image', 'id'])
        for term in (
                'status',
                'name',
                'checksum',
                'updated-at',
                'created-at',
                'deleted-at',
                'location',
                'is-public',
                'owner',
                'disk-format',
                'size',
                'container-format'):
            self.assertTrue(term in r)
            for interm in (
                    'OSFAMILY',
                    'USERS',
                    'ROOT_PARTITION',
                    'OS',
                    'DESCRIPTION'):
                self.assertTrue(interm in r['properties'])

    def test_register(self):
        """Test register"""
        self._prepare_img()
        self._test_register()

    def _test_register(self):
        self.assertTrue(self._imglist)
        for img in self._imglist.values():
            self.assertTrue(img is not None)
            r = set(self._imgdetails[img['name']].keys())
            self.assertTrue(r.issubset(IMGMETA.union(['properties'])))

    def test_unregister(self):
        """Test unregister"""
        self._prepare_img()
        self._test_unregister()

    def _test_unregister(self):
        try:
            for img in self._imglist.values():
                self.client.unregister(img['id'])
                self._prepare_img()
                break
        except ClientError as ce:
            if ce.status in (405,):
                print 'IMAGE UNREGISTER is not supported by server: %s' % ce
            else:
                raise

    def test_set_members(self):
        """Test set_members"""
        self._prepare_img()
        self._test_set_members()

    def _test_set_members(self):
        members = ['*****@*****.**' % self.now]
        for img in self._imglist.values():
            self.client.set_members(img['id'], members)
            r = self.client.list_members(img['id'])
            self.assertEqual(r[0]['member_id'], members[0])

    def test_list_members(self):
        """Test list_members"""
        self._test_list_members()

    def _test_list_members(self):
        self._test_set_members()

    def test_remove_members(self):
        """Test remove_members - NO CHECK"""
        self._prepare_img()
        self._test_remove_members()

    def _test_remove_members(self):
        return
        members = ['*****@*****.**' % self.now, '*****@*****.**' % self.now]
        for img in self._imglist.values():
            self.client.set_members(img['id'], members)
            r = self.client.list_members(img['id'])
            self.assertTrue(len(r) > 1)
            self.client.remove_member(img['id'], members[0])
            r0 = self.client.list_members(img['id'])
            self.assertEqual(len(r), 1 + len(r0))
            self.assertEqual(r0[0]['member_id'], members[1])

    def test_list_shared(self):
        """Test list_shared - NOT CHECKED"""
        self._test_list_shared()

    def _test_list_shared(self):
        #No way to test this, if I dont have member images
        pass
Ejemplo n.º 2
0
class Kamaki(object):
    """Wrapper class for the ./kamaki library"""

    @staticmethod
    def get_default_cloud_name():
        """Returns the name of the default cloud"""
        clouds = config.keys('cloud')
        default = config.get('global', 'default_cloud')
        if not default:
            return clouds[0] if len(clouds) else ""
        return default if default in clouds else ""

    @staticmethod
    def set_default_cloud(name):
        """Sets a cloud account as default"""
        config.set('global', 'default_cloud', name)
        config.write()

    @staticmethod
    def get_clouds():
        """Returns the list of available clouds"""
        names = config.keys('cloud')

        clouds = {}
        for name in names:
            clouds[name] = config.get('cloud', name)

        return clouds

    @staticmethod
    def get_cloud_by_name(name):
        """Returns a dictionary with cloud info"""
        return config.get('cloud', name)

    @staticmethod
    def save_cloud(name, url, token, description=""):
        """Save a new cloud account"""
        cloud = {'url': url, 'token': token}
        if len(description):
            cloud['description'] = description
        config.set('cloud', name, cloud)

        # Make the saved cloud the default one
        config.set('global', 'default_cloud', name)
        config.write()

    @staticmethod
    def remove_cloud(name):
        """Deletes an existing cloud from the ./Kamaki configuration file"""
        config.remove_option('cloud', name)
        config.write()

    @staticmethod
    def create_account(url, token):
        """Given a valid (URL, tokens) pair this method returns an Astakos
        client instance
        """
        client = AstakosClient(url, token)
        try:
            client.authenticate()
        except ClientError:
            return None

        return client

    @staticmethod
    def get_account(cloud_name):
        """Given a saved cloud name this method returns an Astakos client
        instance
        """
        cloud = config.get('cloud', cloud_name)
        assert cloud, "cloud: `%s' does not exist" % cloud_name
        assert 'url' in cloud, "url attr is missing in %s" % cloud_name
        assert 'token' in cloud, "token attr is missing in %s" % cloud_name

        return Kamaki.create_account(cloud['url'], cloud['token'])

    def __init__(self, account, output):
        """Create a Kamaki instance"""
        self.account = account
        self.out = output

        self.pithos = PithosClient(
            self.account.get_service_endpoints('object-store')['publicURL'],
            self.account.token,
            self.account.user_info()['id'],
            CONTAINER)

        self.image = ImageClient(
            self.account.get_service_endpoints('image')['publicURL'],
            self.account.token)

    def upload(self, file_obj, size=None, remote_path=None, container=None,
               content_type=None, hp=None, up=None):
        """Upload a file to Pithos+"""

        path = basename(file_obj.name) if remote_path is None else remote_path

        if container is None:
            container = CONTAINER

        try:
            self.pithos.create_container(container)
        except ClientError as e:
            if e.status != 202:  # Ignore container already exists errors
                raise e

        hash_cb = self.out.progress_generator(hp) if hp is not None else None
        upload_cb = self.out.progress_generator(up) if up is not None else None

        try:
            self.pithos.container = container
            self.pithos.upload_object(path, file_obj, size=size,
                                      hash_cb=hash_cb, upload_cb=upload_cb,
                                      content_type=content_type)
        finally:
            self.pithos.container = CONTAINER

        return "pithos://%s/%s/%s" % (self.account.user_info()['id'],
                                      container, path)

    def register(self, name, location, metadata, public=False):
        """Register an image with Cyclades"""

        is_public = 'true' if public else 'false'
        params = {'is_public': is_public, 'disk_format': 'diskdump'}
        return self.image.register(name, location, params, metadata)

    def share(self, location):
        """Share this file with all the users"""

        self.pithos.set_object_sharing(location, "*")

    def object_exists(self, container, location):
        """Check if an object exists in Pithos+"""

        try:
            self.pithos.container = container
            self.pithos.get_object_info(location)
        except ClientError as e:
            self.pithos.container = CONTAINER
            if e.status == 404:  # Object not found error
                return False
            else:
                raise

        self.pithos.container = CONTAINER
        return True
Ejemplo n.º 3
0
class Kamaki(object):
    """Wrapper class for the ./kamaki library"""
    @staticmethod
    def get_default_cloud_name():
        """Returns the name of the default cloud"""
        clouds = config.keys('cloud')
        default = config.get('global', 'default_cloud')
        if not default:
            return clouds[0] if len(clouds) else ""
        return default if default in clouds else ""

    @staticmethod
    def set_default_cloud(name):
        """Sets a cloud account as default"""
        config.set('global', 'default_cloud', name)
        config.write()

    @staticmethod
    def get_clouds():
        """Returns the list of available clouds"""
        names = config.keys('cloud')

        clouds = {}
        for name in names:
            clouds[name] = config.get('cloud', name)

        return clouds

    @staticmethod
    def get_cloud_by_name(name):
        """Returns a dictionary with cloud info"""
        return config.get('cloud', name)

    @staticmethod
    def save_cloud(name, url, token, description=""):
        """Save a new cloud account"""
        cloud = {'url': url, 'token': token}
        if len(description):
            cloud['description'] = description
        config.set('cloud', name, cloud)

        # Make the saved cloud the default one
        config.set('global', 'default_cloud', name)
        config.write()

    @staticmethod
    def remove_cloud(name):
        """Deletes an existing cloud from the ./Kamaki configuration file"""
        config.remove_option('cloud', name)
        config.write()

    @staticmethod
    def create_account(url, token):
        """Given a valid (URL, tokens) pair this method returns an Astakos
        client instance
        """
        client = AstakosClient(url, token)
        try:
            client.authenticate()
        except ClientError:
            return None

        return client

    @staticmethod
    def get_account(cloud_name):
        """Given a saved cloud name this method returns an Astakos client
        instance
        """
        cloud = config.get('cloud', cloud_name)
        assert cloud, "cloud: `%s' does not exist" % cloud_name
        assert 'url' in cloud, "url attr is missing in %s" % cloud_name
        assert 'token' in cloud, "token attr is missing in %s" % cloud_name

        return Kamaki.create_account(cloud['url'], cloud['token'])

    def __init__(self, account, output):
        """Create a Kamaki instance"""
        self.account = account
        self.out = output

        self.pithos = PithosClient(
            self.account.get_service_endpoints('object-store')['publicURL'],
            self.account.token,
            self.account.user_info()['id'], CONTAINER)

        self.image = ImageClient(
            self.account.get_service_endpoints('image')['publicURL'],
            self.account.token)

    def upload(self,
               file_obj,
               size=None,
               remote_path=None,
               container=None,
               content_type=None,
               hp=None,
               up=None):
        """Upload a file to Pithos+"""

        path = basename(file_obj.name) if remote_path is None else remote_path

        if container is None:
            container = CONTAINER

        try:
            self.pithos.create_container(container)
        except ClientError as e:
            if e.status != 202:  # Ignore container already exists errors
                raise e

        hash_cb = self.out.progress_generator(hp) if hp is not None else None
        upload_cb = self.out.progress_generator(up) if up is not None else None

        try:
            self.pithos.container = container
            self.pithos.upload_object(path,
                                      file_obj,
                                      size=size,
                                      hash_cb=hash_cb,
                                      upload_cb=upload_cb,
                                      content_type=content_type)
        finally:
            self.pithos.container = CONTAINER

        return "pithos://%s/%s/%s" % (self.account.user_info()['id'],
                                      container, path)

    def register(self, name, location, metadata, public=False):
        """Register an image with Cyclades"""

        is_public = 'true' if public else 'false'
        params = {'is_public': is_public, 'disk_format': 'diskdump'}
        return self.image.register(name, location, params, metadata)

    def share(self, location):
        """Share this file with all the users"""

        self.pithos.set_object_sharing(location, "*")

    def object_exists(self, container, location):
        """Check if an object exists in Pithos+"""

        try:
            self.pithos.container = container
            self.pithos.get_object_info(location)
        except ClientError as e:
            self.pithos.container = CONTAINER
            if e.status == 404:  # Object not found error
                return False
            else:
                raise

        self.pithos.container = CONTAINER
        return True
Ejemplo n.º 4
0
uuid = user["access"]["user"]["id"]

#  Initliaze Pithos
service_type = PithosClient.service_type
endpoint = astakos.get_endpoint_url(service_type)
pithos = PithosClient(endpoint, TOKEN)
pithos.account = uuid

#  Initialize Image
service_type = ImageClient.service_type
endpoint = astakos.get_endpoint_url(service_type)
image = ImageClient(endpoint, TOKEN)

#  Our data
local = "my-image.diskdump"
local_meta = "my-image.diskdump.meta"
with open(local_meta) as f:
    meta = json.load(f)

#  Upload the image and meta files
pithos.container = "images"
with open(local) as f:
    pithos.upload_object(local, f)
with open(local_meta) as f:
    pithos.upload_object(local_meta, f)

#  Register image
name, location = meta.pop("name"), meta.pop("location")
properties = meta.pop("properties")
image.register(name, location, properties=properties, params=meta)