2017-01-18 17:43:46 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2017-03-21 23:05:16 +01:00
|
|
|
import argparse, sys, shutil, os, subprocess
|
2017-01-19 17:39:55 +01:00
|
|
|
|
2017-01-19 22:56:17 +01:00
|
|
|
sys.path.append(os.path.dirname(__file__) + '/lib')
|
2017-01-19 17:39:55 +01:00
|
|
|
|
2017-03-21 23:05:16 +01:00
|
|
|
import tamarin
|
2017-01-19 17:39:55 +01:00
|
|
|
|
2017-02-11 11:51:01 +01:00
|
|
|
def create_args_parser():
|
|
|
|
'''Return a new configured ArgumentParser'''
|
2017-01-19 22:56:17 +01:00
|
|
|
profile_names = tamarin.get_available_profile_names()
|
2017-01-19 17:39:55 +01:00
|
|
|
|
2017-02-09 21:53:24 +01:00
|
|
|
parser = argparse.ArgumentParser(description="Generate packages for various GNU/Linux distributions")
|
2017-01-19 17:39:55 +01:00
|
|
|
|
|
|
|
# Define available/required arguments and flags
|
2017-02-09 21:53:24 +01:00
|
|
|
parser.add_argument("project_directory", help="The path to your project to package")
|
|
|
|
parser.add_argument("-o", "--output", help="The path to the generated packages destination directory", default=".")
|
2017-01-19 22:56:17 +01:00
|
|
|
parser.add_argument("-p", "--profile", help="The profile to use to package this project (default: debian)", choices=profile_names, default='debian')
|
2017-02-09 21:53:24 +01:00
|
|
|
parser.add_argument("-a", "--architecture", help="The target architecture for the package (default: amd64)", default='amd64')
|
2017-02-18 11:26:25 +01:00
|
|
|
parser.add_argument("-b", "--base", help="Use the specified image instead of the profile's one", default='')
|
2017-01-24 23:15:13 +01:00
|
|
|
parser.add_argument("--rebuild", help="Ignore cache and rebuild container's image", action="store_true", default=False)
|
2017-02-18 11:26:25 +01:00
|
|
|
parser.add_argument("--debug", help="Will add extra output and start the container in interactive mode", action="store_true", default=False)
|
2017-03-21 23:05:16 +01:00
|
|
|
parser.add_argument("--cleanup", help="Clear the workspace and remove obsolete Docker images before build", action="store_true", default=False)
|
2017-07-11 11:56:41 +02:00
|
|
|
parser.add_argument("-f", "--override-run-flags", help="Override 'docker run' flags", default=[], action='append')
|
2017-01-19 17:39:55 +01:00
|
|
|
|
|
|
|
return parser
|
|
|
|
|
2017-03-21 23:05:16 +01:00
|
|
|
def build_image(build_workspace, base_image, profile_name, profile, debug=False, rebuild=False):
|
|
|
|
|
|
|
|
with open("{:s}/Dockerfile".format(build_workspace), 'w') as dockerfile:
|
|
|
|
dockerfile.write("FROM {:s}\n".format(base_image))
|
2017-02-11 11:51:01 +01:00
|
|
|
|
|
|
|
# Configure "containerbuild" hooks environment
|
|
|
|
hooks_env = os.environ.copy()
|
2017-03-21 23:05:16 +01:00
|
|
|
hooks_env["PATH"] = os.environ['PATH'] + ':' + tamarin.get_lib_dir()
|
2017-02-11 11:51:01 +01:00
|
|
|
|
|
|
|
# Run hooks
|
2017-03-10 21:51:57 +01:00
|
|
|
tamarin.run_profile_hooks(profile, 'containerbuild', cwd=build_workspace, env=hooks_env, debug=debug)
|
2017-02-11 11:51:01 +01:00
|
|
|
|
2017-03-21 23:05:16 +01:00
|
|
|
image_tag = "tamarin:{:s}_{:s}_{:d}".format(profile_name, base_image.replace(':', '_'), os.getpid())
|
|
|
|
|
|
|
|
build_args = [ "build", "-t", image_tag ]
|
|
|
|
|
|
|
|
if rebuild:
|
|
|
|
build_args += [ "--no-cache" ]
|
2017-02-18 11:26:25 +01:00
|
|
|
|
2017-03-21 23:05:16 +01:00
|
|
|
tamarin.run_docker(build_args + [build_workspace], debug=debug)
|
2017-02-11 11:51:01 +01:00
|
|
|
|
2017-03-21 23:05:16 +01:00
|
|
|
return image_tag
|
2017-02-11 11:51:01 +01:00
|
|
|
|
2017-03-21 23:05:16 +01:00
|
|
|
def cleanup(build_workspace=None, debug=False):
|
2017-02-11 11:51:01 +01:00
|
|
|
|
2017-03-21 23:05:16 +01:00
|
|
|
if build_workspace == None:
|
|
|
|
build_workspace = tamarin.get_workspace_subdir('tmp')
|
2017-02-11 11:51:01 +01:00
|
|
|
|
|
|
|
# Suppression de l'espace de travail de build
|
|
|
|
shutil.rmtree(build_workspace, ignore_errors=True)
|
|
|
|
|
|
|
|
def validate_args(args):
|
|
|
|
'''TODO'''
|
|
|
|
|
2017-01-18 17:43:46 +01:00
|
|
|
if __name__ == "__main__":
|
|
|
|
|
2017-02-11 11:51:01 +01:00
|
|
|
parser = create_args_parser()
|
2017-01-19 22:56:17 +01:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2017-03-21 23:05:16 +01:00
|
|
|
if args.cleanup:
|
|
|
|
cleanup(debug=args.debug)
|
|
|
|
|
2017-02-09 21:53:24 +01:00
|
|
|
# Verify project directory
|
|
|
|
project_dir = os.path.abspath(args.project_directory)
|
|
|
|
output_dir = os.path.abspath(args.output)
|
|
|
|
|
2017-01-24 23:15:13 +01:00
|
|
|
# Load build profile
|
2017-03-10 21:51:57 +01:00
|
|
|
profile = tamarin.load_profile(args.profile, debug=args.debug)
|
2017-01-19 22:56:17 +01:00
|
|
|
|
|
|
|
workspace = tamarin.get_workspace_dir()
|
|
|
|
|
|
|
|
pid = os.getpid()
|
2017-01-24 23:15:13 +01:00
|
|
|
build_workspace = tamarin.get_workspace_subdir('tmp/build_{:d}'.format(pid))
|
2017-01-19 22:56:17 +01:00
|
|
|
|
2017-02-18 11:26:25 +01:00
|
|
|
base_image = args.base if args.base != '' else profile['profile']['default_image']
|
2017-02-11 11:51:01 +01:00
|
|
|
|
2017-03-21 23:05:16 +01:00
|
|
|
image_tag = build_image(build_workspace, base_image, args.profile, profile, debug=args.debug, rebuild=args.rebuild)
|
2017-01-24 23:15:13 +01:00
|
|
|
|
2017-03-21 23:05:16 +01:00
|
|
|
kwargs = dict()
|
|
|
|
kwargs['debug'] = args.debug
|
2017-02-18 11:26:25 +01:00
|
|
|
|
2017-07-11 12:07:00 +02:00
|
|
|
docker_args = []
|
|
|
|
|
2017-07-10 17:29:39 +02:00
|
|
|
# Append custom arguments
|
2017-07-11 11:56:41 +02:00
|
|
|
if len(args.override_run_flags) > 0:
|
|
|
|
docker_args += args.override_run_flags
|
|
|
|
else:
|
|
|
|
# rkt run arguments
|
2017-07-11 12:07:00 +02:00
|
|
|
docker_args += [
|
2017-07-11 11:56:41 +02:00
|
|
|
"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])]
|
2017-07-10 17:29:39 +02:00
|
|
|
|
2017-02-18 11:26:25 +01:00
|
|
|
if args.debug:
|
2017-03-21 23:05:16 +01:00
|
|
|
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))
|
2017-02-18 11:26:25 +01:00
|
|
|
else:
|
2017-03-21 23:05:16 +01:00
|
|
|
docker_args += [image_tag, "/usr/bin/python3", "/tamarin/lib/build.py", args.profile, args.architecture]
|
2017-02-18 11:26:25 +01:00
|
|
|
|
2017-01-24 23:15:13 +01:00
|
|
|
# Start container
|
2017-03-21 23:05:16 +01:00
|
|
|
tamarin.run_docker(docker_args, **kwargs)
|
2017-01-20 17:22:54 +01:00
|
|
|
|
2017-03-21 23:05:16 +01:00
|
|
|
cleanup(build_workspace, debug=args.debug)
|