2014-11-12 19:03:07 +00:00
|
|
|
from data import model
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
|
|
|
class BuildJobLoadException(Exception):
|
|
|
|
""" Exception raised if a build job could not be instantiated for some reason. """
|
|
|
|
pass
|
|
|
|
|
|
|
|
class BuildJob(object):
|
|
|
|
""" Represents a single in-progress build job. """
|
|
|
|
def __init__(self, job_item):
|
2014-12-16 18:41:30 +00:00
|
|
|
self.job_item = job_item
|
2014-11-12 19:03:07 +00:00
|
|
|
|
|
|
|
try:
|
2014-12-16 18:41:30 +00:00
|
|
|
self.job_details = json.loads(job_item.body)
|
2014-11-12 19:03:07 +00:00
|
|
|
except ValueError:
|
|
|
|
raise BuildJobLoadException(
|
2014-12-16 18:41:30 +00:00
|
|
|
'Could not parse build queue item config with ID %s' % self.job_details['build_uuid']
|
2014-11-18 20:45:56 +00:00
|
|
|
)
|
2014-11-12 19:03:07 +00:00
|
|
|
|
|
|
|
try:
|
2014-12-16 18:41:30 +00:00
|
|
|
self.repo_build = model.get_repository_build(self.job_details['build_uuid'])
|
2014-11-12 19:03:07 +00:00
|
|
|
except model.InvalidRepositoryBuildException:
|
|
|
|
raise BuildJobLoadException(
|
2014-12-16 18:41:30 +00:00
|
|
|
'Could not load repository build with ID %s' % self.job_details['build_uuid'])
|
2014-11-12 19:03:07 +00:00
|
|
|
|
|
|
|
try:
|
2014-12-16 18:41:30 +00:00
|
|
|
self.build_config = json.loads(self.repo_build.job_config)
|
2014-11-12 19:03:07 +00:00
|
|
|
except ValueError:
|
|
|
|
raise BuildJobLoadException(
|
2014-12-16 18:41:30 +00:00
|
|
|
'Could not parse repository build job config with ID %s' % self.job_details['build_uuid']
|
2014-11-18 20:45:56 +00:00
|
|
|
)
|
2014-11-12 19:03:07 +00:00
|
|
|
|
|
|
|
def determine_cached_tag(self):
|
|
|
|
""" Returns the tag to pull to prime the cache or None if none. """
|
|
|
|
# TODO(jschorr): Change this to use the more complicated caching rules, once we have caching
|
|
|
|
# be a pull of things besides the constructed tags.
|
2014-12-16 18:41:30 +00:00
|
|
|
tags = self.build_config.get('docker_tags', ['latest'])
|
|
|
|
existing_tags = model.list_repository_tags(self.repo_build.repository.namespace_user.username,
|
|
|
|
self.repo_build.repository.name)
|
2014-11-12 19:03:07 +00:00
|
|
|
|
|
|
|
cached_tags = set(tags) & set([tag.name for tag in existing_tags])
|
|
|
|
if cached_tags:
|
2014-11-14 00:41:17 +00:00
|
|
|
return list(cached_tags)[0]
|
2014-11-12 19:03:07 +00:00
|
|
|
|
|
|
|
return None
|