Fix 2 for moving ips to be static
[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     if !$admin_ip { fail("admin_ip was not found $admin_nic") }
33     if !$admin_netmask { fail("admin_netmask was not found on $admin_nic") }
34
35     #Modify ifcfg Admin network
36     augeas { "main-$admin_nic":
37         context => "/files/etc/sysconfig/network-scripts/ifcfg-$admin_nic",
38         changes => [
39                 "set IPADDR $admin_ip",
40                 "set NETMASK $admin_netmask",
41                 "rm GATEWAY",
42                 "rm DNS1",
43                 "set DEFROUTE no",
44                 "rm IPV6_DEFROUTE",
45                 "rm IPV6_PEERDNS",
46                 "rm IPV6_PEERROUTES",
47                 "rm PEERROUTES",
48                 "set PEERDNS no",
49                 "set BOOTPROTO static",
50                 "set IPV6INIT no",
51                 "set IPV6_AUTOCONF no",
52                 "set ONBOOT yes",
53
54         ],
55         notify  => Exec['systemctl restart network'],
56     }
57
58   }
59
60   if ($private_network != '') and ($private_network != 'false') {
61     $private_nic = get_nic_from_network("$private_network")
62     if $private_nic == '' { fail('private_nic was not found') }
63     notify {"Private nic $private_nic":}
64     $private_ip = get_ip_from_nic("$private_nic")
65     $private_netmask = get_netmask_from_nic("$private_nic")
66     if !$private_ip { fail("private_ip was not found on $private_nic") }
67     if !$private_netmask { fail("private_netmask was not found on $private_nic") }
68
69     #Modify ifcfg private network
70     augeas { "main-$private_nic":
71         context => "/files/etc/sysconfig/network-scripts/ifcfg-$private_nic",
72         changes => [
73                 "set IPADDR $private_ip",
74                 "set NETMASK $private_netmask",
75                 "rm GATEWAY",
76                 "rm DNS1",
77                 "set DEFROUTE no",
78                 "rm IPV6_DEFROUTE",
79                 "rm IPV6_PEERDNS",
80                 "rm IPV6_PEERROUTES",
81                 "rm PEERROUTES",
82                 "set PEERDNS no",
83                 "set BOOTPROTO static",
84                 "set IPV6INIT no",
85                 "set IPV6_AUTOCONF no",
86                 "set ONBOOT yes",
87
88         ],
89         notify  => Exec['systemctl restart network'],
90     }
91
92   }
93
94   #find public NIC
95   $public_nic = get_nic_from_network("$public_network")
96   $public_nic_ip = get_ip_from_nic("$public_nic")
97   $public_nic_netmask = get_netmask_from_nic("$public_nic")
98
99  if ($public_nic == '') or ($public_nic_ip == '') or ($public_nic == "br-ex") or ($public_nic == "br_ex") {
100   notify {"Skipping augeas, public_nic ${public_nic}, public_nic_ip ${public_nic_ip}":}
101
102   exec {'ovs-vsctl -t 10 -- --may-exist add-br br-ex':
103        path         => ["/usr/sbin/", "/usr/bin/"],
104        unless       => 'ip addr show br-ex | grep "inet "',
105        before       => Exec['restart-network-public-nic-ip'],
106   }
107   ~>
108   exec {'systemctl restart network':
109        path         => ["/usr/sbin/", "/usr/bin/"],
110        refreshonly  => 'true',
111   }
112
113   exec {'restart-network-public-nic-ip':
114        command      => 'systemctl restart network',
115        path         => ["/usr/sbin/", "/usr/bin/"],
116        onlyif       => 'ip addr show | grep $(ip addr show br-ex | grep -Eo "inet [\.0-9]+" | cut -d " " -f2) | grep -v br-ex',
117   }
118
119  } else {
120   #reconfigure public interface to be ovsport
121   augeas { "main-$public_nic":
122         context => "/files/etc/sysconfig/network-scripts/ifcfg-$public_nic",
123         changes => [
124                 "rm IPADDR",
125                 "rm NETMASK",
126                 "rm GATEWAY",
127                 "rm DNS1",
128                 "rm BOOTPROTO",
129                 "rm DEFROUTE",
130                 "rm IPV6_DEFROUTE",
131                 "rm IPV6_PEERDNS",
132                 "rm IPV6_PEERROUTES",
133                 "rm PEERROUTES",
134                 "set PEERDNS no",
135                 "set BOOTPROTO static",
136                 "set IPV6INIT no",
137                 "set IPV6_AUTOCONF no",
138                 "set ONBOOT yes",
139                 "set TYPE OVSPort",
140                 "set OVS_BRIDGE br-ex",
141                 "set PROMISC yes"
142
143         ],
144         before  => Class["quickstack::pacemaker::params"],
145         require => Service["openvswitch"],
146   }
147
148   ->
149   #create br-ex interface
150   file { 'external_bridge':
151         path => '/etc/sysconfig/network-scripts/ifcfg-br-ex',
152         owner   => 'root',
153         group   => 'root',
154         mode    => '0644',
155         content => template('opnfv/br_ex.erb'),
156         before  => Class["quickstack::pacemaker::params"],
157   }
158   ->
159   exec {'ovs-vsctl -t 10 -- --may-exist add-br br-ex':
160        path         => ["/usr/sbin/", "/usr/bin/"],
161   }
162   ~>
163   exec {'systemctl restart network':
164        path         => ["/usr/sbin/", "/usr/bin/"],
165        refreshonly  => 'true',
166   }
167
168  }
169 }