---
- name: Configure a Buildbot worker for Zcash CI
  hosts: zcash-ci-worker-unix
  become: true
  gather_facts: False

  vars_files:
    - vars/default.yml
    - vars/buildbot.yml

  vars_prompt:
    - name: "buildbot_worker_admin"
      prompt: "Admin details"
      default: "Zcash <sysadmin@z.cash>"
    - name: "buildbot_worker_name"
      prompt: "Buildbot worker name (provided by ZECC)"
      private: no
    - name: "buildbot_worker_password"
      prompt: "Buildbot worker password (provided by ZECC)"

  pre_tasks:
    - name: Install Python 2.7 for Ansible and Buildbot
      raw: test -e /usr/bin/python || test -e /usr/bin/python2 || test -e /usr/bin/python2.7 || test -e /usr/local/bin/python2.7 || (test -e /usr/bin/apt && apt -qqy update && apt install -qqy python) || (test -e /usr/bin/dnf && dnf install -qqy python2) || (test -e /usr/sbin/pkg && pkg install -qqy python2)
      register: output
      changed_when:
        - output.stdout != ""
        - output.stdout != "\r\n"

    - name: Check if Python is in the configured location
      raw: test -e {{ ansible_python_interpreter }}
      ignore_errors: true
      register: python_check
      when: ansible_python_interpreter is defined

    - name: Fail if configured Python is unavailable
      fail:
        msg: "Python is not accessible at {{ ansible_python_interpreter }} on this host! Please set the inventory variable 'ansible_python_interpreter' to the location of the Python 2.7 binary."
      when: ansible_python_interpreter is defined and python_check.rc == 1

    - name: Check if Python is in the default location
      raw: test -e /usr/bin/python
      ignore_errors: true
      register: python_check
      when: ansible_python_interpreter is undefined

    - name: Fail if default Python is unavailable
      fail:
        msg: Python is not accessible at /usr/bin/python on this host! Please set the inventory variable 'ansible_python_interpreter' to the location of the Python 2.7 binary.
      when: ansible_python_interpreter is undefined and python_check.rc == 1

    - name: Gathering Facts
      setup:

    - name: Fail if Python is the wrong version
      fail:
        msg: "The Python binary at {{ ansible_python.executable }} is version {{ ansible_python_version }}! Please set the inventory variable 'ansible_python_interpreter' to the location of the Python 2.7 binary."
      when: ansible_python.version.major != 2 or ansible_python.version.minor != 7

  tasks:
    - name: Get dependencies for distribution
      include_vars: "{{ item }}"
      with_first_found:
        - files:
            - "vars/{{ ansible_distribution }}-{{ ansible_distribution_version }}.yml"
            - "vars/{{ ansible_distribution }}-{{ ansible_distribution_major_version | int }}.yml"
            - "vars/{{ ansible_distribution }}.yml"
            - "vars/{{ ansible_os_family }}.yml"
          skip: true

    - name: Collate dependencies
      set_fact:
        package_deps: "{{ buildbot_deps + fetch_deps + conf_deps + build_deps + link_deps + dist_deps }}"
        python_modules: "{{ buildbot_modules + rpc_test_modules }}"

    - name: Update rolling release [Arch Linux]
      pacman:
        update_cache: yes
        upgrade: yes
      when: ansible_distribution == 'Archlinux'

    - name: Install required packages
      package:
        name: "{{ item }}"
        state: present
      with_items: "{{ package_deps }}"

    - name: Install pip [CentOS]
      include: tasks/install-pip.yml
      when: ansible_distribution == 'CentOS'

    - name: Install required Python modules
      pip:
        name: "{{ item }}"
        state: latest
      with_items: "{{ python_modules }}"
      notify: restart buildbot-worker

    - name: Set up the Buildbot worker user
      user:
        name: "{{ buildbot_worker_user }}"
        comment: Buildbot worker
        shell: /bin/bash
        state: present

    - name: Create Buildbot worker
      command: >
        buildbot-worker create-worker ~/{{ buildbot_worker_name }}
        {{ buildbot_master_host }}:{{ buildbot_master_port }}
        {{ buildbot_worker_name|quote }} {{ buildbot_worker_password|quote }}
      args:
        creates: "~/{{ buildbot_worker_name }}/buildbot.tac"
      become_user: "{{ buildbot_worker_user }}"

    - name: Set admin details for Buildbot worker
      copy:
        content: "{{ buildbot_worker_admin }}"
        dest: "~{{ buildbot_worker_user }}/{{ buildbot_worker_name }}/info/admin"
        owner: "{{ buildbot_worker_user }}"
        group: "{{ buildbot_worker_user }}"
        mode: "0644"

    - name: Set host details for Buildbot worker
      template:
        src: "{{ buildbot_worker_host_template }}"
        dest: "~{{ buildbot_worker_user }}/{{ buildbot_worker_name }}/info/host"
        owner: "{{ buildbot_worker_user }}"
        group: "{{ buildbot_worker_user }}"
        mode: "0644"

    - name: Copy Buildbot worker systemd service unit
      template:
        src: templates/buildbot-worker.service.j2
        dest: "/etc/systemd/system/buildbot-worker.service"
        owner: root
        group: root
        mode: "0644"
      notify: reload systemd

    - name: Start Buildbot worker.
      service:
        name: buildbot-worker
        state: started
        enabled: yes

  handlers:
    - name: restart buildbot-worker
      service:
        name: buildbot-worker
        state: restarted

    - name: reload systemd
      command: /bin/systemctl daemon-reload
