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

Last change on this file since 1650 was 1650, checked in by broder, 15 years ago

Choose a host based on the maximum possible free memory, not the
unallocated memory

This works around the case where dom0's RAM doesn't get ballooned down

  • Property svn:executable set to *
File size: 2.0 KB
RevLine 
[658]1#!/usr/bin/python
[816]2
[658]3"""
4Picks a host to "create" (boot) a VM on, and does so.
5
[665]6Current load-balancing algorithm: wherever there's more free RAM.
[658]7
8TODO: use a lock to avoid creating the same VM twice in a race
9"""
10
[816]11from invirt.remote import bcast
[658]12from subprocess import PIPE, Popen, call
13import sys
14import yaml
15
16def choose_host():
17    # Query each of the hosts.
18    # XXX will the output of 'xm info' always be parseable YAML?
[816]19    results = bcast('info')
[1650]20    return max((int(o['max_free_memory']), s) for (s, o) in results)[1]
[658]21
22def main(argv):
[1088]23    if len(argv) < 3:
[1176]24        print >> sys.stderr, "usage: invirt-remote-create <operation> <machine> [<other args...>]"
[658]25        return 2
[1088]26    operation = argv[1]
27    machine_name = argv[2]
28    args = argv[3:]
29   
30    if operation == 'install':
31        options = dict(arg.split('=', 1) for arg in args)
32        valid_keys = set(('mirror', 'dist', 'arch', 'imagesize', 'noinstall'))
33        if not set(options.keys()).issubset(valid_keys):
34            print >> sys.stderr, "Invalid argument. Use the help command to see valid arguments to install"
35            return 1
36        if any(' ' in val for val in options.values()):
37            print >> sys.stderr, "Arguments to the autoinstaller cannot contain spaces"
38            return 1
[658]39
[1176]40    p = Popen(['/usr/sbin/invirt-remote-proxy-web', 'listvms'], stdout=PIPE)
[658]41    output = p.communicate()[0]
42    if p.returncode != 0:
43        raise RuntimeError("Command '%s' returned non-zero exit status %d"
[1176]44                           % ('invirt-remote-proxy-web', p.returncode)) 
[658]45    vms = yaml.load(output, yaml.CSafeLoader)
46
47    if machine_name in vms:
48        host = vms[machine_name]['host']
[816]49        print >> sys.stderr, ("machine '%s' is already running on host %s"
50                              % (machine_name, host))
[658]51        return 1
52
53    host = choose_host()
54    print 'Creating on host %s...' % host
[664]55    sys.stdout.flush()
[658]56    return call(['remctl', host, 'remote', 'control',
[1088]57                 machine_name, operation] + args)
[658]58
59if __name__ == '__main__':
60    sys.exit(main(sys.argv))
61
62# vim:et:sw=4:ts=4
Note: See TracBrowser for help on using the repository browser.