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