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 results = bcast('availability')
19 return max((int(o), s) for (s, o) in results)[1]
23 print >> sys.stderr, "usage: invirt-remote-create <operation> <machine> [<other args...>]"
26 machine_name = argv[2]
29 if operation == 'install':
30 options = dict(arg.split('=', 1) for arg in args)
31 valid_keys = set(('mirror', 'dist', 'arch', 'imagesize', 'noinstall'))
32 if not set(options.keys()).issubset(valid_keys):
33 print >> sys.stderr, "Invalid argument. Use the help command to see valid arguments to install"
35 if any(' ' in val for val in options.values()):
36 print >> sys.stderr, "Arguments to the autoinstaller cannot contain spaces"
39 p = Popen(['/usr/sbin/invirt-remote-proxy-web', 'listvms'], stdout=PIPE)
40 output = p.communicate()[0]
42 raise RuntimeError("Command '%s' returned non-zero exit status %d"
43 % ('invirt-remote-proxy-web', p.returncode))
44 vms = yaml.load(output, yaml.CSafeLoader)
46 if machine_name in vms:
47 host = vms[machine_name]['host']
48 print >> sys.stderr, ("machine '%s' is already running on host %s"
49 % (machine_name, host))
53 print 'Creating on host %s...' % host
55 return call(['remctl', host, 'remote', 'control',
56 machine_name, operation] + args)
58 if __name__ == '__main__':
59 sys.exit(main(sys.argv))