|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright (c) IBM Corporation 2022, 2025 |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +# See the License for the specific language governing permissions and |
| 11 | +# limitations under the License. |
| 12 | + |
| 13 | + |
| 14 | +from __future__ import absolute_import, division, print_function |
| 15 | + |
| 16 | +import re |
| 17 | + |
| 18 | +__metaclass__ = type |
| 19 | + |
| 20 | + |
| 21 | +def get_zoau_version(ansible_zos_module): |
| 22 | + """ |
| 23 | + Fetches the current ZOAU version from the z/OS system using the `zoaversion` command. |
| 24 | +
|
| 25 | + This function runs the `zoaversion` command on the z/OS system and parses the output |
| 26 | + to extract the version number in the format `vX.Y.Z.W`, where X, Y, Z, and W are numerical digits. |
| 27 | +
|
| 28 | + Parameters |
| 29 | + ---------- |
| 30 | + ansible_zos_module : ansible.module_utils.basic.AnsibleModule |
| 31 | + The Ansible z/OS module that provides the `all.shell()` method to run shell commands. |
| 32 | +
|
| 33 | + Returns |
| 34 | + ------- |
| 35 | + str |
| 36 | + The ZOAU version number (e.g., "1.2.3.4") extracted from the command output. |
| 37 | + Returns `"1.2.0"` if no valid version is found. |
| 38 | + """ |
| 39 | + cmd_str = "zoaversion" |
| 40 | + version_results = ansible_zos_module.all.shell(cmd=cmd_str) |
| 41 | + zoa_version = "0.0.0" # Default version if not found |
| 42 | + |
| 43 | + # Iterate through all contacted hosts and check the result output |
| 44 | + for result in version_results.contacted.values(): |
| 45 | + output = result.get("stdout") |
| 46 | + if output: |
| 47 | + # Search for the version in the format "vX.Y.Z.W" |
| 48 | + match = re.search(r'v?(\d+\.\d+\.\d+(?:\.\d+)?)', output) |
| 49 | + if match: |
| 50 | + zoa_version = match.group(1) |
| 51 | + |
| 52 | + return zoa_version |
| 53 | + |
| 54 | +def is_zoau_version_higher_than(ansible_zos_module,min_version_str): |
| 55 | + """Reports back if ZOAU version is high enough. |
| 56 | +
|
| 57 | + Parameters |
| 58 | + ---------- |
| 59 | + min_version_str : str |
| 60 | + The minimal desired ZOAU version '#.#.#'. |
| 61 | +
|
| 62 | + Returns |
| 63 | + ------- |
| 64 | + bool |
| 65 | + Whether ZOAU version found was high enough. |
| 66 | + """ |
| 67 | + if is_valid_version_string(min_version_str): |
| 68 | + # check zoau version on system (already a list) |
| 69 | + system_version_list = get_zoau_version_str(ansible_zos_module) |
| 70 | + |
| 71 | + # convert input to list format |
| 72 | + min_version_list = min_version_str.split('.') |
| 73 | + |
| 74 | + # convert list of strs to list of ints |
| 75 | + system_version_list = [int(i) for i in system_version_list] |
| 76 | + min_version_list = [int(i) for i in min_version_list] |
| 77 | + |
| 78 | + # compare major |
| 79 | + if system_version_list[0] > min_version_list[0]: |
| 80 | + return True |
| 81 | + if system_version_list[0] < min_version_list[0]: |
| 82 | + return False |
| 83 | + |
| 84 | + # majors are equal, compare minor |
| 85 | + if system_version_list[1] > min_version_list[1]: |
| 86 | + return True |
| 87 | + if system_version_list[1] < min_version_list[1]: |
| 88 | + return False |
| 89 | + |
| 90 | + # majors and minors are equal, compare patch |
| 91 | + if system_version_list[2] > min_version_list[2]: |
| 92 | + return True |
| 93 | + if system_version_list[2] < min_version_list[2]: |
| 94 | + return False |
| 95 | + |
| 96 | + # check for a 4th level if system and min_version provided it |
| 97 | + if len(system_version_list) < 4: |
| 98 | + system_version_list.append(0) |
| 99 | + if len(min_version_list) < 4: |
| 100 | + min_version_list.append(0) |
| 101 | + |
| 102 | + # return result of comparison of 4th levels. |
| 103 | + return system_version_list[3] >= min_version_list[3] |
| 104 | + |
| 105 | + # invalid version string |
| 106 | + return False |
| 107 | + |
| 108 | + |
| 109 | +def is_valid_version_string(version_str): |
| 110 | + """Reports back if string is in proper version format. Expected format is a |
| 111 | + series of numbers (major) followed by a dot(.) followed by another |
| 112 | + series of numbers (minor) followed by another dot(.) followed by a |
| 113 | + series of numbers (patch) i.e. "#.#.#" where '#' can be any integer. |
| 114 | + There is a provision for a 4th level to this eg "v1.2.0.1". |
| 115 | +
|
| 116 | + Parameters |
| 117 | + ---------- |
| 118 | + min_version_str : str |
| 119 | + String to be verified is in correct format. |
| 120 | +
|
| 121 | + Returns |
| 122 | + ------- |
| 123 | + bool |
| 124 | + Whether provided str is in correct format. |
| 125 | + """ |
| 126 | + |
| 127 | + # split string into [major, minor, patch] |
| 128 | + version_list = version_str.split('.') |
| 129 | + |
| 130 | + # check each element in list isnumeric() |
| 131 | + for ver in version_list: |
| 132 | + if not ver.isnumeric(): |
| 133 | + return False |
| 134 | + |
| 135 | + return True |
| 136 | + |
| 137 | +def get_zoau_version_str(ansible_zos_module): |
| 138 | + """Attempts to call zoaversion on target and parses out version string. |
| 139 | +
|
| 140 | + Returns |
| 141 | + ------- |
| 142 | + Union[int, int, int] |
| 143 | + ZOAU version found in format [#,#,#]. There is a |
| 144 | + provision for a 4th level eg "v1.2.0.1". |
| 145 | + """ |
| 146 | + ZOAU_API_VERSION = get_zoau_version(ansible_zos_module) |
| 147 | + version_list = ( |
| 148 | + ZOAU_API_VERSION.split('.') |
| 149 | + ) |
| 150 | + |
| 151 | + return version_list |
0 commit comments