Ejemplo n.º 1
0
    def _download_instance(self, instance_id, download_location, *args, **kwargs):
        """
        Download an existing instance to local download directory
        Required Args:
            instance_id - The instance ID to be downloaded (1234-4321-1234)
            download_location - The exact path where image will be downloaded

        NOTE: It is recommended that you 'prepare the snapshot' before creating
        an image by running 'sync' and 'freeze' on the instance. See
        http://docs.openstack.org/grizzly/openstack-ops/content/snapsnots.html#consistent_snapshots
        """
        #Step 2: Create local path for copying image
        server = self.get_server(instance_id)
        tenant = find(self.keystone.tenants, id=server.tenant_id)
        ss_prefix = kwargs.get('ss_prefix',
                'ChromoSnapShot_%s' % instance_id) #Legacy format
        if os.path.exists(download_location):
            logger.info("Download location exists. Looking for snapshot %s.." %
                        ss_prefix)
            snapshot = self.find_image(ss_prefix, contains=True)
            if snapshot:
                snapshot = snapshot[0]
                logger.info("Found snapshot %s. "
                            "Download skipped for local copy"
                            % snapshot.id)
                return (snapshot.id, download_location)
        now = kwargs.get('timestamp',datetime.datetime.now()) # Pytz datetime
        now_str = now.strftime('%Y-%m-%d_%H:%M:%S')
        ss_name = '%s_%s' % (ss_prefix, now_str)
        meta_data = {}
        snapshot = self.create_snapshot(instance_id, ss_name, delay=True, **meta_data)
        return (snapshot.id,
                self.download_image(snapshot.id, download_location))
Ejemplo n.º 2
0
    def _download_instance(self, instance_id, download_location, *args,
                           **kwargs):
        """
        Download an existing instance to local download directory
        Required Args:
            instance_id - The instance ID to be downloaded (1234-4321-1234)
            download_location - The exact path where image will be downloaded

        NOTE: It is recommended that you 'prepare the snapshot' before creating
        an image by running 'sync' and 'freeze' on the instance. See
        http://docs.openstack.org/grizzly/openstack-ops/content/snapsnots.html#consistent_snapshots
        """
        #Step 2: Create local path for copying image
        server = self.get_server(instance_id)
        if server:
            identity_version = self.creds.get('version', 'v2.0')
            list_args = {}
            if '3' in identity_version:
                domain_id = kwargs.pop('domain', 'default')
                list_args['domain_id'] = domain_id
            tenant = find(self.keystone_tenants_method(),
                          id=server.tenant_id,
                          **list_args)
        else:
            tenant = None
        ss_prefix = kwargs.get('ss_prefix', 'ChromoSnapShot_%s' %
                               instance_id)  #Legacy format
        snapshot = self.find_image(ss_prefix, contains=True)
        if snapshot:
            snapshot = snapshot[0]
            logger.info("Found snapshot %s. " % snapshot.id)
            if self.contains_image(snapshot.id, download_location):
                logger.info(
                    "Download should be valid, returning snapshot+location")
                return (snapshot.id, download_location)
            if hasattr(self, 'hook') and hasattr(self.hook,
                                                 'on_update_status'):
                self.hook.on_update_status("Downloading Snapshot:%s" %
                                           (snapshot.id, ))
            logger.info("Downloading from existing snapshot: %s" %
                        (snapshot.id, ))
            return (snapshot.id,
                    self.download_image(snapshot.id, download_location))

        now = kwargs.get('timestamp', datetime.datetime.now())  # Pytz datetime
        now_str = now.strftime('%Y-%m-%d_%H:%M:%S')
        ss_name = '%s_%s' % (ss_prefix, now_str)
        meta_data = {}
        if hasattr(self, 'hook') and hasattr(self.hook, 'on_update_status'):
            self.hook.on_update_status(
                "Creating Snapshot:%s from Instance:%s" %
                (ss_name, instance_id))
        logger.info("Creating snapshot from instance %s. " % instance_id)
        snapshot = self.create_snapshot(instance_id,
                                        ss_name,
                                        delay=True,
                                        **meta_data)
        return (snapshot.id, self.download_image(snapshot.id,
                                                 download_location))
Ejemplo n.º 3
0
 def get_user(self, username):
     """
     Retrieve user
     Invalid username : raise keystoneclient.exceptions.NotFound
     """
     try:
         return find(self.keystone.users, name=username)
     except NotFound:
         return None
Ejemplo n.º 4
0
 def get_role(self, rolename):
     """
     Retrieve role
     Invalid rolename : raise keystoneclient.exceptions.NotFound
     """
     try:
         return find(self.keystone.roles, name=rolename)
     except NotFound:
         return None
