blob: 2fbf4782786c26c35638d935caf31dc3516e5664 [file] [log] [blame]
Scott Bakerb63ea792016-08-11 10:24:48 -07001#!/bin/bash
2
3iptables -L > /dev/null
4ip6tables -L > /dev/null
5
6CONTAINER={{ container_name }}
7IMAGE={{ docker_image }}
8
9function mac_to_iface {
10 PARENT_MAC=$1
11 ifconfig|grep $PARENT_MAC| awk '{print $1}'|grep -v '\.'
12}
13
14function encapsulate_stag {
15 LAN_IFACE=$1
16 STAG=$2
17 ifconfig $LAN_IFACE >> /dev/null
18 if [ "$?" == 0 ]; then
19 STAG_IFACE=$LAN_IFACE.$STAG
20 ifconfig $LAN_IFACE up
21 ifconfig $STAG_IFACE
22 if [ "$?" == 0 ]; then
23 echo $STAG_IFACE is already created
24 else
25 ifconfig $STAG_IFACE >> /dev/null || ip link add link $LAN_IFACE name $STAG_IFACE type vlan id $STAG
26 fi
27 ifconfig $STAG_IFACE up
28 else
29 echo There is no $LAN_IFACE. Aborting.
30 exit -1
31 fi
32}
33
34
35{% if volumes %}
36{% for volume in volumes %}
37DEST_DIR=/var/container_volumes/$CONTAINER/{{ volume }}
38mkdir -p $DEST_DIR
39VOLUME_ARGS="$VOLUME_ARGS -v $DEST_DIR:{{ volume }}"
40{% endfor %}
41{% endif %}
42
43docker inspect $CONTAINER > /dev/null 2>&1
44if [ "$?" == 1 ]
45then
46 docker pull $IMAGE
47{% if network_method=="host" %}
48 docker run -d --name=$CONTAINER --privileged=true --net=host $VOLUME_ARGS $IMAGE
49{% elif network_method=="bridged" %}
50 docker run -d --name=$CONTAINER --privileged=true --net=bridge $VOLUME_ARGS $IMAGE
51{% else %}
52 docker run -d --name=$CONTAINER --privileged=true --net=none $VOLUME_ARGS $IMAGE
53{% endif %}
54else
55 docker start $CONTAINER
56fi
57
58{% if ports %}
59{% for port in ports %}
60
61{% if port.next_hop %}
62NEXTHOP_ARG="@{{ port.next_hop }}"
63{% else %}
64NEXTHOP_ARG=""
65{% endif %}
66
67{% if port.c_tag %}
68CTAG_ARG="@{{ port.c_tag }}"
69{% else %}
70CTAG_ARG=""
71{% endif %}
72
73{% if port.parent_mac %}
74# container-in-VM
75SRC_DEV=$( mac_to_iface "{{ port.parent_mac }}" )
76CMD="docker exec $CONTAINER ifconfig $SRC_DEV >> /dev/null || pipework $SRC_DEV -i {{ port.device }} $CONTAINER {{ port.ip }}/24$NEXTHOP_ARG {{ port.mac }} $CTAG_ARG"
77echo $CMD
78eval $CMD
79
80{% else %}
81# container-on-metal
82IP="{{ port.ip }}"
83{% if port.mac %}
84MAC="{{ port.mac }}"
85{% else %}
86MAC=""
87{% endif %}
88
89DEVICE="{{ port.device }}"
90BRIDGE="{{ port.bridge }}"
91{% if port.s_tag %}
92# This is intended for lan_network. Assume that BRIDGE is set to br_lan. We
93# create a device that strips off the S-TAG.
94STAG="{{ port.s_tag }}"
95encapsulate_stag $BRIDGE $STAG
96SRC_DEV=$STAG_IFACE
97{% else %}
98# This is for a standard neutron private network. We use a donor VM to setup
99# openvswitch for us, and we snoop at its devices and create a tap using the
100# same settings.
101XOS_NETWORK_ID="{{ port.xos_network_id }}"
102INSTANCE_MAC="{{ port.snoop_instance_mac }}"
103INSTANCE_ID="{{ port.snoop_instance_id }}"
104INSTANCE_TAP=`virsh domiflist $INSTANCE_ID | grep -i $INSTANCE_MAC | awk '{print $1}'`
105INSTANCE_TAP=${INSTANCE_TAP:3}
106VLAN_ID=`ovs-vsctl show | grep -i -A 1 port.*$INSTANCE_TAP | grep -i tag | awk '{print $2}'`
107# One tap for all containers per XOS/neutron network. Included the VLAN_ID in the
108# hash, to cover the case where XOS is reinstalled and the XOS network ids
109# get reused.
110TAP="con`echo ${XOS_NETWORK_ID}_$VLAN_ID|md5sum|awk '{print $1}'`"
111TAP=${TAP:0:10}
112echo im=$INSTANCE_MAC ii=$INSTANCE_ID it=$INSTANCE_TAP vlan=$VLAN_ID tap=$TAP con=$CONTAINER dev=$DEVICE mac=$MAC
113ovs-vsctl show | grep -i $TAP
114if [[ $? == 1 ]]; then
115 echo creating tap
116 ovs-vsctl add-port $BRIDGE $TAP tag=$VLAN_ID -- set interface $TAP type=internal
117else
118 echo tap exists
119fi
120SRC_DEV=$TAP
121{% endif %}
122
123CMD="docker exec $CONTAINER ifconfig $DEVICE >> /dev/null || pipework $SRC_DEV -i $DEVICE $CONTAINER $IP/24$NEXTHOP_ARG $MAC $CTAG_ARG"
124echo $CMD
125eval $CMD
126{% endif %}
127{% endfor %}
128{% endif %}
129
130# Attach to container
131# (this is only done when using upstart, since upstart expects to be attached
132# to a running service)
133if [[ "$1" == "ATTACH" ]]; then
134 docker start -a $CONTAINER
135fi
136