blob: 32e9b558988083bc2fa6673692bb304b85c9e9f1 [file] [log] [blame]
Zack Williams4bd2dbc2016-03-10 12:50:02 -07001#!/bin/sh
2
3SHELL="/bin/bash"
4
5NIC=$( route|grep default|awk '{print $NF}' )
6
7NAME="${1}"
8OP="${2}"
9SUBOP="${3}"
10ARGS="${4}"
11
12add_port_fwd_rule() {
13 DPORT=$1
14 VM=$2
15 TOPORT=$3
16
17 VMIP=$( getent ahosts $VM|head -1|awk '{print $1}' )
18 iptables -t nat -C PREROUTING -p tcp -i $NIC --dport $DPORT -j DNAT --to-destination $VMIP:$TOPORT
19 if [ "$?" -ne 0 ]
20 then
21 iptables -t nat -A PREROUTING -p tcp -i $NIC --dport $DPORT -j DNAT --to-destination $VMIP:$TOPORT
22 fi
23}
24
25if [ "$OP" = "start" ] || [ "$OP" = "reload" ]
26then
27 iptables -t nat -F
28
29{% for vm in head_vm_list -%}
30{% if vm.forwarded_ports is defined -%}
31{% for port in vm.forwarded_ports -%}
32 add_port_fwd_rule {{ port.ext }} {{ vm.name }} {{ port.int }}
33{% endfor -%}
34{% endif -%}
35{% endfor -%}
36
37 # Also flush the filter table before rules re-added
38 iptables -F
39fi
40