Beispiel #1
0
    def run_job(self):
        """
        Deprecate image in all target regions in each source region.
        """
        self.status = SUCCESS

        if self.old_cloud_image_name is None:
            # There is no old image that needs deprecate for the job.
            return

        self.request_credentials([self.account])
        credentials = self.credentials[self.account]

        aliyun_image = AliyunImage(credentials['access_key'],
                                   credentials['access_secret'],
                                   self.region,
                                   self.bucket,
                                   log_callback=self.log_callback)

        try:
            aliyun_image.deprecate_image_in_regions(self.old_cloud_image_name)
        except Exception as error:
            raise MashDeprecateException(
                'Failed to deprecate image {0}: {1}'.format(
                    self.old_cloud_image_name, error))
Beispiel #2
0
    def run_job(self):
        """
        Publish image and update status.
        """
        self.status = SUCCESS
        self.cloud_image_name = self.status_msg['cloud_image_name']

        self.request_credentials([self.account])
        credentials = self.credentials[self.account]

        aliyun_image = AliyunImage(credentials['access_key'],
                                   credentials['access_secret'],
                                   self.region,
                                   self.bucket,
                                   log_callback=self.log_callback)

        try:
            aliyun_image.publish_image_to_regions(self.cloud_image_name,
                                                  self.launch_permission)
        except Exception as error:
            raise MashPublishException(
                'Failed to publish image {0}: {1}'.format(
                    self.cloud_image_name, error))
Beispiel #3
0
    def run_job(self):
        self.status = SUCCESS
        self.percent_uploaded = 0
        self.progress_log = {}
        self.log_callback.info('Uploading image.')

        timestamp = None
        build_time = self.status_msg.get('build_time', 'unknown')

        if self.use_build_time and (build_time != 'unknown'):
            timestamp = timestamp_from_epoch(build_time)
        elif self.use_build_time and (build_time == 'unknown'):
            raise MashUploadException(
                'use_build_time set for job but build time is unknown.')

        self.cloud_image_name = format_string_with_date(
            self.base_cloud_image_name, timestamp=timestamp)

        self.request_credentials([self.account])
        credentials = self.credentials[self.account]

        aliyun_image = AliyunImage(credentials['access_key'],
                                   credentials['access_secret'],
                                   self.region,
                                   self.bucket,
                                   log_callback=self.log_callback)

        object_name = ''.join([self.cloud_image_name, '.qcow2'])

        exists = aliyun_image.image_tarball_exists(object_name)
        if exists and not self.force_replace_image:
            raise MashUploadException(
                'Image: {object_name} already exists '
                'in bucket: {bucket}. Use force_replace_image '
                'to replace the existing tarball.'.format(
                    object_name=object_name, bucket=self.bucket))
        elif exists and self.force_replace_image:
            self.log_callback.info(
                'Deleting image file: {0}, in the bucket named: {1}'.format(
                    object_name, self.bucket))
            aliyun_image.delete_storage_blob(object_name)

        aliyun_image.upload_image_tarball(
            self.status_msg['image_file'],
            blob_name=object_name,
            progress_callback=self.progress_callback)

        self.status_msg['cloud_image_name'] = self.cloud_image_name
        self.status_msg['object_name'] = object_name
        self.log_callback.info(
            'Uploaded image: {0}, to the bucket named: {1}'.format(
                object_name, self.bucket))
Beispiel #4
0
    def run_job(self):
        self.status = SUCCESS
        self.status_msg['source_regions'] = {}
        self.log_callback.info('Creating image.')

        self.cloud_image_name = self.status_msg['cloud_image_name']
        object_name = self.status_msg['object_name']

        self.request_credentials([self.account])
        credentials = self.credentials[self.account]

        aliyun_image = AliyunImage(
            credentials['access_key'],
            credentials['access_secret'],
            self.region,
            self.bucket,
            log_callback=self.log_callback
        )

        try:
            existing_image = aliyun_image.get_compute_image(
                image_name=self.cloud_image_name
            )
        except Exception:
            existing_image = None

        if existing_image:
            self.log_callback.info(
                'Replacing existing image with the same name.'
            )
            aliyun_image.delete_compute_image(self.cloud_image_name)

        kwargs = {}
        if self.disk_size:
            kwargs['disk_image_size'] = self.disk_size

        image_id = aliyun_image.create_compute_image(
            self.cloud_image_name,
            self.cloud_image_description,
            object_name,
            platform=self.platform,
            arch=self.cloud_architecture,
            **kwargs
        )

        self.status_msg['source_regions'][self.region] = image_id
        self.log_callback.info(
            'Created image: {0}'.format(
                self.cloud_image_name
            )
        )
Beispiel #5
0
    def run_job(self):
        """
        Replicate image to all target regions in each source region.
        """
        self.status = SUCCESS
        self.cloud_image_name = self.status_msg['cloud_image_name']

        self.request_credentials([self.account])
        credentials = self.credentials[self.account]

        aliyun_image = AliyunImage(credentials['access_key'],
                                   credentials['access_secret'],
                                   self.region,
                                   self.bucket,
                                   log_callback=self.log_callback)

        self.log_callback.info(
            'Replicating {image}'.format(image=self.cloud_image_name))

        images = aliyun_image.replicate_image(self.cloud_image_name)

        for region, image_id in images.items():
            if image_id:
                aliyun_image.region = region
                try:
                    aliyun_image.wait_on_compute_image(image_id)
                except Exception:
                    self.status = FAILED
            else:
                self.status = FAILED

            if self.status == FAILED:
                msg = 'Replicate to {0} failed.'.format(region)
                self.add_error_msg(msg)
                self.log_callback.warning(msg)

        # Merge region to image id hash
        self.status_msg['source_regions'] = {
            **self.status_msg['source_regions'],
            **images
        }
Beispiel #6
0
    def cleanup_image(self, credentials):
        aliyun_image = AliyunImage(credentials['access_key'],
                                   credentials['access_secret'],
                                   self.region,
                                   self.bucket,
                                   log_callback=self.log_callback)

        self.log_callback.info('Cleaning up image: {0} in region: {1}.'.format(
            self.cloud_image_name, self.region))

        try:
            aliyun_image.delete_compute_image(self.cloud_image_name)
        except Exception as error:
            msg = 'Failed to cleanup image: {0}'.format(error)
            self.log_callback.warning(msg)
            self.add_error_msg(msg)

        try:
            aliyun_image.delete_storage_blob(self.object_name)
        except Exception as error:
            msg = 'Failed to cleanup image blob: {0}'.format(error)
            self.log_callback.warning(msg)
            self.add_error_msg(msg)