Example #1
0
File: vm.py Project: hfm/maglica
def attach_disk(args):
    options = {
        "mandatory": ["name", "size"],
        "optional" : [],
    }
    check_args(args, options)

    size = args["size"]
    if re.match(r'.+G$', size):
        args["size"] = int(args["size"][:-1])
        args["size"] = args["size"] * 1024 * 1024
    elif re.match(r'.+M$', size):
        args["size"] = int(args["size"][:-1])
        args["size"] = args["size"] * 1024
    elif re.match(r'.+K$', size):
        args["size"] = int(args["size"][:-1])

    if args["size"] > 100 * 1024 * 1024:
        raise Exception("Size is too large.")

    name = args["name"]
    virt = Virt(hosts())
    (dom, host) = virt.get_active_domain(name)
    maglica.dispatcher.dispatch({
        "type"   : "vm",
        "host"   : host,
        "action" : "attach_disk",
        "args"   : args,
    })
Example #2
0
File: vm.py Project: hfm/maglica
def clone(args): 
    virt = Virt(hosts())

    options = {
        "mandatory": ["image", "hostname"],
        "optional" : ["on", "start", "format"],
    }
    check_args(args, options)

    images = maglica.image.list()
    target_hosts = []

    for image in images:
        if args["image"] == image["name"]:
            target_hosts.append(image["host"])

    if len(target_hosts) < 1:
        raise Exception('Image "%s" is active or not exist.' % args["image"])

    if args.has_key("on"):
        host = args["on"]
    else:
        min = 65535
        for target_host in target_hosts:
            size = len(virt.get_active_domains_of(target_host["name"])) * 10 / target_host["weight"]
            if size < min:
                min = size
                host = target_host["name"]

    maglica.dispatcher.dispatch({
        "type"   : "vm",
        "action" : "clone",
        "host"   : host,
        "args"   : args,
    })
Example #3
0
File: virt.py Project: hfm/maglica
def test_get_domains():
    virt = Virt()
    virt.hosts[0]["conn"].defineXML(xml)
    dom = virt.get_inactive_domain("test2")
    domains = virt.get_domains()
    eq_(domains[0]["name"], "test")
    eq_(domains[1]["name"], "test2")
Example #4
0
def remove(args):
    options = {
        "mandatory": ["name"],
        "optional": ["on"],
    }
    check_args(args, options)
    name = args["name"]

    virt = Virt(hosts())
    dom = virt.get_inactive_domain(name)

    if not dom:
        (dom, host) = virt.get_active_domain(name)
        if dom:
            raise Exception("Active domain cannot be removed.Please stop it.")
        else:
            raise Exception("Domain not found.")

    host = dom["host"]
    if args.has_key("on"):
        host = args["on"]

    maglica.dispatcher.dispatch({
        "type": "vm",
        "host": host,
        "action": "remove",
        "args": args,
    })
Example #5
0
File: vm.py Project: hfm/maglica
def remove(args):
    options = {
        "mandatory": ["name"],
        "optional" : ["on"],
    }
    check_args(args, options)
    name = args["name"]

    virt = Virt(hosts())
    dom = virt.get_inactive_domain(name)

    if not dom:
        (dom, host) = virt.get_active_domain(name)
        if dom:
            raise Exception("Active domain cannot be removed.Please stop it.")
        else:
            raise Exception("Domain not found.")

    host = dom["host"]
    if args.has_key("on"):
        host = args["on"]

    maglica.dispatcher.dispatch({
        "type"   : "vm",
        "host"   : host,
        "action" : "remove",
        "args"   : args,
    })
Example #6
0
File: virt.py Project: hfm/maglica
def test_get_inactive_domain():
    virt = Virt()
    virt.hosts[0]["conn"].defineXML(xml)
    dom = virt.get_inactive_domain("test2")
    eq_(dom["name"], "test2")
    eq_(dom["host"], "test")
    eq_(dom["state"], "shut off")
Example #7
0
def attach_disk(args):
    options = {
        "mandatory": ["name", "size"],
        "optional": [],
    }
    check_args(args, options)

    size = args["size"]
    if re.match(r'.+G$', size):
        args["size"] = int(args["size"][:-1])
        args["size"] = args["size"] * 1024 * 1024
    elif re.match(r'.+M$', size):
        args["size"] = int(args["size"][:-1])
        args["size"] = args["size"] * 1024
    elif re.match(r'.+K$', size):
        args["size"] = int(args["size"][:-1])

    if args["size"] > 100 * 1024 * 1024:
        raise Exception("Size is too large.")

    name = args["name"]
    virt = Virt(hosts())
    (dom, host) = virt.get_active_domain(name)
    maglica.dispatcher.dispatch({
        "type": "vm",
        "host": host,
        "action": "attach_disk",
        "args": args,
    })
Example #8
0
File: vm.py Project: hfm/maglica
def set_boot_device(args):
    options = {
        "mandatory": ["name", "dev"],
        "optional" : [],
    }
    check_args(args, options)
     
    virt = Virt(hosts())
    virt.set_boot_device(args["name"], args["dev"])
Example #9
0
def destroy(args):
    options = {
        "mandatory": ["name"],
        "optional": [],
    }
    check_args(args, options)

    virt = Virt(hosts())
    virt.destroy(args["name"])
Example #10
0
def console(args):
    options = {
        "mandatory": ["name"],
        "optional": [],
    }
    check_args(args, options)

    virt = Virt(hosts())
    virt.console(args["name"])
Example #11
0
def set_vcpus(args):
    options = {
        "mandatory": ["name", "vcpus"],
        "optional": [],
    }
    check_args(args, options)

    virt = Virt(hosts())
    virt.set_vcpus(args["name"], args["vcpus"])
Example #12
0
def set_boot_device(args):
    options = {
        "mandatory": ["name", "dev"],
        "optional": [],
    }
    check_args(args, options)

    virt = Virt(hosts())
    virt.set_boot_device(args["name"], args["dev"])
Example #13
0
File: vm.py Project: hfm/maglica
def set_vcpus(args):
    options = {
        "mandatory": ["name", "vcpus"],
        "optional" : [],
    }
    check_args(args, options)

    virt = Virt(hosts())
    virt.set_vcpus(args["name"], args["vcpus"])
Example #14
0
File: vm.py Project: hfm/maglica
def console(args):
    options = {
        "mandatory": ["name"],
        "optional" : [],
    }
    check_args(args, options)

    virt = Virt(hosts())
    virt.console(args["name"])
Example #15
0
def info(args):
    options = {
        "mandatory": ["name"],
        "optional": [],
    }
    check_args(args, options)

    virt = Virt(hosts())
    return virt.info(args["name"])
Example #16
0
File: vm.py Project: hfm/maglica
def destroy(args):
    options = {
        "mandatory": ["name"],
        "optional" : [],
    }
    check_args(args, options)

    virt = Virt(hosts())
    virt.destroy(args["name"])
Example #17
0
File: vm.py Project: hfm/maglica
def info(args):
    options = {
        "mandatory": ["name"],
        "optional" : [],
    }
    check_args(args, options)

    virt = Virt(hosts())
    return virt.info(args["name"]);
Example #18
0
def attach_iso(args):
    options = {
        "mandatory": ["name", "iso"],
        "optional": [],
    }
    check_args(args, options)

    name = args["name"]
    iso = args["iso"]

    virt = Virt(hosts())
    virt.attach_iso(name, iso)
Example #19
0
File: vm.py Project: hfm/maglica
def attach_iso(args):
    options = {
        "mandatory": ["name", "iso"],
        "optional" : [],
    }
    check_args(args, options)

    name = args["name"]
    iso  = args["iso"]

    virt = Virt(hosts())
    virt.attach_iso(name, iso)
Example #20
0
File: virt.py Project: hfm/maglica
def test_uri_qemu():
    virt = Virt()

    conf = maglica.config.load_from_dict({
        "libvirt": {
            "driver"    : "qemu",
            "transport" : "tcp",
            "path"      : "system",
        }
    })

    virt.conf = conf

    eq_(virt.uri("host0.example.jp"), "qemu+tcp://host0.example.jp/system")
Example #21
0
File: vm.py Project: hfm/maglica
def set_memory(args):
    options = {
        "mandatory": ["name", "size"],
        "optional" : [],
    }
    check_args(args, options)

    name = args["name"]
    size = args["size"]

    if re.match(r'.+G$', size):
        size = int(size[:-1])
        size = size * 1024 * 1024
    elif re.match(r'.+M$', size):
        size = int(size[:-1])
        size = size * 1024
    elif re.match(r'.+K$', size):
        size = int(size[:-1])

    if size > 10 * 1024 * 1024:
        raise Exception("Size is too large.")

    virt = Virt(hosts())
    virt.set_memory(name, size)
Example #22
0
def set_memory(args):
    options = {
        "mandatory": ["name", "size"],
        "optional": [],
    }
    check_args(args, options)

    name = args["name"]
    size = args["size"]

    if re.match(r'.+G$', size):
        size = int(size[:-1])
        size = size * 1024 * 1024
    elif re.match(r'.+M$', size):
        size = int(size[:-1])
        size = size * 1024
    elif re.match(r'.+K$', size):
        size = int(size[:-1])

    if size > 10 * 1024 * 1024:
        raise Exception("Size is too large.")

    virt = Virt(hosts())
    virt.set_memory(name, size)
Example #23
0
def clone(args):
    virt = Virt(hosts())

    options = {
        "mandatory": ["image", "hostname"],
        "optional": ["on", "start", "format"],
    }
    check_args(args, options)

    images = maglica.image.list()
    target_hosts = []

    for image in images:
        if args["image"] == image["name"]:
            target_hosts.append(image["host"])

    if len(target_hosts) < 1:
        raise Exception('Image "%s" is active or not exist.' % args["image"])

    if args.has_key("on"):
        host = args["on"]
    else:
        min = 65535
        for target_host in target_hosts:
            size = len(virt.get_active_domains_of(
                target_host["name"])) * 10 / target_host["weight"]
            if size < min:
                min = size
                host = target_host["name"]

    maglica.dispatcher.dispatch({
        "type": "vm",
        "action": "clone",
        "host": host,
        "args": args,
    })
Example #24
0
File: virt.py Project: hfm/maglica
def test_already_stopped_exception():
    virt = Virt()
    virt.stop("test")
    virt.stop("test")
Example #25
0
File: virt.py Project: hfm/maglica
def test_start_not_exist_exception():
    virt = Virt()
    virt.start("not_exist")
Example #26
0
File: virt.py Project: hfm/maglica
def test_already_started_exception():
    virt = Virt()
    virt.start("test")
Example #27
0
File: virt.py Project: hfm/maglica
def test_start_and_stop():
    virt = Virt()
    eq_(virt.stop("test"), 0)
    eq_(virt.start("test"), 0)
Example #28
0
File: virt.py Project: hfm/maglica
def test_get_active_domain():
    virt = Virt()
    (dom, host) = virt.get_active_domain("test")
    eq_(host, "test")
    eq_(dom.name(), "test")
    ok_(dom.isActive())
Example #29
0
File: virt.py Project: hfm/maglica
def test_set_boot_device():
    virt = Virt()
    ok_(virt.attach_iso("test", "cdrom"))
Example #30
0
File: virt.py Project: hfm/maglica
def test_stop_not_exist_exception():
    virt = Virt()
    virt.stop("not_exist")
Example #31
0
File: virt.py Project: hfm/maglica
def test_uri_default():
    virt = Virt()
    eq_(virt.uri("host0.example.jp"), "remote://host0.example.jp/")
Example #32
0
File: virt.py Project: hfm/maglica
def test_set_memory():
    virt = Virt()
    ok_(virt.set_memory("test", 1024*1024))
Example #33
0
def list():
    virt = Virt(hosts())
    return virt.get_domains()
Example #34
0
File: virt.py Project: hfm/maglica
def test_attach_iso_to_active_domain():
    virt = Virt()
    ok_(virt.attach_iso("test", "test.iso"))
Example #35
0
File: virt.py Project: hfm/maglica
def test_get_active_domains():
    virt = Virt()
    dom = virt.get_active_domains()[0]
    eq_(dom["name"], "test")
    eq_(dom["host"], "test")
    eq_(dom["state"], "running")
Example #36
0
File: virt.py Project: hfm/maglica
def test_set_vcpus():
    virt = Virt()
    ok_(virt.set_vcpus("test", "1"))
Example #37
0
File: vm.py Project: hfm/maglica
def list():
    virt = Virt(hosts())
    return virt.get_domains()