def transfer_into_tarball(path, arc_name, tb): fns = [arc_name] util.print_iterable(fns, header="Adding the following to your tarball %s" % (util.quote(tb.name))) print("Please wait...") tb.add(path, arc_name, recursive=False)
def modify(name, root, cfg): rpms = expand_rpms(cfg.get('rpms')) if not rpms: return util.print_iterable(rpms, header=("Installing the following rpms" " in module %s" % (util.quote(name)))) util.ensure_dir(util.abs_join(root, 'tmp')) cleanup_fns = [] for fn in rpms: cp_to = util.abs_join(root, 'tmp', os.path.basename(fn)) util.copy(fn, cp_to) cleanup_fns.append(cp_to) real_fns = [] for fn in rpms: real_fns.append(os.path.join('/tmp', os.path.basename(fn))) cmd = ['chroot', root, 'yum', '--nogpgcheck', '-y', 'localinstall'] cmd.extend(real_fns) try: util.subp(cmd, capture=False) finally: # Ensure cleaned up for fn in cleanup_fns: util.del_file(fn)
def download(self): (cache_pth, exists_there) = self._check_cache() if exists_there: return cache_pth print("Downloading from: %s" % (util.quote(self.where_from))) util.ensure_dirs([os.path.dirname(cache_pth)]) print("To: %s" % (util.quote(cache_pth))) util.download_url(self.where_from, cache_pth) try: meta_js = { 'cached_on': util.time_rfc2822(), 'from': self.where_from, 'root_file': self.root_file, } util.write_file("%s.json" % (cache_pth), "%s\n" % (json.dumps(meta_js, indent=4))) return self._adjust_real_root(cache_pth) except: util.del_file(cache_pth) raise
def extract_into(tmp_file_name, fs_type, config): with util.tempdir() as tdir: # Download the image # TODO (make this a true module that can be changed...) tb_down = tar_ball.TarBallDownloader(dict(config['download'])) arch_fn = tb_down.download() # Extract it devname = create_loopback(tmp_file_name, PART_OFFSET) with cmd_undo(['losetup', '-d', devname]): # Mount it root_dir = os.path.join(tdir, 'mnt') os.makedirs(root_dir) util.subp(['mount', devname, root_dir]) # Extract it with cmd_undo(['umount', root_dir]): print("Extracting 'root' tarball %s to %s." % (util.quote(arch_fn), util.quote(root_dir))) util.subp(['tar', '-xzf', arch_fn, '-C', root_dir]) # Fixup the fstab fix_fstab(root_dir, fs_type)
def format_blank(tmp_file_name, size, fs_type): print("Creating the image output file %s (scratch-version)." % (util.quote(tmp_file_name))) with open(tmp_file_name, 'w+') as o_fh: o_fh.truncate(0) cmd = ['qemu-img', 'create', '-f', 'raw', tmp_file_name, size] util.subp(cmd) # Run fdisk on it print("Creating a partition table in %s." % (util.quote(tmp_file_name))) devname = create_loopback(tmp_file_name) with cmd_undo(['losetup', '-d', devname]): # These are commands to fdisk that will get activated (in order) fdisk_in = [ 'n', 'p', '1', '1', '', 'w' ] cmd = ['fdisk', devname] util.subp(cmd, data="\n".join(fdisk_in), rcs=[0, 1]) print("Creating a filesystem of type %s in %s." % (util.quote(fs_type), util.quote(tmp_file_name))) devname = create_loopback(tmp_file_name, PART_OFFSET) # Get a filesystem on it with cmd_undo(['losetup', '-d', devname]): cmd = ['mkfs.%s' % (fs_type), devname] util.subp(cmd)
def _adjust_real_root(self, arch_path): if self.root_file: print("Oh you really meant %s, finding that file..." % (util.quote(self.root_file))) # Extract and then copy over the right file... with util.tempdir() as tdir: arch_dir = os.path.join(tdir, 'archive') os.makedirs(arch_dir) util.subp(['tar', '-xzf', arch_path, '-C', arch_dir]) root_gz = util.find_file(self.root_file, arch_dir) if not root_gz: raise RuntimeError(("Needed file %r not found in" " extracted contents of %s") % (self.root_file, arch_path)) else: util.copy(root_gz, arch_path) return arch_path
def modify(name, root, cfg): user_names = cfg.get("add_users") if not user_names: return util.print_iterable(user_names, header="Adding the following sudo users in module %s" % (util.quote(name))) for uname in user_names: cmd = ["chroot", root, "useradd", "-m", str(uname)] util.subp(cmd, capture=False) if os.path.isfile(os.path.join(root, "etc", "sudoers")): with open(os.path.join(root, "etc", "sudoers"), "a") as fh: new_entry = "%s ALL=(ALL) ALL" % (uname) fh.write("%s\n" % (new_entry))
def main(): parser = optparse.OptionParser() parser.add_option("-s", '--size', dest="size", metavar="SIZE", help="image size (qemu-img understandable)") parser.add_option("-o", '--output', dest="file_name", metavar="FILE", help="output filename") parser.add_option('--fs-type', dest="fs_type", metavar="FILESYSTEM", default='ext4', help=("filesystem type to create" ' (default: %default)')) parser.add_option('-c', '--config', metavar='FILE', dest='config', action='store', default=os.path.join(os.getcwd(), "build.yaml"), help=("yaml config file" " (default: %default)")) parser.add_option('-x', '--compress', dest='compress', action='store_true', default=False, help=("compress the created image set" " (default: %default)")) parser.add_option('--strip', dest='strip_parts', action='store_false', default=True, help=("strip the image partition table" " (default: %default)")) (options, _args) = parser.parse_args() # Ensure options are ok if not options.size: parser.error("Option -s is required") if not options.file_name: parser.error("Option -o is required") if not options.config: parser.error("Option -c is required") full_fn = os.path.abspath(options.file_name) final_format = 'qcow2' config = {} with open(options.config, 'r') as fh: config = util.load_yaml(fh.read()) print("Loaded builder config from %s:" % (util.quote(options.config))) print(json.dumps(config, sort_keys=True, indent=4)) with tempfile.NamedTemporaryFile(suffix='.raw') as tfh: tmp_file_name = tfh.name format_blank(tmp_file_name, options.size, options.fs_type) extract_into(tmp_file_name, options.fs_type, config) (ran, fails) = activate_modules(tmp_file_name, config) if len(fails): fail_am = util.quote(str(len(fails)), quote_color='red') else: fail_am = '0' print("Ran %s modules with %s failures." % (len(ran), fail_am)) if len(fails): print(("Not performing scratch to final image" " conversion due to %s failures!!") % (fail_am)) return len(fails) print("Converting %s to final file %s." % (util.quote(tmp_file_name), util.quote(full_fn))) ec2_convert(tmp_file_name, full_fn, final_format, options.strip_parts, options.compress) return 0