From 487c97e88cf09c9f2cfdb555451319b7fffc86af Mon Sep 17 00:00:00 2001 From: William Petit Date: Fri, 20 Jan 2017 17:22:54 +0100 Subject: [PATCH] =?UTF-8?q?Construction/execution=20d'un=20conteneur=20?= =?UTF-8?q?=C3=A0=20partir=20de=20l'image=20de=20base=20sp=C3=A9cifi=C3=A9?= =?UTF-8?q?e=20dans=20le=20profil?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/tamarin.py | 16 ++++++++++----- package | 43 ++++++++++++++++++++++++++++++++++++++--- src-example/hello-world | 3 --- 3 files changed, 51 insertions(+), 11 deletions(-) delete mode 100755 src-example/hello-world diff --git a/lib/tamarin.py b/lib/tamarin.py index 8123dd5..88e96b2 100644 --- a/lib/tamarin.py +++ b/lib/tamarin.py @@ -59,15 +59,21 @@ def download_acbuild(): web.download_file(file_url=url, dest_path=file_path) return file_path -def run_rkt(args): +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 else [rkt_bin] ) + args + cmd = ( ["sudo", "-E", rkt_bin] if os.geteuid() != 0 and asRoot == True else [rkt_bin] ) + args print(" ".join(cmd)) - return subprocess.call(cmd, stdin=subprocess.PIPE) + if captureOutput: + return subprocess.check_output(cmd, stdin=subprocess.PIPE) + else: + return subprocess.call(cmd, stdin=subprocess.PIPE) -def run_acbuild(args): +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')) print(" ".join([acbuild_bin] + args)) - return subprocess.call([acbuild_bin] + args, stdin=subprocess.PIPE) + if captureOutput: + return subprocess.check_output([acbuild_bin] + args, stdin=subprocess.PIPE) + else: + return subprocess.call([acbuild_bin] + args, stdin=subprocess.PIPE) diff --git a/package b/package index 6946135..befa500 100755 --- a/package +++ b/package @@ -1,6 +1,6 @@ #!/usr/bin/env python3 -import argparse, sys, shutil, os +import argparse, sys, shutil, os, json sys.path.append(os.path.dirname(__file__) + '/lib') @@ -52,13 +52,50 @@ if __name__ == "__main__": image_path = os.path.join(os.sep, acbuild_workspace, image_name) acbuild_flags = ["--work-path", acbuild_workspace] + 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://'): + tamarin.run_rkt([ + "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'] + # Start building image tamarin.run_acbuild(acbuild_flags+["begin"]) tamarin.run_acbuild(acbuild_flags+["set-name", "image_{:d}".format(pid)]) - # tamarin.run_acbuild(acbuild_flags+["dependency", "add", profile['profile']['default_image']]) + 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+["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(["run", "--insecure-options=image", "--dir={:s}".format(workspace_tmp), "--stage1-name={:s}".format(profile['profile']['default_image']), image_path, "--net=host"]) + tamarin.run_rkt([ + "run", + "--insecure-options=image", + image_path, "--net=host" + ] + rkt_flags, asRoot=True) + + # Cleanup + + tamarin.run_rkt([ + "gc", + "--grace-period=0" + ] + rkt_flags, asRoot=True) + + tamarin.run_rkt([ + "image", + "gc" + ] + rkt_flags, asRoot=True) + + shutil.rmtree(acbuild_workspace, ignore_errors=True) diff --git a/src-example/hello-world b/src-example/hello-world deleted file mode 100755 index 124f6f6..0000000 --- a/src-example/hello-world +++ /dev/null @@ -1,3 +0,0 @@ -#!/usr/bin/env bash - -echo $1