2017-01-19 22:56:17 +01:00
|
|
|
import os, glob, subprocess, configparser
|
2017-01-18 17:43:46 +01:00
|
|
|
import web, system
|
2017-02-09 21:53:24 +01:00
|
|
|
import codecs
|
2017-01-18 17:43:46 +01:00
|
|
|
|
2017-03-10 21:51:57 +01:00
|
|
|
def run_profile_hooks(profile, step, cwd=None, env=None, debug=False):
|
2017-01-24 23:15:13 +01:00
|
|
|
hooks_dir = get_hooks_dir()
|
2017-02-09 21:53:24 +01:00
|
|
|
step_hooks = profile[step]["hooks"]
|
|
|
|
if not step_hooks:
|
|
|
|
return
|
|
|
|
for hook_name in step_hooks.split(","):
|
|
|
|
trimmed_hook_name = hook_name.strip(' \t\n\r')
|
|
|
|
if not trimmed_hook_name:
|
|
|
|
continue
|
|
|
|
hook_path = os.path.join(hooks_dir, trimmed_hook_name)
|
|
|
|
code = subprocess.call(hook_path, cwd=cwd, stdin=subprocess.PIPE, env=env)
|
|
|
|
if code != 0:
|
|
|
|
raise Exception("Hook '{:s}' exited with a non zero code ({:d}) !".format(trimmed_hook_name, code))
|
2017-01-24 23:15:13 +01:00
|
|
|
|
|
|
|
def get_hooks_dir():
|
|
|
|
return os.path.realpath(os.path.dirname(os.path.abspath(__file__)) + "/../hooks")
|
|
|
|
|
2017-01-25 14:49:30 +01:00
|
|
|
def get_lib_dir():
|
|
|
|
return os.path.realpath(os.path.dirname(os.path.abspath(__file__)) + "/../lib")
|
|
|
|
|
2017-03-10 21:51:57 +01:00
|
|
|
def load_profile(profile_name, debug=False):
|
2017-01-19 22:56:17 +01:00
|
|
|
profile_filename = profile_name+".conf"
|
|
|
|
for profile_file in get_available_profiles():
|
|
|
|
if profile_filename == os.path.basename(profile_file):
|
|
|
|
config = configparser.ConfigParser()
|
2017-02-09 21:53:24 +01:00
|
|
|
with codecs.open(profile_file, encoding = 'utf-8', mode = 'r') as handle:
|
|
|
|
config.read_file(handle)
|
|
|
|
return config
|
2017-01-19 22:56:17 +01:00
|
|
|
return None
|
|
|
|
|
|
|
|
def get_profiles_dir():
|
|
|
|
return os.path.realpath(os.path.dirname(os.path.abspath(__file__)) + "/../profiles")
|
|
|
|
|
2017-01-19 17:39:55 +01:00
|
|
|
def get_available_profiles():
|
2017-01-19 22:56:17 +01:00
|
|
|
return glob.glob(get_profiles_dir() + '/*.conf')
|
|
|
|
|
|
|
|
def get_available_profile_names():
|
|
|
|
profile_files = get_available_profiles()
|
2017-01-19 17:39:55 +01:00
|
|
|
return [os.path.splitext(os.path.basename(f))[0] for f in profile_files]
|
|
|
|
|
2017-01-18 17:43:46 +01:00
|
|
|
def get_workspace_dir():
|
|
|
|
"""Return the absolute path to the tamarin workspace ($HOME/.tamarin)"""
|
|
|
|
home = os.environ["HOME"]
|
|
|
|
return os.path.join(os.sep, home, '.tamarin')
|
|
|
|
|
|
|
|
def get_workspace_subdir(subdir):
|
|
|
|
"""Return the absolute path to a subdirectory in tamarin workspace"""
|
|
|
|
dir_path = os.path.join(os.sep, get_workspace_dir(), 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]
|
|
|
|
|
2017-03-10 21:51:57 +01:00
|
|
|
def download_rkt(debug=False):
|
2017-01-18 17:43:46 +01:00
|
|
|
"""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"
|
|
|
|
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
|
|
|
|
|
2017-03-10 21:51:57 +01:00
|
|
|
def download_acbuild(debug=False):
|
2017-01-18 17:43:46 +01:00
|
|
|
"""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
|
|
|
|
|
2017-03-10 21:51:57 +01:00
|
|
|
def run_acbuild(args, captureOutput=False, as_root=False, debug=False):
|
2017-01-18 17:43:46 +01:00
|
|
|
"""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'))
|
2017-03-10 21:51:57 +01:00
|
|
|
cmd = ( ["sudo", "-E", acbuild_bin] if os.geteuid() != 0 and as_root == True else [acbuild_bin] ) + args
|
|
|
|
if debug:
|
|
|
|
print(" ".join(cmd))
|
2017-01-20 17:22:54 +01:00
|
|
|
if captureOutput:
|
2017-03-10 21:51:57 +01:00
|
|
|
return subprocess.check_output(cmd, stdin=subprocess.PIPE)
|
2017-01-20 17:22:54 +01:00
|
|
|
else:
|
2017-03-10 21:51:57 +01:00
|
|
|
return subprocess.call(cmd, stdin=subprocess.PIPE)
|