From a3b229a90834d7f093d1d3a9e265eaa24172337d Mon Sep 17 00:00:00 2001 From: Andrea Waltlova Date: Sat, 3 Feb 2024 09:18:55 +0100 Subject: [PATCH] Distinguish severities in ansible playbooks and improve message output Signed-off-by: Andrea Waltlova --- playbooks/leapp_preupgrade_ansible.yml | 39 ++++++++++++++++++++++++-- playbooks/leapp_preupgrade_script.yaml | 5 ++-- playbooks/leapp_upgrade_ansible.yml | 23 +++++++++++++-- playbooks/leapp_upgrade_script.yaml | 5 ++-- scripts/leapp_script.py | 5 ++-- tests/test_parse_results.py | 2 +- 6 files changed, 64 insertions(+), 15 deletions(-) diff --git a/playbooks/leapp_preupgrade_ansible.yml b/playbooks/leapp_preupgrade_ansible.yml index 51f3e8b..d76371c 100644 --- a/playbooks/leapp_preupgrade_ansible.yml +++ b/playbooks/leapp_preupgrade_ansible.yml @@ -174,6 +174,10 @@ ansible.builtin.set_fact: report_content: "{{ report_content_raw.content | b64decode }}" + - name: Set total problems count + ansible.builtin.set_fact: + total_problems_count: "{{ report_content.entries | length }}" + - name: Set inhibitor count ansible.builtin.set_fact: inhibitor_count: "{{ report_content.entries | selectattr('groups', 'defined') | selectattr('groups', 'contains', 'inhibitor') | list | length }}" @@ -182,12 +186,43 @@ ansible.builtin.set_fact: error_count: "{{ report_content.entries | selectattr('groups', 'defined') | selectattr('groups', 'contains', 'error') | list | length }}" + - name: Transform severities in leapp report to distinguish errors and inhibitors from other high risks entries + set_fact: + report_content_edited: >- + {{ + { + "entries": report_content.entries | default([]) | map( + lambda entry: entry | combine( + {"severity": "error"} if "error" in entry.groups else + {"severity": "inhibitor"} if "inhibitor" in entry.groups else {} + ) + ) | list + } + }} + + - name: Set message if inhibitors or errors present + set_fact: + message: >- + "The upgrade cannot proceed. + Your system has {{ error_count }} error{{ 's' if error_count != 1 else '' }} + and {{ inhibitor_count }} inhibitor{{ 's' if inhibitor_count != 1 else '' }} + out of {{ total_problems_count }} potential problems." + when: inhibitor_count != 0 or error_count != 0 + + + - name: Set message if no inhibitors and no errors present + set_fact: + message: >- + {{ 'No problems found. The system is ready for upgrade.' if total_problems_count == 0 + else 'The upgrade can proceed. However, there is one or more warnings about issues that might occur after the upgrade.'}} + when: inhibitor_count == 0 and error_count == 0 + - name: Set result ansible.builtin.set_fact: task_results: - report_json: "{{ report_content }}" + report_json: "{{ report_content_edited }}" report: "{{ report_content_txt_raw.content | b64decode }}" - message: "Your system has {{ error_count }} errors and {{ inhibitor_count }} inhibitors out of {{report_content.entries | length}} potential problems." + message: "{{ message }}" alert: "{{ (inhibitor_count | int > 0) or (error_count | int > 0) }}" - name: Start insights-client for immediate data collection of leapp-report diff --git a/playbooks/leapp_preupgrade_script.yaml b/playbooks/leapp_preupgrade_script.yaml index b3a27a9..55584c0 100644 --- a/playbooks/leapp_preupgrade_script.yaml +++ b/playbooks/leapp_preupgrade_script.yaml @@ -360,10 +360,9 @@ ) ) if reboot_required: - problem_info = message.lstrip("Your system has ").rstrip(".") message = ( - "System will be upgraded (%s). Rebooting system in 1 minute." - % problem_info + "System will be upgraded. Rebooting system in 1 minute. " + "After reboot check inventory to verify the system is registered with new RHEL major version." ) alert = inhibitor_count > 0 or error_count > 0 status = ( diff --git a/playbooks/leapp_upgrade_ansible.yml b/playbooks/leapp_upgrade_ansible.yml index 2475715..4912344 100644 --- a/playbooks/leapp_upgrade_ansible.yml +++ b/playbooks/leapp_upgrade_ansible.yml @@ -176,16 +176,33 @@ ansible.builtin.set_fact: report_content: "{{ report_content_raw.content | b64decode }}" + - name: Transform severities in leapp report to distinguish errors and inhibitors from other high risks entries + set_fact: + report_content_edited: >- + {{ + { + "entries": report_content.entries | default([]) | map( + lambda entry: entry | combine( + {"severity": "error"} if "error" in entry.groups else + {"severity": "inhibitor"} if "inhibitor" in entry.groups else {} + ) + ) | list + } + }} + - name: Prepare final result block: - name: Set default fail message set_fact: - message: "Upgrade of your system failed. Please check the /var/log/leapp/leapp-report.json and /var/log/rhc-worker-playbook/ansible logs for details." + message: >- + "The upgrade cannot proceed. You must resolve existing issues to perform the upgrade. + Please check the /var/log/leapp/leapp-report.json and /var/log/rhc-worker-playbook/ansible logs for details." - name: Set message if leapp upgrade succeeded set_fact: - message: "Upgrade ran successfully. System has now been rebooted. If everything works fine, the system is running the new RHEL version after reboot. Check inventory to verify the system is\ - \ in new major version." + message: >- + "No problems found. The system will be upgraded. Rebooting system in 1 minute. + After reboot check inventory to verify the RHEL system is registered with new major version." when: is_leapp_upgrade_successful is true - name: Set report content as empty if upgrade was successful diff --git a/playbooks/leapp_upgrade_script.yaml b/playbooks/leapp_upgrade_script.yaml index 33b7b02..018da9d 100644 --- a/playbooks/leapp_upgrade_script.yaml +++ b/playbooks/leapp_upgrade_script.yaml @@ -360,10 +360,9 @@ ) ) if reboot_required: - problem_info = message.lstrip("Your system has ").rstrip(".") message = ( - "System will be upgraded (%s). Rebooting system in 1 minute." - % problem_info + "System will be upgraded. Rebooting system in 1 minute. " + "After reboot check inventory to verify the system is registered with new RHEL major version." ) alert = inhibitor_count > 0 or error_count > 0 status = ( diff --git a/scripts/leapp_script.py b/scripts/leapp_script.py index 06cd40f..f6b10f9 100644 --- a/scripts/leapp_script.py +++ b/scripts/leapp_script.py @@ -353,10 +353,9 @@ def parse_results(output, reboot_required=False): ) ) if reboot_required: - problem_info = message.lstrip("Your system has ").rstrip(".") message = ( - "System will be upgraded (%s). Rebooting system in 1 minute." - % problem_info + "System will be upgraded. Rebooting system in 1 minute. " + "After reboot check inventory to verify the system is registered with new RHEL major version." ) alert = inhibitor_count > 0 or error_count > 0 status = ( diff --git a/tests/test_parse_results.py b/tests/test_parse_results.py index 59526d8..f247104 100644 --- a/tests/test_parse_results.py +++ b/tests/test_parse_results.py @@ -70,7 +70,7 @@ def test_gather_report_files_exist_with_reboot(mock_find_level, mock_exists): assert output.report_json.get("test") == "hi" assert ( output.message - == "System will be upgraded (0 errors and 0 inhibitors out of 0 potential problems). Rebooting system in 1 minute." + == "System will be upgraded. Rebooting system in 1 minute. After reboot check inventory to verify the system is registered with new RHEL major version." )