def get_create_message(self): """ Build create job message. """ create_message = { 'create_job': { 'cloud': self.cloud, 'image_description': self.image_description, 'account': self.cloud_account, 'bucket': self.bucket, 'region': self.region, 'availability_domain': self.availability_domain, 'compartment_id': self.compartment_id, 'oci_user_id': self.oci_user_id, 'tenancy': self.tenancy, 'operating_system': self.operating_system, 'operating_system_version': self.operating_system_version } } create_message['create_job'].update(self.base_message) if self.image_type: create_message['create_job']['image_type'] = self.image_type if self.launch_mode: create_message['create_job']['launch_mode'] = self.launch_mode return JsonFormat.json_message(create_message)
def get_obs_message(self): """ Build OBS job message. """ obs_message = { 'obs_job': { 'download_url': self.download_url, 'image': self.image } } obs_message['obs_job'].update(self.base_message) if self.cloud_architecture: obs_message['obs_job']['cloud_architecture'] = \ self.cloud_architecture if self.conditions: obs_message['obs_job']['conditions'] = self.conditions if self.profile: obs_message['obs_job']['profile'] = self.profile if self.conditions_wait_time: obs_message['obs_job']['conditions_wait_time'] = \ self.conditions_wait_time if self.disallow_licenses: obs_message['obs_job'][ 'disallow_licenses'] = self.disallow_licenses if self.disallow_packages: obs_message['obs_job'][ 'disallow_packages'] = self.disallow_packages return JsonFormat.json_message(obs_message)
def get_test_message(self): """ Build test job message. """ test_message = { 'test_job': { 'cloud': self.cloud, 'tests': self.tests, 'account': self.cloud_account, 'bucket': self.bucket, 'region': self.region, 'security_group_id': self.security_group_id, 'vswitch_id': self.vswitch_id, 'distro': self.distro, 'instance_type': self.instance_type } } if self.last_service == 'test' and \ self.cleanup_images in [True, None]: test_message['test_job']['cleanup_images'] = True elif self.cleanup_images is False: test_message['test_job']['cleanup_images'] = False test_message['test_job'].update(self.base_message) return JsonFormat.json_message(test_message)
def get_test_message(self): """ Build test job message. """ test_message = { 'test_job': { 'cloud': self.cloud, 'tests': self.tests, 'account': self.cloud_account, 'bucket': self.bucket, 'region': self.region, 'distro': self.distro, 'instance_type': self.instance_type, 'availability_domain': self.availability_domain, 'compartment_id': self.compartment_id, 'oci_user_id': self.oci_user_id, 'tenancy': self.tenancy } } if self.last_service == 'test' and \ self.cleanup_images in [True, None]: test_message['test_job']['cleanup_images'] = True elif self.cleanup_images is False: test_message['test_job']['cleanup_images'] = False test_message['test_job'].update(self.base_message) return JsonFormat.json_message(test_message)
def get_upload_message(self): """ Build upload job message. """ upload_message = { 'upload_job': { 'cloud_image_name': self.cloud_image_name, 'cloud': self.cloud, 'account': self.cloud_account, 'region': self.region, 'container': self.source_container, 'resource_group': self.source_resource_group, 'storage_account': self.source_storage_account, 'raw_image_upload_type': self.raw_image_upload_type, 'raw_image_upload_account': self.raw_image_upload_account, 'raw_image_upload_location': self.raw_image_upload_location, 'use_build_time': self.use_build_time, 'force_replace_image': self.force_replace_image } } if self.additional_uploads: upload_message['upload_job']['additional_uploads'] = \ self.additional_uploads upload_message['upload_job'].update(self.base_message) return JsonFormat.json_message(upload_message)
def _get_status_message(self, job): """ Build and return json message. Message contains completion status to post to next service exchange. """ key = '{0}_result'.format(self.service_exchange) return JsonFormat.json_message({key: job.get_status_message()})
def get_replicate_message(self): """ Build replicate job message and publish to replicate exchange. """ replicate_message = {'replicate_job': {'cloud': self.cloud}} replicate_message['replicate_job'].update(self.base_message) return JsonFormat.json_message(replicate_message)
def get_publish_message(self): """ Build publish job message. """ publish_message = {'publish_job': {'cloud': self.cloud}} publish_message['publish_job'].update(self.base_message) return JsonFormat.json_message(publish_message)
def get_deprecate_message(self): """ Build deprecate job message. """ deprecate_message = {'deprecate_job': {'cloud': self.cloud}} deprecate_message['deprecate_job'].update(self.base_message) return JsonFormat.json_message(deprecate_message)
def create_json_file(data): try: temp_file = NamedTemporaryFile(delete=False) with open(temp_file.name, 'w') as json_file: json_file.write(JsonFormat.json_message(data)) yield temp_file.name finally: with suppress(OSError): os.remove(temp_file.name)
def test_service_handle_listener_message_no_job(self): self.message.body = JsonFormat.json_message({ "test_result": { "cloud_image_name": "image123", "id": "1", "status": "success", "errors": [] } }) self.service._handle_listener_message(self.message) self.message.ack.assert_called_once_with()
def get_publish_message(self): """ Build publish job message. """ publish_message = { 'publish_job': { 'cloud': self.cloud, 'allow_copy': self.allow_copy, 'share_with': self.share_with, 'publish_regions': self.get_publish_regions() } } publish_message['publish_job'].update(self.base_message) return JsonFormat.json_message(publish_message)
def get_replicate_message(self): """ Build replicate job message and publish to replicate exchange. """ replicate_message = { 'replicate_job': { 'cloud': self.cloud, 'account': self.cloud_account, 'bucket': self.bucket, 'region': self.region } } replicate_message['replicate_job'].update(self.base_message) return JsonFormat.json_message(replicate_message)
def get_replicate_message(self): """ Build replicate job message and publish to replicate exchange. """ replicate_message = { 'replicate_job': { 'image_description': self.image_description, 'cloud': self.cloud, 'replicate_source_regions': self.get_replicate_source_regions() } } replicate_message['replicate_job'].update(self.base_message) return JsonFormat.json_message(replicate_message)
def test_create_json_file(mock_temp_file, mock_os): json_file = MagicMock() json_file.name = 'test.json' mock_temp_file.return_value = json_file data = {'tenantId': '123456', 'subscriptionId': '98765'} with patch('builtins.open', create=True) as mock_open: mock_open.return_value = MagicMock(spec=io.IOBase) with create_json_file(data) as json_file: assert json_file == 'test.json' file_handle = mock_open.return_value.__enter__.return_value file_handle.write.assert_called_with(JsonFormat.json_message(data)) mock_os.remove.assert_called_once_with('test.json')
def get_upload_message(self): """ Build upload job message. """ upload_message = { 'upload_job': { 'cloud_image_name': self.cloud_image_name, 'cloud': self.cloud, 'raw_image_upload_type': self.raw_image_upload_type, 'raw_image_upload_account': self.raw_image_upload_account, 'raw_image_upload_location': self.raw_image_upload_location } } upload_message['upload_job'].update(self.base_message) return JsonFormat.json_message(upload_message)
def get_raw_image_upload_message(self): """ Build raw image upload job message. """ raw_image_upload_message = { 'raw_image_upload_job': { 'cloud': self.cloud, 'raw_image_upload_type': self.raw_image_upload_type, 'raw_image_upload_account': self.raw_image_upload_account, 'raw_image_upload_location': self.raw_image_upload_location } } raw_image_upload_message['raw_image_upload_job'].update( self.base_message) return JsonFormat.json_message(raw_image_upload_message)
def test_service_handle_listener_message_failed(self, mock_cleanup_job): job = Mock() job.id = '1' job.utctime = 'now' self.service.jobs['1'] = job self.message.body = JsonFormat.json_message({ "test_result": { "cloud_image_name": "image123", "id": "1", "status": "failed", "errors": ['Something went terribly wrong!'] } }) self.service._handle_listener_message(self.message) mock_cleanup_job.assert_called_once_with('1')
def get_publish_message(self): """ Build publish job message. """ publish_message = { 'publish_job': { 'cloud': self.cloud, 'bucket': self.bucket, 'account': self.cloud_account, 'region': self.region, 'launch_permission': self.launch_permission } } publish_message['publish_job'].update(self.base_message) return JsonFormat.json_message(publish_message)
def send_job(self, job_doc): """ Create instance of job and send to all services to initiate job. """ job = create_job(job_doc) self.log.info( 'Started a new job: {0}'.format(JsonFormat.json_message(job_doc)), extra={'job_id': job.id} ) for service in self.services: if service == 'deprecate': self.publish_job_doc( 'deprecate', job.get_deprecate_message() ) elif service == 'create': self.publish_job_doc( 'create', job.get_create_message() ) elif service == 'obs': self.publish_job_doc( 'obs', job.get_obs_message() ) elif service == 'publish': self.publish_job_doc( 'publish', job.get_publish_message() ) elif service == 'replicate': self.publish_job_doc( 'replicate', job.get_replicate_message() ) elif service == 'test': self.publish_job_doc( 'test', job.get_test_message() ) elif service == 'upload': self.publish_job_doc( 'upload', job.get_upload_message() ) elif service == 'raw_image_upload': self.publish_job_doc( 'raw_image_upload', job.get_raw_image_upload_message() ) if service == job.last_service: break
def test_service_cleanup_job(self, mock_publish_message, mock_delete_job): job = Mock() job.id = '1' job.status = 'failed' job.utctime = 'now' job.get_job_id.return_value = {'job_id': '1'} job.get_status_message.return_value = {'id': '1', 'status': 'failed'} self.service.jobs['1'] = job self.service._cleanup_job('1') self.service.log.warning.assert_called_once_with('Failed upstream.', extra={'job_id': '1'}) mock_delete_job.assert_called_once_with('1') msg = {"replicate_result": {"id": "1", "status": "failed"}} mock_publish_message.assert_called_once_with( JsonFormat.json_message(msg), '1')
def get_deprecate_message(self): """ Build deprecate job message. """ deprecate_message = { 'deprecate_job': { 'cloud': self.cloud, 'account': self.cloud_account } } deprecate_message['deprecate_job'].update(self.base_message) if self.old_cloud_image_name: deprecate_message['deprecate_job']['old_cloud_image_name'] = \ self.old_cloud_image_name return JsonFormat.json_message(deprecate_message)
def get_create_message(self): """ Build create job message. """ create_message = { 'create_job': { 'cloud': self.cloud, 'image_description': self.image_description, 'family': self.family, 'guest_os_features': self.guest_os_features, 'account': self.cloud_account, 'bucket': self.bucket, 'region': self.region } } create_message['create_job'].update(self.base_message) return JsonFormat.json_message(create_message)
def test_service_handle_listener_message(self, mock_schedule_job): job = Mock() job.id = '1' job.utctime = 'now' self.service.jobs['1'] = job self.message.body = JsonFormat.json_message({ "test_result": { "cloud_image_name": "image123", "id": "1", "status": "success", "errors": [] } }) self.service._handle_listener_message(self.message) assert self.service.jobs['1'].listener_msg == self.message mock_schedule_job.assert_called_once_with('1')
def get_create_message(self): """ Build create job message. """ create_message = { 'create_job': { 'cloud': self.cloud, 'account': self.cloud_account, 'region': self.region, 'container': self.source_container, 'resource_group': self.source_resource_group, 'storage_account': self.source_storage_account, 'boot_firmware': self.boot_firmware } } create_message['create_job'].update(self.base_message) return JsonFormat.json_message(create_message)
def get_create_message(self): """ Build create job message. """ create_message = { 'create_job': { 'cloud_image_name': self.cloud_image_name, 'cloud': self.cloud, 'image_description': self.image_description, 'use_build_time': self.use_build_time, 'target_regions': self.get_create_regions(), 'force_replace_image': self.force_replace_image } } create_message['create_job'].update(self.base_message) if self.cloud_architecture: create_message['create_job']['cloud_architecture'] = \ self.cloud_architecture return JsonFormat.json_message(create_message)
def get_upload_message(self): """ Build upload job message. """ upload_message = { 'upload_job': { 'cloud_image_name': self.cloud_image_name, 'cloud': self.cloud, 'account': self.cloud_account, 'bucket': self.bucket, 'region': self.region, 'raw_image_upload_type': self.raw_image_upload_type, 'raw_image_upload_account': self.raw_image_upload_account, 'raw_image_upload_location': self.raw_image_upload_location, 'use_build_time': self.use_build_time, 'force_replace_image': self.force_replace_image } } upload_message['upload_job'].update(self.base_message) return JsonFormat.json_message(upload_message)
def get_create_message(self): """ Build create job message. """ create_message = { 'create_job': { 'cloud': self.cloud, 'image_description': self.image_description, 'account': self.cloud_account, 'bucket': self.bucket, 'region': self.region, 'platform': self.platform, 'cloud_architecture': self.cloud_architecture } } create_message['create_job'].update(self.base_message) if self.disk_size: create_message['create_job']['disk_size'] = self.disk_size return JsonFormat.json_message(create_message)
def test_publishing_process_job_result_fail(self, mock_publish_message, mock_delete_job): event = Mock() event.job_id = '1' event.exception = None job = Mock() job.id = '1' job.status = 'error' job.utctime = 'now' job.get_job_id.return_value = {'job_id': '1'} job.get_status_message.return_value = {"id": "1", "status": "error"} self.service.jobs['1'] = job self.service._process_job_result(event) self.service.log.error.assert_called_once_with( 'Error occurred in replicate.', extra={'job_id': '1'}) mock_delete_job('1') msg = {"replicate_result": {"id": "1", "status": "error"}} mock_publish_message.assert_called_once_with( JsonFormat.json_message(msg), '1')
def handle_request(url, endpoint, method, job_data=None): """ Post request based on endpoint and data. If response is unsuccessful raise exception. """ request_method = getattr(requests, method) data = None if not job_data else JsonFormat.json_message(job_data) uri = ''.join([url, endpoint]) response = request_method(uri, data=data) if response.status_code not in (200, 201): try: msg = response.json()['msg'] except Exception: msg = 'Request to {uri} failed: {reason}'.format( uri=uri, reason=response.reason) raise MashException(msg) return response