Skip to content

Instantly share code, notes, and snippets.

@wolsen
Created June 11, 2015 20:30
Show Gist options
  • Save wolsen/2091117ae87e157e6f7a to your computer and use it in GitHub Desktop.
Save wolsen/2091117ae87e157e6f7a to your computer and use it in GitHub Desktop.
generate-vms-quickly
#!/usr/bin/env python
import six
import subprocess
import types
import yaml
PARAM_MAPPING = {
'name': '--name',
'arch': '--arch',
'architecture': '--arch',
'uri': '--connect',
'networks': '--network',
'interfaces': '--network',
'disks': '--disk',
'boot': '--boot',
'memory': '--memory',
'mem': '--memory',
'graphics': '--graphics',
}
IGNORE_KEYS = ['count', 'maas', 'type']
def generate_command(node_def):
if not node_def.get('type', None) == 'virtual':
print("Not a virtual node, skipping")
return
cmd = ['virt-install']
count = node_def.get('count', 1)
for k, v in six.iteritems(node_def):
if k in IGNORE_KEYS:
continue
if v is None:
continue
parameter = PARAM_MAPPING.get(k, '--%s' % k)
if parameter is None:
continue
if isinstance(v, types.StringTypes):
cmd = cmd + [parameter, v]
elif not hasattr(v, '__iter__'):
cmd = cmd + [parameter, repr(v)]
else:
for value in v:
cmd = cmd + [parameter, value]
cmd = cmd + ['--noautoconsole']
return cmd
def create_node(node_def):
name = node_def.get('name')
if not node_def.get('type', None) == 'virtual':
print("Node %s is not a virtual node." % name)
return
cmd = generate_command(node_def)
cmd.extend(['--print-xml'])
shell_cmd = ' '.join(cmd)
xml_file = ('/tmp/%s.xml' % name)
try:
shell_cmd = ("%(cmd)s > %(file)s" %
{'cmd': ' '.join(cmd), 'file': xml_file})
print(shell_cmd)
subprocess.check_call(shell_cmd, shell=True)
# Now that the XML has been dumped, need to import it into libvirt
# using the virsh define command.
subprocess.check_call(['virsh', 'define', '--file', xml_file])
except Exception as e:
print("Error defining domain: %s" % e.message)
raise
if __name__ == '__main__':
print("Loading test.yaml ...")
d = yaml.safe_load(open('test.yaml', 'r'))
for node in d.get('nodes', []):
# print("Generating data for node: %s" % str(node))
create_node(node)
# Define some standard options which should be included in all
# of the virtual node definitions. Each specific node can override
# any of the specific options needed.
controller_nodes: &mgmt
type: virtual
networks: ['network=virtual-maas-net,model=virtio']
disks: ['pool=default,size=10']
ram: 1024
boot: network,hd,menu=off
graphics: vnc
nodes:
- name: controller
<<: *mgmt
- name: keystone
<<: *mgmt
- name: glance
<<: *mgmt
- name: neutron
<<: *mgmt
- name: dashboard
<<: *mgmt
- name: cinder
<<: *mgmt
- name: ceph-01
<<: *mgmt
disks: ['pool=default,size=10', 'pool=default,size=10']
- name: ceph-02
<<: *mgmt
disks: ['pool=default,size=10', 'pool=default,size=10']
- name: ceph-03
<<: *mgmt
disks: ['pool=default,size=10', 'pool=default,size=10']
- name: ceph-radosgw
<<: *mgmt
- name: rabbit
<<: *mgmt
- name: mysql
<<: *mgmt
- name: compute-01
<<: *mgmt
- name: compute-02
<<: *mgmt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment