def try_new_build(source, target): img = maybe_get_image(target, source) if img: attributes = { 'target': json.dumps(target.to_json()), 'source': json.dumps(source.to_json()), 'image': img, 'type': BUILD_JOB_TYPE } try: job = batch_client.create_job( img, command=['/bin/bash', '-c', PR_BUILD_SCRIPT], env={ 'SOURCE_REPO_URL': source.ref.repo.url, 'SOURCE_BRANCH': source.ref.name, 'SOURCE_SHA': source.sha, 'TARGET_REPO_URL': target.ref.repo.url, 'TARGET_BRANCH': target.ref.name, 'TARGET_SHA': target.sha }, resources={'requests': { 'cpu': '3.7', 'memory': '4G' }}, tolerations=[{ 'key': 'preemptible', 'value': 'true' }], service_account_name='test-svc', callback=SELF_HOSTNAME + '/ci_build_done', attributes=attributes, volumes=[{ 'volume': { 'name': f'hail-ci-{VERSION}-service-account-key', 'secret': { 'optional': False, 'secretName': f'hail-ci-{VERSION}-service-account-key' } }, 'volume_mount': { 'mountPath': '/secrets', 'name': f'hail-ci-{VERSION}-service-account-key', 'readOnly': True } }]) return Building(job, img, target.sha) except Exception as e: log.exception(f'could not start batch job due to {e}') return Buildable(img, target.sha) else: return NoImage(target.sha)
def refresh_from_batch_job(self, job): state = job.cached_status()['state'] if state == 'Complete': return self.update_from_completed_batch_job(job) elif state == 'Cancelled': log.error( f'a job for me was cancelled {short_str_build_job(job)} {self.short_str()}') job.delete() return self._new_build(try_new_build(self.source, self.target)) else: assert state == 'Created', f'{state} {job.id} {job.attributes} {self.short_str()}' assert 'target' in job.attributes, job.attributes assert 'image' in job.attributes, job.attributes target = FQSHA.from_json(json.loads(job.attributes['target'])) image = job.attributes['image'] if target == self.target: return self._new_build(Building(job, image, target.sha)) else: log.info(f'found deploy job {job.id} for wrong target {target}, should be {self.target}') job.delete() return self