Tamarin/package

141 lines
5.9 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env python3
2023-01-31 11:25:20 +01:00
import argparse, sys, shutil, os, subprocess, pathlib
2017-01-19 17:39:55 +01:00
2023-01-31 11:25:20 +01:00
sys.path.append(pathlib.Path(__file__).parent.joinpath('lib').as_posix())
2017-01-19 17:39:55 +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'''
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=".")
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')
parser.add_argument("-b", "--base", help="Use the specified image instead of the profile's one", default='')
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("--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="")
parser.add_argument("--prepare-only", help="Only prepare build environment for the given profile", action="store_true", default=False)
2021-02-25 08:42:54 +01:00
parser.add_argument("--no-lib-mounts", help="Disable Tamarin library volumes mount", action="store_true", default=False)
2017-01-19 17:39:55 +01:00
return parser
def build_image(build_workspace, base_image, profile_name, profile, debug=False, rebuild=False):
2023-01-31 11:25:20 +01:00
shutil.copytree(tamarin.get_base_dir(), build_workspace.joinpath('.tamarin'))
2023-01-31 11:25:20 +01:00
with open(build_workspace.joinpath("Dockerfile"), 'w') as dockerfile:
dockerfile.write("FROM {:s}\n".format(base_image))
2021-02-25 08:42:54 +01:00
dockerfile.write("COPY .tamarin /tamarin\n")
2017-02-11 11:51:01 +01:00
# Configure "containerbuild" hooks environment
hooks_env = os.environ.copy()
2023-01-31 11:25:20 +01:00
hooks_env["PATH"] = os.environ['PATH'] + ':' + tamarin.get_lib_dir().as_posix()
2017-02-11 11:51:01 +01:00
# Run hooks
2023-01-31 11:25:20 +01:00
tamarin.run_profile_hooks(profile, 'containerbuild', cwd=build_workspace.as_posix(), env=hooks_env, debug=debug)
2017-02-11 11:51:01 +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" ]
tamarin.run_docker(build_args + [build_workspace], debug=debug)
2017-02-11 11:51:01 +01:00
return image_tag
2017-02-11 11:51:01 +01:00
def cleanup(build_workspace=None, debug=False):
2017-02-11 11:51:01 +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'''
if __name__ == "__main__":
2017-02-11 11:51:01 +01:00
parser = create_args_parser()
args = parser.parse_args()
if args.cleanup:
cleanup(debug=args.debug)
2017-02-09 21:53:24 +01:00
# Verify project directory
2023-01-31 11:25:20 +01:00
project_dir = pathlib.Path(args.project_directory).absolute()
output_dir = pathlib.Path(args.output).absolute()
output_dir.mkdir(parents=True, exist_ok=True)
2017-02-09 21:53:24 +01:00
# Load build profile
2017-03-10 21:51:57 +01:00
profile = tamarin.load_profile(args.profile, debug=args.debug)
workspace = tamarin.get_workspace_dir()
pid = os.getpid()
build_workspace = tamarin.get_workspace_subdir('tmp/build_{:d}'.format(pid))
2023-01-31 11:25:20 +01:00
shutil.copytree(tamarin.get_utils_dir(), build_workspace.joinpath('utils'))
2020-09-02 15:48:05 +02:00
base_image = args.base if args.base != '' else profile['profile']['default_image']
2017-02-11 11:51:01 +01:00
image_tag = build_image(build_workspace, base_image, args.profile, profile, debug=args.debug, rebuild=args.rebuild)
if args.prepare_only:
exit()
kwargs = dict()
kwargs['debug'] = args.debug
2017-07-11 14:36:22 +02:00
docker_args = []
2017-07-11 12:07:00 +02:00
2017-07-10 17:29:39 +02:00
# Append custom arguments
2017-07-11 17:39:47 +02:00
if args.override_docker_args != "":
docker_args = args.override_docker_args.replace('[IMAGE_TAG]', image_tag)
2017-07-17 17:59:33 +02:00
docker_args = docker_args.replace('[PROFILE]', args.profile)
docker_args = docker_args.replace('[ARCH]', args.architecture)
else:
2017-07-11 14:36:22 +02:00
docker_args += [ "run", "--rm" ]
2017-07-11 12:14:38 +02:00
# volumes definition
2017-07-11 12:07:00 +02:00
docker_args += [
2023-01-31 11:25:20 +01:00
"-v", "{:s}:/src:z,ro".format(project_dir.as_posix()),
"-v", "{:s}:/dist:z".format(output_dir.as_posix()),
]
2021-02-25 08:42:54 +01:00
if not args.no_lib_mounts:
docker_args += [
2023-01-31 11:25:20 +01:00
"-v", "{:s}:/tamarin/hooks:z,ro".format(tamarin.get_hooks_dir().as_posix()),
"-v", "{:s}:/tamarin/lib:z,ro".format(tamarin.get_lib_dir().as_posix()),
"-v", "{:s}:/tamarin/profiles:z,ro".format(tamarin.get_profiles_dir().as_posix()),
"-v", "{:s}:/tamarin/utils:z,ro".format(tamarin.get_utils_dir().as_posix())
2021-02-25 08:42:54 +01:00
]
# 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-07-11 14:36:22 +02:00
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
tamarin.run_docker(docker_args, **kwargs)
cleanup(build_workspace, debug=args.debug)