---
- hosts: all
  tasks:
    - name: Create log directory to pull
      file:
        path: '{{ zuul.project.src_dir }}/logs'
        state: directory

    - name: Prepare the logs into user space (Ubuntu)
      shell: "egrep {{ item }} /var/log/syslog > {{ zuul.project.src_dir }}/logs/{{ item }}.log"
      with_items:
        - "account-server"
        - "container-server"
        - "object-server"
        - "proxy-server"
        - "ansible"
        - "storlets-daemon"
        - "daemon-factory"
      become: yes
      when:
        - ansible_os_family == 'Debian'

    - name: Prepare the logs into user space (CentOS)
      shell: "egrep {{ item }} /var/log/messages > {{ zuul.project.src_dir }}/logs/{{ item }}.log"
      with_items:
        - "account-server"
        - "container-server"
        - "object-server"
        - "proxy-server"
        - "ansible"
        - "storlets-daemon"
        - "daemon-factory"
      become: yes
      when:
        - ansible_os_family == 'RedHat'

    - name: Change log permisson as readable
      file:
        path: "{{ zuul.project.src_dir }}/logs/{{ item }}.log"
        mode: 0644
      with_items:
        - "account-server"
        - "container-server"
        - "object-server"
        - "proxy-server"
        - "ansible"
        - "storlets-daemon"
        - "daemon-factory"

    - name: Copy logs from worker nodes to executor node
      synchronize:
        # TODO: make it sort of tox_envdir
        src: '{{ zuul.project.src_dir }}/logs'
        dest: '{{ zuul.executor.log_root }}'
        mode: pull
        copy_links: true
        verify_host: true
      ignore_errors: yes

    - name: Copy conf files from worker nodes to executor node
      synchronize:
        src: '/etc/swift'
        dest: '{{ zuul.executor.log_root }}'
        mode: pull
        copy_links: true
        verify_host: true
      ignore_errors: yes