Ejemplo n.º 5
0
 def get_user(self, username):
     """
     Retrieve user
     Invalid username : raise keystoneclient.exceptions.NotFound
     """
     try:
         return find(self.keystone.users, name=username)
     except NotFound:
         return None
Ejemplo n.º 6
0
 def get_project(self, groupname):
     """
     Retrieve project
     Invalid groupname : raise keystoneclient.exceptions.NotFound
     """
     try:
         return find(self.keystone_projects(), name=groupname)
     except NotFound:
         return None
Ejemplo n.º 7
0
 def get_project(self, groupname):
     """
     Retrieve project
     Invalid groupname : raise keystoneclient.exceptions.NotFound
     """
     try:
         return find(self.keystone_projects(), name=groupname)
     except NotFound:
         return None
 def get_role(self, rolename):
     """
     Retrieve role
     Invalid rolename : raise keystoneclient.exceptions.NotFound
     """
     try:
         return find(self.keystone.roles, name=rolename)
     except NotFound:
         return None
 def unshare_image(self, image, tenant_name, **kwargs):
     """
     Remove a shared image with tenant_name
     """
     identity_version = self.creds.get('version','v2.0')
     list_args = {}
     if '3' in identity_version:
         domain_id = kwargs.pop('domain', 'default')
         list_args['domain_id'] = domain_id
     tenant = find(self.keystone_tenants_method(), name=tenant_name, **list_args)
     return self.glance.image_members.delete(image.id, tenant.id)
Ejemplo n.º 10
0
 def get_project(self, project_name, **kwargs):
     """
     Retrieve project
     Invalid project_name : raise keystoneclient.exceptions.NotFound
     """
     if self.keystone_version() == 2:
         kwargs.pop('domain_id', 'default')  # Remove the kwarg
     try:
         return find(self.keystone_projects(), name=project_name, **kwargs)
     except NotFound:
         return None
 def find_tenant(self, tenant_name, **kwargs):
     try:
         identity_version = self.creds.get('version','v2.0')
         list_args = {}
         if '3' in identity_version:
             domain_id = kwargs.pop('domain', 'default')
             list_args['domain_id'] = domain_id
         tenant = find(self.keystone_tenants_method(), name=tenant_name, **list_args)
         return tenant
     except NotFound:
         return None
 def get_project(self, project_name, **kwargs):
     """
     Retrieve project
     Invalid project_name : raise keystoneclient.exceptions.NotFound
     """
     if self.keystone_version() == 2:
         kwargs.pop('domain_id', 'default')  # Remove the kwarg
     try:
         return find(self.keystone_projects(), name=project_name, **kwargs)
     except NotFound:
         return None
Ejemplo n.º 13
0
 def unshare_image(self, image, tenant_name, **kwargs):
     """
     Remove a shared image with tenant_name
     """
     identity_version = self.creds.get('version', 'v2.0')
     list_args = {}
     if '3' in identity_version:
         domain_id = kwargs.pop('domain', 'default')
         list_args['domain_id'] = domain_id
     tenant = find(self.keystone_tenants_method(),
                   name=tenant_name,
                   **list_args)
     return self.glance.image_members.delete(image.id, tenant.id)
Ejemplo n.º 14
0
 def find_tenant(self, tenant_name, **kwargs):
     try:
         identity_version = self.creds.get('version', 'v2.0')
         list_args = {}
         if '3' in identity_version:
             domain_id = kwargs.pop('domain', 'default')
             list_args['domain_id'] = domain_id
         tenant = find(self.keystone_tenants_method(),
                       name=tenant_name,
                       **list_args)
         return tenant
     except NotFound:
         return None
    def _download_instance(self, instance_id, download_location, *args, **kwargs):
        """
        Download an existing instance to local download directory
        Required Args:
            instance_id - The instance ID to be downloaded (1234-4321-1234)
            download_location - The exact path where image will be downloaded

        NOTE: It is recommended that you 'prepare the snapshot' before creating
        an image by running 'sync' and 'freeze' on the instance. See
        http://docs.openstack.org/grizzly/openstack-ops/content/snapsnots.html#consistent_snapshots
        """
        #Step 2: Create local path for copying image
        server = self.get_server(instance_id)
        if server:
            identity_version = self.creds.get('version','v2.0')
            list_args = {}
            if '3' in identity_version:
                domain_id = kwargs.pop('domain', 'default')
                list_args['domain_id'] = domain_id
            tenant = find(self.keystone_tenants_method(), id=server.tenant_id, **list_args)
        else:
            tenant = None
        ss_prefix = kwargs.get('ss_prefix',
                'ChromoSnapShot_%s' % instance_id) #Legacy format
        snapshot = self.find_image(ss_prefix, contains=True)
        if snapshot:
            snapshot = snapshot[0]
            logger.info("Found snapshot %s. " % snapshot.id)
            if self.contains_image(snapshot.id, download_location):
                logger.info("Download should be valid, returning snapshot+location")
                return (snapshot.id, download_location)
            if hasattr(self,'hook') and hasattr(self.hook, 'on_update_status'):
                self.hook.on_update_status("Downloading Snapshot:%s" % (snapshot.id,))
            logger.info("Downloading from existing snapshot: %s" % (snapshot.id,))
            return (snapshot.id,
                    self.download_image(snapshot.id, download_location))

        now = kwargs.get('timestamp',datetime.datetime.now()) # Pytz datetime
        now_str = now.strftime('%Y-%m-%d_%H:%M:%S')
        ss_name = '%s_%s' % (ss_prefix, now_str)
        meta_data = {}
        if hasattr(self,'hook') and hasattr(self.hook, 'on_update_status'):
            self.hook.on_update_status("Creating Snapshot:%s from Instance:%s" % (ss_name, instance_id))
        logger.info("Creating snapshot from instance %s. " % instance_id)
        snapshot = self.create_snapshot(instance_id, ss_name, delay=True, **meta_data)
        return (snapshot.id,
                self.download_image(snapshot.id, download_location))
