
---
- name: Set SSH tunel
  command: ssh -o StrictHostKeyChecking=no -f -N -L localhost:9390:scans.cloud.infn.it:9390 jenkins@scans.cloud.infn.it 

- debug: 
    var: paas_ci_test.endpoints_to_scan

- name: Copy scan script
  copy: 
    src: files/scan.py
    dest: "{{ paas_ci_scan_script_path }}"

- name: Copy utilities module
  copy: 
    src: files/utilities.py
    dest: "{{ paas_ci_scan_script_path }}"

- name: Copy gvm_library module
  copy: 
    src: files/gvm_library.py
    dest: "{{ paas_ci_scan_script_path }}"

- name: Adding execution permission to scan script
  file: 
    dest: "{{ paas_ci_scan_script_path }}/scan.py"
    mode: a+x 

- name:  Run scan
  command: "{{ paas_ci_scan_script_path }}/scan.py --endpoint-keys {{ paas_ci_test.endpoints_to_scan }} --dep-json {{ paas_ci_workdir + '/dep.json'}} --output-dir {{ paas_ci_workdir }}" 
  register: scan_output

- name: Show scan output
  debug:
    msg: "{{ scan_output }}"

- name: Find report files
  find:
    paths: "{{paas_ci_workdir}}"
    patterns: '*infn.it-report.txt'
  register: report_files

- name: read summary report file
  shell: cat "{{ paas_ci_workdir + '/summary-report.json'}}"
  register: summary_report 

- name: import summary_report
  set_fact:
    summary_report_json: "{{ summary_report.stdout | from_json }}"

- name: Show reports
  debug:
    msg: "{{lookup('file', item.path)}}"
  with_items: "{{report_files.files}}"
  when: summary_report_json.global == 'NOK'

- name: Load scans log
  command: cat "{{ paas_ci_workdir + '/scans.log'}}"
  register: scans_log
  when: summary_report_json.global == 'NOK'

- name: import scans_log
  set_fact:
    imported_scans_log: "{{ scans_log.stdout }}"

- name: Show scans log
  debug:
    var: imported_scans_log
  when: summary_report_json.global == 'NOK'

- name: Detailed report summary
  debug:
    var: summary_report_json

- name: Report Summary
  assert:
    that:
      - summary_report_json.global == 'OK'
    fail_msg: "SCAN FAILED - deployment: {{ summary_report_json.deployment }}"
    success_msg: "SCAN PASSED - deployment: {{ summary_report_json.deployment }}"