38 lines
1.2 KiB
Python
38 lines
1.2 KiB
Python
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 = []):
|
|
run([
|
|
"image",
|
|
"export",
|
|
image_id,
|
|
dest_file,
|
|
] + rkt_flags)
|