Ejemplo n.º 16
0
 def _parse_download_location(self, server, image_name, **kwargs):
     download_location = kwargs.get('download_location')
     download_dir = kwargs.get('download_dir')
     if not download_dir and not download_location:
         raise Exception("Could not parse download location. Expected "
                         "'download_dir' or 'download_location'")
     elif not download_location:
         #Use download dir & tenant_name to keep filesystem order
         tenant = find(self.keystone.tenants, id=server.tenant_id)
         local_user_dir = os.path.join(download_dir, tenant.name)
         if not os.path.exists(os.path.dirname(local_user_dir)):
             os.makedirs(local_user_dir)
         download_location = os.path.join(
             local_user_dir, '%s.qcow2' % self.clean_path(image_name))
     elif not download_dir:
         download_dir = os.path.dirname(download_location)
     return download_dir, download_location
 def _parse_download_location(self, server, image_name, **kwargs):
     download_location = kwargs.get('download_location')
     download_dir = kwargs.get('download_dir')
     identity_version = self.creds.get('version','v2.0')
     list_args = {}
     if '3' in identity_version:
         domain_id = kwargs.pop('domain', 'default')
         list_args['domain_id'] = domain_id
     if not download_dir and not download_location:
         raise Exception("Could not parse download location. Expected "
                         "'download_dir' or 'download_location'")
     elif not download_location:
         #Use download dir & tenant_name to keep filesystem order
         tenant = find(self.keystone_tenants_method(), id=server.tenant_id, **list_args)
         local_user_dir = os.path.join(download_dir, tenant.name)
         if not os.path.exists(os.path.dirname(local_user_dir)):
             os.makedirs(local_user_dir)
         download_location = os.path.join(
             local_user_dir, '%s.qcow2' % self.clean_path(image_name))
     elif not download_dir:
         download_dir = os.path.dirname(download_location)
     return download_dir, download_location
Ejemplo n.º 18
0
 def _parse_download_location(self, server, image_name, **kwargs):
     download_location = kwargs.get('download_location')
     download_dir = kwargs.get('download_dir')
     identity_version = self.creds.get('version', 'v2.0')
     list_args = {}
     if '3' in identity_version:
         domain_id = kwargs.pop('domain', 'default')
         list_args['domain_id'] = domain_id
     if not download_dir and not download_location:
         raise Exception("Could not parse download location. Expected "
                         "'download_dir' or 'download_location'")
     elif not download_location:
         #Use download dir & tenant_name to keep filesystem order
         tenant = find(self.keystone_tenants_method(),
                       id=server.tenant_id,
                       **list_args)
         local_user_dir = os.path.join(download_dir, tenant.name)
         if not os.path.exists(os.path.dirname(local_user_dir)):
             os.makedirs(local_user_dir)
         download_location = os.path.join(
             local_user_dir, '%s.qcow2' % self.clean_path(image_name))
     elif not download_dir:
         download_dir = os.path.dirname(download_location)
     return download_dir, download_location
Ejemplo n.º 19
0
 def unshare_image(self, image, tenant_name):
     tenant = find(self.keystone.tenants, name=tenant_name)
     return self.glance.image_members.delete(image.id, tenant.id)
Ejemplo n.º 20
0
 def find_tenant(self, tenant_name):
     try:
         tenant = find(self.keystone.tenants, name=tenant_name)
         return tenant
     except NotFound:
         return None