| garciadeblas | 93c6131 | 2016-09-28 15:12:48 +0200 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # Copyright 2016 Telefónica Investigación y Desarrollo S.A.U. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | ############ |
| 17 | # Functions |
| 18 | ############ |
| 19 | usage(){ |
| 20 | echo -e "usage: $0 [OPTIONS]" |
| 21 | echo -e "Install NAT rules for OSM" |
| 22 | echo -e " OPTIONS" |
| 23 | echo -e " -u: UI/SO (rift) IP address" |
| 24 | echo -e " -r: RO (openmano) IP address" |
| 25 | echo -e " -v: VCA (juju) IP address" |
| prithiv | 4a98a04 | 2017-12-05 23:19:39 +0000 | [diff] [blame] | 26 | echo -e " -m: MON IP address" |
| garciadeblas | 93c6131 | 2016-09-28 15:12:48 +0200 | [diff] [blame] | 27 | echo -e " -h: show this help" |
| 28 | } |
| 29 | |
| 30 | ################### |
| 31 | # End of functions |
| 32 | ################### |
| 33 | |
| 34 | #Check root privileges |
| 35 | [ "$USER" != "root" ] && echo "Needed root privileges (run with sudo)" >&2 && exit 1 |
| 36 | |
| 37 | HERE=$(realpath $(dirname $0)) |
| 38 | OSM_DEVOPS=$(dirname $HERE) |
| garciadeblas | 2cfd884 | 2016-10-02 01:38:11 +0200 | [diff] [blame] | 39 | OSM_JENKINS="$OSM_DEVOPS/jenkins" |
| 40 | . $OSM_JENKINS/common/all_funcs |
| garciadeblas | 93c6131 | 2016-09-28 15:12:48 +0200 | [diff] [blame] | 41 | |
| 42 | #Get default IP address |
| 43 | . $OSM_DEVOPS/installers/export_ips |
| 44 | |
| 45 | UI_IP=$DEFAULT_IP |
| 46 | RO_IP=$DEFAULT_IP |
| 47 | VCA_IP=$DEFAULT_IP |
| prithiv | 4a98a04 | 2017-12-05 23:19:39 +0000 | [diff] [blame] | 48 | MON_IP=$DEFAULT_IP |
| garciadeblas | 93c6131 | 2016-09-28 15:12:48 +0200 | [diff] [blame] | 49 | |
| 50 | #read input options |
| 51 | while getopts ":u:r:v:h-:" o; do |
| 52 | case "${o}" in |
| 53 | u) |
| 54 | export UI_IP="$OPTARG" |
| 55 | ;; |
| 56 | r) |
| 57 | export RO_IP="$OPTARG" |
| 58 | ;; |
| 59 | v) |
| 60 | export VCA_IP="$OPTARG" |
| 61 | ;; |
| 62 | h) |
| 63 | usage && exit 0 |
| 64 | ;; |
| 65 | -) |
| 66 | [ "${OPTARG}" == "help" ] && usage && exit 0 |
| 67 | echo -e "Invalid option: '--$OPTARG'\nTry $0 --help for more information" >&2 |
| 68 | exit 1 |
| 69 | ;; |
| 70 | \?) |
| 71 | echo -e "Invalid option: '-$OPTARG'\nTry $0 --help for more information" >&2 |
| 72 | exit 1 |
| 73 | ;; |
| 74 | :) |
| 75 | echo -e "Option '-$OPTARG' requires an argument\nTry $0 --help for more information" >&2 |
| 76 | exit 1 |
| 77 | ;; |
| 78 | *) |
| 79 | usage >&2 |
| 80 | exit -1 |
| 81 | ;; |
| 82 | esac |
| 83 | done |
| 84 | |
| 85 | ############# |
| 86 | # NAT port forwarding configuration |
| 87 | ############# |
| 88 | echo |
| 89 | echo "*** Configuring iptables rules ***" |
| 90 | |
| prithiv | 4a98a04 | 2017-12-05 23:19:39 +0000 | [diff] [blame] | 91 | awk -v RO_IP="$RO_IP" -v VCA_IP="$VCA_IP" -v UI_IP="$UI_IP" -v openmano_ip="$RO_CONTAINER_IP" -v MON_IP="$MON_IP" -v rift_ip="$SO_CONTAINER_IP" -v juju_ip="$VCA_CONTAINER_IP" ' |
| 92 | BEGIN {innat=0; innatpre=0; osmpre=0; donepre=0; innatpost=0; osmpost=0; donepost=0} |
| garciadeblas | 93c6131 | 2016-09-28 15:12:48 +0200 | [diff] [blame] | 93 | /^\*nat/ { |
| 94 | innat=1; |
| 95 | print; |
| 96 | next |
| 97 | } |
| 98 | innat==1 && /\:PREROUTING/ { |
| 99 | innatpre=1; |
| 100 | print; |
| 101 | next; |
| 102 | } |
| 103 | innatpre==1 && /\#Autogenerated by nat_osm/ { |
| 104 | osmpre=1; |
| 105 | next; |
| 106 | } |
| 107 | osmpre==1 && /#End autogeneration by nat_osm/ { |
| 108 | print "#Autogenerated by nat_osm" |
| 109 | print "-A PREROUTING -d "RO_IP" -p tcp -m tcp --dport 9090 -j DNAT --to-destination "openmano_ip |
| 110 | print "-A PREROUTING -d "UI_IP" -p tcp -m tcp --dport 8000 -j DNAT --to-destination "rift_ip |
| garciadeblas | 93c6131 | 2016-09-28 15:12:48 +0200 | [diff] [blame] | 111 | print "-A PREROUTING -d "UI_IP" -p tcp -m tcp --dport 8443 -j DNAT --to-destination "rift_ip |
| garciadeblas | 55490d4 | 2016-10-29 14:22:03 +0200 | [diff] [blame] | 112 | print "-A PREROUTING -d "UI_IP" -p tcp -m tcp --dport 8008 -j DNAT --to-destination "rift_ip |
| Jeremy Mordkoff | baaec06 | 2017-10-03 15:53:14 -0400 | [diff] [blame] | 113 | print "-A PREROUTING -d "UI_IP" -p tcp -m tcp --dport 8009 -j DNAT --to-destination "rift_ip |
| garciadeblas | 55490d4 | 2016-10-29 14:22:03 +0200 | [diff] [blame] | 114 | print "-A PREROUTING -d "UI_IP" -p tcp -m tcp --dport 80 -j DNAT --to-destination "rift_ip |
| garciadeblas | 93c6131 | 2016-09-28 15:12:48 +0200 | [diff] [blame] | 115 | #print "-A PREROUTING -d "VCA_IP" -p tcp -m tcp --dport 443 -j DNAT --to-destination "juju_ip |
| 116 | #print "-A PREROUTING -d "VCA_IP" -p tcp -m tcp --dport 17070 -j DNAT --to-destination "juju_ip |
| 117 | print "#End autogeneration by nat_osm" |
| 118 | osmpre=0; |
| 119 | donepre=1; |
| 120 | next; |
| 121 | } |
| 122 | osmpre==1 {next;} |
| 123 | innatpre==1 && /\:INPUT/ { |
| 124 | innatpre=0; |
| 125 | if (donepre==0) { |
| 126 | print "#Autogenerated by nat_osm" |
| 127 | print "-A PREROUTING -d "RO_IP" -p tcp -m tcp --dport 9090 -j DNAT --to-destination "openmano_ip |
| 128 | print "-A PREROUTING -d "UI_IP" -p tcp -m tcp --dport 8000 -j DNAT --to-destination "rift_ip |
| garciadeblas | 93c6131 | 2016-09-28 15:12:48 +0200 | [diff] [blame] | 129 | print "-A PREROUTING -d "UI_IP" -p tcp -m tcp --dport 8443 -j DNAT --to-destination "rift_ip |
| garciadeblas | 55490d4 | 2016-10-29 14:22:03 +0200 | [diff] [blame] | 130 | print "-A PREROUTING -d "UI_IP" -p tcp -m tcp --dport 8008 -j DNAT --to-destination "rift_ip |
| Jeremy Mordkoff | baaec06 | 2017-10-03 15:53:14 -0400 | [diff] [blame] | 131 | print "-A PREROUTING -d "UI_IP" -p tcp -m tcp --dport 8009 -j DNAT --to-destination "rift_ip |
| garciadeblas | 55490d4 | 2016-10-29 14:22:03 +0200 | [diff] [blame] | 132 | print "-A PREROUTING -d "UI_IP" -p tcp -m tcp --dport 80 -j DNAT --to-destination "rift_ip |
| garciadeblas | 93c6131 | 2016-09-28 15:12:48 +0200 | [diff] [blame] | 133 | #print "-A PREROUTING -d "VCA_IP" -p tcp -m tcp --dport 443 -j DNAT --to-destination "juju_ip |
| 134 | #print "-A PREROUTING -d "VCA_IP" -p tcp -m tcp --dport 17070 -j DNAT --to-destination "juju_ip |
| 135 | print "#End autogeneration by nat_osm" |
| 136 | donepre=1; |
| 137 | } |
| 138 | print; |
| 139 | next; |
| 140 | } |
| 141 | |
| garciadeblas | 6264e9b | 2018-03-15 09:40:51 +0100 | [diff] [blame] | 142 | innat==1 && /\:OUTPUT/ { |
| 143 | innatoutput=1; |
| 144 | print; |
| 145 | next; |
| 146 | } |
| 147 | innatoutput==1 && /\#Autogenerated by nat_osm/ { |
| 148 | osmoutput=1; |
| 149 | next; |
| 150 | } |
| 151 | osmoutput==1 && /#End autogeneration by nat_osm/ { |
| 152 | print "#Autogenerated by nat_osm" |
| 153 | print "-A OUTPUT -p tcp -o lo --dport 8009 -j DNAT --to "rift_ip":8009" |
| 154 | print "-A OUTPUT -p tcp -o lo --dport 8443 -j DNAT --to "rift_ip":8443" |
| 155 | print "#End autogeneration by nat_osm" |
| 156 | osmoutput=0; |
| 157 | doneoutput=1; |
| 158 | next; |
| 159 | } |
| 160 | osmoutput==1 {next;} |
| 161 | innatoutput==1 && /\:POSTROUTING/ { |
| 162 | innatoutput=0; |
| 163 | if (doneoutput==0) { |
| 164 | print "#Autogenerated by nat_osm" |
| 165 | print "-A OUTPUT -p tcp -o lo --dport 8009 -j DNAT --to "rift_ip":8009" |
| 166 | print "-A OUTPUT -p tcp -o lo --dport 8443 -j DNAT --to "rift_ip":8443" |
| 167 | print "#End autogeneration by nat_osm" |
| 168 | doneoutput=1; |
| 169 | } |
| 170 | innatpost=1; |
| 171 | print; |
| 172 | next; |
| 173 | } |
| 174 | |
| garciadeblas | 93c6131 | 2016-09-28 15:12:48 +0200 | [diff] [blame] | 175 | innat==1 && /\:POSTROUTING/ { |
| 176 | innatpost=1; |
| 177 | print; |
| 178 | next; |
| 179 | } |
| 180 | innatpost==1 && /\#Autogenerated by nat_osm/ { |
| 181 | osmpost=1; |
| 182 | next; |
| 183 | } |
| 184 | osmpost==1 && /#End autogeneration by nat_osm/ { |
| garciadeblas | bd8f9f3 | 2017-03-08 15:40:40 +0100 | [diff] [blame] | 185 | print "#Autogenerated by nat_osm" |
| 186 | print "-A POSTROUTING -s "rift_ip"/24 -d "rift_ip" -p tcp --dport 8443 -j MASQUERADE" |
| garciadeblas | 6264e9b | 2018-03-15 09:40:51 +0100 | [diff] [blame] | 187 | print "-A POSTROUTING -s "UI_IP"/32 -d "rift_ip" -p tcp --dport 8009 -j MASQUERADE" |
| 188 | print "-A POSTROUTING -s "UI_IP"/32 -d "rift_ip" -p tcp --dport 8443 -j MASQUERADE" |
| garciadeblas | 93c6131 | 2016-09-28 15:12:48 +0200 | [diff] [blame] | 189 | #print "-A POSTROUTING -s "rift_ip" -p tcp -m tcp --dport 9090 -d "openmano_ip" -j SNAT --to "UI_IP |
| 190 | #print "-A POSTROUTING -s "rift_ip" -p tcp -m tcp --dport 17070 -d "juju_ip" -j SNAT --to "UI_IP |
| garciadeblas | bd8f9f3 | 2017-03-08 15:40:40 +0100 | [diff] [blame] | 191 | print "#End autogeneration by nat_osm" |
| garciadeblas | 93c6131 | 2016-09-28 15:12:48 +0200 | [diff] [blame] | 192 | osmpost=0; |
| 193 | donepost=1; |
| 194 | next; |
| 195 | } |
| 196 | osmpost==1 {next;} |
| 197 | innatpost==1 && /COMMIT/ { |
| 198 | innatpost=0; |
| 199 | innat=0; |
| 200 | if (donepost==0) { |
| garciadeblas | bd8f9f3 | 2017-03-08 15:40:40 +0100 | [diff] [blame] | 201 | print "#Autogenerated by nat_osm" |
| 202 | print "-A POSTROUTING -s "rift_ip"/24 -d "rift_ip" -p tcp --dport 8443 -j MASQUERADE" |
| garciadeblas | 6264e9b | 2018-03-15 09:40:51 +0100 | [diff] [blame] | 203 | print "-A POSTROUTING -s "UI_IP"/32 -d "rift_ip" -p tcp --dport 8009 -j MASQUERADE" |
| 204 | print "-A POSTROUTING -s "UI_IP"/32 -d "rift_ip" -p tcp --dport 8443 -j MASQUERADE" |
| garciadeblas | 93c6131 | 2016-09-28 15:12:48 +0200 | [diff] [blame] | 205 | #print "-A POSTROUTING -s "rift_ip" -p tcp -m tcp --dport 9090 -d "openmano_ip" -j SNAT --to "UI_IP |
| 206 | #print "-A POSTROUTING -s "rift_ip" -p tcp -m tcp --dport 17070 -d "juju_ip" -j SNAT --to "UI_IP |
| garciadeblas | bd8f9f3 | 2017-03-08 15:40:40 +0100 | [diff] [blame] | 207 | print "#End autogeneration by nat_osm" |
| garciadeblas | 93c6131 | 2016-09-28 15:12:48 +0200 | [diff] [blame] | 208 | donepost=1; |
| 209 | } |
| 210 | print; |
| 211 | next; |
| 212 | } |
| 213 | { |
| 214 | print |
| 215 | } |
| 216 | ' /etc/iptables/rules.v4 > testfile.tmp && mv testfile.tmp /etc/iptables/rules.v4 |
| 217 | |
| 218 | service netfilter-persistent restart |
| 219 | |