Michael Walsh | fdc5ced | 2017-08-17 13:15:15 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | r""" |
| 4 | Companion file to utils.robot. |
| 5 | """ |
| 6 | |
| 7 | import gen_print as gp |
| 8 | import gen_robot_keyword as grk |
Michael Walsh | f880ac6 | 2017-11-10 11:29:37 -0600 | [diff] [blame] | 9 | import bmc_ssh_utils as bsu |
| 10 | import var_funcs as vf |
Michael Walsh | fdc5ced | 2017-08-17 13:15:15 -0500 | [diff] [blame] | 11 | from robot.libraries.BuiltIn import BuiltIn |
Michael Walsh | f880ac6 | 2017-11-10 11:29:37 -0600 | [diff] [blame] | 12 | from robot.libraries import DateTime |
| 13 | try: |
| 14 | from robot.utils import DotDict |
| 15 | except ImportError: |
| 16 | pass |
| 17 | import collections |
Michael Walsh | fdc5ced | 2017-08-17 13:15:15 -0500 | [diff] [blame] | 18 | |
| 19 | |
Michael Walsh | fdc5ced | 2017-08-17 13:15:15 -0500 | [diff] [blame] | 20 | def set_power_policy_method(): |
Michael Walsh | fdc5ced | 2017-08-17 13:15:15 -0500 | [diff] [blame] | 21 | r""" |
| 22 | Set the global bmc_power_policy_method to either 'Old' or 'New'. |
| 23 | |
| 24 | The power policy data has moved from an 'org' location to an 'xyz' |
| 25 | location. This keyword will determine whether the new method of getting |
| 26 | the power policy is valid and will set the global bmc_power_policy_method |
| 27 | variable accordingly. If power_policy_setup is already set (by a prior |
| 28 | call to this function), this keyword will simply return. |
| 29 | |
| 30 | If bmc_power_policy_method is "Old", this function will adjust the global |
| 31 | policy variables from data/variables.py: RESTORE_LAST_STATE, |
| 32 | ALWAYS_POWER_ON, ALWAYS_POWER_OFF. |
| 33 | """ |
| 34 | |
| 35 | # Retrieve global variables. |
| 36 | power_policy_setup = \ |
| 37 | int(BuiltIn().get_variable_value("${power_policy_setup}", |
| 38 | default=0)) |
| 39 | bmc_power_policy_method = \ |
| 40 | BuiltIn().get_variable_value("${bmc_power_policy_method}", |
| 41 | default=0) |
| 42 | gp.dpvar(power_policy_setup) |
| 43 | |
| 44 | # If this function has already been run once, we need not continue. |
| 45 | if power_policy_setup: |
| 46 | return |
| 47 | |
| 48 | gp.dpvar(bmc_power_policy_method, 1) |
| 49 | |
| 50 | # The user has not set bmc_power_policy_method via a -v parm so we will |
| 51 | # determine what it should be. |
| 52 | if bmc_power_policy_method == "": |
| 53 | status, ret_values = grk.run_key_u("New Get Power Policy", ignore=1) |
| 54 | if status == 'PASS': |
| 55 | bmc_power_policy_method = 'New' |
| 56 | else: |
| 57 | bmc_power_policy_method = 'Old' |
| 58 | |
| 59 | gp.qpvar(bmc_power_policy_method) |
| 60 | # For old style, we will rewrite these global variable settings to old |
| 61 | # values. |
| 62 | if bmc_power_policy_method == "Old": |
| 63 | BuiltIn().set_global_variable("${RESTORE_LAST_STATE}", |
| 64 | "RESTORE_LAST_STATE") |
| 65 | BuiltIn().set_global_variable("${ALWAYS_POWER_ON}", |
| 66 | "ALWAYS_POWER_ON") |
| 67 | BuiltIn().set_global_variable("${ALWAYS_POWER_OFF}", |
| 68 | "ALWAYS_POWER_OFF") |
| 69 | |
| 70 | # Set global variables to control subsequent calls to this function. |
| 71 | BuiltIn().set_global_variable("${bmc_power_policy_method}", |
| 72 | bmc_power_policy_method) |
| 73 | BuiltIn().set_global_variable("${power_policy_setup}", 1) |
| 74 | |
| 75 | |
Michael Walsh | fdc5ced | 2017-08-17 13:15:15 -0500 | [diff] [blame] | 76 | def translate_power_policy_value(policy): |
Michael Walsh | fdc5ced | 2017-08-17 13:15:15 -0500 | [diff] [blame] | 77 | r""" |
| 78 | Translate the policy value and return the result. |
| 79 | |
| 80 | Using old style functions, callers might call like this with a hard- |
| 81 | code value for policy: |
| 82 | |
George Keishing | efc3ff2 | 2017-12-12 11:49:25 -0600 | [diff] [blame] | 83 | Set BMC Power Policy ALWAYS_POWER_OFF |
Michael Walsh | fdc5ced | 2017-08-17 13:15:15 -0500 | [diff] [blame] | 84 | |
| 85 | This function will get the value of the corresponding global variable (if |
| 86 | it exists) and return it. |
| 87 | |
| 88 | This will allow the old style call to still work on systems using the new |
| 89 | method of storing the policy value. |
| 90 | """ |
| 91 | |
| 92 | valid_power_policy_vars = \ |
| 93 | BuiltIn().get_variable_value("${valid_power_policy_vars}") |
| 94 | |
| 95 | if policy not in valid_power_policy_vars: |
| 96 | return policy |
| 97 | |
| 98 | status, ret_values = grk.run_key_u("Get Variable Value ${" + policy + "}", |
| 99 | quiet=1) |
| 100 | return ret_values |
| 101 | |
Michael Walsh | f880ac6 | 2017-11-10 11:29:37 -0600 | [diff] [blame] | 102 | |
Michael Walsh | f880ac6 | 2017-11-10 11:29:37 -0600 | [diff] [blame] | 103 | def get_bmc_date_time(): |
Michael Walsh | f880ac6 | 2017-11-10 11:29:37 -0600 | [diff] [blame] | 104 | r""" |
| 105 | Get date/time info from BMC and return as a dictionary. |
| 106 | |
| 107 | Example of dictionary data returned by this keyword. |
| 108 | time_dict: |
| 109 | [local_time]: Fri 2017-11-03 152756 UTC |
| 110 | [local_time_seconds]: 1509740876 |
| 111 | [universal_time]: Fri 2017-11-03 152756 UTC |
| 112 | [universal_time_seconds]: 1509740876 |
| 113 | [rtc_time]: Fri 2016-05-20 163403 |
| 114 | [rtc_time_seconds]: 1463780043 |
| 115 | [time_zone]: n/a (UTC, +0000) |
| 116 | [network_time_on]: yes |
| 117 | [ntp_synchronized]: no |
| 118 | [rtc_in_local_tz]: no |
| 119 | """ |
| 120 | |
| 121 | out_buf, stderr, rc = bsu.bmc_execute_command('timedatectl') |
| 122 | # Example of output returned by call to timedatectl: |
| 123 | # Local time: Fri 2017-11-03 15:27:56 UTC |
| 124 | # Universal time: Fri 2017-11-03 15:27:56 UTC |
| 125 | # RTC time: Fri 2016-05-20 16:34:03 |
| 126 | # Time zone: n/a (UTC, +0000) |
| 127 | # Network time on: yes |
| 128 | # NTP synchronized: no |
| 129 | # RTC in local TZ: no |
| 130 | |
| 131 | # Convert the out_buf to a dictionary. |
| 132 | initial_time_dict = vf.key_value_outbuf_to_dict(out_buf) |
| 133 | |
| 134 | # For each "_time" entry in the dictionary, we will create a corresponding |
| 135 | # "_time_seconds" entry. We create a new dictionary so that the entries |
| 136 | # are kept in a nice order for printing. |
| 137 | try: |
| 138 | result_time_dict = collections.OrderedDict() |
| 139 | except AttributeError: |
| 140 | result_time_dict = DotDict() |
| 141 | |
| 142 | for key, value in initial_time_dict.items(): |
| 143 | result_time_dict[key] = value |
| 144 | if not key.endswith("_time"): |
| 145 | continue |
| 146 | result_time_dict[key + '_seconds'] = \ |
| 147 | int(DateTime.convert_date(value, result_format='epoch')) |
| 148 | |
| 149 | return result_time_dict |
| 150 | |
Michael Walsh | 193743e | 2017-11-20 16:43:31 -0600 | [diff] [blame] | 151 | |
| 152 | def get_bmc_df(df_parm_string=""): |
Michael Walsh | 193743e | 2017-11-20 16:43:31 -0600 | [diff] [blame] | 153 | r""" |
| 154 | Get df report from BMC and return as a report "object". |
| 155 | |
| 156 | A df report object is a list where each entry is a dictionary whose keys |
| 157 | are the field names from the first entry in report_list. |
| 158 | |
| 159 | Example df report object: |
| 160 | |
| 161 | df_report: |
| 162 | df_report[0]: |
| 163 | [filesystem]: dev |
| 164 | [1k-blocks]: 247120 |
| 165 | [used]: 0 |
| 166 | [available]: 247120 |
| 167 | [use%]: 0% |
| 168 | [mounted]: /dev |
| 169 | df_report[1]: |
| 170 | [filesystem]: dev |
| 171 | [1k-blocks]: 247120 |
| 172 | [used]: 0 |
| 173 | [available]: 247120 |
| 174 | [use%]: 0% |
| 175 | [mounted]: /dev |
| 176 | |
| 177 | . Description of argument(s): |
| 178 | df_parm_string A string containing valid df command parms (e.g. |
| 179 | "-h /var"). |
| 180 | """ |
| 181 | |
| 182 | out_buf, stderr, rc = bsu.bmc_execute_command("df " + df_parm_string) |
| 183 | return vf.outbuf_to_report(out_buf) |
George Keishing | 6f407b9 | 2018-01-16 02:21:47 -0600 | [diff] [blame] | 184 | |
| 185 | |
| 186 | def get_sbe(): |
George Keishing | 6f407b9 | 2018-01-16 02:21:47 -0600 | [diff] [blame] | 187 | r""" |
| 188 | Return CFAM value which contains such things as SBE side bit. |
| 189 | """ |
| 190 | |
| 191 | cmd_buf = "pdbg -d p9w -p0 getcfam 0x2808 | sed -re 's/.* = //g'" |
| 192 | out_buf, stderr, rc = bsu.bmc_execute_command(cmd_buf) |
| 193 | |
| 194 | return int(out_buf, 16) |
| 195 | |
George Keishing | bf72477 | 2018-02-21 08:57:16 -0600 | [diff] [blame] | 196 | |
Gunnar Mills | 096cd56 | 2018-03-26 10:19:12 -0500 | [diff] [blame] | 197 | def compare_mac_address(sys_mac_addr, user_mac_addr): |
George Keishing | bf72477 | 2018-02-21 08:57:16 -0600 | [diff] [blame] | 198 | r""" |
| 199 | Return 1 if the MAC value matched, otherwise 0. |
| 200 | |
| 201 | . Description of argument(s): |
| 202 | sys_mac_addr A valid system MAC string (e.g. "70:e2:84:14:2a:08") |
| 203 | user_mac_addr A user provided MAC string (e.g. "70:e2:84:14:2a:08") |
| 204 | """ |
| 205 | |
| 206 | index = 0 |
| 207 | # Example: ['70', 'e2', '84', '14', '2a', '08'] |
| 208 | mac_list = user_mac_addr.split(":") |
| 209 | for item in sys_mac_addr.split(":"): |
| 210 | if int(item, 16) == int(mac_list[index], 16): |
| 211 | index = index + 1 |
| 212 | continue |
| 213 | return 0 |
| 214 | |
| 215 | return 1 |