6 from dha import DeploymentHardwareAdapter
8 exec_cmd = common.exec_cmd
11 check_dir_exists = common.check_dir_exists
12 check_file_exists = common.check_file_exists
13 check_if_root = common.check_if_root
16 iface vfuelnet inet static
20 pre-down iptables -t nat -D POSTROUTING --out-interface p1p1.20 -j MASQUERADE -m comment --comment "vfuelnet"
21 pre-down iptables -D FORWARD --in-interface vfuelnet --out-interface p1p1.20 -m comment --comment "vfuelnet"
22 post-up iptables -t nat -A POSTROUTING --out-interface p1p1.20 -j MASQUERADE -m comment --comment "vfuelnet"
23 post-up iptables -A FORWARD --in-interface vfuelnet --out-interface p1p1.20 -m comment --comment "vfuelnet"
25 VM_DIR = 'baremetal/vm'
26 FUEL_DISK_SIZE = '30G'
28 INTERFACE_CONFIG = '/etc/network/interfaces'
32 def __init__(self, storage_dir, dha_file):
33 self.dha = DeploymentHardwareAdapter(dha_file)
34 self.storage_dir = storage_dir
35 self.parser = etree.XMLParser(remove_blank_text=True)
36 self.fuel_node_id = self.dha.get_fuel_node_id()
37 self.file_dir = os.path.dirname(os.path.realpath(__file__))
38 self.vm_dir = '%s/%s' % (self.file_dir, VM_DIR)
40 def setup_environment(self):
42 check_dir_exists(self.vm_dir)
43 self.setup_networking()
47 def setup_networking(self):
48 with open(INTERFACE_CONFIG) as f:
50 if VFUELNET not in data:
51 log('Appending to file %s:\n %s' % (INTERFACE_CONFIG, VFUELNET))
52 with open(INTERFACE_CONFIG, 'a') as f:
53 f.write('\n%s\n' % VFUELNET)
54 if exec_cmd('ip link show | grep %s' % IFACE):
55 log('Bring DOWN interface %s' % IFACE)
56 exec_cmd('ifdown %s' % IFACE, False)
57 log('Bring UP interface %s' % IFACE)
58 exec_cmd('ifup %s' % IFACE, False)
61 vm_name = self.dha.get_node_property(self.fuel_node_id, 'libvirtName')
62 r, c = exec_cmd('virsh dumpxml %s' % vm_name, False)
66 self.undefine_vm_delete_disk(r, vm_name)
68 def undefine_vm_delete_disk(self, printout, vm_name):
70 xml_dump = etree.fromstring(printout, self.parser)
71 disks = xml_dump.xpath('/domain/devices/disk')
73 sources = disk.xpath('source')
74 for source in sources:
75 source_file = source.get('file')
77 disk_files.append(source_file)
78 log('Deleting VM %s with disks %s' % (vm_name, disk_files))
79 exec_cmd('virsh destroy %s' % vm_name, False)
80 exec_cmd('virsh undefine %s' % vm_name, False)
81 for file in disk_files:
82 exec_cmd('rm -f %s' % file)
85 temp_dir = exec_cmd('mktemp -d')
86 vm_name = self.dha.get_node_property(self.fuel_node_id, 'libvirtName')
87 vm_template = self.dha.get_node_property(self.fuel_node_id,
89 disk_path = '%s/%s.raw' % (self.storage_dir, vm_name)
90 exec_cmd('fallocate -l %s %s' % (FUEL_DISK_SIZE, disk_path))
91 self.define_vm(vm_name, vm_template, temp_dir, disk_path)
92 exec_cmd('rm -fr %s' % temp_dir)
94 def define_vm(self, vm_name, vm_template, temp_dir, disk_path):
95 log('Creating VM %s with disks %s' % (vm_name, disk_path))
96 temp_vm_file = '%s/%s' % (temp_dir, vm_name)
97 exec_cmd('cp %s/%s %s' % (self.vm_dir, vm_template, temp_vm_file))
98 with open(temp_vm_file) as f:
99 vm_xml = etree.parse(f)
100 names = vm_xml.xpath('/domain/name')
103 uuids = vm_xml.xpath('/domain/uuid')
105 uuid.getparent().remove(uuid)
106 disks = vm_xml.xpath('/domain/devices/disk')
108 sources = disk.xpath('source')
109 for source in sources:
110 source.set('file', disk_path)
111 with open(temp_vm_file, 'w') as f:
112 vm_xml.write(f, pretty_print=True, xml_declaration=True)
113 exec_cmd('virsh define %s' % temp_vm_file)
119 python setup_vfuel.py <storage_directory> <dha_file>
122 python setup_vfuel.py /mnt/images dha.yaml
125 def parse_arguments():
126 if len(sys.argv) != 3:
127 log('Incorrect number of arguments')
130 storage_dir = sys.argv[-2]
131 dha_file = sys.argv[-1]
132 check_dir_exists(storage_dir)
133 check_file_exists(dha_file)
134 return storage_dir, dha_file
137 storage_dir, dha_file = parse_arguments()
139 vfuel = VFuel(storage_dir, dha_file)
140 vfuel.setup_environment()
142 if __name__ == '__main__':