---
- 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 }}
      ceilometer_host_port: {{ ceilometer_port }}
      ceilometer_pub_sub_url: {{ ceilometer_pub_sub_url }}
      allowed_tenant_ids:
        {% for allowed_tenant_id in allowed_tenant_ids %}
        - {{ allowed_tenant_id }}
        {% endfor %}

  tasks:
{% if delete %}
  - name: Remove tenant
# FIXME: Adding dummy template action to avoid "action attribute missing in task" error
    template: src=/opt/xos/synchronizers/monitoring_channel/templates/ceilometer_proxy_config.j2 dest=/usr/local/share/monitoring-channel-{{ unique_id }}_ceilometer_proxy_config mode=0777
    notify:
     - stop monitoring-channel
     - remove container
{% else %}
{% if full_setup %}
#  - name: Docker repository
#    copy: src=/opt/xos/synchronizers/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 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/synchronizers/monitoring_channel/files/vm-resolv.conf
#      dest=/etc/resolv.conf
{% endif %}

  - name: ceilometer proxy config
    template: src=/opt/xos/synchronizers/monitoring_channel/templates/ceilometer_proxy_config.j2 dest=/usr/local/share/monitoring-channel-{{ unique_id }}_ceilometer_proxy_config mode=0777
    notify:
#    - restart monitoring-channel
     - stop monitoring-channel
     - remove container
     - start monitoring-channel

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

  - name: Monitoring channel startup script
    template: src=/opt/xos/synchronizers/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

#  - name: Start monitoring-channel container
#    docker:
#      docker_api_version: "1.18"
#      name: monitoring-channel-{{ unique_id }}
#      # was: reloaded
#      state: running
#      image: srikanthvavila/monitoring-channel
#      expose:
#      - 8000
#      ports:
#      - "{{ ceilometer_port }}:8000"
#      volumes:
#      - /usr/local/share/monitoring-channel-{{ unique_id }}_ceilometer_proxy_config:/usr/local/share/ceilometer_proxy_config
#
#  - name: Get Docker IP
#    #TODO: copy dockerip.sh to monitoring service synchronizer
#    script: /opt/xos/synchronizers/onos/scripts/dockerip.sh monitoring-channel-{{ unique_id }}
#    register: dockerip
#
#  - name: Wait for Monitoring channel to come up
#    wait_for:
#      host={{ '{{' }} dockerip.stdout {{ '}}' }}
#      port={{ '{{' }} item {{ '}}' }}
#      state=present
#    with_items:
#    - {{ ceilometer_port }}
# 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
{% endif %}

  handlers:
  - name: restart monitoring-channel
    shell: service monitoring-channel-{{ unique_id }} stop; sleep 1; service monitoring-channel-{{ 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=monitoring-channel

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