Merge "transplant.py: Allow SSH on all interfaces."
[armband.git] / patches / opnfv-fuel / 0005-transplant-Generate-extra-interfaces-config-file.patch
1 From: Josep Puigdemont <josep.puigdemont@enea.com>
2 Date: Wed, 4 May 2016 17:58:56 +0200
3 Subject: [PATCH] transplant: Generate extra interfaces config file
4
5 The DEA override may contain a IFCGF_<interface> section in its 'fuel:'
6 section, containing the necessary keys to produce a ifcfg-<interface>
7 file, like in this example:
8
9 fuel:
10    IFCFG_ETH1:
11      device: eth1
12      ipaddress: 10.0.1.10
13      netmask: 255.255.255.0
14      gateway: 10.0.1.254
15
16 FIXME: In order for Network Manager to use the newly added interfaces
17 for outgoing traffic and honor their GATEWAY setting (e.g. if we just
18 added one public interface), the default route on admin iface (most of
19 the time called eth0) should be disabled. For now, we assume the admin
20 interface is always "eth0".
21
22 Change-Id: I0457dc9a0d49e46b8ca85cfe7a4435c2b15f5238
23 Signed-off-by: Josep Puigdemont <josep.puigdemont@enea.com>
24 Signed-off-by: Alexandu Avadanii <alexandru.avadanii@enea.com>
25 ---
26  deploy/transplant_fuel_settings.py | 37 +++++++++++++++++++++++++++++++++++++
27  1 file changed, 37 insertions(+)
28
29 diff --git a/deploy/transplant_fuel_settings.py b/deploy/transplant_fuel_settings.py
30 index e57a4fb..9a65cf6 100644
31 --- a/deploy/transplant_fuel_settings.py
32 +++ b/deploy/transplant_fuel_settings.py
33 @@ -11,10 +11,14 @@
34  import sys
35  import io
36  import yaml
37 +import re
38 +import os
39  from dea import DeploymentEnvironmentAdapter
40  
41  from common import (
42      check_file_exists,
43 +    exec_cmd,
44 +    log,
45  )
46  
47  ASTUTE_YAML = '/etc/fuel/astute.yaml'
48 @@ -35,15 +39,47 @@ def parse_arguments():
49      check_file_exists(dea_file)
50      return dea_file
51  
52 +def write_ifcfg_file(key, fuel_conf):
53 +    config = ('BOOTPROTO=none\n'
54 +              'ONBOOT=yes\n'
55 +              'TYPE=Ethernet\n'
56 +              'NM_CONTROLLED=yes\n')
57 +    for skey in ('ipaddress', 'device', 'netmask', 'gateway'):
58 +        if not fuel_conf[key].get(skey):
59 +            log('Warning: missing key %s for %s' % (skey, key))
60 +            config += '%s=\n' % skey.upper()
61 +        elif skey == 'ipaddress':
62 +            config += 'IPADDR=%s\n' % fuel_conf[key][skey]
63 +        else:
64 +            config += '%s=%s\n' % (skey.upper(), fuel_conf[key][skey])
65 +
66 +    fname = os.path.join('/etc/sysconfig/network-scripts/',
67 +                         key.lower().replace('_','-'))
68 +    with open(fname, 'wc') as f:
69 +        f.write(config)
70  
71  def transplant(dea, astute):
72      fuel_conf = dea.get_fuel_config()
73 +    require_network_restart = False
74      for key in fuel_conf.iterkeys():
75          if key == 'ADMIN_NETWORK':
76              for skey in fuel_conf[key].iterkeys():
77                  astute[key][skey] = fuel_conf[key][skey]
78 +        elif re.match('^IFCFG', key):
79 +            log('Adding interface configuration for: %s' % key.lower())
80 +            require_network_restart = True
81 +            write_ifcfg_file(key, fuel_conf)
82 +            if astute.has_key(key):
83 +                astute.pop(key, None)
84          else:
85              astute[key] = fuel_conf[key]
86 +    if require_network_restart:
87 +        admin_ifcfg = '/etc/sysconfig/network-scripts/ifcfg-eth0'
88 +        exec_cmd('echo "DEFROUTE=no" >> %s' % admin_ifcfg)
89 +        log('At least one interface was reconfigured, restart network manager')
90 +        exec_cmd('systemctl restart network')
91 +        log('At least one interface was reconfigured, accept SSH on all')
92 +        exec_cmd('iptables -A INPUT -p tcp --dport ssh -j ACCEPT')
93      return astute
94  
95  
96 @@ -51,11 +85,14 @@ def main():
97      dea_file = parse_arguments()
98      check_file_exists(ASTUTE_YAML)
99      dea = DeploymentEnvironmentAdapter(dea_file)
100 +    log('Reading astute file %s' % ASTUTE_YAML)
101      with io.open(ASTUTE_YAML) as stream:
102          astute = yaml.load(stream)
103 +    log('Initiating transplant')
104      transplant(dea, astute)
105      with io.open(ASTUTE_YAML, 'w') as stream:
106          yaml.dump(astute, stream, default_flow_style=False)
107 +    log('Transplant done')
108  
109  
110  if __name__ == '__main__':