[baremetal] Bring in opendaylight scenario
[fuel.git] / deploy / reap.py
index 7d99631..be72918 100644 (file)
@@ -16,6 +16,9 @@ import yaml
 import glob
 import shutil
 import tempfile
+import re
+import netaddr
+import templater
 
 from common import (
     N,
@@ -77,8 +80,6 @@ DHA_2 = '''
 # which may not be correct - please adjust as needed.
 '''
 
-TEMPLATER = 'templater.py'
-
 DISKS = {'fuel': '100G',
          'controller': '100G',
          'compute': '100G'}
@@ -166,17 +167,18 @@ class Reap(object):
     def reap_nodes_interfaces_transformations(self):
         node_list = parse(exec_cmd('fuel node'))
         real_node_ids = [node[N['id']] for node in node_list]
+        real_node_ids = map(int, real_node_ids)
         real_node_ids.sort()
-        min_node = real_node_ids[0]
+        min_node = min(real_node_ids)
         interfaces = {}
         transformations = {}
         dea_nodes = []
         dha_nodes = []
 
         for real_node_id in real_node_ids:
-            node_id = int(real_node_id) - int(min_node) + 1
+            node_id = real_node_id - min_node + 1
             self.last_node = node_id
-            node = self.get_node_by_id(node_list, real_node_id)
+            node = self.get_node_by_id(node_list, str(real_node_id))
             roles = commafy(node[N['roles']])
             if not roles:
                 err('Fuel Node %s has no role' % real_node_id)
@@ -253,6 +255,40 @@ class Reap(object):
             if key not in ['ipaddress', 'netmask',
                            'dhcp_pool_start', 'dhcp_pool_end', 'ssh_network']:
                 del fuel['ADMIN_NETWORK'][key]
+
+        ## FIXME(armband): Factor in support for adding public/other interfaces.
+        ## TODO: Following block expects interface name(s) to be lowercase only
+        interfaces_list = exec_cmd('ip -o -4 a | grep -e "e[nt][hopsx].*"')
+        for interface in re.split('\n', interfaces_list):
+            # Sample output line from above cmd:
+            # 3: eth1 inet 10.0.2.10/24 scope global eth1 valid_lft forever ...
+            ifcfg = re.split(r'\s+', interface)
+            ifcfg_name = ifcfg[1]
+            ifcfg_ipaddr = ifcfg[3]
+
+            # Filter out admin interface (device name is not known, match IP)
+            current_network = netaddr.IPNetwork(ifcfg_ipaddr)
+            if str(current_network.ip) == fuel['ADMIN_NETWORK']['ipaddress']:
+                continue
+
+            # Read ifcfg-* network interface config file, write IFCFG_<IFNAME>
+            ifcfg_sec = 'IFCFG_%s' % ifcfg_name.upper()
+            fuel[ifcfg_sec] = {}
+            ifcfg_data = {}
+            ifcfg_f = ('/etc/sysconfig/network-scripts/ifcfg-%s' % ifcfg_name)
+            with open(ifcfg_f) as f:
+                for line in f:
+                    if line.startswith('#'):
+                        continue
+                    (key, val) = line.split('=')
+                    ifcfg_data[key.lower()] = val.rstrip()
+
+            # Keep only needed info (e.g. filter-out type=Ethernet).
+            fuel[ifcfg_sec]['ipaddress'] = ifcfg_data['ipaddr']
+            fuel[ifcfg_sec]['device'] = ifcfg_data['device']
+            fuel[ifcfg_sec]['netmask'] = str(current_network.netmask)
+            fuel[ifcfg_sec]['gateway'] = ifcfg_data['gateway']
+
         self.write_yaml(self.dea_file, {'fuel': fuel})
 
     def reap_network_settings(self):
@@ -317,8 +353,10 @@ class Reap(object):
         self.download_config('network')
 
     def create_base_dea(self):
-        exec_cmd('python %s %s %s %s'
-                 % (TEMPLATER, self.dea_file, self.template, self.base_dea))
+        templater = templater.Templater(self.dea_file,
+                                        self.template,
+                                        self.base_dea)
+        templater.run()
 
     def finale(self):
         log('DEA file is available at %s' % self.dea_file)