Skip to content

Commit

Permalink
Merge upgrade and preupgrade script, add sync script for rhc devel wo…
Browse files Browse the repository at this point in the history
…rker
  • Loading branch information
Peter Zdravecký committed Jan 16, 2024
1 parent 7bd3516 commit 1346d74
Show file tree
Hide file tree
Showing 39 changed files with 378 additions and 793 deletions.
73 changes: 73 additions & 0 deletions misc/sync_scripts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import os
import ruamel.yaml

# Scripts located in this project
SCRIPT_PATH = "scripts/leapp_script.py"

# Yaml playbooks in rhc-worker-script
PRE_UPGRADE_YAML_PATH = os.path.join(
"..", "rhc-worker-script/development/nginx/data/leapp_preupgrade.yml"
)
UPGRADE_YAML_PATH = os.path.join(
"..", "rhc-worker-script/development/nginx/data/leapp_upgrade.yml"
)

DEFAULT_YAML_ENVELOPE = """
- name: LEAPP
vars:
insights_signature: |
ascii_armored gpg signature
insights_signature_exclude: /vars/insights_signature,/vars/content_vars
interpreter: /usr/bin/python
content: |
placeholder
content_vars:
# variables that will be handed to the script as environment vars
# will be prefixed with RHC_WORKER_*
LEAPP_SCRIPT_TYPE: type
"""


def _get_updated_yaml_content(yaml_path, script_path):
if not os.path.exists(yaml_path):
yaml = ruamel.yaml.YAML()
config = yaml.load(DEFAULT_YAML_ENVELOPE)
mapping = 2
offset = 0
else:
config, mapping, offset = ruamel.yaml.util.load_yaml_guess_indent(
open(yaml_path)
)
print(mapping, offset)

with open(script_path) as script:
content = script.read()

script_type = "PREUPGRADE" if "preupgrade" in yaml_path else "UPGRADE"
config[0]["name"] = "LEAPP %s" % script_type.title()
config[0]["vars"]["content"] = content
config[0]["vars"]["content_vars"]["LEAPP_SCRIPT_TYPE"] = script_type
return config, mapping, offset


def _write_content(config, path, mapping=None, offset=None):
yaml = ruamel.yaml.YAML()
if mapping and offset:
yaml.indent(mapping=mapping, sequence=mapping, offset=offset)
with open(path, "w") as handler:
yaml.dump(config, handler)


def main():
config, mapping, offset = _get_updated_yaml_content(
PRE_UPGRADE_YAML_PATH, SCRIPT_PATH
)
print("Writing new content to %s" % PRE_UPGRADE_YAML_PATH)
_write_content(config, PRE_UPGRADE_YAML_PATH, mapping, offset)
config, mapping, offset = _get_updated_yaml_content(UPGRADE_YAML_PATH, SCRIPT_PATH)
print("Writing new content to %s" % UPGRADE_YAML_PATH)
_write_content(config, UPGRADE_YAML_PATH, mapping, offset)


if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ jsonschema==3.2.0
mock==3.0.5
pytest==4.6.11
pytest-cov==2.12.1
ruamel.yaml==0.16.13
File renamed without changes.
69 changes: 0 additions & 69 deletions schemas/upgrade_schema_1.0.json

This file was deleted.

68 changes: 44 additions & 24 deletions scripts/leapp_upgrade.py → scripts/leapp_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
import os
import subprocess


# SCRIPT_TYPE is either 'PREUPGRADE' or 'UPGRADE'
# Value is set in signed yaml envelope in content_vars (RHC_WORKER_LEAPP_SCRIPT_TYPE)
SCRIPT_TYPE = os.environ.get("RHC_WORKER_LEAPP_SCRIPT_TYPE", "None")
IS_UPGRADE = SCRIPT_TYPE == "UPGRADE"
IS_PREUPGRADE = SCRIPT_TYPE == "PREUPGRADE"
JSON_REPORT_PATH = "/var/log/leapp/leapp-report.json"
TXT_REPORT_PATH = "/var/log/leapp/leapp-report.txt"
REBOOT_GUIDANCE_MESSAGE = "A reboot is required to continue. Please reboot your system."
Expand Down Expand Up @@ -97,11 +101,21 @@ def get_rhel_version():


def is_non_eligible_releases(release):
print("Exit if not RHEL 7.9 or 8.4")
eligible_releases = ["7.9", "8.4"]
"""Check if the release is eligible for upgrade or preupgrade."""
not_eligable_condition = False
major_version, minor = release.split(".") if release is not None else (None, None)
version_str = major_version + "." + minor
return release is None or version_str not in eligible_releases
if IS_UPGRADE:
print("Exit if not RHEL 7.9 or 8.4")
eligible_releases = ["7.9", "8.4"]
version_str = major_version + "." + minor
not_eligable_condition = version_str not in eligible_releases
elif IS_PREUPGRADE:
print("Exit if not RHEL 7 or RHEL 8 ...")
major_version, _ = release.split(".") if release is not None else (None, None)
not_eligable_condition = major_version not in ["7", "8"]
print(not_eligable_condition)

