Prepare the build worker to support multiple tags and subdirectories. Change the build database config to accept a job config object instead of breaking out the parameters into independent blocks.

This commit is contained in:
jakedt 2014-02-24 16:11:23 -05:00
parent 4b0f4c0a7b
commit 13dea98499
9 changed files with 114 additions and 53 deletions

View file

@ -50,18 +50,23 @@ class StatusWrapper(object):
class DockerfileBuildContext(object):
def __init__(self, build_context_dir, tag_name, push_token, build_uuid):
def __init__(self, build_context_dir, dockerfile_subdir, repo, tag_names,
push_token, build_uuid):
self._build_dir = build_context_dir
self._tag_name = tag_name
self._dockerfile_subdir = dockerfile_subdir
self._repo = repo
self._tag_names = tag_names
self._push_token = push_token
self._cl = Client(timeout=1200, version='1.7')
self._status = StatusWrapper(build_uuid)
self._build_logger = partial(build_logs.append_log_message, build_uuid)
dockerfile_path = os.path.join(self._build_dir, "Dockerfile")
dockerfile_path = os.path.join(self._build_dir, dockerfile_subdir,
"Dockerfile")
self._num_steps = DockerfileBuildContext.__count_steps(dockerfile_path)
logger.debug('Will build and push to tag named: %s' % self._tag_name)
logger.debug('Will build and push to repo %s with tags named: %s' %
(self._repo, self._tag_names))
def __enter__(self):
return self
@ -94,9 +99,13 @@ class DockerfileBuildContext(object):
with self._status as status:
status['total_commands'] = self._num_steps
logger.debug('Building to tag named: %s' % self._tag_name)
build_status = self._cl.build(path=self._build_dir, tag=self._tag_name,
stream=True)
logger.debug('Building to tags named: %s' % self._tag_names)
context_path = os.path.join(self._build_dir, self._dockerfile_subdir)
logger.debug('Final context path: %s exists: %s' %
(context_path, os.path.exists(context_path)))
build_status = self._cl.build(path=context_path, stream=True)
current_step = 0
built_image = None
@ -128,9 +137,9 @@ class DockerfileBuildContext(object):
def push(self, built_image):
# Login to the registry
host = re.match(r'([a-z0-9.:]+)/.+/.+$', self._tag_name)
host = re.match(r'([a-z0-9.:]+)/.+/.+$', self._repo)
if not host:
raise RuntimeError('Invalid tag name: %s' % self._tag_name)
raise RuntimeError('Invalid repo name: %s' % self._repo)
for protocol in ['https', 'http']:
registry_endpoint = '%s://%s/v1/' % (protocol, host.group(1))
@ -142,13 +151,18 @@ class DockerfileBuildContext(object):
except APIError:
pass # Probably the wrong protocol
for tag in self._tag_names:
logger.debug('Tagging image %s as %s:%s' %
(built_image, self._repo, tag))
self._cl.tag(built_image, self._repo, tag)
history = json.loads(self._cl.history(built_image))
num_images = len(history)
with self._status as status:
status['total_images'] = num_images
logger.debug('Pushing to tag name: %s' % self._tag_name)
resp = self._cl.push(self._tag_name, stream=True)
logger.debug('Pushing to repo %s' % self._repo)
resp = self._cl.push(self._repo, stream=True)
for status_str in resp:
status = json.loads(status_str)
@ -258,8 +272,13 @@ class DockerfileBuildWorker(Worker):
job_details['repository'],
job_details['build_uuid'])
resource_url = user_files.get_file_url(repository_build.resource_key)
tag_name = repository_build.tag
job_config = json.loads(repository_build.job_config)
resource_url = user_files.get_file_url(job_config['resource_key'])
tag_names = job_config['docker_tags']
build_subdir = job_config['build_subdir']
repo = job_config['repository']
access_token = repository_build.access_token.code
log_appender = partial(build_logs.append_log_message,
@ -267,16 +286,15 @@ class DockerfileBuildWorker(Worker):
log_appender('initializing', build_logs.PHASE)
start_msg = ('Starting job with resource url: %s tag: %s' % (resource_url,
tag_name))
logger.debug(start_msg)
start_msg = ('Starting job with resource url: %s repo: %s' % (resource_url,
repo))
log_appender(start_msg)
docker_resource = requests.get(resource_url)
c_type = docker_resource.headers['content-type']
filetype_msg = ('Request to build file of type: %s with tag: %s' %
(c_type, tag_name))
filetype_msg = ('Request to build type: %s with repo: %s and tags: %s' %
(c_type, repo, tag_names))
logger.info(filetype_msg)
log_appender(filetype_msg)
@ -288,7 +306,8 @@ class DockerfileBuildWorker(Worker):
repository_build.phase = 'building'
repository_build.save()
with DockerfileBuildContext(build_dir, tag_name, access_token,
with DockerfileBuildContext(build_dir, build_subdir, repo, tag_names,
access_token,
repository_build.uuid) as build_ctxt:
try:
built_image = build_ctxt.build()
@ -298,7 +317,7 @@ class DockerfileBuildWorker(Worker):
repository_build.phase = 'error'
repository_build.save()
log_appender('Unable to build dockerfile.', build_logs.ERROR)
return False
return True
log_appender('pushing', build_logs.PHASE)
repository_build.phase = 'pushing'
@ -316,7 +335,7 @@ class DockerfileBuildWorker(Worker):
repository_build.phase = 'error'
repository_build.save()
log_appender(str(exc), build_logs.ERROR)
return False
return True
return True