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