Created
August 30, 2016 16:35
-
-
Save benley/ae20e3ac6dcf737ee9bacc6bf6f010de to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def _get_docker_env(repository_ctx): | |
"""Get whitelisted environment variables from the local system.""" | |
env = {} | |
env_whitelist = [ | |
"HTTP_PROXY", "http_proxy", | |
"HTTPS_PROXY", "https_proxy", | |
"NO_PROXY", "no_proxy", | |
] | |
for k, v in repository_ctx.os.environ.items(): | |
if k in env_whitelist or k.startswith("DOCKER"): | |
env[k] = v | |
return env | |
def _docker_pull_impl(repository_ctx): | |
"""Implementation for docker_pull rules""" | |
image = repository_ctx.attr.image | |
env = _get_docker_env(repository_ctx) | |
if image.endswith(":latest"): | |
print("\n".join([ | |
"In @%s, pulling docker image %s:" % (repository_ctx.name, image), | |
"Pulling :latest images is not recommended, as it breaks repeatability!" | |
])) | |
repository_ctx.file( | |
"BUILD", | |
"\n".join([ | |
"docker_build(", | |
" name = 'image',", | |
" base = ':base.tar',", | |
" visibility = ['//visibility:public'],", | |
")", | |
"", | |
]), | |
False # not executable | |
) | |
result = repository_ctx.execute( | |
["docker", "pull", image], | |
600, env) | |
if result.return_code != 0: | |
fail("Docker pull failed with error code %s:\n%s" % | |
(result.return_code, result.stderr)) | |
result = repository_ctx.execute( | |
["docker", "save", "-o", "base.tar", image], | |
600, env) | |
if result.return_code != 0: | |
fail("Docker save failed with error code %s:\n%s" % | |
(result.return_code, result.stderr)) | |
docker_pull = repository_rule( | |
implementation = _docker_pull_impl, | |
attrs = { | |
"image": attr.string(mandatory=True), | |
}, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment