source: trunk/packages/invirt-remote/server/usr/sbin/invirt-remote-create @ 2134

Last change on this file since 2134 was 2134, checked in by iannucci, 16 years ago

Added all the other quotas for great win.

  • Property svn:executable set to *
File size: 2.9 KB
Line 
1#!/usr/bin/python
2
3"""
4Picks a host to "create" (boot) a VM on, and does so.
5
6Current load-balancing algorithm: wherever there's more free RAM.
7
8TODO: use a lock to avoid creating the same VM twice in a race
9"""
10
11from invirt.remote import bcast
12from subprocess import PIPE, Popen, call
13import sys
14import yaml
15import invirt.database
16
17def maxMemory(owner, xmlist):
18    """
19    Return the memory available for a new machine.
20    """
21    machines = invirt.database.Machine.query().filter_by(owner=owner)
22    (quota_total, quota_single) = invirt.database.Owner.getMemoryQuotas(owner)
23
24    active_machines = [m for m in machines if m.name in xmlist]
25    mem_usage = sum([x.memory for x in active_machines])
26    return min(quota_single, quota_total-mem_usage)
27
28def choose_host():
29    # Query each of the hosts.
30    results = bcast('availability')
31    return max((int(o), s) for (s, o) in results)[1]
32
33def main(argv):
34    if len(argv) < 3:
35        print >> sys.stderr, "usage: invirt-remote-create <operation> <machine> [<other args...>]"
36        return 2
37    operation = argv[1]
38    machine_name = argv[2]
39    args = argv[3:]
40   
41    if operation == 'install':
42        options = dict(arg.split('=', 1) for arg in args)
43        valid_keys = set(('mirror', 'dist', 'arch', 'imagesize', 'noinstall'))
44        if not set(options.keys()).issubset(valid_keys):
45            print >> sys.stderr, "Invalid argument. Use the help command to see valid arguments to install"
46            return 1
47        if any(' ' in val for val in options.values()):
48            print >> sys.stderr, "Arguments to the autoinstaller cannot contain spaces"
49            return 1
50
51    p = Popen(['/usr/sbin/invirt-remote-proxy-web', 'listvms'], stdout=PIPE)
52    output = p.communicate()[0]
53    if p.returncode != 0:
54        raise RuntimeError("Command '%s' returned non-zero exit status %d"
55                           % ('invirt-remote-proxy-web', p.returncode)) 
56    vms = yaml.load(output, yaml.CSafeLoader)
57
58    if machine_name in vms:
59        host = vms[machine_name]['host']
60        print >> sys.stderr, ("machine '%s' is already running on host %s"
61                              % (machine_name, host))
62        return 1
63
64    if operation == "create":
65        invirt.database.connect()
66        machine = invirt.database.Machine.query().filter_by(name=machine_name).first()
67
68        owner = machine.owner
69        vm_memory = machine.memory
70
71        max_memory = maxMemory(owner, vms.keys())
72        if vm_memory > max_memory:
73            print >>sys.stderr, "owner %s requested %d MB of memory for vm %s; %d MB allowed" % (owner, vm_memory, machine_name, max_memory)
74            return 1
75
76    host = choose_host()
77    print 'Creating on host %s...' % host
78    sys.stdout.flush()
79    return call(['remctl', host, 'remote', 'control',
80                 machine_name, operation] + args)
81
82if __name__ == '__main__':
83    sys.exit(main(sys.argv))
84
85# vim:et:sw=4:ts=4
Note: See TracBrowser for help on using the repository browser.