4 Picks a host to "create" (boot) a VM on, and does so.
6 Current load-balancing algorithm: wherever there's more free RAM.
8 TODO: use a lock to avoid creating the same VM twice in a race
11 from invirt.remote import bcast
12 from subprocess import PIPE, Popen, call
17 # Query each of the hosts.
18 # XXX will the output of 'xm info' always be parseable YAML?
19 results = bcast('info')
20 return max((int(o['free_memory']), s) for (s, o) in results)[1]
24 print >> sys.stderr, "usage: invirt-remote-create <operation> <machine> [<other args...>]"
27 machine_name = argv[2]
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"
36 if any(' ' in val for val in options.values()):
37 print >> sys.stderr, "Arguments to the autoinstaller cannot contain spaces"
40 p = Popen(['/usr/sbin/invirt-remote-proxy-web', 'listvms'], stdout=PIPE)
41 output = p.communicate()[0]
43 raise RuntimeError("Command '%s' returned non-zero exit status %d"
44 % ('invirt-remote-proxy-web', p.returncode))
45 vms = yaml.load(output, yaml.CSafeLoader)
47 if machine_name in vms:
48 host = vms[machine_name]['host']
49 print >> sys.stderr, ("machine '%s' is already running on host %s"
50 % (machine_name, host))
54 print 'Creating on host %s...' % host
56 return call(['remctl', host, 'remote', 'control',
57 machine_name, operation] + args)
59 if __name__ == '__main__':
60 sys.exit(main(sys.argv))