From 82fb9d31e1e398a7b48db713f2c3bf4e7f485ebf Mon Sep 17 00:00:00 2001 From: William Petit Date: Tue, 24 Jan 2017 17:32:19 +0100 Subject: [PATCH] Gestion de l'image de base --- lib/rkt.py | 37 +++++++++++++++++++++++++++++++++++++ lib/tamarin.py | 10 ---------- package | 41 +++++++++++++++++++++-------------------- 3 files changed, 58 insertions(+), 30 deletions(-) create mode 100644 lib/rkt.py diff --git a/lib/rkt.py b/lib/rkt.py new file mode 100644 index 0000000..c34e183 --- /dev/null +++ b/lib/rkt.py @@ -0,0 +1,37 @@ +import system, subprocess, os, tamarin, json, re + +def run(args, as_root = False, capture_output=False): + """Run rkt with the specified args (use the local copy if rkt is not found in the $PATH)""" + rkt_bin = system.which('rkt', tamarin.get_workspace_subdir('rkt')) + cmd = ( ["sudo", "-E", rkt_bin] if os.geteuid() != 0 and as_root == True else [rkt_bin] ) + args + print(" ".join(cmd)) + if capture_output: + return subprocess.check_output(cmd, stdin=subprocess.PIPE) + else: + return subprocess.call(cmd, stdin=subprocess.PIPE) + +def get_images_list(rkt_flags = []): + output = run([ + "image", + "list", + "--format=json" + ] + rkt_flags, capture_output=True) + # Fetch the list of installed images + return json.loads(output.decode('utf-8')) + +def find_image_by_name(name_pattern, rkt_flags = []): + if type(name_pattern) is str: + name_pattern = re.compile(name_pattern) + images_list = get_images_list(rkt_flags = rkt_flags) + for image in images_list: + if name_pattern.search(image['name']): + return image + return None + +def export_image(image_id, dest_file, rkt_flags = []): + output = run([ + "image", + "export", + image_id, + dest_file, + ] + rkt_flags) diff --git a/lib/tamarin.py b/lib/tamarin.py index 88e96b2..22f29a7 100644 --- a/lib/tamarin.py +++ b/lib/tamarin.py @@ -59,16 +59,6 @@ def download_acbuild(): web.download_file(file_url=url, dest_path=file_path) return file_path -def run_rkt(args, asRoot = False, captureOutput=False): - """Run rkt with the specified args (use the local copy if rkt is not found in the $PATH)""" - rkt_bin = system.which('rkt', get_workspace_subdir('rkt')) - cmd = ( ["sudo", "-E", rkt_bin] if os.geteuid() != 0 and asRoot == True else [rkt_bin] ) + args - print(" ".join(cmd)) - if captureOutput: - return subprocess.check_output(cmd, stdin=subprocess.PIPE) - else: - return subprocess.call(cmd, stdin=subprocess.PIPE) - def run_acbuild(args, captureOutput=False): """Run acbuild with the specified args (use the local copy if acbuild is not found in the $PATH)""" acbuild_bin = system.which('acbuild', get_workspace_subdir('acbuild')) diff --git a/package b/package index befa500..89f65df 100755 --- a/package +++ b/package @@ -1,10 +1,10 @@ #!/usr/bin/env python3 -import argparse, sys, shutil, os, json +import argparse, sys, shutil, os sys.path.append(os.path.dirname(__file__) + '/lib') -import tamarin, system +import tamarin, system, rkt def configure_args_parser(): @@ -57,45 +57,46 @@ if __name__ == "__main__": 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://'): - tamarin.run_rkt([ + rkt.run([ "fetch", "--insecure-options=image", base_image ] + rkt_flags) - output = tamarin.run_rkt([ - "image", - "list", - "--format=json" - ] + rkt_flags, captureOutput=True) - # Fetch the list of installed images - images_list = json.loads(output.decode('utf-8')) - base_image = images_list[0]['name'] + + 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); # Start building image - tamarin.run_acbuild(acbuild_flags+["begin"]) + 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+["dependency", "add", base_image]) - tamarin.run_acbuild(acbuild_flags+["set-exec", "--", "/bin/sh", "-c", "echo Hello World"]) + 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"]) # rkt run --insecure-options=image ./nginx.aci --volume html,kind=host,source=/path/to/test --net=host - tamarin.run_rkt([ + rkt.run([ "run", "--insecure-options=image", image_path, "--net=host" - ] + rkt_flags, asRoot=True) + ] + rkt_flags, as_root=True) # Cleanup - tamarin.run_rkt([ + rkt.run([ "gc", "--grace-period=0" - ] + rkt_flags, asRoot=True) + ] + rkt_flags, as_root=True) - tamarin.run_rkt([ + rkt.run([ "image", "gc" - ] + rkt_flags, asRoot=True) + ] + rkt_flags, as_root=True) shutil.rmtree(acbuild_workspace, ignore_errors=True)