source: trunk/packages/sipb-xen-remote-server/files/usr/sbin/sipb-xen-remote-create @ 737

Last change on this file since 737 was 665, checked in by price, 16 years ago

remote-create: document load-balancing algorithm

We take the host with more RAM free.

  • Property svn:executable set to *
File size: 2.1 KB
Line 
1#!/usr/bin/python
2"""
3Picks a host to "create" (boot) a VM on, and does so.
4
5Current load-balancing algorithm: wherever there's more free RAM.
6
7TODO: use a lock to avoid creating the same VM twice in a race
8"""
9
10
11from subprocess import PIPE, Popen, call
12import sys
13import yaml
14
15
16def choose_host():
17    # Query each of the hosts.
18    # TODO get `servers` from a real list of all the VM hosts (instead of
19    # hardcoding the list here)
20    servers = ['black-mesa.mit.edu', 'sx-blade-2.mit.edu']
21    pipes = [(server,
22              Popen(['remctl', server, 'remote', 'web', 'info'], stdout=PIPE))
23             for server in servers]
24    outputs = [(s, p.communicate()[0]) for (s, p) in pipes]
25    for (s, p) in pipes:
26        if p.returncode != 0:
27            raise RuntimeError("remctl to host %s returned non-zero exit status %d"
28                               % (s, p.returncode)) 
29    results = [(s, yaml.load(o, yaml.CSafeLoader)) for (s, o) in outputs]
30    # XXX will the output of 'xm info' always be parseable YAML?
31
32    return max( (int(o['free_memory']), s) for (s, o) in results )[1]
33
34
35def main(argv):
36    if len(argv) < 2:
37        print >>sys.stderr, "usage: sipb-xen-remote-create <machine> [<other args...>]"
38        return 2
39    machine_name = argv[1]
40    args = argv[2:]
41
42
43    p = Popen(['/usr/sbin/sipb-xen-remote-proxy-web', 'listvms'], stdout=PIPE)
44    output = p.communicate()[0]
45    if p.returncode != 0:
46        raise RuntimeError("Command '%s' returned non-zero exit status %d"
47                           % ('sipb-xen-remote-proxy-web', p.returncode)) 
48    vms = yaml.load(output, yaml.CSafeLoader)
49
50    if machine_name in vms:
51        host = vms[machine_name]['host']
52        print >>sys.stderr, ("machine '%s' is already running on host %s"
53                             % (machine_name, host))
54        return 1
55
56
57    host = choose_host()
58    print 'Creating on host %s...' % host
59    sys.stdout.flush()
60    return call(['remctl', host, 'remote', 'control',
61                 machine_name, 'create'] + args)
62
63if __name__ == '__main__':
64    sys.exit(main(sys.argv))
65
66# vim:et:sw=4:ts=4
Note: See TracBrowser for help on using the repository browser.