def list_(args, config): if args.name: all_boxes = ops.ls() if args.name not in all_boxes: report.die("No such container: '%s'" % args.name) boxes = {args.name: all_boxes[args.name]} else: boxes = ops.ls() print report.format(dict(status="OK", action="list", data=boxes, message=format_list(boxes)))
def shutdown(args, config): must_be_root() names = ops.ls() for name in names: if ops.is_running(name): try: ops.stop(name) try: box_config = ops.get_box_config(name) except Exception as e: report.die("Cannot load '%s' config, is it broken? %s" % (name, e)) if os.path.exists(ops.get_box_home(name, "heaver_root_generated")): imager = image.get_image_operator(config["image"]) if box_config["datamounts"]: for mountpoint, source in box_config["datamounts"]: imager.disassemble_instance(source) imager.disassemble_instance(box_config["root"]) except ops.InvocationError as e: report.die("LXC is broken, cannot stop container %s: %s" % (name, e)) except ops.ContainerNotFound as e: # WTF? Should not happen (containers in ops.ls() already exists) report.die("Container %s just disappeared, panic!" % name) send_status_soft(args, config) print report.format(dict(status="OK", action="shutdown", id=name, message="Container %s stopped at shutdown" % name))
def must_get_names(args): "Helper for retrieving container names" if args.all: try: names = ops.ls().keys() except ops.InvocationError as e: report.die("LXC is broken: %s" % e) else: if not ops.exists(args.name): report.die("No such container: %s" % args.name) names = [args.name] return names
def make_tarball(args, config): must_be_root() if not args.name: report.die("No container chosen (tarball cannot be feed with --all)") name = args.name if name not in ops.ls(): report.die("No such container: '%s'" % name) if ops.is_running(name): report.die("Container '%s' must be stopped for tarballing" % name) tar_path = args.tarball if tar_path == "<generated>": # make temporary path for tarball import tempfile try: tar_fd, tar_path = tempfile.mkstemp(dir=HEAVER_TMPDIR, prefix="%s.tar." % name) tar_file = os.fdopen(tar_fd, "w") except Exception as e: # FIXME: proper exceptions? report.die("Cannot create tarball of container '%s': '%s'" % (name, e)) else: try: tar_file = open(tar_path, "wb") except Exception as e: # FIXME: proper exceptions? # cannot open file - no directory or no write access report.die("Cannot create tarball of container '%s': %s" % (name, e)) box_config = ops.get_box_config(name) imager = image.get_image_operator(config["image"]) imager.assemble_instance(box_config["root"]) try: ops.write_tarball(name, tar_file) except Exception as e: # FIXME: proper exceptions? tar_file.close() os.unlink(tar_path) report.die("Cannot create tarball of container '%s': %s" % (name, e)) tar_file.close() print report.format(dict(status="OK", action="tarball", data=tar_path, message="Tarball of container '%s' created at '%s'" % (name, tar_path)))
def get_status(config): if "hostname" in config: hostname = config["hostname"] else: import platform hostname = platform.node() status = dict(la=ops.get_la(), ram=ops.get_ram(), oom=ops.get_oom_stats(), boxes=ops.ls(), hostname=hostname) used_ranges = utils.sync_open(config["net"]["used_ranges_path"], "a+") networks = must_load_net_config(config["net"]["networks"], used_ranges.read().strip().split()) used_ranges.close() ips_free = 0 for net in networks.values(): ips_free += net["pool"].count_free_addresses() status["ips_free"] = ips_free imager = image.get_image_operator(config["image"]) status["fs"] = imager.get_free_space() status["now"] = time.time() return status
def startup(args, config): must_be_root() unset_cgroup_hierarchy() names = ops.ls() for name in names: running_flag = ops.get_box_home(name, "heaver_box_running") if os.path.exists(running_flag): try: try: box_config = ops.get_box_config(name) except Exception as e: report.die("Cannot load '%s' config, is it broken? %s" % (name, e)) if os.path.exists(ops.get_box_home(name, "heaver_root_generated")): imager = image.get_image_operator(config["image"]) imager.assemble_instance(box_config["root"]) if box_config["datamounts"]: root = box_config["root"] for mountpoint, source in box_config["datamounts"]: imager.assemble_instance(source) # since 'mountpoint' starts with / we need to remove it mountpoint = mountpoint.lstrip("/") mount_path = os.path.join(root, mountpoint) if not os.path.exists(mount_path): os.makedirs(mount_path) ops.start(name) except ops.InvocationError as e: report.die("LXC is broken, cannot start container %s: %s" % (name, e)) except ops.ContainerNotFound as e: # WTF? Should not happen (containers in ops.ls() already exists) report.die("Container %s just disappeared, panic!" % name) send_status_soft(args, config) print report.format(dict(status="OK", action="startup", id=name, message="Container %s started after boot" % name))