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-17 17:54:25 +02:00
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="")
2020-03-25 12:16:02 +01:00
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
2017-03-21 23:05:16 +01:00
def build_image(build_workspace, base_image, profile_name, profile, debug=False, rebuild=False):
2021-02-25 08:42:54 +01:00
shutil.copytree(tamarin.get_base_dir(), os.path.join(build_workspace, '.tamarin'))
2017-03-21 23:05:16 +01:00
with open("{:s}/Dockerfile".format(build_workspace), '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()
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
2020-09-02 15:48:05 +02:00
shutil.copytree(tamarin.get_utils_dir(), os.path.join(build_workspace, 'utils'))
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
2020-03-25 12:16:02 +01:00
if args.prepare_only:
exit()
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 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)
2017-07-11 11:56:41 +02:00
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 += [
2022-04-11 08:40:46 +02:00
"-v", "{:s}:/src:z,ro".format(project_dir),
"-v", "{:s}:/dist:z".format(output_dir),
2017-07-11 11:56:41 +02:00
]
2021-02-25 08:42:54 +01:00
if not args.no_lib_mounts:
docker_args += [
2022-04-11 08:40:46 +02:00
"-v", "{:s}:/tamarin/hooks:z,ro".format(tamarin.get_hooks_dir()),
"-v", "{:s}:/tamarin/lib:z,ro".format(tamarin.get_lib_dir()),
"-v", "{:s}:/tamarin/profiles:z,ro".format(tamarin.get_profiles_dir()),
"-v", "{:s}:/tamarin/utils:z,ro".format(tamarin.get_utils_dir())
2021-02-25 08:42:54 +01:00
]
2017-07-11 11:56:41 +02: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]
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)