Configures static ip on private and admin interfaces for nodes
[genesis.git] / common / puppet-opnfv / manifests / external_net_presetup.pp
1 #Copyright 2015 Open Platform for NFV Project, Inc. and its contributors
2 #
3 #   Licensed under the Apache License, Version 2.0 (the "License");
4 #   you may not use this file except in compliance with the License.
5 #   You may obtain a copy of the License at
6 #
7 #       http://www.apache.org/licenses/LICENSE-2.0
8 #
9 #   Unless required by applicable law or agreed to in writing, software
10 #   distributed under the License is distributed on an "AS IS" BASIS,
11 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 #   See the License for the specific language governing permissions and
13 #   limitations under the License.
14
15 class opnfv::external_net_presetup {
16
17   if $public_gateway == '' { fail('public_gateway is empty') }
18   if $public_dns == '' { fail('public_dns is empty') }
19   if $public_network == '' { fail('public_network is empty') }
20   if $public_subnet == '' { fail('public_subnet is empty') }
21   if $public_allocation_start == '' { fail('public_allocation_start is empty') }
22   if $public_allocation_end == '' { fail('public_allocation_end is empty') }
23   if !$controllers_hostnames_array { fail('controllers_hostnames_array is empty') }
24   $controllers_hostnames_array_str = $controllers_hostnames_array
25   $controllers_hostnames_array = split($controllers_hostnames_array, ',')
26
27   if ($admin_network != '') and ($admin_network != 'false') {
28     $admin_nic = get_nic_from_network("$admin_network")
29     if $admin_nic == '' { fail('admin_nic was not found') }
30     $admin_ip = get_ip_from_nic("admin_nic")
31     $admin_netmask = get_netmask_from_nic("$admin_nic")
32     #Modify ifcfg Admin network
33     augeas { "main-$admin_nic":
34         context => "/files/etc/sysconfig/network-scripts/ifcfg-$public_nic",
35         changes => [
36                 "set IPADDR $admin_ip",
37                 "set NETMASK $admin_netmask",
38                 "rm GATEWAY",
39                 "rm DNS1",
40                 "set DEFROUTE no",
41                 "rm IPV6_DEFROUTE",
42                 "rm IPV6_PEERDNS",
43                 "rm IPV6_PEERROUTES",
44                 "rm PEERROUTES",
45                 "set PEERDNS no",
46                 "set BOOTPROTO static",
47                 "set IPV6INIT no",
48                 "set IPV6_AUTOCONF no",
49                 "set ONBOOT yes",
50
51         ],
52         before  => Exec['systemctl restart network'],
53     }
54
55   }
56
57   if ($private_network != '') and ($private_network != 'false') {
58     $private_nic = get_nic_from_network("$private_network")
59     if $private_nic == '' { fail('private_nic was not found') }
60     $private_ip = get_ip_from_nic("private_nic")
61     $private_netmask = get_netmask_from_nic("$private_nic")
62     #Modify ifcfg private network
63     augeas { "main-$private_nic":
64         context => "/files/etc/sysconfig/network-scripts/ifcfg-$private_nic",
65         changes => [
66                 "set IPADDR $private_ip",
67                 "set NETMASK $private_netmask",
68                 "rm GATEWAY",
69                 "rm DNS1",
70                 "set DEFROUTE no",
71                 "rm IPV6_DEFROUTE",
72                 "rm IPV6_PEERDNS",
73                 "rm IPV6_PEERROUTES",
74                 "rm PEERROUTES",
75                 "set PEERDNS no",
76                 "set BOOTPROTO static",
77                 "set IPV6INIT no",
78                 "set IPV6_AUTOCONF no",
79                 "set ONBOOT yes",
80
81         ],
82         before  => Exec['systemctl restart network'],
83     }
84
85   }
86
87   #find public NIC
88   $public_nic = get_nic_from_network("$public_network")
89   $public_nic_ip = get_ip_from_nic("$public_nic")
90   $public_nic_netmask = get_netmask_from_nic("$public_nic")
91
92  if ($public_nic == '') or ($public_nic_ip == '') or ($public_nic == "br-ex") or ($public_nic == "br_ex") {
93   notify {"Skipping augeas, public_nic ${public_nic}, public_nic_ip ${public_nic_ip}":}
94
95   exec {'ovs-vsctl -t 10 -- --may-exist add-br br-ex':
96        path         => ["/usr/sbin/", "/usr/bin/"],
97        unless       => 'ip addr show br-ex | grep "inet "',
98        before       => Exec['restart-network-public-nic-ip'],
99   }
100   ~>
101   exec {'systemctl restart network':
102        path         => ["/usr/sbin/", "/usr/bin/"],
103        refreshonly  => 'true',
104   }
105
106   exec {'restart-network-public-nic-ip':
107        command      => 'systemctl restart network',
108        path         => ["/usr/sbin/", "/usr/bin/"],
109        onlyif       => 'ip addr show | grep $(ip addr show br-ex | grep -Eo "inet [\.0-9]+" | cut -d " " -f2) | grep -v br-ex',
110   }
111
112  } else {
113   #reconfigure public interface to be ovsport
114   augeas { "main-$public_nic":
115         context => "/files/etc/sysconfig/network-scripts/ifcfg-$public_nic",
116         changes => [
117                 "rm IPADDR",
118                 "rm NETMASK",
119                 "rm GATEWAY",
120                 "rm DNS1",
121                 "rm BOOTPROTO",
122                 "rm DEFROUTE",
123                 "rm IPV6_DEFROUTE",
124                 "rm IPV6_PEERDNS",
125                 "rm IPV6_PEERROUTES",
126                 "rm PEERROUTES",
127                 "set PEERDNS no",
128                 "set BOOTPROTO static",
129                 "set IPV6INIT no",
130                 "set IPV6_AUTOCONF no",
131                 "set ONBOOT yes",
132                 "set TYPE OVSPort",
133                 "set OVS_BRIDGE br-ex",
134                 "set PROMISC yes"
135
136         ],
137         before  => Class["quickstack::pacemaker::params"],
138         require => Service["openvswitch"],
139   }
140
141   ->
142   #create br-ex interface
143   file { 'external_bridge':
144         path => '/etc/sysconfig/network-scripts/ifcfg-br-ex',
145         owner   => 'root',
146         group   => 'root',
147         mode    => '0644',
148         content => template('opnfv/br_ex.erb'),
149         before  => Class["quickstack::pacemaker::params"],
150   }
151   ->
152   exec {'ovs-vsctl -t 10 -- --may-exist add-br br-ex':
153        path         => ["/usr/sbin/", "/usr/bin/"],
154   }
155   ~>
156   exec {'systemctl restart network':
157        path         => ["/usr/sbin/", "/usr/bin/"],
158        refreshonly  => 'true',
159   }
160
161  }
162 }