---
- hosts: {{ instance_name }}
  gather_facts: False
  connection: ssh
  user: ubuntu
  sudo: yes
  vars:
      unique_id: {{ unique_id }}
      auth_url: {{ auth_url }}
      admin_user: {{ admin_user }}
      admin_password: {{ admin_password }}
      admin_tenant: {{ admin_tenant }}
      shared_lan_ip: {{ private_ip }}
      shared_lan_mac: {{ private_mac }}
      headnode_flat_lan_ip: {{ rabbit_host }}
      ceilometer_client_acess_ip: {{ ceilometer_ip }}
      ceilometer_client_acess_mac: {{ ceilometer_mac }}
      allowed_tenant_ids:
        {% for allowed_tenant_id in allowed_tenant_ids %}
        - {{ allowed_tenant_id }}
        {% endfor %}

  tasks:
{% if full_setup %}
  - name: Docker repository
    copy: src=/opt/xos/observers/monitoring_channel/files/docker.list
      dest=/etc/apt/sources.list.d/docker.list

  - name: Import the repository key
    apt_key: keyserver=keyserver.ubuntu.com id=36A1D7869245C8950F966E92D8576A8BA88D21E9

  - name: install Docker
    apt: name=lxc-docker-1.5.0 state=present update_cache=yes

  - name: install python-setuptools
    apt: name=python-setuptools state=present

  - name: install pip
    easy_install: name=pip

  - name: install docker-py
    pip: name=docker-py version=0.5.3

  - name: install Pipework
    get_url: url=https://raw.githubusercontent.com/jpetazzo/pipework/master/pipework
       dest=/usr/local/bin/pipework
       mode=0755

  - name: Disable resolvconf service
    shell: service resolvconf stop
    shell: echo manual > /etc/init/resolvconf.override
    shell: rm -f /etc/resolv.conf

  - name: Install resolv.conf
    copy: src=/opt/xos/observers/monitoring_channel/files/vm-resolv.conf
      dest=/etc/resolv.conf
{% endif %}

  - name: ceilometer proxy config
    template: src=/opt/xos/observers/monitoring_channel/templates/ceilometer_proxy_config.j2 dest=/usr/local/share/monitoring-channel-{{ unique_id }}_ceilometer_proxy_config mode=0777

  - name: Monitoring channel upstart
    template: src=/opt/xos/observers/monitoring_channel/templates/monitoring-channel.conf.j2 dest=/etc/init/monitoring-channel-{{ unique_id }}.conf

  - name: Monitoring channel startup script
    template: src=/opt/xos/observers/monitoring_channel/templates/start-monitoring-channel.sh.j2 dest=/usr/local/sbin/start-monitoring-channel-{{ unique_id }}.sh mode=0755
    notify:
#    - restart monitoring-channel
     - stop monitoring-channel
     - remove container
     - start monitoring-channel

# These are samples, not necessary for correct function of demo

  - name: Make sure Monitoring channel service is running
    service: name=monitoring-channel-{{ unique_id }} state=started

  handlers:
  - name: restart monitoring-channel
    shell: service monitoring-channel-{{ unique_id }} stop; sleep 1; service vcpe-{{ unique_id }} start

  - name: stop monitoring-channel
    service: name=monitoring-channel-{{ unique_id }} state=stopped

  - name: remove container
    docker: name=monitoring-channel-{{ unique_id }} state=absent image=docker-vcpe

  - name: start monitoring-channel
    service: name=monitoring-channel-{{ unique_id }} state=started
