Tamarin/lib/rkt.py

39 lines
1.3 KiB
Python
Raw Normal View History

2017-01-24 17:32:19 +01:00
import system, subprocess, os, tamarin, json, re
2017-03-10 21:51:57 +01:00
def run(args, as_root = False, capture_output=False, debug=False):
2017-01-24 17:32:19 +01:00
"""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
2017-03-10 21:51:57 +01:00
if debug:
print(" ".join(cmd))
2017-01-24 17:32:19 +01:00
if capture_output:
return subprocess.check_output(cmd, stdin=subprocess.PIPE)
else:
return subprocess.call(cmd, stdin=subprocess.PIPE)
2017-03-10 21:51:57 +01:00
def get_images_list(rkt_flags = [], debug=False):
2017-01-24 17:32:19 +01:00
output = run([
"image",
"list",
"--format=json"
2017-03-10 21:51:57 +01:00
] + rkt_flags, capture_output=True, debug=debug)
2017-01-24 17:32:19 +01:00
# 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
2017-03-10 21:51:57 +01:00
def export_image(image_id, dest_file, rkt_flags = [], debug=False):
run([
2017-01-24 17:32:19 +01:00
"image",
"export",
image_id,
dest_file,
2017-03-10 21:51:57 +01:00
] + rkt_flags, debug=debug)