Base gestion mode debug

This commit is contained in:
wpetit 2017-03-10 21:51:57 +01:00
parent 9780cafffd
commit 448a16f659
5 changed files with 52 additions and 44 deletions

2
.gitignore vendored
View File

@ -3,3 +3,5 @@ packages
*~ *~
__pycache__ __pycache__
*.pyc *.pyc
*.changes
*.deb

View File

@ -1,21 +1,22 @@
import system, subprocess, os, tamarin, json, re import system, subprocess, os, tamarin, json, re
def run(args, as_root = False, capture_output=False): def run(args, as_root = False, capture_output=False, debug=False):
"""Run rkt with the specified args (use the local copy if rkt is not found in the $PATH)""" """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')) 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 cmd = ( ["sudo", "-E", rkt_bin] if os.geteuid() != 0 and as_root == True else [rkt_bin] ) + args
if debug:
print(" ".join(cmd)) print(" ".join(cmd))
if capture_output: if capture_output:
return subprocess.check_output(cmd, stdin=subprocess.PIPE) return subprocess.check_output(cmd, stdin=subprocess.PIPE)
else: else:
return subprocess.call(cmd, stdin=subprocess.PIPE) return subprocess.call(cmd, stdin=subprocess.PIPE)
def get_images_list(rkt_flags = []): def get_images_list(rkt_flags = [], debug=False):
output = run([ output = run([
"image", "image",
"list", "list",
"--format=json" "--format=json"
] + rkt_flags, capture_output=True) ] + rkt_flags, capture_output=True, debug=debug)
# Fetch the list of installed images # Fetch the list of installed images
return json.loads(output.decode('utf-8')) return json.loads(output.decode('utf-8'))
@ -28,10 +29,10 @@ def find_image_by_name(name_pattern, rkt_flags = []):
return image return image
return None return None
def export_image(image_id, dest_file, rkt_flags = []): def export_image(image_id, dest_file, rkt_flags = [], debug=False):
run([ run([
"image", "image",
"export", "export",
image_id, image_id,
dest_file, dest_file,
] + rkt_flags) ] + rkt_flags, debug=debug)

View File

@ -1,6 +1,7 @@
import tarfile, os import tarfile, os
def extract_tar(file_path, dest_dir = "."): def extract_tar(file_path, dest_dir = ".", debug=False):
if debug:
print('Extracting "{:s}" to "{:s}"'.format(file_path, dest_dir)) print('Extracting "{:s}" to "{:s}"'.format(file_path, dest_dir))
with tarfile.open(file_path) as tar: with tarfile.open(file_path) as tar:
tar.extractall(dest_dir) tar.extractall(dest_dir)

View File

@ -2,7 +2,7 @@ import os, glob, subprocess, configparser
import web, system import web, system
import codecs import codecs
def run_profile_hooks(profile, step, cwd=None, env=None): def run_profile_hooks(profile, step, cwd=None, env=None, debug=False):
hooks_dir = get_hooks_dir() hooks_dir = get_hooks_dir()
step_hooks = profile[step]["hooks"] step_hooks = profile[step]["hooks"]
if not step_hooks: if not step_hooks:
@ -22,7 +22,7 @@ def get_hooks_dir():
def get_lib_dir(): def get_lib_dir():
return os.path.realpath(os.path.dirname(os.path.abspath(__file__)) + "/../lib") return os.path.realpath(os.path.dirname(os.path.abspath(__file__)) + "/../lib")
def load_profile(profile_name): def load_profile(profile_name, debug=False):
profile_filename = profile_name+".conf" profile_filename = profile_name+".conf"
for profile_file in get_available_profiles(): for profile_file in get_available_profiles():
if profile_filename == os.path.basename(profile_file): if profile_filename == os.path.basename(profile_file):
@ -63,25 +63,27 @@ def get_rkt_achive_dest_dir():
workspace_tmp = get_workspace_subdir('tmp') workspace_tmp = get_workspace_subdir('tmp')
return glob.glob(os.path.join(os.sep, workspace_tmp, 'rkt-v*'))[0] return glob.glob(os.path.join(os.sep, workspace_tmp, 'rkt-v*'))[0]
def download_rkt(): def download_rkt(debug=False):
"""Download a local copy of rkt in the tamarin workspace and return the absolute path to the archive""" """Download a local copy of rkt in the tamarin workspace and return the absolute path to the archive"""
url = "https://github.com/coreos/rkt/releases/download/v1.22.0/rkt-v1.22.0.tar.gz" url = "https://github.com/coreos/rkt/releases/download/v1.22.0/rkt-v1.22.0.tar.gz"
file_path=os.path.join(os.sep, get_workspace_subdir('tmp'), "rkt.tar.gz") file_path=os.path.join(os.sep, get_workspace_subdir('tmp'), "rkt.tar.gz")
web.download_file(file_url=url, dest_path=file_path) web.download_file(file_url=url, dest_path=file_path)
return file_path return file_path
def download_acbuild(): def download_acbuild(debug=False):
"""Download a local copy of acbuild in the tamarin workspace and return the absolute path to the archive""" """Download a local copy of acbuild in the tamarin workspace and return the absolute path to the archive"""
url = "https://github.com/containers/build/releases/download/v0.4.0/acbuild-v0.4.0.tar.gz" url = "https://github.com/containers/build/releases/download/v0.4.0/acbuild-v0.4.0.tar.gz"
file_path=os.path.join(os.sep, get_workspace_subdir('tmp'), "acbuild.tar.gz") file_path=os.path.join(os.sep, get_workspace_subdir('tmp'), "acbuild.tar.gz")
web.download_file(file_url=url, dest_path=file_path) web.download_file(file_url=url, dest_path=file_path)
return file_path return file_path
def run_acbuild(args, captureOutput=False): def run_acbuild(args, captureOutput=False, as_root=False, debug=False):
"""Run acbuild with the specified args (use the local copy if acbuild is not found in the $PATH)""" """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')) acbuild_bin = system.which('acbuild', get_workspace_subdir('acbuild'))
print(" ".join([acbuild_bin] + args)) cmd = ( ["sudo", "-E", acbuild_bin] if os.geteuid() != 0 and as_root == True else [acbuild_bin] ) + args
if debug:
print(" ".join(cmd))
if captureOutput: if captureOutput:
return subprocess.check_output([acbuild_bin] + args, stdin=subprocess.PIPE) return subprocess.check_output(cmd, stdin=subprocess.PIPE)
else: else:
return subprocess.call([acbuild_bin] + args, stdin=subprocess.PIPE) return subprocess.call(cmd, stdin=subprocess.PIPE)

