# create a new loopabull server
- import_playbook: "/srv/web/infra/ansible/playbooks/include/virt-create.yml myhosts=loopabull:loopabull-stg"

- name: make the box be real
  hosts: loopabull:loopabull-stg
  user: root
  gather_facts: True

  vars_files:
   - /srv/web/infra/ansible/vars/global.yml
   - "/srv/private/ansible/vars.yml"
   - "/srv/web/infra/ansible/vars/{{ ansible_distribution }}.yml"

  pre_tasks:
  - import_tasks: "{{ tasks_path }}/yumrepos.yml"

  roles:
  - base
  - rkhunter
  - nagios_client
  - hosts
  - fas_client
  - collectd/base
  - sudo

  tasks:
  - import_tasks: "{{ tasks_path }}/2fa_client.yml"
  - import_tasks: "{{ tasks_path }}/motd.yml"

  handlers:
  - import_tasks: "{{ handlers_path }}/restart_services.yml"

- name: Deploy and configure loopabull
  hosts: loopabull:loopabull-stg
  user: root
  gather_facts: True

  vars_files:
   - /srv/web/infra/ansible/vars/global.yml
   - "/srv/private/ansible/vars.yml"
   - "/srv/web/infra/ansible/vars/{{ ansible_distribution }}.yml"

  handlers:
  - import_tasks: "{{ handlers_path }}/restart_services.yml"

  tasks:
    - name: ensure ~/.ssh dir exists
      file:
        path: "/home/root/.ssh/"
        state: directory
    - name: place loopabull_ociimage user private keys
      copy:
        src: "{{ private }}/files/loopabull/keys/{{ env }}_ociimage"
        dest: "/home/root/.ssh/id_rsa.loopabull_ociimage"
        mode: 0600
    - name: Install required packages
      package:
        name: python-fedmsg-rabbitmq-serializer
        state: latest

  roles:
    - rabbitmq
    - fedmsg/base
    - fedmsg/hub
    - {
      role: loopabull,
        loglevel: info,
        plugin: fedmsgrabbitmq,
        routing_keys: [
          "org.fedoraproject.stg.buildsys.build.state.change",
          "org.fedoraproject.prod.buildsys.build.state.change",
          "org.centos.prod.ci.pipeline.allpackages-pr.complete",
          "org.centos.prod.ci.pipeline.allpackages-pr.package.running",
        ],
        playbooks_dir: /srv/loopabull-tasks/playbooks,
        ansible_cfg_path: /etc/ansible/ansible.cfg,
        playbook_cmd: /usr/bin/ansible-playbook
    }


- name: Post Loopabull install configuration
  hosts: loopabull:loopabull-stg
  user: root
  gather_facts: True

  vars_files:
   - /srv/web/infra/ansible/vars/global.yml
   - "/srv/private/ansible/vars.yml"
   - "/srv/web/infra/ansible/vars/{{ ansible_distribution }}.yml"

  handlers:
  - import_tasks: "{{ handlers_path }}/restart_services.yml"

  tasks:
    - name: Enable fedmsg-rabbitmq-serializer
      copy:
        src: "{{files}}/loopabull/serializer.py"
        dest: "/etc/fedmsg.d/serializer.py"
      notify: restart fedmsg-hub
