def push(image, dist): if (image is None): squashfs_file = get_from_config("images", "trg_img", dist) if (squashfs_file == "" or squashfs_file == None): image_name = dist + "_image.squashfs" squashfs_file = "/var/lib/clara/images/" + image_name else: squashfs_file = image if not os.path.isfile(squashfs_file): clara_exit("{0} doesn't exist.".format(squashfs_file)) # Send files where they will be used sftp_mode = has_config_value("images", "hosts", dist) dargs = docopt.docopt(__doc__) if sftp_mode: sftp_user = get_from_config("images", "sftp_user", dist) sftp_private_key = get_from_config("images", "sftp_private_key", dist) sftp_passphrase = get_from_config_or("images", "sftp_passphrase", dist, None) sftp_hosts = get_from_config("images", "hosts", dist).split(',') sftp_client = sftp.Sftp(sftp_hosts, sftp_user, sftp_private_key, sftp_passphrase) sftp_client.upload([squashfs_file], os.path.dirname(squashfs_file), 0o755) else: clara_exit("Hosts not found for the image {0}".format(squashfs_file))
def genimg(image, work_dir, dist): if (image is None): squashfs_file = get_from_config("images", "trg_img", dist) if (squashfs_file == "" or squashfs_file == None): image_name = dist + "_image.squashfs" squashfs_file = "/var/lib/clara/images/" + image_name if os.path.isfile(squashfs_file): os.rename(squashfs_file, squashfs_file + ".old") logging.info( "Previous image renamed to {0}.".format(squashfs_file + ".old")) else: path_to_image = os.path.dirname(image) if not os.path.isdir(path_to_image) and len(path_to_image) != 0: os.makedirs(path_to_image) squashfs_file = image if os.path.isfile(squashfs_file): os.rename(squashfs_file, squashfs_file + ".old") logging.info("Previous image renamed to {0}.".format(squashfs_file + ".old")) logging.info("Creating image at {0}".format(squashfs_file)) if not os.path.exists(os.path.dirname(squashfs_file)): logging.info("Creating local directory path", os.path.dirname(squashfs_file)) os.makedirs(os.path.dirname(squashfs_file)) if conf.ddebug: run([ "mksquashfs", work_dir, squashfs_file, "-no-exports", "-noappend", "-info" ]) else: run([ "mksquashfs", work_dir, squashfs_file, "-no-exports", "-noappend" ]) os.chmod(squashfs_file, 0o755) sftp_mode = has_config_value("images", "hosts", dist) dargs = docopt.docopt(__doc__) if sftp_mode and not dargs['--no-sync']: sftp_user = get_from_config("images", "sftp_user", dist) sftp_private_key = get_from_config("images", "sftp_private_key", dist) sftp_passphrase = get_from_config_or("images", "sftp_passphrase", dist, None) sftp_hosts = get_from_config("images", "hosts", dist).split(',') sftp_client = sftp.Sftp(sftp_hosts, sftp_user, sftp_private_key, sftp_passphrase) sftp_client.upload([squashfs_file], os.path.dirname(squashfs_file), 0o755)
def mktorrent(image): if (image is None): squashfs_file = get_from_config("images", "trg_img", dist) else: squashfs_file = image trackers_port = get_from_config("p2p", "trackers_port", dist) trackers_schema = get_from_config("p2p", "trackers_schema", dist) seeding_service = get_from_config("p2p", "seeding_service", dist) init_stop = get_from_config("p2p", "init_stop", dist) init_start = get_from_config("p2p", "init_start", dist) # trackers is a dictionary with pairs nodeset and torrent file trackers = {} for e in get_from_config("p2p", "trackers", dist).split(";"): k, v = e.split(":") trackers[k] = v # seeders in the config file is a dictionary with pairs nodeset and torrent file seeders_dict = {} for e in get_from_config("p2p", "seeders", dist).split(";"): k, v = e.split(":") seeders_dict[k] = v seeders = ",".join(seeders_dict.keys()) if not os.path.isfile(squashfs_file): clara_exit("{0} doesn't exist".format(squashfs_file)) # Remove old torrent files for f in trackers.values(): if os.path.isfile(f): os.remove(f) clush(seeders, init_stop.format(seeding_service)) sftp_mode = has_config_value("p2p", "sftp_user", dist) if sftp_mode: sftp_user = get_from_config("p2p", "sftp_user", dist) sftp_private_key = get_from_config("p2p", "sftp_private_key", dist) sftp_passphrase = get_from_config_or("p2p", "sftp_passphrase", dist, None) sftp_client = sftp.Sftp(seeders.split(','), sftp_user, sftp_private_key, sftp_passphrase) for e in trackers.keys(): announce = [] for t in list(ClusterShell.NodeSet.NodeSet(e)): announce.append("{0}://{1}:{2}/announce".format(trackers_schema, t, trackers_port)) run(["/usr/bin/mktorrent", "-a", ",".join(announce), "-o", trackers[e], squashfs_file]) if sftp_mode: sftp_client.upload([trackers[e]], os.path.dirname(trackers[e])) clush(seeders, init_start.format(seeding_service))
def geninitrd(path, work_dir, dist): ID, VERSION_ID = get_osRelease(dist) image = imageInstant(work_dir, ID, VERSION_ID) distrib = image.dist if (path is None): trg_dir = get_from_config("images", "trg_dir", dist) if (trg_dir == "" or trg_dir == None): trg_dir = "/var/lib/clara/images/" else: trg_dir = path if not os.path.isdir(trg_dir): os.makedirs(trg_dir) squashfs_file = get_from_config("images", "trg_img", dist) if (squashfs_file == "" or squashfs_file == None): image_name = dist + "_image.squashfs" squashfs_file = "/var/lib/clara/images/" + image_name if not os.path.isfile(squashfs_file): clara_exit("{0} does not exist!".format(squashfs_file)) if conf.ddebug: run(["unsquashfs", "-li", "-f", "-d", work_dir, squashfs_file]) else: run(["unsquashfs", "-f", "-d", work_dir, squashfs_file]) mount_chroot(work_dir) # Install the kernel in the image kver = get_from_config("images", "kver", dist) if len(kver) == 0: clara_exit("kver hasn't be set in config.ini") else: run_chroot(["chroot", work_dir, distrib["pkgManager"], "update"], work_dir) if ID == "debian": run_chroot([ "chroot", work_dir, distrib["pkgManager"], "install", "--no-install-recommends", "--yes", "--force-yes", "linux-image-" + kver ], work_dir) if ID == "centos": run_chroot([ "chroot", work_dir, distrib["pkgManager"], "install", "-y", "kernel-" + kver ], work_dir) # Install packages from 'packages_initrd' packages_initrd = get_from_config("images", "packages_initrd", dist) if len(packages_initrd) == 0: logging.warning("packages_initrd hasn't be set in config.ini") else: pkgs = packages_initrd.split(',') if ID == "debian": opts = ["--no-install-recommends", "--yes", "--force-yes"] intitrd_opts = ["-o", "/tmp/initrd-" + kver, kver] if ID == "centos": opts = ["-y"] intitrd_opts = [ "--force", "--add", "livenet", "-v", "/tmp/initrd-" + kver, "--kver", kver ] opts = ["chroot", work_dir, distrib["pkgManager"], "install" ] + opts + pkgs run_chroot(opts, work_dir) # Generate the initrd in the image intitrd_opts = ["chroot", work_dir, distrib["initrdGen"]] + intitrd_opts run_chroot(intitrd_opts, work_dir) umount_chroot(work_dir) # Copy the initrd out of the chroot initrd_file = trg_dir + "/initrd-" + kver shutil.copy(work_dir + "/tmp/initrd-" + kver, initrd_file) os.chmod(initrd_file, 0o644) logging.info("Initrd available at " + initrd_file) # Copy vmlinuz out of the chroot vmlinuz_file = trg_dir + "/vmlinuz-" + kver shutil.copy(work_dir + "/boot/vmlinuz-" + kver, vmlinuz_file) os.chmod(vmlinuz_file, 0o644) logging.info("vmlinuz available at " + vmlinuz_file) # Send files where they will be used sftp_mode = has_config_value("images", "hosts", dist) dargs = docopt.docopt(__doc__) if sftp_mode and not dargs['--no-sync']: sftp_user = get_from_config("images", "sftp_user", dist) sftp_private_key = get_from_config("images", "sftp_private_key", dist) sftp_passphrase = get_from_config_or("images", "sftp_passphrase", dist, None) sftp_hosts = get_from_config("images", "hosts", dist).split(',') sftp_client = sftp.Sftp(sftp_hosts, sftp_user, sftp_private_key, sftp_passphrase) sftp_client.upload([initrd_file, vmlinuz_file], trg_dir, 0o644)
def geninitrd(path): if (path is None): trg_dir = get_from_config("images", "trg_dir", dist) else: trg_dir = path if not os.path.isdir(trg_dir): os.makedirs(trg_dir) squashfs_file = get_from_config("images", "trg_img", dist) if not os.path.isfile(squashfs_file): clara_exit("{0} does not exist!".format(squashfs_file)) if conf.ddebug: run(["unsquashfs", "-li", "-f", "-d", work_dir, squashfs_file]) else: run(["unsquashfs", "-f", "-d", work_dir, squashfs_file]) mount_chroot() # Install the kernel in the image kver = get_from_config("images", "kver", dist) if len(kver) == 0: clara_exit("kver hasn't be set in config.ini") else: run_chroot(["chroot", work_dir, "apt-get", "update"]) run_chroot([ "chroot", work_dir, "apt-get", "install", "--no-install-recommends", "--yes", "--force-yes", "linux-image-" + kver ]) # Install packages from 'packages_initrd' packages_initrd = get_from_config("images", "packages_initrd", dist) if len(packages_initrd) == 0: logging.warning("packages_initrd hasn't be set in config.ini") else: pkgs = packages_initrd.split(',') run_chroot([ "chroot", work_dir, "apt-get", "install", "--no-install-recommends", "--yes", "--force-yes" ] + pkgs) # Generate the initrd in the image run_chroot( ["chroot", work_dir, "mkinitramfs", "-o", "/tmp/initrd-" + kver, kver]) umount_chroot() # Copy the initrd out of the chroot initrd_file = trg_dir + "/initrd-" + kver shutil.copy(work_dir + "/tmp/initrd-" + kver, initrd_file) os.chmod(initrd_file, 0o644) logging.info("Initrd available at " + initrd_file) # Copy vmlinuz out of the chroot vmlinuz_file = trg_dir + "/vmlinuz-" + kver shutil.copy(work_dir + "/boot/vmlinuz-" + kver, vmlinuz_file) os.chmod(vmlinuz_file, 0o644) logging.info("vmlinuz available at " + vmlinuz_file) # Send files where they will be used sftp_mode = has_config_value("images", "hosts", dist) dargs = docopt.docopt(__doc__) if sftp_mode and not dargs['--no-sync']: sftp_user = get_from_config("images", "sftp_user", dist) sftp_private_key = get_from_config("images", "sftp_private_key", dist) sftp_passphrase = get_from_config_or("images", "sftp_passphrase", dist, None) sftp_hosts = get_from_config("images", "hosts", dist).split(',') sftp_client = sftp.Sftp(sftp_hosts, sftp_user, sftp_private_key, sftp_passphrase) sftp_client.upload([initrd_file, vmlinuz_file], trg_dir, 0o644)