Génération de l'image avec mise en cache
This commit is contained in:
59
package
59
package
@ -1,6 +1,6 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import argparse, sys, shutil, os
|
||||
import argparse, sys, shutil, os, hashlib
|
||||
|
||||
sys.path.append(os.path.dirname(__file__) + '/lib')
|
||||
|
||||
@ -15,6 +15,7 @@ def configure_args_parser():
|
||||
# Define available/required arguments and flags
|
||||
parser.add_argument("project_path", help="The path to your project to package")
|
||||
parser.add_argument("-p", "--profile", help="The profile to use to package this project (default: debian)", choices=profile_names, default='debian')
|
||||
parser.add_argument("--rebuild", help="Ignore cache and rebuild container's image", action="store_true", default=False)
|
||||
|
||||
return parser
|
||||
|
||||
@ -23,6 +24,7 @@ if __name__ == "__main__":
|
||||
parser = configure_args_parser()
|
||||
args = parser.parse_args()
|
||||
|
||||
# Load build profile
|
||||
profile = tamarin.load_profile(args.profile)
|
||||
|
||||
workspace = tamarin.get_workspace_dir()
|
||||
@ -46,13 +48,11 @@ if __name__ == "__main__":
|
||||
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]
|
||||
build_workspace = tamarin.get_workspace_subdir('tmp/build_{:d}'.format(pid))
|
||||
|
||||
rkt_flags = ["--dir={:s}".format(workspace_tmp)]
|
||||
rkt_store = tamarin.get_workspace_subdir('store')
|
||||
rkt_flags = ["--dir={:s}".format(rkt_store)]
|
||||
|
||||
base_image = profile['profile']['default_image']
|
||||
# If the base image is Docker-based, preload it and get its name from the store
|
||||
@ -63,28 +63,45 @@ if __name__ == "__main__":
|
||||
base_image
|
||||
] + rkt_flags)
|
||||
|
||||
name_pattern = base_image.split('/')[-1] + '$'
|
||||
image = rkt.find_image_by_name(name_pattern, rkt_flags=rkt_flags)
|
||||
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]));
|
||||
|
||||
base_aci_name = image['id'][:21]+'.aci'
|
||||
base_aci_file = workspace_tmp+'/'+base_aci_name
|
||||
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);
|
||||
|
||||
if not os.path.isfile(base_aci_file):
|
||||
rkt.export_image(image['id'], workspace_tmp+'/'+base_aci_name, rkt_flags=rkt_flags);
|
||||
# Build image
|
||||
tamarin.run_acbuild(acbuild_flags+["set-name", "image_{:d}".format(pid)])
|
||||
tamarin.run_acbuild(acbuild_flags+["set-exec", "--", "/bin/sh", "-c", "echo Hello World"])
|
||||
|
||||
# Start building image
|
||||
tamarin.run_acbuild(acbuild_flags+["begin", base_aci_file])
|
||||
tamarin.run_acbuild(acbuild_flags+["set-name", "image_{:d}".format(pid)])
|
||||
tamarin.run_acbuild(acbuild_flags+["set-exec", "--", "/bin/sh", "-c", "uname -a"])
|
||||
tamarin.run_acbuild(acbuild_flags+["write", image_path])
|
||||
tamarin.run_acbuild(acbuild_flags+["end"])
|
||||
# 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)
|
||||
|
||||
# rkt run --insecure-options=image ./nginx.aci --volume html,kind=host,source=/path/to/test --net=host
|
||||
# Cache image
|
||||
shutil.copyfile(aci_file, cached_image_file)
|
||||
|
||||
# Start container
|
||||
rkt.run([
|
||||
"run",
|
||||
"--insecure-options=image",
|
||||
image_path, "--net=host"
|
||||
aci_file, "--net=host"
|
||||
] + rkt_flags, as_root=True)
|
||||
|
||||
# Cleanup
|
||||
@ -99,4 +116,4 @@ if __name__ == "__main__":
|
||||
"gc"
|
||||
] + rkt_flags, as_root=True)
|
||||
|
||||
shutil.rmtree(acbuild_workspace, ignore_errors=True)
|
||||
shutil.rmtree(build_workspace, ignore_errors=True)
|
||||
|
Reference in New Issue
Block a user