Scott Baker | b63ea79 | 2016-08-11 10:24:48 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | iptables -L > /dev/null |
| 4 | ip6tables -L > /dev/null |
| 5 | |
| 6 | CONTAINER={{ container_name }} |
| 7 | IMAGE={{ docker_image }} |
| 8 | |
| 9 | function mac_to_iface { |
| 10 | PARENT_MAC=$1 |
| 11 | ifconfig|grep $PARENT_MAC| awk '{print $1}'|grep -v '\.' |
| 12 | } |
| 13 | |
| 14 | function 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 %} |
| 37 | DEST_DIR=/var/container_volumes/$CONTAINER/{{ volume }} |
| 38 | mkdir -p $DEST_DIR |
| 39 | VOLUME_ARGS="$VOLUME_ARGS -v $DEST_DIR:{{ volume }}" |
| 40 | {% endfor %} |
| 41 | {% endif %} |
| 42 | |
| 43 | docker inspect $CONTAINER > /dev/null 2>&1 |
| 44 | if [ "$?" == 1 ] |
| 45 | then |
| 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 %} |
| 54 | else |
| 55 | docker start $CONTAINER |
| 56 | fi |
| 57 | |
| 58 | {% if ports %} |
| 59 | {% for port in ports %} |
| 60 | |
| 61 | {% if port.next_hop %} |
| 62 | NEXTHOP_ARG="@{{ port.next_hop }}" |
| 63 | {% else %} |
| 64 | NEXTHOP_ARG="" |
| 65 | {% endif %} |
| 66 | |
| 67 | {% if port.c_tag %} |
| 68 | CTAG_ARG="@{{ port.c_tag }}" |
| 69 | {% else %} |
| 70 | CTAG_ARG="" |
| 71 | {% endif %} |
| 72 | |
| 73 | {% if port.parent_mac %} |
| 74 | # container-in-VM |
| 75 | SRC_DEV=$( mac_to_iface "{{ port.parent_mac }}" ) |
| 76 | CMD="docker exec $CONTAINER ifconfig $SRC_DEV >> /dev/null || pipework $SRC_DEV -i {{ port.device }} $CONTAINER {{ port.ip }}/24$NEXTHOP_ARG {{ port.mac }} $CTAG_ARG" |
| 77 | echo $CMD |
| 78 | eval $CMD |
| 79 | |
| 80 | {% else %} |
| 81 | # container-on-metal |
| 82 | IP="{{ port.ip }}" |
| 83 | {% if port.mac %} |
| 84 | MAC="{{ port.mac }}" |
| 85 | {% else %} |
| 86 | MAC="" |
| 87 | {% endif %} |
| 88 | |
| 89 | DEVICE="{{ port.device }}"
|
| 90 | BRIDGE="{{ 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.
|
| 94 | STAG="{{ port.s_tag }}"
|
| 95 | encapsulate_stag $BRIDGE $STAG
|
| 96 | SRC_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.
|
| 101 | XOS_NETWORK_ID="{{ port.xos_network_id }}"
|
| 102 | INSTANCE_MAC="{{ port.snoop_instance_mac }}" |
| 103 | INSTANCE_ID="{{ port.snoop_instance_id }}" |
| 104 | INSTANCE_TAP=`virsh domiflist $INSTANCE_ID | grep -i $INSTANCE_MAC | awk '{print $1}'` |
| 105 | INSTANCE_TAP=${INSTANCE_TAP:3} |
| 106 | VLAN_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. |
| 110 | TAP="con`echo ${XOS_NETWORK_ID}_$VLAN_ID|md5sum|awk '{print $1}'`" |
| 111 | TAP=${TAP:0:10} |
| 112 | echo im=$INSTANCE_MAC ii=$INSTANCE_ID it=$INSTANCE_TAP vlan=$VLAN_ID tap=$TAP con=$CONTAINER dev=$DEVICE mac=$MAC |
| 113 | ovs-vsctl show | grep -i $TAP |
| 114 | if [[ $? == 1 ]]; then |
| 115 | echo creating tap |
| 116 | ovs-vsctl add-port $BRIDGE $TAP tag=$VLAN_ID -- set interface $TAP type=internal |
| 117 | else |
| 118 | echo tap exists |
| 119 | fi |
| 120 | SRC_DEV=$TAP |
| 121 | {% endif %} |
| 122 | |
| 123 | CMD="docker exec $CONTAINER ifconfig $DEVICE >> /dev/null || pipework $SRC_DEV -i $DEVICE $CONTAINER $IP/24$NEXTHOP_ARG $MAC $CTAG_ARG" |
| 124 | echo $CMD |
| 125 | eval $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) |
| 133 | if [[ "$1" == "ATTACH" ]]; then |
| 134 | docker start -a $CONTAINER |
| 135 | fi |
| 136 | |