Skip to content

Commit

Permalink
Sonar upgrade POC - Reordered functions according to python convention
Browse files Browse the repository at this point in the history
  • Loading branch information
linda.nasredin committed Aug 14, 2023
1 parent 255a2da commit 60a3e9c
Showing 1 changed file with 38 additions and 37 deletions.
75 changes: 38 additions & 37 deletions examples/upgrade/python_upgrader/run_preflight_validations.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,25 @@
from decimal import Decimal
from datetime import datetime


def main(target_version):
print("---------------------------------------------------------------------")
time = datetime.now().strftime("%a %b %d %H:%M:%S UTC %Y")
print(f"Running upgrade preflight validations at {time}")
source_version = get_sonar_version()
different_version, min_version_validation_passed, max_version_hop_validation_passed = \
validate_sonar_version(source_version, target_version)
result = {
"different_version": different_version,
"min_version": min_version_validation_passed,
"max_version_hop": max_version_hop_validation_passed
}
result_json_string = json.dumps(result)
# The string "Preflight validations result:" is part of the protocol, if you change it, change its usage
print(f"Preflight validations result: {result_json_string}")
print("---------------------------------------------------------------------")


def get_sonar_version():
jsonar_file_path = "/etc/sysconfig/jsonar"
target_key = "VERSION="
Expand All @@ -24,27 +43,6 @@ def get_sonar_version():
return version


# For example, if version is the string "4.12.0.10.0", returns the number 4.12
def extract_major_version(version):
second_period_index = version.find(".", version.find(".") + 1)
if second_period_index != -1:
major_version_str = version[:second_period_index]
return Decimal(major_version_str)
else:
raise Exception(f"Invalid version format: {version}, must be x.x.x.x.x")


def validate_min_version(source_major_version):
return source_major_version >= 4.10


def validate_max_version_hop(source_major_version, target_major_version):
# TODO handle when 5.x will be released
hop = target_major_version - source_major_version
print(f"Version hop: {hop}")
return hop <= 0.02


def validate_sonar_version(source_version, target_version):
different_version = source_version != target_version
if different_version:
Expand All @@ -64,22 +62,25 @@ def validate_sonar_version(source_version, target_version):
return different_version, min_version_validation_passed, max_version_hop_validation_passed


def main(target_version):
print("---------------------------------------------------------------------")
time = datetime.now().strftime("%a %b %d %H:%M:%S UTC %Y")
print(f"Running upgrade preflight validations at {time}")
source_version = get_sonar_version()
different_version, min_version_validation_passed, max_version_hop_validation_passed = \
validate_sonar_version(source_version, target_version)
result = {
"different_version": different_version,
"min_version": min_version_validation_passed,
"max_version_hop": max_version_hop_validation_passed
}
result_json_string = json.dumps(result)
# The string "Preflight validations result:" is part of the protocol, if you change it, change its usage
print(f"Preflight validations result: {result_json_string}")
print("---------------------------------------------------------------------")
# For example, if version is the string "4.12.0.10.0", returns the number 4.12
def extract_major_version(version):
second_period_index = version.find(".", version.find(".") + 1)
if second_period_index != -1:
major_version_str = version[:second_period_index]
return Decimal(major_version_str)
else:
raise Exception(f"Invalid version format: {version}, must be x.x.x.x.x")


def validate_min_version(source_major_version):
return source_major_version >= 4.10


def validate_max_version_hop(source_major_version, target_major_version):
# TODO handle when 5.x will be released
hop = target_major_version - source_major_version
print(f"Version hop: {hop}")
return hop <= 0.02


if __name__ == "__main__":
Expand Down

0 comments on commit 60a3e9c

Please sign in to comment.