Merge "Be smarter about what to do when making a docker client"

This commit is contained in:
Jenkins 2016-06-28 09:10:00 +00:00 committed by Gerrit Code Review
commit 89765f3123

View File

@ -121,14 +121,21 @@ def join_many(threads):
t.join() t.join()
def docker_client(): class DockerTask(task.Task):
try:
docker_kwargs = docker.utils.kwargs_from_env() docker_kwargs = docker.utils.kwargs_from_env()
return docker.Client(version='auto', **docker_kwargs)
except docker.errors.DockerException: def __init__(self):
LOG.exception('Can not communicate with docker service.' super(DockerTask, self).__init__()
'Please check docker service is running without errors') self._dc = None
sys.exit(1)
@property
def dc(self):
if self._dc is not None:
return self._dc
docker_kwargs = self.docker_kwargs.copy()
self._dc = docker.Client(version='auto', **docker_kwargs)
return self._dc
class Image(object): class Image(object):
@ -185,12 +192,11 @@ class PushIntoQueueTask(task.Task):
self.success = True self.success = True
class PushTask(task.Task): class PushTask(DockerTask):
"""Task that pushes a image to a docker repository.""" """Task that pushes a image to a docker repository."""
def __init__(self, conf, image): def __init__(self, conf, image):
super(PushTask, self).__init__() super(PushTask, self).__init__()
self.dc = docker_client()
self.conf = conf self.conf = conf
self.image = image self.image = image
self.logger = image.logger self.logger = image.logger
@ -232,14 +238,13 @@ class PushTask(task.Task):
self.logger.error(stream['errorDetail']['message']) self.logger.error(stream['errorDetail']['message'])
class BuildTask(task.Task): class BuildTask(DockerTask):
"""Task that builds out an image.""" """Task that builds out an image."""
def __init__(self, conf, image, push_queue): def __init__(self, conf, image, push_queue):
super(BuildTask, self).__init__() super(BuildTask, self).__init__()
self.conf = conf self.conf = conf
self.image = image self.image = image
self.dc = docker_client()
self.push_queue = push_queue self.push_queue = push_queue
self.nocache = not conf.cache or conf.no_cache self.nocache = not conf.cache or conf.no_cache
self.forcerm = not conf.keep self.forcerm = not conf.keep
@ -403,31 +408,35 @@ class BuildTask(task.Task):
pull = True if image.parent is None else False pull = True if image.parent is None else False
buildargs = self.update_buildargs() buildargs = self.update_buildargs()
for response in self.dc.build(path=image.path, try:
tag=image.canonical_name, for response in self.dc.build(path=image.path,
nocache=not self.conf.cache, tag=image.canonical_name,
rm=True, nocache=not self.conf.cache,
pull=pull, rm=True,
forcerm=self.forcerm, pull=pull,
buildargs=buildargs): forcerm=self.forcerm,
stream = json.loads(response.decode('utf-8')) buildargs=buildargs):
stream = json.loads(response.decode('utf-8'))
if 'stream' in stream: if 'stream' in stream:
for line in stream['stream'].split('\n'): for line in stream['stream'].split('\n'):
if line: if line:
self.logger.info('%s', line) self.logger.info('%s', line)
if 'errorDetail' in stream:
if 'errorDetail' in stream: image.status = STATUS_ERROR
image.status = STATUS_ERROR self.logger.error('Error\'d with the following message')
self.logger.error('Error\'d with the following message') for line in stream['errorDetail']['message'].split('\n'):
for line in stream['errorDetail']['message'].split('\n'): if line:
if line: self.logger.error('%s', line)
self.logger.error('%s', line) return
return except docker.errors.DockerException:
image.status = STATUS_ERROR
image.status = STATUS_BUILT self.logger.exception('Unknown docker error when building')
except Exception:
self.logger.info('Built') image.status = STATUS_ERROR
self.logger.exception('Unknown error when building')
else:
image.status = STATUS_BUILT
self.logger.info('Built')
class WorkerThread(threading.Thread): class WorkerThread(threading.Thread):