Example #1
0
File: main.py Project: ngsru/heaver
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))
Example #2
0
File: main.py Project: ngsru/heaver
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)))