---
- hosts: all
  roles:
    - role: common
    - role: local_tests
    - role: thruk_developer
    - role: thruk_plugin_editor
    - role: node_control_client
    - role: omd_clone_fake_version
  tasks:
  - copy:
      src: "/root/thruk_local.conf"
      dest: "/omd/sites/demo/etc/thruk/thruk_local.conf"
      owner: "demo"
      group: "demo"

  - copy:
      src: "/scenario/objects.cfg"
      dest: "/omd/sites/demo/etc/naemon/conf.d/objects.cfg"
      owner: "demo"
      group: "demo"

  - shell: sed -e "s/###SITE###/{{ lookup('env','PREFIX') }}/g" -i /omd/sites/demo/etc/naemon/conf.d/objects.cfg -i /omd/sites/demo/etc/thruk/thruk_local.conf

  - shell: echo "key_tier1a" > /omd/sites/demo/var/thruk/secret.key

  - file:
      path: /omd/sites/demo/var/thruk/secret.key
      mode: 0600
      owner: demo
      group: demo

  # might exist already because of the mounted src folder
  - shell: rm -f /omd/sites/demo/etc/thruk/plugins-enabled/node-control

  - name: enable node-control plugin
    shell: sudo su - demo -c "thruk plugin enable node-control"

  - copy:
      src: "/root/1.tbp"
      dest: "/omd/sites/demo/etc/thruk/bp/1.tbp"
      owner: "demo"
      group: "demo"

  - name: "disable shutdown warnings"
    lineinfile:
      dest: /opt/omd/sites/{{ site }}/.thruk
      create: true
      line: "export THRUK_NO_SHUTDOWN_WARNINGS=1"
      regexp: 'export THRUK_NO_SHUTDOWN_WARNINGS'

  - shell: sudo su - demo -c "omd start core; thruk bp commit; omd stop core"

  - name: "wait for {{ item }} to come up"
    uri:
      url: "https://{{ item }}.tier2.test.local/demo/thruk/cgi-bin/remote.cgi"
      validate_certs: False
      status_code: 200
    register: result
    until: result.status == 200
    retries: 180
    delay: 1
    with_items:
      - tier2a
      - tier2b
      - tier2c
      - tier2d
      - tier2e

  - include_role:
      name: thruk_logcache_mysql

  - shell: sudo su - demo -c "git add . && git commit -m 'setup complete'"
