Skip to content
This repository has been archived by the owner on Apr 15, 2020. It is now read-only.

Commit

Permalink
Merged in feature/workaround-docker-py-bug (pull request #28)
Browse files Browse the repository at this point in the history
Workaround docker-py issue
  • Loading branch information
Lusheng Lv committed Sep 20, 2016
2 parents a31fecf + b9dc813 commit 3e6ea7f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 46 deletions.
2 changes: 1 addition & 1 deletion badwolf/default_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
},
'formatters': {
'default': {
'format': '%(asctime)s %(levelname)-2s %(name)s.%(funcName)s:%(lineno)-5d %(message)s', # NOQA
'format': '%(asctime)s %(levelname)-2s Process-%(process)d %(name)s.%(funcName)s:%(lineno)-5d %(message)s', # NOQA
},
},
}
Expand Down
76 changes: 38 additions & 38 deletions badwolf/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ def __init__(self, repository, actor, type, message, source,
class TestRunner(object):
"""Badwolf test runner"""

def __init__(self, context, lock):
def __init__(self, context, docker_version='auto'):
self.context = context
self.lock = lock
self.repo_full_name = context.repository
self.repo_name = context.repository.split('/')[-1]
self.task_id = str(uuid.uuid4())
Expand All @@ -65,6 +64,7 @@ def __init__(self, context, lock):
self.docker = Client(
base_url=current_app.config['DOCKER_HOST'],
timeout=current_app.config['DOCKER_API_TIMEOUT'],
version=docker_version,
)

def run(self):
Expand Down Expand Up @@ -207,38 +207,37 @@ def validate_settings(self):
def get_docker_image(self):
docker_image_name = self.repo_full_name.replace('/', '-')
output = []
with self.lock:
docker_image = self.docker.images(docker_image_name)
if not docker_image or self.context.rebuild:
dockerfile = os.path.join(self.clone_path, self.spec.dockerfile)
build_options = {
'tag': docker_image_name,
'rm': True,
}
if not os.path.exists(dockerfile):
logger.warning(
'No Dockerfile: %s found for repo: %s, using simple runner image',
dockerfile,
self.repo_full_name
)
dockerfile_content = 'FROM messense/badwolf-test-runner\n'
fileobj = io.BytesIO(dockerfile_content.encode('utf-8'))
build_options['fileobj'] = fileobj
else:
build_options['dockerfile'] = self.spec.dockerfile

build_success = False
logger.info('Building Docker image %s', docker_image_name)
self.update_build_status('INPROGRESS', 'Building Docker image')
res = self.docker.build(self.clone_path, **build_options)
for line in res:
if b'Successfully built' in line:
build_success = True
log = to_text(json.loads(to_text(line))['stream'])
output.append(log)
logger.info('`docker build` : %s', log.strip())
if not build_success:
return None, ''.join(output)
docker_image = self.docker.images(docker_image_name)
if not docker_image or self.context.rebuild:
dockerfile = os.path.join(self.clone_path, self.spec.dockerfile)
build_options = {
'tag': docker_image_name,
'rm': True,
}
if not os.path.exists(dockerfile):
logger.warning(
'No Dockerfile: %s found for repo: %s, using simple runner image',
dockerfile,
self.repo_full_name
)
dockerfile_content = 'FROM messense/badwolf-test-runner\n'
fileobj = io.BytesIO(dockerfile_content.encode('utf-8'))
build_options['fileobj'] = fileobj
else:
build_options['dockerfile'] = self.spec.dockerfile

build_success = False
logger.info('Building Docker image %s', docker_image_name)
self.update_build_status('INPROGRESS', 'Building Docker image')
res = self.docker.build(self.clone_path, **build_options)
for line in res:
if b'Successfully built' in line:
build_success = True
log = to_text(json.loads(to_text(line))['stream'])
output.append(log)
logger.info('`docker build` : %s', log.strip())
if not build_success:
return None, ''.join(output)

return docker_image_name, ''.join(output)

Expand Down Expand Up @@ -276,7 +275,9 @@ def run_tests_in_container(self, docker_image_name):
'mode': 'rw',
},
}
)
),
stdin_open=False,
tty=False
)
container_id = container['Id']
logger.info('Created container %s from image %s', container_id, docker_image_name)
Expand All @@ -285,17 +286,16 @@ def run_tests_in_container(self, docker_image_name):
try:
self.docker.start(container_id)
self.update_build_status('INPROGRESS', 'Running tests in Docker container')
for line in self.docker.logs(container_id, stream=True):
output.append(to_text(line))
exit_code = self.docker.wait(container_id, current_app.config['DOCKER_RUN_TIMEOUT'])
except (APIError, DockerException, ReadTimeout) as e:
exit_code = -1
output.append(to_text(e))
logger.exception('Docker error')
finally:
try:
output.append(to_text(self.docker.logs(container_id)))
self.docker.remove_container(container_id, force=True)
except (APIError, DockerException):
except (APIError, DockerException, ReadTimeout):
logger.exception('Error removing docker container')

return exit_code, ''.join(output)
Expand Down
6 changes: 1 addition & 5 deletions badwolf/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from __future__ import absolute_import, unicode_literals
import atexit
import logging
import multiprocessing
from concurrent.futures import ProcessPoolExecutor

from badwolf.extensions import sentry
Expand All @@ -11,8 +10,6 @@
logger = logging.getLogger(__name__)

executor = ProcessPoolExecutor(max_workers=10)
# per repository lock
_LOCKS = {}


@atexit.register
Expand Down Expand Up @@ -43,6 +40,5 @@ def delay(*args, **kwargs):
def run_test(context):
from badwolf.runner import TestRunner

lock = _LOCKS.setdefault(context.repository, multiprocessing.Lock())
runner = TestRunner(context, lock)
runner = TestRunner(context)
runner.run()
3 changes: 1 addition & 2 deletions tests/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from __future__ import absolute_import, unicode_literals
import os
import tempfile
from multiprocessing import Lock

import git
import mock
Expand All @@ -29,7 +28,7 @@ def push_context():

@pytest.fixture(scope='function')
def push_runner(push_context):
runner = TestRunner(push_context, Lock())
runner = TestRunner(push_context, docker_version=None)
runner.clone_path = os.path.join(
tempfile.gettempdir(),
'badwolf',
Expand Down

0 comments on commit 3e6ea7f

Please sign in to comment.