2017-01-18 17:43:46 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2017-01-24 17:32:19 +01:00
|
|
|
import argparse, sys, shutil, os
|
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-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()
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
acbuild_workspace = tamarin.get_acbuild_workspace_dir()
|
|
|
|
pid = os.getpid()
|
|
|
|
image_name = "image_{:d}.aci".format(pid)
|
|
|
|
image_path = os.path.join(os.sep, acbuild_workspace, image_name)
|
|
|
|
acbuild_flags = ["--work-path", acbuild_workspace]
|
|
|
|
|
2017-01-20 17:22:54 +01:00
|
|
|
rkt_flags = ["--dir={:s}".format(workspace_tmp)]
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
name_pattern = base_image.split('/')[-1] + '$'
|
|
|
|
image = rkt.find_image_by_name(name_pattern, rkt_flags=rkt_flags)
|
|
|
|
|
|
|
|
|
|
|
|
base_aci_name = image['id'][:21]+'.aci'
|
|
|
|
base_aci_file = workspace_tmp+'/'+base_aci_name
|
|
|
|
|
|
|
|
if not os.path.isfile(base_aci_file):
|
|
|
|
rkt.export_image(image['id'], workspace_tmp+'/'+base_aci_name, rkt_flags=rkt_flags);
|
2017-01-20 17:22:54 +01:00
|
|
|
|
2017-01-19 22:56:17 +01:00
|
|
|
# Start building image
|
2017-01-24 17:32:19 +01:00
|
|
|
tamarin.run_acbuild(acbuild_flags+["begin", base_aci_file])
|
2017-01-19 22:56:17 +01:00
|
|
|
tamarin.run_acbuild(acbuild_flags+["set-name", "image_{:d}".format(pid)])
|
2017-01-24 17:32:19 +01:00
|
|
|
tamarin.run_acbuild(acbuild_flags+["set-exec", "--", "/bin/sh", "-c", "uname -a"])
|
2017-01-19 22:56:17 +01:00
|
|
|
tamarin.run_acbuild(acbuild_flags+["write", image_path])
|
|
|
|
tamarin.run_acbuild(acbuild_flags+["end"])
|
|
|
|
|
|
|
|
# rkt run --insecure-options=image ./nginx.aci --volume html,kind=host,source=/path/to/test --net=host
|
2017-01-24 17:32:19 +01:00
|
|
|
rkt.run([
|
2017-01-20 17:22:54 +01:00
|
|
|
"run",
|
|
|
|
"--insecure-options=image",
|
|
|
|
image_path, "--net=host"
|
2017-01-24 17:32:19 +01:00
|
|
|
] + rkt_flags, 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
|
|
|
|
|
|
|
shutil.rmtree(acbuild_workspace, ignore_errors=True)
|