Python wrapper with Docker backend with profiles
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
import sys, os, argparse, tempfile
|
||||
sys.path.append(os.path.dirname(__file__) + '/lib')
|
||||
import tamarin, system, rkt
|
||||
import tamarin
|
||||
|
||||
def get_args_parser():
|
||||
parser = argparse.ArgumentParser(description="Tamarin's container entrypoint")
|
||||
@ -12,7 +12,6 @@ def get_args_parser():
|
||||
def get_buildtools_dir():
|
||||
return os.path.realpath(os.path.dirname(os.path.abspath(__file__)) + "/buildtools")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
parser = get_args_parser()
|
||||
|
38
lib/rkt.py
38
lib/rkt.py
@ -1,38 +0,0 @@
|
||||
import system, subprocess, os, tamarin, json, re
|
||||
|
||||
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)"""
|
||||
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
|
||||
if debug:
|
||||
print(" ".join(cmd))
|
||||
if capture_output:
|
||||
return subprocess.check_output(cmd, stdin=subprocess.PIPE)
|
||||
else:
|
||||
return subprocess.check_call(cmd, stdin=subprocess.PIPE)
|
||||
|
||||
def get_images_list(rkt_flags = [], debug=False):
|
||||
output = run([
|
||||
"image",
|
||||
"list",
|
||||
"--format=json"
|
||||
] + rkt_flags, capture_output=True, debug=debug)
|
||||
# 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 = [], debug=False):
|
||||
run([
|
||||
"image",
|
||||
"export",
|
||||
image_id,
|
||||
dest_file,
|
||||
] + rkt_flags, debug=debug)
|
@ -1,29 +0,0 @@
|
||||
import tarfile, os
|
||||
|
||||
def extract_tar(file_path, dest_dir = ".", debug=False):
|
||||
if debug:
|
||||
print('Extracting "{:s}" to "{:s}"'.format(file_path, dest_dir))
|
||||
with tarfile.open(file_path) as tar:
|
||||
tar.extractall(dest_dir)
|
||||
tar.close()
|
||||
|
||||
def which(program, additional_paths = None):
|
||||
|
||||
def is_exe(fpath):
|
||||
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
|
||||
|
||||
fpath, fname = os.path.split(program)
|
||||
if fpath:
|
||||
if is_exe(program):
|
||||
return program
|
||||
else:
|
||||
paths = os.environ["PATH"].split(os.pathsep);
|
||||
if additional_paths != None:
|
||||
paths.append(additional_paths)
|
||||
for path in paths:
|
||||
path = path.strip('"')
|
||||
exe_file = os.path.join(path, program)
|
||||
if is_exe(exe_file):
|
||||
return exe_file
|
||||
|
||||
return None
|
@ -1,8 +1,6 @@
|
||||
import os, glob, subprocess, configparser
|
||||
import web, system
|
||||
import codecs
|
||||
import os, glob, subprocess, configparser, codecs, sys
|
||||
|
||||
def run_profile_hooks(profile, step, cwd=None, env=None, debug=False):
|
||||
def run_profile_hooks(profile, step, **kwargs):
|
||||
hooks_dir = get_hooks_dir()
|
||||
step_hooks = profile[step]["hooks"]
|
||||
if not step_hooks:
|
||||
@ -12,7 +10,7 @@ def run_profile_hooks(profile, step, cwd=None, env=None, debug=False):
|
||||
if not trimmed_hook_name:
|
||||
continue
|
||||
hook_path = os.path.join(hooks_dir, trimmed_hook_name)
|
||||
code = subprocess.check_call(hook_path, cwd=cwd, stdin=subprocess.PIPE, env=env)
|
||||
run([hook_path], **kwargs)
|
||||
|
||||
def get_hooks_dir():
|
||||
return os.path.realpath(os.path.dirname(os.path.abspath(__file__)) + "/../hooks")
|
||||
@ -51,37 +49,17 @@ def get_workspace_subdir(subdir):
|
||||
os.makedirs(dir_path, exist_ok=True)
|
||||
return dir_path
|
||||
|
||||
def get_acbuild_achive_dest_dir():
|
||||
"""Return the first path matching the acbuild archive extraction destination in tamarin workspace"""
|
||||
workspace_tmp = get_workspace_subdir('tmp')
|
||||
return glob.glob(os.path.join(os.sep, workspace_tmp, 'acbuild-v*'))[0]
|
||||
|
||||
def get_rkt_achive_dest_dir():
|
||||
"""Return the first path matching the rkt archive extraction destination in tamarin workspace"""
|
||||
workspace_tmp = get_workspace_subdir('tmp')
|
||||
return glob.glob(os.path.join(os.sep, workspace_tmp, 'rkt-v*'))[0]
|
||||
|
||||
def download_rkt(debug=False):
|
||||
"""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.25.0/rkt-v1.25.0.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)
|
||||
return file_path
|
||||
|
||||
def download_acbuild(debug=False):
|
||||
"""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"
|
||||
file_path=os.path.join(os.sep, get_workspace_subdir('tmp'), "acbuild.tar.gz")
|
||||
web.download_file(file_url=url, dest_path=file_path)
|
||||
return file_path
|
||||
|
||||
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)"""
|
||||
acbuild_bin = system.which('acbuild', get_workspace_subdir('acbuild'))
|
||||
cmd = ( ["sudo", "-E", acbuild_bin] if os.geteuid() != 0 and as_root == True else [acbuild_bin] ) + args
|
||||
def run(cmd, captureOutput=False, pty=False, debug=False, **kwargs):
|
||||
"""Execute an arbitrary command on the system"""
|
||||
if debug:
|
||||
print(" ".join(cmd))
|
||||
stdin=subprocess.PIPE
|
||||
if pty:
|
||||
kwargs['stdin'] = sys.stdin
|
||||
if captureOutput:
|
||||
return subprocess.check_output(cmd, stdin=subprocess.PIPE)
|
||||
return subprocess.check_output(cmd, **kwargs)
|
||||
else:
|
||||
return subprocess.check_call(cmd, stdin=subprocess.PIPE)
|
||||
return subprocess.check_call(cmd, **kwargs)
|
||||
|
||||
def run_docker(args, captureOutput=False, **kwargs):
|
||||
return run(["docker"] + args, captureOutput=captureOutput, **kwargs)
|
||||
|
33
lib/web.py
33
lib/web.py
@ -1,33 +0,0 @@
|
||||
from urllib import request
|
||||
import math, sys
|
||||
|
||||
def print_progress_bar(percent_progress=0, char_size=50, clear_line=True):
|
||||
|
||||
bar_progress = math.floor(char_size*(percent_progress/100))
|
||||
bar = "=" * bar_progress + " " * (char_size - bar_progress)
|
||||
|
||||
if clear_line:
|
||||
sys.stdout.write(u"\u001b[1000D")
|
||||
sys.stdout.write("[{:s}] {:d}%".format(bar, int(percent_progress)))
|
||||
sys.stdout.flush()
|
||||
|
||||
def download_file(file_url, dest_path, bulk_size = 8192):
|
||||
|
||||
req = request.urlopen(file_url)
|
||||
meta = req.info()
|
||||
file_size = int(meta.get('Content-Length'))
|
||||
|
||||
with open(dest_path, 'wb') as dest_file:
|
||||
print('Downloading "{:s}". Size: {:d}b'.format(file_url, file_size))
|
||||
downloaded_size = 0
|
||||
while True:
|
||||
buff = req.read(bulk_size)
|
||||
if not buff:
|
||||
break
|
||||
downloaded_size += len(buff)
|
||||
dest_file.write(buff)
|
||||
progress = downloaded_size/file_size*100
|
||||
print_progress_bar(progress)
|
||||
dest_file.close()
|
||||
# Add linebreak
|
||||
print("")
|
Reference in New Issue
Block a user