---
- hosts: test_machine
  tasks:
    - name: Check sshpass
      shell: which sshpass
      ignore_errors: yes
      register: path

    - name: Install sshpass if missing
      apt: name=sshpass state=present
      sudo: yes
      when: path.rc != 0

    - name: check whether ssh key has been generated
      command: find {{ lookup('env', 'HOME') }}/.ssh -name 'id_rsa.pub'
      register: result

    - name: Generate ssh key if missing
      user: name={{ ansible_user_id }} generate_ssh_key=yes
      when: result.stdout == ""

    - name: Copy public key to /tmp
      copy: src={{ lookup('env', 'HOME') }}/.ssh/id_rsa.pub
            dest=/tmp/

    - name: Copy RackHD/on-tools into /tmp/on-tools
      git: repo=https://github.com/RackHD/on-tools.git
           dest=/tmp/on-tools/
           accept_hostkey=true
           version="{{ branch | default('master') }}"

- hosts: rackhd
  vars_files:
    - vars/setup.yml
  tasks:
    - name: Copy public key to authorized_key for ssh in rsync
      authorized_key: user={{ ansible_ssh_user }}
                      key="{{ item }}"
      with_file:
        - /tmp/id_rsa.pub

    - name: Install rsync
      apt: update_cache=yes
      apt: name="rsync"
      sudo: yes

    - name: Copy config file to rsyncd
      copy: src=files/rsync dest=/etc/default/
      sudo: yes

    - name: Copy config file to rsyncd
      copy: src=files/rsyncd.conf dest=/etc/
      sudo: yes

    - name: Restart rsyncd server
      command: /etc/init.d/rsync restart
      sudo: yes

    - name: Check whether atop has been installed
      shell: whereis atop | awk -F ':' '{print $2}'
      register: atop_path

    - name: Check whether netatop has been installed
      shell: whereis netatop | awk -F ':' '{print $2}'
      register: netatop_path

    - name: Check whether netatop is running
      shell: ps aux | grep netatop | sed "/grep/d"
      register: netatop_status

    - name: Start netatop if already installed but not running
      shell: insmod /lib/modules/`uname -r`/extra/netatop.ko; /usr/sbin/netatopd
      sudo: yes
      when: (netatop_path.stdout != "") and (netatop_status.stdout_lines|count < 2)

    - name: Install dependent tools for atop and netatop
      apt: name={{ item.package }}
      sudo: yes
      with_items:
        atop_apt_package_manifest
      when: (atop_path.stdout == "") or (netatop_path.stdout == "")

    - name: Fetch atop packages
      get_url: url={{ item.value.url }}/{{ item.value.name }}.{{ item.value.suffix }}
               dest=/tmp/
      with_dict:
        atop_wget_package_manifest
      when: (atop_path.stdout == "") or (netatop_path.stdout == "")

    - name: Unpack atop packages
      command:  tar -vxf /tmp/{{ item.value.name }}.{{ item.value.suffix }} -C /tmp
      with_dict:
        atop_wget_package_manifest
      when: (atop_path.stdout == "") or (netatop_path.stdout == "")

    - name: Install atop
      command: "{{ item }} chdir=/tmp/{{ atop_wget_package_manifest.atop.name }}"
      sudo: yes
      with_items:
        atop_wget_package_manifest.atop.command
      when: atop_path.stdout == ""

    - name: Install netatop
      command: "{{ item }} chdir=/tmp/{{ atop_wget_package_manifest.netatop.name }}"
      sudo: yes
      with_items:
        atop_wget_package_manifest.netatop.command
      when: netatop_path.stdout == ""

    - name: Restart netatop
      service: name=netatop state=restarted
      sudo: yes
      when: netatop_path.stdout == ""

    - name: Copy atop config file to /etc
      copy: src=files/atoprc dest=/etc/
      sudo: yes

    - name: Copy data-collection daemon to /tmp
      copy: src=files/collect_data.sh dest=/tmp/ mode=0755

    - name: Copy database data-collection daemon to /tmp
      copy: src=files/db_disk.sh dest=/tmp/ mode=0755

    - name: Disable firewall for host port
      command: ufw allow {{ host_port }}
      sudo: yes
