Justin Thaler | b8807ce | 2018-05-25 19:16:20 -0500 | [diff] [blame] | 1 | #!/usr/bin/python3 |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2 | """ |
Joseph Reynolds | a2d54c5 | 2019-06-11 22:02:57 -0500 | [diff] [blame] | 3 | Copyright 2017,2019 IBM Corporation |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | you may not use this file except in compliance with the License. |
| 7 | You may obtain a copy of the License at |
| 8 | |
| 9 | http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | Unless required by applicable law or agreed to in writing, software |
| 12 | distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | See the License for the specific language governing permissions and |
| 15 | limitations under the License. |
| 16 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 17 | import argparse |
| 18 | import requests |
| 19 | import getpass |
| 20 | import json |
| 21 | import os |
| 22 | import urllib3 |
| 23 | import time, datetime |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 24 | import binascii |
| 25 | import subprocess |
| 26 | import platform |
| 27 | import zipfile |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 28 | import tarfile |
| 29 | import tempfile |
| 30 | import hashlib |
Justin Thaler | a6b5df7 | 2018-07-16 11:10:07 -0500 | [diff] [blame] | 31 | import re |
Justin Thaler | 24d4efa | 2018-11-08 22:48:10 -0600 | [diff] [blame] | 32 | import uuid |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 33 | |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 34 | jsonHeader = {'Content-Type' : 'application/json'} |
| 35 | xAuthHeader = {} |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 36 | baseTimeout = 60 |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 37 | serverTypeMap = { |
| 38 | 'ActiveDirectory' : 'active_directory', |
| 39 | 'OpenLDAP' : 'openldap' |
| 40 | } |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 41 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 42 | def hilight(textToColor, color, bold): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 43 | """ |
| 44 | Used to add highlights to various text for displaying in a terminal |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 45 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 46 | @param textToColor: string, the text to be colored |
| 47 | @param color: string, used to color the text red or green |
| 48 | @param bold: boolean, used to bold the textToColor |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 49 | @return: Buffered reader containing the modified string. |
| 50 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 51 | if(sys.platform.__contains__("win")): |
| 52 | if(color == "red"): |
| 53 | os.system('color 04') |
| 54 | elif(color == "green"): |
| 55 | os.system('color 02') |
| 56 | else: |
| 57 | os.system('color') #reset to default |
| 58 | return textToColor |
| 59 | else: |
| 60 | attr = [] |
| 61 | if(color == "red"): |
| 62 | attr.append('31') |
| 63 | elif(color == "green"): |
| 64 | attr.append('32') |
| 65 | else: |
| 66 | attr.append('0') |
| 67 | if bold: |
| 68 | attr.append('1') |
| 69 | else: |
| 70 | attr.append('0') |
| 71 | return '\x1b[%sm%s\x1b[0m' % (';'.join(attr),textToColor) |
| 72 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 73 | def connectionErrHandler(jsonFormat, errorStr, err): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 74 | """ |
| 75 | Error handler various connection errors to bmcs |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 76 | |
| 77 | @param jsonFormat: boolean, used to output in json format with an error code. |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 78 | @param errorStr: string, used to color the text red or green |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 79 | @param err: string, the text from the exception |
| 80 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 81 | if errorStr == "Timeout": |
| 82 | if not jsonFormat: |
| 83 | return("FQPSPIN0000M: Connection timed out. Ensure you have network connectivity to the bmc") |
| 84 | else: |
Justin Thaler | 115bca7 | 2018-05-25 19:29:08 -0500 | [diff] [blame] | 85 | conerror = {} |
| 86 | conerror['CommonEventID'] = 'FQPSPIN0000M' |
| 87 | conerror['sensor']="N/A" |
| 88 | conerror['state']="N/A" |
| 89 | conerror['additionalDetails'] = "N/A" |
| 90 | conerror['Message']="Connection timed out. Ensure you have network connectivity to the BMC" |
| 91 | conerror['LengthyDescription'] = "While trying to establish a connection with the specified BMC, the BMC failed to respond in adequate time. Verify the BMC is functioning properly, and the network connectivity to the BMC is stable." |
| 92 | conerror['Serviceable']="Yes" |
| 93 | conerror['CallHomeCandidate']= "No" |
| 94 | conerror['Severity'] = "Critical" |
| 95 | conerror['EventType'] = "Communication Failure/Timeout" |
| 96 | conerror['VMMigrationFlag'] = "Yes" |
| 97 | conerror["AffectedSubsystem"] = "Interconnect (Networking)" |
| 98 | conerror["timestamp"] = str(int(time.time())) |
| 99 | conerror["UserAction"] = "Verify network connectivity between the two systems and the bmc is functional." |
| 100 | eventdict = {} |
| 101 | eventdict['event0'] = conerror |
| 102 | eventdict['numAlerts'] = '1' |
Justin Thaler | 115bca7 | 2018-05-25 19:29:08 -0500 | [diff] [blame] | 103 | errorMessageStr = errorMessageStr = json.dumps(eventdict, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 104 | return(errorMessageStr) |
| 105 | elif errorStr == "ConnectionError": |
| 106 | if not jsonFormat: |
| 107 | return("FQPSPIN0001M: " + str(err)) |
| 108 | else: |
Justin Thaler | 115bca7 | 2018-05-25 19:29:08 -0500 | [diff] [blame] | 109 | conerror = {} |
| 110 | conerror['CommonEventID'] = 'FQPSPIN0001M' |
| 111 | conerror['sensor']="N/A" |
| 112 | conerror['state']="N/A" |
| 113 | conerror['additionalDetails'] = str(err) |
| 114 | conerror['Message']="Connection Error. View additional details for more information" |
| 115 | conerror['LengthyDescription'] = "A connection error to the specified BMC occurred and additional details are provided. Review these details to resolve the issue." |
| 116 | conerror['Serviceable']="Yes" |
| 117 | conerror['CallHomeCandidate']= "No" |
| 118 | conerror['Severity'] = "Critical" |
| 119 | conerror['EventType'] = "Communication Failure/Timeout" |
| 120 | conerror['VMMigrationFlag'] = "Yes" |
| 121 | conerror["AffectedSubsystem"] = "Interconnect (Networking)" |
| 122 | conerror["timestamp"] = str(int(time.time())) |
| 123 | conerror["UserAction"] = "Correct the issue highlighted in additional details and try again" |
| 124 | eventdict = {} |
| 125 | eventdict['event0'] = conerror |
| 126 | eventdict['numAlerts'] = '1' |
Justin Thaler | 115bca7 | 2018-05-25 19:29:08 -0500 | [diff] [blame] | 127 | errorMessageStr = json.dumps(eventdict, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 128 | return(errorMessageStr) |
Justin Thaler | 115bca7 | 2018-05-25 19:29:08 -0500 | [diff] [blame] | 129 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 130 | else: |
| 131 | return("Unknown Error: "+ str(err)) |
| 132 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 133 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 134 | def setColWidth(keylist, numCols, dictForOutput, colNames): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 135 | """ |
| 136 | Sets the output width of the columns to display |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 137 | |
| 138 | @param keylist: list, list of strings representing the keys for the dictForOutput |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 139 | @param numcols: the total number of columns in the final output |
| 140 | @param dictForOutput: dictionary, contains the information to print to the screen |
| 141 | @param colNames: list, The strings to use for the column headings, in order of the keylist |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 142 | @return: A list of the column widths for each respective column. |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 143 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 144 | colWidths = [] |
| 145 | for x in range(0, numCols): |
| 146 | colWidths.append(0) |
| 147 | for key in dictForOutput: |
| 148 | for x in range(0, numCols): |
| 149 | colWidths[x] = max(colWidths[x], len(str(dictForOutput[key][keylist[x]]))) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 150 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 151 | for x in range(0, numCols): |
| 152 | colWidths[x] = max(colWidths[x], len(colNames[x])) +2 |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 153 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 154 | return colWidths |
| 155 | |
| 156 | def loadPolicyTable(pathToPolicyTable): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 157 | """ |
| 158 | loads a json based policy table into a dictionary |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 159 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 160 | @param value: boolean, the value to convert |
| 161 | @return: A string of "Yes" or "No" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 162 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 163 | policyTable = {} |
| 164 | if(os.path.exists(pathToPolicyTable)): |
| 165 | with open(pathToPolicyTable, 'r') as stream: |
| 166 | try: |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 167 | contents =json.load(stream) |
| 168 | policyTable = contents['events'] |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 169 | except Exception as err: |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 170 | print(err) |
| 171 | return policyTable |
| 172 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 173 | |
| 174 | def boolToString(value): |
| 175 | """ |
| 176 | converts a boolean value to a human readable string value |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 177 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 178 | @param value: boolean, the value to convert |
| 179 | @return: A string of "Yes" or "No" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 180 | """ |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 181 | if(value): |
| 182 | return "Yes" |
| 183 | else: |
| 184 | return "No" |
| 185 | |
Justin Thaler | a6b5df7 | 2018-07-16 11:10:07 -0500 | [diff] [blame] | 186 | def stringToInt(text): |
| 187 | """ |
| 188 | returns an integer if the string can be converted, otherwise returns the string |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 189 | |
Justin Thaler | a6b5df7 | 2018-07-16 11:10:07 -0500 | [diff] [blame] | 190 | @param text: the string to try to convert to an integer |
| 191 | """ |
| 192 | if text.isdigit(): |
| 193 | return int(text) |
| 194 | else: |
| 195 | return text |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 196 | |
Justin Thaler | a6b5df7 | 2018-07-16 11:10:07 -0500 | [diff] [blame] | 197 | def naturalSort(text): |
| 198 | """ |
| 199 | provides a way to naturally sort a list |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 200 | |
Justin Thaler | a6b5df7 | 2018-07-16 11:10:07 -0500 | [diff] [blame] | 201 | @param text: the key to convert for sorting |
| 202 | @return list containing the broken up string parts by integers and strings |
| 203 | """ |
| 204 | stringPartList = [] |
| 205 | for c in re.split('(\d+)', text): |
| 206 | stringPartList.append(stringToInt(c)) |
| 207 | return stringPartList |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 208 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 209 | def tableDisplay(keylist, colNames, output): |
| 210 | """ |
| 211 | Logs into the BMC and creates a session |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 212 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 213 | @param keylist: list, keys for the output dictionary, ordered by colNames |
| 214 | @param colNames: Names for the Table of the columns |
| 215 | @param output: The dictionary of data to display |
| 216 | @return: Session object |
| 217 | """ |
| 218 | colWidth = setColWidth(keylist, len(colNames), output, colNames) |
| 219 | row = "" |
| 220 | outputText = "" |
| 221 | for i in range(len(colNames)): |
| 222 | if (i != 0): row = row + "| " |
| 223 | row = row + colNames[i].ljust(colWidth[i]) |
| 224 | outputText += row + "\n" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 225 | |
Justin Thaler | a6b5df7 | 2018-07-16 11:10:07 -0500 | [diff] [blame] | 226 | output_keys = list(output.keys()) |
| 227 | output_keys.sort(key=naturalSort) |
| 228 | for key in output_keys: |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 229 | row = "" |
Justin Thaler | 8fe0c73 | 2018-07-24 14:32:35 -0500 | [diff] [blame] | 230 | for i in range(len(keylist)): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 231 | if (i != 0): row = row + "| " |
| 232 | row = row + output[key][keylist[i]].ljust(colWidth[i]) |
| 233 | outputText += row + "\n" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 234 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 235 | return outputText |
| 236 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 237 | def checkFWactivation(host, args, session): |
| 238 | """ |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 239 | Checks the software inventory for an image that is being activated. |
| 240 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 241 | @return: True if an image is being activated, false is no activations are happening |
| 242 | """ |
| 243 | url="https://"+host+"/xyz/openbmc_project/software/enumerate" |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 244 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 245 | resp = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 246 | except(requests.exceptions.Timeout): |
| 247 | print(connectionErrHandler(args.json, "Timeout", None)) |
| 248 | return(True) |
| 249 | except(requests.exceptions.ConnectionError) as err: |
| 250 | print( connectionErrHandler(args.json, "ConnectionError", err)) |
| 251 | return True |
Justin Thaler | 3a5771b | 2019-01-23 14:31:52 -0600 | [diff] [blame] | 252 | fwInfo = resp.json()['data'] |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 253 | for key in fwInfo: |
| 254 | if 'Activation' in fwInfo[key]: |
| 255 | if 'Activating' in fwInfo[key]['Activation'] or 'Activating' in fwInfo[key]['RequestedActivation']: |
| 256 | return True |
| 257 | return False |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 258 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 259 | def login(host, username, pw,jsonFormat): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 260 | """ |
| 261 | Logs into the BMC and creates a session |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 262 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 263 | @param host: string, the hostname or IP address of the bmc to log into |
| 264 | @param username: The user name for the bmc to log into |
| 265 | @param pw: The password for the BMC to log into |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 266 | @param jsonFormat: boolean, flag that will only allow relevant data from user command to be display. This function becomes silent when set to true. |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 267 | @return: Session object |
| 268 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 269 | if(jsonFormat==False): |
| 270 | print("Attempting login...") |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 271 | mysess = requests.session() |
| 272 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 273 | r = mysess.post('https://'+host+'/login', headers=jsonHeader, json = {"data": [username, pw]}, verify=False, timeout=baseTimeout) |
Sunitha Harish | 336cda2 | 2019-07-23 02:02:52 -0500 | [diff] [blame] | 274 | if r.status_code == 200: |
| 275 | cookie = r.headers['Set-Cookie'] |
| 276 | match = re.search('SESSION=(\w+);', cookie) |
| 277 | if match: |
| 278 | xAuthHeader['X-Auth-Token'] = match.group(1) |
| 279 | jsonHeader.update(xAuthHeader) |
| 280 | loginMessage = json.loads(r.text) |
| 281 | if (loginMessage['status'] != "ok"): |
| 282 | print(loginMessage["data"]["description"].encode('utf-8')) |
| 283 | sys.exit(1) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 284 | # if(sys.version_info < (3,0)): |
| 285 | # urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) |
| 286 | # if sys.version_info >= (3,0): |
| 287 | # requests.packages.urllib3.disable_warnings(requests.packages.urllib3.exceptions.InsecureRequestWarning) |
Sunitha Harish | 336cda2 | 2019-07-23 02:02:52 -0500 | [diff] [blame] | 288 | return mysess |
| 289 | else: |
| 290 | return None |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 291 | except(requests.exceptions.Timeout): |
Justin Thaler | 115bca7 | 2018-05-25 19:29:08 -0500 | [diff] [blame] | 292 | return (connectionErrHandler(jsonFormat, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 293 | except(requests.exceptions.ConnectionError) as err: |
Justin Thaler | 115bca7 | 2018-05-25 19:29:08 -0500 | [diff] [blame] | 294 | return (connectionErrHandler(jsonFormat, "ConnectionError", err)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 295 | |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 296 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 297 | def logout(host, username, pw, session, jsonFormat): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 298 | """ |
| 299 | Logs out of the bmc and terminates the session |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 300 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 301 | @param host: string, the hostname or IP address of the bmc to log out of |
| 302 | @param username: The user name for the bmc to log out of |
| 303 | @param pw: The password for the BMC to log out of |
| 304 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 305 | @param jsonFormat: boolean, flag that will only allow relevant data from user command to be display. This function becomes silent when set to true. |
| 306 | """ |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 307 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 308 | r = session.post('https://'+host+'/logout', headers=jsonHeader,json = {"data": [username, pw]}, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 309 | except(requests.exceptions.Timeout): |
| 310 | print(connectionErrHandler(jsonFormat, "Timeout", None)) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 311 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 312 | if(jsonFormat==False): |
Matt Spinler | eae05b0 | 2019-01-24 12:59:34 -0600 | [diff] [blame] | 313 | if r.status_code == 200: |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 314 | print('User ' +username + ' has been logged out') |
| 315 | |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 316 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 317 | def fru(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 318 | """ |
| 319 | prints out the system inventory. deprecated see fruPrint and fruList |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 320 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 321 | @param host: string, the hostname or IP address of the bmc |
| 322 | @param args: contains additional arguments used by the fru sub command |
| 323 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 324 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 325 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 326 | #url="https://"+host+"/org/openbmc/inventory/system/chassis/enumerate" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 327 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 328 | #print(url) |
| 329 | #res = session.get(url, headers=httpHeader, verify=False) |
| 330 | #print(res.text) |
| 331 | #sample = res.text |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 332 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 333 | #inv_list = json.loads(sample)["data"] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 334 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 335 | url="https://"+host+"/xyz/openbmc_project/inventory/enumerate" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 336 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 337 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 338 | except(requests.exceptions.Timeout): |
| 339 | return(connectionErrHandler(args.json, "Timeout", None)) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 340 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 341 | sample = res.text |
| 342 | # inv_list.update(json.loads(sample)["data"]) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 343 | # |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 344 | # #determine column width's |
| 345 | # colNames = ["FRU Name", "FRU Type", "Has Fault", "Is FRU", "Present", "Version"] |
| 346 | # colWidths = setColWidth(["FRU Name", "fru_type", "fault", "is_fru", "present", "version"], 6, inv_list, colNames) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 347 | # |
| 348 | # print("FRU Name".ljust(colWidths[0])+ "FRU Type".ljust(colWidths[1]) + "Has Fault".ljust(colWidths[2]) + "Is FRU".ljust(colWidths[3])+ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 349 | # "Present".ljust(colWidths[4]) + "Version".ljust(colWidths[5])) |
| 350 | # format the output |
| 351 | # for key in sorted(inv_list.keys()): |
| 352 | # keyParts = key.split("/") |
| 353 | # isFRU = "True" if (inv_list[key]["is_fru"]==1) else "False" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 354 | # |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 355 | # fruEntry = (keyParts[len(keyParts) - 1].ljust(colWidths[0]) + inv_list[key]["fru_type"].ljust(colWidths[1])+ |
| 356 | # inv_list[key]["fault"].ljust(colWidths[2])+isFRU.ljust(colWidths[3])+ |
| 357 | # inv_list[key]["present"].ljust(colWidths[4])+ inv_list[key]["version"].ljust(colWidths[5])) |
| 358 | # if(isTTY): |
| 359 | # if(inv_list[key]["is_fru"] == 1): |
| 360 | # color = "green" |
| 361 | # bold = True |
| 362 | # else: |
| 363 | # color='black' |
| 364 | # bold = False |
| 365 | # fruEntry = hilight(fruEntry, color, bold) |
| 366 | # print (fruEntry) |
| 367 | return sample |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 368 | |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 369 | def fruPrint(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 370 | """ |
| 371 | prints out all inventory |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 372 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 373 | @param host: string, the hostname or IP address of the bmc |
| 374 | @param args: contains additional arguments used by the fru sub command |
| 375 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 376 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 377 | @return returns the total fru list. |
| 378 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 379 | url="https://"+host+"/xyz/openbmc_project/inventory/enumerate" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 380 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 381 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 382 | except(requests.exceptions.Timeout): |
| 383 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 384 | |
Justin Thaler | 3a5771b | 2019-01-23 14:31:52 -0600 | [diff] [blame] | 385 | frulist={} |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 386 | # print(res.text) |
Justin Thaler | 3a5771b | 2019-01-23 14:31:52 -0600 | [diff] [blame] | 387 | if res.status_code==200: |
| 388 | frulist['Hardware'] = res.json()['data'] |
| 389 | else: |
| 390 | if not args.json: |
| 391 | return "Error retrieving the system inventory. BMC message: {msg}".format(msg=res.json()['message']) |
| 392 | else: |
| 393 | return res.json() |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 394 | url="https://"+host+"/xyz/openbmc_project/software/enumerate" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 395 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 396 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 397 | except(requests.exceptions.Timeout): |
| 398 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 399 | # print(res.text) |
Justin Thaler | 3a5771b | 2019-01-23 14:31:52 -0600 | [diff] [blame] | 400 | if res.status_code==200: |
| 401 | frulist['Software'] = res.json()['data'] |
| 402 | else: |
| 403 | if not args.json(): |
| 404 | return "Error retrieving the system inventory. BMC message: {msg}".format(msg=res.json()['message']) |
| 405 | else: |
| 406 | return res.json() |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 407 | return frulist |
| 408 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 409 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 410 | def fruList(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 411 | """ |
| 412 | prints out all inventory or only a specific specified item |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 413 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 414 | @param host: string, the hostname or IP address of the bmc |
| 415 | @param args: contains additional arguments used by the fru sub command |
| 416 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 417 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 418 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 419 | if(args.items==True): |
| 420 | return fruPrint(host, args, session) |
| 421 | else: |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 422 | return fruPrint(host, args, session) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 423 | |
| 424 | |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 425 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 426 | def fruStatus(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 427 | """ |
| 428 | prints out the status of all FRUs |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 429 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 430 | @param host: string, the hostname or IP address of the bmc |
| 431 | @param args: contains additional arguments used by the fru sub command |
| 432 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 433 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 434 | """ |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 435 | url="https://"+host+"/xyz/openbmc_project/inventory/enumerate" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 436 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 437 | res = session.get(url, headers=jsonHeader, verify=False) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 438 | except(requests.exceptions.Timeout): |
| 439 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 440 | # print(res.text) |
Justin Thaler | 3a5771b | 2019-01-23 14:31:52 -0600 | [diff] [blame] | 441 | frulist = res.json()['data'] |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 442 | frus = {} |
| 443 | for key in frulist: |
| 444 | component = frulist[key] |
| 445 | isFru = False |
| 446 | present = False |
| 447 | func = False |
| 448 | hasSels = False |
| 449 | keyPieces = key.split('/') |
| 450 | fruName = keyPieces[-1] |
| 451 | if 'core' in fruName: #associate cores to cpus |
| 452 | fruName = keyPieces[-2] + '-' + keyPieces[-1] |
| 453 | if 'Functional' in component: |
| 454 | if('Present' in component): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 455 | if 'FieldReplaceable' in component: |
| 456 | if component['FieldReplaceable'] == 1: |
| 457 | isFru = True |
| 458 | if "fan" in fruName: |
| 459 | isFru = True; |
| 460 | if component['Present'] == 1: |
| 461 | present = True |
| 462 | if component['Functional'] == 1: |
| 463 | func = True |
| 464 | if ((key + "/fault") in frulist): |
| 465 | hasSels = True; |
| 466 | if args.verbose: |
| 467 | if hasSels: |
| 468 | loglist = [] |
| 469 | faults = frulist[key+"/fault"]['endpoints'] |
| 470 | for item in faults: |
| 471 | loglist.append(item.split('/')[-1]) |
| 472 | frus[fruName] = {"compName": fruName, "Functional": boolToString(func), "Present":boolToString(present), "IsFru": boolToString(isFru), "selList": ', '.join(loglist).strip() } |
| 473 | else: |
| 474 | frus[fruName] = {"compName": fruName, "Functional": boolToString(func), "Present":boolToString(present), "IsFru": boolToString(isFru), "selList": "None" } |
| 475 | else: |
| 476 | frus[fruName] = {"compName": fruName, "Functional": boolToString(func), "Present":boolToString(present), "IsFru": boolToString(isFru), "hasSEL": boolToString(hasSels) } |
Justin Thaler | fb9c81c | 2018-07-16 11:14:37 -0500 | [diff] [blame] | 477 | elif "power_supply" in fruName or "powersupply" in fruName: |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 478 | if component['Present'] ==1: |
| 479 | present = True |
| 480 | isFru = True |
| 481 | if ((key + "/fault") in frulist): |
| 482 | hasSels = True; |
| 483 | if args.verbose: |
| 484 | if hasSels: |
| 485 | loglist = [] |
| 486 | faults = frulist[key+"/fault"]['endpoints'] |
Obihörnchen | ff8035f | 2018-12-05 21:07:37 +0100 | [diff] [blame] | 487 | for item in faults: |
| 488 | loglist.append(item.split('/')[-1]) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 489 | frus[fruName] = {"compName": fruName, "Functional": "No", "Present":boolToString(present), "IsFru": boolToString(isFru), "selList": ', '.join(loglist).strip() } |
| 490 | else: |
| 491 | frus[fruName] = {"compName": fruName, "Functional": "Yes", "Present":boolToString(present), "IsFru": boolToString(isFru), "selList": "None" } |
| 492 | else: |
| 493 | frus[fruName] = {"compName": fruName, "Functional": boolToString(not hasSels), "Present":boolToString(present), "IsFru": boolToString(isFru), "hasSEL": boolToString(hasSels) } |
| 494 | if not args.json: |
| 495 | if not args.verbose: |
| 496 | colNames = ["Component", "Is a FRU", "Present", "Functional", "Has Logs"] |
| 497 | keylist = ["compName", "IsFru", "Present", "Functional", "hasSEL"] |
| 498 | else: |
| 499 | colNames = ["Component", "Is a FRU", "Present", "Functional", "Assoc. Log Number(s)"] |
| 500 | keylist = ["compName", "IsFru", "Present", "Functional", "selList"] |
| 501 | return tableDisplay(keylist, colNames, frus) |
| 502 | else: |
| 503 | return str(json.dumps(frus, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False)) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 504 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 505 | def sensor(host, args, session): |
| 506 | """ |
| 507 | prints out all sensors |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 508 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 509 | @param host: string, the hostname or IP address of the bmc |
| 510 | @param args: contains additional arguments used by the sensor sub command |
| 511 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 512 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 513 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 514 | url="https://"+host+"/xyz/openbmc_project/sensors/enumerate" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 515 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 516 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 517 | except(requests.exceptions.Timeout): |
| 518 | return(connectionErrHandler(args.json, "Timeout", None)) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 519 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 520 | #Get OCC status |
| 521 | url="https://"+host+"/org/open_power/control/enumerate" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 522 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 523 | occres = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 524 | except(requests.exceptions.Timeout): |
| 525 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 526 | if not args.json: |
| 527 | colNames = ['sensor', 'type', 'units', 'value', 'target'] |
Justin Thaler | 3a5771b | 2019-01-23 14:31:52 -0600 | [diff] [blame] | 528 | sensors = res.json()["data"] |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 529 | output = {} |
| 530 | for key in sensors: |
| 531 | senDict = {} |
| 532 | keyparts = key.split("/") |
| 533 | senDict['sensorName'] = keyparts[-1] |
| 534 | senDict['type'] = keyparts[-2] |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 535 | try: |
| 536 | senDict['units'] = sensors[key]['Unit'].split('.')[-1] |
| 537 | except KeyError: |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 538 | senDict['units'] = "N/A" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 539 | if('Scale' in sensors[key]): |
| 540 | scale = 10 ** sensors[key]['Scale'] |
| 541 | else: |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 542 | scale = 1 |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 543 | try: |
| 544 | senDict['value'] = str(sensors[key]['Value'] * scale) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 545 | except KeyError: |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 546 | if 'value' in sensors[key]: |
| 547 | senDict['value'] = sensors[key]['value'] |
| 548 | else: |
| 549 | senDict['value'] = "N/A" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 550 | if 'Target' in sensors[key]: |
| 551 | senDict['target'] = str(sensors[key]['Target']) |
| 552 | else: |
| 553 | senDict['target'] = 'N/A' |
| 554 | output[senDict['sensorName']] = senDict |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 555 | |
Justin Thaler | 3a5771b | 2019-01-23 14:31:52 -0600 | [diff] [blame] | 556 | occstatus = occres.json()["data"] |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 557 | if '/org/open_power/control/occ0' in occstatus: |
| 558 | occ0 = occstatus["/org/open_power/control/occ0"]['OccActive'] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 559 | if occ0 == 1: |
| 560 | occ0 = 'Active' |
| 561 | else: |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 562 | occ0 = 'Inactive' |
| 563 | output['OCC0'] = {'sensorName':'OCC0', 'type': 'Discrete', 'units': 'N/A', 'value': occ0, 'target': 'Active'} |
| 564 | occ1 = occstatus["/org/open_power/control/occ1"]['OccActive'] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 565 | if occ1 == 1: |
| 566 | occ1 = 'Active' |
| 567 | else: |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 568 | occ1 = 'Inactive' |
| 569 | output['OCC1'] = {'sensorName':'OCC1', 'type': 'Discrete', 'units': 'N/A', 'value': occ0, 'target': 'Active'} |
| 570 | else: |
| 571 | output['OCC0'] = {'sensorName':'OCC0', 'type': 'Discrete', 'units': 'N/A', 'value': 'Inactive', 'target': 'Inactive'} |
| 572 | output['OCC1'] = {'sensorName':'OCC1', 'type': 'Discrete', 'units': 'N/A', 'value': 'Inactive', 'target': 'Inactive'} |
| 573 | keylist = ['sensorName', 'type', 'units', 'value', 'target'] |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 574 | |
| 575 | return tableDisplay(keylist, colNames, output) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 576 | else: |
| 577 | return res.text + occres.text |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 578 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 579 | def sel(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 580 | """ |
| 581 | prints out the bmc alerts |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 582 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 583 | @param host: string, the hostname or IP address of the bmc |
| 584 | @param args: contains additional arguments used by the sel sub command |
| 585 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 586 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 587 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 588 | |
| 589 | url="https://"+host+"/xyz/openbmc_project/logging/entry/enumerate" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 590 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 591 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 592 | except(requests.exceptions.Timeout): |
| 593 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 594 | return res.text |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 595 | |
| 596 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 597 | def parseESEL(args, eselRAW): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 598 | """ |
| 599 | parses the esel data and gets predetermined search terms |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 600 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 601 | @param eselRAW: string, the raw esel string from the bmc |
| 602 | @return: A dictionary containing the quick snapshot data unless args.fullEsel is listed then a full PEL log is returned |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 603 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 604 | eselParts = {} |
| 605 | esel_bin = binascii.unhexlify(''.join(eselRAW.split()[16:])) |
| 606 | #search terms contains the search term as the key and the return dictionary key as it's value |
| 607 | searchTerms = { 'Signature Description':'signatureDescription', 'devdesc':'devdesc', |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 608 | 'Callout type': 'calloutType', 'Procedure':'procedure', 'Sensor Type': 'sensorType'} |
Justin Thaler | 24d4efa | 2018-11-08 22:48:10 -0600 | [diff] [blame] | 609 | uniqueID = str(uuid.uuid4()) |
| 610 | eselBinPath = tempfile.gettempdir() + os.sep + uniqueID + 'esel.bin' |
Justin Thaler | cf1deae | 2018-05-25 19:35:21 -0500 | [diff] [blame] | 611 | with open(eselBinPath, 'wb') as f: |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 612 | f.write(esel_bin) |
| 613 | errlPath = "" |
| 614 | #use the right errl file for the machine architecture |
| 615 | arch = platform.machine() |
| 616 | if(arch =='x86_64' or arch =='AMD64'): |
| 617 | if os.path.exists('/opt/ibm/ras/bin/x86_64/errl'): |
| 618 | errlPath = '/opt/ibm/ras/bin/x86_64/errl' |
| 619 | elif os.path.exists('errl/x86_64/errl'): |
| 620 | errlPath = 'errl/x86_64/errl' |
| 621 | else: |
| 622 | errlPath = 'x86_64/errl' |
| 623 | elif (platform.machine()=='ppc64le'): |
| 624 | if os.path.exists('/opt/ibm/ras/bin/ppc64le/errl'): |
| 625 | errlPath = '/opt/ibm/ras/bin/ppc64le/errl' |
| 626 | elif os.path.exists('errl/ppc64le/errl'): |
| 627 | errlPath = 'errl/ppc64le/errl' |
| 628 | else: |
| 629 | errlPath = 'ppc64le/errl' |
| 630 | else: |
| 631 | print("machine architecture not supported for parsing eSELs") |
| 632 | return eselParts |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 633 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 634 | if(os.path.exists(errlPath)): |
Justin Thaler | cf1deae | 2018-05-25 19:35:21 -0500 | [diff] [blame] | 635 | output= subprocess.check_output([errlPath, '-d', '--file='+eselBinPath]).decode('utf-8') |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 636 | # output = proc.communicate()[0] |
| 637 | lines = output.split('\n') |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 638 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 639 | if(hasattr(args, 'fullEsel')): |
| 640 | return output |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 641 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 642 | for i in range(0, len(lines)): |
| 643 | lineParts = lines[i].split(':') |
| 644 | if(len(lineParts)>1): #ignore multi lines, output formatting lines, and other information |
| 645 | for term in searchTerms: |
| 646 | if(term in lineParts[0]): |
| 647 | temp = lines[i][lines[i].find(':')+1:].strip()[:-1].strip() |
| 648 | if lines[i+1].find(':') != -1: |
| 649 | if (len(lines[i+1].split(':')[0][1:].strip())==0): |
| 650 | while(len(lines[i][:lines[i].find(':')].strip())>2): |
Justin Thaler | 4303042 | 2018-11-08 22:50:21 -0600 | [diff] [blame] | 651 | #has multiple lines, process and update line counter |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 652 | if((i+1) <= len(lines)): |
| 653 | i+=1 |
| 654 | else: |
| 655 | i=i-1 |
| 656 | break |
Justin Thaler | 4303042 | 2018-11-08 22:50:21 -0600 | [diff] [blame] | 657 | #Append the content from the next line removing the pretty display characters |
| 658 | #Finds the first colon then starts 2 characters after, then removes all whitespace |
| 659 | temp = temp + lines[i][lines[i].find(':')+2:].strip()[:-1].strip()[:-1].strip() |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 660 | if(searchTerms[term] in eselParts): |
| 661 | eselParts[searchTerms[term]] = eselParts[searchTerms[term]] + ", " + temp |
| 662 | else: |
| 663 | eselParts[searchTerms[term]] = temp |
Justin Thaler | cf1deae | 2018-05-25 19:35:21 -0500 | [diff] [blame] | 664 | os.remove(eselBinPath) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 665 | else: |
| 666 | print("errl file cannot be found") |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 667 | |
| 668 | return eselParts |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 669 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 670 | |
Matt Spinler | 02d0dff | 2018-08-29 13:19:25 -0500 | [diff] [blame] | 671 | def getESELSeverity(esel): |
| 672 | """ |
| 673 | Finds the severity type in an eSEL from the User Header section. |
| 674 | @param esel - the eSEL data |
| 675 | @return severity - e.g. 'Critical' |
| 676 | """ |
| 677 | |
| 678 | # everything but 1 and 2 are Critical |
| 679 | # '1': 'recovered', |
| 680 | # '2': 'predictive', |
| 681 | # '4': 'unrecoverable', |
| 682 | # '5': 'critical', |
| 683 | # '6': 'diagnostic', |
| 684 | # '7': 'symptom' |
| 685 | severities = { |
| 686 | '1': 'Informational', |
| 687 | '2': 'Warning' |
| 688 | } |
| 689 | |
| 690 | try: |
| 691 | headerPosition = esel.index('55 48') # 'UH' |
| 692 | # The severity is the last byte in the 8 byte section (a byte is ' bb') |
| 693 | severity = esel[headerPosition:headerPosition+32].split(' ')[-1] |
| 694 | type = severity[0] |
| 695 | except ValueError: |
| 696 | print("Could not find severity value in UH section in eSEL") |
| 697 | type = 'x'; |
| 698 | |
| 699 | return severities.get(type, 'Critical') |
| 700 | |
| 701 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 702 | def sortSELs(events): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 703 | """ |
| 704 | sorts the sels by timestamp, then log entry number |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 705 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 706 | @param events: Dictionary containing events |
| 707 | @return: list containing a list of the ordered log entries, and dictionary of keys |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 708 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 709 | logNumList = [] |
| 710 | timestampList = [] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 711 | eventKeyDict = {} |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 712 | eventsWithTimestamp = {} |
| 713 | logNum2events = {} |
| 714 | for key in events: |
| 715 | if key == 'numAlerts': continue |
| 716 | if 'callout' in key: continue |
| 717 | timestamp = (events[key]['timestamp']) |
| 718 | if timestamp not in timestampList: |
| 719 | eventsWithTimestamp[timestamp] = [events[key]['logNum']] |
| 720 | else: |
| 721 | eventsWithTimestamp[timestamp].append(events[key]['logNum']) |
| 722 | #map logNumbers to the event dictionary keys |
| 723 | eventKeyDict[str(events[key]['logNum'])] = key |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 724 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 725 | timestampList = list(eventsWithTimestamp.keys()) |
| 726 | timestampList.sort() |
| 727 | for ts in timestampList: |
| 728 | if len(eventsWithTimestamp[ts]) > 1: |
| 729 | tmplist = eventsWithTimestamp[ts] |
| 730 | tmplist.sort() |
| 731 | logNumList = logNumList + tmplist |
| 732 | else: |
| 733 | logNumList = logNumList + eventsWithTimestamp[ts] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 734 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 735 | return [logNumList, eventKeyDict] |
| 736 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 737 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 738 | def parseAlerts(policyTable, selEntries, args): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 739 | """ |
| 740 | parses alerts in the IBM CER format, using an IBM policy Table |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 741 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 742 | @param policyTable: dictionary, the policy table entries |
| 743 | @param selEntries: dictionary, the alerts retrieved from the bmc |
| 744 | @return: A dictionary of the parsed entries, in chronological order |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 745 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 746 | eventDict = {} |
| 747 | eventNum ="" |
| 748 | count = 0 |
| 749 | esel = "" |
| 750 | eselParts = {} |
| 751 | i2cdevice= "" |
Matt Spinler | 02d0dff | 2018-08-29 13:19:25 -0500 | [diff] [blame] | 752 | eselSeverity = None |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 753 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 754 | 'prepare and sort the event entries' |
| 755 | for key in selEntries: |
| 756 | if 'callout' not in key: |
| 757 | selEntries[key]['logNum'] = key.split('/')[-1] |
| 758 | selEntries[key]['timestamp'] = selEntries[key]['Timestamp'] |
| 759 | sortedEntries = sortSELs(selEntries) |
| 760 | logNumList = sortedEntries[0] |
| 761 | eventKeyDict = sortedEntries[1] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 762 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 763 | for logNum in logNumList: |
| 764 | key = eventKeyDict[logNum] |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 765 | hasEsel=False |
| 766 | i2creadFail = False |
| 767 | if 'callout' in key: |
| 768 | continue |
| 769 | else: |
| 770 | messageID = str(selEntries[key]['Message']) |
| 771 | addDataPiece = selEntries[key]['AdditionalData'] |
| 772 | calloutIndex = 0 |
| 773 | calloutFound = False |
| 774 | for i in range(len(addDataPiece)): |
| 775 | if("CALLOUT_INVENTORY_PATH" in addDataPiece[i]): |
| 776 | calloutIndex = i |
| 777 | calloutFound = True |
| 778 | fruCallout = str(addDataPiece[calloutIndex]).split('=')[1] |
| 779 | if("CALLOUT_DEVICE_PATH" in addDataPiece[i]): |
| 780 | i2creadFail = True |
Matt Spinler | d178a47 | 2018-08-31 09:48:52 -0500 | [diff] [blame] | 781 | |
| 782 | fruCallout = str(addDataPiece[calloutIndex]).split('=')[1] |
| 783 | |
| 784 | # Fall back to "I2C"/"FSI" if dev path isn't in policy table |
| 785 | if (messageID + '||' + fruCallout) not in policyTable: |
| 786 | i2cdevice = str(addDataPiece[i]).strip().split('=')[1] |
| 787 | i2cdevice = '/'.join(i2cdevice.split('/')[-4:]) |
| 788 | if 'fsi' in str(addDataPiece[calloutIndex]).split('=')[1]: |
| 789 | fruCallout = 'FSI' |
| 790 | else: |
| 791 | fruCallout = 'I2C' |
Justin Thaler | e34c43a | 2018-05-25 19:37:55 -0500 | [diff] [blame] | 792 | calloutFound = True |
| 793 | if("CALLOUT_GPIO_NUM" in addDataPiece[i]): |
| 794 | if not calloutFound: |
| 795 | fruCallout = 'GPIO' |
| 796 | calloutFound = True |
| 797 | if("CALLOUT_IIC_BUS" in addDataPiece[i]): |
| 798 | if not calloutFound: |
| 799 | fruCallout = "I2C" |
| 800 | calloutFound = True |
| 801 | if("CALLOUT_IPMI_SENSOR_NUM" in addDataPiece[i]): |
| 802 | if not calloutFound: |
| 803 | fruCallout = "IPMI" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 804 | calloutFound = True |
| 805 | if("ESEL" in addDataPiece[i]): |
| 806 | esel = str(addDataPiece[i]).strip().split('=')[1] |
Matt Spinler | 02d0dff | 2018-08-29 13:19:25 -0500 | [diff] [blame] | 807 | eselSeverity = getESELSeverity(esel) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 808 | if args.devdebug: |
| 809 | eselParts = parseESEL(args, esel) |
| 810 | hasEsel=True |
| 811 | if("GPU" in addDataPiece[i]): |
| 812 | fruCallout = '/xyz/openbmc_project/inventory/system/chassis/motherboard/gpu' + str(addDataPiece[i]).strip()[-1] |
| 813 | calloutFound = True |
| 814 | if("PROCEDURE" in addDataPiece[i]): |
| 815 | fruCallout = str(hex(int(str(addDataPiece[i]).split('=')[1])))[2:] |
| 816 | calloutFound = True |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 817 | if("RAIL_NAME" in addDataPiece[i]): |
| 818 | calloutFound=True |
| 819 | fruCallout = str(addDataPiece[i]).split('=')[1].strip() |
| 820 | if("INPUT_NAME" in addDataPiece[i]): |
| 821 | calloutFound=True |
| 822 | fruCallout = str(addDataPiece[i]).split('=')[1].strip() |
| 823 | if("SENSOR_TYPE" in addDataPiece[i]): |
| 824 | calloutFound=True |
| 825 | fruCallout = str(addDataPiece[i]).split('=')[1].strip() |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 826 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 827 | if(calloutFound): |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 828 | if fruCallout != "": |
| 829 | policyKey = messageID +"||" + fruCallout |
Matt Spinler | 02d0dff | 2018-08-29 13:19:25 -0500 | [diff] [blame] | 830 | |
| 831 | # Also use the severity for hostboot errors |
| 832 | if eselSeverity and messageID == 'org.open_power.Host.Error.Event': |
| 833 | policyKey += '||' + eselSeverity |
| 834 | |
| 835 | # if not in the table, fall back to the original key |
| 836 | if policyKey not in policyTable: |
| 837 | policyKey = policyKey.replace('||'+eselSeverity, '') |
| 838 | |
Justin Thaler | e34c43a | 2018-05-25 19:37:55 -0500 | [diff] [blame] | 839 | if policyKey not in policyTable: |
| 840 | policyKey = messageID |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 841 | else: |
| 842 | policyKey = messageID |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 843 | else: |
| 844 | policyKey = messageID |
| 845 | event = {} |
| 846 | eventNum = str(count) |
| 847 | if policyKey in policyTable: |
| 848 | for pkey in policyTable[policyKey]: |
| 849 | if(type(policyTable[policyKey][pkey])== bool): |
| 850 | event[pkey] = boolToString(policyTable[policyKey][pkey]) |
| 851 | else: |
| 852 | if (i2creadFail and pkey == 'Message'): |
| 853 | event[pkey] = policyTable[policyKey][pkey] + ' ' +i2cdevice |
| 854 | else: |
| 855 | event[pkey] = policyTable[policyKey][pkey] |
| 856 | event['timestamp'] = selEntries[key]['Timestamp'] |
| 857 | event['resolved'] = bool(selEntries[key]['Resolved']) |
| 858 | if(hasEsel): |
| 859 | if args.devdebug: |
| 860 | event['eselParts'] = eselParts |
| 861 | event['raweSEL'] = esel |
| 862 | event['logNum'] = key.split('/')[-1] |
| 863 | eventDict['event' + eventNum] = event |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 864 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 865 | else: |
| 866 | severity = str(selEntries[key]['Severity']).split('.')[-1] |
| 867 | if severity == 'Error': |
| 868 | severity = 'Critical' |
| 869 | eventDict['event'+eventNum] = {} |
| 870 | eventDict['event' + eventNum]['error'] = "error: Not found in policy table: " + policyKey |
| 871 | eventDict['event' + eventNum]['timestamp'] = selEntries[key]['Timestamp'] |
| 872 | eventDict['event' + eventNum]['Severity'] = severity |
| 873 | if(hasEsel): |
| 874 | if args.devdebug: |
| 875 | eventDict['event' +eventNum]['eselParts'] = eselParts |
| 876 | eventDict['event' +eventNum]['raweSEL'] = esel |
| 877 | eventDict['event' +eventNum]['logNum'] = key.split('/')[-1] |
| 878 | eventDict['event' +eventNum]['resolved'] = bool(selEntries[key]['Resolved']) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 879 | count += 1 |
| 880 | return eventDict |
| 881 | |
| 882 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 883 | def selDisplay(events, args): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 884 | """ |
| 885 | displays alerts in human readable format |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 886 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 887 | @param events: Dictionary containing events |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 888 | @return: |
| 889 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 890 | activeAlerts = [] |
| 891 | historyAlerts = [] |
| 892 | sortedEntries = sortSELs(events) |
| 893 | logNumList = sortedEntries[0] |
| 894 | eventKeyDict = sortedEntries[1] |
| 895 | keylist = ['Entry', 'ID', 'Timestamp', 'Serviceable', 'Severity','Message'] |
| 896 | if(args.devdebug): |
| 897 | colNames = ['Entry', 'ID', 'Timestamp', 'Serviceable', 'Severity','Message', 'eSEL contents'] |
| 898 | keylist.append('eSEL') |
| 899 | else: |
| 900 | colNames = ['Entry', 'ID', 'Timestamp', 'Serviceable', 'Severity', 'Message'] |
| 901 | for log in logNumList: |
| 902 | selDict = {} |
| 903 | alert = events[eventKeyDict[str(log)]] |
| 904 | if('error' in alert): |
| 905 | selDict['Entry'] = alert['logNum'] |
| 906 | selDict['ID'] = 'Unknown' |
| 907 | selDict['Timestamp'] = datetime.datetime.fromtimestamp(int(alert['timestamp']/1000)).strftime("%Y-%m-%d %H:%M:%S") |
| 908 | msg = alert['error'] |
| 909 | polMsg = msg.split("policy table:")[0] |
| 910 | msg = msg.split("policy table:")[1] |
| 911 | msgPieces = msg.split("||") |
| 912 | err = msgPieces[0] |
| 913 | if(err.find("org.open_power.")!=-1): |
| 914 | err = err.split("org.open_power.")[1] |
| 915 | elif(err.find("xyz.openbmc_project.")!=-1): |
| 916 | err = err.split("xyz.openbmc_project.")[1] |
| 917 | else: |
| 918 | err = msgPieces[0] |
| 919 | callout = "" |
| 920 | if len(msgPieces) >1: |
| 921 | callout = msgPieces[1] |
| 922 | if(callout.find("/org/open_power/")!=-1): |
| 923 | callout = callout.split("/org/open_power/")[1] |
| 924 | elif(callout.find("/xyz/openbmc_project/")!=-1): |
| 925 | callout = callout.split("/xyz/openbmc_project/")[1] |
| 926 | else: |
| 927 | callout = msgPieces[1] |
| 928 | selDict['Message'] = polMsg +"policy table: "+ err + "||" + callout |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 929 | selDict['Serviceable'] = 'Unknown' |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 930 | selDict['Severity'] = alert['Severity'] |
| 931 | else: |
| 932 | selDict['Entry'] = alert['logNum'] |
| 933 | selDict['ID'] = alert['CommonEventID'] |
| 934 | selDict['Timestamp'] = datetime.datetime.fromtimestamp(int(alert['timestamp']/1000)).strftime("%Y-%m-%d %H:%M:%S") |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 935 | selDict['Message'] = alert['Message'] |
| 936 | selDict['Serviceable'] = alert['Serviceable'] |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 937 | selDict['Severity'] = alert['Severity'] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 938 | |
| 939 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 940 | eselOrder = ['refCode','signatureDescription', 'eselType', 'devdesc', 'calloutType', 'procedure'] |
| 941 | if ('eselParts' in alert and args.devdebug): |
| 942 | eselOutput = "" |
| 943 | for item in eselOrder: |
| 944 | if item in alert['eselParts']: |
| 945 | eselOutput = eselOutput + item + ": " + alert['eselParts'][item] + " | " |
| 946 | selDict['eSEL'] = eselOutput |
| 947 | else: |
| 948 | if args.devdebug: |
| 949 | selDict['eSEL'] = "None" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 950 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 951 | if not alert['resolved']: |
| 952 | activeAlerts.append(selDict) |
| 953 | else: |
| 954 | historyAlerts.append(selDict) |
| 955 | mergedOutput = activeAlerts + historyAlerts |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 956 | colWidth = setColWidth(keylist, len(colNames), dict(enumerate(mergedOutput)), colNames) |
| 957 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 958 | output = "" |
| 959 | if(len(activeAlerts)>0): |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 960 | row = "" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 961 | output +="----Active Alerts----\n" |
| 962 | for i in range(0, len(colNames)): |
| 963 | if i!=0: row =row + "| " |
| 964 | row = row + colNames[i].ljust(colWidth[i]) |
| 965 | output += row + "\n" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 966 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 967 | for i in range(0,len(activeAlerts)): |
| 968 | row = "" |
| 969 | for j in range(len(activeAlerts[i])): |
| 970 | if (j != 0): row = row + "| " |
| 971 | row = row + activeAlerts[i][keylist[j]].ljust(colWidth[j]) |
| 972 | output += row + "\n" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 973 | |
| 974 | if(len(historyAlerts)>0): |
| 975 | row = "" |
| 976 | output+= "----Historical Alerts----\n" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 977 | for i in range(len(colNames)): |
| 978 | if i!=0: row =row + "| " |
| 979 | row = row + colNames[i].ljust(colWidth[i]) |
| 980 | output += row + "\n" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 981 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 982 | for i in range(0, len(historyAlerts)): |
| 983 | row = "" |
| 984 | for j in range(len(historyAlerts[i])): |
| 985 | if (j != 0): row = row + "| " |
| 986 | row = row + historyAlerts[i][keylist[j]].ljust(colWidth[j]) |
| 987 | output += row + "\n" |
| 988 | # print(events[eventKeyDict[str(log)]]) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 989 | return output |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 990 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 991 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 992 | def selPrint(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 993 | """ |
| 994 | prints out all bmc alerts |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 995 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 996 | @param host: string, the hostname or IP address of the bmc |
| 997 | @param args: contains additional arguments used by the fru sub command |
| 998 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 999 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1000 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1001 | if(args.policyTableLoc is None): |
| 1002 | if os.path.exists('policyTable.json'): |
| 1003 | ptableLoc = "policyTable.json" |
| 1004 | elif os.path.exists('/opt/ibm/ras/lib/policyTable.json'): |
| 1005 | ptableLoc = '/opt/ibm/ras/lib/policyTable.json' |
| 1006 | else: |
| 1007 | ptableLoc = 'lib/policyTable.json' |
| 1008 | else: |
| 1009 | ptableLoc = args.policyTableLoc |
| 1010 | policyTable = loadPolicyTable(ptableLoc) |
| 1011 | rawselEntries = "" |
| 1012 | if(hasattr(args, 'fileloc') and args.fileloc is not None): |
| 1013 | if os.path.exists(args.fileloc): |
| 1014 | with open(args.fileloc, 'r') as selFile: |
| 1015 | selLines = selFile.readlines() |
| 1016 | rawselEntries = ''.join(selLines) |
| 1017 | else: |
| 1018 | print("Error: File not found") |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1019 | sys.exit(1) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1020 | else: |
| 1021 | rawselEntries = sel(host, args, session) |
| 1022 | loadFailed = False |
| 1023 | try: |
| 1024 | selEntries = json.loads(rawselEntries) |
| 1025 | except ValueError: |
| 1026 | loadFailed = True |
| 1027 | if loadFailed: |
| 1028 | cleanSels = json.dumps(rawselEntries).replace('\\n', '') |
| 1029 | #need to load json twice as original content was string escaped a second time |
| 1030 | selEntries = json.loads(json.loads(cleanSels)) |
| 1031 | selEntries = selEntries['data'] |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1032 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1033 | if 'description' in selEntries: |
| 1034 | if(args.json): |
| 1035 | return("{\n\t\"numAlerts\": 0\n}") |
| 1036 | else: |
| 1037 | return("No log entries found") |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1038 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1039 | else: |
| 1040 | if(len(policyTable)>0): |
| 1041 | events = parseAlerts(policyTable, selEntries, args) |
| 1042 | if(args.json): |
| 1043 | events["numAlerts"] = len(events) |
| 1044 | retValue = str(json.dumps(events, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False)) |
| 1045 | return retValue |
| 1046 | elif(hasattr(args, 'fullSel')): |
| 1047 | return events |
| 1048 | else: |
| 1049 | #get log numbers to order event entries sequentially |
| 1050 | return selDisplay(events, args) |
| 1051 | else: |
| 1052 | if(args.json): |
| 1053 | return selEntries |
| 1054 | else: |
| 1055 | print("error: Policy Table not found.") |
| 1056 | return selEntries |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1057 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1058 | def selList(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1059 | """ |
| 1060 | prints out all all bmc alerts, or only prints out the specified alerts |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1061 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1062 | @param host: string, the hostname or IP address of the bmc |
| 1063 | @param args: contains additional arguments used by the fru sub command |
| 1064 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1065 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1066 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1067 | return(sel(host, args, session)) |
| 1068 | |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1069 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1070 | def selClear(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1071 | """ |
| 1072 | clears all alerts |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1073 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1074 | @param host: string, the hostname or IP address of the bmc |
| 1075 | @param args: contains additional arguments used by the fru sub command |
| 1076 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1077 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1078 | """ |
Matt Spinler | 47b13e9 | 2019-01-04 14:58:53 -0600 | [diff] [blame] | 1079 | url="https://"+host+"/xyz/openbmc_project/logging/action/DeleteAll" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1080 | data = "{\"data\": [] }" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1081 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1082 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1083 | res = session.post(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1084 | except(requests.exceptions.Timeout): |
| 1085 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1086 | if res.status_code == 200: |
| 1087 | return "The Alert Log has been cleared. Please allow a few minutes for the action to complete." |
| 1088 | else: |
| 1089 | print("Unable to clear the logs, trying to clear 1 at a time") |
| 1090 | sels = json.loads(sel(host, args, session))['data'] |
| 1091 | for key in sels: |
| 1092 | if 'callout' not in key: |
| 1093 | logNum = key.split('/')[-1] |
| 1094 | url = "https://"+ host+ "/xyz/openbmc_project/logging/entry/"+logNum+"/action/Delete" |
| 1095 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1096 | session.post(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1097 | except(requests.exceptions.Timeout): |
| 1098 | return connectionErrHandler(args.json, "Timeout", None) |
| 1099 | sys.exit(1) |
| 1100 | except(requests.exceptions.ConnectionError) as err: |
| 1101 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 1102 | sys.exit(1) |
| 1103 | return ('Sel clearing complete') |
| 1104 | |
| 1105 | def selSetResolved(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1106 | """ |
| 1107 | sets a sel entry to resolved |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1108 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1109 | @param host: string, the hostname or IP address of the bmc |
| 1110 | @param args: contains additional arguments used by the fru sub command |
| 1111 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1112 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1113 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1114 | url="https://"+host+"/xyz/openbmc_project/logging/entry/" + str(args.selNum) + "/attr/Resolved" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1115 | data = "{\"data\": 1 }" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1116 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1117 | res = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1118 | except(requests.exceptions.Timeout): |
| 1119 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1120 | if res.status_code == 200: |
| 1121 | return "Sel entry "+ str(args.selNum) +" is now set to resolved" |
| 1122 | else: |
| 1123 | return "Unable to set the alert to resolved" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1124 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1125 | def selResolveAll(host, args, session): |
| 1126 | """ |
| 1127 | sets a sel entry to resolved |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1128 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1129 | @param host: string, the hostname or IP address of the bmc |
| 1130 | @param args: contains additional arguments used by the fru sub command |
| 1131 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1132 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1133 | """ |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1134 | rawselEntries = sel(host, args, session) |
| 1135 | loadFailed = False |
| 1136 | try: |
| 1137 | selEntries = json.loads(rawselEntries) |
| 1138 | except ValueError: |
| 1139 | loadFailed = True |
| 1140 | if loadFailed: |
| 1141 | cleanSels = json.dumps(rawselEntries).replace('\\n', '') |
| 1142 | #need to load json twice as original content was string escaped a second time |
| 1143 | selEntries = json.loads(json.loads(cleanSels)) |
| 1144 | selEntries = selEntries['data'] |
| 1145 | |
| 1146 | if 'description' in selEntries: |
| 1147 | if(args.json): |
| 1148 | return("{\n\t\"selsResolved\": 0\n}") |
| 1149 | else: |
| 1150 | return("No log entries found") |
| 1151 | else: |
| 1152 | d = vars(args) |
| 1153 | successlist = [] |
| 1154 | failedlist = [] |
| 1155 | for key in selEntries: |
| 1156 | if 'callout' not in key: |
| 1157 | d['selNum'] = key.split('/')[-1] |
| 1158 | resolved = selSetResolved(host,args,session) |
| 1159 | if 'Sel entry' in resolved: |
| 1160 | successlist.append(d['selNum']) |
| 1161 | else: |
| 1162 | failedlist.append(d['selNum']) |
| 1163 | output = "" |
| 1164 | successlist.sort() |
| 1165 | failedlist.sort() |
| 1166 | if len(successlist)>0: |
| 1167 | output = "Successfully resolved: " +', '.join(successlist) +"\n" |
| 1168 | if len(failedlist)>0: |
| 1169 | output += "Failed to resolve: " + ', '.join(failedlist) + "\n" |
| 1170 | return output |
| 1171 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1172 | def chassisPower(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1173 | """ |
| 1174 | called by the chassis function. Controls the power state of the chassis, or gets the status |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1175 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1176 | @param host: string, the hostname or IP address of the bmc |
| 1177 | @param args: contains additional arguments used by the fru sub command |
| 1178 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1179 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1180 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1181 | if(args.powcmd == 'on'): |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1182 | if checkFWactivation(host, args, session): |
| 1183 | return ("Chassis Power control disabled during firmware activation") |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1184 | print("Attempting to Power on...:") |
| 1185 | url="https://"+host+"/xyz/openbmc_project/state/host0/attr/RequestedHostTransition" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1186 | data = '{"data":"xyz.openbmc_project.State.Host.Transition.On"}' |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1187 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1188 | res = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1189 | except(requests.exceptions.Timeout): |
| 1190 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1191 | return res.text |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1192 | elif(args.powcmd == 'softoff'): |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1193 | if checkFWactivation(host, args, session): |
| 1194 | return ("Chassis Power control disabled during firmware activation") |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1195 | print("Attempting to Power off gracefully...:") |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1196 | url="https://"+host+"/xyz/openbmc_project/state/host0/attr/RequestedHostTransition" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1197 | data = '{"data":"xyz.openbmc_project.State.Host.Transition.Off"}' |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1198 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1199 | res = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1200 | except(requests.exceptions.Timeout): |
| 1201 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 1202 | return res.text |
| 1203 | elif(args.powcmd == 'hardoff'): |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1204 | if checkFWactivation(host, args, session): |
| 1205 | return ("Chassis Power control disabled during firmware activation") |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1206 | print("Attempting to Power off immediately...:") |
| 1207 | url="https://"+host+"/xyz/openbmc_project/state/chassis0/attr/RequestedPowerTransition" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1208 | data = '{"data":"xyz.openbmc_project.State.Chassis.Transition.Off"}' |
| 1209 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1210 | res = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1211 | except(requests.exceptions.Timeout): |
| 1212 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1213 | return res.text |
| 1214 | elif(args.powcmd == 'status'): |
| 1215 | url="https://"+host+"/xyz/openbmc_project/state/chassis0/attr/CurrentPowerState" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1216 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1217 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1218 | except(requests.exceptions.Timeout): |
| 1219 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1220 | chassisState = json.loads(res.text)['data'].split('.')[-1] |
| 1221 | url="https://"+host+"/xyz/openbmc_project/state/host0/attr/CurrentHostState" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1222 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1223 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1224 | except(requests.exceptions.Timeout): |
| 1225 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1226 | hostState = json.loads(res.text)['data'].split('.')[-1] |
| 1227 | url="https://"+host+"/xyz/openbmc_project/state/bmc0/attr/CurrentBMCState" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1228 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1229 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1230 | except(requests.exceptions.Timeout): |
| 1231 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1232 | bmcState = json.loads(res.text)['data'].split('.')[-1] |
| 1233 | if(args.json): |
| 1234 | outDict = {"Chassis Power State" : chassisState, "Host Power State" : hostState, "BMC Power State":bmcState} |
| 1235 | return json.dumps(outDict, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False) |
| 1236 | else: |
| 1237 | return "Chassis Power State: " +chassisState + "\nHost Power State: " + hostState + "\nBMC Power State: " + bmcState |
| 1238 | else: |
| 1239 | return "Invalid chassis power command" |
| 1240 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1241 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1242 | def chassisIdent(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1243 | """ |
| 1244 | called by the chassis function. Controls the identify led of the chassis. Sets or gets the state |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1245 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1246 | @param host: string, the hostname or IP address of the bmc |
| 1247 | @param args: contains additional arguments used by the fru sub command |
| 1248 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1249 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1250 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1251 | if(args.identcmd == 'on'): |
| 1252 | print("Attempting to turn identify light on...:") |
| 1253 | url="https://"+host+"/xyz/openbmc_project/led/groups/enclosure_identify/attr/Asserted" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1254 | data = '{"data":true}' |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1255 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1256 | res = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1257 | except(requests.exceptions.Timeout): |
| 1258 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1259 | return res.text |
| 1260 | elif(args.identcmd == 'off'): |
| 1261 | print("Attempting to turn identify light off...:") |
| 1262 | url="https://"+host+"/xyz/openbmc_project/led/groups/enclosure_identify/attr/Asserted" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1263 | data = '{"data":false}' |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1264 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1265 | res = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1266 | except(requests.exceptions.Timeout): |
| 1267 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1268 | return res.text |
| 1269 | elif(args.identcmd == 'status'): |
| 1270 | url="https://"+host+"/xyz/openbmc_project/led/groups/enclosure_identify" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1271 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1272 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1273 | except(requests.exceptions.Timeout): |
| 1274 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1275 | status = json.loads(res.text)['data'] |
| 1276 | if(args.json): |
| 1277 | return status |
| 1278 | else: |
| 1279 | if status['Asserted'] == 0: |
| 1280 | return "Identify light is off" |
| 1281 | else: |
| 1282 | return "Identify light is blinking" |
| 1283 | else: |
| 1284 | return "Invalid chassis identify command" |
| 1285 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1286 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1287 | def chassis(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1288 | """ |
| 1289 | controls the different chassis commands |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1290 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1291 | @param host: string, the hostname or IP address of the bmc |
| 1292 | @param args: contains additional arguments used by the fru sub command |
| 1293 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1294 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1295 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1296 | if(hasattr(args, 'powcmd')): |
| 1297 | result = chassisPower(host,args,session) |
| 1298 | elif(hasattr(args, 'identcmd')): |
| 1299 | result = chassisIdent(host, args, session) |
| 1300 | else: |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1301 | return "This feature is not yet implemented" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1302 | return result |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1303 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1304 | def bmcDumpRetrieve(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1305 | """ |
| 1306 | Downloads a dump file from the bmc |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1307 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1308 | @param host: string, the hostname or IP address of the bmc |
| 1309 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1310 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1311 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1312 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1313 | dumpNum = args.dumpNum |
| 1314 | if (args.dumpSaveLoc is not None): |
| 1315 | saveLoc = args.dumpSaveLoc |
| 1316 | else: |
Justin Thaler | cf1deae | 2018-05-25 19:35:21 -0500 | [diff] [blame] | 1317 | saveLoc = tempfile.gettempdir() |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1318 | url ='https://'+host+'/download/dump/' + str(dumpNum) |
| 1319 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1320 | r = session.get(url, headers=jsonHeader, stream=True, verify=False, timeout=baseTimeout) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1321 | if (args.dumpSaveLoc is not None): |
| 1322 | if os.path.exists(saveLoc): |
| 1323 | if saveLoc[-1] != os.path.sep: |
| 1324 | saveLoc = saveLoc + os.path.sep |
| 1325 | filename = saveLoc + host+'-dump' + str(dumpNum) + '.tar.xz' |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1326 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1327 | else: |
| 1328 | return 'Invalid save location specified' |
| 1329 | else: |
Justin Thaler | cf1deae | 2018-05-25 19:35:21 -0500 | [diff] [blame] | 1330 | filename = tempfile.gettempdir()+os.sep + host+'-dump' + str(dumpNum) + '.tar.xz' |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1331 | |
| 1332 | with open(filename, 'wb') as f: |
| 1333 | for chunk in r.iter_content(chunk_size =1024): |
| 1334 | if chunk: |
| 1335 | f.write(chunk) |
| 1336 | return 'Saved as ' + filename |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1337 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1338 | except(requests.exceptions.Timeout): |
| 1339 | return connectionErrHandler(args.json, "Timeout", None) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1340 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1341 | except(requests.exceptions.ConnectionError) as err: |
| 1342 | return connectionErrHandler(args.json, "ConnectionError", err) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1343 | |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1344 | def bmcDumpList(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1345 | """ |
| 1346 | Lists the number of dump files on the bmc |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1347 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1348 | @param host: string, the hostname or IP address of the bmc |
| 1349 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1350 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1351 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1352 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1353 | url ='https://'+host+'/xyz/openbmc_project/dump/list' |
| 1354 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1355 | r = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | 3a5771b | 2019-01-23 14:31:52 -0600 | [diff] [blame] | 1356 | dumpList = r.json() |
| 1357 | return dumpList |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1358 | except(requests.exceptions.Timeout): |
| 1359 | return connectionErrHandler(args.json, "Timeout", None) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1360 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1361 | except(requests.exceptions.ConnectionError) as err: |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1362 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 1363 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1364 | def bmcDumpDelete(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1365 | """ |
| 1366 | Deletes BMC dump files from the bmc |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1367 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1368 | @param host: string, the hostname or IP address of the bmc |
| 1369 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1370 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1371 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1372 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1373 | dumpList = [] |
| 1374 | successList = [] |
| 1375 | failedList = [] |
| 1376 | if args.dumpNum is not None: |
| 1377 | if isinstance(args.dumpNum, list): |
| 1378 | dumpList = args.dumpNum |
| 1379 | else: |
| 1380 | dumpList.append(args.dumpNum) |
| 1381 | for dumpNum in dumpList: |
| 1382 | url ='https://'+host+'/xyz/openbmc_project/dump/entry/'+str(dumpNum)+'/action/Delete' |
| 1383 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1384 | r = session.post(url, headers=jsonHeader, json = {"data": []}, verify=False, timeout=baseTimeout) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1385 | if r.status_code == 200: |
| 1386 | successList.append(str(dumpNum)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1387 | else: |
| 1388 | failedList.append(str(dumpNum)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1389 | except(requests.exceptions.Timeout): |
| 1390 | return connectionErrHandler(args.json, "Timeout", None) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1391 | except(requests.exceptions.ConnectionError) as err: |
| 1392 | return connectionErrHandler(args.json, "ConnectionError", err) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1393 | output = "Successfully deleted dumps: " + ', '.join(successList) |
| 1394 | if(len(failedList)>0): |
| 1395 | output+= '\nFailed to delete dumps: ' + ', '.join(failedList) |
| 1396 | return output |
| 1397 | else: |
| 1398 | return 'You must specify an entry number to delete' |
| 1399 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1400 | def bmcDumpDeleteAll(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1401 | """ |
| 1402 | Deletes All BMC dump files from the bmc |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1403 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1404 | @param host: string, the hostname or IP address of the bmc |
| 1405 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1406 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1407 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1408 | """ |
| 1409 | dumpResp = bmcDumpList(host, args, session) |
| 1410 | if 'FQPSPIN0000M' in dumpResp or 'FQPSPIN0001M'in dumpResp: |
| 1411 | return dumpResp |
Justin Thaler | 3a5771b | 2019-01-23 14:31:52 -0600 | [diff] [blame] | 1412 | dumpList = dumpResp['data'] |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1413 | d = vars(args) |
| 1414 | dumpNums = [] |
| 1415 | for dump in dumpList: |
| 1416 | if '/xyz/openbmc_project/dump/internal/manager' not in dump: |
| 1417 | dumpNums.append(int(dump.strip().split('/')[-1])) |
| 1418 | d['dumpNum'] = dumpNums |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1419 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1420 | return bmcDumpDelete(host, args, session) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1421 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1422 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1423 | def bmcDumpCreate(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1424 | """ |
| 1425 | Creates a bmc dump file |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1426 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1427 | @param host: string, the hostname or IP address of the bmc |
| 1428 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1429 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1430 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1431 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1432 | url = 'https://'+host+'/xyz/openbmc_project/dump/action/CreateDump' |
| 1433 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 1434 | r = session.post(url, headers=jsonHeader, json = {"data": []}, verify=False, timeout=baseTimeout) |
Matt Spinler | eae05b0 | 2019-01-24 12:59:34 -0600 | [diff] [blame] | 1435 | if(r.status_code == 200 and not args.json): |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1436 | return ('Dump successfully created') |
Justin Thaler | 3a5771b | 2019-01-23 14:31:52 -0600 | [diff] [blame] | 1437 | elif(args.json): |
| 1438 | return r.json() |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1439 | else: |
| 1440 | return ('Failed to create dump') |
| 1441 | except(requests.exceptions.Timeout): |
| 1442 | return connectionErrHandler(args.json, "Timeout", None) |
| 1443 | except(requests.exceptions.ConnectionError) as err: |
| 1444 | return connectionErrHandler(args.json, "ConnectionError", err) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1445 | |
| 1446 | |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1447 | def csdDumpInitiate(host, args, session): |
| 1448 | """ |
| 1449 | Starts the process of getting the current list of dumps then initiates the creation of one. |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1450 | |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1451 | @param host: string, the hostname or IP address of the bmc |
| 1452 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1453 | @param session: the active session to use |
| 1454 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1455 | """ |
| 1456 | errorInfo = "" |
| 1457 | dumpcount = 0 |
| 1458 | try: |
| 1459 | d = vars(args) |
| 1460 | d['json'] = True |
| 1461 | except Exception as e: |
| 1462 | errorInfo += "Failed to set the json flag to True \n Exception: {eInfo}\n".format(eInfo=e) |
| 1463 | |
| 1464 | try: |
| 1465 | for i in range(3): |
| 1466 | dumpInfo = bmcDumpList(host, args, session) |
| 1467 | if 'data' in dumpInfo: |
| 1468 | dumpcount = len(dumpInfo['data']) |
| 1469 | break |
| 1470 | else: |
| 1471 | errorInfo+= "Dump List Message returned: " + json.dumps(dumpInfo,indent=0, separators=(',', ':')).replace('\n','') +"\n" |
| 1472 | except Exception as e: |
| 1473 | errorInfo+= "Failed to collect the list of dumps.\nException: {eInfo}\n".format(eInfo=e) |
| 1474 | |
| 1475 | #Create a user initiated dump |
| 1476 | try: |
| 1477 | for i in range(3): |
| 1478 | dumpcreated = bmcDumpCreate(host, args, session) |
| 1479 | if 'message' in dumpcreated: |
| 1480 | if 'ok' in dumpcreated['message'].lower(): |
| 1481 | break |
| 1482 | else: |
| 1483 | errorInfo+= "Dump create message returned: " + json.dumps(dumpInfo,indent=0, separators=(',', ':')).replace('\n','') +"\n" |
| 1484 | else: |
| 1485 | errorInfo+= "Dump create message returned: " + json.dumps(dumpInfo,indent=0, separators=(',', ':')).replace('\n','') +"\n" |
| 1486 | except Exception as e: |
| 1487 | errorInfo+= "Dump create exception encountered: {eInfo}\n".format(eInfo=e) |
| 1488 | |
| 1489 | output = {} |
| 1490 | output['errors'] = errorInfo |
| 1491 | output['dumpcount'] = dumpcount |
| 1492 | return output |
| 1493 | |
| 1494 | def csdInventory(host, args,session, fileDir): |
| 1495 | """ |
| 1496 | Collects the BMC inventory, retrying if necessary |
| 1497 | |
| 1498 | @param host: string, the hostname or IP address of the bmc |
| 1499 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1500 | @param session: the active session to use |
| 1501 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1502 | @param fileDir: string representation of the path to use for putting files created |
| 1503 | """ |
| 1504 | errorInfo = "===========Inventory =============\n" |
| 1505 | output={} |
| 1506 | inventoryCollected = False |
| 1507 | try: |
| 1508 | for i in range(3): |
| 1509 | frulist = fruPrint(host, args, session) |
| 1510 | if 'Hardware' in frulist: |
| 1511 | inventoryCollected = True |
| 1512 | break |
| 1513 | else: |
| 1514 | errorInfo += json.dumps(frulist, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False) + '\n' |
| 1515 | except Exception as e: |
| 1516 | errorInfo += "Inventory collection exception: {eInfo}\n".format(eInfo=e) |
| 1517 | if inventoryCollected: |
| 1518 | try: |
| 1519 | with open(fileDir +os.sep+'inventory.txt', 'w') as f: |
| 1520 | f.write(json.dumps(frulist, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False) + '\n') |
| 1521 | print("Inventory collected and stored in " + fileDir + os.sep + "inventory.txt") |
| 1522 | output['fileLoc'] = fileDir+os.sep+'inventory.txt' |
| 1523 | except Exception as e: |
| 1524 | print("Failed to write inventory to file.") |
| 1525 | errorInfo += "Error writing inventory to the file. Exception: {eInfo}\n".format(eInfo=e) |
| 1526 | |
| 1527 | output['errors'] = errorInfo |
| 1528 | |
| 1529 | return output |
| 1530 | |
| 1531 | def csdSensors(host, args,session, fileDir): |
| 1532 | """ |
| 1533 | Collects the BMC sensor readings, retrying if necessary |
| 1534 | |
| 1535 | @param host: string, the hostname or IP address of the bmc |
| 1536 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1537 | @param session: the active session to use |
| 1538 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1539 | @param fileDir: string representation of the path to use for putting files created |
| 1540 | """ |
| 1541 | errorInfo = "===========Sensors =============\n" |
| 1542 | sensorsCollected = False |
| 1543 | output={} |
| 1544 | try: |
| 1545 | d = vars(args) |
| 1546 | d['json'] = False |
| 1547 | except Exception as e: |
| 1548 | errorInfo += "Failed to set the json flag to False \n Exception: {eInfo}\n".format(eInfo=e) |
| 1549 | |
| 1550 | try: |
| 1551 | for i in range(3): |
| 1552 | sensorReadings = sensor(host, args, session) |
| 1553 | if 'OCC0' in sensorReadings: |
| 1554 | sensorsCollected = True |
| 1555 | break |
| 1556 | else: |
| 1557 | errorInfo += sensorReadings |
| 1558 | except Exception as e: |
| 1559 | errorInfo += "Sensor reading collection exception: {eInfo}\n".format(eInfo=e) |
| 1560 | if sensorsCollected: |
| 1561 | try: |
| 1562 | with open(fileDir +os.sep+'sensorReadings.txt', 'w') as f: |
| 1563 | f.write(sensorReadings) |
| 1564 | print("Sensor readings collected and stored in " + fileDir + os.sep+ "sensorReadings.txt") |
| 1565 | output['fileLoc'] = fileDir+os.sep+'sensorReadings.txt' |
| 1566 | except Exception as e: |
| 1567 | print("Failed to write sensor readings to file system.") |
| 1568 | errorInfo += "Error writing sensor readings to the file. Exception: {eInfo}\n".format(eInfo=e) |
| 1569 | |
| 1570 | output['errors'] = errorInfo |
| 1571 | return output |
| 1572 | |
| 1573 | def csdLEDs(host,args, session, fileDir): |
| 1574 | """ |
| 1575 | Collects the BMC LED status, retrying if necessary |
| 1576 | |
| 1577 | @param host: string, the hostname or IP address of the bmc |
| 1578 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1579 | @param session: the active session to use |
| 1580 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1581 | @param fileDir: string representation of the path to use for putting files created |
| 1582 | """ |
| 1583 | errorInfo = "===========LEDs =============\n" |
| 1584 | ledsCollected = False |
| 1585 | output={} |
| 1586 | try: |
| 1587 | d = vars(args) |
| 1588 | d['json'] = True |
| 1589 | except Exception as e: |
| 1590 | errorInfo += "Failed to set the json flag to False \n Exception: {eInfo}\n".format(eInfo=e) |
| 1591 | try: |
| 1592 | url="https://"+host+"/xyz/openbmc_project/led/enumerate" |
| 1593 | httpHeader = {'Content-Type':'application/json'} |
| 1594 | for i in range(3): |
| 1595 | try: |
| 1596 | ledRes = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
| 1597 | if ledRes.status_code == 200: |
| 1598 | ledsCollected = True |
| 1599 | leds = ledRes.json()['data'] |
| 1600 | break |
| 1601 | else: |
| 1602 | errorInfo += ledRes.text |
| 1603 | except(requests.exceptions.Timeout): |
| 1604 | errorInfo+=json.dumps( connectionErrHandler(args.json, "Timeout", None), sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False) + '\n' |
| 1605 | except(requests.exceptions.ConnectionError) as err: |
| 1606 | errorInfo += json.dumps(connectionErrHandler(args.json, "ConnectionError", err), sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False) + '\n' |
| 1607 | except Exception as e: |
| 1608 | errorInfo += "LED status collection exception: {eInfo}\n".format(eInfo=e) |
| 1609 | |
| 1610 | if ledsCollected: |
| 1611 | try: |
| 1612 | with open(fileDir +os.sep+'ledStatus.txt', 'w') as f: |
| 1613 | f.write(json.dumps(leds, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False) + '\n') |
| 1614 | print("LED status collected and stored in " + fileDir + os.sep+ "ledStatus.txt") |
| 1615 | output['fileLoc'] = fileDir+os.sep+'ledStatus.txt' |
| 1616 | except Exception as e: |
| 1617 | print("Failed to write LED status to file system.") |
| 1618 | errorInfo += "Error writing LED status to the file. Exception: {eInfo}\n".format(eInfo=e) |
| 1619 | |
| 1620 | output['errors'] = errorInfo |
| 1621 | return output |
| 1622 | |
| 1623 | def csdSelShortList(host, args, session, fileDir): |
| 1624 | """ |
| 1625 | Collects the BMC log entries, retrying if necessary |
| 1626 | |
| 1627 | @param host: string, the hostname or IP address of the bmc |
| 1628 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1629 | @param session: the active session to use |
| 1630 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1631 | @param fileDir: string representation of the path to use for putting files created |
| 1632 | """ |
| 1633 | errorInfo = "===========SEL Short List =============\n" |
| 1634 | selsCollected = False |
| 1635 | output={} |
| 1636 | try: |
| 1637 | d = vars(args) |
| 1638 | d['json'] = False |
| 1639 | except Exception as e: |
| 1640 | errorInfo += "Failed to set the json flag to False \n Exception: {eInfo}\n".format(eInfo=e) |
| 1641 | |
| 1642 | try: |
| 1643 | for i in range(3): |
| 1644 | sels = selPrint(host,args,session) |
| 1645 | if '----Active Alerts----' in sels or 'No log entries found' in sels or '----Historical Alerts----' in sels: |
| 1646 | selsCollected = True |
| 1647 | break |
| 1648 | else: |
| 1649 | errorInfo += sels + '\n' |
| 1650 | except Exception as e: |
| 1651 | errorInfo += "SEL short list collection exception: {eInfo}\n".format(eInfo=e) |
| 1652 | |
| 1653 | if selsCollected: |
| 1654 | try: |
| 1655 | with open(fileDir +os.sep+'SELshortlist.txt', 'w') as f: |
| 1656 | f.write(sels) |
| 1657 | print("SEL short list collected and stored in " + fileDir + os.sep+ "SELshortlist.txt") |
| 1658 | output['fileLoc'] = fileDir+os.sep+'SELshortlist.txt' |
| 1659 | except Exception as e: |
| 1660 | print("Failed to write SEL short list to file system.") |
| 1661 | errorInfo += "Error writing SEL short list to the file. Exception: {eInfo}\n".format(eInfo=e) |
| 1662 | |
| 1663 | output['errors'] = errorInfo |
| 1664 | return output |
| 1665 | |
| 1666 | def csdParsedSels(host, args, session, fileDir): |
| 1667 | """ |
| 1668 | Collects the BMC log entries, retrying if necessary |
| 1669 | |
| 1670 | @param host: string, the hostname or IP address of the bmc |
| 1671 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1672 | @param session: the active session to use |
| 1673 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1674 | @param fileDir: string representation of the path to use for putting files created |
| 1675 | """ |
| 1676 | errorInfo = "===========SEL Parsed List =============\n" |
| 1677 | selsCollected = False |
| 1678 | output={} |
| 1679 | try: |
| 1680 | d = vars(args) |
| 1681 | d['json'] = True |
| 1682 | d['fullEsel'] = True |
| 1683 | except Exception as e: |
| 1684 | errorInfo += "Failed to set the json flag to True \n Exception: {eInfo}\n".format(eInfo=e) |
| 1685 | |
| 1686 | try: |
| 1687 | for i in range(3): |
| 1688 | parsedfullsels = json.loads(selPrint(host,args,session)) |
| 1689 | if 'numAlerts' in parsedfullsels: |
| 1690 | selsCollected = True |
| 1691 | break |
| 1692 | else: |
| 1693 | errorInfo += parsedfullsels + '\n' |
| 1694 | except Exception as e: |
| 1695 | errorInfo += "Parsed full SELs collection exception: {eInfo}\n".format(eInfo=e) |
| 1696 | |
| 1697 | if selsCollected: |
| 1698 | try: |
| 1699 | sortedSELs = sortSELs(parsedfullsels) |
| 1700 | with open(fileDir +os.sep+'parsedSELs.txt', 'w') as f: |
| 1701 | for log in sortedSELs[0]: |
| 1702 | esel = "" |
| 1703 | parsedfullsels[sortedSELs[1][str(log)]]['timestamp'] = datetime.datetime.fromtimestamp(int(parsedfullsels[sortedSELs[1][str(log)]]['timestamp']/1000)).strftime("%Y-%m-%d %H:%M:%S") |
| 1704 | if ('raweSEL' in parsedfullsels[sortedSELs[1][str(log)]] and args.devdebug): |
| 1705 | esel = parsedfullsels[sortedSELs[1][str(log)]]['raweSEL'] |
| 1706 | del parsedfullsels[sortedSELs[1][str(log)]]['raweSEL'] |
| 1707 | f.write(json.dumps(parsedfullsels[sortedSELs[1][str(log)]],sort_keys=True, indent=4, separators=(',', ': '))) |
| 1708 | if(args.devdebug and esel != ""): |
| 1709 | f.write(parseESEL(args, esel)) |
| 1710 | print("Parsed SELs collected and stored in " + fileDir + os.sep+ "parsedSELs.txt") |
| 1711 | output['fileLoc'] = fileDir+os.sep+'parsedSELs.txt' |
| 1712 | except Exception as e: |
| 1713 | print("Failed to write fully parsed SELs to file system.") |
| 1714 | errorInfo += "Error writing fully parsed SELs to the file. Exception: {eInfo}\n".format(eInfo=e) |
| 1715 | |
| 1716 | output['errors'] = errorInfo |
| 1717 | return output |
| 1718 | |
| 1719 | def csdFullEnumeration(host, args, session, fileDir): |
| 1720 | """ |
| 1721 | Collects a full enumeration of /xyz/openbmc_project/, retrying if necessary |
| 1722 | |
| 1723 | @param host: string, the hostname or IP address of the bmc |
| 1724 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1725 | @param session: the active session to use |
| 1726 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1727 | @param fileDir: string representation of the path to use for putting files created |
| 1728 | """ |
| 1729 | errorInfo = "===========BMC Full Enumeration =============\n" |
| 1730 | bmcFullCollected = False |
| 1731 | output={} |
| 1732 | try: |
| 1733 | d = vars(args) |
| 1734 | d['json'] = True |
| 1735 | except Exception as e: |
| 1736 | errorInfo += "Failed to set the json flag to False \n Exception: {eInfo}\n".format(eInfo=e) |
| 1737 | try: |
| 1738 | print("Attempting to get a full BMC enumeration") |
| 1739 | url="https://"+host+"/xyz/openbmc_project/enumerate" |
| 1740 | httpHeader = {'Content-Type':'application/json'} |
| 1741 | for i in range(3): |
| 1742 | try: |
| 1743 | bmcRes = session.get(url, headers=jsonHeader, verify=False, timeout=180) |
| 1744 | if bmcRes.status_code == 200: |
| 1745 | bmcFullCollected = True |
| 1746 | fullEnumeration = bmcRes.json() |
| 1747 | break |
| 1748 | else: |
| 1749 | errorInfo += bmcRes.text |
| 1750 | except(requests.exceptions.Timeout): |
| 1751 | errorInfo+=json.dumps( connectionErrHandler(args.json, "Timeout", None), sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False) + '\n' |
| 1752 | except(requests.exceptions.ConnectionError) as err: |
| 1753 | errorInfo += json.dumps(connectionErrHandler(args.json, "ConnectionError", err), sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False) + '\n' |
| 1754 | except Exception as e: |
| 1755 | errorInfo += "RAW BMC data collection exception: {eInfo}\n".format(eInfo=e) |
| 1756 | |
| 1757 | if bmcFullCollected: |
| 1758 | try: |
| 1759 | with open(fileDir +os.sep+'bmcFullRaw.txt', 'w') as f: |
| 1760 | f.write(json.dumps(fullEnumeration, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False) + '\n') |
| 1761 | print("RAW BMC data collected and saved into " + fileDir + os.sep+ "bmcFullRaw.txt") |
| 1762 | output['fileLoc'] = fileDir+os.sep+'bmcFullRaw.txt' |
| 1763 | except Exception as e: |
| 1764 | print("Failed to write RAW BMC data to file system.") |
| 1765 | errorInfo += "Error writing RAW BMC data collection to the file. Exception: {eInfo}\n".format(eInfo=e) |
| 1766 | |
| 1767 | output['errors'] = errorInfo |
| 1768 | return output |
| 1769 | |
| 1770 | def csdCollectAllDumps(host, args, session, fileDir): |
| 1771 | """ |
| 1772 | Collects all of the bmc dump files and stores them in fileDir |
| 1773 | |
| 1774 | @param host: string, the hostname or IP address of the bmc |
| 1775 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1776 | @param session: the active session to use |
| 1777 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1778 | @param fileDir: string representation of the path to use for putting files created |
| 1779 | """ |
| 1780 | |
| 1781 | errorInfo = "===========BMC Dump Collection =============\n" |
| 1782 | dumpListCollected = False |
| 1783 | output={} |
| 1784 | dumpList = {} |
| 1785 | try: |
| 1786 | d = vars(args) |
| 1787 | d['json'] = True |
| 1788 | d['dumpSaveLoc'] = fileDir |
| 1789 | except Exception as e: |
| 1790 | errorInfo += "Failed to set the json flag to True, or failed to set the dumpSave Location \n Exception: {eInfo}\n".format(eInfo=e) |
| 1791 | |
| 1792 | print('Collecting bmc dump files') |
| 1793 | |
| 1794 | try: |
| 1795 | for i in range(3): |
| 1796 | dumpResp = bmcDumpList(host, args, session) |
| 1797 | if 'message' in dumpResp: |
| 1798 | if 'ok' in dumpResp['message'].lower(): |
| 1799 | dumpList = dumpResp['data'] |
| 1800 | dumpListCollected = True |
| 1801 | break |
| 1802 | else: |
| 1803 | errorInfo += "Status was not OK when retrieving the list of dumps available. \n Response: \n{resp}\n".format(resp=dumpResp) |
| 1804 | else: |
| 1805 | errorInfo += "Invalid response received from the BMC while retrieving the list of dumps available.\n {resp}\n".format(resp=dumpResp) |
| 1806 | except Exception as e: |
| 1807 | errorInfo += "BMC dump list exception: {eInfo}\n".format(eInfo=e) |
| 1808 | |
| 1809 | if dumpListCollected: |
| 1810 | output['fileList'] = [] |
| 1811 | for dump in dumpList: |
| 1812 | try: |
| 1813 | if '/xyz/openbmc_project/dump/internal/manager' not in dump: |
| 1814 | d['dumpNum'] = int(dump.strip().split('/')[-1]) |
| 1815 | print('retrieving dump file ' + str(d['dumpNum'])) |
| 1816 | filename = bmcDumpRetrieve(host, args, session).split('Saved as ')[-1] |
| 1817 | output['fileList'].append(filename) |
| 1818 | except Exception as e: |
| 1819 | print("Unable to collect dump: {dumpInfo}".format(dumpInfo=dump)) |
| 1820 | errorInfo += "Exception collecting a bmc dump {dumpInfo}\n {eInfo}\n".format(dumpInfo=dump, eInfo=e) |
| 1821 | output['errors'] = errorInfo |
| 1822 | return output |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1823 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1824 | def collectServiceData(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1825 | """ |
| 1826 | Collects all data needed for service from the BMC |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1827 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1828 | @param host: string, the hostname or IP address of the bmc |
| 1829 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1830 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1831 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1832 | """ |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1833 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1834 | global toolVersion |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1835 | filelist = [] |
| 1836 | errorInfo = "" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1837 | |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1838 | #get current number of bmc dumps and create a new bmc dump |
| 1839 | dumpInitdata = csdDumpInitiate(host, args, session) |
| 1840 | dumpcount = dumpInitdata['dumpcount'] |
| 1841 | errorInfo += dumpInitdata['errors'] |
| 1842 | #create the directory to put files |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1843 | try: |
| 1844 | args.silent = True |
Justin Thaler | cf1deae | 2018-05-25 19:35:21 -0500 | [diff] [blame] | 1845 | myDir = tempfile.gettempdir()+os.sep + host + "--" + datetime.datetime.now().strftime("%Y-%m-%d_%H.%M.%S") |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1846 | os.makedirs(myDir) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1847 | |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1848 | except Exception as e: |
| 1849 | print('Unable to create the temporary directory for data collection. Ensure sufficient privileges to create temporary directory. Aborting.') |
| 1850 | return("Python exception: {eInfo}".format(eInfo = e)) |
| 1851 | |
| 1852 | #Collect Inventory |
| 1853 | inventoryData = csdInventory(host, args, session, myDir) |
| 1854 | if 'fileLoc' in inventoryData: |
| 1855 | filelist.append(inventoryData['fileLoc']) |
| 1856 | errorInfo += inventoryData['errors'] |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1857 | #Read all the sensor and OCC status |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1858 | sensorData = csdSensors(host,args,session,myDir) |
| 1859 | if 'fileLoc' in sensorData: |
| 1860 | filelist.append(sensorData['fileLoc']) |
| 1861 | errorInfo += sensorData['errors'] |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1862 | #Collect all of the LEDs status |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1863 | ledStatus = csdLEDs(host, args, session, myDir) |
| 1864 | if 'fileLoc' in ledStatus: |
| 1865 | filelist.append(ledStatus['fileLoc']) |
| 1866 | errorInfo += ledStatus['errors'] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1867 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1868 | #Collect the bmc logs |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1869 | selShort = csdSelShortList(host, args, session, myDir) |
| 1870 | if 'fileLoc' in selShort: |
| 1871 | filelist.append(selShort['fileLoc']) |
| 1872 | errorInfo += selShort['errors'] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1873 | |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1874 | parsedSELs = csdParsedSels(host, args, session, myDir) |
| 1875 | if 'fileLoc' in parsedSELs: |
| 1876 | filelist.append(parsedSELs['fileLoc']) |
| 1877 | errorInfo += parsedSELs['errors'] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1878 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1879 | #collect RAW bmc enumeration |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1880 | bmcRaw = csdFullEnumeration(host, args, session, myDir) |
| 1881 | if 'fileLoc' in bmcRaw: |
| 1882 | filelist.append(bmcRaw['fileLoc']) |
| 1883 | errorInfo += bmcRaw['errors'] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1884 | |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1885 | #wait for new dump to finish being created |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1886 | waitingForNewDump = True |
| 1887 | count = 0; |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1888 | print("Waiting for new BMC dump to finish being created.") |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1889 | while(waitingForNewDump): |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1890 | dumpList = bmcDumpList(host, args, session)['data'] |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1891 | if len(dumpList) > dumpcount: |
| 1892 | waitingForNewDump = False |
| 1893 | break; |
| 1894 | elif(count>30): |
| 1895 | print("Timed out waiting for bmc to make a new dump file. Dump space may be full.") |
| 1896 | break; |
| 1897 | else: |
| 1898 | time.sleep(2) |
| 1899 | count += 1 |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1900 | |
| 1901 | #collect all of the dump files |
| 1902 | getBMCDumps = csdCollectAllDumps(host, args, session, myDir) |
| 1903 | if 'fileList' in getBMCDumps: |
| 1904 | filelist+= getBMCDumps['fileList'] |
| 1905 | errorInfo += getBMCDumps['errors'] |
| 1906 | |
| 1907 | #write the runtime errors to a file |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1908 | try: |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1909 | with open(myDir +os.sep+'openbmctoolRuntimeErrors.txt', 'w') as f: |
| 1910 | f.write(errorInfo) |
| 1911 | print("OpenBMC tool runtime errors collected and stored in " + myDir + os.sep+ "openbmctoolRuntimeErrors.txt") |
| 1912 | filelist.append(myDir+os.sep+'openbmctoolRuntimeErrors.txt') |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1913 | except Exception as e: |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1914 | print("Failed to write OpenBMC tool runtime errors to file system.") |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1915 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1916 | #create the zip file |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1917 | try: |
Justin Thaler | cf1deae | 2018-05-25 19:35:21 -0500 | [diff] [blame] | 1918 | filename = myDir.split(tempfile.gettempdir()+os.sep)[-1] + "_" + toolVersion + '_openbmc.zip' |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1919 | zf = zipfile.ZipFile(myDir+os.sep + filename, 'w') |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1920 | for myfile in filelist: |
| 1921 | zf.write(myfile, os.path.basename(myfile)) |
| 1922 | zf.close() |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1923 | print("Zip file with all collected data created and stored in: {fileInfo}".format(fileInfo=myDir+os.sep+filename)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1924 | except Exception as e: |
| 1925 | print("Failed to create zip file with collected information") |
Justin Thaler | 666cf34 | 2019-01-23 14:44:27 -0600 | [diff] [blame] | 1926 | return "data collection finished" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1927 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1928 | |
| 1929 | def healthCheck(host, args, session): |
| 1930 | """ |
| 1931 | runs a health check on the platform |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1932 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1933 | @param host: string, the hostname or IP address of the bmc |
| 1934 | @param args: contains additional arguments used by the bmc sub command |
| 1935 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1936 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1937 | """ |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1938 | #check fru status and get as json to easily work through |
| 1939 | d = vars(args) |
| 1940 | useJson = d['json'] |
| 1941 | d['json'] = True |
| 1942 | d['verbose']= False |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1943 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1944 | frus = json.loads(fruStatus(host, args, session)) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1945 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1946 | hwStatus= "OK" |
| 1947 | performanceStatus = "OK" |
| 1948 | for key in frus: |
| 1949 | if frus[key]["Functional"] == "No" and frus[key]["Present"] == "Yes": |
| 1950 | hwStatus= "Degraded" |
Justin Thaler | fb9c81c | 2018-07-16 11:14:37 -0500 | [diff] [blame] | 1951 | if("power_supply" in key or "powersupply" in key): |
| 1952 | gpuCount =0 |
| 1953 | for comp in frus: |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1954 | if "gv100card" in comp: |
| 1955 | gpuCount +=1 |
| 1956 | if gpuCount > 4: |
| 1957 | hwStatus = "Critical" |
| 1958 | performanceStatus="Degraded" |
| 1959 | break; |
| 1960 | elif("fan" in key): |
| 1961 | hwStatus = "Degraded" |
| 1962 | else: |
| 1963 | performanceStatus = "Degraded" |
| 1964 | if useJson: |
| 1965 | output = {"Hardware Status": hwStatus, "Performance": performanceStatus} |
| 1966 | output = json.dumps(output, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False) |
| 1967 | else: |
| 1968 | output = ("Hardware Status: " + hwStatus + |
| 1969 | "\nPerformance: " +performanceStatus ) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1970 | |
| 1971 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1972 | #SW407886: Clear the duplicate entries |
| 1973 | #collect the dups |
| 1974 | d['devdebug'] = False |
| 1975 | sels = json.loads(selPrint(host, args, session)) |
| 1976 | logNums2Clr = [] |
| 1977 | oldestLogNum={"logNum": "bogus" ,"key" : ""} |
| 1978 | count = 0 |
| 1979 | if sels['numAlerts'] > 0: |
| 1980 | for key in sels: |
| 1981 | if "numAlerts" in key: |
| 1982 | continue |
| 1983 | try: |
| 1984 | if "slave@00:00/00:00:00:06/sbefifo1-dev0/occ1-dev0" in sels[key]['Message']: |
| 1985 | count += 1 |
| 1986 | if count > 1: |
| 1987 | #preserve first occurrence |
| 1988 | if sels[key]['timestamp'] < sels[oldestLogNum['key']]['timestamp']: |
| 1989 | oldestLogNum['key']=key |
| 1990 | oldestLogNum['logNum'] = sels[key]['logNum'] |
| 1991 | else: |
| 1992 | oldestLogNum['key']=key |
| 1993 | oldestLogNum['logNum'] = sels[key]['logNum'] |
| 1994 | logNums2Clr.append(sels[key]['logNum']) |
| 1995 | except KeyError: |
| 1996 | continue |
| 1997 | if(count >0): |
| 1998 | logNums2Clr.remove(oldestLogNum['logNum']) |
| 1999 | #delete the dups |
| 2000 | if count >1: |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2001 | data = "{\"data\": [] }" |
| 2002 | for logNum in logNums2Clr: |
| 2003 | url = "https://"+ host+ "/xyz/openbmc_project/logging/entry/"+logNum+"/action/Delete" |
| 2004 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2005 | session.post(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2006 | except(requests.exceptions.Timeout): |
| 2007 | deleteFailed = True |
| 2008 | except(requests.exceptions.ConnectionError) as err: |
| 2009 | deleteFailed = True |
| 2010 | #End of defect resolve code |
| 2011 | d['json'] = useJson |
| 2012 | return output |
| 2013 | |
| 2014 | |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2015 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 2016 | def bmc(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2017 | """ |
| 2018 | handles various bmc level commands, currently bmc rebooting |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2019 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2020 | @param host: string, the hostname or IP address of the bmc |
| 2021 | @param args: contains additional arguments used by the bmc sub command |
| 2022 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2023 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 2024 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 2025 | if(args.type is not None): |
| 2026 | return bmcReset(host, args, session) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2027 | if(args.info): |
| 2028 | return "Not implemented at this time" |
| 2029 | |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2030 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2031 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 2032 | def bmcReset(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2033 | """ |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2034 | controls resetting the bmc. warm reset reboots the bmc, cold reset removes the configuration and reboots. |
| 2035 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2036 | @param host: string, the hostname or IP address of the bmc |
| 2037 | @param args: contains additional arguments used by the bmcReset sub command |
| 2038 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2039 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 2040 | """ |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2041 | if checkFWactivation(host, args, session): |
| 2042 | return ("BMC reset control disabled during firmware activation") |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 2043 | if(args.type == "warm"): |
| 2044 | print("\nAttempting to reboot the BMC...:") |
| 2045 | url="https://"+host+"/xyz/openbmc_project/state/bmc0/attr/RequestedBMCTransition" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2046 | data = '{"data":"xyz.openbmc_project.State.BMC.Transition.Reboot"}' |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2047 | res = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 2048 | return res.text |
| 2049 | elif(args.type =="cold"): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2050 | print("\nAttempting to reboot the BMC...:") |
| 2051 | url="https://"+host+"/xyz/openbmc_project/state/bmc0/attr/RequestedBMCTransition" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2052 | data = '{"data":"xyz.openbmc_project.State.BMC.Transition.Reboot"}' |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2053 | res = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2054 | return res.text |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 2055 | else: |
| 2056 | return "invalid command" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2057 | |
| 2058 | def gardClear(host, args, session): |
| 2059 | """ |
| 2060 | clears the gard records from the bmc |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2061 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2062 | @param host: string, the hostname or IP address of the bmc |
| 2063 | @param args: contains additional arguments used by the gardClear sub command |
| 2064 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2065 | """ |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2066 | url="https://"+host+"/org/open_power/control/gard/action/Reset" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2067 | data = '{"data":[]}' |
| 2068 | try: |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2069 | |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2070 | res = session.post(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2071 | if res.status_code == 404: |
| 2072 | return "Command not supported by this firmware version" |
| 2073 | else: |
| 2074 | return res.text |
| 2075 | except(requests.exceptions.Timeout): |
| 2076 | return connectionErrHandler(args.json, "Timeout", None) |
| 2077 | except(requests.exceptions.ConnectionError) as err: |
| 2078 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2079 | |
| 2080 | def activateFWImage(host, args, session): |
| 2081 | """ |
| 2082 | activates a firmware image on the bmc |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2083 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2084 | @param host: string, the hostname or IP address of the bmc |
| 2085 | @param args: contains additional arguments used by the fwflash sub command |
| 2086 | @param session: the active session to use |
| 2087 | @param fwID: the unique ID of the fw image to activate |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2088 | """ |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2089 | fwID = args.imageID |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2090 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2091 | #determine the existing versions |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2092 | url="https://"+host+"/xyz/openbmc_project/software/enumerate" |
| 2093 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2094 | resp = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2095 | except(requests.exceptions.Timeout): |
| 2096 | return connectionErrHandler(args.json, "Timeout", None) |
| 2097 | except(requests.exceptions.ConnectionError) as err: |
| 2098 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2099 | existingSoftware = json.loads(resp.text)['data'] |
| 2100 | altVersionID = '' |
| 2101 | versionType = '' |
| 2102 | imageKey = '/xyz/openbmc_project/software/'+fwID |
| 2103 | if imageKey in existingSoftware: |
| 2104 | versionType = existingSoftware[imageKey]['Purpose'] |
| 2105 | for key in existingSoftware: |
| 2106 | if imageKey == key: |
| 2107 | continue |
| 2108 | if 'Purpose' in existingSoftware[key]: |
| 2109 | if versionType == existingSoftware[key]['Purpose']: |
| 2110 | altVersionID = key.split('/')[-1] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2111 | |
| 2112 | |
| 2113 | |
| 2114 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2115 | url="https://"+host+"/xyz/openbmc_project/software/"+ fwID + "/attr/Priority" |
| 2116 | url1="https://"+host+"/xyz/openbmc_project/software/"+ altVersionID + "/attr/Priority" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2117 | data = "{\"data\": 0}" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2118 | data1 = "{\"data\": 1 }" |
| 2119 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2120 | resp = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
| 2121 | resp1 = session.put(url1, headers=jsonHeader, data=data1, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2122 | except(requests.exceptions.Timeout): |
| 2123 | return connectionErrHandler(args.json, "Timeout", None) |
| 2124 | except(requests.exceptions.ConnectionError) as err: |
| 2125 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2126 | if(not args.json): |
| 2127 | if resp.status_code == 200 and resp1.status_code == 200: |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2128 | return 'Firmware flash and activation completed. Please reboot the bmc and then boot the host OS for the changes to take effect. ' |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2129 | else: |
| 2130 | return "Firmware activation failed." |
| 2131 | else: |
| 2132 | return resp.text + resp1.text |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2133 | |
| 2134 | def activateStatus(host, args, session): |
| 2135 | if checkFWactivation(host, args, session): |
| 2136 | return("Firmware is currently being activated. Do not reboot the BMC or start the Host OS") |
| 2137 | else: |
| 2138 | return("No firmware activations are pending") |
| 2139 | |
| 2140 | def extractFWimage(path, imageType): |
| 2141 | """ |
| 2142 | extracts the bmc image and returns information about the package |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2143 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2144 | @param path: the path and file name of the firmware image |
| 2145 | @param imageType: The type of image the user is trying to flash. Host or BMC |
| 2146 | @return: the image id associated with the package. returns an empty string on error. |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2147 | """ |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2148 | f = tempfile.TemporaryFile() |
| 2149 | tmpDir = tempfile.gettempdir() |
| 2150 | newImageID = "" |
| 2151 | if os.path.exists(path): |
| 2152 | try: |
| 2153 | imageFile = tarfile.open(path,'r') |
| 2154 | contents = imageFile.getmembers() |
| 2155 | for tf in contents: |
| 2156 | if 'MANIFEST' in tf.name: |
| 2157 | imageFile.extract(tf.name, path=tmpDir) |
| 2158 | with open(tempfile.gettempdir() +os.sep+ tf.name, 'r') as imageInfo: |
| 2159 | for line in imageInfo: |
| 2160 | if 'purpose' in line: |
| 2161 | purpose = line.split('=')[1] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2162 | if imageType not in purpose.split('.')[-1]: |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2163 | print('The specified image is not for ' + imageType) |
| 2164 | print('Please try again with the image for ' + imageType) |
| 2165 | return "" |
| 2166 | if 'version' == line.split('=')[0]: |
| 2167 | version = line.split('=')[1].strip().encode('utf-8') |
| 2168 | m = hashlib.sha512() |
| 2169 | m.update(version) |
| 2170 | newImageID = m.hexdigest()[:8] |
| 2171 | break |
| 2172 | try: |
| 2173 | os.remove(tempfile.gettempdir() +os.sep+ tf.name) |
| 2174 | except OSError: |
| 2175 | pass |
| 2176 | return newImageID |
| 2177 | except tarfile.ExtractError as e: |
| 2178 | print('Unable to extract information from the firmware file.') |
| 2179 | print('Ensure you have write access to the directory: ' + tmpDir) |
| 2180 | return newImageID |
| 2181 | except tarfile.TarError as e: |
| 2182 | print('This is not a valid firmware file.') |
| 2183 | return newImageID |
| 2184 | print("This is not a valid firmware file.") |
| 2185 | return newImageID |
| 2186 | else: |
| 2187 | print('The filename and path provided are not valid.') |
| 2188 | return newImageID |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2189 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2190 | def getAllFWImageIDs(fwInvDict): |
| 2191 | """ |
| 2192 | gets a list of all the firmware image IDs |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2193 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2194 | @param fwInvDict: the dictionary to search for FW image IDs |
| 2195 | @return: list containing string representation of the found image ids |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2196 | """ |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2197 | idList = [] |
| 2198 | for key in fwInvDict: |
| 2199 | if 'Version' in fwInvDict[key]: |
| 2200 | idList.append(key.split('/')[-1]) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2201 | return idList |
| 2202 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2203 | def fwFlash(host, args, session): |
| 2204 | """ |
| 2205 | updates the bmc firmware and pnor firmware |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2206 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2207 | @param host: string, the hostname or IP address of the bmc |
| 2208 | @param args: contains additional arguments used by the fwflash sub command |
| 2209 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2210 | """ |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2211 | d = vars(args) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2212 | if(args.type == 'bmc'): |
| 2213 | purp = 'BMC' |
| 2214 | else: |
| 2215 | purp = 'Host' |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2216 | |
| 2217 | #check power state of the machine. No concurrent FW updates allowed |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2218 | d['powcmd'] = 'status' |
| 2219 | powerstate = chassisPower(host, args, session) |
| 2220 | if 'Chassis Power State: On' in powerstate: |
| 2221 | return("Aborting firmware update. Host is powered on. Please turn off the host and try again.") |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2222 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2223 | #determine the existing images on the bmc |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2224 | url="https://"+host+"/xyz/openbmc_project/software/enumerate" |
| 2225 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2226 | resp = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2227 | except(requests.exceptions.Timeout): |
| 2228 | return connectionErrHandler(args.json, "Timeout", None) |
| 2229 | except(requests.exceptions.ConnectionError) as err: |
| 2230 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2231 | oldsoftware = json.loads(resp.text)['data'] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2232 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2233 | #Extract the tar and get information from the manifest file |
| 2234 | newversionID = extractFWimage(args.fileloc, purp) |
| 2235 | if newversionID == "": |
| 2236 | return "Unable to verify FW image." |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2237 | |
| 2238 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2239 | #check if the new image is already on the bmc |
| 2240 | if newversionID not in getAllFWImageIDs(oldsoftware): |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2241 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2242 | #upload the file |
| 2243 | httpHeader = {'Content-Type':'application/octet-stream'} |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2244 | httpHeader.update(xAuthHeader) |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2245 | url="https://"+host+"/upload/image" |
| 2246 | data=open(args.fileloc,'rb').read() |
| 2247 | print("Uploading file to BMC") |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2248 | try: |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2249 | resp = session.post(url, headers=httpHeader, data=data, verify=False) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2250 | except(requests.exceptions.Timeout): |
| 2251 | return connectionErrHandler(args.json, "Timeout", None) |
| 2252 | except(requests.exceptions.ConnectionError) as err: |
| 2253 | return connectionErrHandler(args.json, "ConnectionError", err) |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2254 | if resp.status_code != 200: |
| 2255 | return "Failed to upload the file to the bmc" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2256 | else: |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2257 | print("Upload complete.") |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2258 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2259 | #verify bmc processed the image |
| 2260 | software ={} |
| 2261 | for i in range(0, 5): |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2262 | url="https://"+host+"/xyz/openbmc_project/software/enumerate" |
| 2263 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2264 | resp = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2265 | except(requests.exceptions.Timeout): |
| 2266 | return connectionErrHandler(args.json, "Timeout", None) |
| 2267 | except(requests.exceptions.ConnectionError) as err: |
| 2268 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2269 | software = json.loads(resp.text)['data'] |
| 2270 | #check if bmc is done processing the new image |
| 2271 | if (newversionID in getAllFWImageIDs(software)): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2272 | break |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2273 | else: |
| 2274 | time.sleep(15) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2275 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2276 | #activate the new image |
| 2277 | print("Activating new image: "+newversionID) |
| 2278 | url="https://"+host+"/xyz/openbmc_project/software/"+ newversionID + "/attr/RequestedActivation" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2279 | data = '{"data":"xyz.openbmc_project.Software.Activation.RequestedActivations.Active"}' |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2280 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2281 | resp = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2282 | except(requests.exceptions.Timeout): |
| 2283 | return connectionErrHandler(args.json, "Timeout", None) |
| 2284 | except(requests.exceptions.ConnectionError) as err: |
| 2285 | return connectionErrHandler(args.json, "ConnectionError", err) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2286 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2287 | #wait for the activation to complete, timeout after ~1 hour |
| 2288 | i=0 |
| 2289 | while i < 360: |
| 2290 | url="https://"+host+"/xyz/openbmc_project/software/"+ newversionID |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2291 | data = '{"data":"xyz.openbmc_project.Software.Activation.RequestedActivations.Active"}' |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2292 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2293 | resp = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2294 | except(requests.exceptions.Timeout): |
| 2295 | return connectionErrHandler(args.json, "Timeout", None) |
| 2296 | except(requests.exceptions.ConnectionError) as err: |
| 2297 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2298 | fwInfo = json.loads(resp.text)['data'] |
| 2299 | if 'Activating' not in fwInfo['Activation'] and 'Activating' not in fwInfo['RequestedActivation']: |
| 2300 | print('') |
| 2301 | break |
| 2302 | else: |
| 2303 | sys.stdout.write('.') |
| 2304 | sys.stdout.flush() |
| 2305 | time.sleep(10) #check every 10 seconds |
| 2306 | return "Firmware flash and activation completed. Please reboot the bmc and then boot the host OS for the changes to take effect. " |
| 2307 | else: |
| 2308 | print("This image has been found on the bmc. Activating image: " + newversionID) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2309 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 2310 | d['imageID'] = newversionID |
| 2311 | return activateFWImage(host, args, session) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 2312 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2313 | def getFWInventoryAttributes(rawFWInvItem, ID): |
| 2314 | """ |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2315 | gets and lists all of the firmware in the system. |
| 2316 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2317 | @return: returns a dictionary containing the image attributes |
| 2318 | """ |
| 2319 | reqActivation = rawFWInvItem["RequestedActivation"].split('.')[-1] |
| 2320 | pendingActivation = "" |
| 2321 | if reqActivation == "None": |
| 2322 | pendingActivation = "No" |
| 2323 | else: |
| 2324 | pendingActivation = "Yes" |
| 2325 | firmwareAttr = {ID: { |
| 2326 | "Purpose": rawFWInvItem["Purpose"].split('.')[-1], |
| 2327 | "Version": rawFWInvItem["Version"], |
| 2328 | "RequestedActivation": pendingActivation, |
| 2329 | "ID": ID}} |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2330 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2331 | if "ExtendedVersion" in rawFWInvItem: |
| 2332 | firmwareAttr[ID]['ExtendedVersion'] = rawFWInvItem['ExtendedVersion'].split(',') |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2333 | else: |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2334 | firmwareAttr[ID]['ExtendedVersion'] = "" |
| 2335 | return firmwareAttr |
| 2336 | |
| 2337 | def parseFWdata(firmwareDict): |
| 2338 | """ |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2339 | creates a dictionary with parsed firmware data |
| 2340 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2341 | @return: returns a dictionary containing the image attributes |
| 2342 | """ |
| 2343 | firmwareInfoDict = {"Functional": {}, "Activated":{}, "NeedsActivated":{}} |
| 2344 | for key in firmwareDict['data']: |
| 2345 | #check for valid endpoint |
| 2346 | if "Purpose" in firmwareDict['data'][key]: |
| 2347 | id = key.split('/')[-1] |
| 2348 | if firmwareDict['data'][key]['Activation'].split('.')[-1] == "Active": |
| 2349 | fwActivated = True |
| 2350 | else: |
| 2351 | fwActivated = False |
Justin Thaler | cb68e06 | 2019-03-26 19:04:52 -0500 | [diff] [blame] | 2352 | if 'Priority' in firmwareDict['data'][key]: |
| 2353 | if firmwareDict['data'][key]['Priority'] == 0: |
| 2354 | firmwareInfoDict['Functional'].update(getFWInventoryAttributes(firmwareDict['data'][key], id)) |
| 2355 | elif firmwareDict['data'][key]['Priority'] >= 0 and fwActivated: |
| 2356 | firmwareInfoDict['Activated'].update(getFWInventoryAttributes(firmwareDict['data'][key], id)) |
| 2357 | else: |
| 2358 | firmwareInfoDict['NeedsActivated'].update(getFWInventoryAttributes(firmwareDict['data'][key], id)) |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2359 | else: |
Justin Thaler | cb68e06 | 2019-03-26 19:04:52 -0500 | [diff] [blame] | 2360 | firmwareInfoDict['NeedsActivated'].update(getFWInventoryAttributes(firmwareDict['data'][key], id)) |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2361 | emptySections = [] |
| 2362 | for key in firmwareInfoDict: |
| 2363 | if len(firmwareInfoDict[key])<=0: |
| 2364 | emptySections.append(key) |
| 2365 | for key in emptySections: |
| 2366 | del firmwareInfoDict[key] |
| 2367 | return firmwareInfoDict |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2368 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2369 | def displayFWInvenory(firmwareInfoDict, args): |
| 2370 | """ |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2371 | gets and lists all of the firmware in the system. |
| 2372 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2373 | @return: returns a string containing all of the firmware information |
| 2374 | """ |
| 2375 | output = "" |
| 2376 | if not args.json: |
| 2377 | for key in firmwareInfoDict: |
| 2378 | for subkey in firmwareInfoDict[key]: |
| 2379 | firmwareInfoDict[key][subkey]['ExtendedVersion'] = str(firmwareInfoDict[key][subkey]['ExtendedVersion']) |
| 2380 | if not args.verbose: |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2381 | output = "---Running Images---\n" |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2382 | colNames = ["Purpose", "Version", "ID"] |
| 2383 | keylist = ["Purpose", "Version", "ID"] |
| 2384 | output += tableDisplay(keylist, colNames, firmwareInfoDict["Functional"]) |
| 2385 | if "Activated" in firmwareInfoDict: |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2386 | output += "\n---Available Images---\n" |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2387 | output += tableDisplay(keylist, colNames, firmwareInfoDict["Activated"]) |
| 2388 | if "NeedsActivated" in firmwareInfoDict: |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2389 | output += "\n---Needs Activated Images---\n" |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2390 | output += tableDisplay(keylist, colNames, firmwareInfoDict["NeedsActivated"]) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2391 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2392 | else: |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2393 | output = "---Running Images---\n" |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2394 | colNames = ["Purpose", "Version", "ID", "Pending Activation", "Extended Version"] |
| 2395 | keylist = ["Purpose", "Version", "ID", "RequestedActivation", "ExtendedVersion"] |
| 2396 | output += tableDisplay(keylist, colNames, firmwareInfoDict["Functional"]) |
| 2397 | if "Activated" in firmwareInfoDict: |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2398 | output += "\n---Available Images---\n" |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2399 | output += tableDisplay(keylist, colNames, firmwareInfoDict["Activated"]) |
| 2400 | if "NeedsActivated" in firmwareInfoDict: |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2401 | output += "\n---Needs Activated Images---\n" |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2402 | output += tableDisplay(keylist, colNames, firmwareInfoDict["NeedsActivated"]) |
| 2403 | return output |
| 2404 | else: |
| 2405 | return str(json.dumps(firmwareInfoDict, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False)) |
| 2406 | |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2407 | def firmwareList(host, args, session): |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2408 | """ |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2409 | gets and lists all of the firmware in the system. |
| 2410 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2411 | @return: returns a string containing all of the firmware information |
| 2412 | """ |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2413 | url="https://{hostname}/xyz/openbmc_project/software/enumerate".format(hostname=host) |
| 2414 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2415 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2416 | except(requests.exceptions.Timeout): |
| 2417 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2418 | firmwareDict = json.loads(res.text) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2419 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2420 | #sort the received information |
| 2421 | firmwareInfoDict = parseFWdata(firmwareDict) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2422 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2423 | #display the information |
| 2424 | return displayFWInvenory(firmwareInfoDict, args) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2425 | |
| 2426 | |
Adriana Kobylak | 5af2fad | 2018-11-08 12:33:43 -0600 | [diff] [blame] | 2427 | def deleteFWVersion(host, args, session): |
| 2428 | """ |
| 2429 | deletes a firmware version on the BMC |
| 2430 | |
| 2431 | @param host: string, the hostname or IP address of the BMC |
| 2432 | @param args: contains additional arguments used by the fwflash sub command |
| 2433 | @param session: the active session to use |
| 2434 | @param fwID: the unique ID of the fw version to delete |
| 2435 | """ |
| 2436 | fwID = args.versionID |
| 2437 | |
| 2438 | print("Deleting version: "+fwID) |
| 2439 | url="https://"+host+"/xyz/openbmc_project/software/"+ fwID + "/action/Delete" |
Adriana Kobylak | 5af2fad | 2018-11-08 12:33:43 -0600 | [diff] [blame] | 2440 | data = "{\"data\": [] }" |
| 2441 | |
| 2442 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2443 | res = session.post(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Adriana Kobylak | 5af2fad | 2018-11-08 12:33:43 -0600 | [diff] [blame] | 2444 | except(requests.exceptions.Timeout): |
| 2445 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2446 | if res.status_code == 200: |
| 2447 | return ('The firmware version has been deleted') |
| 2448 | else: |
| 2449 | return ('Unable to delete the specified firmware version') |
| 2450 | |
| 2451 | |
Deepak Kodihalli | 22d4df0 | 2018-09-18 06:52:43 -0500 | [diff] [blame] | 2452 | def restLogging(host, args, session): |
| 2453 | """ |
| 2454 | Called by the logging function. Turns REST API logging on/off. |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2455 | |
Deepak Kodihalli | 22d4df0 | 2018-09-18 06:52:43 -0500 | [diff] [blame] | 2456 | @param host: string, the hostname or IP address of the bmc |
| 2457 | @param args: contains additional arguments used by the logging sub command |
| 2458 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2459 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
Deepak Kodihalli | 22d4df0 | 2018-09-18 06:52:43 -0500 | [diff] [blame] | 2460 | """ |
Deepak Kodihalli | 22d4df0 | 2018-09-18 06:52:43 -0500 | [diff] [blame] | 2461 | url="https://"+host+"/xyz/openbmc_project/logging/rest_api_logs/attr/Enabled" |
Deepak Kodihalli | 22d4df0 | 2018-09-18 06:52:43 -0500 | [diff] [blame] | 2462 | |
| 2463 | if(args.rest_logging == 'on'): |
| 2464 | data = '{"data": 1}' |
| 2465 | elif(args.rest_logging == 'off'): |
| 2466 | data = '{"data": 0}' |
| 2467 | else: |
| 2468 | return "Invalid logging rest_api command" |
| 2469 | |
| 2470 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2471 | res = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Deepak Kodihalli | 22d4df0 | 2018-09-18 06:52:43 -0500 | [diff] [blame] | 2472 | except(requests.exceptions.Timeout): |
| 2473 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2474 | return res.text |
| 2475 | |
| 2476 | |
Deepak Kodihalli | 02d5328 | 2018-09-18 06:53:31 -0500 | [diff] [blame] | 2477 | def remoteLogging(host, args, session): |
| 2478 | """ |
| 2479 | Called by the logging function. View config information for/disable remote logging (rsyslog). |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2480 | |
Deepak Kodihalli | 02d5328 | 2018-09-18 06:53:31 -0500 | [diff] [blame] | 2481 | @param host: string, the hostname or IP address of the bmc |
| 2482 | @param args: contains additional arguments used by the logging sub command |
| 2483 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2484 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
Deepak Kodihalli | 02d5328 | 2018-09-18 06:53:31 -0500 | [diff] [blame] | 2485 | """ |
| 2486 | |
| 2487 | url="https://"+host+"/xyz/openbmc_project/logging/config/remote" |
Deepak Kodihalli | 02d5328 | 2018-09-18 06:53:31 -0500 | [diff] [blame] | 2488 | |
| 2489 | try: |
| 2490 | if(args.remote_logging == 'view'): |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2491 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Deepak Kodihalli | 02d5328 | 2018-09-18 06:53:31 -0500 | [diff] [blame] | 2492 | elif(args.remote_logging == 'disable'): |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2493 | res = session.put(url + '/attr/Port', headers=jsonHeader, json = {"data": 0}, verify=False, timeout=baseTimeout) |
| 2494 | res = session.put(url + '/attr/Address', headers=jsonHeader, json = {"data": ""}, verify=False, timeout=baseTimeout) |
Deepak Kodihalli | 02d5328 | 2018-09-18 06:53:31 -0500 | [diff] [blame] | 2495 | else: |
| 2496 | return "Invalid logging remote_logging command" |
| 2497 | except(requests.exceptions.Timeout): |
| 2498 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2499 | return res.text |
| 2500 | |
| 2501 | |
| 2502 | def remoteLoggingConfig(host, args, session): |
| 2503 | """ |
| 2504 | Called by the logging function. Configures remote logging (rsyslog). |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2505 | |
Deepak Kodihalli | 02d5328 | 2018-09-18 06:53:31 -0500 | [diff] [blame] | 2506 | @param host: string, the hostname or IP address of the bmc |
| 2507 | @param args: contains additional arguments used by the logging sub command |
| 2508 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2509 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
Deepak Kodihalli | 02d5328 | 2018-09-18 06:53:31 -0500 | [diff] [blame] | 2510 | """ |
| 2511 | |
| 2512 | url="https://"+host+"/xyz/openbmc_project/logging/config/remote" |
Deepak Kodihalli | 02d5328 | 2018-09-18 06:53:31 -0500 | [diff] [blame] | 2513 | |
| 2514 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2515 | res = session.put(url + '/attr/Port', headers=jsonHeader, json = {"data": args.port}, verify=False, timeout=baseTimeout) |
| 2516 | res = session.put(url + '/attr/Address', headers=jsonHeader, json = {"data": args.address}, verify=False, timeout=baseTimeout) |
Deepak Kodihalli | 02d5328 | 2018-09-18 06:53:31 -0500 | [diff] [blame] | 2517 | except(requests.exceptions.Timeout): |
| 2518 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2519 | return res.text |
| 2520 | |
Marri Devender Rao | 82590dc | 2019-06-06 04:54:22 -0500 | [diff] [blame] | 2521 | def redfishSupportPresent(host, session): |
| 2522 | url = "https://" + host + "/redfish/v1" |
| 2523 | try: |
| 2524 | resp = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
| 2525 | except(requests.exceptions.Timeout): |
| 2526 | return False |
| 2527 | except(requests.exceptions.ConnectionError) as err: |
| 2528 | return False |
| 2529 | if resp.status_code != 200: |
| 2530 | return False |
| 2531 | else: |
| 2532 | return True |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 2533 | |
Dhruvaraj Subhashchandran | 64e7f6f | 2018-10-02 03:42:14 -0500 | [diff] [blame] | 2534 | def certificateUpdate(host, args, session): |
| 2535 | """ |
| 2536 | Called by certificate management function. update server/client/authority certificates |
| 2537 | Example: |
| 2538 | certificate update server https -f cert.pem |
| 2539 | certificate update authority ldap -f Root-CA.pem |
| 2540 | certificate update client ldap -f cert.pem |
| 2541 | @param host: string, the hostname or IP address of the bmc |
| 2542 | @param args: contains additional arguments used by the certificate update sub command |
| 2543 | @param session: the active session to use |
| 2544 | """ |
Dhruvaraj Subhashchandran | 64e7f6f | 2018-10-02 03:42:14 -0500 | [diff] [blame] | 2545 | httpHeader = {'Content-Type': 'application/octet-stream'} |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2546 | httpHeader.update(xAuthHeader) |
Dhruvaraj Subhashchandran | 64e7f6f | 2018-10-02 03:42:14 -0500 | [diff] [blame] | 2547 | data = open(args.fileloc, 'rb').read() |
Dhruvaraj Subhashchandran | 64e7f6f | 2018-10-02 03:42:14 -0500 | [diff] [blame] | 2548 | try: |
Marri Devender Rao | 82590dc | 2019-06-06 04:54:22 -0500 | [diff] [blame] | 2549 | if redfishSupportPresent(host, session): |
Marri Devender Rao | 62db08a | 2019-08-22 03:16:02 -0500 | [diff] [blame] | 2550 | if(args.type.lower() == 'server' and args.service.lower() != "https"): |
| 2551 | return "Invalid service type" |
| 2552 | if(args.type.lower() == 'client' and args.service.lower() != "ldap"): |
| 2553 | return "Invalid service type" |
| 2554 | if(args.type.lower() == 'authority' and args.service.lower() != "ldap"): |
| 2555 | return "Invalid service type" |
Marri Devender Rao | 82590dc | 2019-06-06 04:54:22 -0500 | [diff] [blame] | 2556 | url = ""; |
| 2557 | if(args.type.lower() == 'server'): |
| 2558 | url = "https://" + host + \ |
| 2559 | "/redfish/v1/Managers/bmc/NetworkProtocol/HTTPS/Certificates" |
| 2560 | elif(args.type.lower() == 'client'): |
| 2561 | url = "https://" + host + \ |
| 2562 | "/redfish/v1/AccountService/LDAP/Certificates" |
| 2563 | elif(args.type.lower() == 'authority'): |
| 2564 | url = "https://" + host + \ |
| 2565 | "/redfish/v1/Managers/bmc/Truststore/Certificates" |
| 2566 | else: |
| 2567 | return "Unsupported certificate type" |
| 2568 | resp = session.post(url, headers=httpHeader, data=data, |
| 2569 | verify=False) |
| 2570 | else: |
| 2571 | url = "https://" + host + "/xyz/openbmc_project/certs/" + \ |
| 2572 | args.type.lower() + "/" + args.service.lower() |
| 2573 | resp = session.put(url, headers=httpHeader, data=data, verify=False) |
Dhruvaraj Subhashchandran | 64e7f6f | 2018-10-02 03:42:14 -0500 | [diff] [blame] | 2574 | except(requests.exceptions.Timeout): |
| 2575 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2576 | except(requests.exceptions.ConnectionError) as err: |
| 2577 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2578 | if resp.status_code != 200: |
| 2579 | print(resp.text) |
| 2580 | return "Failed to update the certificate" |
| 2581 | else: |
Marri Devender Rao | 82590dc | 2019-06-06 04:54:22 -0500 | [diff] [blame] | 2582 | print("Update complete.") |
Dhruvaraj Subhashchandran | 64e7f6f | 2018-10-02 03:42:14 -0500 | [diff] [blame] | 2583 | |
| 2584 | def certificateDelete(host, args, session): |
| 2585 | """ |
| 2586 | Called by certificate management function to delete certificate |
| 2587 | Example: |
| 2588 | certificate delete server https |
| 2589 | certificate delete authority ldap |
| 2590 | certificate delete client ldap |
| 2591 | @param host: string, the hostname or IP address of the bmc |
| 2592 | @param args: contains additional arguments used by the certificate delete sub command |
| 2593 | @param session: the active session to use |
| 2594 | """ |
Marri Devender Rao | 77e7868 | 2019-07-17 03:18:35 -0500 | [diff] [blame] | 2595 | if redfishSupportPresent(host, session): |
| 2596 | return "Not supported, please use certificate replace instead"; |
Dhruvaraj Subhashchandran | 64e7f6f | 2018-10-02 03:42:14 -0500 | [diff] [blame] | 2597 | httpHeader = {'Content-Type': 'multipart/form-data'} |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2598 | httpHeader.update(xAuthHeader) |
Dhruvaraj Subhashchandran | 64e7f6f | 2018-10-02 03:42:14 -0500 | [diff] [blame] | 2599 | url = "https://" + host + "/xyz/openbmc_project/certs/" + args.type.lower() + "/" + args.service.lower() |
| 2600 | print("Deleting certificate url=" + url) |
| 2601 | try: |
| 2602 | resp = session.delete(url, headers=httpHeader) |
| 2603 | except(requests.exceptions.Timeout): |
| 2604 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2605 | except(requests.exceptions.ConnectionError) as err: |
| 2606 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2607 | if resp.status_code != 200: |
| 2608 | print(resp.text) |
| 2609 | return "Failed to delete the certificate" |
| 2610 | else: |
Marri Devender Rao | 77e7868 | 2019-07-17 03:18:35 -0500 | [diff] [blame] | 2611 | print("Delete complete.") |
Deepak Kodihalli | 02d5328 | 2018-09-18 06:53:31 -0500 | [diff] [blame] | 2612 | |
Marri Devender Rao | dfe81ad | 2019-07-01 05:38:09 -0500 | [diff] [blame] | 2613 | def certificateReplace(host, args, session): |
| 2614 | """ |
| 2615 | Called by certificate management function. replace server/client/ |
| 2616 | authority certificates |
| 2617 | Example: |
| 2618 | certificate replace server https -f cert.pem |
| 2619 | certificate replace authority ldap -f Root-CA.pem |
| 2620 | certificate replace client ldap -f cert.pem |
| 2621 | @param host: string, the hostname or IP address of the bmc |
| 2622 | @param args: contains additional arguments used by the certificate |
| 2623 | replace sub command |
| 2624 | @param session: the active session to use |
| 2625 | """ |
| 2626 | cert = open(args.fileloc, 'rb').read() |
| 2627 | try: |
| 2628 | if redfishSupportPresent(host, session): |
| 2629 | httpHeader = {'Content-Type': 'application/json'} |
| 2630 | httpHeader.update(xAuthHeader) |
| 2631 | url = ""; |
Marri Devender Rao | 62db08a | 2019-08-22 03:16:02 -0500 | [diff] [blame] | 2632 | if(args.type.lower() == 'server' and args.service.lower() != "https"): |
| 2633 | return "Invalid service type" |
| 2634 | if(args.type.lower() == 'client' and args.service.lower() != "ldap"): |
| 2635 | return "Invalid service type" |
| 2636 | if(args.type.lower() == 'authority' and args.service.lower() != "ldap"): |
| 2637 | return "Invalid service type" |
Marri Devender Rao | dfe81ad | 2019-07-01 05:38:09 -0500 | [diff] [blame] | 2638 | if(args.type.lower() == 'server'): |
| 2639 | url = "/redfish/v1/Managers/bmc/NetworkProtocol/HTTPS/Certificates/1" |
| 2640 | elif(args.type.lower() == 'client'): |
| 2641 | url = "/redfish/v1/AccountService/LDAP/Certificates/1" |
| 2642 | elif(args.type.lower() == 'authority'): |
| 2643 | url = "/redfish/v1/Managers/bmc/Truststore/Certificates/1" |
| 2644 | replaceUrl = "https://" + host + \ |
| 2645 | "/redfish/v1/CertificateService/Actions/CertificateService.ReplaceCertificate" |
| 2646 | data ={"CertificateUri":{"@odata.id":url}, "CertificateType":"PEM", |
| 2647 | "CertificateString":cert} |
| 2648 | resp = session.post(replaceUrl, headers=httpHeader, json=data, verify=False) |
| 2649 | else: |
| 2650 | httpHeader = {'Content-Type': 'application/octet-stream'} |
| 2651 | httpHeader.update(xAuthHeader) |
| 2652 | url = "https://" + host + "/xyz/openbmc_project/certs/" + \ |
| 2653 | args.type.lower() + "/" + args.service.lower() |
| 2654 | resp = session.delete(url, headers=httpHeader) |
| 2655 | resp = session.put(url, headers=httpHeader, data=cert, verify=False) |
| 2656 | except(requests.exceptions.Timeout): |
| 2657 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2658 | except(requests.exceptions.ConnectionError) as err: |
| 2659 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2660 | if resp.status_code != 200: |
| 2661 | print(resp.text) |
| 2662 | return "Failed to replace the certificate" |
| 2663 | else: |
| 2664 | print("Replace complete.") |
| 2665 | return resp.text |
| 2666 | |
Marri Devender Rao | 3464640 | 2019-07-01 05:46:03 -0500 | [diff] [blame] | 2667 | def certificateDisplay(host, args, session): |
| 2668 | """ |
| 2669 | Called by certificate management function. display server/client/ |
| 2670 | authority certificates |
| 2671 | Example: |
| 2672 | certificate display server |
| 2673 | certificate display authority |
| 2674 | certificate display client |
| 2675 | @param host: string, the hostname or IP address of the bmc |
| 2676 | @param args: contains additional arguments used by the certificate |
| 2677 | display sub command |
| 2678 | @param session: the active session to use |
| 2679 | """ |
| 2680 | if not redfishSupportPresent(host, session): |
| 2681 | return "Not supported"; |
| 2682 | |
| 2683 | httpHeader = {'Content-Type': 'application/octet-stream'} |
| 2684 | httpHeader.update(xAuthHeader) |
| 2685 | if(args.type.lower() == 'server'): |
| 2686 | url = "https://" + host + \ |
| 2687 | "/redfish/v1/Managers/bmc/NetworkProtocol/HTTPS/Certificates/1" |
| 2688 | elif(args.type.lower() == 'client'): |
| 2689 | url = "https://" + host + \ |
| 2690 | "/redfish/v1/AccountService/LDAP/Certificates/1" |
| 2691 | elif(args.type.lower() == 'authority'): |
| 2692 | url = "https://" + host + \ |
| 2693 | "/redfish/v1/Managers/bmc/Truststore/Certificates/1" |
| 2694 | try: |
| 2695 | resp = session.get(url, headers=httpHeader, verify=False) |
| 2696 | except(requests.exceptions.Timeout): |
| 2697 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2698 | except(requests.exceptions.ConnectionError) as err: |
| 2699 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2700 | if resp.status_code != 200: |
| 2701 | print(resp.text) |
| 2702 | return "Failed to display the certificate" |
| 2703 | else: |
| 2704 | print("Display complete.") |
| 2705 | return resp.text |
| 2706 | |
Marri Devender Rao | a208ff8 | 2019-07-01 05:51:27 -0500 | [diff] [blame] | 2707 | def certificateList(host, args, session): |
| 2708 | """ |
| 2709 | Called by certificate management function. |
| 2710 | Example: |
| 2711 | certificate list |
| 2712 | @param host: string, the hostname or IP address of the bmc |
| 2713 | @param args: contains additional arguments used by the certificate |
| 2714 | list sub command |
| 2715 | @param session: the active session to use |
| 2716 | """ |
| 2717 | if not redfishSupportPresent(host, session): |
| 2718 | return "Not supported"; |
| 2719 | |
| 2720 | httpHeader = {'Content-Type': 'application/octet-stream'} |
| 2721 | httpHeader.update(xAuthHeader) |
| 2722 | url = "https://" + host + \ |
| 2723 | "/redfish/v1/CertificateService/CertificateLocations/" |
| 2724 | try: |
| 2725 | resp = session.get(url, headers=httpHeader, verify=False) |
| 2726 | except(requests.exceptions.Timeout): |
| 2727 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2728 | except(requests.exceptions.ConnectionError) as err: |
| 2729 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2730 | if resp.status_code != 200: |
| 2731 | print(resp.text) |
| 2732 | return "Failed to list certificates" |
| 2733 | else: |
| 2734 | print("List certificates complete.") |
| 2735 | return resp.text |
| 2736 | |
Marri Devender Rao | 3cdf8ae | 2019-07-01 06:01:40 -0500 | [diff] [blame] | 2737 | def certificateGenerateCSR(host, args, session): |
| 2738 | """ |
| 2739 | Called by certificate management function. Generate CSR for server/ |
| 2740 | client certificates |
| 2741 | Example: |
Marri Devender Rao | df0e1a4 | 2019-09-09 08:18:27 -0500 | [diff] [blame^] | 2742 | certificate generatecsr server NJ w3.ibm.com US IBM IBM-UNIT NY EC prime256v1 cp abc.com an.com,bm.com gn sn un in |
| 2743 | certificate generatecsr client NJ w3.ibm.com US IBM IBM-UNIT NY EC prime256v1 cp abc.com an.com,bm.com gn sn un in |
Marri Devender Rao | 3cdf8ae | 2019-07-01 06:01:40 -0500 | [diff] [blame] | 2744 | @param host: string, the hostname or IP address of the bmc |
| 2745 | @param args: contains additional arguments used by the certificate replace sub command |
| 2746 | @param session: the active session to use |
| 2747 | """ |
| 2748 | if not redfishSupportPresent(host, session): |
| 2749 | return "Not supported"; |
| 2750 | |
| 2751 | httpHeader = {'Content-Type': 'application/octet-stream'} |
| 2752 | httpHeader.update(xAuthHeader) |
| 2753 | url = ""; |
| 2754 | if(args.type.lower() == 'server'): |
| 2755 | url = "/redfish/v1/Managers/bmc/NetworkProtocol/HTTPS/Certificates/" |
Marri Devender Rao | 88064f0 | 2019-08-19 09:00:30 -0500 | [diff] [blame] | 2756 | usage_list = ["ServerAuthentication"] |
Marri Devender Rao | 3cdf8ae | 2019-07-01 06:01:40 -0500 | [diff] [blame] | 2757 | elif(args.type.lower() == 'client'): |
| 2758 | url = "/redfish/v1/AccountService/LDAP/Certificates/" |
Marri Devender Rao | 88064f0 | 2019-08-19 09:00:30 -0500 | [diff] [blame] | 2759 | usage_list = ["ClientAuthentication"] |
Marri Devender Rao | 3cdf8ae | 2019-07-01 06:01:40 -0500 | [diff] [blame] | 2760 | elif(args.type.lower() == 'authority'): |
| 2761 | url = "/redfish/v1/Managers/bmc/Truststore/Certificates/" |
| 2762 | print("Generating CSR url=" + url) |
| 2763 | generateCSRUrl = "https://" + host + \ |
| 2764 | "/redfish/v1/CertificateService/Actions/CertificateService.GenerateCSR" |
| 2765 | try: |
Marri Devender Rao | 3cdf8ae | 2019-07-01 06:01:40 -0500 | [diff] [blame] | 2766 | alt_name_list = args.alternativeNames.split(",") |
| 2767 | data ={"CertificateCollection":{"@odata.id":url}, |
| 2768 | "CommonName":args.commonName, "City":args.city, |
| 2769 | "Country":args.country, "Organization":args.organization, |
| 2770 | "OrganizationalUnit":args.organizationUnit, "State":args.state, |
Marri Devender Rao | df0e1a4 | 2019-09-09 08:18:27 -0500 | [diff] [blame^] | 2771 | "KeyPairAlgorithm":args.keyPairAlgorithm, "KeyCurveId":args.keyCurveId, |
Marri Devender Rao | 3cdf8ae | 2019-07-01 06:01:40 -0500 | [diff] [blame] | 2772 | "AlternativeNames":alt_name_list, "ContactPerson":args.contactPerson, |
| 2773 | "Email":args.email, "GivenName":args.givenname, "Initials":args.initials, |
| 2774 | "KeyUsage":usage_list, "Surname":args.surname, |
| 2775 | "UnstructuredName":args.unstructuredname} |
| 2776 | resp = session.post(generateCSRUrl, headers=httpHeader, |
| 2777 | json=data, verify=False) |
| 2778 | except(requests.exceptions.Timeout): |
| 2779 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2780 | except(requests.exceptions.ConnectionError) as err: |
| 2781 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2782 | if resp.status_code != 200: |
| 2783 | print(resp.text) |
| 2784 | return "Failed to generate CSR" |
| 2785 | else: |
| 2786 | print("GenerateCSR complete.") |
| 2787 | return resp.text |
| 2788 | |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 2789 | def enableLDAPConfig(host, args, session): |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 2790 | """ |
| 2791 | Called by the ldap function. Configures LDAP. |
| 2792 | |
| 2793 | @param host: string, the hostname or IP address of the bmc |
| 2794 | @param args: contains additional arguments used by the ldap subcommand |
| 2795 | @param session: the active session to use |
| 2796 | @param args.json: boolean, if this flag is set to true, the output will |
| 2797 | be provided in json format for programmatic consumption |
| 2798 | """ |
| 2799 | |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 2800 | if(isRedfishSupport): |
| 2801 | return enableLDAP(host, args, session) |
| 2802 | else: |
| 2803 | return enableLegacyLDAP(host, args, session) |
| 2804 | |
| 2805 | def enableLegacyLDAP(host, args, session): |
| 2806 | """ |
| 2807 | Called by the ldap function. Configures LDAP on Lagecy systems. |
| 2808 | |
| 2809 | @param host: string, the hostname or IP address of the bmc |
| 2810 | @param args: contains additional arguments used by the ldap subcommand |
| 2811 | @param session: the active session to use |
| 2812 | @param args.json: boolean, if this flag is set to true, the output will |
| 2813 | be provided in json format for programmatic consumption |
| 2814 | """ |
| 2815 | |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 2816 | url='https://'+host+'/xyz/openbmc_project/user/ldap/action/CreateConfig' |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 2817 | scope = { |
| 2818 | 'sub' : 'xyz.openbmc_project.User.Ldap.Create.SearchScope.sub', |
| 2819 | 'one' : 'xyz.openbmc_project.User.Ldap.Create.SearchScope.one', |
| 2820 | 'base': 'xyz.openbmc_project.User.Ldap.Create.SearchScope.base' |
| 2821 | } |
| 2822 | |
| 2823 | serverType = { |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 2824 | 'ActiveDirectory' : 'xyz.openbmc_project.User.Ldap.Create.Type.ActiveDirectory', |
| 2825 | 'OpenLDAP' : 'xyz.openbmc_project.User.Ldap.Create.Type.OpenLdap' |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 2826 | } |
| 2827 | |
| 2828 | data = {"data": [args.uri, args.bindDN, args.baseDN, args.bindPassword, scope[args.scope], serverType[args.serverType]]} |
| 2829 | |
| 2830 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2831 | res = session.post(url, headers=jsonHeader, json=data, verify=False, timeout=baseTimeout) |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 2832 | except(requests.exceptions.Timeout): |
| 2833 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2834 | except(requests.exceptions.ConnectionError) as err: |
| 2835 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2836 | |
| 2837 | return res.text |
| 2838 | |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 2839 | def enableLDAP(host, args, session): |
| 2840 | """ |
| 2841 | Called by the ldap function. Configures LDAP for systems with latest user-manager design changes |
| 2842 | |
| 2843 | @param host: string, the hostname or IP address of the bmc |
| 2844 | @param args: contains additional arguments used by the ldap subcommand |
| 2845 | @param session: the active session to use |
| 2846 | @param args.json: boolean, if this flag is set to true, the output will |
| 2847 | be provided in json format for programmatic consumption |
| 2848 | """ |
| 2849 | |
| 2850 | scope = { |
| 2851 | 'sub' : 'xyz.openbmc_project.User.Ldap.Config.SearchScope.sub', |
| 2852 | 'one' : 'xyz.openbmc_project.User.Ldap.Config.SearchScope.one', |
| 2853 | 'base': 'xyz.openbmc_project.User.Ldap.Config.SearchScope.base' |
| 2854 | } |
| 2855 | |
| 2856 | serverType = { |
| 2857 | 'ActiveDirectory' : 'xyz.openbmc_project.User.Ldap.Config.Type.ActiveDirectory', |
| 2858 | 'OpenLDAP' : 'xyz.openbmc_project.User.Ldap.Config.Type.OpenLdap' |
| 2859 | } |
| 2860 | |
| 2861 | url = "https://"+host+"/xyz/openbmc_project/user/ldap/" |
| 2862 | |
| 2863 | serverTypeEnabled = getLDAPTypeEnabled(host,session) |
| 2864 | serverTypeToBeEnabled = args.serverType |
| 2865 | |
| 2866 | #If the given LDAP type is already enabled, then return |
| 2867 | if (serverTypeToBeEnabled == serverTypeEnabled): |
| 2868 | return("Server type " + serverTypeToBeEnabled + " is already enabled...") |
| 2869 | |
| 2870 | try: |
| 2871 | |
| 2872 | # Copy the role map from the currently enabled LDAP server type |
| 2873 | # to the newly enabled server type |
| 2874 | # Disable the currently enabled LDAP server type. Unless |
| 2875 | # it is disabled, we cannot enable a new LDAP server type |
| 2876 | if (serverTypeEnabled is not None): |
| 2877 | |
| 2878 | if (serverTypeToBeEnabled != serverTypeEnabled): |
| 2879 | res = syncRoleMap(host,args,session,serverTypeEnabled,serverTypeToBeEnabled) |
| 2880 | |
| 2881 | data = "{\"data\": 0 }" |
| 2882 | res = session.put(url + serverTypeMap[serverTypeEnabled] + '/attr/Enabled', headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
| 2883 | |
| 2884 | data = {"data": args.baseDN} |
| 2885 | res = session.put(url + serverTypeMap[serverTypeToBeEnabled] + '/attr/LDAPBaseDN', headers=jsonHeader, json=data, verify=False, timeout=baseTimeout) |
| 2886 | if (res.status_code != requests.codes.ok): |
| 2887 | print("Updates to the property LDAPBaseDN failed...") |
| 2888 | return(res.text) |
| 2889 | |
| 2890 | data = {"data": args.bindDN} |
| 2891 | res = session.put(url + serverTypeMap[serverTypeToBeEnabled] + '/attr/LDAPBindDN', headers=jsonHeader, json=data, verify=False, timeout=baseTimeout) |
| 2892 | if (res.status_code != requests.codes.ok): |
| 2893 | print("Updates to the property LDAPBindDN failed...") |
| 2894 | return(res.text) |
| 2895 | |
| 2896 | data = {"data": args.bindPassword} |
| 2897 | res = session.put(url + serverTypeMap[serverTypeToBeEnabled] + '/attr/LDAPBindDNPassword', headers=jsonHeader, json=data, verify=False, timeout=baseTimeout) |
| 2898 | if (res.status_code != requests.codes.ok): |
| 2899 | print("Updates to the property LDAPBindDNPassword failed...") |
| 2900 | return(res.text) |
| 2901 | |
| 2902 | data = {"data": scope[args.scope]} |
| 2903 | res = session.put(url + serverTypeMap[serverTypeToBeEnabled] + '/attr/LDAPSearchScope', headers=jsonHeader, json=data, verify=False, timeout=baseTimeout) |
| 2904 | if (res.status_code != requests.codes.ok): |
| 2905 | print("Updates to the property LDAPSearchScope failed...") |
| 2906 | return(res.text) |
| 2907 | |
| 2908 | data = {"data": args.uri} |
| 2909 | res = session.put(url + serverTypeMap[serverTypeToBeEnabled] + '/attr/LDAPServerURI', headers=jsonHeader, json=data, verify=False, timeout=baseTimeout) |
| 2910 | if (res.status_code != requests.codes.ok): |
| 2911 | print("Updates to the property LDAPServerURI failed...") |
| 2912 | return(res.text) |
| 2913 | |
| 2914 | data = {"data": args.groupAttrName} |
| 2915 | res = session.put(url + serverTypeMap[serverTypeToBeEnabled] + '/attr/GroupNameAttribute', headers=jsonHeader, json=data, verify=False, timeout=baseTimeout) |
| 2916 | if (res.status_code != requests.codes.ok): |
| 2917 | print("Updates to the property GroupNameAttribute failed...") |
| 2918 | return(res.text) |
| 2919 | |
| 2920 | data = {"data": args.userAttrName} |
| 2921 | res = session.put(url + serverTypeMap[serverTypeToBeEnabled] + '/attr/UserNameAttribute', headers=jsonHeader, json=data, verify=False, timeout=baseTimeout) |
| 2922 | if (res.status_code != requests.codes.ok): |
| 2923 | print("Updates to the property UserNameAttribute failed...") |
| 2924 | return(res.text) |
| 2925 | |
| 2926 | #After updating the properties, enable the new server type |
| 2927 | data = "{\"data\": 1 }" |
| 2928 | res = session.put(url + serverTypeMap[serverTypeToBeEnabled] + '/attr/Enabled', headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
| 2929 | |
| 2930 | except(requests.exceptions.Timeout): |
| 2931 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2932 | except(requests.exceptions.ConnectionError) as err: |
| 2933 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2934 | return res.text |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 2935 | |
| 2936 | def disableLDAP(host, args, session): |
| 2937 | """ |
| 2938 | Called by the ldap function. Deletes the LDAP Configuration. |
| 2939 | |
| 2940 | @param host: string, the hostname or IP address of the bmc |
| 2941 | @param args: contains additional arguments used by the ldap subcommand |
| 2942 | @param session: the active session to use |
| 2943 | @param args.json: boolean, if this flag is set to true, the output |
| 2944 | will be provided in json format for programmatic consumption |
| 2945 | """ |
| 2946 | |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 2947 | try: |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 2948 | if (isRedfishSupport) : |
| 2949 | |
| 2950 | url = "https://"+host+"/xyz/openbmc_project/user/ldap/" |
| 2951 | |
| 2952 | serverTypeEnabled = getLDAPTypeEnabled(host,session) |
| 2953 | |
| 2954 | if (serverTypeEnabled is not None): |
| 2955 | #To keep the role map in sync, |
| 2956 | #If the server type being disabled has role map, then |
| 2957 | # - copy the role map to the other server type(s) |
| 2958 | for serverType in serverTypeMap.keys(): |
| 2959 | if (serverType != serverTypeEnabled): |
| 2960 | res = syncRoleMap(host,args,session,serverTypeEnabled,serverType) |
| 2961 | |
| 2962 | #Disable the currently enabled LDAP server type |
| 2963 | data = "{\"data\": 0 }" |
| 2964 | res = session.put(url + serverTypeMap[serverTypeEnabled] + '/attr/Enabled', headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
| 2965 | |
| 2966 | else: |
| 2967 | return("LDAP server has not been enabled...") |
| 2968 | |
| 2969 | else : |
| 2970 | url='https://'+host+'/xyz/openbmc_project/user/ldap/config/action/delete' |
| 2971 | data = {"data": []} |
| 2972 | res = session.post(url, headers=jsonHeader, json=data, verify=False, timeout=baseTimeout) |
| 2973 | |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 2974 | except(requests.exceptions.Timeout): |
| 2975 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2976 | except(requests.exceptions.ConnectionError) as err: |
| 2977 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2978 | |
| 2979 | return res.text |
| 2980 | |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2981 | def enableDHCP(host, args, session): |
| 2982 | |
| 2983 | """ |
| 2984 | Called by the network function. Enables DHCP. |
| 2985 | |
| 2986 | @param host: string, the hostname or IP address of the bmc |
| 2987 | @param args: contains additional arguments used by the ldap subcommand |
| 2988 | args.json: boolean, if this flag is set to true, the output |
| 2989 | will be provided in json format for programmatic consumption |
| 2990 | @param session: the active session to use |
| 2991 | """ |
| 2992 | |
| 2993 | url = "https://"+host+"/xyz/openbmc_project/network/"+args.Interface+\ |
| 2994 | "/attr/DHCPEnabled" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2995 | data = "{\"data\": 1 }" |
| 2996 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2997 | res = session.put(url, headers=jsonHeader, data=data, verify=False, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 2998 | timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2999 | |
| 3000 | except(requests.exceptions.Timeout): |
| 3001 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3002 | except(requests.exceptions.ConnectionError) as err: |
| 3003 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3004 | if res.status_code == 403: |
| 3005 | return "The specified Interface"+"("+args.Interface+")"+\ |
| 3006 | " doesn't exist" |
| 3007 | |
| 3008 | return res.text |
| 3009 | |
| 3010 | |
| 3011 | def disableDHCP(host, args, session): |
| 3012 | """ |
| 3013 | Called by the network function. Disables DHCP. |
| 3014 | |
| 3015 | @param host: string, the hostname or IP address of the bmc |
| 3016 | @param args: contains additional arguments used by the ldap subcommand |
| 3017 | args.json: boolean, if this flag is set to true, the output |
| 3018 | will be provided in json format for programmatic consumption |
| 3019 | @param session: the active session to use |
| 3020 | """ |
| 3021 | |
| 3022 | url = "https://"+host+"/xyz/openbmc_project/network/"+args.Interface+\ |
| 3023 | "/attr/DHCPEnabled" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3024 | data = "{\"data\": 0 }" |
| 3025 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 3026 | res = session.put(url, headers=jsonHeader, data=data, verify=False, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3027 | timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3028 | except(requests.exceptions.Timeout): |
| 3029 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3030 | except(requests.exceptions.ConnectionError) as err: |
| 3031 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3032 | if res.status_code == 403: |
| 3033 | return "The specified Interface"+"("+args.Interface+")"+\ |
| 3034 | " doesn't exist" |
| 3035 | return res.text |
| 3036 | |
| 3037 | |
| 3038 | def getHostname(host, args, session): |
| 3039 | |
| 3040 | """ |
| 3041 | Called by the network function. Prints out the Hostname. |
| 3042 | |
| 3043 | @param host: string, the hostname or IP address of the bmc |
| 3044 | @param args: contains additional arguments used by the ldap subcommand |
| 3045 | args.json: boolean, if this flag is set to true, the output |
| 3046 | will be provided in json format for programmatic consumption |
| 3047 | @param session: the active session to use |
| 3048 | """ |
| 3049 | |
| 3050 | url = "https://"+host+"/xyz/openbmc_project/network/config/attr/HostName" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3051 | |
| 3052 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3053 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3054 | except(requests.exceptions.Timeout): |
| 3055 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3056 | except(requests.exceptions.ConnectionError) as err: |
| 3057 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3058 | |
| 3059 | return res.text |
| 3060 | |
| 3061 | |
| 3062 | def setHostname(host, args, session): |
| 3063 | """ |
| 3064 | Called by the network function. Sets the Hostname. |
| 3065 | |
| 3066 | @param host: string, the hostname or IP address of the bmc |
| 3067 | @param args: contains additional arguments used by the ldap subcommand |
| 3068 | args.json: boolean, if this flag is set to true, the output |
| 3069 | will be provided in json format for programmatic consumption |
| 3070 | @param session: the active session to use |
| 3071 | """ |
| 3072 | |
| 3073 | url = "https://"+host+"/xyz/openbmc_project/network/config/attr/HostName" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3074 | |
| 3075 | data = {"data": args.HostName} |
| 3076 | |
| 3077 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 3078 | res = session.put(url, headers=jsonHeader, json=data, verify=False, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3079 | timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3080 | except(requests.exceptions.Timeout): |
| 3081 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3082 | except(requests.exceptions.ConnectionError) as err: |
| 3083 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3084 | |
| 3085 | return res.text |
| 3086 | |
| 3087 | |
| 3088 | def getDomainName(host, args, session): |
| 3089 | |
| 3090 | """ |
| 3091 | Called by the network function. Prints out the DomainName. |
| 3092 | |
| 3093 | @param host: string, the hostname or IP address of the bmc |
| 3094 | @param args: contains additional arguments used by the ldap subcommand |
| 3095 | args.json: boolean, if this flag is set to true, the output |
| 3096 | will be provided in json format for programmatic consumption |
| 3097 | @param session: the active session to use |
| 3098 | """ |
| 3099 | |
| 3100 | url = "https://"+host+"/xyz/openbmc_project/network/"+args.Interface+\ |
| 3101 | "/attr/DomainName" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3102 | |
| 3103 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3104 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3105 | except(requests.exceptions.Timeout): |
| 3106 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3107 | except(requests.exceptions.ConnectionError) as err: |
| 3108 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3109 | if res.status_code == 404: |
| 3110 | return "The specified Interface"+"("+args.Interface+")"+\ |
| 3111 | " doesn't exist" |
| 3112 | |
| 3113 | return res.text |
| 3114 | |
| 3115 | |
| 3116 | def setDomainName(host, args, session): |
| 3117 | """ |
| 3118 | Called by the network function. Sets the DomainName. |
| 3119 | |
| 3120 | @param host: string, the hostname or IP address of the bmc |
| 3121 | @param args: contains additional arguments used by the ldap subcommand |
| 3122 | args.json: boolean, if this flag is set to true, the output |
| 3123 | will be provided in json format for programmatic consumption |
| 3124 | @param session: the active session to use |
| 3125 | """ |
| 3126 | |
| 3127 | url = "https://"+host+"/xyz/openbmc_project/network/"+args.Interface+\ |
| 3128 | "/attr/DomainName" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3129 | |
| 3130 | data = {"data": args.DomainName.split(",")} |
| 3131 | |
| 3132 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 3133 | res = session.put(url, headers=jsonHeader, json=data, verify=False, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3134 | timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3135 | except(requests.exceptions.Timeout): |
| 3136 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3137 | except(requests.exceptions.ConnectionError) as err: |
| 3138 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3139 | if res.status_code == 403: |
| 3140 | return "The specified Interface"+"("+args.Interface+")"+\ |
| 3141 | " doesn't exist" |
| 3142 | |
| 3143 | return res.text |
| 3144 | |
| 3145 | |
| 3146 | def getMACAddress(host, args, session): |
| 3147 | |
| 3148 | """ |
| 3149 | Called by the network function. Prints out the MACAddress. |
| 3150 | |
| 3151 | @param host: string, the hostname or IP address of the bmc |
| 3152 | @param args: contains additional arguments used by the ldap subcommand |
| 3153 | args.json: boolean, if this flag is set to true, the output |
| 3154 | will be provided in json format for programmatic consumption |
| 3155 | @param session: the active session to use |
| 3156 | """ |
| 3157 | |
| 3158 | url = "https://"+host+"/xyz/openbmc_project/network/"+args.Interface+\ |
| 3159 | "/attr/MACAddress" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3160 | |
| 3161 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3162 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3163 | except(requests.exceptions.Timeout): |
| 3164 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3165 | except(requests.exceptions.ConnectionError) as err: |
| 3166 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3167 | if res.status_code == 404: |
| 3168 | return "The specified Interface"+"("+args.Interface+")"+\ |
| 3169 | " doesn't exist" |
| 3170 | |
| 3171 | return res.text |
| 3172 | |
| 3173 | |
| 3174 | def setMACAddress(host, args, session): |
| 3175 | """ |
| 3176 | Called by the network function. Sets the MACAddress. |
| 3177 | |
| 3178 | @param host: string, the hostname or IP address of the bmc |
| 3179 | @param args: contains additional arguments used by the ldap subcommand |
| 3180 | args.json: boolean, if this flag is set to true, the output |
| 3181 | will be provided in json format for programmatic consumption |
| 3182 | @param session: the active session to use |
| 3183 | """ |
| 3184 | |
| 3185 | url = "https://"+host+"/xyz/openbmc_project/network/"+args.Interface+\ |
| 3186 | "/attr/MACAddress" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3187 | |
| 3188 | data = {"data": args.MACAddress} |
| 3189 | |
| 3190 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 3191 | res = session.put(url, headers=jsonHeader, json=data, verify=False, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3192 | timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3193 | except(requests.exceptions.Timeout): |
| 3194 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3195 | except(requests.exceptions.ConnectionError) as err: |
| 3196 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3197 | if res.status_code == 403: |
| 3198 | return "The specified Interface"+"("+args.Interface+")"+\ |
| 3199 | " doesn't exist" |
| 3200 | |
| 3201 | return res.text |
| 3202 | |
| 3203 | |
| 3204 | def getDefaultGateway(host, args, session): |
| 3205 | |
| 3206 | """ |
| 3207 | Called by the network function. Prints out the DefaultGateway. |
| 3208 | |
| 3209 | @param host: string, the hostname or IP address of the bmc |
| 3210 | @param args: contains additional arguments used by the ldap subcommand |
| 3211 | args.json: boolean, if this flag is set to true, the output |
| 3212 | will be provided in json format for programmatic consumption |
| 3213 | @param session: the active session to use |
| 3214 | """ |
| 3215 | |
| 3216 | url = "https://"+host+"/xyz/openbmc_project/network/config/attr/DefaultGateway" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3217 | |
| 3218 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3219 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3220 | except(requests.exceptions.Timeout): |
| 3221 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3222 | except(requests.exceptions.ConnectionError) as err: |
| 3223 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3224 | if res.status_code == 404: |
| 3225 | return "Failed to get Default Gateway info!!" |
| 3226 | |
| 3227 | return res.text |
| 3228 | |
| 3229 | |
| 3230 | def setDefaultGateway(host, args, session): |
| 3231 | """ |
| 3232 | Called by the network function. Sets the DefaultGateway. |
| 3233 | |
| 3234 | @param host: string, the hostname or IP address of the bmc |
| 3235 | @param args: contains additional arguments used by the ldap subcommand |
| 3236 | args.json: boolean, if this flag is set to true, the output |
| 3237 | will be provided in json format for programmatic consumption |
| 3238 | @param session: the active session to use |
| 3239 | """ |
| 3240 | |
| 3241 | url = "https://"+host+"/xyz/openbmc_project/network/config/attr/DefaultGateway" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3242 | |
| 3243 | data = {"data": args.DefaultGW} |
| 3244 | |
| 3245 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 3246 | res = session.put(url, headers=jsonHeader, json=data, verify=False, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3247 | timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3248 | except(requests.exceptions.Timeout): |
| 3249 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3250 | except(requests.exceptions.ConnectionError) as err: |
| 3251 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3252 | if res.status_code == 403: |
| 3253 | return "Failed to set Default Gateway!!" |
| 3254 | |
| 3255 | return res.text |
| 3256 | |
| 3257 | |
| 3258 | def viewNWConfig(host, args, session): |
| 3259 | """ |
| 3260 | Called by the ldap function. Prints out network configured properties |
| 3261 | |
| 3262 | @param host: string, the hostname or IP address of the bmc |
| 3263 | @param args: contains additional arguments used by the ldap subcommand |
| 3264 | args.json: boolean, if this flag is set to true, the output |
| 3265 | will be provided in json format for programmatic consumption |
| 3266 | @param session: the active session to use |
| 3267 | @return returns LDAP's configured properties. |
| 3268 | """ |
| 3269 | url = "https://"+host+"/xyz/openbmc_project/network/enumerate" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3270 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3271 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3272 | except(requests.exceptions.Timeout): |
| 3273 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3274 | except(requests.exceptions.ConnectionError) as err: |
| 3275 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3276 | except(requests.exceptions.RequestException) as err: |
| 3277 | return connectionErrHandler(args.json, "RequestException", err) |
| 3278 | if res.status_code == 404: |
| 3279 | return "LDAP server config has not been created" |
| 3280 | return res.text |
| 3281 | |
| 3282 | |
| 3283 | def getDNS(host, args, session): |
| 3284 | |
| 3285 | """ |
| 3286 | Called by the network function. Prints out DNS servers on the interface |
| 3287 | |
| 3288 | @param host: string, the hostname or IP address of the bmc |
| 3289 | @param args: contains additional arguments used by the ldap subcommand |
| 3290 | args.json: boolean, if this flag is set to true, the output |
| 3291 | will be provided in json format for programmatic consumption |
| 3292 | @param session: the active session to use |
| 3293 | """ |
| 3294 | |
| 3295 | url = "https://" + host + "/xyz/openbmc_project/network/" + args.Interface\ |
| 3296 | + "/attr/Nameservers" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3297 | |
| 3298 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3299 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3300 | except(requests.exceptions.Timeout): |
| 3301 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3302 | except(requests.exceptions.ConnectionError) as err: |
| 3303 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3304 | if res.status_code == 404: |
| 3305 | return "The specified Interface"+"("+args.Interface+")" + \ |
| 3306 | " doesn't exist" |
| 3307 | |
| 3308 | return res.text |
| 3309 | |
| 3310 | |
| 3311 | def setDNS(host, args, session): |
| 3312 | """ |
| 3313 | Called by the network function. Sets DNS servers on the interface. |
| 3314 | |
| 3315 | @param host: string, the hostname or IP address of the bmc |
| 3316 | @param args: contains additional arguments used by the ldap subcommand |
| 3317 | args.json: boolean, if this flag is set to true, the output |
| 3318 | will be provided in json format for programmatic consumption |
| 3319 | @param session: the active session to use |
| 3320 | """ |
| 3321 | |
| 3322 | url = "https://" + host + "/xyz/openbmc_project/network/" + args.Interface\ |
| 3323 | + "/attr/Nameservers" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3324 | |
| 3325 | data = {"data": args.DNSServers.split(",")} |
| 3326 | |
| 3327 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 3328 | res = session.put(url, headers=jsonHeader, json=data, verify=False, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3329 | timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3330 | except(requests.exceptions.Timeout): |
| 3331 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3332 | except(requests.exceptions.ConnectionError) as err: |
| 3333 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3334 | if res.status_code == 403: |
| 3335 | return "The specified Interface"+"("+args.Interface+")" +\ |
| 3336 | " doesn't exist" |
| 3337 | |
| 3338 | return res.text |
| 3339 | |
| 3340 | |
| 3341 | def getNTP(host, args, session): |
| 3342 | |
| 3343 | """ |
| 3344 | Called by the network function. Prints out NTP servers on the interface |
| 3345 | |
| 3346 | @param host: string, the hostname or IP address of the bmc |
| 3347 | @param args: contains additional arguments used by the ldap subcommand |
| 3348 | args.json: boolean, if this flag is set to true, the output |
| 3349 | will be provided in json format for programmatic consumption |
| 3350 | @param session: the active session to use |
| 3351 | """ |
| 3352 | |
| 3353 | url = "https://" + host + "/xyz/openbmc_project/network/" + args.Interface\ |
| 3354 | + "/attr/NTPServers" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3355 | |
| 3356 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3357 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3358 | except(requests.exceptions.Timeout): |
| 3359 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3360 | except(requests.exceptions.ConnectionError) as err: |
| 3361 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3362 | if res.status_code == 404: |
| 3363 | return "The specified Interface"+"("+args.Interface+")" + \ |
| 3364 | " doesn't exist" |
| 3365 | |
| 3366 | return res.text |
| 3367 | |
| 3368 | |
| 3369 | def setNTP(host, args, session): |
| 3370 | """ |
| 3371 | Called by the network function. Sets NTP servers on the interface. |
| 3372 | |
| 3373 | @param host: string, the hostname or IP address of the bmc |
| 3374 | @param args: contains additional arguments used by the ldap subcommand |
| 3375 | args.json: boolean, if this flag is set to true, the output |
| 3376 | will be provided in json format for programmatic consumption |
| 3377 | @param session: the active session to use |
| 3378 | """ |
| 3379 | |
| 3380 | url = "https://" + host + "/xyz/openbmc_project/network/" + args.Interface\ |
| 3381 | + "/attr/NTPServers" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3382 | |
| 3383 | data = {"data": args.NTPServers.split(",")} |
| 3384 | |
| 3385 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 3386 | res = session.put(url, headers=jsonHeader, json=data, verify=False, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3387 | timeout=baseTimeout) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3388 | except(requests.exceptions.Timeout): |
| 3389 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3390 | except(requests.exceptions.ConnectionError) as err: |
| 3391 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3392 | if res.status_code == 403: |
| 3393 | return "The specified Interface"+"("+args.Interface+")" +\ |
| 3394 | " doesn't exist" |
| 3395 | |
| 3396 | return res.text |
| 3397 | |
| 3398 | |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 3399 | def addIP(host, args, session): |
| 3400 | """ |
| 3401 | Called by the network function. Configures IP address on given interface |
| 3402 | |
| 3403 | @param host: string, the hostname or IP address of the bmc |
| 3404 | @param args: contains additional arguments used by the ldap subcommand |
| 3405 | args.json: boolean, if this flag is set to true, the output |
| 3406 | will be provided in json format for programmatic consumption |
| 3407 | @param session: the active session to use |
| 3408 | """ |
| 3409 | |
| 3410 | url = "https://" + host + "/xyz/openbmc_project/network/" + args.Interface\ |
| 3411 | + "/action/IP" |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 3412 | protocol = { |
| 3413 | 'ipv4': 'xyz.openbmc_project.Network.IP.Protocol.IPv4', |
| 3414 | 'ipv6': 'xyz.openbmc_project.Network.IP.Protocol.IPv6' |
| 3415 | } |
| 3416 | |
| 3417 | data = {"data": [protocol[args.type], args.address, int(args.prefixLength), |
| 3418 | args.gateway]} |
| 3419 | |
| 3420 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 3421 | res = session.post(url, headers=jsonHeader, json=data, verify=False, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3422 | timeout=baseTimeout) |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 3423 | except(requests.exceptions.Timeout): |
| 3424 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3425 | except(requests.exceptions.ConnectionError) as err: |
| 3426 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3427 | if res.status_code == 404: |
| 3428 | return "The specified Interface" + "(" + args.Interface + ")" +\ |
| 3429 | " doesn't exist" |
| 3430 | |
| 3431 | return res.text |
| 3432 | |
| 3433 | |
| 3434 | def getIP(host, args, session): |
| 3435 | """ |
| 3436 | Called by the network function. Prints out IP address of given interface |
| 3437 | |
| 3438 | @param host: string, the hostname or IP address of the bmc |
| 3439 | @param args: contains additional arguments used by the ldap subcommand |
| 3440 | args.json: boolean, if this flag is set to true, the output |
| 3441 | will be provided in json format for programmatic consumption |
| 3442 | @param session: the active session to use |
| 3443 | """ |
| 3444 | |
| 3445 | url = "https://" + host+"/xyz/openbmc_project/network/" + args.Interface +\ |
| 3446 | "/enumerate" |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 3447 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3448 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 3449 | except(requests.exceptions.Timeout): |
| 3450 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3451 | except(requests.exceptions.ConnectionError) as err: |
| 3452 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3453 | if res.status_code == 404: |
| 3454 | return "The specified Interface" + "(" + args.Interface + ")" +\ |
| 3455 | " doesn't exist" |
| 3456 | |
| 3457 | return res.text |
| 3458 | |
| 3459 | |
| 3460 | def deleteIP(host, args, session): |
| 3461 | """ |
| 3462 | Called by the network function. Deletes the IP address from given Interface |
| 3463 | |
| 3464 | @param host: string, the hostname or IP address of the bmc |
| 3465 | @param args: contains additional arguments used by the ldap subcommand |
| 3466 | @param session: the active session to use |
| 3467 | @param args.json: boolean, if this flag is set to true, the output |
| 3468 | will be provided in json format for programmatic consumption |
| 3469 | """ |
| 3470 | |
| 3471 | url = "https://"+host+"/xyz/openbmc_project/network/" + args.Interface+\ |
| 3472 | "/enumerate" |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 3473 | data = {"data": []} |
| 3474 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3475 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 3476 | except(requests.exceptions.Timeout): |
| 3477 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3478 | except(requests.exceptions.ConnectionError) as err: |
| 3479 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3480 | if res.status_code == 404: |
| 3481 | return "The specified Interface" + "(" + args.Interface + ")" +\ |
| 3482 | " doesn't exist" |
| 3483 | objDict = json.loads(res.text) |
| 3484 | if not objDict['data']: |
| 3485 | return "No object found for given address on given Interface" |
| 3486 | |
| 3487 | for obj in objDict['data']: |
Sunitha Harish | 0baf637 | 2019-07-31 03:59:03 -0500 | [diff] [blame] | 3488 | try: |
| 3489 | if args.address in objDict['data'][obj]['Address']: |
| 3490 | url = "https://"+host+obj+"/action/Delete" |
| 3491 | try: |
| 3492 | res = session.post(url, headers=jsonHeader, json=data, |
| 3493 | verify=False, timeout=baseTimeout) |
| 3494 | except(requests.exceptions.Timeout): |
| 3495 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3496 | except(requests.exceptions.ConnectionError) as err: |
| 3497 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3498 | return res.text |
| 3499 | else: |
| 3500 | continue |
| 3501 | except KeyError: |
| 3502 | return "No object found for address " + args.address + \ |
| 3503 | " on Interface(" + args.Interface + ")" |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 3504 | |
| 3505 | |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 3506 | def addVLAN(host, args, session): |
| 3507 | """ |
| 3508 | Called by the network function. Creates VLAN on given interface. |
| 3509 | |
| 3510 | @param host: string, the hostname or IP address of the bmc |
| 3511 | @param args: contains additional arguments used by the ldap subcommand |
| 3512 | args.json: boolean, if this flag is set to true, the output |
| 3513 | will be provided in json format for programmatic consumption |
| 3514 | @param session: the active session to use |
| 3515 | """ |
| 3516 | |
| 3517 | url = "https://" + host+"/xyz/openbmc_project/network/action/VLAN" |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 3518 | |
Sunitha Harish | 0baf637 | 2019-07-31 03:59:03 -0500 | [diff] [blame] | 3519 | data = {"data": [args.Interface,int(args.Identifier)]} |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 3520 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 3521 | res = session.post(url, headers=jsonHeader, json=data, verify=False, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3522 | timeout=baseTimeout) |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 3523 | except(requests.exceptions.Timeout): |
| 3524 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3525 | except(requests.exceptions.ConnectionError) as err: |
| 3526 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3527 | if res.status_code == 400: |
Sunitha Harish | 0baf637 | 2019-07-31 03:59:03 -0500 | [diff] [blame] | 3528 | return "Adding VLAN to interface" + "(" + args.Interface + ")" +\ |
| 3529 | " failed" |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 3530 | |
| 3531 | return res.text |
| 3532 | |
| 3533 | |
| 3534 | def deleteVLAN(host, args, session): |
| 3535 | """ |
| 3536 | Called by the network function. Creates VLAN on given interface. |
| 3537 | |
| 3538 | @param host: string, the hostname or IP address of the bmc |
| 3539 | @param args: contains additional arguments used by the ldap subcommand |
| 3540 | args.json: boolean, if this flag is set to true, the output |
| 3541 | will be provided in json format for programmatic consumption |
| 3542 | @param session: the active session to use |
| 3543 | """ |
| 3544 | |
Sunitha Harish | 577a503 | 2019-08-08 06:27:40 -0500 | [diff] [blame] | 3545 | url = "https://" + host+"/xyz/openbmc_project/network/"+args.Interface+"/action/Delete" |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 3546 | data = {"data": []} |
| 3547 | |
| 3548 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3549 | res = session.post(url, headers=jsonHeader, json=data, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 3550 | except(requests.exceptions.Timeout): |
| 3551 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3552 | except(requests.exceptions.ConnectionError) as err: |
| 3553 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3554 | if res.status_code == 404: |
Sunitha Harish | 577a503 | 2019-08-08 06:27:40 -0500 | [diff] [blame] | 3555 | return "The specified VLAN"+"("+args.Interface+")" +" doesn't exist" |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 3556 | |
| 3557 | return res.text |
| 3558 | |
| 3559 | |
| 3560 | def viewDHCPConfig(host, args, session): |
| 3561 | """ |
| 3562 | Called by the network function. Shows DHCP configured Properties. |
| 3563 | |
| 3564 | @param host: string, the hostname or IP address of the bmc |
| 3565 | @param args: contains additional arguments used by the ldap subcommand |
| 3566 | args.json: boolean, if this flag is set to true, the output |
| 3567 | will be provided in json format for programmatic consumption |
| 3568 | @param session: the active session to use |
| 3569 | """ |
| 3570 | |
| 3571 | url="https://"+host+"/xyz/openbmc_project/network/config/dhcp" |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 3572 | |
| 3573 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3574 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 3575 | except(requests.exceptions.Timeout): |
| 3576 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3577 | except(requests.exceptions.ConnectionError) as err: |
| 3578 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3579 | |
| 3580 | return res.text |
| 3581 | |
| 3582 | |
| 3583 | def configureDHCP(host, args, session): |
| 3584 | """ |
| 3585 | Called by the network function. Configures/updates DHCP Properties. |
| 3586 | |
| 3587 | @param host: string, the hostname or IP address of the bmc |
| 3588 | @param args: contains additional arguments used by the ldap subcommand |
| 3589 | args.json: boolean, if this flag is set to true, the output |
| 3590 | will be provided in json format for programmatic consumption |
| 3591 | @param session: the active session to use |
| 3592 | """ |
| 3593 | |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 3594 | |
| 3595 | try: |
| 3596 | url="https://"+host+"/xyz/openbmc_project/network/config/dhcp" |
| 3597 | if(args.DNSEnabled == True): |
| 3598 | data = '{"data": 1}' |
| 3599 | else: |
| 3600 | data = '{"data": 0}' |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 3601 | res = session.put(url + '/attr/DNSEnabled', headers=jsonHeader, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3602 | data=data, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 3603 | if(args.HostNameEnabled == True): |
| 3604 | data = '{"data": 1}' |
| 3605 | else: |
| 3606 | data = '{"data": 0}' |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 3607 | res = session.put(url + '/attr/HostNameEnabled', headers=jsonHeader, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3608 | data=data, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 3609 | if(args.NTPEnabled == True): |
| 3610 | data = '{"data": 1}' |
| 3611 | else: |
| 3612 | data = '{"data": 0}' |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 3613 | res = session.put(url + '/attr/NTPEnabled', headers=jsonHeader, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3614 | data=data, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 3615 | if(args.SendHostNameEnabled == True): |
| 3616 | data = '{"data": 1}' |
| 3617 | else: |
| 3618 | data = '{"data": 0}' |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 3619 | res = session.put(url + '/attr/SendHostNameEnabled', headers=jsonHeader, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3620 | data=data, verify=False, timeout=baseTimeout) |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 3621 | except(requests.exceptions.Timeout): |
| 3622 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3623 | except(requests.exceptions.ConnectionError) as err: |
| 3624 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3625 | |
| 3626 | return res.text |
| 3627 | |
| 3628 | |
| 3629 | def nwReset(host, args, session): |
| 3630 | |
| 3631 | """ |
| 3632 | Called by the network function. Resets networks setting to factory defaults. |
| 3633 | |
| 3634 | @param host: string, the hostname or IP address of the bmc |
| 3635 | @param args: contains additional arguments used by the ldap subcommand |
| 3636 | args.json: boolean, if this flag is set to true, the output |
| 3637 | will be provided in json format for programmatic consumption |
| 3638 | @param session: the active session to use |
| 3639 | """ |
| 3640 | |
| 3641 | url = "https://"+host+"/xyz/openbmc_project/network/action/Reset" |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 3642 | data = '{"data":[] }' |
| 3643 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 3644 | res = session.post(url, headers=jsonHeader, data=data, verify=False, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3645 | timeout=baseTimeout) |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 3646 | |
| 3647 | except(requests.exceptions.Timeout): |
| 3648 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3649 | except(requests.exceptions.ConnectionError) as err: |
| 3650 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3651 | |
| 3652 | return res.text |
| 3653 | |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 3654 | def getLDAPTypeEnabled(host,session): |
| 3655 | |
| 3656 | """ |
| 3657 | Called by LDAP related functions to find the LDAP server type that has been enabled. |
| 3658 | Returns None if LDAP has not been configured. |
| 3659 | |
| 3660 | @param host: string, the hostname or IP address of the bmc |
| 3661 | @param session: the active session to use |
| 3662 | """ |
| 3663 | |
| 3664 | enabled = False |
| 3665 | url = 'https://'+host+'/xyz/openbmc_project/user/ldap/' |
| 3666 | for key,value in serverTypeMap.items(): |
| 3667 | data = {"data": []} |
| 3668 | try: |
| 3669 | res = session.get(url + value + '/attr/Enabled', headers=jsonHeader, json=data, verify=False, timeout=baseTimeout) |
| 3670 | except(requests.exceptions.Timeout): |
| 3671 | print(connectionErrHandler(args.json, "Timeout", None)) |
| 3672 | return |
| 3673 | except(requests.exceptions.ConnectionError) as err: |
| 3674 | print(connectionErrHandler(args.json, "ConnectionError", err)) |
| 3675 | return |
| 3676 | |
| 3677 | enabled = res.json()['data'] |
| 3678 | if (enabled): |
| 3679 | return key |
| 3680 | |
| 3681 | def syncRoleMap(host,args,session,fromServerType,toServerType): |
| 3682 | |
| 3683 | """ |
| 3684 | Called by LDAP related functions to sync the role maps |
| 3685 | Returns False if LDAP has not been configured. |
| 3686 | |
| 3687 | @param host: string, the hostname or IP address of the bmc |
| 3688 | @param session: the active session to use |
| 3689 | @param fromServerType : Server type whose role map has to be copied |
| 3690 | @param toServerType : Server type to which role map has to be copied |
| 3691 | """ |
| 3692 | |
| 3693 | url = "https://"+host+"/xyz/openbmc_project/user/ldap/" |
| 3694 | |
| 3695 | try: |
| 3696 | #Note: If the fromServerType has no role map, then |
| 3697 | #the toServerType will not have any role map. |
| 3698 | |
| 3699 | #delete the privilege mapping from the toServerType and |
| 3700 | #then copy the privilege mapping from fromServerType to |
| 3701 | #toServerType. |
| 3702 | args.serverType = toServerType |
| 3703 | res = deleteAllPrivilegeMapping(host, args, session) |
| 3704 | |
| 3705 | data = {"data": []} |
| 3706 | res = session.get(url + serverTypeMap[fromServerType] + '/role_map/enumerate', headers=jsonHeader, json=data, verify=False, timeout=baseTimeout) |
| 3707 | #Previously enabled server type has no role map |
| 3708 | if (res.status_code != requests.codes.ok): |
| 3709 | |
| 3710 | #fromServerType has no role map; So, no need to copy |
| 3711 | #role map to toServerType. |
| 3712 | return |
| 3713 | |
| 3714 | objDict = json.loads(res.text) |
| 3715 | dataDict = objDict['data'] |
| 3716 | for key,value in dataDict.items(): |
| 3717 | data = {"data": [value["GroupName"], value["Privilege"]]} |
| 3718 | res = session.post(url + serverTypeMap[toServerType] + '/action/Create', headers=jsonHeader, json = data, verify=False, timeout=baseTimeout) |
| 3719 | |
| 3720 | except(requests.exceptions.Timeout): |
| 3721 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3722 | except(requests.exceptions.ConnectionError) as err: |
| 3723 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3724 | return res.text |
| 3725 | |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 3726 | |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 3727 | def createPrivilegeMapping(host, args, session): |
| 3728 | """ |
| 3729 | Called by the ldap function. Creates the group and the privilege mapping. |
| 3730 | |
| 3731 | @param host: string, the hostname or IP address of the bmc |
| 3732 | @param args: contains additional arguments used by the ldap subcommand |
| 3733 | @param session: the active session to use |
| 3734 | @param args.json: boolean, if this flag is set to true, the output |
| 3735 | will be provided in json format for programmatic consumption |
| 3736 | """ |
| 3737 | |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 3738 | try: |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 3739 | if (isRedfishSupport): |
| 3740 | url = 'https://'+host+'/xyz/openbmc_project/user/ldap/' |
| 3741 | |
| 3742 | #To maintain the interface compatibility between op930 and op940, the server type has been made |
| 3743 | #optional. If the server type is not specified, then create the role-mapper for the currently |
| 3744 | #enabled server type. |
| 3745 | serverType = args.serverType |
| 3746 | if (serverType is None): |
| 3747 | serverType = getLDAPTypeEnabled(host,session) |
| 3748 | if (serverType is None): |
| 3749 | return("LDAP server has not been enabled. Please specify LDAP serverType to proceed further...") |
| 3750 | |
| 3751 | data = {"data": [args.groupName,args.privilege]} |
| 3752 | res = session.post(url + serverTypeMap[serverType] + '/action/Create', headers=jsonHeader, json = data, verify=False, timeout=baseTimeout) |
| 3753 | |
| 3754 | else: |
| 3755 | url = 'https://'+host+'/xyz/openbmc_project/user/ldap/action/Create' |
| 3756 | data = {"data": [args.groupName,args.privilege]} |
| 3757 | res = session.post(url, headers=jsonHeader, json = data, verify=False, timeout=baseTimeout) |
| 3758 | |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 3759 | except(requests.exceptions.Timeout): |
| 3760 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3761 | except(requests.exceptions.ConnectionError) as err: |
| 3762 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3763 | return res.text |
| 3764 | |
| 3765 | def listPrivilegeMapping(host, args, session): |
| 3766 | """ |
| 3767 | Called by the ldap function. Lists the group and the privilege mapping. |
| 3768 | |
| 3769 | @param host: string, the hostname or IP address of the bmc |
| 3770 | @param args: contains additional arguments used by the ldap subcommand |
| 3771 | @param session: the active session to use |
| 3772 | @param args.json: boolean, if this flag is set to true, the output |
| 3773 | will be provided in json format for programmatic consumption |
| 3774 | """ |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 3775 | |
| 3776 | if (isRedfishSupport): |
| 3777 | serverType = args.serverType |
| 3778 | if (serverType is None): |
| 3779 | serverType = getLDAPTypeEnabled(host,session) |
| 3780 | if (serverType is None): |
| 3781 | return("LDAP has not been enabled. Please specify LDAP serverType to proceed further...") |
| 3782 | |
| 3783 | url = 'https://'+host+'/xyz/openbmc_project/user/ldap/'+serverTypeMap[serverType]+'/role_map/enumerate' |
| 3784 | |
| 3785 | else: |
| 3786 | url = 'https://'+host+'/xyz/openbmc_project/user/ldap/enumerate' |
| 3787 | |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 3788 | data = {"data": []} |
| 3789 | |
| 3790 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3791 | res = session.get(url, headers=jsonHeader, json = data, verify=False, timeout=baseTimeout) |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 3792 | except(requests.exceptions.Timeout): |
| 3793 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3794 | except(requests.exceptions.ConnectionError) as err: |
| 3795 | return connectionErrHandler(args.json, "ConnectionError", err) |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 3796 | |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 3797 | return res.text |
| 3798 | |
| 3799 | def deletePrivilegeMapping(host, args, session): |
| 3800 | """ |
| 3801 | Called by the ldap function. Deletes the mapping associated with the group. |
| 3802 | |
| 3803 | @param host: string, the hostname or IP address of the bmc |
| 3804 | @param args: contains additional arguments used by the ldap subcommand |
| 3805 | @param session: the active session to use |
| 3806 | @param args.json: boolean, if this flag is set to true, the output |
| 3807 | will be provided in json format for programmatic consumption |
| 3808 | """ |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 3809 | |
| 3810 | ldapNameSpaceObjects = listPrivilegeMapping(host, args, session) |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 3811 | ldapNameSpaceObjects = json.loads(ldapNameSpaceObjects)["data"] |
| 3812 | path = '' |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 3813 | data = {"data": []} |
| 3814 | |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 3815 | if (isRedfishSupport): |
| 3816 | if (args.serverType is None): |
| 3817 | serverType = getLDAPTypeEnabled(host,session) |
| 3818 | if (serverType is None): |
| 3819 | return("LDAP has not been enabled. Please specify LDAP serverType to proceed further...") |
| 3820 | # search for the object having the mapping for the given group |
| 3821 | for key,value in ldapNameSpaceObjects.items(): |
| 3822 | if value['GroupName'] == args.groupName: |
| 3823 | path = key |
| 3824 | break |
| 3825 | |
| 3826 | if path == '': |
| 3827 | return "No privilege mapping found for this group." |
| 3828 | |
| 3829 | # delete the object |
| 3830 | url = 'https://'+host+path+'/action/Delete' |
| 3831 | |
| 3832 | else: |
| 3833 | # not interested in the config objet |
| 3834 | ldapNameSpaceObjects.pop('/xyz/openbmc_project/user/ldap/config', None) |
| 3835 | |
| 3836 | # search for the object having the mapping for the given group |
| 3837 | for key,value in ldapNameSpaceObjects.items(): |
| 3838 | if value['GroupName'] == args.groupName: |
| 3839 | path = key |
| 3840 | break |
| 3841 | |
| 3842 | if path == '': |
| 3843 | return "No privilege mapping found for this group." |
| 3844 | |
| 3845 | # delete the object |
| 3846 | url = 'https://'+host+path+'/action/delete' |
| 3847 | |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 3848 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3849 | res = session.post(url, headers=jsonHeader, json = data, verify=False, timeout=baseTimeout) |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 3850 | except(requests.exceptions.Timeout): |
| 3851 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3852 | except(requests.exceptions.ConnectionError) as err: |
| 3853 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3854 | return res.text |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 3855 | |
Sivas SRR | 7883527 | 2018-11-27 05:27:19 -0600 | [diff] [blame] | 3856 | def deleteAllPrivilegeMapping(host, args, session): |
| 3857 | """ |
| 3858 | Called by the ldap function. Deletes all the privilege mapping and group defined. |
| 3859 | @param host: string, the hostname or IP address of the bmc |
| 3860 | @param args: contains additional arguments used by the ldap subcommand |
| 3861 | @param session: the active session to use |
| 3862 | @param args.json: boolean, if this flag is set to true, the output |
| 3863 | will be provided in json format for programmatic consumption |
| 3864 | """ |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 3865 | |
Sivas SRR | 7883527 | 2018-11-27 05:27:19 -0600 | [diff] [blame] | 3866 | ldapNameSpaceObjects = listPrivilegeMapping(host, args, session) |
| 3867 | ldapNameSpaceObjects = json.loads(ldapNameSpaceObjects)["data"] |
| 3868 | path = '' |
Sivas SRR | 7883527 | 2018-11-27 05:27:19 -0600 | [diff] [blame] | 3869 | data = {"data": []} |
| 3870 | |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 3871 | if (isRedfishSupport): |
| 3872 | if (args.serverType is None): |
| 3873 | serverType = getLDAPTypeEnabled(host,session) |
| 3874 | if (serverType is None): |
| 3875 | return("LDAP has not been enabled. Please specify LDAP serverType to proceed further...") |
| 3876 | |
| 3877 | else: |
| 3878 | # Remove the config object. |
| 3879 | ldapNameSpaceObjects.pop('/xyz/openbmc_project/user/ldap/config', None) |
| 3880 | |
Sivas SRR | 7883527 | 2018-11-27 05:27:19 -0600 | [diff] [blame] | 3881 | try: |
| 3882 | # search for GroupName property and delete if it is available. |
| 3883 | for path in ldapNameSpaceObjects.keys(): |
| 3884 | # delete the object |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 3885 | url = 'https://'+host+path+'/action/Delete' |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3886 | res = session.post(url, headers=jsonHeader, json = data, verify=False, timeout=baseTimeout) |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 3887 | |
Sivas SRR | 7883527 | 2018-11-27 05:27:19 -0600 | [diff] [blame] | 3888 | except(requests.exceptions.Timeout): |
| 3889 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3890 | except(requests.exceptions.ConnectionError) as err: |
| 3891 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3892 | return res.text |
| 3893 | |
Nagaraju Goruganti | 7d1fe17 | 2018-11-13 06:09:29 -0600 | [diff] [blame] | 3894 | def viewLDAPConfig(host, args, session): |
| 3895 | """ |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 3896 | Called by the ldap function. Prints out active LDAP configuration properties |
Nagaraju Goruganti | 7d1fe17 | 2018-11-13 06:09:29 -0600 | [diff] [blame] | 3897 | |
| 3898 | @param host: string, the hostname or IP address of the bmc |
| 3899 | @param args: contains additional arguments used by the ldap subcommand |
| 3900 | args.json: boolean, if this flag is set to true, the output |
| 3901 | will be provided in json format for programmatic consumption |
| 3902 | @param session: the active session to use |
| 3903 | @return returns LDAP's configured properties. |
| 3904 | """ |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 3905 | |
Nagaraju Goruganti | 7d1fe17 | 2018-11-13 06:09:29 -0600 | [diff] [blame] | 3906 | try: |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 3907 | if (isRedfishSupport): |
| 3908 | |
| 3909 | url = "https://"+host+"/xyz/openbmc_project/user/ldap/" |
| 3910 | |
| 3911 | serverTypeEnabled = getLDAPTypeEnabled(host,session) |
| 3912 | |
| 3913 | if (serverTypeEnabled is not None): |
| 3914 | data = {"data": []} |
| 3915 | res = session.get(url + serverTypeMap[serverTypeEnabled], headers=jsonHeader, json=data, verify=False, timeout=baseTimeout) |
| 3916 | else: |
| 3917 | return("LDAP server has not been enabled...") |
| 3918 | |
| 3919 | else : |
| 3920 | url = "https://"+host+"/xyz/openbmc_project/user/ldap/config" |
| 3921 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
| 3922 | |
Nagaraju Goruganti | 7d1fe17 | 2018-11-13 06:09:29 -0600 | [diff] [blame] | 3923 | except(requests.exceptions.Timeout): |
| 3924 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3925 | except(requests.exceptions.ConnectionError) as err: |
| 3926 | return connectionErrHandler(args.json, "ConnectionError", err) |
Nagaraju Goruganti | 7d1fe17 | 2018-11-13 06:09:29 -0600 | [diff] [blame] | 3927 | if res.status_code == 404: |
| 3928 | return "LDAP server config has not been created" |
| 3929 | return res.text |
| 3930 | |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 3931 | def str2bool(v): |
| 3932 | if v.lower() in ('yes', 'true', 't', 'y', '1'): |
| 3933 | return True |
| 3934 | elif v.lower() in ('no', 'false', 'f', 'n', '0'): |
| 3935 | return False |
| 3936 | else: |
| 3937 | raise argparse.ArgumentTypeError('Boolean value expected.') |
Nagaraju Goruganti | 7d1fe17 | 2018-11-13 06:09:29 -0600 | [diff] [blame] | 3938 | |
Matt Spinler | 7d426c2 | 2018-09-24 14:42:07 -0500 | [diff] [blame] | 3939 | def localUsers(host, args, session): |
| 3940 | """ |
| 3941 | Enables and disables local BMC users. |
| 3942 | |
| 3943 | @param host: string, the hostname or IP address of the bmc |
| 3944 | @param args: contains additional arguments used by the logging sub command |
| 3945 | @param session: the active session to use |
| 3946 | """ |
| 3947 | |
Matt Spinler | 7d426c2 | 2018-09-24 14:42:07 -0500 | [diff] [blame] | 3948 | url="https://{hostname}/xyz/openbmc_project/user/enumerate".format(hostname=host) |
| 3949 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3950 | res = session.get(url, headers=jsonHeader, verify=False, timeout=baseTimeout) |
Matt Spinler | 7d426c2 | 2018-09-24 14:42:07 -0500 | [diff] [blame] | 3951 | except(requests.exceptions.Timeout): |
| 3952 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3953 | usersDict = json.loads(res.text) |
| 3954 | |
| 3955 | if not usersDict['data']: |
| 3956 | return "No users found" |
| 3957 | |
| 3958 | output = "" |
| 3959 | for user in usersDict['data']: |
Matt Spinler | 015adc2 | 2018-10-23 14:30:19 -0500 | [diff] [blame] | 3960 | |
| 3961 | # Skip LDAP and another non-local users |
| 3962 | if 'UserEnabled' not in usersDict['data'][user]: |
| 3963 | continue |
| 3964 | |
Matt Spinler | 7d426c2 | 2018-09-24 14:42:07 -0500 | [diff] [blame] | 3965 | name = user.split('/')[-1] |
| 3966 | url = "https://{hostname}{user}/attr/UserEnabled".format(hostname=host, user=user) |
| 3967 | |
| 3968 | if args.local_users == "queryenabled": |
| 3969 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3970 | res = session.get(url, headers=jsonHeader,verify=False, timeout=baseTimeout) |
Matt Spinler | 7d426c2 | 2018-09-24 14:42:07 -0500 | [diff] [blame] | 3971 | except(requests.exceptions.Timeout): |
| 3972 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3973 | |
| 3974 | result = json.loads(res.text) |
| 3975 | output += ("User: {name} Enabled: {result}\n").format(name=name, result=result['data']) |
| 3976 | |
| 3977 | elif args.local_users in ["enableall", "disableall"]: |
| 3978 | action = "" |
| 3979 | if args.local_users == "enableall": |
| 3980 | data = '{"data": true}' |
| 3981 | action = "Enabling" |
| 3982 | else: |
| 3983 | data = '{"data": false}' |
| 3984 | action = "Disabling" |
| 3985 | |
| 3986 | output += "{action} {name}\n".format(action=action, name=name) |
| 3987 | |
| 3988 | try: |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 3989 | resp = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=baseTimeout) |
Matt Spinler | 7d426c2 | 2018-09-24 14:42:07 -0500 | [diff] [blame] | 3990 | except(requests.exceptions.Timeout): |
| 3991 | return connectionErrHandler(args.json, "Timeout", None) |
| 3992 | except(requests.exceptions.ConnectionError) as err: |
| 3993 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3994 | else: |
| 3995 | return "Invalid local users argument" |
| 3996 | |
| 3997 | return output |
| 3998 | |
Marri Devender Rao | 2c2a516 | 2018-11-05 08:57:11 -0600 | [diff] [blame] | 3999 | def setPassword(host, args, session): |
| 4000 | """ |
| 4001 | Set local user password |
| 4002 | @param host: string, the hostname or IP address of the bmc |
| 4003 | @param args: contains additional arguments used by the logging sub |
| 4004 | command |
| 4005 | @param session: the active session to use |
| 4006 | @param args.json: boolean, if this flag is set to true, the output |
| 4007 | will be provided in json format for programmatic consumption |
| 4008 | @return: Session object |
| 4009 | """ |
Marri Devender Rao | 2c2a516 | 2018-11-05 08:57:11 -0600 | [diff] [blame] | 4010 | try: |
Sunitha Harish | c99faba | 2019-07-19 06:55:22 -0500 | [diff] [blame] | 4011 | if(isRedfishSupport): |
| 4012 | url = "https://" + host + "/redfish/v1/AccountService/Accounts/"+ \ |
| 4013 | args.user |
| 4014 | data = {"Password":args.password} |
| 4015 | res = session.patch(url, headers=jsonHeader, json=data, |
| 4016 | verify=False, timeout=baseTimeout) |
| 4017 | else: |
| 4018 | url = "https://" + host + "/xyz/openbmc_project/user/" + args.user + \ |
| 4019 | "/action/SetPassword" |
| 4020 | res = session.post(url, headers=jsonHeader, |
Marri Devender Rao | 2c2a516 | 2018-11-05 08:57:11 -0600 | [diff] [blame] | 4021 | json={"data": [args.password]}, verify=False, |
Justin Thaler | 2719762 | 2019-01-23 14:42:11 -0600 | [diff] [blame] | 4022 | timeout=baseTimeout) |
Marri Devender Rao | 2c2a516 | 2018-11-05 08:57:11 -0600 | [diff] [blame] | 4023 | except(requests.exceptions.Timeout): |
| 4024 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 4025 | except(requests.exceptions.ConnectionError) as err: |
| 4026 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 4027 | except(requests.exceptions.RequestException) as err: |
| 4028 | return connectionErrHandler(args.json, "RequestException", err) |
Sunitha Harish | c99faba | 2019-07-19 06:55:22 -0500 | [diff] [blame] | 4029 | return res.status_code |
Matthew Barth | 368e83c | 2019-02-01 13:48:25 -0600 | [diff] [blame] | 4030 | |
| 4031 | def getThermalZones(host, args, session): |
| 4032 | """ |
| 4033 | Get the available thermal control zones |
| 4034 | @param host: string, the hostname or IP address of the bmc |
| 4035 | @param args: contains additional arguments used to get the thermal |
| 4036 | control zones |
| 4037 | @param session: the active session to use |
| 4038 | @return: Session object |
| 4039 | """ |
| 4040 | url = "https://" + host + "/xyz/openbmc_project/control/thermal/enumerate" |
| 4041 | |
| 4042 | try: |
| 4043 | res = session.get(url, headers=jsonHeader, verify=False, timeout=30) |
| 4044 | except(requests.exceptions.Timeout): |
| 4045 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 4046 | except(requests.exceptions.ConnectionError) as err: |
| 4047 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 4048 | except(requests.exceptions.RequestException) as err: |
| 4049 | return connectionErrHandler(args.json, "RequestException", err) |
| 4050 | |
| 4051 | if (res.status_code == 404): |
| 4052 | return "No thermal control zones found or system is in a" + \ |
| 4053 | " powered off state" |
| 4054 | |
| 4055 | zonesDict = json.loads(res.text) |
| 4056 | if not zonesDict['data']: |
| 4057 | return "No thermal control zones found" |
| 4058 | for zone in zonesDict['data']: |
| 4059 | z = ",".join(str(zone.split('/')[-1]) for zone in zonesDict['data']) |
| 4060 | |
| 4061 | return "Zones: [ " + z + " ]" |
| 4062 | |
| 4063 | |
| 4064 | def getThermalMode(host, args, session): |
| 4065 | """ |
| 4066 | Get thermal control mode |
| 4067 | @param host: string, the hostname or IP address of the bmc |
| 4068 | @param args: contains additional arguments used to get the thermal |
| 4069 | control mode |
| 4070 | @param session: the active session to use |
| 4071 | @param args.zone: the zone to get the mode on |
| 4072 | @return: Session object |
| 4073 | """ |
| 4074 | url = "https://" + host + "/xyz/openbmc_project/control/thermal/" + \ |
| 4075 | args.zone |
| 4076 | |
| 4077 | try: |
| 4078 | res = session.get(url, headers=jsonHeader, verify=False, timeout=30) |
| 4079 | except(requests.exceptions.Timeout): |
| 4080 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 4081 | except(requests.exceptions.ConnectionError) as err: |
| 4082 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 4083 | except(requests.exceptions.RequestException) as err: |
| 4084 | return connectionErrHandler(args.json, "RequestException", err) |
| 4085 | |
| 4086 | if (res.status_code == 404): |
| 4087 | return "Thermal control zone(" + args.zone + ") not found or" + \ |
| 4088 | " system is in a powered off state" |
| 4089 | |
| 4090 | propsDict = json.loads(res.text) |
| 4091 | if not propsDict['data']: |
| 4092 | return "No thermal control properties found on zone(" + args.zone + ")" |
| 4093 | curMode = "Current" |
| 4094 | supModes = "Supported" |
| 4095 | result = "\n" |
| 4096 | for prop in propsDict['data']: |
| 4097 | if (prop.casefold() == curMode.casefold()): |
| 4098 | result += curMode + " Mode: " + propsDict['data'][curMode] + "\n" |
| 4099 | if (prop.casefold() == supModes.casefold()): |
| 4100 | s = ", ".join(str(sup) for sup in propsDict['data'][supModes]) |
| 4101 | result += supModes + " Modes: [ " + s + " ]\n" |
| 4102 | |
| 4103 | return result |
| 4104 | |
| 4105 | def setThermalMode(host, args, session): |
| 4106 | """ |
| 4107 | Set thermal control mode |
| 4108 | @param host: string, the hostname or IP address of the bmc |
| 4109 | @param args: contains additional arguments used for setting the thermal |
| 4110 | control mode |
| 4111 | @param session: the active session to use |
| 4112 | @param args.zone: the zone to set the mode on |
| 4113 | @param args.mode: the mode to enable |
| 4114 | @return: Session object |
| 4115 | """ |
| 4116 | url = "https://" + host + "/xyz/openbmc_project/control/thermal/" + \ |
| 4117 | args.zone + "/attr/Current" |
| 4118 | |
| 4119 | # Check args.mode against supported modes using `getThermalMode` output |
| 4120 | modes = getThermalMode(host, args, session) |
| 4121 | modes = os.linesep.join([m for m in modes.splitlines() if m]) |
| 4122 | modes = modes.replace("\n", ";").strip() |
| 4123 | modesDict = dict(m.split(': ') for m in modes.split(';')) |
| 4124 | sModes = ''.join(s for s in modesDict['Supported Modes'] if s not in '[ ]') |
| 4125 | if args.mode.casefold() not in \ |
| 4126 | (m.casefold() for m in sModes.split(',')) or not args.mode: |
| 4127 | result = ("Unsupported mode('" + args.mode + "') given, " + |
| 4128 | "select a supported mode: \n" + |
| 4129 | getThermalMode(host, args, session)) |
| 4130 | return result |
| 4131 | |
| 4132 | data = '{"data":"' + args.mode + '"}' |
| 4133 | try: |
| 4134 | res = session.get(url, headers=jsonHeader, verify=False, timeout=30) |
| 4135 | except(requests.exceptions.Timeout): |
| 4136 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 4137 | except(requests.exceptions.ConnectionError) as err: |
| 4138 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 4139 | except(requests.exceptions.RequestException) as err: |
| 4140 | return connectionErrHandler(args.json, "RequestException", err) |
| 4141 | |
| 4142 | if (data and res.status_code != 404): |
| 4143 | try: |
| 4144 | res = session.put(url, headers=jsonHeader, |
| 4145 | data=data, verify=False, |
| 4146 | timeout=30) |
| 4147 | except(requests.exceptions.Timeout): |
| 4148 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 4149 | except(requests.exceptions.ConnectionError) as err: |
| 4150 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 4151 | except(requests.exceptions.RequestException) as err: |
| 4152 | return connectionErrHandler(args.json, "RequestException", err) |
| 4153 | |
| 4154 | if res.status_code == 403: |
| 4155 | return "The specified thermal control zone(" + args.zone + ")" + \ |
| 4156 | " does not exist" |
| 4157 | |
| 4158 | return res.text |
| 4159 | else: |
| 4160 | return "Setting thermal control mode(" + args.mode + ")" + \ |
| 4161 | " not supported or operation not available(system powered off?)" |
| 4162 | |
| 4163 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4164 | def createCommandParser(): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4165 | """ |
| 4166 | creates the parser for the command line along with help for each command and subcommand |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4167 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4168 | @return: returns the parser for the command line |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4169 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4170 | parser = argparse.ArgumentParser(description='Process arguments') |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4171 | parser.add_argument("-H", "--host", help='A hostname or IP for the BMC') |
| 4172 | parser.add_argument("-U", "--user", help='The username to login with') |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4173 | group = parser.add_mutually_exclusive_group() |
| 4174 | group.add_argument("-A", "--askpw", action='store_true', help='prompt for password') |
| 4175 | group.add_argument("-P", "--PW", help='Provide the password in-line') |
Joseph Reynolds | a2d54c5 | 2019-06-11 22:02:57 -0500 | [diff] [blame] | 4176 | group.add_argument("-E", "--PWenvvar", action='store_true', help='Get password from envvar OPENBMCTOOL_PASSWORD') |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4177 | parser.add_argument('-j', '--json', action='store_true', help='output json data only') |
| 4178 | parser.add_argument('-t', '--policyTableLoc', help='The location of the policy table to parse alerts') |
| 4179 | parser.add_argument('-c', '--CerFormat', action='store_true', help=argparse.SUPPRESS) |
| 4180 | parser.add_argument('-T', '--procTime', action='store_true', help= argparse.SUPPRESS) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4181 | parser.add_argument('-V', '--version', action='store_true', help='Display the version number of the openbmctool') |
| 4182 | subparsers = parser.add_subparsers(title='subcommands', description='valid subcommands',help="sub-command help", dest='command') |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4183 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4184 | #fru command |
| 4185 | parser_inv = subparsers.add_parser("fru", help='Work with platform inventory') |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4186 | inv_subparser = parser_inv.add_subparsers(title='subcommands', description='valid inventory actions', help="valid inventory actions", dest='command') |
Justin Thaler | 53bf2f1 | 2018-07-16 14:05:32 -0500 | [diff] [blame] | 4187 | inv_subparser.required = True |
| 4188 | #fru print |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4189 | inv_print = inv_subparser.add_parser("print", help="prints out a list of all FRUs") |
| 4190 | inv_print.set_defaults(func=fruPrint) |
| 4191 | #fru list [0....n] |
| 4192 | inv_list = inv_subparser.add_parser("list", help="print out details on selected FRUs. Specifying no items will list the entire inventory") |
| 4193 | inv_list.add_argument('items', nargs='?', help="print out details on selected FRUs. Specifying no items will list the entire inventory") |
| 4194 | inv_list.set_defaults(func=fruList) |
| 4195 | #fru status |
| 4196 | inv_status = inv_subparser.add_parser("status", help="prints out the status of all FRUs") |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4197 | inv_status.add_argument('-v', '--verbose', action='store_true', help='Verbose output') |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4198 | inv_status.set_defaults(func=fruStatus) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4199 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4200 | #sensors command |
| 4201 | parser_sens = subparsers.add_parser("sensors", help="Work with platform sensors") |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4202 | sens_subparser=parser_sens.add_subparsers(title='subcommands', description='valid sensor actions', help='valid sensor actions', dest='command') |
Justin Thaler | 53bf2f1 | 2018-07-16 14:05:32 -0500 | [diff] [blame] | 4203 | sens_subparser.required = True |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4204 | #sensor print |
| 4205 | sens_print= sens_subparser.add_parser('print', help="prints out a list of all Sensors.") |
| 4206 | sens_print.set_defaults(func=sensor) |
| 4207 | #sensor list[0...n] |
| 4208 | sens_list=sens_subparser.add_parser("list", help="Lists all Sensors in the platform. Specify a sensor for full details. ") |
| 4209 | sens_list.add_argument("sensNum", nargs='?', help="The Sensor number to get full details on" ) |
| 4210 | sens_list.set_defaults(func=sensor) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4211 | |
Matthew Barth | 368e83c | 2019-02-01 13:48:25 -0600 | [diff] [blame] | 4212 | #thermal control commands |
| 4213 | parser_therm = subparsers.add_parser("thermal", help="Work with thermal control parameters") |
| 4214 | therm_subparser=parser_therm.add_subparsers(title='subcommands', description='Thermal control actions to work with', help='Valid thermal control actions to work with', dest='command') |
| 4215 | #thermal control zones |
| 4216 | parser_thermZones = therm_subparser.add_parser("zones", help="Get a list of available thermal control zones") |
| 4217 | parser_thermZones.set_defaults(func=getThermalZones) |
| 4218 | #thermal control modes |
| 4219 | parser_thermMode = therm_subparser.add_parser("modes", help="Work with thermal control modes") |
| 4220 | thermMode_sub = parser_thermMode.add_subparsers(title='subactions', description='Work with thermal control modes', help="Work with thermal control modes") |
| 4221 | #get thermal control mode |
| 4222 | parser_getThermMode = thermMode_sub.add_parser("get", help="Get current and supported thermal control modes") |
| 4223 | parser_getThermMode.add_argument('-z', '--zone', required=True, help='Thermal zone to work with') |
| 4224 | parser_getThermMode.set_defaults(func=getThermalMode) |
| 4225 | #set thermal control mode |
| 4226 | parser_setThermMode = thermMode_sub.add_parser("set", help="Set the thermal control mode") |
| 4227 | parser_setThermMode.add_argument('-z', '--zone', required=True, help='Thermal zone to work with') |
| 4228 | parser_setThermMode.add_argument('-m', '--mode', required=True, help='The supported thermal control mode') |
| 4229 | parser_setThermMode.set_defaults(func=setThermalMode) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4230 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4231 | #sel command |
| 4232 | parser_sel = subparsers.add_parser("sel", help="Work with platform alerts") |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4233 | sel_subparser = parser_sel.add_subparsers(title='subcommands', description='valid SEL actions', help = 'valid SEL actions', dest='command') |
Justin Thaler | 53bf2f1 | 2018-07-16 14:05:32 -0500 | [diff] [blame] | 4234 | sel_subparser.required = True |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4235 | #sel print |
| 4236 | sel_print = sel_subparser.add_parser("print", help="prints out a list of all sels in a condensed list") |
| 4237 | sel_print.add_argument('-d', '--devdebug', action='store_true', help=argparse.SUPPRESS) |
| 4238 | sel_print.add_argument('-v', '--verbose', action='store_true', help="Changes the output to being very verbose") |
| 4239 | sel_print.add_argument('-f', '--fileloc', help='Parse a file instead of the BMC output') |
| 4240 | sel_print.set_defaults(func=selPrint) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4241 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4242 | #sel list |
| 4243 | sel_list = sel_subparser.add_parser("list", help="Lists all SELs in the platform. Specifying a specific number will pull all the details for that individual SEL") |
| 4244 | sel_list.add_argument("selNum", nargs='?', type=int, help="The SEL entry to get details on") |
| 4245 | sel_list.set_defaults(func=selList) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4246 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4247 | sel_get = sel_subparser.add_parser("get", help="Gets the verbose details of a specified SEL entry") |
| 4248 | sel_get.add_argument('selNum', type=int, help="the number of the SEL entry to get") |
| 4249 | sel_get.set_defaults(func=selList) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4250 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4251 | sel_clear = sel_subparser.add_parser("clear", help="Clears all entries from the SEL") |
| 4252 | sel_clear.set_defaults(func=selClear) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4253 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4254 | sel_setResolved = sel_subparser.add_parser("resolve", help="Sets the sel entry to resolved") |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4255 | sel_setResolved.add_argument('-n', '--selNum', type=int, help="the number of the SEL entry to resolve") |
| 4256 | sel_ResolveAll_sub = sel_setResolved.add_subparsers(title='subcommands', description='valid subcommands',help="sub-command help", dest='command') |
| 4257 | sel_ResolveAll = sel_ResolveAll_sub.add_parser('all', help='Resolve all SEL entries') |
| 4258 | sel_ResolveAll.set_defaults(func=selResolveAll) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4259 | sel_setResolved.set_defaults(func=selSetResolved) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4260 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4261 | parser_chassis = subparsers.add_parser("chassis", help="Work with chassis power and status") |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4262 | chas_sub = parser_chassis.add_subparsers(title='subcommands', description='valid subcommands',help="sub-command help", dest='command') |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4263 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4264 | parser_chassis.add_argument('status', action='store_true', help='Returns the current status of the platform') |
| 4265 | parser_chassis.set_defaults(func=chassis) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4266 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4267 | parser_chasPower = chas_sub.add_parser("power", help="Turn the chassis on or off, check the power state") |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4268 | parser_chasPower.add_argument('powcmd', choices=['on','softoff', 'hardoff', 'status'], help='The value for the power command. on, off, or status') |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4269 | parser_chasPower.set_defaults(func=chassisPower) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4270 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4271 | #control the chassis identify led |
| 4272 | parser_chasIdent = chas_sub.add_parser("identify", help="Control the chassis identify led") |
| 4273 | parser_chasIdent.add_argument('identcmd', choices=['on', 'off', 'status'], help='The control option for the led: on, off, blink, status') |
| 4274 | parser_chasIdent.set_defaults(func=chassisIdent) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4275 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4276 | #collect service data |
| 4277 | parser_servData = subparsers.add_parser("collect_service_data", help="Collect all bmc data needed for service") |
| 4278 | parser_servData.add_argument('-d', '--devdebug', action='store_true', help=argparse.SUPPRESS) |
| 4279 | parser_servData.set_defaults(func=collectServiceData) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4280 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4281 | #system quick health check |
| 4282 | parser_healthChk = subparsers.add_parser("health_check", help="Work with platform sensors") |
| 4283 | parser_healthChk.set_defaults(func=healthCheck) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4284 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4285 | #work with bmc dumps |
| 4286 | parser_bmcdump = subparsers.add_parser("dump", help="Work with bmc dump files") |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4287 | bmcDump_sub = parser_bmcdump.add_subparsers(title='subcommands', description='valid subcommands',help="sub-command help", dest='command') |
Justin Thaler | 53bf2f1 | 2018-07-16 14:05:32 -0500 | [diff] [blame] | 4288 | bmcDump_sub.required = True |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4289 | dump_Create = bmcDump_sub.add_parser('create', help="Create a bmc dump") |
| 4290 | dump_Create.set_defaults(func=bmcDumpCreate) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4291 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4292 | dump_list = bmcDump_sub.add_parser('list', help="list all bmc dump files") |
| 4293 | dump_list.set_defaults(func=bmcDumpList) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4294 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4295 | parserdumpdelete = bmcDump_sub.add_parser('delete', help="Delete bmc dump files") |
| 4296 | parserdumpdelete.add_argument("-n", "--dumpNum", nargs='*', type=int, help="The Dump entry to delete") |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4297 | parserdumpdelete.set_defaults(func=bmcDumpDelete) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4298 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4299 | bmcDumpDelsub = parserdumpdelete.add_subparsers(title='subcommands', description='valid subcommands',help="sub-command help", dest='command') |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4300 | deleteAllDumps = bmcDumpDelsub.add_parser('all', help='Delete all bmc dump files') |
| 4301 | deleteAllDumps.set_defaults(func=bmcDumpDeleteAll) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4302 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4303 | parser_dumpretrieve = bmcDump_sub.add_parser('retrieve', help='Retrieve a dump file') |
| 4304 | parser_dumpretrieve.add_argument("dumpNum", type=int, help="The Dump entry to delete") |
| 4305 | parser_dumpretrieve.add_argument("-s", "--dumpSaveLoc", help="The location to save the bmc dump file") |
| 4306 | parser_dumpretrieve.set_defaults(func=bmcDumpRetrieve) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4307 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 4308 | #bmc command for reseting the bmc |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4309 | parser_bmc = subparsers.add_parser('bmc', help="Work with the bmc") |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4310 | bmc_sub = parser_bmc.add_subparsers(title='subcommands', description='valid subcommands',help="sub-command help", dest='command') |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4311 | parser_BMCReset = bmc_sub.add_parser('reset', help='Reset the bmc' ) |
| 4312 | parser_BMCReset.add_argument('type', choices=['warm','cold'], help="Warm: Reboot the BMC, Cold: CLEAR config and reboot bmc") |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4313 | parser_bmc.add_argument('info', action='store_true', help="Displays information about the BMC hardware, including device revision, firmware revision, IPMI version supported, manufacturer ID, and information on additional device support.") |
| 4314 | parser_bmc.set_defaults(func=bmc) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4315 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4316 | #add alias to the bmc command |
| 4317 | parser_mc = subparsers.add_parser('mc', help="Work with the management controller") |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4318 | mc_sub = parser_mc.add_subparsers(title='subcommands', description='valid subcommands',help="sub-command help", dest='command') |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4319 | parser_MCReset = mc_sub.add_parser('reset', help='Reset the bmc' ) |
| 4320 | parser_MCReset.add_argument('type', choices=['warm','cold'], help="Reboot the BMC") |
| 4321 | #parser_MCReset.add_argument('cold', action='store_true', help="Reboot the BMC and CLEAR the configuration") |
| 4322 | parser_mc.add_argument('info', action='store_true', help="Displays information about the BMC hardware, including device revision, firmware revision, IPMI version supported, manufacturer ID, and information on additional device support.") |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4323 | parser_MCReset.set_defaults(func=bmcReset) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4324 | parser_mc.set_defaults(func=bmc) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4325 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4326 | #gard clear |
| 4327 | parser_gc = subparsers.add_parser("gardclear", help="Used to clear gard records") |
| 4328 | parser_gc.set_defaults(func=gardClear) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4329 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4330 | #firmware_flash |
| 4331 | parser_fw = subparsers.add_parser("firmware", help="Work with the system firmware") |
| 4332 | fwflash_subproc = parser_fw.add_subparsers(title='subcommands', description='valid firmware commands', help='sub-command help', dest='command') |
Justin Thaler | 53bf2f1 | 2018-07-16 14:05:32 -0500 | [diff] [blame] | 4333 | fwflash_subproc.required = True |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4334 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4335 | fwflash = fwflash_subproc.add_parser('flash', help="Flash the system firmware") |
| 4336 | fwflash.add_argument('type', choices=['bmc', 'pnor'], help="image type to flash") |
| 4337 | fwflash.add_argument('-f', '--fileloc', required=True, help="The absolute path to the firmware image") |
| 4338 | fwflash.set_defaults(func=fwFlash) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4339 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 4340 | fwActivate = fwflash_subproc.add_parser('activate', help="Activate existing image on the bmc") |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4341 | fwActivate.add_argument('imageID', help="The image ID to activate from the firmware list. Ex: 63c95399") |
| 4342 | fwActivate.set_defaults(func=activateFWImage) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4343 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 4344 | fwActivateStatus = fwflash_subproc.add_parser('activation_status', help="Check Status of activations") |
| 4345 | fwActivateStatus.set_defaults(func=activateStatus) |
| 4346 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 4347 | fwList = fwflash_subproc.add_parser('list', help="List all of the installed firmware") |
| 4348 | fwList.add_argument('-v', '--verbose', action='store_true', help='Verbose output') |
| 4349 | fwList.set_defaults(func=firmwareList) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4350 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 4351 | fwprint = fwflash_subproc.add_parser('print', help="List all of the installed firmware") |
| 4352 | fwprint.add_argument('-v', '--verbose', action='store_true', help='Verbose output') |
| 4353 | fwprint.set_defaults(func=firmwareList) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4354 | |
Adriana Kobylak | 5af2fad | 2018-11-08 12:33:43 -0600 | [diff] [blame] | 4355 | fwDelete = fwflash_subproc.add_parser('delete', help="Delete an existing firmware version") |
| 4356 | fwDelete.add_argument('versionID', help="The version ID to delete from the firmware list. Ex: 63c95399") |
| 4357 | fwDelete.set_defaults(func=deleteFWVersion) |
| 4358 | |
Deepak Kodihalli | 22d4df0 | 2018-09-18 06:52:43 -0500 | [diff] [blame] | 4359 | #logging |
| 4360 | parser_logging = subparsers.add_parser("logging", help="logging controls") |
| 4361 | logging_sub = parser_logging.add_subparsers(title='subcommands', description='valid subcommands',help="sub-command help", dest='command') |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4362 | |
Deepak Kodihalli | 22d4df0 | 2018-09-18 06:52:43 -0500 | [diff] [blame] | 4363 | #turn rest api logging on/off |
| 4364 | parser_rest_logging = logging_sub.add_parser("rest_api", help="turn rest api logging on/off") |
| 4365 | parser_rest_logging.add_argument('rest_logging', choices=['on', 'off'], help='The control option for rest logging: on, off') |
| 4366 | parser_rest_logging.set_defaults(func=restLogging) |
Deepak Kodihalli | 02d5328 | 2018-09-18 06:53:31 -0500 | [diff] [blame] | 4367 | |
| 4368 | #remote logging |
| 4369 | parser_remote_logging = logging_sub.add_parser("remote_logging", help="Remote logging (rsyslog) commands") |
| 4370 | parser_remote_logging.add_argument('remote_logging', choices=['view', 'disable'], help='Remote logging (rsyslog) commands') |
| 4371 | parser_remote_logging.set_defaults(func=remoteLogging) |
| 4372 | |
| 4373 | #configure remote logging |
| 4374 | parser_remote_logging_config = logging_sub.add_parser("remote_logging_config", help="Configure remote logging (rsyslog)") |
| 4375 | parser_remote_logging_config.add_argument("-a", "--address", required=True, help="Set IP address of rsyslog server") |
| 4376 | parser_remote_logging_config.add_argument("-p", "--port", required=True, type=int, help="Set Port of rsyslog server") |
| 4377 | parser_remote_logging_config.set_defaults(func=remoteLoggingConfig) |
Dhruvaraj Subhashchandran | 64e7f6f | 2018-10-02 03:42:14 -0500 | [diff] [blame] | 4378 | |
| 4379 | #certificate management |
| 4380 | parser_cert = subparsers.add_parser("certificate", help="Certificate management") |
| 4381 | certMgmt_subproc = parser_cert.add_subparsers(title='subcommands', description='valid certificate commands', help='sub-command help', dest='command') |
| 4382 | |
| 4383 | certUpdate = certMgmt_subproc.add_parser('update', help="Update the certificate") |
| 4384 | certUpdate.add_argument('type', choices=['server', 'client', 'authority'], help="certificate type to update") |
| 4385 | certUpdate.add_argument('service', choices=['https', 'ldap'], help="Service to update") |
| 4386 | certUpdate.add_argument('-f', '--fileloc', required=True, help="The absolute path to the certificate file") |
| 4387 | certUpdate.set_defaults(func=certificateUpdate) |
| 4388 | |
| 4389 | certDelete = certMgmt_subproc.add_parser('delete', help="Delete the certificate") |
| 4390 | certDelete.add_argument('type', choices=['server', 'client', 'authority'], help="certificate type to delete") |
| 4391 | certDelete.add_argument('service', choices=['https', 'ldap'], help="Service to delete the certificate") |
| 4392 | certDelete.set_defaults(func=certificateDelete) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4393 | |
Marri Devender Rao | dfe81ad | 2019-07-01 05:38:09 -0500 | [diff] [blame] | 4394 | certReplace = certMgmt_subproc.add_parser('replace', |
| 4395 | help="Replace the certificate") |
| 4396 | certReplace.add_argument('type', choices=['server', 'client', 'authority'], |
| 4397 | help="certificate type to replace") |
| 4398 | certReplace.add_argument('service', choices=['https', 'ldap'], |
| 4399 | help="Service to replace the certificate") |
| 4400 | certReplace.add_argument('-f', '--fileloc', required=True, |
| 4401 | help="The absolute path to the certificate file") |
| 4402 | certReplace.set_defaults(func=certificateReplace) |
| 4403 | |
Marri Devender Rao | 3464640 | 2019-07-01 05:46:03 -0500 | [diff] [blame] | 4404 | certDisplay = certMgmt_subproc.add_parser('display', |
| 4405 | help="Print the certificate") |
| 4406 | certDisplay.add_argument('type', choices=['server', 'client', 'authority'], |
| 4407 | help="certificate type to display") |
| 4408 | certDisplay.set_defaults(func=certificateDisplay) |
| 4409 | |
Marri Devender Rao | a208ff8 | 2019-07-01 05:51:27 -0500 | [diff] [blame] | 4410 | certList = certMgmt_subproc.add_parser('list', |
| 4411 | help="Certificate list") |
| 4412 | certList.set_defaults(func=certificateList) |
| 4413 | |
Marri Devender Rao | 3cdf8ae | 2019-07-01 06:01:40 -0500 | [diff] [blame] | 4414 | certGenerateCSR = certMgmt_subproc.add_parser('generatecsr', help="Generate CSR") |
| 4415 | certGenerateCSR.add_argument('type', choices=['server', 'client', 'authority'], |
| 4416 | help="Generate CSR") |
| 4417 | certGenerateCSR.add_argument('city', |
| 4418 | help="The city or locality of the organization making the request") |
| 4419 | certGenerateCSR.add_argument('commonName', |
| 4420 | help="The fully qualified domain name of the component that is being secured.") |
| 4421 | certGenerateCSR.add_argument('country', |
| 4422 | help="The country of the organization making the request") |
| 4423 | certGenerateCSR.add_argument('organization', |
| 4424 | help="The name of the organization making the request.") |
| 4425 | certGenerateCSR.add_argument('organizationUnit', |
| 4426 | help="The name of the unit or division of the organization making the request.") |
| 4427 | certGenerateCSR.add_argument('state', |
| 4428 | help="The state, province, or region of the organization making the request.") |
| 4429 | certGenerateCSR.add_argument('keyPairAlgorithm', choices=['RSA', 'EC'], |
| 4430 | help="The type of key pair for use with signing algorithms.") |
Marri Devender Rao | 3cdf8ae | 2019-07-01 06:01:40 -0500 | [diff] [blame] | 4431 | certGenerateCSR.add_argument('keyCurveId', |
| 4432 | help="The curve ID to be used with the key, if needed based on the value of the 'KeyPairAlgorithm' parameter.") |
| 4433 | certGenerateCSR.add_argument('contactPerson', |
| 4434 | help="The name of the user making the request") |
| 4435 | certGenerateCSR.add_argument('email', |
| 4436 | help="The email address of the contact within the organization") |
| 4437 | certGenerateCSR.add_argument('alternativeNames', |
| 4438 | help="Additional hostnames of the component that is being secured") |
| 4439 | certGenerateCSR.add_argument('givenname', |
| 4440 | help="The given name of the user making the request") |
| 4441 | certGenerateCSR.add_argument('surname', |
| 4442 | help="The surname of the user making the request") |
| 4443 | certGenerateCSR.add_argument('unstructuredname', |
| 4444 | help="he unstructured name of the subject") |
| 4445 | certGenerateCSR.add_argument('initials', |
| 4446 | help="The initials of the user making the request") |
Marri Devender Rao | 3cdf8ae | 2019-07-01 06:01:40 -0500 | [diff] [blame] | 4447 | certGenerateCSR.set_defaults(func=certificateGenerateCSR) |
| 4448 | |
Matt Spinler | 7d426c2 | 2018-09-24 14:42:07 -0500 | [diff] [blame] | 4449 | # local users |
| 4450 | parser_users = subparsers.add_parser("local_users", help="Work with local users") |
| 4451 | parser_users.add_argument('local_users', choices=['disableall','enableall', 'queryenabled'], help="Disable, enable or query local user accounts") |
| 4452 | parser_users.add_argument('-v', '--verbose', action='store_true', help='Verbose output') |
| 4453 | parser_users.set_defaults(func=localUsers) |
| 4454 | |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 4455 | #LDAP |
| 4456 | parser_ldap = subparsers.add_parser("ldap", help="LDAP controls") |
| 4457 | ldap_sub = parser_ldap.add_subparsers(title='subcommands', description='valid subcommands',help="sub-command help", dest='command') |
| 4458 | |
| 4459 | #configure and enable LDAP |
| 4460 | parser_ldap_config = ldap_sub.add_parser("enable", help="Configure and enables the LDAP") |
| 4461 | parser_ldap_config.add_argument("-a", "--uri", required=True, help="Set LDAP server URI") |
| 4462 | parser_ldap_config.add_argument("-B", "--bindDN", required=True, help="Set the bind DN of the LDAP server") |
| 4463 | parser_ldap_config.add_argument("-b", "--baseDN", required=True, help="Set the base DN of the LDAP server") |
| 4464 | parser_ldap_config.add_argument("-p", "--bindPassword", required=True, help="Set the bind password of the LDAP server") |
| 4465 | parser_ldap_config.add_argument("-S", "--scope", choices=['sub','one', 'base'], |
| 4466 | help='Specifies the search scope:subtree, one level or base object.') |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 4467 | parser_ldap_config.add_argument("-t", "--serverType", required=True, choices=['ActiveDirectory','OpenLDAP'], |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 4468 | help='Specifies the configured server is ActiveDirectory(AD) or OpenLdap') |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 4469 | parser_ldap_config.add_argument("-g","--groupAttrName", required=False, default='', help="Group Attribute Name") |
| 4470 | parser_ldap_config.add_argument("-u","--userAttrName", required=False, default='', help="User Attribute Name") |
| 4471 | parser_ldap_config.set_defaults(func=enableLDAPConfig) |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 4472 | |
| 4473 | # disable LDAP |
| 4474 | parser_disable_ldap = ldap_sub.add_parser("disable", help="disables the LDAP") |
| 4475 | parser_disable_ldap.set_defaults(func=disableLDAP) |
Nagaraju Goruganti | 7d1fe17 | 2018-11-13 06:09:29 -0600 | [diff] [blame] | 4476 | # view-config |
| 4477 | parser_ldap_config = \ |
| 4478 | ldap_sub.add_parser("view-config", help="prints out a list of all \ |
| 4479 | LDAPS's configured properties") |
| 4480 | parser_ldap_config.set_defaults(func=viewLDAPConfig) |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 4481 | |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 4482 | #create group privilege mapping |
| 4483 | parser_ldap_mapper = ldap_sub.add_parser("privilege-mapper", help="LDAP group privilege controls") |
| 4484 | parser_ldap_mapper_sub = parser_ldap_mapper.add_subparsers(title='subcommands', description='valid subcommands', |
| 4485 | help="sub-command help", dest='command') |
| 4486 | |
| 4487 | parser_ldap_mapper_create = parser_ldap_mapper_sub.add_parser("create", help="Create mapping of ldap group and privilege") |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 4488 | parser_ldap_mapper_create.add_argument("-t", "--serverType", choices=['ActiveDirectory','OpenLDAP'], |
| 4489 | help='Specifies the configured server is ActiveDirectory(AD) or OpenLdap') |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 4490 | parser_ldap_mapper_create.add_argument("-g","--groupName",required=True,help="Group Name") |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 4491 | parser_ldap_mapper_create.add_argument("-p","--privilege",choices=['priv-admin','priv-operator','priv-user','priv-callback'],required=True,help="Privilege") |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 4492 | parser_ldap_mapper_create.set_defaults(func=createPrivilegeMapping) |
| 4493 | |
| 4494 | #list group privilege mapping |
| 4495 | parser_ldap_mapper_list = parser_ldap_mapper_sub.add_parser("list",help="List privilege mapping") |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 4496 | parser_ldap_mapper_list.add_argument("-t", "--serverType", choices=['ActiveDirectory','OpenLDAP'], |
| 4497 | help='Specifies the configured server is ActiveDirectory(AD) or OpenLdap') |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 4498 | parser_ldap_mapper_list.set_defaults(func=listPrivilegeMapping) |
| 4499 | |
| 4500 | #delete group privilege mapping |
| 4501 | parser_ldap_mapper_delete = parser_ldap_mapper_sub.add_parser("delete",help="Delete privilege mapping") |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 4502 | parser_ldap_mapper_delete.add_argument("-t", "--serverType", choices=['ActiveDirectory','OpenLDAP'], |
| 4503 | help='Specifies the configured server is ActiveDirectory(AD) or OpenLdap') |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 4504 | parser_ldap_mapper_delete.add_argument("-g","--groupName",required=True,help="Group Name") |
| 4505 | parser_ldap_mapper_delete.set_defaults(func=deletePrivilegeMapping) |
| 4506 | |
Sivas SRR | 7883527 | 2018-11-27 05:27:19 -0600 | [diff] [blame] | 4507 | #deleteAll group privilege mapping |
| 4508 | parser_ldap_mapper_delete = parser_ldap_mapper_sub.add_parser("purge",help="Delete All privilege mapping") |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 4509 | parser_ldap_mapper_delete.add_argument("-t", "--serverType", choices=['ActiveDirectory','OpenLDAP'], |
| 4510 | help='Specifies the configured server is ActiveDirectory(AD) or OpenLdap') |
Sivas SRR | 7883527 | 2018-11-27 05:27:19 -0600 | [diff] [blame] | 4511 | parser_ldap_mapper_delete.set_defaults(func=deleteAllPrivilegeMapping) |
| 4512 | |
Marri Devender Rao | 2c2a516 | 2018-11-05 08:57:11 -0600 | [diff] [blame] | 4513 | # set local user password |
| 4514 | parser_set_password = subparsers.add_parser("set_password", |
| 4515 | help="Set password of local user") |
| 4516 | parser_set_password.add_argument( "-p", "--password", required=True, |
| 4517 | help="Password of local user") |
| 4518 | parser_set_password.set_defaults(func=setPassword) |
| 4519 | |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 4520 | # network |
| 4521 | parser_nw = subparsers.add_parser("network", help="network controls") |
| 4522 | nw_sub = parser_nw.add_subparsers(title='subcommands', |
| 4523 | description='valid subcommands', |
| 4524 | help="sub-command help", |
| 4525 | dest='command') |
| 4526 | |
| 4527 | # enable DHCP |
| 4528 | parser_enable_dhcp = nw_sub.add_parser("enableDHCP", |
| 4529 | help="enables the DHCP on given " |
| 4530 | "Interface") |
| 4531 | parser_enable_dhcp.add_argument("-I", "--Interface", required=True, |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 4532 | help="Name of the ethernet interface(it can" |
| 4533 | "be obtained by the " |
| 4534 | "command:network view-config)" |
| 4535 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 4536 | parser_enable_dhcp.set_defaults(func=enableDHCP) |
| 4537 | |
| 4538 | # disable DHCP |
| 4539 | parser_disable_dhcp = nw_sub.add_parser("disableDHCP", |
| 4540 | help="disables the DHCP on given " |
| 4541 | "Interface") |
| 4542 | parser_disable_dhcp.add_argument("-I", "--Interface", required=True, |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 4543 | help="Name of the ethernet interface(it can" |
| 4544 | "be obtained by the " |
| 4545 | "command:network view-config)" |
| 4546 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 4547 | parser_disable_dhcp.set_defaults(func=disableDHCP) |
| 4548 | |
| 4549 | # get HostName |
| 4550 | parser_gethostname = nw_sub.add_parser("getHostName", |
| 4551 | help="prints out HostName") |
| 4552 | parser_gethostname.set_defaults(func=getHostname) |
| 4553 | |
| 4554 | # set HostName |
| 4555 | parser_sethostname = nw_sub.add_parser("setHostName", help="sets HostName") |
| 4556 | parser_sethostname.add_argument("-H", "--HostName", required=True, |
| 4557 | help="A HostName for the BMC") |
| 4558 | parser_sethostname.set_defaults(func=setHostname) |
| 4559 | |
| 4560 | # get domainname |
| 4561 | parser_getdomainname = nw_sub.add_parser("getDomainName", |
| 4562 | help="prints out DomainName of " |
| 4563 | "given Interface") |
| 4564 | parser_getdomainname.add_argument("-I", "--Interface", required=True, |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 4565 | help="Name of the ethernet interface(it " |
| 4566 | "can be obtained by the " |
| 4567 | "command:network view-config)" |
| 4568 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 4569 | parser_getdomainname.set_defaults(func=getDomainName) |
| 4570 | |
| 4571 | # set domainname |
| 4572 | parser_setdomainname = nw_sub.add_parser("setDomainName", |
| 4573 | help="sets DomainName of given " |
| 4574 | "Interface") |
| 4575 | parser_setdomainname.add_argument("-D", "--DomainName", required=True, |
| 4576 | help="Ex: DomainName=Domain1,Domain2,...") |
| 4577 | parser_setdomainname.add_argument("-I", "--Interface", required=True, |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 4578 | help="Name of the ethernet interface(it " |
| 4579 | "can be obtained by the " |
| 4580 | "command:network view-config)" |
| 4581 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 4582 | parser_setdomainname.set_defaults(func=setDomainName) |
| 4583 | |
| 4584 | # get MACAddress |
| 4585 | parser_getmacaddress = nw_sub.add_parser("getMACAddress", |
| 4586 | help="prints out MACAddress the " |
| 4587 | "given Interface") |
| 4588 | parser_getmacaddress.add_argument("-I", "--Interface", required=True, |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 4589 | help="Name of the ethernet interface(it " |
| 4590 | "can be obtained by the " |
| 4591 | "command:network view-config)" |
| 4592 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 4593 | parser_getmacaddress.set_defaults(func=getMACAddress) |
| 4594 | |
| 4595 | # set MACAddress |
| 4596 | parser_setmacaddress = nw_sub.add_parser("setMACAddress", |
| 4597 | help="sets MACAddress") |
| 4598 | parser_setmacaddress.add_argument("-MA", "--MACAddress", required=True, |
| 4599 | help="A MACAddress for the given " |
| 4600 | "Interface") |
| 4601 | parser_setmacaddress.add_argument("-I", "--Interface", required=True, |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 4602 | help="Name of the ethernet interface(it can" |
| 4603 | "be obtained by the " |
| 4604 | "command:network view-config)" |
| 4605 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 4606 | parser_setmacaddress.set_defaults(func=setMACAddress) |
| 4607 | |
| 4608 | # get DefaultGW |
| 4609 | parser_getdefaultgw = nw_sub.add_parser("getDefaultGW", |
| 4610 | help="prints out DefaultGateway " |
| 4611 | "the BMC") |
| 4612 | parser_getdefaultgw.set_defaults(func=getDefaultGateway) |
| 4613 | |
| 4614 | # set DefaultGW |
| 4615 | parser_setdefaultgw = nw_sub.add_parser("setDefaultGW", |
| 4616 | help="sets DefaultGW") |
| 4617 | parser_setdefaultgw.add_argument("-GW", "--DefaultGW", required=True, |
| 4618 | help="A DefaultGateway for the BMC") |
| 4619 | parser_setdefaultgw.set_defaults(func=setDefaultGateway) |
| 4620 | |
| 4621 | # view network Config |
| 4622 | parser_ldap_config = nw_sub.add_parser("view-config", help="prints out a " |
| 4623 | "list of all network's configured " |
| 4624 | "properties") |
| 4625 | parser_ldap_config.set_defaults(func=viewNWConfig) |
| 4626 | |
| 4627 | # get DNS |
| 4628 | parser_getDNS = nw_sub.add_parser("getDNS", |
| 4629 | help="prints out DNS servers on the " |
| 4630 | "given interface") |
| 4631 | parser_getDNS.add_argument("-I", "--Interface", required=True, |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 4632 | help="Name of the ethernet interface(it can" |
| 4633 | "be obtained by the " |
| 4634 | "command:network view-config)" |
| 4635 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 4636 | parser_getDNS.set_defaults(func=getDNS) |
| 4637 | |
| 4638 | # set DNS |
| 4639 | parser_setDNS = nw_sub.add_parser("setDNS", |
| 4640 | help="sets DNS servers on the given " |
| 4641 | "interface") |
| 4642 | parser_setDNS.add_argument("-d", "--DNSServers", required=True, |
| 4643 | help="Ex: DNSSERVERS=DNS1,DNS2,...") |
| 4644 | parser_setDNS.add_argument("-I", "--Interface", required=True, |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 4645 | help="Name of the ethernet interface(it can" |
| 4646 | "be obtained by the " |
| 4647 | "command:network view-config)" |
| 4648 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 4649 | parser_setDNS.set_defaults(func=setDNS) |
| 4650 | |
| 4651 | # get NTP |
| 4652 | parser_getNTP = nw_sub.add_parser("getNTP", |
| 4653 | help="prints out NTP servers on the " |
| 4654 | "given interface") |
| 4655 | parser_getNTP.add_argument("-I", "--Interface", required=True, |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 4656 | help="Name of the ethernet interface(it can" |
| 4657 | "be obtained by the " |
| 4658 | "command:network view-config)" |
| 4659 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 4660 | parser_getNTP.set_defaults(func=getNTP) |
| 4661 | |
| 4662 | # set NTP |
| 4663 | parser_setNTP = nw_sub.add_parser("setNTP", |
| 4664 | help="sets NTP servers on the given " |
| 4665 | "interface") |
| 4666 | parser_setNTP.add_argument("-N", "--NTPServers", required=True, |
| 4667 | help="Ex: NTPSERVERS=NTP1,NTP2,...") |
| 4668 | parser_setNTP.add_argument("-I", "--Interface", required=True, |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 4669 | help="Name of the ethernet interface(it can" |
| 4670 | "be obtained by the " |
| 4671 | "command:network view-config)" |
| 4672 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 4673 | parser_setNTP.set_defaults(func=setNTP) |
| 4674 | |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 4675 | # configure IP |
| 4676 | parser_ip_config = nw_sub.add_parser("addIP", help="Sets IP address to" |
| 4677 | "given interface") |
| 4678 | parser_ip_config.add_argument("-a", "--address", required=True, |
| 4679 | help="IP address of given interface") |
| 4680 | parser_ip_config.add_argument("-gw", "--gateway", required=False, default='', |
| 4681 | help="The gateway for given interface") |
| 4682 | parser_ip_config.add_argument("-l", "--prefixLength", required=True, |
| 4683 | help="The prefixLength of IP address") |
Sunitha Harish | 0baf637 | 2019-07-31 03:59:03 -0500 | [diff] [blame] | 4684 | parser_ip_config.add_argument("-p", "--type", required=True, |
| 4685 | choices=['ipv4', 'ipv6'], |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 4686 | help="The protocol type of the given" |
| 4687 | "IP address") |
| 4688 | parser_ip_config.add_argument("-I", "--Interface", required=True, |
| 4689 | help="Name of the ethernet interface(it can" |
| 4690 | "be obtained by the " |
| 4691 | "command:network view-config)" |
| 4692 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
| 4693 | parser_ip_config.set_defaults(func=addIP) |
| 4694 | |
| 4695 | # getIP |
| 4696 | parser_getIP = nw_sub.add_parser("getIP", help="prints out IP address" |
| 4697 | "of given interface") |
| 4698 | parser_getIP.add_argument("-I", "--Interface", required=True, |
| 4699 | help="Name of the ethernet interface(it can" |
| 4700 | "be obtained by the command:network view-config)" |
| 4701 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
| 4702 | parser_getIP.set_defaults(func=getIP) |
| 4703 | |
| 4704 | # rmIP |
| 4705 | parser_rmIP = nw_sub.add_parser("rmIP", help="deletes IP address" |
| 4706 | "of given interface") |
| 4707 | parser_rmIP.add_argument("-a", "--address", required=True, |
| 4708 | help="IP address to remove form given Interface") |
| 4709 | parser_rmIP.add_argument("-I", "--Interface", required=True, |
| 4710 | help="Name of the ethernet interface(it can" |
| 4711 | "be obtained by the command:network view-config)" |
| 4712 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
| 4713 | parser_rmIP.set_defaults(func=deleteIP) |
| 4714 | |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 4715 | # add VLAN |
| 4716 | parser_create_vlan = nw_sub.add_parser("addVLAN", help="enables VLAN " |
| 4717 | "on given interface with given " |
| 4718 | "VLAN Identifier") |
| 4719 | parser_create_vlan.add_argument("-I", "--Interface", required=True, |
| 4720 | choices=['eth0', 'eth1'], |
| 4721 | help="Name of the ethernet interface") |
| 4722 | parser_create_vlan.add_argument("-n", "--Identifier", required=True, |
| 4723 | help="VLAN Identifier") |
| 4724 | parser_create_vlan.set_defaults(func=addVLAN) |
| 4725 | |
| 4726 | # delete VLAN |
| 4727 | parser_delete_vlan = nw_sub.add_parser("deleteVLAN", help="disables VLAN " |
| 4728 | "on given interface with given " |
| 4729 | "VLAN Identifier") |
| 4730 | parser_delete_vlan.add_argument("-I", "--Interface", required=True, |
| 4731 | help="Name of the ethernet interface(it can" |
| 4732 | "be obtained by the " |
| 4733 | "command:network view-config)" |
| 4734 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
| 4735 | parser_delete_vlan.set_defaults(func=deleteVLAN) |
| 4736 | |
| 4737 | # viewDHCPConfig |
| 4738 | parser_viewDHCPConfig = nw_sub.add_parser("viewDHCPConfig", |
| 4739 | help="Shows DHCP configured " |
| 4740 | "Properties") |
| 4741 | parser_viewDHCPConfig.set_defaults(func=viewDHCPConfig) |
| 4742 | |
| 4743 | # configureDHCP |
| 4744 | parser_configDHCP = nw_sub.add_parser("configureDHCP", |
| 4745 | help="Configures/updates DHCP " |
| 4746 | "Properties") |
| 4747 | parser_configDHCP.add_argument("-d", "--DNSEnabled", type=str2bool, |
| 4748 | required=True, help="Sets DNSEnabled property") |
| 4749 | parser_configDHCP.add_argument("-n", "--HostNameEnabled", type=str2bool, |
| 4750 | required=True, |
| 4751 | help="Sets HostNameEnabled property") |
| 4752 | parser_configDHCP.add_argument("-t", "--NTPEnabled", type=str2bool, |
| 4753 | required=True, |
| 4754 | help="Sets NTPEnabled property") |
| 4755 | parser_configDHCP.add_argument("-s", "--SendHostNameEnabled", type=str2bool, |
| 4756 | required=True, |
| 4757 | help="Sets SendHostNameEnabled property") |
| 4758 | parser_configDHCP.set_defaults(func=configureDHCP) |
| 4759 | |
| 4760 | # network factory reset |
| 4761 | parser_nw_reset = nw_sub.add_parser("nwReset", |
| 4762 | help="Resets networks setting to " |
| 4763 | "factory defaults. " |
| 4764 | "note:Reset settings will be applied " |
| 4765 | "after BMC reboot") |
| 4766 | parser_nw_reset.set_defaults(func=nwReset) |
| 4767 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4768 | return parser |
| 4769 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4770 | def main(argv=None): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4771 | """ |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4772 | main function for running the command line utility as a sub application |
| 4773 | """ |
| 4774 | global toolVersion |
Marri Devender Rao | 82590dc | 2019-06-06 04:54:22 -0500 | [diff] [blame] | 4775 | toolVersion = "1.15" |
Sunitha Harish | c99faba | 2019-07-19 06:55:22 -0500 | [diff] [blame] | 4776 | global isRedfishSupport |
RAJESWARAN THILLAIGOVINDAN | 87f087b | 2019-05-08 04:15:26 -0500 | [diff] [blame] | 4777 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4778 | parser = createCommandParser() |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4779 | args = parser.parse_args(argv) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4780 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4781 | totTimeStart = int(round(time.time()*1000)) |
| 4782 | |
| 4783 | if(sys.version_info < (3,0)): |
| 4784 | urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) |
| 4785 | if sys.version_info >= (3,0): |
| 4786 | requests.packages.urllib3.disable_warnings(requests.packages.urllib3.exceptions.InsecureRequestWarning) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4787 | if (args.version): |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 4788 | print("Version: "+ toolVersion) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4789 | sys.exit(0) |
| 4790 | if (hasattr(args, 'fileloc') and args.fileloc is not None and 'print' in args.command): |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4791 | mysess = None |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4792 | print(selPrint('N/A', args, mysess)) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4793 | else: |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4794 | if(hasattr(args, 'host') and hasattr(args,'user')): |
| 4795 | if (args.askpw): |
| 4796 | pw = getpass.getpass() |
| 4797 | elif(args.PW is not None): |
| 4798 | pw = args.PW |
Joseph Reynolds | a2d54c5 | 2019-06-11 22:02:57 -0500 | [diff] [blame] | 4799 | elif(args.PWenvvar): |
| 4800 | pw = os.environ['OPENBMCTOOL_PASSWORD'] |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4801 | else: |
| 4802 | print("You must specify a password") |
| 4803 | sys.exit() |
| 4804 | logintimeStart = int(round(time.time()*1000)) |
| 4805 | mysess = login(args.host, args.user, pw, args.json) |
Sunitha Harish | 336cda2 | 2019-07-23 02:02:52 -0500 | [diff] [blame] | 4806 | if(mysess == None): |
| 4807 | print("Login Failed!") |
| 4808 | sys.exit() |
Justin Thaler | a9415b4 | 2018-05-25 19:40:13 -0500 | [diff] [blame] | 4809 | if(sys.version_info < (3,0)): |
| 4810 | if isinstance(mysess, basestring): |
| 4811 | print(mysess) |
| 4812 | sys.exit(1) |
| 4813 | elif sys.version_info >= (3,0): |
| 4814 | if isinstance(mysess, str): |
| 4815 | print(mysess) |
| 4816 | sys.exit(1) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4817 | logintimeStop = int(round(time.time()*1000)) |
Sunitha Harish | c99faba | 2019-07-19 06:55:22 -0500 | [diff] [blame] | 4818 | isRedfishSupport = redfishSupportPresent(args.host,mysess) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4819 | commandTimeStart = int(round(time.time()*1000)) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4820 | output = args.func(args.host, args, mysess) |
| 4821 | commandTimeStop = int(round(time.time()*1000)) |
Justin Thaler | 761484a | 2019-03-26 19:20:23 -0500 | [diff] [blame] | 4822 | if isinstance(output, dict): |
| 4823 | print(json.dumps(output, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False)) |
| 4824 | else: |
| 4825 | print(output) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4826 | if (mysess is not None): |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4827 | logout(args.host, args.user, pw, mysess, args.json) |
| 4828 | if(args.procTime): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4829 | print("Total time: " + str(int(round(time.time()*1000))- totTimeStart)) |
| 4830 | print("loginTime: " + str(logintimeStop - logintimeStart)) |
| 4831 | print("command Time: " + str(commandTimeStop - commandTimeStart)) |
| 4832 | else: |
Joseph Reynolds | a2d54c5 | 2019-06-11 22:02:57 -0500 | [diff] [blame] | 4833 | print("usage:\n" |
| 4834 | " OPENBMCTOOL_PASSWORD=secret # if using -E\n" |
| 4835 | " openbmctool.py [-h] -H HOST -U USER {-A | -P PW | -E} [-j]\n" + |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4836 | "\t[-t POLICYTABLELOC] [-V]\n" + |
Deepak Kodihalli | 22d4df0 | 2018-09-18 06:52:43 -0500 | [diff] [blame] | 4837 | "\t{fru,sensors,sel,chassis,collect_service_data, \ |
| 4838 | health_check,dump,bmc,mc,gardclear,firmware,logging}\n" + |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4839 | "\t...\n" + |
| 4840 | "openbmctool.py: error: the following arguments are required: -H/--host, -U/--user") |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4841 | sys.exit() |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4842 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4843 | if __name__ == '__main__': |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 4844 | """ |
| 4845 | main function when called from the command line |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4846 | |
| 4847 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4848 | import sys |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 4849 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 4850 | isTTY = sys.stdout.isatty() |
| 4851 | assert sys.version_info >= (2,7) |
| 4852 | main() |