Gestion de l'image de base

This commit is contained in:
wpetit 2017-01-24 17:32:19 +01:00
parent 487c97e88c
commit 82fb9d31e1
3 changed files with 58 additions and 30 deletions

37
lib/rkt.py Normal file
View File

@ -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)

View File

@ -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'))

41
package
View File

@ -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)