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