Merge branch 'feature/docker-args' into develop

This commit is contained in:
wpetit 2017-07-24 17:18:53 +02:00
commit 71f2bb767f
2 changed files with 39 additions and 26 deletions

View File

@ -52,8 +52,9 @@ def get_workspace_subdir(subdir):
def run(cmd, captureOutput=False, pty=False, debug=False, **kwargs): def run(cmd, captureOutput=False, pty=False, debug=False, **kwargs):
"""Execute an arbitrary command on the system""" """Execute an arbitrary command on the system"""
if debug: if debug:
print(" ".join(cmd)) print(" ".join(cmd) if isinstance(cmd, list) else cmd)
stdin=subprocess.PIPE stdin=subprocess.PIPE
kwargs['shell'] = False if isinstance(cmd, list) else True
if pty: if pty:
kwargs['stdin'] = sys.stdin kwargs['stdin'] = sys.stdin
if captureOutput: if captureOutput:
@ -62,4 +63,8 @@ def run(cmd, captureOutput=False, pty=False, debug=False, **kwargs):
return subprocess.check_call(cmd, **kwargs) return subprocess.check_call(cmd, **kwargs)
def run_docker(args, captureOutput=False, **kwargs): def run_docker(args, captureOutput=False, **kwargs):
return run(["docker"] + args, captureOutput=captureOutput, **kwargs) if isinstance(args, list):
cmd = ["docker"] + args
else:
cmd = "docker " + args
return run(cmd, captureOutput=captureOutput, **kwargs)

56
package
View File

@ -21,6 +21,7 @@ def create_args_parser():
parser.add_argument("--rebuild", help="Ignore cache and rebuild container's image", action="store_true", default=False) parser.add_argument("--rebuild", help="Ignore cache and rebuild container's image", action="store_true", default=False)
parser.add_argument("--debug", help="Will add extra output and start the container in interactive mode", action="store_true", default=False) parser.add_argument("--debug", help="Will add extra output and start the container in interactive mode", action="store_true", default=False)
parser.add_argument("--cleanup", help="Clear the workspace and remove obsolete Docker images before build", action="store_true", default=False) parser.add_argument("--cleanup", help="Clear the workspace and remove obsolete Docker images before build", action="store_true", default=False)
parser.add_argument("--override-docker-args", help="Override all 'docker run' arguments. Use '[IMAGE_TAG]', '[PROFILE]' and '[ARCH]' to insert the corresponding values into your command.", default="")
return parser return parser
@ -63,8 +64,6 @@ if __name__ == "__main__":
parser = create_args_parser() parser = create_args_parser()
args = parser.parse_args() args = parser.parse_args()
validate_args(args)
if args.cleanup: if args.cleanup:
cleanup(debug=args.debug) cleanup(debug=args.debug)
@ -84,32 +83,41 @@ if __name__ == "__main__":
image_tag = build_image(build_workspace, base_image, args.profile, profile, debug=args.debug, rebuild=args.rebuild) image_tag = build_image(build_workspace, base_image, args.profile, profile, debug=args.debug, rebuild=args.rebuild)
# rkt run arguments
docker_args = [
"run",
"--rm",
"-v", "{:s}:/src:ro".format(project_dir),
"-v", "{:s}:/dist".format(output_dir),
"-v", "{:s}:/tamarin/hooks:ro".format(tamarin.get_hooks_dir()),
"-v", "{:s}:/tamarin/lib:ro".format(tamarin.get_lib_dir()),
"-v", "{:s}:/tamarin/profiles:ro".format(tamarin.get_profiles_dir())
]
# Use environment proxy if defined
for proxy_var in ['HTTP_PROXY', 'HTTPS_PROXY', 'http_proxy', 'https_proxy']:
if proxy_var in os.environ:
docker_args += ["-e", "{:s}={:s}".format(proxy_var, os.environ[proxy_var])]
kwargs = dict() kwargs = dict()
kwargs['debug'] = args.debug kwargs['debug'] = args.debug
if args.debug: docker_args = []
kwargs['pty'] = True
docker_args += ["-it", image_tag, "/bin/sh"] # Append custom arguments
helper_cmd = " ".join(["/usr/bin/python3", "/tamarin/lib/build.py", args.profile, args.architecture]) if args.override_docker_args != "":
print("Executer '{:s}' pour lancer la construction du paquet.".format(helper_cmd)) docker_args = args.override_docker_args.replace('[IMAGE_TAG]', image_tag)
docker_args = docker_args.replace('[PROFILE]', args.profile)
docker_args = docker_args.replace('[ARCH]', args.architecture)
else: else:
docker_args += [image_tag, "/usr/bin/python3", "/tamarin/lib/build.py", args.profile, args.architecture]
docker_args += [ "run", "--rm" ]
# volumes definition
docker_args += [
"-v", "{:s}:/src:ro".format(project_dir),
"-v", "{:s}:/dist".format(output_dir),
"-v", "{:s}:/tamarin/hooks:ro".format(tamarin.get_hooks_dir()),
"-v", "{:s}:/tamarin/lib:ro".format(tamarin.get_lib_dir()),
"-v", "{:s}:/tamarin/profiles:ro".format(tamarin.get_profiles_dir())
]
# Use environment proxy if defined
for proxy_var in ['HTTP_PROXY', 'HTTPS_PROXY', 'http_proxy', 'https_proxy']:
if proxy_var in os.environ:
docker_args += ["-e", "{:s}={:s}".format(proxy_var, os.environ[proxy_var])]
if args.debug:
kwargs['pty'] = True
docker_args += ["-it", image_tag, "/bin/sh"]
helper_cmd = " ".join(["/usr/bin/python3", "/tamarin/lib/build.py", args.profile, args.architecture])
print("Executer '{:s}' pour lancer la construction du paquet.".format(helper_cmd))
else:
docker_args += [image_tag, "/usr/bin/python3", "/tamarin/lib/build.py", args.profile, args.architecture]
# Start container # Start container
tamarin.run_docker(docker_args, **kwargs) tamarin.run_docker(docker_args, **kwargs)