From f419a6e4f6e4dcccb4ffc537a0646e5e206a651e Mon Sep 17 00:00:00 2001 From: r00t Date: Wed, 16 Nov 2016 02:59:55 -0500 Subject: [PATCH] checkin for the day. got pychroot added to AUR, so now i can do more work on this. tomorrow. --- bdisk/chroot.py | 46 ++++++++++++++++++- .../templates}/VERSION_INFO.txt.j2 | 2 +- 2 files changed, 45 insertions(+), 3 deletions(-) rename {templates => extra/templates}/VERSION_INFO.txt.j2 (51%) diff --git a/bdisk/chroot.py b/bdisk/chroot.py index 421bbea..3870f5e 100755 --- a/bdisk/chroot.py +++ b/bdisk/chroot.py @@ -8,6 +8,9 @@ import gnupg import tarfile import subprocess import re +import git +import jinja2 +import datetime from urllib.request import urlopen def arch_chk(arch): @@ -133,6 +136,45 @@ def build_chroot(arch, chrootdir, dlpath, extradir): for file in prebuild_arch_overlay[arch]['files']: shutil.copy2(extradir + '/pre-build.d/' + arch + '/' + file, chrootdir + '/' + file) os.chown(chrootdir + '/' + file, 0, 0) - + return(chrootdir) - return(destdir) +def prep_chroot(templates_dir, chrootdir, bdisk, arch): + build = {} + # let's prep some variables to write out the version info.txt + # get the git tag and short commit hash + repo = git.Repo(bdisk['dir']) + refs = repo.git.describe(repo.head.commit).split('-') + build['ver'] = refs[0] + '-' + refs[2] + # and these should be passed in from the args, from the most part. + build['name'] = bdisk['name'] + build['time'] = datetime.datetime.utcnow().strftime("%a %b %d %H:%M:%S UTC %Y") + build['host'] = bdisk['hostname'] + build['user'] = os.environ['USER'] + if os.environ['SUDO_USER']: + build['realuser'] = os.environ['SUDO_USER'] + # and now that we have that dict, let's write out the VERSION_INFO.txt file. + env = jinja2.Environment(loader=FileSystemLoader(templates_dir)) + tpl = env.get_template('VERSION_INFO.txt.j2') + tpl_out = template.render(build = build) + with open(chrootdir + '/root/VERSION_INFO.txt', "wb+") as f: + fh.write(tpl_out) + return(build) + +def mount(srcdir, destdir, fstype, options = ''): + if os.geteuid() != 0: + exit("HEY, you need to run this with root privileges because we do mounting and stuff for chroots.") + # thank you https://stackoverflow.com/a/29156997 + # it'd be greate to use systemd-nspawn for this, but: + # 1.) not all systems even HAVE systemd, and + # 2.) there's no python interface- i'm trying to keep the shell calls to a minimum. + os.makedirs(destdir, exist_ok = True) + ret = ctypes.CDLL('libc.so.6', use_errno = True).mount(srcdir, destdir, fstype, 0, options) + if ret < 0: + errno = ctypes.get_errno() + raise RuntimeError("Error mounting {0} ({1}) on {2} with options '{3}': {4}".format(srcdir, fstype, destdir, options, os.strerror(errno))) + else: + return(True) + +def mount_chroot(chrootdir, destdir): + # here's where we actually set up the mountpoints for a chroot. + mount('', chrootdir + '/proc', 'proc', 'nosuid,noexec,nodev') diff --git a/templates/VERSION_INFO.txt.j2 b/extra/templates/VERSION_INFO.txt.j2 similarity index 51% rename from templates/VERSION_INFO.txt.j2 rename to extra/templates/VERSION_INFO.txt.j2 index b9f9387..b4fb810 100644 --- a/templates/VERSION_INFO.txt.j2 +++ b/extra/templates/VERSION_INFO.txt.j2 @@ -2,4 +2,4 @@ Version: {{ build['ver'] }} Build: {{ build['name'] }} Time: {{ build['time'] }} Machine: {{ build['host'] }} -User: {{ build['user'] }} ({{ build['realuser'] }}) +User: {{ build['user'] }}{% if build['realuser'] is defined and build['realuser'] > 0 %} ({{ build['realuser'] }}){% endif %}