return release is None or not_eligable_condition


# Code taken from
Expand Down Expand Up @@ -248,7 +262,11 @@ def should_use_no_rhsm_check(rhui_installed, command):
)

if rhui_installed and not rhsm_repo_check_fail:
print("RHUI packages detected, adding --no-rhsm flag to preupgrade command")
print(
"RHUI packages detected, adding --no-rhsm flag to {} command".format(
SCRIPT_TYPE.title()
)
)
command.append("--no-rhsm")
return True
return False
Expand All @@ -270,7 +288,7 @@ def install_leapp_pkg_corresponding_to_installed_rhui(rhui_pkgs):


def remove_previous_reports():
print("Removing previous preupgrade reports at /var/log/leapp/leapp-report.* ...")
print("Removing previous leapp reports at /var/log/leapp/leapp-report.* ...")

if os.path.exists(JSON_REPORT_PATH):
os.remove(JSON_REPORT_PATH)
Expand All @@ -279,20 +297,12 @@ def remove_previous_reports():
os.remove(TXT_REPORT_PATH)


def execute_upgrade(command):
print("Executing upgrade ...")
def execute_operation(command):
print("Executing {} ...".format(SCRIPT_TYPE.title()))
output, _ = run_subprocess(command)

return output

# NOTE: we do not care about returncode because non-null always means actor error (or leapp error)
# if returncode:
# print(
# "The process leapp exited with code '%s' and output: %s\n"
# % (returncode, output)
# )
# raise ProcessError(message="Leapp exited with code '%s'." % returncode)


def _find_highest_report_level(entries):
"""
Expand All @@ -311,7 +321,7 @@ def _find_highest_report_level(entries):


def parse_results(output, reboot_required=False):
print("Processing upgrade results ...")
print("Processing {} results ...".format(SCRIPT_TYPE.title()))

report_json = "Not found"
message = "Can't open json report at " + JSON_REPORT_PATH
Expand Down Expand Up @@ -358,7 +368,7 @@ def parse_results(output, reboot_required=False):
def update_insights_inventory():
"""Call insights-client to update insights inventory."""
print("Updating system status in Red Hat Insights.")
output, returncode = run_subprocess(["/usr/bin/insights-client"])
output, returncode = run_subprocess(cmd=["/usr/bin/insights-client"])

if returncode:
raise ProcessError(
Expand All @@ -377,7 +387,15 @@ def reboot_system():

def main():
try:
# Exit if not RHEL 7.9 or 8.4
# Exit if invalid value for SCRIPT_TYPE
if SCRIPT_TYPE not in ["PREUPGRADE", "UPGRADE"]:
raise ProcessError(
message="Allowed values for RHC_WORKER_LEAPP_SCRIPT_TYPE are 'PREUPGRADE' and 'UPGRADE'.",
report="Exiting because RHC_WORKER_LEAPP_SCRIPT_TYPE='%s'"
% SCRIPT_TYPE,
)

# Exit if not eligible release
dist, version = get_rhel_version()
if dist != "rhel" or is_non_eligible_releases(version):
raise ProcessError(
Expand All @@ -387,20 +405,22 @@ def main():
)

output = OutputCollector()
preupgrade_command = ["/usr/bin/leapp", "preupgrade", "--report-schema=1.1.0"]
upgrade_command = ["/usr/bin/leapp", "upgrade", "--report-schema=1.1.0"]
operation_command = preupgrade_command if IS_PREUPGRADE else upgrade_command
rhui_pkgs = setup_leapp(version)

# Check for RHUI PKGs
use_no_rhsm = should_use_no_rhsm_check(len(rhui_pkgs) > 1, upgrade_command)
use_no_rhsm = should_use_no_rhsm_check(len(rhui_pkgs) > 1, operation_command)
if use_no_rhsm:
install_leapp_pkg_corresponding_to_installed_rhui(rhui_pkgs)

remove_previous_reports()
leapp_upgrade_output = execute_upgrade(upgrade_command)
reboot_required = REBOOT_GUIDANCE_MESSAGE in leapp_upgrade_output
leapp_output = execute_operation(operation_command)
reboot_required = REBOOT_GUIDANCE_MESSAGE in leapp_output
parse_results(output, reboot_required)
update_insights_inventory()
print("Leapp upgrade command successfully executed.")
print("Operation {} finished successfully.".format(SCRIPT_TYPE.title()))
if reboot_required:
reboot_system()
except ProcessError as exception:
Expand Down
File renamed without changes.
13 changes: 0 additions & 13 deletions tests/preupgrade/test_check_pkg_installed.py

This file was deleted.

57 changes: 0 additions & 57 deletions tests/preupgrade/test_collect_report_level.py

This file was deleted.

20 changes: 0 additions & 20 deletions tests/preupgrade/test_get_rhel_version.py

This file was deleted.

Loading

0 comments on commit 1346d74

Please sign in to comment.