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(): |
| 21 | |
| 22 | r""" |
| 23 | Set the global bmc_power_policy_method to either 'Old' or 'New'. |
| 24 | |
| 25 | The power policy data has moved from an 'org' location to an 'xyz' |
| 26 | location. This keyword will determine whether the new method of getting |
| 27 | the power policy is valid and will set the global bmc_power_policy_method |
| 28 | variable accordingly. If power_policy_setup is already set (by a prior |
| 29 | call to this function), this keyword will simply return. |
| 30 | |
| 31 | If bmc_power_policy_method is "Old", this function will adjust the global |
| 32 | policy variables from data/variables.py: RESTORE_LAST_STATE, |
| 33 | ALWAYS_POWER_ON, ALWAYS_POWER_OFF. |
| 34 | """ |
| 35 | |
| 36 | # Retrieve global variables. |
| 37 | power_policy_setup = \ |
| 38 | int(BuiltIn().get_variable_value("${power_policy_setup}", |
| 39 | default=0)) |
| 40 | bmc_power_policy_method = \ |
| 41 | BuiltIn().get_variable_value("${bmc_power_policy_method}", |
| 42 | default=0) |
| 43 | gp.dpvar(power_policy_setup) |
| 44 | |
| 45 | # If this function has already been run once, we need not continue. |
| 46 | if power_policy_setup: |
| 47 | return |
| 48 | |
| 49 | gp.dpvar(bmc_power_policy_method, 1) |
| 50 | |
| 51 | # The user has not set bmc_power_policy_method via a -v parm so we will |
| 52 | # determine what it should be. |
| 53 | if bmc_power_policy_method == "": |
| 54 | status, ret_values = grk.run_key_u("New Get Power Policy", ignore=1) |
| 55 | if status == 'PASS': |
| 56 | bmc_power_policy_method = 'New' |
| 57 | else: |
| 58 | bmc_power_policy_method = 'Old' |
| 59 | |
| 60 | gp.qpvar(bmc_power_policy_method) |
| 61 | # For old style, we will rewrite these global variable settings to old |
| 62 | # values. |
| 63 | if bmc_power_policy_method == "Old": |
| 64 | BuiltIn().set_global_variable("${RESTORE_LAST_STATE}", |
| 65 | "RESTORE_LAST_STATE") |
| 66 | BuiltIn().set_global_variable("${ALWAYS_POWER_ON}", |
| 67 | "ALWAYS_POWER_ON") |
| 68 | BuiltIn().set_global_variable("${ALWAYS_POWER_OFF}", |
| 69 | "ALWAYS_POWER_OFF") |
| 70 | |
| 71 | # Set global variables to control subsequent calls to this function. |
| 72 | BuiltIn().set_global_variable("${bmc_power_policy_method}", |
| 73 | bmc_power_policy_method) |
| 74 | BuiltIn().set_global_variable("${power_policy_setup}", 1) |
| 75 | |
| 76 | |
Michael Walsh | fdc5ced | 2017-08-17 13:15:15 -0500 | [diff] [blame] | 77 | def translate_power_policy_value(policy): |
| 78 | |
| 79 | r""" |
| 80 | Translate the policy value and return the result. |
| 81 | |
| 82 | Using old style functions, callers might call like this with a hard- |
| 83 | code value for policy: |
| 84 | |
| 85 | Set BMC Power Policy RESTORE_LAST_STATE |
| 86 | |
| 87 | This function will get the value of the corresponding global variable (if |
| 88 | it exists) and return it. |
| 89 | |
| 90 | This will allow the old style call to still work on systems using the new |
| 91 | method of storing the policy value. |
| 92 | """ |
| 93 | |
| 94 | valid_power_policy_vars = \ |
| 95 | BuiltIn().get_variable_value("${valid_power_policy_vars}") |
| 96 | |
| 97 | if policy not in valid_power_policy_vars: |
| 98 | return policy |
| 99 | |
| 100 | status, ret_values = grk.run_key_u("Get Variable Value ${" + policy + "}", |
| 101 | quiet=1) |
| 102 | return ret_values |
| 103 | |
Michael Walsh | f880ac6 | 2017-11-10 11:29:37 -0600 | [diff] [blame] | 104 | |
Michael Walsh | f880ac6 | 2017-11-10 11:29:37 -0600 | [diff] [blame] | 105 | def get_bmc_date_time(): |
| 106 | |
| 107 | r""" |
| 108 | Get date/time info from BMC and return as a dictionary. |
| 109 | |
| 110 | Example of dictionary data returned by this keyword. |
| 111 | time_dict: |
| 112 | [local_time]: Fri 2017-11-03 152756 UTC |
| 113 | [local_time_seconds]: 1509740876 |
| 114 | [universal_time]: Fri 2017-11-03 152756 UTC |
| 115 | [universal_time_seconds]: 1509740876 |
| 116 | [rtc_time]: Fri 2016-05-20 163403 |
| 117 | [rtc_time_seconds]: 1463780043 |
| 118 | [time_zone]: n/a (UTC, +0000) |
| 119 | [network_time_on]: yes |
| 120 | [ntp_synchronized]: no |
| 121 | [rtc_in_local_tz]: no |
| 122 | """ |
| 123 | |
| 124 | out_buf, stderr, rc = bsu.bmc_execute_command('timedatectl') |
| 125 | # Example of output returned by call to timedatectl: |
| 126 | # Local time: Fri 2017-11-03 15:27:56 UTC |
| 127 | # Universal time: Fri 2017-11-03 15:27:56 UTC |
| 128 | # RTC time: Fri 2016-05-20 16:34:03 |
| 129 | # Time zone: n/a (UTC, +0000) |
| 130 | # Network time on: yes |
| 131 | # NTP synchronized: no |
| 132 | # RTC in local TZ: no |
| 133 | |
| 134 | # Convert the out_buf to a dictionary. |
| 135 | initial_time_dict = vf.key_value_outbuf_to_dict(out_buf) |
| 136 | |
| 137 | # For each "_time" entry in the dictionary, we will create a corresponding |
| 138 | # "_time_seconds" entry. We create a new dictionary so that the entries |
| 139 | # are kept in a nice order for printing. |
| 140 | try: |
| 141 | result_time_dict = collections.OrderedDict() |
| 142 | except AttributeError: |
| 143 | result_time_dict = DotDict() |
| 144 | |
| 145 | for key, value in initial_time_dict.items(): |
| 146 | result_time_dict[key] = value |
| 147 | if not key.endswith("_time"): |
| 148 | continue |
| 149 | result_time_dict[key + '_seconds'] = \ |
| 150 | int(DateTime.convert_date(value, result_format='epoch')) |
| 151 | |
| 152 | return result_time_dict |
| 153 | |