---
- name: Gather build logs
  hosts: vps_runner
  gather_facts: false
  vars_files:
    - ./roles/common/vars/main.yml
    - ./roles/common/vars/secrets.yml

  tasks:
    - name: Get droplet info
      community.digitalocean.digital_ocean_droplet_info:
        oauth_token: "{{ DO_API_TOKEN }}"
      delegate_to: localhost
      register: all_droplets

    - name: Set ansible_host to VPS IP for SSH connection
      set_fact:
        ansible_host: "{{ (all_droplets.data | selectattr('name', 'equalto', 'automated-builder-vps') | first).networks.v4 | selectattr('type', 'equalto', 'public') | map(attribute='ip_address') | first }}"

    - name: Gather facts
      setup:

    ## fail_on_missing: false - a failed build often leaves only one
    ## of the two logs on the VPS (e.g. build.log never created because
    ## the build aborted during install_source). Without this, the first
    ## missing src aborts the whole play and we fetch nothing, so the
    ## upload-artifact step has no logs to upload. Grab whatever exists.
    - name: Copy install_source log
      fetch:
        src: "/home/ansible/install_source.log"
        dest: "{{ logs_dir }}/install_source.log"
        flat: yes
        fail_on_missing: false

    - name: Copy build log
      fetch:
        src: "/home/ansible/build.log"
        dest: "{{ logs_dir }}/build.log"
        flat: yes
        fail_on_missing: false
