2017-01-18 17:43:46 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2017-01-24 23:15:13 +01:00
|
|
|
import argparse, sys, shutil, os, hashlib
|
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-01-24 17:32:19 +01:00
|
|
|
import tamarin, system, rkt
|
2017-01-19 17:39:55 +01:00
|
|
|
|
|
|
|
def configure_args_parser():
|
|
|
|
|
2017-01-19 22:56:17 +01:00
|
|
|
profile_names = tamarin.get_available_profile_names()
|
2017-01-19 17:39:55 +01:00
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description="Generate packages for various GNU/Linux distribution")
|
|
|
|
|
|
|
|
# Define available/required arguments and flags
|
|
|
|
parser.add_argument("project_path", help="The path to your project to package")
|
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-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-01-19 17:39:55 +01:00
|
|
|
|
|
|
|
return parser
|
|
|
|
|
2017-01-18 17:43:46 +01:00
|
|
|
if __name__ == "__main__":
|
|
|
|
|
2017-01-19 17:39:55 +01:00
|
|
|
parser = configure_args_parser()
|
2017-01-19 22:56:17 +01:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2017-01-24 23:15:13 +01:00
|
|
|
# Load build profile
|
2017-01-19 22:56:17 +01:00
|
|
|
profile = tamarin.load_profile(args.profile)
|
|
|
|
|
|
|
|
workspace = tamarin.get_workspace_dir()
|
|
|
|
workspace_tmp = tamarin.get_workspace_subdir('tmp')
|
|
|
|
|
|
|
|
local_rkt_dir = tamarin.get_workspace_subdir('rkt')
|
|
|
|
if not system.which('rkt', local_rkt_dir):
|
|
|
|
# Download and extract rkt
|
|
|
|
rkt_archive_path = tamarin.download_rkt()
|
|
|
|
system.extract_tar(rkt_archive_path, workspace_tmp)
|
|
|
|
rkt_archive_dir = tamarin.get_rkt_achive_dest_dir()
|
|
|
|
shutil.rmtree(local_rkt_dir, ignore_errors=True)
|
|
|
|
os.rename(rkt_archive_dir, local_rkt_dir)
|
|
|
|
|
|
|
|
local_acbuild_dir = tamarin.get_workspace_subdir('acbuild')
|
|
|
|
if not system.which('acbuild', local_acbuild_dir):
|
|
|
|
# Download and extract acbuild
|
|
|
|
acbuild_archive_path = tamarin.download_acbuild()
|
|
|
|
system.extract_tar(acbuild_archive_path, workspace_tmp)
|
|
|
|
acbuild_archive_dir = tamarin.get_acbuild_achive_dest_dir()
|
|
|
|
shutil.rmtree(local_acbuild_dir, ignore_errors=True)
|
|
|
|
os.rename(acbuild_archive_dir, local_acbuild_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-01-24 23:15:13 +01:00
|
|
|
rkt_store = tamarin.get_workspace_subdir('store')
|
|
|
|
rkt_flags = ["--dir={:s}".format(rkt_store)]
|
2017-01-20 17:22:54 +01:00
|
|
|
|
|
|
|
base_image = profile['profile']['default_image']
|
|
|
|
# If the base image is Docker-based, preload it and get its name from the store
|
|
|
|
if base_image.startswith('docker://'):
|
2017-01-24 17:32:19 +01:00
|
|
|
rkt.run([
|
2017-01-20 17:22:54 +01:00
|
|
|
"fetch",
|
|
|
|
"--insecure-options=image",
|
|
|
|
base_image
|
|
|
|
] + rkt_flags)
|
2017-01-24 17:32:19 +01:00
|
|
|
|
2017-01-24 23:15:13 +01:00
|
|
|
aci_file = os.path.join(os.sep, build_workspace, 'image.aci')
|
|
|
|
acbuild_flags = ["--modify", aci_file, "--work-path", build_workspace]
|
|
|
|
|
|
|
|
# Use cached image base on base_image and containerbuild hooks
|
|
|
|
containerbuild_hooks = profile['containerbuild']['hooks']
|
|
|
|
hasher = hashlib.sha1()
|
|
|
|
hasher.update(base_image.encode())
|
|
|
|
hasher.update(containerbuild_hooks.encode())
|
|
|
|
image_hash = hasher.hexdigest()
|
|
|
|
cache_dir = tamarin.get_workspace_subdir('cache')
|
|
|
|
cached_image_file = os.path.join(os.sep, cache_dir, '{:s}.aci'.format(image_hash[:12]));
|
|
|
|
|
|
|
|
if not args.rebuild and os.path.exists(cached_image_file):
|
|
|
|
# Copy cached image
|
|
|
|
shutil.copyfile(cached_image_file, aci_file)
|
|
|
|
else:
|
|
|
|
# Find and export base image from rkt' store
|
|
|
|
name_pattern = base_image.split('/')[-1] + '$'
|
|
|
|
image = rkt.find_image_by_name(name_pattern, rkt_flags=rkt_flags)
|
|
|
|
rkt.export_image(image['id'], aci_file, rkt_flags=rkt_flags);
|
|
|
|
|
|
|
|
# Build image
|
|
|
|
tamarin.run_acbuild(acbuild_flags+["set-name", "image_{:d}".format(pid)])
|
2017-01-25 14:49:30 +01:00
|
|
|
tamarin.run_acbuild(acbuild_flags+["copy-to-dir", tamarin.get_hooks_dir(), '/tamarin'])
|
|
|
|
tamarin.run_acbuild(acbuild_flags+["copy-to-dir", tamarin.get_lib_dir(), '/tamarin'])
|
2017-01-24 23:15:13 +01:00
|
|
|
|
|
|
|
# Execute containerbuild hooks
|
|
|
|
cpb_hooks_env = os.environ.copy()
|
|
|
|
cpb_hooks_env["PATH"] = os.environ['PATH'] + ':' + local_acbuild_dir
|
|
|
|
cpb_hooks_env["TAMARIN_ACBUILD"] = " ".join([system.which('acbuild', local_acbuild_dir)]+acbuild_flags)
|
|
|
|
tamarin.run_profile_hooks(profile, 'containerbuild', cwd=build_workspace, env=cpb_hooks_env)
|
|
|
|
|
|
|
|
# Cache image
|
|
|
|
shutil.copyfile(aci_file, cached_image_file)
|
|
|
|
|
|
|
|
# Start container
|
2017-01-25 14:49:30 +01:00
|
|
|
rkt.run(rkt_flags+[
|
2017-01-20 17:22:54 +01:00
|
|
|
"run",
|
|
|
|
"--insecure-options=image",
|
2017-01-25 14:49:30 +01:00
|
|
|
aci_file, "--net=host",
|
|
|
|
"--exec", "/usr/bin/python3", "--", "/tamarin/lib/build.py"
|
|
|
|
], as_root=True)
|
2017-01-20 17:22:54 +01:00
|
|
|
|
|
|
|
# Cleanup
|
|
|
|
|
2017-01-24 17:32:19 +01:00
|
|
|
rkt.run([
|
2017-01-20 17:22:54 +01:00
|
|
|
"gc",
|
|
|
|
"--grace-period=0"
|
2017-01-24 17:32:19 +01:00
|
|
|
] + rkt_flags, as_root=True)
|
2017-01-20 17:22:54 +01:00
|
|
|
|
2017-01-24 17:32:19 +01:00
|
|
|
rkt.run([
|
2017-01-20 17:22:54 +01:00
|
|
|
"image",
|
|
|
|
"gc"
|
2017-01-24 17:32:19 +01:00
|
|
|
] + rkt_flags, as_root=True)
|
2017-01-20 17:22:54 +01:00
|
|
|
|
2017-01-24 23:15:13 +01:00
|
|
|
shutil.rmtree(build_workspace, ignore_errors=True)
|