From 02747946747c1aaa246aa720380a0924d77cd714 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Thu, 19 May 2016 00:12:44 +0000 Subject: [PATCH] Add a common module library. --- server/build_image.py | 16 ++++++--- server/module_lib/icon_lib.py | 62 +++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 server/module_lib/icon_lib.py diff --git a/server/build_image.py b/server/build_image.py index 3d05e1e..854d389 100755 --- a/server/build_image.py +++ b/server/build_image.py @@ -216,17 +216,21 @@ class ImageBuilder(object): os.fchmod(fh.fileno(), 0o744) def _InstallPackages(self, chroot_path): - os.environ['DEBIAN_FRONTEND'] = 'noninteractive' + env = os.environ.copy() + env['DEBIAN_FRONTEND'] = 'noninteractive' + self._ExecChroot( chroot_path, 'apt-get', - 'update') + 'update', + env=env) self._ExecChroot( chroot_path, 'apt-get', 'install', '--assume-yes', - *self._BASE_PACKAGES) + *self._BASE_PACKAGES, + env=env) def _WriteVersion(self, chroot_path, timestamp): with open(os.path.join(chroot_path, 'etc', 'iconograph.json'), 'w') as fh: @@ -240,13 +244,17 @@ class ImageBuilder(object): fh.write('\n') def _RunModules(self, chroot_path): + env = os.environ.copy() + env['PYTHONPATH'] = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), 'module_lib')) + for module in self._modules: self._Exec( '%(module)s --chroot-path=%(chroot_path)s' % { 'module': module, 'chroot_path': chroot_path, }, - shell=True) + shell=True, + env=env) def _CleanPackages(self, chroot_path): self._ExecChroot( diff --git a/server/module_lib/icon_lib.py b/server/module_lib/icon_lib.py new file mode 100644 index 0000000..c015dd9 --- /dev/null +++ b/server/module_lib/icon_lib.py @@ -0,0 +1,62 @@ +#!/usr/bin/python3 + +import os +import shutil +import subprocess +import sys + + +class IconModule(object): + + def __init__(self, chroot_path): + self._chroot_path = chroot_path + + def Exec(self, *args, **kwargs): + print('+', args) + subprocess.check_call(args, **kwargs) + + def ExecChroot(self, *args, **kwargs): + self.Exec('chroot', self._chroot_path, *args, **kwargs) + + def CopyRootFSOverlay(self, source_dir): + start_pos = len(source_dir) + 1 + for dirpath, dirnames, filenames in os.walk(source_dir): + dest_dir = os.path.join(self._chroot_path, dirpath[start_pos:]) + # pylint: disable=unexpected-keyword-arg + os.makedirs(dest_dir, exist_ok=True) + shutil.copystat(dirpath, dest_dir) + for dirname in dirnames: + source_path = os.path.join(dirpath, dirname) + dest_path = os.path.join(dest_dir, dirname) + try: + link = os.readlink(source_path) + os.symlink(link, dest_path) + except OSError: + pass + for filename in filenames: + source_path = os.path.join(dirpath, filename) + dest_path = os.path.join(dest_dir, filename) + try: + link = os.readlink(source_path) + os.symlink(link, dest_path) + except OSError: + shutil.copy(source_path, dest_path) + + # In case we copied libraries + self.ExecChroot('ldconfig') + + def InstallPackages(self, *packages): + self.ExecChroot('apt-get', 'install', '--assume-yes', '--no-install-recommends', *packages) + + def InstallPythonPackages(self, *packages): + self.InstallPackages('python-pip') + self.ExecChroot('pip', 'install', *packages) + + def AddSystemUsers(self, *users): + for user in users: + self.ExecChroot('adduser', '--system', '--group', '--no-create-home', '--disabled-login', user) + + def AddKernelModules(self, *modules): + with open(os.path.join(self._chroot_path, 'etc', 'modules'), 'a') as fh: + for module in modules: + fh.write('%s\n' % module)