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

Commit

Permalink
Add binding Docker socket support. Closes #23
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Apr 16, 2017
1 parent 8d8a94f commit 054bd99
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 8 deletions.
19 changes: 13 additions & 6 deletions badwolf/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,19 +185,26 @@ def run_in_container(self, docker_image_name):
if self.context.pr_id:
environment['BADWOLF_PULL_REQUEST'] = to_text(self.context.pr_id)

volumes = {
self.context.clone_path: {
'bind': '/mnt/src',
'mode': 'rw',
},
}
if self.spec.docker:
volumes['/var/run/docker.sock'] = {
'bind': '/var/run/docker.sock',
'mode': 'ro',
}
environment.setdefault('DOCKER_HOST', 'unix:///var/run/docker.sock')
logger.debug('Docker container environment: \n %r', environment)
container = self.docker.containers.create(
docker_image_name,
entrypoint=['/bin/sh', '-c'],
command=['echo $BADWOLF_SCRIPT | base64 --decode | /bin/sh'],
environment=environment,
working_dir='/mnt/src',
volumes={
self.context.clone_path: {
'bind': '/mnt/src',
'mode': 'rw',
},
},
volumes=volumes,
privileged=self.spec.privileged,
stdin_open=False,
tty=True
Expand Down
2 changes: 1 addition & 1 deletion badwolf/default_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
SENTRY_DSN = os.environ.get('SENTRY_DSN', '')

# Docker
DOCKER_HOST = os.environ.get('DOCKER_HOST', 'unix://var/run/docker.sock')
DOCKER_HOST = os.environ.get('DOCKER_HOST', 'unix:///var/run/docker.sock')
DOCKER_API_TIMEOUT = int(os.environ.get('DOCKER_API_TIMEOUT', 600))
DOCKER_RUN_TIMEOUT = int(os.environ.get('DOCKER_RUN_TIMEOUT', 1200))

Expand Down
2 changes: 2 additions & 0 deletions badwolf/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ def _postprocess(self, data):
class SpecificationSchema(Schema):
image = fields.String(missing=None)
dockerfile = fields.String(missing='Dockerfile')
docker = fields.Boolean(missing=False)
privileged = fields.Boolean(missing=False)
services = ListField(fields.String(), load_from='service', missing=list)
branch = SetField(fields.String(), missing=set)
Expand Down Expand Up @@ -175,6 +176,7 @@ def __init__(self):
self.services = []
self.scripts = []
self.dockerfile = 'Dockerfile'
self.docker = False # Bind Docker sock to container or not
self.after_success = []
self.after_failure = []
self.notification = ObjectDict(
Expand Down
2 changes: 1 addition & 1 deletion docs/build.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ Tips
* 在 commit 的 message 中包含 `ci skip` 跳过测试
* 在评论中包含 `ci retry` 重跑测试
* 在评论或 commit message 或 Pull Request 的标题/描述中包含 `ci rebuild` 重新构建 Docker 镜像,同时包含 `no cache` 禁用 Docker 构建缓存
* 在 Pull Request 的标题/描述中包含 `merge skip` 禁用自动合并 Pull Request 功能
* 在 Pull Request 的标题/描述中包含 `merge skip` 或者 `wip` 或者 `working in progress` 禁用自动合并 Pull Request 功能
17 changes: 17 additions & 0 deletions tests/test_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,23 @@ def test_parse_privileged(app):
assert not spec.privileged


def test_parse_docker(app):
s = """docker: True"""
f = io.StringIO(s)
spec = Specification.parse_file(f)
assert spec.docker

s = """docker: no"""
f = io.StringIO(s)
spec = Specification.parse_file(f)
assert not spec.docker

s = """linter: flake8"""
f = io.StringIO(s)
spec = Specification.parse_file(f)
assert not spec.docker


def test_parse_image(app):
s = """script: ls"""
f = io.StringIO(s)
Expand Down

0 comments on commit 054bd99

Please sign in to comment.