Merge branch 'master' of https://github.com/open-cloud/xos
diff --git a/xos/openstack_observer/steps/sync_container.py b/xos/openstack_observer/steps/sync_container.py
index fdee9e7..8f5940b 100644
--- a/xos/openstack_observer/steps/sync_container.py
+++ b/xos/openstack_observer/steps/sync_container.py
@@ -51,6 +51,7 @@
             pd={}
             pd["mac"] = port.mac or ""
             pd["ip"] = port.ip or ""
+            pd["xos_network_id"] = port.network.id
 
             if o.isolation == "container":
                 # container on bare metal
@@ -84,7 +85,6 @@
     def get_extra_attributes(self, o):
         fields={}
         fields["ansible_tag"] = "container-%s" % str(o.id)
-        fields["container_name"] = "%s-%s" % (o.slice.name, str(o.id))
         fields["docker_image"] = o.image.name
         fields["ports"] = self.get_ports(o)
         if o.volumes:
diff --git a/xos/openstack_observer/steps/sync_container.yaml b/xos/openstack_observer/steps/sync_container.yaml
index 4a050a5..8a1f353 100644
--- a/xos/openstack_observer/steps/sync_container.yaml
+++ b/xos/openstack_observer/steps/sync_container.yaml
@@ -11,6 +11,7 @@
     ports:
     {% for port in ports %}
        - device: {{ port.device }}
+         xos_network_id: {{ port.xos_network_id }}
          mac: {{ port.mac|default("") }}
          ip: {{ port.ip }}
          snoop_instance_mac: {{ port.snoop_instance_mac }}
diff --git a/xos/openstack_observer/templates/container.conf.j2 b/xos/openstack_observer/templates/container.conf.j2
index d3ef42d..7cbb880 100644
--- a/xos/openstack_observer/templates/container.conf.j2
+++ b/xos/openstack_observer/templates/container.conf.j2
@@ -6,6 +6,9 @@
 respawn
 
 script
-  /usr/local/sbin/start-container-{{ container_name }}.sh
+  /usr/local/sbin/start-container-{{ container_name }}.sh ATTACH
 end script
 
+post-stop script
+  /usr/local/sbin/stop-container-{{ container_name }}.sh
+end script
\ No newline at end of file
diff --git a/xos/openstack_observer/templates/container.service.j2 b/xos/openstack_observer/templates/container.service.j2
index 9e2b83c..817d6d7 100644
--- a/xos/openstack_observer/templates/container.service.j2
+++ b/xos/openstack_observer/templates/container.service.j2
@@ -3,7 +3,9 @@
 After=docker.service
 
 [Service]
-ExecStart=/bin/bash -c "/usr/local/sbin/start-container-{{ container_name }}.sh"
+ExecStart=/bin/bash -c "/usr/local/sbin/start-container-{{ container_name }}.sh ATTACH"
+ExecStop=/bin/bash -c "/usr/local/sbin/stop-container-{{ container_name }}.sh"
+SuccessExitStatus=0 137
 
 [Install]
 WantedBy=multi-user.target
diff --git a/xos/openstack_observer/templates/start-container.sh.j2 b/xos/openstack_observer/templates/start-container.sh.j2
index 9ed7faa..42d93a2 100644
--- a/xos/openstack_observer/templates/start-container.sh.j2
+++ b/xos/openstack_observer/templates/start-container.sh.j2
@@ -56,12 +56,16 @@
 {% endif %}
 
 DEVICE="{{ port.device }}"

+XOS_NETWORK_ID="{{ port.xos_network_id }}"

 INSTANCE_MAC="{{ port.snoop_instance_mac }}"
 INSTANCE_ID="{{ port.snoop_instance_id }}"
 INSTANCE_TAP=`virsh domiflist $INSTANCE_ID | grep -i $INSTANCE_MAC | awk '{print $1}'`
 INSTANCE_TAP=${INSTANCE_TAP:3}
 VLAN_ID=`ovs-vsctl show | grep -i -A 1 port.*$INSTANCE_TAP | grep -i tag | awk '{print $2}'`
-TAP="con`echo ${CONTAINER}_$DEVICE|md5sum|awk '{print $1}'`"
+# One tap for all containers per XOS/neutron network. Included the VLAN_ID in the
+# hash, to cover the case where XOS is reinstalled and the XOS network ids
+# get reused.
+TAP="con`echo ${XOS_NETWORK_ID}_$VLAN_ID|md5sum|awk '{print $1}'`"
 TAP=${TAP:0:10}
 echo im=$INSTANCE_MAC ii=$INSTANCE_ID it=$INSTANCE_TAP vlan=$VLAN_ID tap=$TAP con=$CONTAINER dev=$DEVICE mac=$MAC
 ovs-vsctl show | grep -i $TAP
@@ -80,4 +84,9 @@
 {% endif %}
 
 # Attach to container
-# docker start -a $CONTAINER
+# (this is only done when using upstart, since upstart expects to be attached
+#  to a running service)
+if [[ "$1" == "ATTACH" ]]; then
+    docker start -a $CONTAINER
+fi
+
diff --git a/xos/openstack_observer/templates/stop-container.sh.j2 b/xos/openstack_observer/templates/stop-container.sh.j2
new file mode 100644
index 0000000..9cabb00
--- /dev/null
+++ b/xos/openstack_observer/templates/stop-container.sh.j2
@@ -0,0 +1,4 @@
+CONTAINER={{ container_name }}
+
+docker stop $CONTAINER
+docker rm $CONTAINER