- hosts: all
  vars:
    zuul_log_aws_access_key: minioadmin
    zuul_log_aws_secret_key: minioadmin
    zuul_log_bucket: zuul
    test_content: "123abc"
    _undocumented_test_worker_node_: "{{ inventory_hostname }}"
  roles:
    - ensure-docker
    - ensure-pip
  tasks:
    - name: Install boto
      pip:
        extra_args: "--break-system-packages"
        name:
          - boto3
          - botocore

    - name: Start minio server
      command: >-
        docker run -d -p 9000:9000
        -e MINIO_ACCESS_KEY={{ zuul_log_aws_access_key }}
        -e MINIO_SECRET_KEY={{ zuul_log_aws_secret_key }}
        quay.io/minio/minio server /data

    - name: Make sure bucket exists
      s3_bucket:
        name: "{{ zuul_log_bucket }}"
        state: present
        s3_url: 'http://localhost:9000'
        aws_access_key: "{{ zuul_log_aws_access_key }}"
        aws_secret_key: "{{ zuul_log_aws_secret_key }}"

    - name: Create Logdir
      become: true
      file:
        state: directory
        path: "{{ zuul.executor.log_root }}"
        mode: "0755"
        group: "zuul"
        owner: "zuul"
      register: fake_zuul_logdir

    - name: Add content to tempfile
      copy:
        content: "{{ test_content }}"
        dest: "{{ fake_zuul_logdir.path }}/testfile"

    - name: Upload file to s3
      include_role:
        name: upload-logs-s3
      vars:
        upload_logs_s3_endpoint: 'http://localhost:9000'

    - name: Download mc
      get_url:
        url: https://dl.min.io/client/mc/release/linux-amd64/mc
        dest: "{{ fake_zuul_logdir.path }}/mc"
        mode: 0755

    - name: Add localhost minio host
      command: "{{ fake_zuul_logdir.path }}/mc config host add local http://localhost:9000 {{ zuul_log_aws_access_key }} {{ zuul_log_aws_secret_key }}"

    - name: List files in minio bucket
      command: "{{ fake_zuul_logdir.path }}/mc find local/zuul"

    - name: Check for testfile in minio bucket
      command: "{{ fake_zuul_logdir.path }}/mc find local/zuul/{{ zuul_log_path }}/testfile"