58
package
View File

@ -23,23 +23,23 @@ def create_args_parser():
return parser return parser
def download_and_extract_rkt(dest_dir, verbose=True): def download_and_extract_rkt(dest_dir, debug=False):
'''Download and extract rkt to the given destination directory''' '''Download and extract rkt to the given destination directory'''
rkt_archive_path = tamarin.download_rkt() rkt_archive_path = tamarin.download_rkt(debug=debug)
system.extract_tar(rkt_archive_path, workspace_tmp) system.extract_tar(rkt_archive_path, workspace_tmp, debug=debug)
rkt_archive_dir = tamarin.get_rkt_achive_dest_dir() rkt_archive_dir = tamarin.get_rkt_achive_dest_dir()
shutil.rmtree(local_rkt_dir, ignore_errors=True) shutil.rmtree(local_rkt_dir, ignore_errors=True)
os.rename(rkt_archive_dir, dest_dir) os.rename(rkt_archive_dir, dest_dir)
def download_and_extract_acbuild(dest_dir, verbose=True): def download_and_extract_acbuild(dest_dir, debug=False):
'''Download and extract acbuild to the given destination directory''' '''Download and extract acbuild to the given destination directory'''
acbuild_archive_path = tamarin.download_acbuild() acbuild_archive_path = tamarin.download_acbuild(debug=debug)
system.extract_tar(acbuild_archive_path, workspace_tmp) system.extract_tar(acbuild_archive_path, workspace_tmp, debug=debug)
acbuild_archive_dir = tamarin.get_acbuild_achive_dest_dir() acbuild_archive_dir = tamarin.get_acbuild_achive_dest_dir()
shutil.rmtree(local_acbuild_dir, ignore_errors=True) shutil.rmtree(local_acbuild_dir, ignore_errors=True)
os.rename(acbuild_archive_dir, dest_dir) os.rename(acbuild_archive_dir, dest_dir)
def get_cached_image_path(profile): def get_cached_image_path(profile, debug=False):
'''Compute and return the path for an hypothetic cached image for the given profile''' '''Compute and return the path for an hypothetic cached image for the given profile'''
containerbuild_hooks = profile['containerbuild']['hooks'] containerbuild_hooks = profile['containerbuild']['hooks']
hasher = hashlib.sha1() hasher = hashlib.sha1()
@ -49,23 +49,23 @@ def get_cached_image_path(profile):
cache_dir = tamarin.get_workspace_subdir('cache') cache_dir = tamarin.get_workspace_subdir('cache')
return os.path.join(os.sep, cache_dir, '{:s}.aci'.format(image_hash[:12])); return os.path.join(os.sep, cache_dir, '{:s}.aci'.format(image_hash[:12]));
def build_image(build_workspace, aci_file, base_image, profile): def build_image(build_workspace, aci_file, base_image, profile, debug=False):
acbuild_flags = ["--work-path", build_workspace] acbuild_flags = ["--work-path", build_workspace]
# Find and export base image from rkt' store # Find and export base image from rkt' store
name_pattern = base_image.split('/')[-1] + '$' name_pattern = base_image.split('/')[-1] + '$'
image = rkt.find_image_by_name(name_pattern, rkt_flags=rkt_flags) image = rkt.find_image_by_name(name_pattern, rkt_flags=rkt_flags)
rkt.export_image(image['id'], aci_file, rkt_flags=rkt_flags); rkt.export_image(image['id'], aci_file, rkt_flags=rkt_flags, debug=debug);
# Build image # Build image
tamarin.run_acbuild(acbuild_flags+["begin", aci_file]) tamarin.run_acbuild(acbuild_flags+["begin", aci_file], debug=debug)
tamarin.run_acbuild(acbuild_flags+["set-name", "image_{:d}".format(pid)]) tamarin.run_acbuild(acbuild_flags+["set-name", "image_{:d}".format(pid)], debug=debug)
tamarin.run_acbuild(acbuild_flags+["mount", "add", "src", "/src", "--read-only"]) tamarin.run_acbuild(acbuild_flags+["mount", "add", "src", "/src", "--read-only"], debug=debug)
tamarin.run_acbuild(acbuild_flags+["mount", "add", "dist", "/dist"]) tamarin.run_acbuild(acbuild_flags+["mount", "add", "dist", "/dist"], debug=debug)
tamarin.run_acbuild(acbuild_flags+["mount", "add", "tamarin-hooks", "/tamarin/hooks", "--read-only"]) tamarin.run_acbuild(acbuild_flags+["mount", "add", "tamarin-hooks", "/tamarin/hooks", "--read-only"], debug=debug)
tamarin.run_acbuild(acbuild_flags+["mount", "add", "tamarin-lib", "/tamarin/lib", "--read-only"]) tamarin.run_acbuild(acbuild_flags+["mount", "add", "tamarin-lib", "/tamarin/lib", "--read-only"], debug=debug)
tamarin.run_acbuild(acbuild_flags+["mount", "add", "tamarin-profiles", "/tamarin/profiles", "--read-only"]) tamarin.run_acbuild(acbuild_flags+["mount", "add", "tamarin-profiles", "/tamarin/profiles", "--read-only"], debug=debug)
# Configure "containerbuild" hooks environment # Configure "containerbuild" hooks environment
hooks_env = os.environ.copy() hooks_env = os.environ.copy()
@ -74,26 +74,26 @@ def build_image(build_workspace, aci_file, base_image, profile):
hooks_env["TAMARIN_ACBUILD_ENGINE"] = "chroot" if not system.which('systemctl') else "systemd-nspawn" hooks_env["TAMARIN_ACBUILD_ENGINE"] = "chroot" if not system.which('systemctl') else "systemd-nspawn"
# Run hooks # Run hooks
tamarin.run_profile_hooks(profile, 'containerbuild', cwd=build_workspace, env=hooks_env) tamarin.run_profile_hooks(profile, 'containerbuild', cwd=build_workspace, env=hooks_env, debug=debug)
tamarin.run_acbuild(acbuild_flags+["write", "--overwrite", aci_file]) tamarin.run_acbuild(acbuild_flags+["write", "--overwrite", aci_file], as_root=True, debug=debug)
tamarin.run_acbuild(acbuild_flags+["end"]) tamarin.run_acbuild(acbuild_flags+["end"], as_root=True, debug=debug)
return aci_file return aci_file
def cleanup(build_workspace, rkt_flags): def cleanup(build_workspace, rkt_flags, debug=False):
# Nettoyage des conteneurs # Nettoyage des conteneurs
rkt.run([ rkt.run([
"gc", "gc",
"--grace-period=0" "--grace-period=0"
] + rkt_flags, as_root=True) ] + rkt_flags, as_root=True, debug=debug)
# Nettoyage des images obsolètes du store # Nettoyage des images obsolètes du store
rkt.run([ rkt.run([
"image", "image",
"gc" "gc"
] + rkt_flags, as_root=True) ] + rkt_flags, as_root=True, debug=debug)
# Suppression de l'espace de travail de build # Suppression de l'espace de travail de build
shutil.rmtree(build_workspace, ignore_errors=True) shutil.rmtree(build_workspace, ignore_errors=True)
@ -113,7 +113,7 @@ if __name__ == "__main__":
output_dir = os.path.abspath(args.output) output_dir = os.path.abspath(args.output)
# Load build profile # Load build profile
profile = tamarin.load_profile(args.profile) profile = tamarin.load_profile(args.profile, debug=args.debug)
workspace = tamarin.get_workspace_dir() workspace = tamarin.get_workspace_dir()
workspace_tmp = tamarin.get_workspace_subdir('tmp') workspace_tmp = tamarin.get_workspace_subdir('tmp')
@ -140,16 +140,16 @@ if __name__ == "__main__":
"fetch", "fetch",
"--insecure-options=image", "--insecure-options=image",
base_image base_image
] + rkt_flags) ] + rkt_flags, debug=args.debug)
aci_file = os.path.join(os.sep, build_workspace, 'image.aci') aci_file = os.path.join(os.sep, build_workspace, 'image.aci')
cached_image_file = get_cached_image_path(profile) cached_image_file = get_cached_image_path(profile, debug=args.debug)
if not args.rebuild and os.path.exists(cached_image_file): if not args.rebuild and os.path.exists(cached_image_file):
# Copy cached image # Copy cached image
shutil.copyfile(cached_image_file, aci_file) shutil.copyfile(cached_image_file, aci_file)
else: else:
build_image(build_workspace, aci_file, base_image, profile) build_image(build_workspace, aci_file, base_image, profile, debug=args.debug)
# Cache image # Cache image
shutil.copyfile(aci_file, cached_image_file) shutil.copyfile(aci_file, cached_image_file)
@ -172,11 +172,13 @@ if __name__ == "__main__":
if args.debug: if args.debug:
rkt_args += ["--interactive", "--exec", "/bin/bash"] rkt_args += ["--interactive", "--exec", "/bin/bash"]
helper_cmd = " ".join(["/usr/bin/python3", "/tamarin/lib/build.py", args.profile, args.architecture])
print("Executer '{:s}' pour lancer la construction du paquet.".format(helper_cmd))
else: else:
rkt_args += ["--exec", "/usr/bin/python3", "--", "/tamarin/lib/build.py", args.profile, args.architecture] rkt_args += ["--exec", "/usr/bin/python3", "--", "/tamarin/lib/build.py", args.profile, args.architecture]
# Start container # Start container
rkt.run(rkt_flags+rkt_args, as_root=True) rkt.run(rkt_flags+rkt_args, as_root=True, debug=args.debug)
# Cleanup # Cleanup
cleanup(build_workspace, rkt_flags) cleanup(build_workspace, rkt_flags, debug=args.debug)