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 | """ |
| 3 | Copyright 2017 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 = {} |
| 36 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 37 | def hilight(textToColor, color, bold): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 38 | """ |
| 39 | Used to add highlights to various text for displaying in a terminal |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 40 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 41 | @param textToColor: string, the text to be colored |
| 42 | @param color: string, used to color the text red or green |
| 43 | @param bold: boolean, used to bold the textToColor |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 44 | @return: Buffered reader containing the modified string. |
| 45 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 46 | if(sys.platform.__contains__("win")): |
| 47 | if(color == "red"): |
| 48 | os.system('color 04') |
| 49 | elif(color == "green"): |
| 50 | os.system('color 02') |
| 51 | else: |
| 52 | os.system('color') #reset to default |
| 53 | return textToColor |
| 54 | else: |
| 55 | attr = [] |
| 56 | if(color == "red"): |
| 57 | attr.append('31') |
| 58 | elif(color == "green"): |
| 59 | attr.append('32') |
| 60 | else: |
| 61 | attr.append('0') |
| 62 | if bold: |
| 63 | attr.append('1') |
| 64 | else: |
| 65 | attr.append('0') |
| 66 | return '\x1b[%sm%s\x1b[0m' % (';'.join(attr),textToColor) |
| 67 | |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 68 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 69 | def connectionErrHandler(jsonFormat, errorStr, err): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 70 | """ |
| 71 | Error handler various connection errors to bmcs |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 72 | |
| 73 | @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] | 74 | @param errorStr: string, used to color the text red or green |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 75 | @param err: string, the text from the exception |
| 76 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 77 | if errorStr == "Timeout": |
| 78 | if not jsonFormat: |
| 79 | return("FQPSPIN0000M: Connection timed out. Ensure you have network connectivity to the bmc") |
| 80 | else: |
Justin Thaler | 115bca7 | 2018-05-25 19:29:08 -0500 | [diff] [blame] | 81 | conerror = {} |
| 82 | conerror['CommonEventID'] = 'FQPSPIN0000M' |
| 83 | conerror['sensor']="N/A" |
| 84 | conerror['state']="N/A" |
| 85 | conerror['additionalDetails'] = "N/A" |
| 86 | conerror['Message']="Connection timed out. Ensure you have network connectivity to the BMC" |
| 87 | 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." |
| 88 | conerror['Serviceable']="Yes" |
| 89 | conerror['CallHomeCandidate']= "No" |
| 90 | conerror['Severity'] = "Critical" |
| 91 | conerror['EventType'] = "Communication Failure/Timeout" |
| 92 | conerror['VMMigrationFlag'] = "Yes" |
| 93 | conerror["AffectedSubsystem"] = "Interconnect (Networking)" |
| 94 | conerror["timestamp"] = str(int(time.time())) |
| 95 | conerror["UserAction"] = "Verify network connectivity between the two systems and the bmc is functional." |
| 96 | eventdict = {} |
| 97 | eventdict['event0'] = conerror |
| 98 | eventdict['numAlerts'] = '1' |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 99 | |
Justin Thaler | 115bca7 | 2018-05-25 19:29:08 -0500 | [diff] [blame] | 100 | 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] | 101 | return(errorMessageStr) |
| 102 | elif errorStr == "ConnectionError": |
| 103 | if not jsonFormat: |
| 104 | return("FQPSPIN0001M: " + str(err)) |
| 105 | else: |
Justin Thaler | 115bca7 | 2018-05-25 19:29:08 -0500 | [diff] [blame] | 106 | conerror = {} |
| 107 | conerror['CommonEventID'] = 'FQPSPIN0001M' |
| 108 | conerror['sensor']="N/A" |
| 109 | conerror['state']="N/A" |
| 110 | conerror['additionalDetails'] = str(err) |
| 111 | conerror['Message']="Connection Error. View additional details for more information" |
| 112 | conerror['LengthyDescription'] = "A connection error to the specified BMC occurred and additional details are provided. Review these details to resolve the issue." |
| 113 | conerror['Serviceable']="Yes" |
| 114 | conerror['CallHomeCandidate']= "No" |
| 115 | conerror['Severity'] = "Critical" |
| 116 | conerror['EventType'] = "Communication Failure/Timeout" |
| 117 | conerror['VMMigrationFlag'] = "Yes" |
| 118 | conerror["AffectedSubsystem"] = "Interconnect (Networking)" |
| 119 | conerror["timestamp"] = str(int(time.time())) |
| 120 | conerror["UserAction"] = "Correct the issue highlighted in additional details and try again" |
| 121 | eventdict = {} |
| 122 | eventdict['event0'] = conerror |
| 123 | eventdict['numAlerts'] = '1' |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 124 | |
Justin Thaler | 115bca7 | 2018-05-25 19:29:08 -0500 | [diff] [blame] | 125 | 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] | 126 | return(errorMessageStr) |
Justin Thaler | 115bca7 | 2018-05-25 19:29:08 -0500 | [diff] [blame] | 127 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 128 | else: |
| 129 | return("Unknown Error: "+ str(err)) |
| 130 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 131 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 132 | def setColWidth(keylist, numCols, dictForOutput, colNames): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 133 | """ |
| 134 | Sets the output width of the columns to display |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 135 | |
| 136 | @param keylist: list, list of strings representing the keys for the dictForOutput |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 137 | @param numcols: the total number of columns in the final output |
| 138 | @param dictForOutput: dictionary, contains the information to print to the screen |
| 139 | @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] | 140 | @return: A list of the column widths for each respective column. |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 141 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 142 | colWidths = [] |
| 143 | for x in range(0, numCols): |
| 144 | colWidths.append(0) |
| 145 | for key in dictForOutput: |
| 146 | for x in range(0, numCols): |
| 147 | colWidths[x] = max(colWidths[x], len(str(dictForOutput[key][keylist[x]]))) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 148 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 149 | for x in range(0, numCols): |
| 150 | colWidths[x] = max(colWidths[x], len(colNames[x])) +2 |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 151 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 152 | return colWidths |
| 153 | |
| 154 | def loadPolicyTable(pathToPolicyTable): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 155 | """ |
| 156 | loads a json based policy table into a dictionary |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 157 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 158 | @param value: boolean, the value to convert |
| 159 | @return: A string of "Yes" or "No" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 160 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 161 | policyTable = {} |
| 162 | if(os.path.exists(pathToPolicyTable)): |
| 163 | with open(pathToPolicyTable, 'r') as stream: |
| 164 | try: |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 165 | contents =json.load(stream) |
| 166 | policyTable = contents['events'] |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 167 | except Exception as err: |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 168 | print(err) |
| 169 | return policyTable |
| 170 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 171 | |
| 172 | def boolToString(value): |
| 173 | """ |
| 174 | converts a boolean value to a human readable string value |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 175 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 176 | @param value: boolean, the value to convert |
| 177 | @return: A string of "Yes" or "No" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 178 | """ |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 179 | if(value): |
| 180 | return "Yes" |
| 181 | else: |
| 182 | return "No" |
| 183 | |
Justin Thaler | a6b5df7 | 2018-07-16 11:10:07 -0500 | [diff] [blame] | 184 | def stringToInt(text): |
| 185 | """ |
| 186 | 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] | 187 | |
Justin Thaler | a6b5df7 | 2018-07-16 11:10:07 -0500 | [diff] [blame] | 188 | @param text: the string to try to convert to an integer |
| 189 | """ |
| 190 | if text.isdigit(): |
| 191 | return int(text) |
| 192 | else: |
| 193 | return text |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 194 | |
Justin Thaler | a6b5df7 | 2018-07-16 11:10:07 -0500 | [diff] [blame] | 195 | def naturalSort(text): |
| 196 | """ |
| 197 | provides a way to naturally sort a list |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 198 | |
Justin Thaler | a6b5df7 | 2018-07-16 11:10:07 -0500 | [diff] [blame] | 199 | @param text: the key to convert for sorting |
| 200 | @return list containing the broken up string parts by integers and strings |
| 201 | """ |
| 202 | stringPartList = [] |
| 203 | for c in re.split('(\d+)', text): |
| 204 | stringPartList.append(stringToInt(c)) |
| 205 | return stringPartList |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 206 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 207 | def tableDisplay(keylist, colNames, output): |
| 208 | """ |
| 209 | Logs into the BMC and creates a session |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 210 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 211 | @param keylist: list, keys for the output dictionary, ordered by colNames |
| 212 | @param colNames: Names for the Table of the columns |
| 213 | @param output: The dictionary of data to display |
| 214 | @return: Session object |
| 215 | """ |
| 216 | colWidth = setColWidth(keylist, len(colNames), output, colNames) |
| 217 | row = "" |
| 218 | outputText = "" |
| 219 | for i in range(len(colNames)): |
| 220 | if (i != 0): row = row + "| " |
| 221 | row = row + colNames[i].ljust(colWidth[i]) |
| 222 | outputText += row + "\n" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 223 | |
Justin Thaler | a6b5df7 | 2018-07-16 11:10:07 -0500 | [diff] [blame] | 224 | output_keys = list(output.keys()) |
| 225 | output_keys.sort(key=naturalSort) |
| 226 | for key in output_keys: |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 227 | row = "" |
Justin Thaler | 8fe0c73 | 2018-07-24 14:32:35 -0500 | [diff] [blame] | 228 | for i in range(len(keylist)): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 229 | if (i != 0): row = row + "| " |
| 230 | row = row + output[key][keylist[i]].ljust(colWidth[i]) |
| 231 | outputText += row + "\n" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 232 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 233 | return outputText |
| 234 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 235 | def checkFWactivation(host, args, session): |
| 236 | """ |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 237 | Checks the software inventory for an image that is being activated. |
| 238 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 239 | @return: True if an image is being activated, false is no activations are happening |
| 240 | """ |
| 241 | url="https://"+host+"/xyz/openbmc_project/software/enumerate" |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 242 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 243 | resp = session.get(url, headers=jsonHeader, verify=False, timeout=30) |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 244 | except(requests.exceptions.Timeout): |
| 245 | print(connectionErrHandler(args.json, "Timeout", None)) |
| 246 | return(True) |
| 247 | except(requests.exceptions.ConnectionError) as err: |
| 248 | print( connectionErrHandler(args.json, "ConnectionError", err)) |
| 249 | return True |
| 250 | fwInfo = json.loads(resp.text)['data'] |
| 251 | for key in fwInfo: |
| 252 | if 'Activation' in fwInfo[key]: |
| 253 | if 'Activating' in fwInfo[key]['Activation'] or 'Activating' in fwInfo[key]['RequestedActivation']: |
| 254 | return True |
| 255 | return False |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 256 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 257 | def login(host, username, pw,jsonFormat): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 258 | """ |
| 259 | Logs into the BMC and creates a session |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 260 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 261 | @param host: string, the hostname or IP address of the bmc to log into |
| 262 | @param username: The user name for the bmc to log into |
| 263 | @param pw: The password for the BMC to log into |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 264 | @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] | 265 | @return: Session object |
| 266 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 267 | if(jsonFormat==False): |
| 268 | print("Attempting login...") |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 269 | mysess = requests.session() |
| 270 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 271 | r = mysess.post('https://'+host+'/login', headers=jsonHeader, json = {"data": [username, pw]}, verify=False, timeout=30) |
| 272 | |
| 273 | cookie = r.headers['Set-Cookie'] |
| 274 | match = re.search('SESSION=(\w+);', cookie) |
| 275 | if match: |
| 276 | xAuthHeader['X-Auth-Token'] = match.group(1) |
| 277 | jsonHeader.update(xAuthHeader) |
| 278 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 279 | loginMessage = json.loads(r.text) |
| 280 | if (loginMessage['status'] != "ok"): |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 281 | print(loginMessage["data"]["description"].encode('utf-8')) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 282 | sys.exit(1) |
| 283 | # if(sys.version_info < (3,0)): |
| 284 | # urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) |
| 285 | # if sys.version_info >= (3,0): |
| 286 | # requests.packages.urllib3.disable_warnings(requests.packages.urllib3.exceptions.InsecureRequestWarning) |
| 287 | return mysess |
| 288 | except(requests.exceptions.Timeout): |
Justin Thaler | 115bca7 | 2018-05-25 19:29:08 -0500 | [diff] [blame] | 289 | return (connectionErrHandler(jsonFormat, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 290 | except(requests.exceptions.ConnectionError) as err: |
Justin Thaler | 115bca7 | 2018-05-25 19:29:08 -0500 | [diff] [blame] | 291 | return (connectionErrHandler(jsonFormat, "ConnectionError", err)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 292 | |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 293 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 294 | def logout(host, username, pw, session, jsonFormat): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 295 | """ |
| 296 | Logs out of the bmc and terminates the session |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 297 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 298 | @param host: string, the hostname or IP address of the bmc to log out of |
| 299 | @param username: The user name for the bmc to log out of |
| 300 | @param pw: The password for the BMC to log out of |
| 301 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 302 | @param jsonFormat: boolean, flag that will only allow relevant data from user command to be display. This function becomes silent when set to true. |
| 303 | """ |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 304 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 305 | r = session.post('https://'+host+'/logout', headers=jsonHeader,json = {"data": [username, pw]}, verify=False, timeout=10) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 306 | except(requests.exceptions.Timeout): |
| 307 | print(connectionErrHandler(jsonFormat, "Timeout", None)) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 308 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 309 | if(jsonFormat==False): |
Matt Spinler | eae05b0 | 2019-01-24 12:59:34 -0600 | [diff] [blame] | 310 | if r.status_code == 200: |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 311 | print('User ' +username + ' has been logged out') |
| 312 | |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 313 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 314 | def fru(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 315 | """ |
| 316 | prints out the system inventory. deprecated see fruPrint and fruList |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 317 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 318 | @param host: string, the hostname or IP address of the bmc |
| 319 | @param args: contains additional arguments used by the fru sub command |
| 320 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 321 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 322 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 323 | #url="https://"+host+"/org/openbmc/inventory/system/chassis/enumerate" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 324 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 325 | #print(url) |
| 326 | #res = session.get(url, headers=httpHeader, verify=False) |
| 327 | #print(res.text) |
| 328 | #sample = res.text |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 329 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 330 | #inv_list = json.loads(sample)["data"] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 331 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 332 | url="https://"+host+"/xyz/openbmc_project/inventory/enumerate" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 333 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 334 | res = session.get(url, headers=jsonHeader, verify=False, timeout=40) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 335 | except(requests.exceptions.Timeout): |
| 336 | return(connectionErrHandler(args.json, "Timeout", None)) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 337 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 338 | sample = res.text |
| 339 | # inv_list.update(json.loads(sample)["data"]) |
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 | # #determine column width's |
| 342 | # colNames = ["FRU Name", "FRU Type", "Has Fault", "Is FRU", "Present", "Version"] |
| 343 | # 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] | 344 | # |
| 345 | # 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] | 346 | # "Present".ljust(colWidths[4]) + "Version".ljust(colWidths[5])) |
| 347 | # format the output |
| 348 | # for key in sorted(inv_list.keys()): |
| 349 | # keyParts = key.split("/") |
| 350 | # isFRU = "True" if (inv_list[key]["is_fru"]==1) else "False" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 351 | # |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 352 | # fruEntry = (keyParts[len(keyParts) - 1].ljust(colWidths[0]) + inv_list[key]["fru_type"].ljust(colWidths[1])+ |
| 353 | # inv_list[key]["fault"].ljust(colWidths[2])+isFRU.ljust(colWidths[3])+ |
| 354 | # inv_list[key]["present"].ljust(colWidths[4])+ inv_list[key]["version"].ljust(colWidths[5])) |
| 355 | # if(isTTY): |
| 356 | # if(inv_list[key]["is_fru"] == 1): |
| 357 | # color = "green" |
| 358 | # bold = True |
| 359 | # else: |
| 360 | # color='black' |
| 361 | # bold = False |
| 362 | # fruEntry = hilight(fruEntry, color, bold) |
| 363 | # print (fruEntry) |
| 364 | return sample |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 365 | |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 366 | def fruPrint(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 367 | """ |
| 368 | prints out all inventory |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 369 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 370 | @param host: string, the hostname or IP address of the bmc |
| 371 | @param args: contains additional arguments used by the fru sub command |
| 372 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 373 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 374 | @return returns the total fru list. |
| 375 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 376 | url="https://"+host+"/xyz/openbmc_project/inventory/enumerate" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 377 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 378 | res = session.get(url, headers=jsonHeader, verify=False, timeout=40) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 379 | except(requests.exceptions.Timeout): |
| 380 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 381 | |
| 382 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 383 | # print(res.text) |
| 384 | frulist = res.text |
| 385 | url="https://"+host+"/xyz/openbmc_project/software/enumerate" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 386 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 387 | res = session.get(url, headers=jsonHeader, verify=False, timeout=40) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 388 | except(requests.exceptions.Timeout): |
| 389 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 390 | # print(res.text) |
| 391 | frulist = frulist +"\n" + res.text |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 392 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 393 | return frulist |
| 394 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 395 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 396 | def fruList(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 397 | """ |
| 398 | prints out all inventory or only a specific specified item |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 399 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 400 | @param host: string, the hostname or IP address of the bmc |
| 401 | @param args: contains additional arguments used by the fru sub command |
| 402 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 403 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 404 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 405 | if(args.items==True): |
| 406 | return fruPrint(host, args, session) |
| 407 | else: |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 408 | return fruPrint(host, args, session) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 409 | |
| 410 | |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 411 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 412 | def fruStatus(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 413 | """ |
| 414 | prints out the status of all FRUs |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 415 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 416 | @param host: string, the hostname or IP address of the bmc |
| 417 | @param args: contains additional arguments used by the fru sub command |
| 418 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 419 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 420 | """ |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 421 | url="https://"+host+"/xyz/openbmc_project/inventory/enumerate" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 422 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 423 | res = session.get(url, headers=jsonHeader, verify=False) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 424 | except(requests.exceptions.Timeout): |
| 425 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 426 | # print(res.text) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 427 | frulist = json.loads(res.text)['data'] |
| 428 | frus = {} |
| 429 | for key in frulist: |
| 430 | component = frulist[key] |
| 431 | isFru = False |
| 432 | present = False |
| 433 | func = False |
| 434 | hasSels = False |
| 435 | keyPieces = key.split('/') |
| 436 | fruName = keyPieces[-1] |
| 437 | if 'core' in fruName: #associate cores to cpus |
| 438 | fruName = keyPieces[-2] + '-' + keyPieces[-1] |
| 439 | if 'Functional' in component: |
| 440 | if('Present' in component): |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 441 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 442 | if 'FieldReplaceable' in component: |
| 443 | if component['FieldReplaceable'] == 1: |
| 444 | isFru = True |
| 445 | if "fan" in fruName: |
| 446 | isFru = True; |
| 447 | if component['Present'] == 1: |
| 448 | present = True |
| 449 | if component['Functional'] == 1: |
| 450 | func = True |
| 451 | if ((key + "/fault") in frulist): |
| 452 | hasSels = True; |
| 453 | if args.verbose: |
| 454 | if hasSels: |
| 455 | loglist = [] |
| 456 | faults = frulist[key+"/fault"]['endpoints'] |
| 457 | for item in faults: |
| 458 | loglist.append(item.split('/')[-1]) |
| 459 | frus[fruName] = {"compName": fruName, "Functional": boolToString(func), "Present":boolToString(present), "IsFru": boolToString(isFru), "selList": ', '.join(loglist).strip() } |
| 460 | else: |
| 461 | frus[fruName] = {"compName": fruName, "Functional": boolToString(func), "Present":boolToString(present), "IsFru": boolToString(isFru), "selList": "None" } |
| 462 | else: |
| 463 | 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] | 464 | elif "power_supply" in fruName or "powersupply" in fruName: |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 465 | if component['Present'] ==1: |
| 466 | present = True |
| 467 | isFru = True |
| 468 | if ((key + "/fault") in frulist): |
| 469 | hasSels = True; |
| 470 | if args.verbose: |
| 471 | if hasSels: |
| 472 | loglist = [] |
| 473 | faults = frulist[key+"/fault"]['endpoints'] |
Obihörnchen | ff8035f | 2018-12-05 21:07:37 +0100 | [diff] [blame] | 474 | for item in faults: |
| 475 | loglist.append(item.split('/')[-1]) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 476 | frus[fruName] = {"compName": fruName, "Functional": "No", "Present":boolToString(present), "IsFru": boolToString(isFru), "selList": ', '.join(loglist).strip() } |
| 477 | else: |
| 478 | frus[fruName] = {"compName": fruName, "Functional": "Yes", "Present":boolToString(present), "IsFru": boolToString(isFru), "selList": "None" } |
| 479 | else: |
| 480 | frus[fruName] = {"compName": fruName, "Functional": boolToString(not hasSels), "Present":boolToString(present), "IsFru": boolToString(isFru), "hasSEL": boolToString(hasSels) } |
| 481 | if not args.json: |
| 482 | if not args.verbose: |
| 483 | colNames = ["Component", "Is a FRU", "Present", "Functional", "Has Logs"] |
| 484 | keylist = ["compName", "IsFru", "Present", "Functional", "hasSEL"] |
| 485 | else: |
| 486 | colNames = ["Component", "Is a FRU", "Present", "Functional", "Assoc. Log Number(s)"] |
| 487 | keylist = ["compName", "IsFru", "Present", "Functional", "selList"] |
| 488 | return tableDisplay(keylist, colNames, frus) |
| 489 | else: |
| 490 | 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] | 491 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 492 | def sensor(host, args, session): |
| 493 | """ |
| 494 | prints out all sensors |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 495 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 496 | @param host: string, the hostname or IP address of the bmc |
| 497 | @param args: contains additional arguments used by the sensor sub command |
| 498 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 499 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 500 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 501 | url="https://"+host+"/xyz/openbmc_project/sensors/enumerate" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 502 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 503 | res = session.get(url, headers=jsonHeader, verify=False, timeout=30) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 504 | except(requests.exceptions.Timeout): |
| 505 | return(connectionErrHandler(args.json, "Timeout", None)) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 506 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 507 | #Get OCC status |
| 508 | url="https://"+host+"/org/open_power/control/enumerate" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 509 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 510 | occres = session.get(url, headers=jsonHeader, verify=False, timeout=30) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 511 | except(requests.exceptions.Timeout): |
| 512 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 513 | if not args.json: |
| 514 | colNames = ['sensor', 'type', 'units', 'value', 'target'] |
| 515 | sensors = json.loads(res.text)["data"] |
| 516 | output = {} |
| 517 | for key in sensors: |
| 518 | senDict = {} |
| 519 | keyparts = key.split("/") |
| 520 | senDict['sensorName'] = keyparts[-1] |
| 521 | senDict['type'] = keyparts[-2] |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 522 | try: |
| 523 | senDict['units'] = sensors[key]['Unit'].split('.')[-1] |
| 524 | except KeyError: |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 525 | senDict['units'] = "N/A" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 526 | if('Scale' in sensors[key]): |
| 527 | scale = 10 ** sensors[key]['Scale'] |
| 528 | else: |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 529 | scale = 1 |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 530 | try: |
| 531 | senDict['value'] = str(sensors[key]['Value'] * scale) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 532 | except KeyError: |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 533 | if 'value' in sensors[key]: |
| 534 | senDict['value'] = sensors[key]['value'] |
| 535 | else: |
| 536 | senDict['value'] = "N/A" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 537 | if 'Target' in sensors[key]: |
| 538 | senDict['target'] = str(sensors[key]['Target']) |
| 539 | else: |
| 540 | senDict['target'] = 'N/A' |
| 541 | output[senDict['sensorName']] = senDict |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 542 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 543 | occstatus = json.loads(occres.text)["data"] |
| 544 | if '/org/open_power/control/occ0' in occstatus: |
| 545 | occ0 = occstatus["/org/open_power/control/occ0"]['OccActive'] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 546 | if occ0 == 1: |
| 547 | occ0 = 'Active' |
| 548 | else: |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 549 | occ0 = 'Inactive' |
| 550 | output['OCC0'] = {'sensorName':'OCC0', 'type': 'Discrete', 'units': 'N/A', 'value': occ0, 'target': 'Active'} |
| 551 | occ1 = occstatus["/org/open_power/control/occ1"]['OccActive'] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 552 | if occ1 == 1: |
| 553 | occ1 = 'Active' |
| 554 | else: |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 555 | occ1 = 'Inactive' |
| 556 | output['OCC1'] = {'sensorName':'OCC1', 'type': 'Discrete', 'units': 'N/A', 'value': occ0, 'target': 'Active'} |
| 557 | else: |
| 558 | output['OCC0'] = {'sensorName':'OCC0', 'type': 'Discrete', 'units': 'N/A', 'value': 'Inactive', 'target': 'Inactive'} |
| 559 | output['OCC1'] = {'sensorName':'OCC1', 'type': 'Discrete', 'units': 'N/A', 'value': 'Inactive', 'target': 'Inactive'} |
| 560 | keylist = ['sensorName', 'type', 'units', 'value', 'target'] |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 561 | |
| 562 | return tableDisplay(keylist, colNames, output) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 563 | else: |
| 564 | return res.text + occres.text |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 565 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 566 | def sel(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 567 | """ |
| 568 | prints out the bmc alerts |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 569 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 570 | @param host: string, the hostname or IP address of the bmc |
| 571 | @param args: contains additional arguments used by the sel sub command |
| 572 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 573 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 574 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 575 | |
| 576 | url="https://"+host+"/xyz/openbmc_project/logging/entry/enumerate" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 577 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 578 | res = session.get(url, headers=jsonHeader, verify=False, timeout=60) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 579 | except(requests.exceptions.Timeout): |
| 580 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 581 | return res.text |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 582 | |
| 583 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 584 | def parseESEL(args, eselRAW): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 585 | """ |
| 586 | parses the esel data and gets predetermined search terms |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 587 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 588 | @param eselRAW: string, the raw esel string from the bmc |
| 589 | @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] | 590 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 591 | eselParts = {} |
| 592 | esel_bin = binascii.unhexlify(''.join(eselRAW.split()[16:])) |
| 593 | #search terms contains the search term as the key and the return dictionary key as it's value |
| 594 | searchTerms = { 'Signature Description':'signatureDescription', 'devdesc':'devdesc', |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 595 | 'Callout type': 'calloutType', 'Procedure':'procedure', 'Sensor Type': 'sensorType'} |
Justin Thaler | 24d4efa | 2018-11-08 22:48:10 -0600 | [diff] [blame] | 596 | uniqueID = str(uuid.uuid4()) |
| 597 | eselBinPath = tempfile.gettempdir() + os.sep + uniqueID + 'esel.bin' |
Justin Thaler | cf1deae | 2018-05-25 19:35:21 -0500 | [diff] [blame] | 598 | with open(eselBinPath, 'wb') as f: |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 599 | f.write(esel_bin) |
| 600 | errlPath = "" |
| 601 | #use the right errl file for the machine architecture |
| 602 | arch = platform.machine() |
| 603 | if(arch =='x86_64' or arch =='AMD64'): |
| 604 | if os.path.exists('/opt/ibm/ras/bin/x86_64/errl'): |
| 605 | errlPath = '/opt/ibm/ras/bin/x86_64/errl' |
| 606 | elif os.path.exists('errl/x86_64/errl'): |
| 607 | errlPath = 'errl/x86_64/errl' |
| 608 | else: |
| 609 | errlPath = 'x86_64/errl' |
| 610 | elif (platform.machine()=='ppc64le'): |
| 611 | if os.path.exists('/opt/ibm/ras/bin/ppc64le/errl'): |
| 612 | errlPath = '/opt/ibm/ras/bin/ppc64le/errl' |
| 613 | elif os.path.exists('errl/ppc64le/errl'): |
| 614 | errlPath = 'errl/ppc64le/errl' |
| 615 | else: |
| 616 | errlPath = 'ppc64le/errl' |
| 617 | else: |
| 618 | print("machine architecture not supported for parsing eSELs") |
| 619 | return eselParts |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 620 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 621 | if(os.path.exists(errlPath)): |
Justin Thaler | cf1deae | 2018-05-25 19:35:21 -0500 | [diff] [blame] | 622 | output= subprocess.check_output([errlPath, '-d', '--file='+eselBinPath]).decode('utf-8') |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 623 | # output = proc.communicate()[0] |
| 624 | lines = output.split('\n') |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 625 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 626 | if(hasattr(args, 'fullEsel')): |
| 627 | return output |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 628 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 629 | for i in range(0, len(lines)): |
| 630 | lineParts = lines[i].split(':') |
| 631 | if(len(lineParts)>1): #ignore multi lines, output formatting lines, and other information |
| 632 | for term in searchTerms: |
| 633 | if(term in lineParts[0]): |
| 634 | temp = lines[i][lines[i].find(':')+1:].strip()[:-1].strip() |
| 635 | if lines[i+1].find(':') != -1: |
| 636 | if (len(lines[i+1].split(':')[0][1:].strip())==0): |
| 637 | while(len(lines[i][:lines[i].find(':')].strip())>2): |
Justin Thaler | 4303042 | 2018-11-08 22:50:21 -0600 | [diff] [blame] | 638 | #has multiple lines, process and update line counter |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 639 | if((i+1) <= len(lines)): |
| 640 | i+=1 |
| 641 | else: |
| 642 | i=i-1 |
| 643 | break |
Justin Thaler | 4303042 | 2018-11-08 22:50:21 -0600 | [diff] [blame] | 644 | #Append the content from the next line removing the pretty display characters |
| 645 | #Finds the first colon then starts 2 characters after, then removes all whitespace |
| 646 | 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] | 647 | if(searchTerms[term] in eselParts): |
| 648 | eselParts[searchTerms[term]] = eselParts[searchTerms[term]] + ", " + temp |
| 649 | else: |
| 650 | eselParts[searchTerms[term]] = temp |
Justin Thaler | cf1deae | 2018-05-25 19:35:21 -0500 | [diff] [blame] | 651 | os.remove(eselBinPath) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 652 | else: |
| 653 | print("errl file cannot be found") |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 654 | |
| 655 | return eselParts |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 656 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 657 | |
Matt Spinler | 02d0dff | 2018-08-29 13:19:25 -0500 | [diff] [blame] | 658 | def getESELSeverity(esel): |
| 659 | """ |
| 660 | Finds the severity type in an eSEL from the User Header section. |
| 661 | @param esel - the eSEL data |
| 662 | @return severity - e.g. 'Critical' |
| 663 | """ |
| 664 | |
| 665 | # everything but 1 and 2 are Critical |
| 666 | # '1': 'recovered', |
| 667 | # '2': 'predictive', |
| 668 | # '4': 'unrecoverable', |
| 669 | # '5': 'critical', |
| 670 | # '6': 'diagnostic', |
| 671 | # '7': 'symptom' |
| 672 | severities = { |
| 673 | '1': 'Informational', |
| 674 | '2': 'Warning' |
| 675 | } |
| 676 | |
| 677 | try: |
| 678 | headerPosition = esel.index('55 48') # 'UH' |
| 679 | # The severity is the last byte in the 8 byte section (a byte is ' bb') |
| 680 | severity = esel[headerPosition:headerPosition+32].split(' ')[-1] |
| 681 | type = severity[0] |
| 682 | except ValueError: |
| 683 | print("Could not find severity value in UH section in eSEL") |
| 684 | type = 'x'; |
| 685 | |
| 686 | return severities.get(type, 'Critical') |
| 687 | |
| 688 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 689 | def sortSELs(events): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 690 | """ |
| 691 | sorts the sels by timestamp, then log entry number |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 692 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 693 | @param events: Dictionary containing events |
| 694 | @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] | 695 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 696 | logNumList = [] |
| 697 | timestampList = [] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 698 | eventKeyDict = {} |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 699 | eventsWithTimestamp = {} |
| 700 | logNum2events = {} |
| 701 | for key in events: |
| 702 | if key == 'numAlerts': continue |
| 703 | if 'callout' in key: continue |
| 704 | timestamp = (events[key]['timestamp']) |
| 705 | if timestamp not in timestampList: |
| 706 | eventsWithTimestamp[timestamp] = [events[key]['logNum']] |
| 707 | else: |
| 708 | eventsWithTimestamp[timestamp].append(events[key]['logNum']) |
| 709 | #map logNumbers to the event dictionary keys |
| 710 | eventKeyDict[str(events[key]['logNum'])] = key |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 711 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 712 | timestampList = list(eventsWithTimestamp.keys()) |
| 713 | timestampList.sort() |
| 714 | for ts in timestampList: |
| 715 | if len(eventsWithTimestamp[ts]) > 1: |
| 716 | tmplist = eventsWithTimestamp[ts] |
| 717 | tmplist.sort() |
| 718 | logNumList = logNumList + tmplist |
| 719 | else: |
| 720 | logNumList = logNumList + eventsWithTimestamp[ts] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 721 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 722 | return [logNumList, eventKeyDict] |
| 723 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 724 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 725 | def parseAlerts(policyTable, selEntries, args): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 726 | """ |
| 727 | parses alerts in the IBM CER format, using an IBM policy Table |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 728 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 729 | @param policyTable: dictionary, the policy table entries |
| 730 | @param selEntries: dictionary, the alerts retrieved from the bmc |
| 731 | @return: A dictionary of the parsed entries, in chronological order |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 732 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 733 | eventDict = {} |
| 734 | eventNum ="" |
| 735 | count = 0 |
| 736 | esel = "" |
| 737 | eselParts = {} |
| 738 | i2cdevice= "" |
Matt Spinler | 02d0dff | 2018-08-29 13:19:25 -0500 | [diff] [blame] | 739 | eselSeverity = None |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 740 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 741 | 'prepare and sort the event entries' |
| 742 | for key in selEntries: |
| 743 | if 'callout' not in key: |
| 744 | selEntries[key]['logNum'] = key.split('/')[-1] |
| 745 | selEntries[key]['timestamp'] = selEntries[key]['Timestamp'] |
| 746 | sortedEntries = sortSELs(selEntries) |
| 747 | logNumList = sortedEntries[0] |
| 748 | eventKeyDict = sortedEntries[1] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 749 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 750 | for logNum in logNumList: |
| 751 | key = eventKeyDict[logNum] |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 752 | hasEsel=False |
| 753 | i2creadFail = False |
| 754 | if 'callout' in key: |
| 755 | continue |
| 756 | else: |
| 757 | messageID = str(selEntries[key]['Message']) |
| 758 | addDataPiece = selEntries[key]['AdditionalData'] |
| 759 | calloutIndex = 0 |
| 760 | calloutFound = False |
| 761 | for i in range(len(addDataPiece)): |
| 762 | if("CALLOUT_INVENTORY_PATH" in addDataPiece[i]): |
| 763 | calloutIndex = i |
| 764 | calloutFound = True |
| 765 | fruCallout = str(addDataPiece[calloutIndex]).split('=')[1] |
| 766 | if("CALLOUT_DEVICE_PATH" in addDataPiece[i]): |
| 767 | i2creadFail = True |
Matt Spinler | d178a47 | 2018-08-31 09:48:52 -0500 | [diff] [blame] | 768 | |
| 769 | fruCallout = str(addDataPiece[calloutIndex]).split('=')[1] |
| 770 | |
| 771 | # Fall back to "I2C"/"FSI" if dev path isn't in policy table |
| 772 | if (messageID + '||' + fruCallout) not in policyTable: |
| 773 | i2cdevice = str(addDataPiece[i]).strip().split('=')[1] |
| 774 | i2cdevice = '/'.join(i2cdevice.split('/')[-4:]) |
| 775 | if 'fsi' in str(addDataPiece[calloutIndex]).split('=')[1]: |
| 776 | fruCallout = 'FSI' |
| 777 | else: |
| 778 | fruCallout = 'I2C' |
Justin Thaler | e34c43a | 2018-05-25 19:37:55 -0500 | [diff] [blame] | 779 | calloutFound = True |
| 780 | if("CALLOUT_GPIO_NUM" in addDataPiece[i]): |
| 781 | if not calloutFound: |
| 782 | fruCallout = 'GPIO' |
| 783 | calloutFound = True |
| 784 | if("CALLOUT_IIC_BUS" in addDataPiece[i]): |
| 785 | if not calloutFound: |
| 786 | fruCallout = "I2C" |
| 787 | calloutFound = True |
| 788 | if("CALLOUT_IPMI_SENSOR_NUM" in addDataPiece[i]): |
| 789 | if not calloutFound: |
| 790 | fruCallout = "IPMI" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 791 | calloutFound = True |
| 792 | if("ESEL" in addDataPiece[i]): |
| 793 | esel = str(addDataPiece[i]).strip().split('=')[1] |
Matt Spinler | 02d0dff | 2018-08-29 13:19:25 -0500 | [diff] [blame] | 794 | eselSeverity = getESELSeverity(esel) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 795 | if args.devdebug: |
| 796 | eselParts = parseESEL(args, esel) |
| 797 | hasEsel=True |
| 798 | if("GPU" in addDataPiece[i]): |
| 799 | fruCallout = '/xyz/openbmc_project/inventory/system/chassis/motherboard/gpu' + str(addDataPiece[i]).strip()[-1] |
| 800 | calloutFound = True |
| 801 | if("PROCEDURE" in addDataPiece[i]): |
| 802 | fruCallout = str(hex(int(str(addDataPiece[i]).split('=')[1])))[2:] |
| 803 | calloutFound = True |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 804 | if("RAIL_NAME" in addDataPiece[i]): |
| 805 | calloutFound=True |
| 806 | fruCallout = str(addDataPiece[i]).split('=')[1].strip() |
| 807 | if("INPUT_NAME" in addDataPiece[i]): |
| 808 | calloutFound=True |
| 809 | fruCallout = str(addDataPiece[i]).split('=')[1].strip() |
| 810 | if("SENSOR_TYPE" in addDataPiece[i]): |
| 811 | calloutFound=True |
| 812 | fruCallout = str(addDataPiece[i]).split('=')[1].strip() |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 813 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 814 | if(calloutFound): |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 815 | if fruCallout != "": |
| 816 | policyKey = messageID +"||" + fruCallout |
Matt Spinler | 02d0dff | 2018-08-29 13:19:25 -0500 | [diff] [blame] | 817 | |
| 818 | # Also use the severity for hostboot errors |
| 819 | if eselSeverity and messageID == 'org.open_power.Host.Error.Event': |
| 820 | policyKey += '||' + eselSeverity |
| 821 | |
| 822 | # if not in the table, fall back to the original key |
| 823 | if policyKey not in policyTable: |
| 824 | policyKey = policyKey.replace('||'+eselSeverity, '') |
| 825 | |
Justin Thaler | e34c43a | 2018-05-25 19:37:55 -0500 | [diff] [blame] | 826 | if policyKey not in policyTable: |
| 827 | policyKey = messageID |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 828 | else: |
| 829 | policyKey = messageID |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 830 | else: |
| 831 | policyKey = messageID |
| 832 | event = {} |
| 833 | eventNum = str(count) |
| 834 | if policyKey in policyTable: |
| 835 | for pkey in policyTable[policyKey]: |
| 836 | if(type(policyTable[policyKey][pkey])== bool): |
| 837 | event[pkey] = boolToString(policyTable[policyKey][pkey]) |
| 838 | else: |
| 839 | if (i2creadFail and pkey == 'Message'): |
| 840 | event[pkey] = policyTable[policyKey][pkey] + ' ' +i2cdevice |
| 841 | else: |
| 842 | event[pkey] = policyTable[policyKey][pkey] |
| 843 | event['timestamp'] = selEntries[key]['Timestamp'] |
| 844 | event['resolved'] = bool(selEntries[key]['Resolved']) |
| 845 | if(hasEsel): |
| 846 | if args.devdebug: |
| 847 | event['eselParts'] = eselParts |
| 848 | event['raweSEL'] = esel |
| 849 | event['logNum'] = key.split('/')[-1] |
| 850 | eventDict['event' + eventNum] = event |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 851 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 852 | else: |
| 853 | severity = str(selEntries[key]['Severity']).split('.')[-1] |
| 854 | if severity == 'Error': |
| 855 | severity = 'Critical' |
| 856 | eventDict['event'+eventNum] = {} |
| 857 | eventDict['event' + eventNum]['error'] = "error: Not found in policy table: " + policyKey |
| 858 | eventDict['event' + eventNum]['timestamp'] = selEntries[key]['Timestamp'] |
| 859 | eventDict['event' + eventNum]['Severity'] = severity |
| 860 | if(hasEsel): |
| 861 | if args.devdebug: |
| 862 | eventDict['event' +eventNum]['eselParts'] = eselParts |
| 863 | eventDict['event' +eventNum]['raweSEL'] = esel |
| 864 | eventDict['event' +eventNum]['logNum'] = key.split('/')[-1] |
| 865 | eventDict['event' +eventNum]['resolved'] = bool(selEntries[key]['Resolved']) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 866 | count += 1 |
| 867 | return eventDict |
| 868 | |
| 869 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 870 | def selDisplay(events, args): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 871 | """ |
| 872 | displays alerts in human readable format |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 873 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 874 | @param events: Dictionary containing events |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 875 | @return: |
| 876 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 877 | activeAlerts = [] |
| 878 | historyAlerts = [] |
| 879 | sortedEntries = sortSELs(events) |
| 880 | logNumList = sortedEntries[0] |
| 881 | eventKeyDict = sortedEntries[1] |
| 882 | keylist = ['Entry', 'ID', 'Timestamp', 'Serviceable', 'Severity','Message'] |
| 883 | if(args.devdebug): |
| 884 | colNames = ['Entry', 'ID', 'Timestamp', 'Serviceable', 'Severity','Message', 'eSEL contents'] |
| 885 | keylist.append('eSEL') |
| 886 | else: |
| 887 | colNames = ['Entry', 'ID', 'Timestamp', 'Serviceable', 'Severity', 'Message'] |
| 888 | for log in logNumList: |
| 889 | selDict = {} |
| 890 | alert = events[eventKeyDict[str(log)]] |
| 891 | if('error' in alert): |
| 892 | selDict['Entry'] = alert['logNum'] |
| 893 | selDict['ID'] = 'Unknown' |
| 894 | selDict['Timestamp'] = datetime.datetime.fromtimestamp(int(alert['timestamp']/1000)).strftime("%Y-%m-%d %H:%M:%S") |
| 895 | msg = alert['error'] |
| 896 | polMsg = msg.split("policy table:")[0] |
| 897 | msg = msg.split("policy table:")[1] |
| 898 | msgPieces = msg.split("||") |
| 899 | err = msgPieces[0] |
| 900 | if(err.find("org.open_power.")!=-1): |
| 901 | err = err.split("org.open_power.")[1] |
| 902 | elif(err.find("xyz.openbmc_project.")!=-1): |
| 903 | err = err.split("xyz.openbmc_project.")[1] |
| 904 | else: |
| 905 | err = msgPieces[0] |
| 906 | callout = "" |
| 907 | if len(msgPieces) >1: |
| 908 | callout = msgPieces[1] |
| 909 | if(callout.find("/org/open_power/")!=-1): |
| 910 | callout = callout.split("/org/open_power/")[1] |
| 911 | elif(callout.find("/xyz/openbmc_project/")!=-1): |
| 912 | callout = callout.split("/xyz/openbmc_project/")[1] |
| 913 | else: |
| 914 | callout = msgPieces[1] |
| 915 | selDict['Message'] = polMsg +"policy table: "+ err + "||" + callout |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 916 | selDict['Serviceable'] = 'Unknown' |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 917 | selDict['Severity'] = alert['Severity'] |
| 918 | else: |
| 919 | selDict['Entry'] = alert['logNum'] |
| 920 | selDict['ID'] = alert['CommonEventID'] |
| 921 | 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] | 922 | selDict['Message'] = alert['Message'] |
| 923 | selDict['Serviceable'] = alert['Serviceable'] |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 924 | selDict['Severity'] = alert['Severity'] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 925 | |
| 926 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 927 | eselOrder = ['refCode','signatureDescription', 'eselType', 'devdesc', 'calloutType', 'procedure'] |
| 928 | if ('eselParts' in alert and args.devdebug): |
| 929 | eselOutput = "" |
| 930 | for item in eselOrder: |
| 931 | if item in alert['eselParts']: |
| 932 | eselOutput = eselOutput + item + ": " + alert['eselParts'][item] + " | " |
| 933 | selDict['eSEL'] = eselOutput |
| 934 | else: |
| 935 | if args.devdebug: |
| 936 | selDict['eSEL'] = "None" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 937 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 938 | if not alert['resolved']: |
| 939 | activeAlerts.append(selDict) |
| 940 | else: |
| 941 | historyAlerts.append(selDict) |
| 942 | mergedOutput = activeAlerts + historyAlerts |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 943 | colWidth = setColWidth(keylist, len(colNames), dict(enumerate(mergedOutput)), colNames) |
| 944 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 945 | output = "" |
| 946 | if(len(activeAlerts)>0): |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 947 | row = "" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 948 | output +="----Active Alerts----\n" |
| 949 | for i in range(0, len(colNames)): |
| 950 | if i!=0: row =row + "| " |
| 951 | row = row + colNames[i].ljust(colWidth[i]) |
| 952 | output += row + "\n" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 953 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 954 | for i in range(0,len(activeAlerts)): |
| 955 | row = "" |
| 956 | for j in range(len(activeAlerts[i])): |
| 957 | if (j != 0): row = row + "| " |
| 958 | row = row + activeAlerts[i][keylist[j]].ljust(colWidth[j]) |
| 959 | output += row + "\n" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 960 | |
| 961 | if(len(historyAlerts)>0): |
| 962 | row = "" |
| 963 | output+= "----Historical Alerts----\n" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 964 | for i in range(len(colNames)): |
| 965 | if i!=0: row =row + "| " |
| 966 | row = row + colNames[i].ljust(colWidth[i]) |
| 967 | output += row + "\n" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 968 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 969 | for i in range(0, len(historyAlerts)): |
| 970 | row = "" |
| 971 | for j in range(len(historyAlerts[i])): |
| 972 | if (j != 0): row = row + "| " |
| 973 | row = row + historyAlerts[i][keylist[j]].ljust(colWidth[j]) |
| 974 | output += row + "\n" |
| 975 | # print(events[eventKeyDict[str(log)]]) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 976 | return output |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 977 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 978 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 979 | def selPrint(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 980 | """ |
| 981 | prints out all bmc alerts |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 982 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 983 | @param host: string, the hostname or IP address of the bmc |
| 984 | @param args: contains additional arguments used by the fru sub command |
| 985 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 986 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 987 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 988 | if(args.policyTableLoc is None): |
| 989 | if os.path.exists('policyTable.json'): |
| 990 | ptableLoc = "policyTable.json" |
| 991 | elif os.path.exists('/opt/ibm/ras/lib/policyTable.json'): |
| 992 | ptableLoc = '/opt/ibm/ras/lib/policyTable.json' |
| 993 | else: |
| 994 | ptableLoc = 'lib/policyTable.json' |
| 995 | else: |
| 996 | ptableLoc = args.policyTableLoc |
| 997 | policyTable = loadPolicyTable(ptableLoc) |
| 998 | rawselEntries = "" |
| 999 | if(hasattr(args, 'fileloc') and args.fileloc is not None): |
| 1000 | if os.path.exists(args.fileloc): |
| 1001 | with open(args.fileloc, 'r') as selFile: |
| 1002 | selLines = selFile.readlines() |
| 1003 | rawselEntries = ''.join(selLines) |
| 1004 | else: |
| 1005 | print("Error: File not found") |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1006 | sys.exit(1) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1007 | else: |
| 1008 | rawselEntries = sel(host, args, session) |
| 1009 | loadFailed = False |
| 1010 | try: |
| 1011 | selEntries = json.loads(rawselEntries) |
| 1012 | except ValueError: |
| 1013 | loadFailed = True |
| 1014 | if loadFailed: |
| 1015 | cleanSels = json.dumps(rawselEntries).replace('\\n', '') |
| 1016 | #need to load json twice as original content was string escaped a second time |
| 1017 | selEntries = json.loads(json.loads(cleanSels)) |
| 1018 | selEntries = selEntries['data'] |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1019 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1020 | if 'description' in selEntries: |
| 1021 | if(args.json): |
| 1022 | return("{\n\t\"numAlerts\": 0\n}") |
| 1023 | else: |
| 1024 | return("No log entries found") |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1025 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1026 | else: |
| 1027 | if(len(policyTable)>0): |
| 1028 | events = parseAlerts(policyTable, selEntries, args) |
| 1029 | if(args.json): |
| 1030 | events["numAlerts"] = len(events) |
| 1031 | retValue = str(json.dumps(events, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False)) |
| 1032 | return retValue |
| 1033 | elif(hasattr(args, 'fullSel')): |
| 1034 | return events |
| 1035 | else: |
| 1036 | #get log numbers to order event entries sequentially |
| 1037 | return selDisplay(events, args) |
| 1038 | else: |
| 1039 | if(args.json): |
| 1040 | return selEntries |
| 1041 | else: |
| 1042 | print("error: Policy Table not found.") |
| 1043 | return selEntries |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1044 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1045 | def selList(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1046 | """ |
| 1047 | 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] | 1048 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1049 | @param host: string, the hostname or IP address of the bmc |
| 1050 | @param args: contains additional arguments used by the fru sub command |
| 1051 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1052 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1053 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1054 | return(sel(host, args, session)) |
| 1055 | |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1056 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1057 | def selClear(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1058 | """ |
| 1059 | clears all alerts |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1060 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1061 | @param host: string, the hostname or IP address of the bmc |
| 1062 | @param args: contains additional arguments used by the fru sub command |
| 1063 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1064 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1065 | """ |
Matt Spinler | 47b13e9 | 2019-01-04 14:58:53 -0600 | [diff] [blame] | 1066 | url="https://"+host+"/xyz/openbmc_project/logging/action/DeleteAll" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1067 | data = "{\"data\": [] }" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1068 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1069 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 1070 | res = session.post(url, headers=jsonHeader, data=data, verify=False, timeout=30) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1071 | except(requests.exceptions.Timeout): |
| 1072 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1073 | if res.status_code == 200: |
| 1074 | return "The Alert Log has been cleared. Please allow a few minutes for the action to complete." |
| 1075 | else: |
| 1076 | print("Unable to clear the logs, trying to clear 1 at a time") |
| 1077 | sels = json.loads(sel(host, args, session))['data'] |
| 1078 | for key in sels: |
| 1079 | if 'callout' not in key: |
| 1080 | logNum = key.split('/')[-1] |
| 1081 | url = "https://"+ host+ "/xyz/openbmc_project/logging/entry/"+logNum+"/action/Delete" |
| 1082 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 1083 | session.post(url, headers=jsonHeader, data=data, verify=False, timeout=30) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1084 | except(requests.exceptions.Timeout): |
| 1085 | return connectionErrHandler(args.json, "Timeout", None) |
| 1086 | sys.exit(1) |
| 1087 | except(requests.exceptions.ConnectionError) as err: |
| 1088 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 1089 | sys.exit(1) |
| 1090 | return ('Sel clearing complete') |
| 1091 | |
| 1092 | def selSetResolved(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1093 | """ |
| 1094 | sets a sel entry to resolved |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1095 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1096 | @param host: string, the hostname or IP address of the bmc |
| 1097 | @param args: contains additional arguments used by the fru sub command |
| 1098 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1099 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1100 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1101 | 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] | 1102 | data = "{\"data\": 1 }" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1103 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 1104 | res = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=30) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1105 | except(requests.exceptions.Timeout): |
| 1106 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1107 | if res.status_code == 200: |
| 1108 | return "Sel entry "+ str(args.selNum) +" is now set to resolved" |
| 1109 | else: |
| 1110 | return "Unable to set the alert to resolved" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1111 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1112 | def selResolveAll(host, args, session): |
| 1113 | """ |
| 1114 | sets a sel entry to resolved |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1115 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1116 | @param host: string, the hostname or IP address of the bmc |
| 1117 | @param args: contains additional arguments used by the fru sub command |
| 1118 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1119 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1120 | """ |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1121 | rawselEntries = sel(host, args, session) |
| 1122 | loadFailed = False |
| 1123 | try: |
| 1124 | selEntries = json.loads(rawselEntries) |
| 1125 | except ValueError: |
| 1126 | loadFailed = True |
| 1127 | if loadFailed: |
| 1128 | cleanSels = json.dumps(rawselEntries).replace('\\n', '') |
| 1129 | #need to load json twice as original content was string escaped a second time |
| 1130 | selEntries = json.loads(json.loads(cleanSels)) |
| 1131 | selEntries = selEntries['data'] |
| 1132 | |
| 1133 | if 'description' in selEntries: |
| 1134 | if(args.json): |
| 1135 | return("{\n\t\"selsResolved\": 0\n}") |
| 1136 | else: |
| 1137 | return("No log entries found") |
| 1138 | else: |
| 1139 | d = vars(args) |
| 1140 | successlist = [] |
| 1141 | failedlist = [] |
| 1142 | for key in selEntries: |
| 1143 | if 'callout' not in key: |
| 1144 | d['selNum'] = key.split('/')[-1] |
| 1145 | resolved = selSetResolved(host,args,session) |
| 1146 | if 'Sel entry' in resolved: |
| 1147 | successlist.append(d['selNum']) |
| 1148 | else: |
| 1149 | failedlist.append(d['selNum']) |
| 1150 | output = "" |
| 1151 | successlist.sort() |
| 1152 | failedlist.sort() |
| 1153 | if len(successlist)>0: |
| 1154 | output = "Successfully resolved: " +', '.join(successlist) +"\n" |
| 1155 | if len(failedlist)>0: |
| 1156 | output += "Failed to resolve: " + ', '.join(failedlist) + "\n" |
| 1157 | return output |
| 1158 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1159 | def chassisPower(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1160 | """ |
| 1161 | 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] | 1162 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1163 | @param host: string, the hostname or IP address of the bmc |
| 1164 | @param args: contains additional arguments used by the fru sub command |
| 1165 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1166 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1167 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1168 | if(args.powcmd == 'on'): |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1169 | if checkFWactivation(host, args, session): |
| 1170 | return ("Chassis Power control disabled during firmware activation") |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1171 | print("Attempting to Power on...:") |
| 1172 | url="https://"+host+"/xyz/openbmc_project/state/host0/attr/RequestedHostTransition" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1173 | data = '{"data":"xyz.openbmc_project.State.Host.Transition.On"}' |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1174 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 1175 | res = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=30) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1176 | except(requests.exceptions.Timeout): |
| 1177 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1178 | return res.text |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1179 | elif(args.powcmd == 'softoff'): |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1180 | if checkFWactivation(host, args, session): |
| 1181 | return ("Chassis Power control disabled during firmware activation") |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1182 | print("Attempting to Power off gracefully...:") |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1183 | url="https://"+host+"/xyz/openbmc_project/state/host0/attr/RequestedHostTransition" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1184 | data = '{"data":"xyz.openbmc_project.State.Host.Transition.Off"}' |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1185 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 1186 | res = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=30) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1187 | except(requests.exceptions.Timeout): |
| 1188 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 1189 | return res.text |
| 1190 | elif(args.powcmd == 'hardoff'): |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1191 | if checkFWactivation(host, args, session): |
| 1192 | return ("Chassis Power control disabled during firmware activation") |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1193 | print("Attempting to Power off immediately...:") |
| 1194 | url="https://"+host+"/xyz/openbmc_project/state/chassis0/attr/RequestedPowerTransition" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1195 | data = '{"data":"xyz.openbmc_project.State.Chassis.Transition.Off"}' |
| 1196 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 1197 | res = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=30) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1198 | except(requests.exceptions.Timeout): |
| 1199 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1200 | return res.text |
| 1201 | elif(args.powcmd == 'status'): |
| 1202 | url="https://"+host+"/xyz/openbmc_project/state/chassis0/attr/CurrentPowerState" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1203 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 1204 | res = session.get(url, headers=jsonHeader, verify=False, timeout=30) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1205 | except(requests.exceptions.Timeout): |
| 1206 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1207 | chassisState = json.loads(res.text)['data'].split('.')[-1] |
| 1208 | url="https://"+host+"/xyz/openbmc_project/state/host0/attr/CurrentHostState" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1209 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 1210 | res = session.get(url, headers=jsonHeader, verify=False, timeout=30) |
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 | hostState = json.loads(res.text)['data'].split('.')[-1] |
| 1214 | url="https://"+host+"/xyz/openbmc_project/state/bmc0/attr/CurrentBMCState" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1215 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 1216 | res = session.get(url, headers=jsonHeader, verify=False, timeout=30) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1217 | except(requests.exceptions.Timeout): |
| 1218 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1219 | bmcState = json.loads(res.text)['data'].split('.')[-1] |
| 1220 | if(args.json): |
| 1221 | outDict = {"Chassis Power State" : chassisState, "Host Power State" : hostState, "BMC Power State":bmcState} |
| 1222 | return json.dumps(outDict, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False) |
| 1223 | else: |
| 1224 | return "Chassis Power State: " +chassisState + "\nHost Power State: " + hostState + "\nBMC Power State: " + bmcState |
| 1225 | else: |
| 1226 | return "Invalid chassis power command" |
| 1227 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1228 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1229 | def chassisIdent(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1230 | """ |
| 1231 | 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] | 1232 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1233 | @param host: string, the hostname or IP address of the bmc |
| 1234 | @param args: contains additional arguments used by the fru sub command |
| 1235 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1236 | @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] | 1237 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1238 | if(args.identcmd == 'on'): |
| 1239 | print("Attempting to turn identify light on...:") |
| 1240 | url="https://"+host+"/xyz/openbmc_project/led/groups/enclosure_identify/attr/Asserted" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1241 | data = '{"data":true}' |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1242 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 1243 | res = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=30) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1244 | except(requests.exceptions.Timeout): |
| 1245 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1246 | return res.text |
| 1247 | elif(args.identcmd == 'off'): |
| 1248 | print("Attempting to turn identify light off...:") |
| 1249 | url="https://"+host+"/xyz/openbmc_project/led/groups/enclosure_identify/attr/Asserted" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1250 | data = '{"data":false}' |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1251 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 1252 | res = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=30) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1253 | except(requests.exceptions.Timeout): |
| 1254 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1255 | return res.text |
| 1256 | elif(args.identcmd == 'status'): |
| 1257 | url="https://"+host+"/xyz/openbmc_project/led/groups/enclosure_identify" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1258 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 1259 | res = session.get(url, headers=jsonHeader, verify=False, timeout=30) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1260 | except(requests.exceptions.Timeout): |
| 1261 | return(connectionErrHandler(args.json, "Timeout", None)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1262 | status = json.loads(res.text)['data'] |
| 1263 | if(args.json): |
| 1264 | return status |
| 1265 | else: |
| 1266 | if status['Asserted'] == 0: |
| 1267 | return "Identify light is off" |
| 1268 | else: |
| 1269 | return "Identify light is blinking" |
| 1270 | else: |
| 1271 | return "Invalid chassis identify command" |
| 1272 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1273 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1274 | def chassis(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1275 | """ |
| 1276 | controls the different chassis commands |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1277 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1278 | @param host: string, the hostname or IP address of the bmc |
| 1279 | @param args: contains additional arguments used by the fru sub command |
| 1280 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1281 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1282 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1283 | if(hasattr(args, 'powcmd')): |
| 1284 | result = chassisPower(host,args,session) |
| 1285 | elif(hasattr(args, 'identcmd')): |
| 1286 | result = chassisIdent(host, args, session) |
| 1287 | else: |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1288 | return "This feature is not yet implemented" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1289 | return result |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1290 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1291 | def bmcDumpRetrieve(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1292 | """ |
| 1293 | Downloads a dump file from the bmc |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1294 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1295 | @param host: string, the hostname or IP address of the bmc |
| 1296 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1297 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1298 | @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] | 1299 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1300 | dumpNum = args.dumpNum |
| 1301 | if (args.dumpSaveLoc is not None): |
| 1302 | saveLoc = args.dumpSaveLoc |
| 1303 | else: |
Justin Thaler | cf1deae | 2018-05-25 19:35:21 -0500 | [diff] [blame] | 1304 | saveLoc = tempfile.gettempdir() |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1305 | url ='https://'+host+'/download/dump/' + str(dumpNum) |
| 1306 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 1307 | r = session.get(url, headers=jsonHeader, stream=True, verify=False, timeout=30) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1308 | if (args.dumpSaveLoc is not None): |
| 1309 | if os.path.exists(saveLoc): |
| 1310 | if saveLoc[-1] != os.path.sep: |
| 1311 | saveLoc = saveLoc + os.path.sep |
| 1312 | filename = saveLoc + host+'-dump' + str(dumpNum) + '.tar.xz' |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1313 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1314 | else: |
| 1315 | return 'Invalid save location specified' |
| 1316 | else: |
Justin Thaler | cf1deae | 2018-05-25 19:35:21 -0500 | [diff] [blame] | 1317 | filename = tempfile.gettempdir()+os.sep + host+'-dump' + str(dumpNum) + '.tar.xz' |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1318 | |
| 1319 | with open(filename, 'wb') as f: |
| 1320 | for chunk in r.iter_content(chunk_size =1024): |
| 1321 | if chunk: |
| 1322 | f.write(chunk) |
| 1323 | return 'Saved as ' + filename |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1324 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1325 | except(requests.exceptions.Timeout): |
| 1326 | return connectionErrHandler(args.json, "Timeout", None) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1327 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1328 | except(requests.exceptions.ConnectionError) as err: |
| 1329 | return connectionErrHandler(args.json, "ConnectionError", err) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1330 | |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1331 | def bmcDumpList(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1332 | """ |
| 1333 | Lists the number of dump files on the bmc |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1334 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1335 | @param host: string, the hostname or IP address of the bmc |
| 1336 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1337 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1338 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1339 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1340 | url ='https://'+host+'/xyz/openbmc_project/dump/list' |
| 1341 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 1342 | r = session.get(url, headers=jsonHeader, verify=False, timeout=20) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1343 | dumpList = json.loads(r.text) |
| 1344 | return r.text |
| 1345 | except(requests.exceptions.Timeout): |
| 1346 | return connectionErrHandler(args.json, "Timeout", None) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1347 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1348 | except(requests.exceptions.ConnectionError) as err: |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1349 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 1350 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1351 | def bmcDumpDelete(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1352 | """ |
| 1353 | Deletes BMC dump files from the bmc |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1354 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1355 | @param host: string, the hostname or IP address of the bmc |
| 1356 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1357 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1358 | @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] | 1359 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1360 | dumpList = [] |
| 1361 | successList = [] |
| 1362 | failedList = [] |
| 1363 | if args.dumpNum is not None: |
| 1364 | if isinstance(args.dumpNum, list): |
| 1365 | dumpList = args.dumpNum |
| 1366 | else: |
| 1367 | dumpList.append(args.dumpNum) |
| 1368 | for dumpNum in dumpList: |
| 1369 | url ='https://'+host+'/xyz/openbmc_project/dump/entry/'+str(dumpNum)+'/action/Delete' |
| 1370 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 1371 | r = session.post(url, headers=jsonHeader, json = {"data": []}, verify=False, timeout=30) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1372 | if r.status_code == 200: |
| 1373 | successList.append(str(dumpNum)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1374 | else: |
| 1375 | failedList.append(str(dumpNum)) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1376 | except(requests.exceptions.Timeout): |
| 1377 | return connectionErrHandler(args.json, "Timeout", None) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1378 | except(requests.exceptions.ConnectionError) as err: |
| 1379 | return connectionErrHandler(args.json, "ConnectionError", err) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1380 | output = "Successfully deleted dumps: " + ', '.join(successList) |
| 1381 | if(len(failedList)>0): |
| 1382 | output+= '\nFailed to delete dumps: ' + ', '.join(failedList) |
| 1383 | return output |
| 1384 | else: |
| 1385 | return 'You must specify an entry number to delete' |
| 1386 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1387 | def bmcDumpDeleteAll(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1388 | """ |
| 1389 | Deletes All BMC dump files from the bmc |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1390 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1391 | @param host: string, the hostname or IP address of the bmc |
| 1392 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1393 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1394 | @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] | 1395 | """ |
| 1396 | dumpResp = bmcDumpList(host, args, session) |
| 1397 | if 'FQPSPIN0000M' in dumpResp or 'FQPSPIN0001M'in dumpResp: |
| 1398 | return dumpResp |
| 1399 | dumpList = json.loads(dumpResp)['data'] |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1400 | d = vars(args) |
| 1401 | dumpNums = [] |
| 1402 | for dump in dumpList: |
| 1403 | if '/xyz/openbmc_project/dump/internal/manager' not in dump: |
| 1404 | dumpNums.append(int(dump.strip().split('/')[-1])) |
| 1405 | d['dumpNum'] = dumpNums |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1406 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1407 | return bmcDumpDelete(host, args, session) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1408 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1409 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1410 | def bmcDumpCreate(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1411 | """ |
| 1412 | Creates a bmc dump file |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1413 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1414 | @param host: string, the hostname or IP address of the bmc |
| 1415 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1416 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1417 | @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] | 1418 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1419 | url = 'https://'+host+'/xyz/openbmc_project/dump/action/CreateDump' |
| 1420 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 1421 | r = session.post(url, headers=jsonHeader, json = {"data": []}, verify=False, timeout=30) |
Matt Spinler | eae05b0 | 2019-01-24 12:59:34 -0600 | [diff] [blame] | 1422 | if(r.status_code == 200 and not args.json): |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1423 | return ('Dump successfully created') |
| 1424 | else: |
| 1425 | return ('Failed to create dump') |
| 1426 | except(requests.exceptions.Timeout): |
| 1427 | return connectionErrHandler(args.json, "Timeout", None) |
| 1428 | except(requests.exceptions.ConnectionError) as err: |
| 1429 | return connectionErrHandler(args.json, "ConnectionError", err) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1430 | |
| 1431 | |
| 1432 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1433 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1434 | def collectServiceData(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1435 | """ |
| 1436 | Collects all data needed for service from the BMC |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1437 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1438 | @param host: string, the hostname or IP address of the bmc |
| 1439 | @param args: contains additional arguments used by the collectServiceData sub command |
| 1440 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1441 | @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] | 1442 | """ |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1443 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1444 | global toolVersion |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1445 | #create a bmc dump |
| 1446 | dumpcount = len(json.loads(bmcDumpList(host, args, session))['data']) |
| 1447 | try: |
| 1448 | dumpcreated = bmcDumpCreate(host, args, session) |
| 1449 | except Exception as e: |
| 1450 | print('failed to create a bmc dump') |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1451 | |
| 1452 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1453 | #Collect Inventory |
| 1454 | try: |
| 1455 | args.silent = True |
Justin Thaler | cf1deae | 2018-05-25 19:35:21 -0500 | [diff] [blame] | 1456 | 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] | 1457 | os.makedirs(myDir) |
| 1458 | filelist = [] |
| 1459 | frulist = fruPrint(host, args, session) |
| 1460 | with open(myDir +'/inventory.txt', 'w') as f: |
| 1461 | f.write(frulist) |
| 1462 | print("Inventory collected and stored in " + myDir + "/inventory.txt") |
| 1463 | filelist.append(myDir+'/inventory.txt') |
| 1464 | except Exception as e: |
| 1465 | print("Failed to collect inventory") |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1466 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1467 | #Read all the sensor and OCC status |
| 1468 | try: |
| 1469 | sensorReadings = sensor(host, args, session) |
| 1470 | with open(myDir +'/sensorReadings.txt', 'w') as f: |
| 1471 | f.write(sensorReadings) |
| 1472 | print("Sensor readings collected and stored in " +myDir + "/sensorReadings.txt") |
| 1473 | filelist.append(myDir+'/sensorReadings.txt') |
| 1474 | except Exception as e: |
| 1475 | print("Failed to collect sensor readings") |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1476 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1477 | #Collect all of the LEDs status |
| 1478 | try: |
| 1479 | url="https://"+host+"/xyz/openbmc_project/led/enumerate" |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 1480 | leds = session.get(url, headers=jsonHeader, verify=False, timeout=20) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1481 | with open(myDir +'/ledStatus.txt', 'w') as f: |
| 1482 | f.write(leds.text) |
| 1483 | print("System LED status collected and stored in "+myDir +"/ledStatus.txt") |
| 1484 | filelist.append(myDir+'/ledStatus.txt') |
| 1485 | except Exception as e: |
| 1486 | print("Failed to collect LED status") |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1487 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1488 | #Collect the bmc logs |
| 1489 | try: |
| 1490 | sels = selPrint(host,args,session) |
| 1491 | with open(myDir +'/SELshortlist.txt', 'w') as f: |
| 1492 | f.write(str(sels)) |
| 1493 | print("sel short list collected and stored in "+myDir +"/SELshortlist.txt") |
| 1494 | filelist.append(myDir+'/SELshortlist.txt') |
| 1495 | time.sleep(2) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1496 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1497 | d = vars(args) |
| 1498 | d['json'] = True |
| 1499 | d['fullSel'] = True |
| 1500 | parsedfullsels = json.loads(selPrint(host, args, session)) |
| 1501 | d['fullEsel'] = True |
| 1502 | sortedSELs = sortSELs(parsedfullsels) |
| 1503 | with open(myDir +'/parsedSELs.txt', 'w') as f: |
| 1504 | for log in sortedSELs[0]: |
| 1505 | esel = "" |
| 1506 | parsedfullsels[sortedSELs[1][str(log)]]['timestamp'] = datetime.datetime.fromtimestamp(int(parsedfullsels[sortedSELs[1][str(log)]]['timestamp']/1000)).strftime("%Y-%m-%d %H:%M:%S") |
| 1507 | if ('raweSEL' in parsedfullsels[sortedSELs[1][str(log)]] and args.devdebug): |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1508 | esel = parsedfullsels[sortedSELs[1][str(log)]]['raweSEL'] |
| 1509 | del parsedfullsels[sortedSELs[1][str(log)]]['raweSEL'] |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1510 | f.write(json.dumps(parsedfullsels[sortedSELs[1][str(log)]],sort_keys=True, indent=4, separators=(',', ': '))) |
| 1511 | if(args.devdebug and esel != ""): |
| 1512 | f.write(parseESEL(args, esel)) |
| 1513 | print("fully parsed sels collected and stored in "+myDir +"/parsedSELs.txt") |
| 1514 | filelist.append(myDir+'/parsedSELs.txt') |
| 1515 | except Exception as e: |
| 1516 | print("Failed to collect system event logs") |
| 1517 | print(e) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1518 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1519 | #collect RAW bmc enumeration |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1520 | try: |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1521 | url="https://"+host+"/xyz/openbmc_project/enumerate" |
| 1522 | print("Attempting to get a full BMC enumeration") |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 1523 | fullDump = session.get(url, headers=jsonHeader, verify=False, timeout=120) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1524 | with open(myDir +'/bmcFullRaw.txt', 'w') as f: |
| 1525 | f.write(fullDump.text) |
| 1526 | print("RAW BMC data collected and saved into "+myDir +"/bmcFullRaw.txt") |
| 1527 | filelist.append(myDir+'/bmcFullRaw.txt') |
| 1528 | except Exception as e: |
| 1529 | print("Failed to collect bmc full enumeration") |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1530 | |
| 1531 | #collect the dump files |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1532 | waitingForNewDump = True |
| 1533 | count = 0; |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1534 | while(waitingForNewDump): |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1535 | dumpList = json.loads(bmcDumpList(host, args, session))['data'] |
| 1536 | if len(dumpList) > dumpcount: |
| 1537 | waitingForNewDump = False |
| 1538 | break; |
| 1539 | elif(count>30): |
| 1540 | print("Timed out waiting for bmc to make a new dump file. Dump space may be full.") |
| 1541 | break; |
| 1542 | else: |
| 1543 | time.sleep(2) |
| 1544 | count += 1 |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1545 | try: |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1546 | print('Collecting bmc dump files') |
| 1547 | d['dumpSaveLoc'] = myDir |
| 1548 | dumpList = json.loads(bmcDumpList(host, args, session))['data'] |
| 1549 | for dump in dumpList: |
| 1550 | if '/xyz/openbmc_project/dump/internal/manager' not in dump: |
| 1551 | d['dumpNum'] = int(dump.strip().split('/')[-1]) |
| 1552 | print('retrieving dump file ' + str(d['dumpNum'])) |
| 1553 | filename = bmcDumpRetrieve(host, args, session).split('Saved as ')[-1] |
| 1554 | filelist.append(filename) |
| 1555 | time.sleep(2) |
| 1556 | except Exception as e: |
| 1557 | print("Failed to collect bmc dump files") |
| 1558 | print(e) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1559 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1560 | #create the zip file |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1561 | try: |
Justin Thaler | cf1deae | 2018-05-25 19:35:21 -0500 | [diff] [blame] | 1562 | filename = myDir.split(tempfile.gettempdir()+os.sep)[-1] + "_" + toolVersion + '_openbmc.zip' |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1563 | zf = zipfile.ZipFile(myDir+'/' + filename, 'w') |
| 1564 | for myfile in filelist: |
| 1565 | zf.write(myfile, os.path.basename(myfile)) |
| 1566 | zf.close() |
| 1567 | except Exception as e: |
| 1568 | print("Failed to create zip file with collected information") |
| 1569 | return "data collection complete" |
| 1570 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1571 | |
| 1572 | def healthCheck(host, args, session): |
| 1573 | """ |
| 1574 | runs a health check on the platform |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1575 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1576 | @param host: string, the hostname or IP address of the bmc |
| 1577 | @param args: contains additional arguments used by the bmc sub command |
| 1578 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1579 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1580 | """ |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1581 | #check fru status and get as json to easily work through |
| 1582 | d = vars(args) |
| 1583 | useJson = d['json'] |
| 1584 | d['json'] = True |
| 1585 | d['verbose']= False |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1586 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1587 | frus = json.loads(fruStatus(host, args, session)) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1588 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1589 | hwStatus= "OK" |
| 1590 | performanceStatus = "OK" |
| 1591 | for key in frus: |
| 1592 | if frus[key]["Functional"] == "No" and frus[key]["Present"] == "Yes": |
| 1593 | hwStatus= "Degraded" |
Justin Thaler | fb9c81c | 2018-07-16 11:14:37 -0500 | [diff] [blame] | 1594 | if("power_supply" in key or "powersupply" in key): |
| 1595 | gpuCount =0 |
| 1596 | for comp in frus: |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1597 | if "gv100card" in comp: |
| 1598 | gpuCount +=1 |
| 1599 | if gpuCount > 4: |
| 1600 | hwStatus = "Critical" |
| 1601 | performanceStatus="Degraded" |
| 1602 | break; |
| 1603 | elif("fan" in key): |
| 1604 | hwStatus = "Degraded" |
| 1605 | else: |
| 1606 | performanceStatus = "Degraded" |
| 1607 | if useJson: |
| 1608 | output = {"Hardware Status": hwStatus, "Performance": performanceStatus} |
| 1609 | output = json.dumps(output, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False) |
| 1610 | else: |
| 1611 | output = ("Hardware Status: " + hwStatus + |
| 1612 | "\nPerformance: " +performanceStatus ) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1613 | |
| 1614 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1615 | #SW407886: Clear the duplicate entries |
| 1616 | #collect the dups |
| 1617 | d['devdebug'] = False |
| 1618 | sels = json.loads(selPrint(host, args, session)) |
| 1619 | logNums2Clr = [] |
| 1620 | oldestLogNum={"logNum": "bogus" ,"key" : ""} |
| 1621 | count = 0 |
| 1622 | if sels['numAlerts'] > 0: |
| 1623 | for key in sels: |
| 1624 | if "numAlerts" in key: |
| 1625 | continue |
| 1626 | try: |
| 1627 | if "slave@00:00/00:00:00:06/sbefifo1-dev0/occ1-dev0" in sels[key]['Message']: |
| 1628 | count += 1 |
| 1629 | if count > 1: |
| 1630 | #preserve first occurrence |
| 1631 | if sels[key]['timestamp'] < sels[oldestLogNum['key']]['timestamp']: |
| 1632 | oldestLogNum['key']=key |
| 1633 | oldestLogNum['logNum'] = sels[key]['logNum'] |
| 1634 | else: |
| 1635 | oldestLogNum['key']=key |
| 1636 | oldestLogNum['logNum'] = sels[key]['logNum'] |
| 1637 | logNums2Clr.append(sels[key]['logNum']) |
| 1638 | except KeyError: |
| 1639 | continue |
| 1640 | if(count >0): |
| 1641 | logNums2Clr.remove(oldestLogNum['logNum']) |
| 1642 | #delete the dups |
| 1643 | if count >1: |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1644 | data = "{\"data\": [] }" |
| 1645 | for logNum in logNums2Clr: |
| 1646 | url = "https://"+ host+ "/xyz/openbmc_project/logging/entry/"+logNum+"/action/Delete" |
| 1647 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 1648 | session.post(url, headers=jsonHeader, data=data, verify=False, timeout=30) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1649 | except(requests.exceptions.Timeout): |
| 1650 | deleteFailed = True |
| 1651 | except(requests.exceptions.ConnectionError) as err: |
| 1652 | deleteFailed = True |
| 1653 | #End of defect resolve code |
| 1654 | d['json'] = useJson |
| 1655 | return output |
| 1656 | |
| 1657 | |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1658 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1659 | def bmc(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1660 | """ |
| 1661 | handles various bmc level commands, currently bmc rebooting |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1662 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1663 | @param host: string, the hostname or IP address of the bmc |
| 1664 | @param args: contains additional arguments used by the bmc sub command |
| 1665 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1666 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1667 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1668 | if(args.type is not None): |
| 1669 | return bmcReset(host, args, session) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1670 | if(args.info): |
| 1671 | return "Not implemented at this time" |
| 1672 | |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1673 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1674 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1675 | def bmcReset(host, args, session): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1676 | """ |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1677 | controls resetting the bmc. warm reset reboots the bmc, cold reset removes the configuration and reboots. |
| 1678 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1679 | @param host: string, the hostname or IP address of the bmc |
| 1680 | @param args: contains additional arguments used by the bmcReset sub command |
| 1681 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1682 | @param args.json: boolean, if this flag is set to true, the output will be provided in json format for programmatic consumption |
| 1683 | """ |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1684 | if checkFWactivation(host, args, session): |
| 1685 | return ("BMC reset control disabled during firmware activation") |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1686 | if(args.type == "warm"): |
| 1687 | print("\nAttempting to reboot the BMC...:") |
| 1688 | url="https://"+host+"/xyz/openbmc_project/state/bmc0/attr/RequestedBMCTransition" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1689 | data = '{"data":"xyz.openbmc_project.State.BMC.Transition.Reboot"}' |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 1690 | res = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=20) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1691 | return res.text |
| 1692 | elif(args.type =="cold"): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1693 | print("\nAttempting to reboot the BMC...:") |
| 1694 | url="https://"+host+"/xyz/openbmc_project/state/bmc0/attr/RequestedBMCTransition" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1695 | data = '{"data":"xyz.openbmc_project.State.BMC.Transition.Reboot"}' |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 1696 | res = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=20) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1697 | return res.text |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 1698 | else: |
| 1699 | return "invalid command" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1700 | |
| 1701 | def gardClear(host, args, session): |
| 1702 | """ |
| 1703 | clears the gard records from the bmc |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1704 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1705 | @param host: string, the hostname or IP address of the bmc |
| 1706 | @param args: contains additional arguments used by the gardClear sub command |
| 1707 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1708 | """ |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1709 | url="https://"+host+"/org/open_power/control/gard/action/Reset" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1710 | data = '{"data":[]}' |
| 1711 | try: |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1712 | |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 1713 | res = session.post(url, headers=jsonHeader, data=data, verify=False, timeout=30) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1714 | if res.status_code == 404: |
| 1715 | return "Command not supported by this firmware version" |
| 1716 | else: |
| 1717 | return res.text |
| 1718 | except(requests.exceptions.Timeout): |
| 1719 | return connectionErrHandler(args.json, "Timeout", None) |
| 1720 | except(requests.exceptions.ConnectionError) as err: |
| 1721 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 1722 | |
| 1723 | def activateFWImage(host, args, session): |
| 1724 | """ |
| 1725 | activates a firmware image on the bmc |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1726 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1727 | @param host: string, the hostname or IP address of the bmc |
| 1728 | @param args: contains additional arguments used by the fwflash sub command |
| 1729 | @param session: the active session to use |
| 1730 | @param fwID: the unique ID of the fw image to activate |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1731 | """ |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1732 | fwID = args.imageID |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1733 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1734 | #determine the existing versions |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1735 | url="https://"+host+"/xyz/openbmc_project/software/enumerate" |
| 1736 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 1737 | resp = session.get(url, headers=jsonHeader, verify=False, timeout=30) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1738 | except(requests.exceptions.Timeout): |
| 1739 | return connectionErrHandler(args.json, "Timeout", None) |
| 1740 | except(requests.exceptions.ConnectionError) as err: |
| 1741 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 1742 | existingSoftware = json.loads(resp.text)['data'] |
| 1743 | altVersionID = '' |
| 1744 | versionType = '' |
| 1745 | imageKey = '/xyz/openbmc_project/software/'+fwID |
| 1746 | if imageKey in existingSoftware: |
| 1747 | versionType = existingSoftware[imageKey]['Purpose'] |
| 1748 | for key in existingSoftware: |
| 1749 | if imageKey == key: |
| 1750 | continue |
| 1751 | if 'Purpose' in existingSoftware[key]: |
| 1752 | if versionType == existingSoftware[key]['Purpose']: |
| 1753 | altVersionID = key.split('/')[-1] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1754 | |
| 1755 | |
| 1756 | |
| 1757 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1758 | url="https://"+host+"/xyz/openbmc_project/software/"+ fwID + "/attr/Priority" |
| 1759 | url1="https://"+host+"/xyz/openbmc_project/software/"+ altVersionID + "/attr/Priority" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1760 | data = "{\"data\": 0}" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1761 | data1 = "{\"data\": 1 }" |
| 1762 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 1763 | resp = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=30) |
| 1764 | resp1 = session.put(url1, headers=jsonHeader, data=data1, verify=False, timeout=30) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1765 | except(requests.exceptions.Timeout): |
| 1766 | return connectionErrHandler(args.json, "Timeout", None) |
| 1767 | except(requests.exceptions.ConnectionError) as err: |
| 1768 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 1769 | if(not args.json): |
| 1770 | if resp.status_code == 200 and resp1.status_code == 200: |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1771 | 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] | 1772 | else: |
| 1773 | return "Firmware activation failed." |
| 1774 | else: |
| 1775 | return resp.text + resp1.text |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1776 | |
| 1777 | def activateStatus(host, args, session): |
| 1778 | if checkFWactivation(host, args, session): |
| 1779 | return("Firmware is currently being activated. Do not reboot the BMC or start the Host OS") |
| 1780 | else: |
| 1781 | return("No firmware activations are pending") |
| 1782 | |
| 1783 | def extractFWimage(path, imageType): |
| 1784 | """ |
| 1785 | extracts the bmc image and returns information about the package |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1786 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1787 | @param path: the path and file name of the firmware image |
| 1788 | @param imageType: The type of image the user is trying to flash. Host or BMC |
| 1789 | @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] | 1790 | """ |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1791 | f = tempfile.TemporaryFile() |
| 1792 | tmpDir = tempfile.gettempdir() |
| 1793 | newImageID = "" |
| 1794 | if os.path.exists(path): |
| 1795 | try: |
| 1796 | imageFile = tarfile.open(path,'r') |
| 1797 | contents = imageFile.getmembers() |
| 1798 | for tf in contents: |
| 1799 | if 'MANIFEST' in tf.name: |
| 1800 | imageFile.extract(tf.name, path=tmpDir) |
| 1801 | with open(tempfile.gettempdir() +os.sep+ tf.name, 'r') as imageInfo: |
| 1802 | for line in imageInfo: |
| 1803 | if 'purpose' in line: |
| 1804 | purpose = line.split('=')[1] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1805 | if imageType not in purpose.split('.')[-1]: |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1806 | print('The specified image is not for ' + imageType) |
| 1807 | print('Please try again with the image for ' + imageType) |
| 1808 | return "" |
| 1809 | if 'version' == line.split('=')[0]: |
| 1810 | version = line.split('=')[1].strip().encode('utf-8') |
| 1811 | m = hashlib.sha512() |
| 1812 | m.update(version) |
| 1813 | newImageID = m.hexdigest()[:8] |
| 1814 | break |
| 1815 | try: |
| 1816 | os.remove(tempfile.gettempdir() +os.sep+ tf.name) |
| 1817 | except OSError: |
| 1818 | pass |
| 1819 | return newImageID |
| 1820 | except tarfile.ExtractError as e: |
| 1821 | print('Unable to extract information from the firmware file.') |
| 1822 | print('Ensure you have write access to the directory: ' + tmpDir) |
| 1823 | return newImageID |
| 1824 | except tarfile.TarError as e: |
| 1825 | print('This is not a valid firmware file.') |
| 1826 | return newImageID |
| 1827 | print("This is not a valid firmware file.") |
| 1828 | return newImageID |
| 1829 | else: |
| 1830 | print('The filename and path provided are not valid.') |
| 1831 | return newImageID |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1832 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1833 | def getAllFWImageIDs(fwInvDict): |
| 1834 | """ |
| 1835 | gets a list of all the firmware image IDs |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1836 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1837 | @param fwInvDict: the dictionary to search for FW image IDs |
| 1838 | @return: list containing string representation of the found image ids |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1839 | """ |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1840 | idList = [] |
| 1841 | for key in fwInvDict: |
| 1842 | if 'Version' in fwInvDict[key]: |
| 1843 | idList.append(key.split('/')[-1]) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1844 | return idList |
| 1845 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1846 | def fwFlash(host, args, session): |
| 1847 | """ |
| 1848 | updates the bmc firmware and pnor firmware |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1849 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1850 | @param host: string, the hostname or IP address of the bmc |
| 1851 | @param args: contains additional arguments used by the fwflash sub command |
| 1852 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1853 | """ |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1854 | d = vars(args) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1855 | if(args.type == 'bmc'): |
| 1856 | purp = 'BMC' |
| 1857 | else: |
| 1858 | purp = 'Host' |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1859 | |
| 1860 | #check power state of the machine. No concurrent FW updates allowed |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1861 | d['powcmd'] = 'status' |
| 1862 | powerstate = chassisPower(host, args, session) |
| 1863 | if 'Chassis Power State: On' in powerstate: |
| 1864 | 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] | 1865 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1866 | #determine the existing images on the bmc |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1867 | url="https://"+host+"/xyz/openbmc_project/software/enumerate" |
| 1868 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 1869 | resp = session.get(url, headers=jsonHeader, verify=False, timeout=30) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1870 | except(requests.exceptions.Timeout): |
| 1871 | return connectionErrHandler(args.json, "Timeout", None) |
| 1872 | except(requests.exceptions.ConnectionError) as err: |
| 1873 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 1874 | oldsoftware = json.loads(resp.text)['data'] |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1875 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1876 | #Extract the tar and get information from the manifest file |
| 1877 | newversionID = extractFWimage(args.fileloc, purp) |
| 1878 | if newversionID == "": |
| 1879 | return "Unable to verify FW image." |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1880 | |
| 1881 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1882 | #check if the new image is already on the bmc |
| 1883 | if newversionID not in getAllFWImageIDs(oldsoftware): |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1884 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1885 | #upload the file |
| 1886 | httpHeader = {'Content-Type':'application/octet-stream'} |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 1887 | httpHeader.update(xAuthHeader) |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1888 | url="https://"+host+"/upload/image" |
| 1889 | data=open(args.fileloc,'rb').read() |
| 1890 | print("Uploading file to BMC") |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1891 | try: |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1892 | resp = session.post(url, headers=httpHeader, data=data, verify=False) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1893 | except(requests.exceptions.Timeout): |
| 1894 | return connectionErrHandler(args.json, "Timeout", None) |
| 1895 | except(requests.exceptions.ConnectionError) as err: |
| 1896 | return connectionErrHandler(args.json, "ConnectionError", err) |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1897 | if resp.status_code != 200: |
| 1898 | return "Failed to upload the file to the bmc" |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1899 | else: |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1900 | print("Upload complete.") |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1901 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1902 | #verify bmc processed the image |
| 1903 | software ={} |
| 1904 | for i in range(0, 5): |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1905 | url="https://"+host+"/xyz/openbmc_project/software/enumerate" |
| 1906 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 1907 | resp = session.get(url, headers=jsonHeader, verify=False, timeout=30) |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1908 | except(requests.exceptions.Timeout): |
| 1909 | return connectionErrHandler(args.json, "Timeout", None) |
| 1910 | except(requests.exceptions.ConnectionError) as err: |
| 1911 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 1912 | software = json.loads(resp.text)['data'] |
| 1913 | #check if bmc is done processing the new image |
| 1914 | if (newversionID in getAllFWImageIDs(software)): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1915 | break |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1916 | else: |
| 1917 | time.sleep(15) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1918 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1919 | #activate the new image |
| 1920 | print("Activating new image: "+newversionID) |
| 1921 | url="https://"+host+"/xyz/openbmc_project/software/"+ newversionID + "/attr/RequestedActivation" |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1922 | data = '{"data":"xyz.openbmc_project.Software.Activation.RequestedActivations.Active"}' |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1923 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 1924 | resp = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=30) |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1925 | except(requests.exceptions.Timeout): |
| 1926 | return connectionErrHandler(args.json, "Timeout", None) |
| 1927 | except(requests.exceptions.ConnectionError) as err: |
| 1928 | return connectionErrHandler(args.json, "ConnectionError", err) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1929 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1930 | #wait for the activation to complete, timeout after ~1 hour |
| 1931 | i=0 |
| 1932 | while i < 360: |
| 1933 | url="https://"+host+"/xyz/openbmc_project/software/"+ newversionID |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1934 | data = '{"data":"xyz.openbmc_project.Software.Activation.RequestedActivations.Active"}' |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1935 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 1936 | resp = session.get(url, headers=jsonHeader, verify=False, timeout=30) |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1937 | except(requests.exceptions.Timeout): |
| 1938 | return connectionErrHandler(args.json, "Timeout", None) |
| 1939 | except(requests.exceptions.ConnectionError) as err: |
| 1940 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 1941 | fwInfo = json.loads(resp.text)['data'] |
| 1942 | if 'Activating' not in fwInfo['Activation'] and 'Activating' not in fwInfo['RequestedActivation']: |
| 1943 | print('') |
| 1944 | break |
| 1945 | else: |
| 1946 | sys.stdout.write('.') |
| 1947 | sys.stdout.flush() |
| 1948 | time.sleep(10) #check every 10 seconds |
| 1949 | return "Firmware flash and activation completed. Please reboot the bmc and then boot the host OS for the changes to take effect. " |
| 1950 | else: |
| 1951 | print("This image has been found on the bmc. Activating image: " + newversionID) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1952 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 1953 | d['imageID'] = newversionID |
| 1954 | return activateFWImage(host, args, session) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 1955 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 1956 | def getFWInventoryAttributes(rawFWInvItem, ID): |
| 1957 | """ |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1958 | gets and lists all of the firmware in the system. |
| 1959 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 1960 | @return: returns a dictionary containing the image attributes |
| 1961 | """ |
| 1962 | reqActivation = rawFWInvItem["RequestedActivation"].split('.')[-1] |
| 1963 | pendingActivation = "" |
| 1964 | if reqActivation == "None": |
| 1965 | pendingActivation = "No" |
| 1966 | else: |
| 1967 | pendingActivation = "Yes" |
| 1968 | firmwareAttr = {ID: { |
| 1969 | "Purpose": rawFWInvItem["Purpose"].split('.')[-1], |
| 1970 | "Version": rawFWInvItem["Version"], |
| 1971 | "RequestedActivation": pendingActivation, |
| 1972 | "ID": ID}} |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1973 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 1974 | if "ExtendedVersion" in rawFWInvItem: |
| 1975 | firmwareAttr[ID]['ExtendedVersion'] = rawFWInvItem['ExtendedVersion'].split(',') |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1976 | else: |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 1977 | firmwareAttr[ID]['ExtendedVersion'] = "" |
| 1978 | return firmwareAttr |
| 1979 | |
| 1980 | def parseFWdata(firmwareDict): |
| 1981 | """ |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 1982 | creates a dictionary with parsed firmware data |
| 1983 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 1984 | @return: returns a dictionary containing the image attributes |
| 1985 | """ |
| 1986 | firmwareInfoDict = {"Functional": {}, "Activated":{}, "NeedsActivated":{}} |
| 1987 | for key in firmwareDict['data']: |
| 1988 | #check for valid endpoint |
| 1989 | if "Purpose" in firmwareDict['data'][key]: |
| 1990 | id = key.split('/')[-1] |
| 1991 | if firmwareDict['data'][key]['Activation'].split('.')[-1] == "Active": |
| 1992 | fwActivated = True |
| 1993 | else: |
| 1994 | fwActivated = False |
| 1995 | if firmwareDict['data'][key]['Priority'] == 0: |
| 1996 | firmwareInfoDict['Functional'].update(getFWInventoryAttributes(firmwareDict['data'][key], id)) |
| 1997 | elif firmwareDict['data'][key]['Priority'] >= 0 and fwActivated: |
| 1998 | firmwareInfoDict['Activated'].update(getFWInventoryAttributes(firmwareDict['data'][key], id)) |
| 1999 | else: |
| 2000 | firmwareInfoDict['Activated'].update(getFWInventoryAttributes(firmwareDict['data'][key], id)) |
| 2001 | emptySections = [] |
| 2002 | for key in firmwareInfoDict: |
| 2003 | if len(firmwareInfoDict[key])<=0: |
| 2004 | emptySections.append(key) |
| 2005 | for key in emptySections: |
| 2006 | del firmwareInfoDict[key] |
| 2007 | return firmwareInfoDict |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2008 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2009 | def displayFWInvenory(firmwareInfoDict, args): |
| 2010 | """ |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2011 | gets and lists all of the firmware in the system. |
| 2012 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2013 | @return: returns a string containing all of the firmware information |
| 2014 | """ |
| 2015 | output = "" |
| 2016 | if not args.json: |
| 2017 | for key in firmwareInfoDict: |
| 2018 | for subkey in firmwareInfoDict[key]: |
| 2019 | firmwareInfoDict[key][subkey]['ExtendedVersion'] = str(firmwareInfoDict[key][subkey]['ExtendedVersion']) |
| 2020 | if not args.verbose: |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2021 | output = "---Running Images---\n" |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2022 | colNames = ["Purpose", "Version", "ID"] |
| 2023 | keylist = ["Purpose", "Version", "ID"] |
| 2024 | output += tableDisplay(keylist, colNames, firmwareInfoDict["Functional"]) |
| 2025 | if "Activated" in firmwareInfoDict: |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2026 | output += "\n---Available Images---\n" |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2027 | output += tableDisplay(keylist, colNames, firmwareInfoDict["Activated"]) |
| 2028 | if "NeedsActivated" in firmwareInfoDict: |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2029 | output += "\n---Needs Activated Images---\n" |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2030 | output += tableDisplay(keylist, colNames, firmwareInfoDict["NeedsActivated"]) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2031 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2032 | else: |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2033 | output = "---Running Images---\n" |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2034 | colNames = ["Purpose", "Version", "ID", "Pending Activation", "Extended Version"] |
| 2035 | keylist = ["Purpose", "Version", "ID", "RequestedActivation", "ExtendedVersion"] |
| 2036 | output += tableDisplay(keylist, colNames, firmwareInfoDict["Functional"]) |
| 2037 | if "Activated" in firmwareInfoDict: |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2038 | output += "\n---Available Images---\n" |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2039 | output += tableDisplay(keylist, colNames, firmwareInfoDict["Activated"]) |
| 2040 | if "NeedsActivated" in firmwareInfoDict: |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2041 | output += "\n---Needs Activated Images---\n" |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2042 | output += tableDisplay(keylist, colNames, firmwareInfoDict["NeedsActivated"]) |
| 2043 | return output |
| 2044 | else: |
| 2045 | return str(json.dumps(firmwareInfoDict, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=False)) |
| 2046 | |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2047 | def firmwareList(host, args, session): |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2048 | """ |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2049 | gets and lists all of the firmware in the system. |
| 2050 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2051 | @return: returns a string containing all of the firmware information |
| 2052 | """ |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2053 | url="https://{hostname}/xyz/openbmc_project/software/enumerate".format(hostname=host) |
| 2054 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2055 | res = session.get(url, headers=jsonHeader, verify=False, timeout=40) |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2056 | except(requests.exceptions.Timeout): |
| 2057 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2058 | firmwareDict = json.loads(res.text) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2059 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2060 | #sort the received information |
| 2061 | firmwareInfoDict = parseFWdata(firmwareDict) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2062 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 2063 | #display the information |
| 2064 | return displayFWInvenory(firmwareInfoDict, args) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2065 | |
| 2066 | |
Adriana Kobylak | 5af2fad | 2018-11-08 12:33:43 -0600 | [diff] [blame] | 2067 | def deleteFWVersion(host, args, session): |
| 2068 | """ |
| 2069 | deletes a firmware version on the BMC |
| 2070 | |
| 2071 | @param host: string, the hostname or IP address of the BMC |
| 2072 | @param args: contains additional arguments used by the fwflash sub command |
| 2073 | @param session: the active session to use |
| 2074 | @param fwID: the unique ID of the fw version to delete |
| 2075 | """ |
| 2076 | fwID = args.versionID |
| 2077 | |
| 2078 | print("Deleting version: "+fwID) |
| 2079 | url="https://"+host+"/xyz/openbmc_project/software/"+ fwID + "/action/Delete" |
Adriana Kobylak | 5af2fad | 2018-11-08 12:33:43 -0600 | [diff] [blame] | 2080 | data = "{\"data\": [] }" |
| 2081 | |
| 2082 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2083 | res = session.post(url, headers=jsonHeader, data=data, verify=False, timeout=30) |
Adriana Kobylak | 5af2fad | 2018-11-08 12:33:43 -0600 | [diff] [blame] | 2084 | except(requests.exceptions.Timeout): |
| 2085 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2086 | if res.status_code == 200: |
| 2087 | return ('The firmware version has been deleted') |
| 2088 | else: |
| 2089 | return ('Unable to delete the specified firmware version') |
| 2090 | |
| 2091 | |
Deepak Kodihalli | 22d4df0 | 2018-09-18 06:52:43 -0500 | [diff] [blame] | 2092 | def restLogging(host, args, session): |
| 2093 | """ |
| 2094 | Called by the logging function. Turns REST API logging on/off. |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2095 | |
Deepak Kodihalli | 22d4df0 | 2018-09-18 06:52:43 -0500 | [diff] [blame] | 2096 | @param host: string, the hostname or IP address of the bmc |
| 2097 | @param args: contains additional arguments used by the logging sub command |
| 2098 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2099 | @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] | 2100 | """ |
Deepak Kodihalli | 22d4df0 | 2018-09-18 06:52:43 -0500 | [diff] [blame] | 2101 | url="https://"+host+"/xyz/openbmc_project/logging/rest_api_logs/attr/Enabled" |
Deepak Kodihalli | 22d4df0 | 2018-09-18 06:52:43 -0500 | [diff] [blame] | 2102 | |
| 2103 | if(args.rest_logging == 'on'): |
| 2104 | data = '{"data": 1}' |
| 2105 | elif(args.rest_logging == 'off'): |
| 2106 | data = '{"data": 0}' |
| 2107 | else: |
| 2108 | return "Invalid logging rest_api command" |
| 2109 | |
| 2110 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2111 | res = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=30) |
Deepak Kodihalli | 22d4df0 | 2018-09-18 06:52:43 -0500 | [diff] [blame] | 2112 | except(requests.exceptions.Timeout): |
| 2113 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2114 | return res.text |
| 2115 | |
| 2116 | |
Deepak Kodihalli | 02d5328 | 2018-09-18 06:53:31 -0500 | [diff] [blame] | 2117 | def remoteLogging(host, args, session): |
| 2118 | """ |
| 2119 | 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] | 2120 | |
Deepak Kodihalli | 02d5328 | 2018-09-18 06:53:31 -0500 | [diff] [blame] | 2121 | @param host: string, the hostname or IP address of the bmc |
| 2122 | @param args: contains additional arguments used by the logging sub command |
| 2123 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2124 | @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] | 2125 | """ |
| 2126 | |
| 2127 | url="https://"+host+"/xyz/openbmc_project/logging/config/remote" |
Deepak Kodihalli | 02d5328 | 2018-09-18 06:53:31 -0500 | [diff] [blame] | 2128 | |
| 2129 | try: |
| 2130 | if(args.remote_logging == 'view'): |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2131 | res = session.get(url, headers=jsonHeader, verify=False, timeout=30) |
Deepak Kodihalli | 02d5328 | 2018-09-18 06:53:31 -0500 | [diff] [blame] | 2132 | elif(args.remote_logging == 'disable'): |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2133 | res = session.put(url + '/attr/Port', headers=jsonHeader, json = {"data": 0}, verify=False, timeout=30) |
| 2134 | res = session.put(url + '/attr/Address', headers=jsonHeader, json = {"data": ""}, verify=False, timeout=30) |
Deepak Kodihalli | 02d5328 | 2018-09-18 06:53:31 -0500 | [diff] [blame] | 2135 | else: |
| 2136 | return "Invalid logging remote_logging command" |
| 2137 | except(requests.exceptions.Timeout): |
| 2138 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2139 | return res.text |
| 2140 | |
| 2141 | |
| 2142 | def remoteLoggingConfig(host, args, session): |
| 2143 | """ |
| 2144 | Called by the logging function. Configures remote logging (rsyslog). |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2145 | |
Deepak Kodihalli | 02d5328 | 2018-09-18 06:53:31 -0500 | [diff] [blame] | 2146 | @param host: string, the hostname or IP address of the bmc |
| 2147 | @param args: contains additional arguments used by the logging sub command |
| 2148 | @param session: the active session to use |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 2149 | @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] | 2150 | """ |
| 2151 | |
| 2152 | url="https://"+host+"/xyz/openbmc_project/logging/config/remote" |
Deepak Kodihalli | 02d5328 | 2018-09-18 06:53:31 -0500 | [diff] [blame] | 2153 | |
| 2154 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2155 | res = session.put(url + '/attr/Port', headers=jsonHeader, json = {"data": args.port}, verify=False, timeout=30) |
| 2156 | res = session.put(url + '/attr/Address', headers=jsonHeader, json = {"data": args.address}, verify=False, timeout=30) |
Deepak Kodihalli | 02d5328 | 2018-09-18 06:53:31 -0500 | [diff] [blame] | 2157 | except(requests.exceptions.Timeout): |
| 2158 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2159 | return res.text |
| 2160 | |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 2161 | |
Dhruvaraj Subhashchandran | 64e7f6f | 2018-10-02 03:42:14 -0500 | [diff] [blame] | 2162 | def certificateUpdate(host, args, session): |
| 2163 | """ |
| 2164 | Called by certificate management function. update server/client/authority certificates |
| 2165 | Example: |
| 2166 | certificate update server https -f cert.pem |
| 2167 | certificate update authority ldap -f Root-CA.pem |
| 2168 | certificate update client ldap -f cert.pem |
| 2169 | @param host: string, the hostname or IP address of the bmc |
| 2170 | @param args: contains additional arguments used by the certificate update sub command |
| 2171 | @param session: the active session to use |
| 2172 | """ |
| 2173 | |
| 2174 | httpHeader = {'Content-Type': 'application/octet-stream'} |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2175 | httpHeader.update(xAuthHeader) |
Dhruvaraj Subhashchandran | 64e7f6f | 2018-10-02 03:42:14 -0500 | [diff] [blame] | 2176 | url = "https://" + host + "/xyz/openbmc_project/certs/" + args.type.lower() + "/" + args.service.lower() |
| 2177 | data = open(args.fileloc, 'rb').read() |
| 2178 | print("Updating certificate url=" + url) |
| 2179 | try: |
| 2180 | resp = session.put(url, headers=httpHeader, data=data, verify=False) |
| 2181 | except(requests.exceptions.Timeout): |
| 2182 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2183 | except(requests.exceptions.ConnectionError) as err: |
| 2184 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2185 | if resp.status_code != 200: |
| 2186 | print(resp.text) |
| 2187 | return "Failed to update the certificate" |
| 2188 | else: |
| 2189 | print("Update complete.") |
| 2190 | |
| 2191 | |
| 2192 | def certificateDelete(host, args, session): |
| 2193 | """ |
| 2194 | Called by certificate management function to delete certificate |
| 2195 | Example: |
| 2196 | certificate delete server https |
| 2197 | certificate delete authority ldap |
| 2198 | certificate delete client ldap |
| 2199 | @param host: string, the hostname or IP address of the bmc |
| 2200 | @param args: contains additional arguments used by the certificate delete sub command |
| 2201 | @param session: the active session to use |
| 2202 | """ |
| 2203 | |
| 2204 | httpHeader = {'Content-Type': 'multipart/form-data'} |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2205 | httpHeader.update(xAuthHeader) |
Dhruvaraj Subhashchandran | 64e7f6f | 2018-10-02 03:42:14 -0500 | [diff] [blame] | 2206 | url = "https://" + host + "/xyz/openbmc_project/certs/" + args.type.lower() + "/" + args.service.lower() |
| 2207 | print("Deleting certificate url=" + url) |
| 2208 | try: |
| 2209 | resp = session.delete(url, headers=httpHeader) |
| 2210 | except(requests.exceptions.Timeout): |
| 2211 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2212 | except(requests.exceptions.ConnectionError) as err: |
| 2213 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2214 | if resp.status_code != 200: |
| 2215 | print(resp.text) |
| 2216 | return "Failed to delete the certificate" |
| 2217 | else: |
| 2218 | print("Delete complete.") |
| 2219 | |
Deepak Kodihalli | 02d5328 | 2018-09-18 06:53:31 -0500 | [diff] [blame] | 2220 | |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 2221 | def enableLDAP(host, args, session): |
| 2222 | """ |
| 2223 | Called by the ldap function. Configures LDAP. |
| 2224 | |
| 2225 | @param host: string, the hostname or IP address of the bmc |
| 2226 | @param args: contains additional arguments used by the ldap subcommand |
| 2227 | @param session: the active session to use |
| 2228 | @param args.json: boolean, if this flag is set to true, the output will |
| 2229 | be provided in json format for programmatic consumption |
| 2230 | """ |
| 2231 | |
| 2232 | url='https://'+host+'/xyz/openbmc_project/user/ldap/action/CreateConfig' |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 2233 | scope = { |
| 2234 | 'sub' : 'xyz.openbmc_project.User.Ldap.Create.SearchScope.sub', |
| 2235 | 'one' : 'xyz.openbmc_project.User.Ldap.Create.SearchScope.one', |
| 2236 | 'base': 'xyz.openbmc_project.User.Ldap.Create.SearchScope.base' |
| 2237 | } |
| 2238 | |
| 2239 | serverType = { |
| 2240 | 'ActiveDirectory' : 'xyz.openbmc_project.User.Ldap.Create.Type.ActiveDirectory', |
| 2241 | 'OpenLDAP' : 'xyz.openbmc_project.User.Ldap.Create.Type.OpenLdap' |
| 2242 | } |
| 2243 | |
| 2244 | data = {"data": [args.uri, args.bindDN, args.baseDN, args.bindPassword, scope[args.scope], serverType[args.serverType]]} |
| 2245 | |
| 2246 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2247 | res = session.post(url, headers=jsonHeader, json=data, verify=False, timeout=30) |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 2248 | except(requests.exceptions.Timeout): |
| 2249 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2250 | except(requests.exceptions.ConnectionError) as err: |
| 2251 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2252 | |
| 2253 | return res.text |
| 2254 | |
| 2255 | |
| 2256 | def disableLDAP(host, args, session): |
| 2257 | """ |
| 2258 | Called by the ldap function. Deletes the LDAP Configuration. |
| 2259 | |
| 2260 | @param host: string, the hostname or IP address of the bmc |
| 2261 | @param args: contains additional arguments used by the ldap subcommand |
| 2262 | @param session: the active session to use |
| 2263 | @param args.json: boolean, if this flag is set to true, the output |
| 2264 | will be provided in json format for programmatic consumption |
| 2265 | """ |
| 2266 | |
| 2267 | url='https://'+host+'/xyz/openbmc_project/user/ldap/config/action/delete' |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 2268 | data = {"data": []} |
| 2269 | |
| 2270 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2271 | res = session.post(url, headers=jsonHeader, json=data, verify=False, timeout=30) |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 2272 | except(requests.exceptions.Timeout): |
| 2273 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2274 | except(requests.exceptions.ConnectionError) as err: |
| 2275 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2276 | |
| 2277 | return res.text |
| 2278 | |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2279 | |
| 2280 | def enableDHCP(host, args, session): |
| 2281 | |
| 2282 | """ |
| 2283 | Called by the network function. Enables DHCP. |
| 2284 | |
| 2285 | @param host: string, the hostname or IP address of the bmc |
| 2286 | @param args: contains additional arguments used by the ldap subcommand |
| 2287 | args.json: boolean, if this flag is set to true, the output |
| 2288 | will be provided in json format for programmatic consumption |
| 2289 | @param session: the active session to use |
| 2290 | """ |
| 2291 | |
| 2292 | url = "https://"+host+"/xyz/openbmc_project/network/"+args.Interface+\ |
| 2293 | "/attr/DHCPEnabled" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2294 | data = "{\"data\": 1 }" |
| 2295 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2296 | res = session.put(url, headers=jsonHeader, data=data, verify=False, |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2297 | timeout=30) |
| 2298 | |
| 2299 | except(requests.exceptions.Timeout): |
| 2300 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2301 | except(requests.exceptions.ConnectionError) as err: |
| 2302 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2303 | if res.status_code == 403: |
| 2304 | return "The specified Interface"+"("+args.Interface+")"+\ |
| 2305 | " doesn't exist" |
| 2306 | |
| 2307 | return res.text |
| 2308 | |
| 2309 | |
| 2310 | def disableDHCP(host, args, session): |
| 2311 | """ |
| 2312 | Called by the network function. Disables DHCP. |
| 2313 | |
| 2314 | @param host: string, the hostname or IP address of the bmc |
| 2315 | @param args: contains additional arguments used by the ldap subcommand |
| 2316 | args.json: boolean, if this flag is set to true, the output |
| 2317 | will be provided in json format for programmatic consumption |
| 2318 | @param session: the active session to use |
| 2319 | """ |
| 2320 | |
| 2321 | url = "https://"+host+"/xyz/openbmc_project/network/"+args.Interface+\ |
| 2322 | "/attr/DHCPEnabled" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2323 | data = "{\"data\": 0 }" |
| 2324 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2325 | res = session.put(url, headers=jsonHeader, data=data, verify=False, |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2326 | timeout=30) |
| 2327 | except(requests.exceptions.Timeout): |
| 2328 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2329 | except(requests.exceptions.ConnectionError) as err: |
| 2330 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2331 | if res.status_code == 403: |
| 2332 | return "The specified Interface"+"("+args.Interface+")"+\ |
| 2333 | " doesn't exist" |
| 2334 | return res.text |
| 2335 | |
| 2336 | |
| 2337 | def getHostname(host, args, session): |
| 2338 | |
| 2339 | """ |
| 2340 | Called by the network function. Prints out the Hostname. |
| 2341 | |
| 2342 | @param host: string, the hostname or IP address of the bmc |
| 2343 | @param args: contains additional arguments used by the ldap subcommand |
| 2344 | args.json: boolean, if this flag is set to true, the output |
| 2345 | will be provided in json format for programmatic consumption |
| 2346 | @param session: the active session to use |
| 2347 | """ |
| 2348 | |
| 2349 | url = "https://"+host+"/xyz/openbmc_project/network/config/attr/HostName" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2350 | |
| 2351 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2352 | res = session.get(url, headers=jsonHeader, verify=False, timeout=40) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2353 | except(requests.exceptions.Timeout): |
| 2354 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2355 | except(requests.exceptions.ConnectionError) as err: |
| 2356 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2357 | |
| 2358 | return res.text |
| 2359 | |
| 2360 | |
| 2361 | def setHostname(host, args, session): |
| 2362 | """ |
| 2363 | Called by the network function. Sets the Hostname. |
| 2364 | |
| 2365 | @param host: string, the hostname or IP address of the bmc |
| 2366 | @param args: contains additional arguments used by the ldap subcommand |
| 2367 | args.json: boolean, if this flag is set to true, the output |
| 2368 | will be provided in json format for programmatic consumption |
| 2369 | @param session: the active session to use |
| 2370 | """ |
| 2371 | |
| 2372 | url = "https://"+host+"/xyz/openbmc_project/network/config/attr/HostName" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2373 | |
| 2374 | data = {"data": args.HostName} |
| 2375 | |
| 2376 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2377 | res = session.put(url, headers=jsonHeader, json=data, verify=False, |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2378 | timeout=30) |
| 2379 | except(requests.exceptions.Timeout): |
| 2380 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2381 | except(requests.exceptions.ConnectionError) as err: |
| 2382 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2383 | |
| 2384 | return res.text |
| 2385 | |
| 2386 | |
| 2387 | def getDomainName(host, args, session): |
| 2388 | |
| 2389 | """ |
| 2390 | Called by the network function. Prints out the DomainName. |
| 2391 | |
| 2392 | @param host: string, the hostname or IP address of the bmc |
| 2393 | @param args: contains additional arguments used by the ldap subcommand |
| 2394 | args.json: boolean, if this flag is set to true, the output |
| 2395 | will be provided in json format for programmatic consumption |
| 2396 | @param session: the active session to use |
| 2397 | """ |
| 2398 | |
| 2399 | url = "https://"+host+"/xyz/openbmc_project/network/"+args.Interface+\ |
| 2400 | "/attr/DomainName" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2401 | |
| 2402 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2403 | res = session.get(url, headers=jsonHeader, verify=False, timeout=40) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2404 | except(requests.exceptions.Timeout): |
| 2405 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2406 | except(requests.exceptions.ConnectionError) as err: |
| 2407 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2408 | if res.status_code == 404: |
| 2409 | return "The specified Interface"+"("+args.Interface+")"+\ |
| 2410 | " doesn't exist" |
| 2411 | |
| 2412 | return res.text |
| 2413 | |
| 2414 | |
| 2415 | def setDomainName(host, args, session): |
| 2416 | """ |
| 2417 | Called by the network function. Sets the DomainName. |
| 2418 | |
| 2419 | @param host: string, the hostname or IP address of the bmc |
| 2420 | @param args: contains additional arguments used by the ldap subcommand |
| 2421 | args.json: boolean, if this flag is set to true, the output |
| 2422 | will be provided in json format for programmatic consumption |
| 2423 | @param session: the active session to use |
| 2424 | """ |
| 2425 | |
| 2426 | url = "https://"+host+"/xyz/openbmc_project/network/"+args.Interface+\ |
| 2427 | "/attr/DomainName" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2428 | |
| 2429 | data = {"data": args.DomainName.split(",")} |
| 2430 | |
| 2431 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2432 | res = session.put(url, headers=jsonHeader, json=data, verify=False, |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2433 | timeout=30) |
| 2434 | except(requests.exceptions.Timeout): |
| 2435 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2436 | except(requests.exceptions.ConnectionError) as err: |
| 2437 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2438 | if res.status_code == 403: |
| 2439 | return "The specified Interface"+"("+args.Interface+")"+\ |
| 2440 | " doesn't exist" |
| 2441 | |
| 2442 | return res.text |
| 2443 | |
| 2444 | |
| 2445 | def getMACAddress(host, args, session): |
| 2446 | |
| 2447 | """ |
| 2448 | Called by the network function. Prints out the MACAddress. |
| 2449 | |
| 2450 | @param host: string, the hostname or IP address of the bmc |
| 2451 | @param args: contains additional arguments used by the ldap subcommand |
| 2452 | args.json: boolean, if this flag is set to true, the output |
| 2453 | will be provided in json format for programmatic consumption |
| 2454 | @param session: the active session to use |
| 2455 | """ |
| 2456 | |
| 2457 | url = "https://"+host+"/xyz/openbmc_project/network/"+args.Interface+\ |
| 2458 | "/attr/MACAddress" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2459 | |
| 2460 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2461 | res = session.get(url, headers=jsonHeader, verify=False, timeout=40) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2462 | except(requests.exceptions.Timeout): |
| 2463 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2464 | except(requests.exceptions.ConnectionError) as err: |
| 2465 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2466 | if res.status_code == 404: |
| 2467 | return "The specified Interface"+"("+args.Interface+")"+\ |
| 2468 | " doesn't exist" |
| 2469 | |
| 2470 | return res.text |
| 2471 | |
| 2472 | |
| 2473 | def setMACAddress(host, args, session): |
| 2474 | """ |
| 2475 | Called by the network function. Sets the MACAddress. |
| 2476 | |
| 2477 | @param host: string, the hostname or IP address of the bmc |
| 2478 | @param args: contains additional arguments used by the ldap subcommand |
| 2479 | args.json: boolean, if this flag is set to true, the output |
| 2480 | will be provided in json format for programmatic consumption |
| 2481 | @param session: the active session to use |
| 2482 | """ |
| 2483 | |
| 2484 | url = "https://"+host+"/xyz/openbmc_project/network/"+args.Interface+\ |
| 2485 | "/attr/MACAddress" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2486 | |
| 2487 | data = {"data": args.MACAddress} |
| 2488 | |
| 2489 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2490 | res = session.put(url, headers=jsonHeader, json=data, verify=False, |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2491 | timeout=30) |
| 2492 | except(requests.exceptions.Timeout): |
| 2493 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2494 | except(requests.exceptions.ConnectionError) as err: |
| 2495 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2496 | if res.status_code == 403: |
| 2497 | return "The specified Interface"+"("+args.Interface+")"+\ |
| 2498 | " doesn't exist" |
| 2499 | |
| 2500 | return res.text |
| 2501 | |
| 2502 | |
| 2503 | def getDefaultGateway(host, args, session): |
| 2504 | |
| 2505 | """ |
| 2506 | Called by the network function. Prints out the DefaultGateway. |
| 2507 | |
| 2508 | @param host: string, the hostname or IP address of the bmc |
| 2509 | @param args: contains additional arguments used by the ldap subcommand |
| 2510 | args.json: boolean, if this flag is set to true, the output |
| 2511 | will be provided in json format for programmatic consumption |
| 2512 | @param session: the active session to use |
| 2513 | """ |
| 2514 | |
| 2515 | url = "https://"+host+"/xyz/openbmc_project/network/config/attr/DefaultGateway" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2516 | |
| 2517 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2518 | res = session.get(url, headers=jsonHeader, verify=False, timeout=40) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2519 | except(requests.exceptions.Timeout): |
| 2520 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2521 | except(requests.exceptions.ConnectionError) as err: |
| 2522 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2523 | if res.status_code == 404: |
| 2524 | return "Failed to get Default Gateway info!!" |
| 2525 | |
| 2526 | return res.text |
| 2527 | |
| 2528 | |
| 2529 | def setDefaultGateway(host, args, session): |
| 2530 | """ |
| 2531 | Called by the network function. Sets the DefaultGateway. |
| 2532 | |
| 2533 | @param host: string, the hostname or IP address of the bmc |
| 2534 | @param args: contains additional arguments used by the ldap subcommand |
| 2535 | args.json: boolean, if this flag is set to true, the output |
| 2536 | will be provided in json format for programmatic consumption |
| 2537 | @param session: the active session to use |
| 2538 | """ |
| 2539 | |
| 2540 | url = "https://"+host+"/xyz/openbmc_project/network/config/attr/DefaultGateway" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2541 | |
| 2542 | data = {"data": args.DefaultGW} |
| 2543 | |
| 2544 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2545 | res = session.put(url, headers=jsonHeader, json=data, verify=False, |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2546 | timeout=30) |
| 2547 | except(requests.exceptions.Timeout): |
| 2548 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2549 | except(requests.exceptions.ConnectionError) as err: |
| 2550 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2551 | if res.status_code == 403: |
| 2552 | return "Failed to set Default Gateway!!" |
| 2553 | |
| 2554 | return res.text |
| 2555 | |
| 2556 | |
| 2557 | def viewNWConfig(host, args, session): |
| 2558 | """ |
| 2559 | Called by the ldap function. Prints out network configured properties |
| 2560 | |
| 2561 | @param host: string, the hostname or IP address of the bmc |
| 2562 | @param args: contains additional arguments used by the ldap subcommand |
| 2563 | args.json: boolean, if this flag is set to true, the output |
| 2564 | will be provided in json format for programmatic consumption |
| 2565 | @param session: the active session to use |
| 2566 | @return returns LDAP's configured properties. |
| 2567 | """ |
| 2568 | url = "https://"+host+"/xyz/openbmc_project/network/enumerate" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2569 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2570 | res = session.get(url, headers=jsonHeader, verify=False, timeout=40) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2571 | except(requests.exceptions.Timeout): |
| 2572 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2573 | except(requests.exceptions.ConnectionError) as err: |
| 2574 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2575 | except(requests.exceptions.RequestException) as err: |
| 2576 | return connectionErrHandler(args.json, "RequestException", err) |
| 2577 | if res.status_code == 404: |
| 2578 | return "LDAP server config has not been created" |
| 2579 | return res.text |
| 2580 | |
| 2581 | |
| 2582 | def getDNS(host, args, session): |
| 2583 | |
| 2584 | """ |
| 2585 | Called by the network function. Prints out DNS servers on the interface |
| 2586 | |
| 2587 | @param host: string, the hostname or IP address of the bmc |
| 2588 | @param args: contains additional arguments used by the ldap subcommand |
| 2589 | args.json: boolean, if this flag is set to true, the output |
| 2590 | will be provided in json format for programmatic consumption |
| 2591 | @param session: the active session to use |
| 2592 | """ |
| 2593 | |
| 2594 | url = "https://" + host + "/xyz/openbmc_project/network/" + args.Interface\ |
| 2595 | + "/attr/Nameservers" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2596 | |
| 2597 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2598 | res = session.get(url, headers=jsonHeader, verify=False, timeout=40) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2599 | except(requests.exceptions.Timeout): |
| 2600 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2601 | except(requests.exceptions.ConnectionError) as err: |
| 2602 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2603 | if res.status_code == 404: |
| 2604 | return "The specified Interface"+"("+args.Interface+")" + \ |
| 2605 | " doesn't exist" |
| 2606 | |
| 2607 | return res.text |
| 2608 | |
| 2609 | |
| 2610 | def setDNS(host, args, session): |
| 2611 | """ |
| 2612 | Called by the network function. Sets DNS servers on the interface. |
| 2613 | |
| 2614 | @param host: string, the hostname or IP address of the bmc |
| 2615 | @param args: contains additional arguments used by the ldap subcommand |
| 2616 | args.json: boolean, if this flag is set to true, the output |
| 2617 | will be provided in json format for programmatic consumption |
| 2618 | @param session: the active session to use |
| 2619 | """ |
| 2620 | |
| 2621 | url = "https://" + host + "/xyz/openbmc_project/network/" + args.Interface\ |
| 2622 | + "/attr/Nameservers" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2623 | |
| 2624 | data = {"data": args.DNSServers.split(",")} |
| 2625 | |
| 2626 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2627 | res = session.put(url, headers=jsonHeader, json=data, verify=False, |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2628 | timeout=30) |
| 2629 | except(requests.exceptions.Timeout): |
| 2630 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2631 | except(requests.exceptions.ConnectionError) as err: |
| 2632 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2633 | if res.status_code == 403: |
| 2634 | return "The specified Interface"+"("+args.Interface+")" +\ |
| 2635 | " doesn't exist" |
| 2636 | |
| 2637 | return res.text |
| 2638 | |
| 2639 | |
| 2640 | def getNTP(host, args, session): |
| 2641 | |
| 2642 | """ |
| 2643 | Called by the network function. Prints out NTP servers on the interface |
| 2644 | |
| 2645 | @param host: string, the hostname or IP address of the bmc |
| 2646 | @param args: contains additional arguments used by the ldap subcommand |
| 2647 | args.json: boolean, if this flag is set to true, the output |
| 2648 | will be provided in json format for programmatic consumption |
| 2649 | @param session: the active session to use |
| 2650 | """ |
| 2651 | |
| 2652 | url = "https://" + host + "/xyz/openbmc_project/network/" + args.Interface\ |
| 2653 | + "/attr/NTPServers" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2654 | |
| 2655 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2656 | res = session.get(url, headers=jsonHeader, verify=False, timeout=40) |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2657 | except(requests.exceptions.Timeout): |
| 2658 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2659 | except(requests.exceptions.ConnectionError) as err: |
| 2660 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2661 | if res.status_code == 404: |
| 2662 | return "The specified Interface"+"("+args.Interface+")" + \ |
| 2663 | " doesn't exist" |
| 2664 | |
| 2665 | return res.text |
| 2666 | |
| 2667 | |
| 2668 | def setNTP(host, args, session): |
| 2669 | """ |
| 2670 | Called by the network function. Sets NTP servers on the interface. |
| 2671 | |
| 2672 | @param host: string, the hostname or IP address of the bmc |
| 2673 | @param args: contains additional arguments used by the ldap subcommand |
| 2674 | args.json: boolean, if this flag is set to true, the output |
| 2675 | will be provided in json format for programmatic consumption |
| 2676 | @param session: the active session to use |
| 2677 | """ |
| 2678 | |
| 2679 | url = "https://" + host + "/xyz/openbmc_project/network/" + args.Interface\ |
| 2680 | + "/attr/NTPServers" |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2681 | |
| 2682 | data = {"data": args.NTPServers.split(",")} |
| 2683 | |
| 2684 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2685 | res = session.put(url, headers=jsonHeader, json=data, verify=False, |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 2686 | timeout=30) |
| 2687 | except(requests.exceptions.Timeout): |
| 2688 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2689 | except(requests.exceptions.ConnectionError) as err: |
| 2690 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2691 | if res.status_code == 403: |
| 2692 | return "The specified Interface"+"("+args.Interface+")" +\ |
| 2693 | " doesn't exist" |
| 2694 | |
| 2695 | return res.text |
| 2696 | |
| 2697 | |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 2698 | def addIP(host, args, session): |
| 2699 | """ |
| 2700 | Called by the network function. Configures IP address on given interface |
| 2701 | |
| 2702 | @param host: string, the hostname or IP address of the bmc |
| 2703 | @param args: contains additional arguments used by the ldap subcommand |
| 2704 | args.json: boolean, if this flag is set to true, the output |
| 2705 | will be provided in json format for programmatic consumption |
| 2706 | @param session: the active session to use |
| 2707 | """ |
| 2708 | |
| 2709 | url = "https://" + host + "/xyz/openbmc_project/network/" + args.Interface\ |
| 2710 | + "/action/IP" |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 2711 | protocol = { |
| 2712 | 'ipv4': 'xyz.openbmc_project.Network.IP.Protocol.IPv4', |
| 2713 | 'ipv6': 'xyz.openbmc_project.Network.IP.Protocol.IPv6' |
| 2714 | } |
| 2715 | |
| 2716 | data = {"data": [protocol[args.type], args.address, int(args.prefixLength), |
| 2717 | args.gateway]} |
| 2718 | |
| 2719 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2720 | res = session.post(url, headers=jsonHeader, json=data, verify=False, |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 2721 | timeout=30) |
| 2722 | except(requests.exceptions.Timeout): |
| 2723 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2724 | except(requests.exceptions.ConnectionError) as err: |
| 2725 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2726 | if res.status_code == 404: |
| 2727 | return "The specified Interface" + "(" + args.Interface + ")" +\ |
| 2728 | " doesn't exist" |
| 2729 | |
| 2730 | return res.text |
| 2731 | |
| 2732 | |
| 2733 | def getIP(host, args, session): |
| 2734 | """ |
| 2735 | Called by the network function. Prints out IP address of given interface |
| 2736 | |
| 2737 | @param host: string, the hostname or IP address of the bmc |
| 2738 | @param args: contains additional arguments used by the ldap subcommand |
| 2739 | args.json: boolean, if this flag is set to true, the output |
| 2740 | will be provided in json format for programmatic consumption |
| 2741 | @param session: the active session to use |
| 2742 | """ |
| 2743 | |
| 2744 | url = "https://" + host+"/xyz/openbmc_project/network/" + args.Interface +\ |
| 2745 | "/enumerate" |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 2746 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2747 | res = session.get(url, headers=jsonHeader, verify=False, timeout=40) |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 2748 | except(requests.exceptions.Timeout): |
| 2749 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2750 | except(requests.exceptions.ConnectionError) as err: |
| 2751 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2752 | if res.status_code == 404: |
| 2753 | return "The specified Interface" + "(" + args.Interface + ")" +\ |
| 2754 | " doesn't exist" |
| 2755 | |
| 2756 | return res.text |
| 2757 | |
| 2758 | |
| 2759 | def deleteIP(host, args, session): |
| 2760 | """ |
| 2761 | Called by the network function. Deletes the IP address from given Interface |
| 2762 | |
| 2763 | @param host: string, the hostname or IP address of the bmc |
| 2764 | @param args: contains additional arguments used by the ldap subcommand |
| 2765 | @param session: the active session to use |
| 2766 | @param args.json: boolean, if this flag is set to true, the output |
| 2767 | will be provided in json format for programmatic consumption |
| 2768 | """ |
| 2769 | |
| 2770 | url = "https://"+host+"/xyz/openbmc_project/network/" + args.Interface+\ |
| 2771 | "/enumerate" |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 2772 | data = {"data": []} |
| 2773 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2774 | res = session.get(url, headers=jsonHeader, verify=False, timeout=40) |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 2775 | except(requests.exceptions.Timeout): |
| 2776 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2777 | except(requests.exceptions.ConnectionError) as err: |
| 2778 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2779 | if res.status_code == 404: |
| 2780 | return "The specified Interface" + "(" + args.Interface + ")" +\ |
| 2781 | " doesn't exist" |
| 2782 | objDict = json.loads(res.text) |
| 2783 | if not objDict['data']: |
| 2784 | return "No object found for given address on given Interface" |
| 2785 | |
| 2786 | for obj in objDict['data']: |
| 2787 | if args.address in objDict['data'][obj]['Address']: |
| 2788 | url = "https://"+host+obj+"/action/delete" |
| 2789 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2790 | res = session.post(url, headers=jsonHeader, json=data, |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 2791 | verify=False, timeout=30) |
| 2792 | except(requests.exceptions.Timeout): |
| 2793 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2794 | except(requests.exceptions.ConnectionError) as err: |
| 2795 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2796 | return res.text |
| 2797 | else: |
| 2798 | continue |
| 2799 | return "No object found for given address on given Interface" |
| 2800 | |
| 2801 | |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 2802 | def addVLAN(host, args, session): |
| 2803 | """ |
| 2804 | Called by the network function. Creates VLAN on given interface. |
| 2805 | |
| 2806 | @param host: string, the hostname or IP address of the bmc |
| 2807 | @param args: contains additional arguments used by the ldap subcommand |
| 2808 | args.json: boolean, if this flag is set to true, the output |
| 2809 | will be provided in json format for programmatic consumption |
| 2810 | @param session: the active session to use |
| 2811 | """ |
| 2812 | |
| 2813 | url = "https://" + host+"/xyz/openbmc_project/network/action/VLAN" |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 2814 | |
| 2815 | data = {"data": [args.Interface,args.Identifier]} |
| 2816 | |
| 2817 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2818 | res = session.post(url, headers=jsonHeader, json=data, verify=False, |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 2819 | timeout=30) |
| 2820 | except(requests.exceptions.Timeout): |
| 2821 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2822 | except(requests.exceptions.ConnectionError) as err: |
| 2823 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2824 | if res.status_code == 400: |
| 2825 | return "The specified Interface" + "(" + args.Interface + ")" +\ |
| 2826 | " doesn't exist" |
| 2827 | |
| 2828 | return res.text |
| 2829 | |
| 2830 | |
| 2831 | def deleteVLAN(host, args, session): |
| 2832 | """ |
| 2833 | Called by the network function. Creates VLAN on given interface. |
| 2834 | |
| 2835 | @param host: string, the hostname or IP address of the bmc |
| 2836 | @param args: contains additional arguments used by the ldap subcommand |
| 2837 | args.json: boolean, if this flag is set to true, the output |
| 2838 | will be provided in json format for programmatic consumption |
| 2839 | @param session: the active session to use |
| 2840 | """ |
| 2841 | |
| 2842 | url = "https://" + host+"/xyz/openbmc_project/network/"+args.Interface+"/action/delete" |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 2843 | data = {"data": []} |
| 2844 | |
| 2845 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2846 | res = session.post(url, headers=jsonHeader, json=data, verify=False, timeout=30) |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 2847 | except(requests.exceptions.Timeout): |
| 2848 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2849 | except(requests.exceptions.ConnectionError) as err: |
| 2850 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2851 | if res.status_code == 404: |
| 2852 | return "The specified VLAN"+"("+args.Interface+"_"+args.Identifier\ |
| 2853 | +")" +" doesn't exist" |
| 2854 | |
| 2855 | return res.text |
| 2856 | |
| 2857 | |
| 2858 | def viewDHCPConfig(host, args, session): |
| 2859 | """ |
| 2860 | Called by the network function. Shows DHCP configured Properties. |
| 2861 | |
| 2862 | @param host: string, the hostname or IP address of the bmc |
| 2863 | @param args: contains additional arguments used by the ldap subcommand |
| 2864 | args.json: boolean, if this flag is set to true, the output |
| 2865 | will be provided in json format for programmatic consumption |
| 2866 | @param session: the active session to use |
| 2867 | """ |
| 2868 | |
| 2869 | url="https://"+host+"/xyz/openbmc_project/network/config/dhcp" |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 2870 | |
| 2871 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2872 | res = session.get(url, headers=jsonHeader, verify=False, timeout=30) |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 2873 | except(requests.exceptions.Timeout): |
| 2874 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2875 | except(requests.exceptions.ConnectionError) as err: |
| 2876 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2877 | |
| 2878 | return res.text |
| 2879 | |
| 2880 | |
| 2881 | def configureDHCP(host, args, session): |
| 2882 | """ |
| 2883 | Called by the network function. Configures/updates DHCP Properties. |
| 2884 | |
| 2885 | @param host: string, the hostname or IP address of the bmc |
| 2886 | @param args: contains additional arguments used by the ldap subcommand |
| 2887 | args.json: boolean, if this flag is set to true, the output |
| 2888 | will be provided in json format for programmatic consumption |
| 2889 | @param session: the active session to use |
| 2890 | """ |
| 2891 | |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 2892 | |
| 2893 | try: |
| 2894 | url="https://"+host+"/xyz/openbmc_project/network/config/dhcp" |
| 2895 | if(args.DNSEnabled == True): |
| 2896 | data = '{"data": 1}' |
| 2897 | else: |
| 2898 | data = '{"data": 0}' |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2899 | res = session.put(url + '/attr/DNSEnabled', headers=jsonHeader, |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 2900 | data=data, verify=False, timeout=30) |
| 2901 | if(args.HostNameEnabled == True): |
| 2902 | data = '{"data": 1}' |
| 2903 | else: |
| 2904 | data = '{"data": 0}' |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2905 | res = session.put(url + '/attr/HostNameEnabled', headers=jsonHeader, |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 2906 | data=data, verify=False, timeout=30) |
| 2907 | if(args.NTPEnabled == True): |
| 2908 | data = '{"data": 1}' |
| 2909 | else: |
| 2910 | data = '{"data": 0}' |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2911 | res = session.put(url + '/attr/NTPEnabled', headers=jsonHeader, |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 2912 | data=data, verify=False, timeout=30) |
| 2913 | if(args.SendHostNameEnabled == True): |
| 2914 | data = '{"data": 1}' |
| 2915 | else: |
| 2916 | data = '{"data": 0}' |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2917 | res = session.put(url + '/attr/SendHostNameEnabled', headers=jsonHeader, |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 2918 | data=data, verify=False, timeout=30) |
| 2919 | except(requests.exceptions.Timeout): |
| 2920 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2921 | except(requests.exceptions.ConnectionError) as err: |
| 2922 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2923 | |
| 2924 | return res.text |
| 2925 | |
| 2926 | |
| 2927 | def nwReset(host, args, session): |
| 2928 | |
| 2929 | """ |
| 2930 | Called by the network function. Resets networks setting to factory defaults. |
| 2931 | |
| 2932 | @param host: string, the hostname or IP address of the bmc |
| 2933 | @param args: contains additional arguments used by the ldap subcommand |
| 2934 | args.json: boolean, if this flag is set to true, the output |
| 2935 | will be provided in json format for programmatic consumption |
| 2936 | @param session: the active session to use |
| 2937 | """ |
| 2938 | |
| 2939 | url = "https://"+host+"/xyz/openbmc_project/network/action/Reset" |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 2940 | data = '{"data":[] }' |
| 2941 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2942 | res = session.post(url, headers=jsonHeader, data=data, verify=False, |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 2943 | timeout=30) |
| 2944 | |
| 2945 | except(requests.exceptions.Timeout): |
| 2946 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2947 | except(requests.exceptions.ConnectionError) as err: |
| 2948 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2949 | |
| 2950 | return res.text |
| 2951 | |
| 2952 | |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 2953 | def createPrivilegeMapping(host, args, session): |
| 2954 | """ |
| 2955 | Called by the ldap function. Creates the group and the privilege mapping. |
| 2956 | |
| 2957 | @param host: string, the hostname or IP address of the bmc |
| 2958 | @param args: contains additional arguments used by the ldap subcommand |
| 2959 | @param session: the active session to use |
| 2960 | @param args.json: boolean, if this flag is set to true, the output |
| 2961 | will be provided in json format for programmatic consumption |
| 2962 | """ |
| 2963 | |
| 2964 | url = 'https://'+host+'/xyz/openbmc_project/user/ldap/action/Create' |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 2965 | |
| 2966 | data = {"data": [args.groupName,args.privilege]} |
| 2967 | |
| 2968 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2969 | res = session.post(url, headers=jsonHeader, json = data, verify=False, timeout=30) |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 2970 | except(requests.exceptions.Timeout): |
| 2971 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2972 | except(requests.exceptions.ConnectionError) as err: |
| 2973 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2974 | return res.text |
| 2975 | |
| 2976 | def listPrivilegeMapping(host, args, session): |
| 2977 | """ |
| 2978 | Called by the ldap function. Lists the group and the privilege mapping. |
| 2979 | |
| 2980 | @param host: string, the hostname or IP address of the bmc |
| 2981 | @param args: contains additional arguments used by the ldap subcommand |
| 2982 | @param session: the active session to use |
| 2983 | @param args.json: boolean, if this flag is set to true, the output |
| 2984 | will be provided in json format for programmatic consumption |
| 2985 | """ |
| 2986 | url = 'https://'+host+'/xyz/openbmc_project/user/ldap/enumerate' |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 2987 | data = {"data": []} |
| 2988 | |
| 2989 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 2990 | res = session.get(url, headers=jsonHeader, json = data, verify=False, timeout=30) |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 2991 | except(requests.exceptions.Timeout): |
| 2992 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 2993 | except(requests.exceptions.ConnectionError) as err: |
| 2994 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 2995 | return res.text |
| 2996 | |
| 2997 | def deletePrivilegeMapping(host, args, session): |
| 2998 | """ |
| 2999 | Called by the ldap function. Deletes the mapping associated with the group. |
| 3000 | |
| 3001 | @param host: string, the hostname or IP address of the bmc |
| 3002 | @param args: contains additional arguments used by the ldap subcommand |
| 3003 | @param session: the active session to use |
| 3004 | @param args.json: boolean, if this flag is set to true, the output |
| 3005 | will be provided in json format for programmatic consumption |
| 3006 | """ |
| 3007 | (ldapNameSpaceObjects) = listPrivilegeMapping(host, args, session) |
| 3008 | ldapNameSpaceObjects = json.loads(ldapNameSpaceObjects)["data"] |
| 3009 | path = '' |
| 3010 | |
| 3011 | # not interested in the config objet |
| 3012 | ldapNameSpaceObjects.pop('/xyz/openbmc_project/user/ldap/config', None) |
| 3013 | |
| 3014 | # search for the object having the mapping for the given group |
| 3015 | for key,value in ldapNameSpaceObjects.items(): |
| 3016 | if value['GroupName'] == args.groupName: |
| 3017 | path = key |
| 3018 | break |
| 3019 | |
| 3020 | if path == '': |
| 3021 | return "No privilege mapping found for this group." |
| 3022 | |
| 3023 | # delete the object |
| 3024 | url = 'https://'+host+path+'/action/delete' |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 3025 | data = {"data": []} |
| 3026 | |
| 3027 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 3028 | res = session.post(url, headers=jsonHeader, json = data, verify=False, timeout=30) |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 3029 | except(requests.exceptions.Timeout): |
| 3030 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3031 | except(requests.exceptions.ConnectionError) as err: |
| 3032 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3033 | return res.text |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 3034 | |
Sivas SRR | 7883527 | 2018-11-27 05:27:19 -0600 | [diff] [blame] | 3035 | def deleteAllPrivilegeMapping(host, args, session): |
| 3036 | """ |
| 3037 | Called by the ldap function. Deletes all the privilege mapping and group defined. |
| 3038 | @param host: string, the hostname or IP address of the bmc |
| 3039 | @param args: contains additional arguments used by the ldap subcommand |
| 3040 | @param session: the active session to use |
| 3041 | @param args.json: boolean, if this flag is set to true, the output |
| 3042 | will be provided in json format for programmatic consumption |
| 3043 | """ |
| 3044 | ldapNameSpaceObjects = listPrivilegeMapping(host, args, session) |
| 3045 | ldapNameSpaceObjects = json.loads(ldapNameSpaceObjects)["data"] |
| 3046 | path = '' |
| 3047 | |
| 3048 | # Remove the config object. |
| 3049 | ldapNameSpaceObjects.pop('/xyz/openbmc_project/user/ldap/config', None) |
Sivas SRR | 7883527 | 2018-11-27 05:27:19 -0600 | [diff] [blame] | 3050 | data = {"data": []} |
| 3051 | |
| 3052 | try: |
| 3053 | # search for GroupName property and delete if it is available. |
| 3054 | for path in ldapNameSpaceObjects.keys(): |
| 3055 | # delete the object |
| 3056 | url = 'https://'+host+path+'/action/delete' |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 3057 | res = session.post(url, headers=jsonHeader, json = data, verify=False, timeout=30) |
Sivas SRR | 7883527 | 2018-11-27 05:27:19 -0600 | [diff] [blame] | 3058 | except(requests.exceptions.Timeout): |
| 3059 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3060 | except(requests.exceptions.ConnectionError) as err: |
| 3061 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3062 | return res.text |
| 3063 | |
Nagaraju Goruganti | 7d1fe17 | 2018-11-13 06:09:29 -0600 | [diff] [blame] | 3064 | def viewLDAPConfig(host, args, session): |
| 3065 | """ |
| 3066 | Called by the ldap function. Prints out LDAP's configured properties |
| 3067 | |
| 3068 | @param host: string, the hostname or IP address of the bmc |
| 3069 | @param args: contains additional arguments used by the ldap subcommand |
| 3070 | args.json: boolean, if this flag is set to true, the output |
| 3071 | will be provided in json format for programmatic consumption |
| 3072 | @param session: the active session to use |
| 3073 | @return returns LDAP's configured properties. |
| 3074 | """ |
| 3075 | url = "https://"+host+"/xyz/openbmc_project/user/ldap/config" |
Nagaraju Goruganti | 7d1fe17 | 2018-11-13 06:09:29 -0600 | [diff] [blame] | 3076 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 3077 | res = session.get(url, headers=jsonHeader, verify=False, timeout=40) |
Nagaraju Goruganti | 7d1fe17 | 2018-11-13 06:09:29 -0600 | [diff] [blame] | 3078 | except(requests.exceptions.Timeout): |
| 3079 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3080 | except(requests.exceptions.ConnectionError) as err: |
| 3081 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3082 | except(requests.exceptions.RequestException) as err: |
| 3083 | return connectionErrHandler(args.json, "RequestException", err) |
| 3084 | if res.status_code == 404: |
| 3085 | return "LDAP server config has not been created" |
| 3086 | return res.text |
| 3087 | |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 3088 | def str2bool(v): |
| 3089 | if v.lower() in ('yes', 'true', 't', 'y', '1'): |
| 3090 | return True |
| 3091 | elif v.lower() in ('no', 'false', 'f', 'n', '0'): |
| 3092 | return False |
| 3093 | else: |
| 3094 | raise argparse.ArgumentTypeError('Boolean value expected.') |
Nagaraju Goruganti | 7d1fe17 | 2018-11-13 06:09:29 -0600 | [diff] [blame] | 3095 | |
Matt Spinler | 7d426c2 | 2018-09-24 14:42:07 -0500 | [diff] [blame] | 3096 | def localUsers(host, args, session): |
| 3097 | """ |
| 3098 | Enables and disables local BMC users. |
| 3099 | |
| 3100 | @param host: string, the hostname or IP address of the bmc |
| 3101 | @param args: contains additional arguments used by the logging sub command |
| 3102 | @param session: the active session to use |
| 3103 | """ |
| 3104 | |
Matt Spinler | 7d426c2 | 2018-09-24 14:42:07 -0500 | [diff] [blame] | 3105 | url="https://{hostname}/xyz/openbmc_project/user/enumerate".format(hostname=host) |
| 3106 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 3107 | res = session.get(url, headers=jsonHeader, verify=False, timeout=40) |
Matt Spinler | 7d426c2 | 2018-09-24 14:42:07 -0500 | [diff] [blame] | 3108 | except(requests.exceptions.Timeout): |
| 3109 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3110 | usersDict = json.loads(res.text) |
| 3111 | |
| 3112 | if not usersDict['data']: |
| 3113 | return "No users found" |
| 3114 | |
| 3115 | output = "" |
| 3116 | for user in usersDict['data']: |
Matt Spinler | 015adc2 | 2018-10-23 14:30:19 -0500 | [diff] [blame] | 3117 | |
| 3118 | # Skip LDAP and another non-local users |
| 3119 | if 'UserEnabled' not in usersDict['data'][user]: |
| 3120 | continue |
| 3121 | |
Matt Spinler | 7d426c2 | 2018-09-24 14:42:07 -0500 | [diff] [blame] | 3122 | name = user.split('/')[-1] |
| 3123 | url = "https://{hostname}{user}/attr/UserEnabled".format(hostname=host, user=user) |
| 3124 | |
| 3125 | if args.local_users == "queryenabled": |
| 3126 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 3127 | res = session.get(url, headers=jsonHeader,verify=False, timeout=30) |
Matt Spinler | 7d426c2 | 2018-09-24 14:42:07 -0500 | [diff] [blame] | 3128 | except(requests.exceptions.Timeout): |
| 3129 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3130 | |
| 3131 | result = json.loads(res.text) |
| 3132 | output += ("User: {name} Enabled: {result}\n").format(name=name, result=result['data']) |
| 3133 | |
| 3134 | elif args.local_users in ["enableall", "disableall"]: |
| 3135 | action = "" |
| 3136 | if args.local_users == "enableall": |
| 3137 | data = '{"data": true}' |
| 3138 | action = "Enabling" |
| 3139 | else: |
| 3140 | data = '{"data": false}' |
| 3141 | action = "Disabling" |
| 3142 | |
| 3143 | output += "{action} {name}\n".format(action=action, name=name) |
| 3144 | |
| 3145 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 3146 | resp = session.put(url, headers=jsonHeader, data=data, verify=False, timeout=30) |
Matt Spinler | 7d426c2 | 2018-09-24 14:42:07 -0500 | [diff] [blame] | 3147 | except(requests.exceptions.Timeout): |
| 3148 | return connectionErrHandler(args.json, "Timeout", None) |
| 3149 | except(requests.exceptions.ConnectionError) as err: |
| 3150 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3151 | else: |
| 3152 | return "Invalid local users argument" |
| 3153 | |
| 3154 | return output |
| 3155 | |
Marri Devender Rao | 2c2a516 | 2018-11-05 08:57:11 -0600 | [diff] [blame] | 3156 | def setPassword(host, args, session): |
| 3157 | """ |
| 3158 | Set local user password |
| 3159 | @param host: string, the hostname or IP address of the bmc |
| 3160 | @param args: contains additional arguments used by the logging sub |
| 3161 | command |
| 3162 | @param session: the active session to use |
| 3163 | @param args.json: boolean, if this flag is set to true, the output |
| 3164 | will be provided in json format for programmatic consumption |
| 3165 | @return: Session object |
| 3166 | """ |
| 3167 | url = "https://" + host + "/xyz/openbmc_project/user/" + args.user + \ |
| 3168 | "/action/SetPassword" |
Marri Devender Rao | 2c2a516 | 2018-11-05 08:57:11 -0600 | [diff] [blame] | 3169 | try: |
Matt Spinler | 220c3c4 | 2019-01-04 15:09:29 -0600 | [diff] [blame] | 3170 | res = session.post(url, headers=jsonHeader, |
Marri Devender Rao | 2c2a516 | 2018-11-05 08:57:11 -0600 | [diff] [blame] | 3171 | json={"data": [args.password]}, verify=False, |
| 3172 | timeout=30) |
| 3173 | except(requests.exceptions.Timeout): |
| 3174 | return(connectionErrHandler(args.json, "Timeout", None)) |
| 3175 | except(requests.exceptions.ConnectionError) as err: |
| 3176 | return connectionErrHandler(args.json, "ConnectionError", err) |
| 3177 | except(requests.exceptions.RequestException) as err: |
| 3178 | return connectionErrHandler(args.json, "RequestException", err) |
| 3179 | return res.text |
| 3180 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3181 | def createCommandParser(): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 3182 | """ |
| 3183 | 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] | 3184 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 3185 | @return: returns the parser for the command line |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3186 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3187 | parser = argparse.ArgumentParser(description='Process arguments') |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 3188 | parser.add_argument("-H", "--host", help='A hostname or IP for the BMC') |
| 3189 | parser.add_argument("-U", "--user", help='The username to login with') |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3190 | group = parser.add_mutually_exclusive_group() |
| 3191 | group.add_argument("-A", "--askpw", action='store_true', help='prompt for password') |
| 3192 | group.add_argument("-P", "--PW", help='Provide the password in-line') |
| 3193 | parser.add_argument('-j', '--json', action='store_true', help='output json data only') |
| 3194 | parser.add_argument('-t', '--policyTableLoc', help='The location of the policy table to parse alerts') |
| 3195 | parser.add_argument('-c', '--CerFormat', action='store_true', help=argparse.SUPPRESS) |
| 3196 | parser.add_argument('-T', '--procTime', action='store_true', help= argparse.SUPPRESS) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 3197 | parser.add_argument('-V', '--version', action='store_true', help='Display the version number of the openbmctool') |
| 3198 | 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] | 3199 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3200 | #fru command |
| 3201 | parser_inv = subparsers.add_parser("fru", help='Work with platform inventory') |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 3202 | 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] | 3203 | inv_subparser.required = True |
| 3204 | #fru print |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3205 | inv_print = inv_subparser.add_parser("print", help="prints out a list of all FRUs") |
| 3206 | inv_print.set_defaults(func=fruPrint) |
| 3207 | #fru list [0....n] |
| 3208 | inv_list = inv_subparser.add_parser("list", help="print out details on selected FRUs. Specifying no items will list the entire inventory") |
| 3209 | inv_list.add_argument('items', nargs='?', help="print out details on selected FRUs. Specifying no items will list the entire inventory") |
| 3210 | inv_list.set_defaults(func=fruList) |
| 3211 | #fru status |
| 3212 | 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] | 3213 | inv_status.add_argument('-v', '--verbose', action='store_true', help='Verbose output') |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3214 | inv_status.set_defaults(func=fruStatus) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3215 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3216 | #sensors command |
| 3217 | parser_sens = subparsers.add_parser("sensors", help="Work with platform sensors") |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 3218 | 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] | 3219 | sens_subparser.required = True |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3220 | #sensor print |
| 3221 | sens_print= sens_subparser.add_parser('print', help="prints out a list of all Sensors.") |
| 3222 | sens_print.set_defaults(func=sensor) |
| 3223 | #sensor list[0...n] |
| 3224 | sens_list=sens_subparser.add_parser("list", help="Lists all Sensors in the platform. Specify a sensor for full details. ") |
| 3225 | sens_list.add_argument("sensNum", nargs='?', help="The Sensor number to get full details on" ) |
| 3226 | sens_list.set_defaults(func=sensor) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3227 | |
| 3228 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3229 | #sel command |
| 3230 | parser_sel = subparsers.add_parser("sel", help="Work with platform alerts") |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 3231 | 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] | 3232 | sel_subparser.required = True |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3233 | #sel print |
| 3234 | sel_print = sel_subparser.add_parser("print", help="prints out a list of all sels in a condensed list") |
| 3235 | sel_print.add_argument('-d', '--devdebug', action='store_true', help=argparse.SUPPRESS) |
| 3236 | sel_print.add_argument('-v', '--verbose', action='store_true', help="Changes the output to being very verbose") |
| 3237 | sel_print.add_argument('-f', '--fileloc', help='Parse a file instead of the BMC output') |
| 3238 | sel_print.set_defaults(func=selPrint) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3239 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3240 | #sel list |
| 3241 | 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") |
| 3242 | sel_list.add_argument("selNum", nargs='?', type=int, help="The SEL entry to get details on") |
| 3243 | sel_list.set_defaults(func=selList) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3244 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3245 | sel_get = sel_subparser.add_parser("get", help="Gets the verbose details of a specified SEL entry") |
| 3246 | sel_get.add_argument('selNum', type=int, help="the number of the SEL entry to get") |
| 3247 | sel_get.set_defaults(func=selList) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3248 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3249 | sel_clear = sel_subparser.add_parser("clear", help="Clears all entries from the SEL") |
| 3250 | sel_clear.set_defaults(func=selClear) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3251 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3252 | 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] | 3253 | sel_setResolved.add_argument('-n', '--selNum', type=int, help="the number of the SEL entry to resolve") |
| 3254 | sel_ResolveAll_sub = sel_setResolved.add_subparsers(title='subcommands', description='valid subcommands',help="sub-command help", dest='command') |
| 3255 | sel_ResolveAll = sel_ResolveAll_sub.add_parser('all', help='Resolve all SEL entries') |
| 3256 | sel_ResolveAll.set_defaults(func=selResolveAll) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3257 | sel_setResolved.set_defaults(func=selSetResolved) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3258 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3259 | 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] | 3260 | 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] | 3261 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3262 | parser_chassis.add_argument('status', action='store_true', help='Returns the current status of the platform') |
| 3263 | parser_chassis.set_defaults(func=chassis) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3264 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3265 | 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] | 3266 | 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] | 3267 | parser_chasPower.set_defaults(func=chassisPower) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3268 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3269 | #control the chassis identify led |
| 3270 | parser_chasIdent = chas_sub.add_parser("identify", help="Control the chassis identify led") |
| 3271 | parser_chasIdent.add_argument('identcmd', choices=['on', 'off', 'status'], help='The control option for the led: on, off, blink, status') |
| 3272 | parser_chasIdent.set_defaults(func=chassisIdent) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3273 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3274 | #collect service data |
| 3275 | parser_servData = subparsers.add_parser("collect_service_data", help="Collect all bmc data needed for service") |
| 3276 | parser_servData.add_argument('-d', '--devdebug', action='store_true', help=argparse.SUPPRESS) |
| 3277 | parser_servData.set_defaults(func=collectServiceData) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3278 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 3279 | #system quick health check |
| 3280 | parser_healthChk = subparsers.add_parser("health_check", help="Work with platform sensors") |
| 3281 | parser_healthChk.set_defaults(func=healthCheck) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3282 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3283 | #work with bmc dumps |
| 3284 | parser_bmcdump = subparsers.add_parser("dump", help="Work with bmc dump files") |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 3285 | 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] | 3286 | bmcDump_sub.required = True |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3287 | dump_Create = bmcDump_sub.add_parser('create', help="Create a bmc dump") |
| 3288 | dump_Create.set_defaults(func=bmcDumpCreate) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3289 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3290 | dump_list = bmcDump_sub.add_parser('list', help="list all bmc dump files") |
| 3291 | dump_list.set_defaults(func=bmcDumpList) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3292 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3293 | parserdumpdelete = bmcDump_sub.add_parser('delete', help="Delete bmc dump files") |
| 3294 | 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] | 3295 | parserdumpdelete.set_defaults(func=bmcDumpDelete) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3296 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 3297 | 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] | 3298 | deleteAllDumps = bmcDumpDelsub.add_parser('all', help='Delete all bmc dump files') |
| 3299 | deleteAllDumps.set_defaults(func=bmcDumpDeleteAll) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3300 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3301 | parser_dumpretrieve = bmcDump_sub.add_parser('retrieve', help='Retrieve a dump file') |
| 3302 | parser_dumpretrieve.add_argument("dumpNum", type=int, help="The Dump entry to delete") |
| 3303 | parser_dumpretrieve.add_argument("-s", "--dumpSaveLoc", help="The location to save the bmc dump file") |
| 3304 | parser_dumpretrieve.set_defaults(func=bmcDumpRetrieve) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3305 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 3306 | #bmc command for reseting the bmc |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3307 | parser_bmc = subparsers.add_parser('bmc', help="Work with the bmc") |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 3308 | 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] | 3309 | parser_BMCReset = bmc_sub.add_parser('reset', help='Reset the bmc' ) |
| 3310 | 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] | 3311 | 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.") |
| 3312 | parser_bmc.set_defaults(func=bmc) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3313 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3314 | #add alias to the bmc command |
| 3315 | parser_mc = subparsers.add_parser('mc', help="Work with the management controller") |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 3316 | 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] | 3317 | parser_MCReset = mc_sub.add_parser('reset', help='Reset the bmc' ) |
| 3318 | parser_MCReset.add_argument('type', choices=['warm','cold'], help="Reboot the BMC") |
| 3319 | #parser_MCReset.add_argument('cold', action='store_true', help="Reboot the BMC and CLEAR the configuration") |
| 3320 | 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] | 3321 | parser_MCReset.set_defaults(func=bmcReset) |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3322 | parser_mc.set_defaults(func=bmc) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3323 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 3324 | #gard clear |
| 3325 | parser_gc = subparsers.add_parser("gardclear", help="Used to clear gard records") |
| 3326 | parser_gc.set_defaults(func=gardClear) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3327 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 3328 | #firmware_flash |
| 3329 | parser_fw = subparsers.add_parser("firmware", help="Work with the system firmware") |
| 3330 | 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] | 3331 | fwflash_subproc.required = True |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3332 | |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 3333 | fwflash = fwflash_subproc.add_parser('flash', help="Flash the system firmware") |
| 3334 | fwflash.add_argument('type', choices=['bmc', 'pnor'], help="image type to flash") |
| 3335 | fwflash.add_argument('-f', '--fileloc', required=True, help="The absolute path to the firmware image") |
| 3336 | fwflash.set_defaults(func=fwFlash) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3337 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 3338 | 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] | 3339 | fwActivate.add_argument('imageID', help="The image ID to activate from the firmware list. Ex: 63c95399") |
| 3340 | fwActivate.set_defaults(func=activateFWImage) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3341 | |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 3342 | fwActivateStatus = fwflash_subproc.add_parser('activation_status', help="Check Status of activations") |
| 3343 | fwActivateStatus.set_defaults(func=activateStatus) |
| 3344 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 3345 | fwList = fwflash_subproc.add_parser('list', help="List all of the installed firmware") |
| 3346 | fwList.add_argument('-v', '--verbose', action='store_true', help='Verbose output') |
| 3347 | fwList.set_defaults(func=firmwareList) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3348 | |
Justin Thaler | 3d71d40 | 2018-07-24 14:35:39 -0500 | [diff] [blame] | 3349 | fwprint = fwflash_subproc.add_parser('print', help="List all of the installed firmware") |
| 3350 | fwprint.add_argument('-v', '--verbose', action='store_true', help='Verbose output') |
| 3351 | fwprint.set_defaults(func=firmwareList) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3352 | |
Adriana Kobylak | 5af2fad | 2018-11-08 12:33:43 -0600 | [diff] [blame] | 3353 | fwDelete = fwflash_subproc.add_parser('delete', help="Delete an existing firmware version") |
| 3354 | fwDelete.add_argument('versionID', help="The version ID to delete from the firmware list. Ex: 63c95399") |
| 3355 | fwDelete.set_defaults(func=deleteFWVersion) |
| 3356 | |
Deepak Kodihalli | 22d4df0 | 2018-09-18 06:52:43 -0500 | [diff] [blame] | 3357 | #logging |
| 3358 | parser_logging = subparsers.add_parser("logging", help="logging controls") |
| 3359 | 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] | 3360 | |
Deepak Kodihalli | 22d4df0 | 2018-09-18 06:52:43 -0500 | [diff] [blame] | 3361 | #turn rest api logging on/off |
| 3362 | parser_rest_logging = logging_sub.add_parser("rest_api", help="turn rest api logging on/off") |
| 3363 | parser_rest_logging.add_argument('rest_logging', choices=['on', 'off'], help='The control option for rest logging: on, off') |
| 3364 | parser_rest_logging.set_defaults(func=restLogging) |
Deepak Kodihalli | 02d5328 | 2018-09-18 06:53:31 -0500 | [diff] [blame] | 3365 | |
| 3366 | #remote logging |
| 3367 | parser_remote_logging = logging_sub.add_parser("remote_logging", help="Remote logging (rsyslog) commands") |
| 3368 | parser_remote_logging.add_argument('remote_logging', choices=['view', 'disable'], help='Remote logging (rsyslog) commands') |
| 3369 | parser_remote_logging.set_defaults(func=remoteLogging) |
| 3370 | |
| 3371 | #configure remote logging |
| 3372 | parser_remote_logging_config = logging_sub.add_parser("remote_logging_config", help="Configure remote logging (rsyslog)") |
| 3373 | parser_remote_logging_config.add_argument("-a", "--address", required=True, help="Set IP address of rsyslog server") |
| 3374 | parser_remote_logging_config.add_argument("-p", "--port", required=True, type=int, help="Set Port of rsyslog server") |
| 3375 | parser_remote_logging_config.set_defaults(func=remoteLoggingConfig) |
Dhruvaraj Subhashchandran | 64e7f6f | 2018-10-02 03:42:14 -0500 | [diff] [blame] | 3376 | |
| 3377 | #certificate management |
| 3378 | parser_cert = subparsers.add_parser("certificate", help="Certificate management") |
| 3379 | certMgmt_subproc = parser_cert.add_subparsers(title='subcommands', description='valid certificate commands', help='sub-command help', dest='command') |
| 3380 | |
| 3381 | certUpdate = certMgmt_subproc.add_parser('update', help="Update the certificate") |
| 3382 | certUpdate.add_argument('type', choices=['server', 'client', 'authority'], help="certificate type to update") |
| 3383 | certUpdate.add_argument('service', choices=['https', 'ldap'], help="Service to update") |
| 3384 | certUpdate.add_argument('-f', '--fileloc', required=True, help="The absolute path to the certificate file") |
| 3385 | certUpdate.set_defaults(func=certificateUpdate) |
| 3386 | |
| 3387 | certDelete = certMgmt_subproc.add_parser('delete', help="Delete the certificate") |
| 3388 | certDelete.add_argument('type', choices=['server', 'client', 'authority'], help="certificate type to delete") |
| 3389 | certDelete.add_argument('service', choices=['https', 'ldap'], help="Service to delete the certificate") |
| 3390 | certDelete.set_defaults(func=certificateDelete) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3391 | |
Matt Spinler | 7d426c2 | 2018-09-24 14:42:07 -0500 | [diff] [blame] | 3392 | # local users |
| 3393 | parser_users = subparsers.add_parser("local_users", help="Work with local users") |
| 3394 | parser_users.add_argument('local_users', choices=['disableall','enableall', 'queryenabled'], help="Disable, enable or query local user accounts") |
| 3395 | parser_users.add_argument('-v', '--verbose', action='store_true', help='Verbose output') |
| 3396 | parser_users.set_defaults(func=localUsers) |
| 3397 | |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 3398 | #LDAP |
| 3399 | parser_ldap = subparsers.add_parser("ldap", help="LDAP controls") |
| 3400 | ldap_sub = parser_ldap.add_subparsers(title='subcommands', description='valid subcommands',help="sub-command help", dest='command') |
| 3401 | |
| 3402 | #configure and enable LDAP |
| 3403 | parser_ldap_config = ldap_sub.add_parser("enable", help="Configure and enables the LDAP") |
| 3404 | parser_ldap_config.add_argument("-a", "--uri", required=True, help="Set LDAP server URI") |
| 3405 | parser_ldap_config.add_argument("-B", "--bindDN", required=True, help="Set the bind DN of the LDAP server") |
| 3406 | parser_ldap_config.add_argument("-b", "--baseDN", required=True, help="Set the base DN of the LDAP server") |
| 3407 | parser_ldap_config.add_argument("-p", "--bindPassword", required=True, help="Set the bind password of the LDAP server") |
| 3408 | parser_ldap_config.add_argument("-S", "--scope", choices=['sub','one', 'base'], |
| 3409 | help='Specifies the search scope:subtree, one level or base object.') |
| 3410 | parser_ldap_config.add_argument("-t", "--serverType", choices=['ActiveDirectory','OpenLDAP'], |
| 3411 | help='Specifies the configured server is ActiveDirectory(AD) or OpenLdap') |
| 3412 | parser_ldap_config.set_defaults(func=enableLDAP) |
| 3413 | |
| 3414 | # disable LDAP |
| 3415 | parser_disable_ldap = ldap_sub.add_parser("disable", help="disables the LDAP") |
| 3416 | parser_disable_ldap.set_defaults(func=disableLDAP) |
Nagaraju Goruganti | 7d1fe17 | 2018-11-13 06:09:29 -0600 | [diff] [blame] | 3417 | # view-config |
| 3418 | parser_ldap_config = \ |
| 3419 | ldap_sub.add_parser("view-config", help="prints out a list of all \ |
| 3420 | LDAPS's configured properties") |
| 3421 | parser_ldap_config.set_defaults(func=viewLDAPConfig) |
Ratan Gupta | 9166cd2 | 2018-10-01 18:09:40 +0530 | [diff] [blame] | 3422 | |
Ratan Gupta | feee637 | 2018-10-17 23:25:51 +0530 | [diff] [blame] | 3423 | #create group privilege mapping |
| 3424 | parser_ldap_mapper = ldap_sub.add_parser("privilege-mapper", help="LDAP group privilege controls") |
| 3425 | parser_ldap_mapper_sub = parser_ldap_mapper.add_subparsers(title='subcommands', description='valid subcommands', |
| 3426 | help="sub-command help", dest='command') |
| 3427 | |
| 3428 | parser_ldap_mapper_create = parser_ldap_mapper_sub.add_parser("create", help="Create mapping of ldap group and privilege") |
| 3429 | parser_ldap_mapper_create.add_argument("-g","--groupName",required=True,help="Group Name") |
| 3430 | parser_ldap_mapper_create.add_argument("-p","--privilege",choices=['priv-admin','priv-user'],required=True,help="Privilege") |
| 3431 | parser_ldap_mapper_create.set_defaults(func=createPrivilegeMapping) |
| 3432 | |
| 3433 | #list group privilege mapping |
| 3434 | parser_ldap_mapper_list = parser_ldap_mapper_sub.add_parser("list",help="List privilege mapping") |
| 3435 | parser_ldap_mapper_list.set_defaults(func=listPrivilegeMapping) |
| 3436 | |
| 3437 | #delete group privilege mapping |
| 3438 | parser_ldap_mapper_delete = parser_ldap_mapper_sub.add_parser("delete",help="Delete privilege mapping") |
| 3439 | parser_ldap_mapper_delete.add_argument("-g","--groupName",required=True,help="Group Name") |
| 3440 | parser_ldap_mapper_delete.set_defaults(func=deletePrivilegeMapping) |
| 3441 | |
Sivas SRR | 7883527 | 2018-11-27 05:27:19 -0600 | [diff] [blame] | 3442 | #deleteAll group privilege mapping |
| 3443 | parser_ldap_mapper_delete = parser_ldap_mapper_sub.add_parser("purge",help="Delete All privilege mapping") |
| 3444 | parser_ldap_mapper_delete.set_defaults(func=deleteAllPrivilegeMapping) |
| 3445 | |
Marri Devender Rao | 2c2a516 | 2018-11-05 08:57:11 -0600 | [diff] [blame] | 3446 | # set local user password |
| 3447 | parser_set_password = subparsers.add_parser("set_password", |
| 3448 | help="Set password of local user") |
| 3449 | parser_set_password.add_argument( "-p", "--password", required=True, |
| 3450 | help="Password of local user") |
| 3451 | parser_set_password.set_defaults(func=setPassword) |
| 3452 | |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3453 | # network |
| 3454 | parser_nw = subparsers.add_parser("network", help="network controls") |
| 3455 | nw_sub = parser_nw.add_subparsers(title='subcommands', |
| 3456 | description='valid subcommands', |
| 3457 | help="sub-command help", |
| 3458 | dest='command') |
| 3459 | |
| 3460 | # enable DHCP |
| 3461 | parser_enable_dhcp = nw_sub.add_parser("enableDHCP", |
| 3462 | help="enables the DHCP on given " |
| 3463 | "Interface") |
| 3464 | parser_enable_dhcp.add_argument("-I", "--Interface", required=True, |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 3465 | help="Name of the ethernet interface(it can" |
| 3466 | "be obtained by the " |
| 3467 | "command:network view-config)" |
| 3468 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3469 | parser_enable_dhcp.set_defaults(func=enableDHCP) |
| 3470 | |
| 3471 | # disable DHCP |
| 3472 | parser_disable_dhcp = nw_sub.add_parser("disableDHCP", |
| 3473 | help="disables the DHCP on given " |
| 3474 | "Interface") |
| 3475 | parser_disable_dhcp.add_argument("-I", "--Interface", required=True, |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 3476 | help="Name of the ethernet interface(it can" |
| 3477 | "be obtained by the " |
| 3478 | "command:network view-config)" |
| 3479 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3480 | parser_disable_dhcp.set_defaults(func=disableDHCP) |
| 3481 | |
| 3482 | # get HostName |
| 3483 | parser_gethostname = nw_sub.add_parser("getHostName", |
| 3484 | help="prints out HostName") |
| 3485 | parser_gethostname.set_defaults(func=getHostname) |
| 3486 | |
| 3487 | # set HostName |
| 3488 | parser_sethostname = nw_sub.add_parser("setHostName", help="sets HostName") |
| 3489 | parser_sethostname.add_argument("-H", "--HostName", required=True, |
| 3490 | help="A HostName for the BMC") |
| 3491 | parser_sethostname.set_defaults(func=setHostname) |
| 3492 | |
| 3493 | # get domainname |
| 3494 | parser_getdomainname = nw_sub.add_parser("getDomainName", |
| 3495 | help="prints out DomainName of " |
| 3496 | "given Interface") |
| 3497 | parser_getdomainname.add_argument("-I", "--Interface", required=True, |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 3498 | help="Name of the ethernet interface(it " |
| 3499 | "can be obtained by the " |
| 3500 | "command:network view-config)" |
| 3501 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3502 | parser_getdomainname.set_defaults(func=getDomainName) |
| 3503 | |
| 3504 | # set domainname |
| 3505 | parser_setdomainname = nw_sub.add_parser("setDomainName", |
| 3506 | help="sets DomainName of given " |
| 3507 | "Interface") |
| 3508 | parser_setdomainname.add_argument("-D", "--DomainName", required=True, |
| 3509 | help="Ex: DomainName=Domain1,Domain2,...") |
| 3510 | parser_setdomainname.add_argument("-I", "--Interface", required=True, |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 3511 | help="Name of the ethernet interface(it " |
| 3512 | "can be obtained by the " |
| 3513 | "command:network view-config)" |
| 3514 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3515 | parser_setdomainname.set_defaults(func=setDomainName) |
| 3516 | |
| 3517 | # get MACAddress |
| 3518 | parser_getmacaddress = nw_sub.add_parser("getMACAddress", |
| 3519 | help="prints out MACAddress the " |
| 3520 | "given Interface") |
| 3521 | parser_getmacaddress.add_argument("-I", "--Interface", required=True, |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 3522 | help="Name of the ethernet interface(it " |
| 3523 | "can be obtained by the " |
| 3524 | "command:network view-config)" |
| 3525 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3526 | parser_getmacaddress.set_defaults(func=getMACAddress) |
| 3527 | |
| 3528 | # set MACAddress |
| 3529 | parser_setmacaddress = nw_sub.add_parser("setMACAddress", |
| 3530 | help="sets MACAddress") |
| 3531 | parser_setmacaddress.add_argument("-MA", "--MACAddress", required=True, |
| 3532 | help="A MACAddress for the given " |
| 3533 | "Interface") |
| 3534 | parser_setmacaddress.add_argument("-I", "--Interface", required=True, |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 3535 | help="Name of the ethernet interface(it can" |
| 3536 | "be obtained by the " |
| 3537 | "command:network view-config)" |
| 3538 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3539 | parser_setmacaddress.set_defaults(func=setMACAddress) |
| 3540 | |
| 3541 | # get DefaultGW |
| 3542 | parser_getdefaultgw = nw_sub.add_parser("getDefaultGW", |
| 3543 | help="prints out DefaultGateway " |
| 3544 | "the BMC") |
| 3545 | parser_getdefaultgw.set_defaults(func=getDefaultGateway) |
| 3546 | |
| 3547 | # set DefaultGW |
| 3548 | parser_setdefaultgw = nw_sub.add_parser("setDefaultGW", |
| 3549 | help="sets DefaultGW") |
| 3550 | parser_setdefaultgw.add_argument("-GW", "--DefaultGW", required=True, |
| 3551 | help="A DefaultGateway for the BMC") |
| 3552 | parser_setdefaultgw.set_defaults(func=setDefaultGateway) |
| 3553 | |
| 3554 | # view network Config |
| 3555 | parser_ldap_config = nw_sub.add_parser("view-config", help="prints out a " |
| 3556 | "list of all network's configured " |
| 3557 | "properties") |
| 3558 | parser_ldap_config.set_defaults(func=viewNWConfig) |
| 3559 | |
| 3560 | # get DNS |
| 3561 | parser_getDNS = nw_sub.add_parser("getDNS", |
| 3562 | help="prints out DNS servers on the " |
| 3563 | "given interface") |
| 3564 | parser_getDNS.add_argument("-I", "--Interface", required=True, |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 3565 | help="Name of the ethernet interface(it can" |
| 3566 | "be obtained by the " |
| 3567 | "command:network view-config)" |
| 3568 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3569 | parser_getDNS.set_defaults(func=getDNS) |
| 3570 | |
| 3571 | # set DNS |
| 3572 | parser_setDNS = nw_sub.add_parser("setDNS", |
| 3573 | help="sets DNS servers on the given " |
| 3574 | "interface") |
| 3575 | parser_setDNS.add_argument("-d", "--DNSServers", required=True, |
| 3576 | help="Ex: DNSSERVERS=DNS1,DNS2,...") |
| 3577 | parser_setDNS.add_argument("-I", "--Interface", required=True, |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 3578 | help="Name of the ethernet interface(it can" |
| 3579 | "be obtained by the " |
| 3580 | "command:network view-config)" |
| 3581 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3582 | parser_setDNS.set_defaults(func=setDNS) |
| 3583 | |
| 3584 | # get NTP |
| 3585 | parser_getNTP = nw_sub.add_parser("getNTP", |
| 3586 | help="prints out NTP servers on the " |
| 3587 | "given interface") |
| 3588 | parser_getNTP.add_argument("-I", "--Interface", required=True, |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 3589 | help="Name of the ethernet interface(it can" |
| 3590 | "be obtained by the " |
| 3591 | "command:network view-config)" |
| 3592 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3593 | parser_getNTP.set_defaults(func=getNTP) |
| 3594 | |
| 3595 | # set NTP |
| 3596 | parser_setNTP = nw_sub.add_parser("setNTP", |
| 3597 | help="sets NTP servers on the given " |
| 3598 | "interface") |
| 3599 | parser_setNTP.add_argument("-N", "--NTPServers", required=True, |
| 3600 | help="Ex: NTPSERVERS=NTP1,NTP2,...") |
| 3601 | parser_setNTP.add_argument("-I", "--Interface", required=True, |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 3602 | help="Name of the ethernet interface(it can" |
| 3603 | "be obtained by the " |
| 3604 | "command:network view-config)" |
| 3605 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
Nagaraju Goruganti | 9908bcf | 2018-11-14 22:07:25 -0600 | [diff] [blame] | 3606 | parser_setNTP.set_defaults(func=setNTP) |
| 3607 | |
Nagaraju Goruganti | 97a2060 | 2018-11-16 03:06:08 -0600 | [diff] [blame] | 3608 | # configure IP |
| 3609 | parser_ip_config = nw_sub.add_parser("addIP", help="Sets IP address to" |
| 3610 | "given interface") |
| 3611 | parser_ip_config.add_argument("-a", "--address", required=True, |
| 3612 | help="IP address of given interface") |
| 3613 | parser_ip_config.add_argument("-gw", "--gateway", required=False, default='', |
| 3614 | help="The gateway for given interface") |
| 3615 | parser_ip_config.add_argument("-l", "--prefixLength", required=True, |
| 3616 | help="The prefixLength of IP address") |
| 3617 | parser_ip_config.add_argument("-p", "--type", choices=['ipv4', 'ipv6'], |
| 3618 | help="The protocol type of the given" |
| 3619 | "IP address") |
| 3620 | parser_ip_config.add_argument("-I", "--Interface", required=True, |
| 3621 | help="Name of the ethernet interface(it can" |
| 3622 | "be obtained by the " |
| 3623 | "command:network view-config)" |
| 3624 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
| 3625 | parser_ip_config.set_defaults(func=addIP) |
| 3626 | |
| 3627 | # getIP |
| 3628 | parser_getIP = nw_sub.add_parser("getIP", help="prints out IP address" |
| 3629 | "of given interface") |
| 3630 | parser_getIP.add_argument("-I", "--Interface", required=True, |
| 3631 | help="Name of the ethernet interface(it can" |
| 3632 | "be obtained by the command:network view-config)" |
| 3633 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
| 3634 | parser_getIP.set_defaults(func=getIP) |
| 3635 | |
| 3636 | # rmIP |
| 3637 | parser_rmIP = nw_sub.add_parser("rmIP", help="deletes IP address" |
| 3638 | "of given interface") |
| 3639 | parser_rmIP.add_argument("-a", "--address", required=True, |
| 3640 | help="IP address to remove form given Interface") |
| 3641 | parser_rmIP.add_argument("-I", "--Interface", required=True, |
| 3642 | help="Name of the ethernet interface(it can" |
| 3643 | "be obtained by the command:network view-config)" |
| 3644 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
| 3645 | parser_rmIP.set_defaults(func=deleteIP) |
| 3646 | |
Nagaraju Goruganti | f21d43c | 2018-11-19 10:47:19 -0600 | [diff] [blame] | 3647 | # add VLAN |
| 3648 | parser_create_vlan = nw_sub.add_parser("addVLAN", help="enables VLAN " |
| 3649 | "on given interface with given " |
| 3650 | "VLAN Identifier") |
| 3651 | parser_create_vlan.add_argument("-I", "--Interface", required=True, |
| 3652 | choices=['eth0', 'eth1'], |
| 3653 | help="Name of the ethernet interface") |
| 3654 | parser_create_vlan.add_argument("-n", "--Identifier", required=True, |
| 3655 | help="VLAN Identifier") |
| 3656 | parser_create_vlan.set_defaults(func=addVLAN) |
| 3657 | |
| 3658 | # delete VLAN |
| 3659 | parser_delete_vlan = nw_sub.add_parser("deleteVLAN", help="disables VLAN " |
| 3660 | "on given interface with given " |
| 3661 | "VLAN Identifier") |
| 3662 | parser_delete_vlan.add_argument("-I", "--Interface", required=True, |
| 3663 | help="Name of the ethernet interface(it can" |
| 3664 | "be obtained by the " |
| 3665 | "command:network view-config)" |
| 3666 | "Ex: eth0 or eth1 or VLAN(VLAN=eth0_50 etc)") |
| 3667 | parser_delete_vlan.set_defaults(func=deleteVLAN) |
| 3668 | |
| 3669 | # viewDHCPConfig |
| 3670 | parser_viewDHCPConfig = nw_sub.add_parser("viewDHCPConfig", |
| 3671 | help="Shows DHCP configured " |
| 3672 | "Properties") |
| 3673 | parser_viewDHCPConfig.set_defaults(func=viewDHCPConfig) |
| 3674 | |
| 3675 | # configureDHCP |
| 3676 | parser_configDHCP = nw_sub.add_parser("configureDHCP", |
| 3677 | help="Configures/updates DHCP " |
| 3678 | "Properties") |
| 3679 | parser_configDHCP.add_argument("-d", "--DNSEnabled", type=str2bool, |
| 3680 | required=True, help="Sets DNSEnabled property") |
| 3681 | parser_configDHCP.add_argument("-n", "--HostNameEnabled", type=str2bool, |
| 3682 | required=True, |
| 3683 | help="Sets HostNameEnabled property") |
| 3684 | parser_configDHCP.add_argument("-t", "--NTPEnabled", type=str2bool, |
| 3685 | required=True, |
| 3686 | help="Sets NTPEnabled property") |
| 3687 | parser_configDHCP.add_argument("-s", "--SendHostNameEnabled", type=str2bool, |
| 3688 | required=True, |
| 3689 | help="Sets SendHostNameEnabled property") |
| 3690 | parser_configDHCP.set_defaults(func=configureDHCP) |
| 3691 | |
| 3692 | # network factory reset |
| 3693 | parser_nw_reset = nw_sub.add_parser("nwReset", |
| 3694 | help="Resets networks setting to " |
| 3695 | "factory defaults. " |
| 3696 | "note:Reset settings will be applied " |
| 3697 | "after BMC reboot") |
| 3698 | parser_nw_reset.set_defaults(func=nwReset) |
| 3699 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3700 | return parser |
| 3701 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3702 | def main(argv=None): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 3703 | """ |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3704 | main function for running the command line utility as a sub application |
| 3705 | """ |
| 3706 | global toolVersion |
Adriana Kobylak | 5af2fad | 2018-11-08 12:33:43 -0600 | [diff] [blame] | 3707 | toolVersion = "1.11" |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3708 | parser = createCommandParser() |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3709 | args = parser.parse_args(argv) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3710 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3711 | totTimeStart = int(round(time.time()*1000)) |
| 3712 | |
| 3713 | if(sys.version_info < (3,0)): |
| 3714 | urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) |
| 3715 | if sys.version_info >= (3,0): |
| 3716 | requests.packages.urllib3.disable_warnings(requests.packages.urllib3.exceptions.InsecureRequestWarning) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 3717 | if (args.version): |
Justin Thaler | 22b1bb5 | 2018-03-15 13:31:32 -0500 | [diff] [blame] | 3718 | print("Version: "+ toolVersion) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 3719 | sys.exit(0) |
| 3720 | 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] | 3721 | mysess = None |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 3722 | print(selPrint('N/A', args, mysess)) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3723 | else: |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 3724 | if(hasattr(args, 'host') and hasattr(args,'user')): |
| 3725 | if (args.askpw): |
| 3726 | pw = getpass.getpass() |
| 3727 | elif(args.PW is not None): |
| 3728 | pw = args.PW |
| 3729 | else: |
| 3730 | print("You must specify a password") |
| 3731 | sys.exit() |
| 3732 | logintimeStart = int(round(time.time()*1000)) |
| 3733 | mysess = login(args.host, args.user, pw, args.json) |
Justin Thaler | a9415b4 | 2018-05-25 19:40:13 -0500 | [diff] [blame] | 3734 | if(sys.version_info < (3,0)): |
| 3735 | if isinstance(mysess, basestring): |
| 3736 | print(mysess) |
| 3737 | sys.exit(1) |
| 3738 | elif sys.version_info >= (3,0): |
| 3739 | if isinstance(mysess, str): |
| 3740 | print(mysess) |
| 3741 | sys.exit(1) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 3742 | logintimeStop = int(round(time.time()*1000)) |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3743 | |
| 3744 | commandTimeStart = int(round(time.time()*1000)) |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 3745 | output = args.func(args.host, args, mysess) |
| 3746 | commandTimeStop = int(round(time.time()*1000)) |
| 3747 | print(output) |
| 3748 | if (mysess is not None): |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3749 | logout(args.host, args.user, pw, mysess, args.json) |
| 3750 | if(args.procTime): |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 3751 | print("Total time: " + str(int(round(time.time()*1000))- totTimeStart)) |
| 3752 | print("loginTime: " + str(logintimeStop - logintimeStart)) |
| 3753 | print("command Time: " + str(commandTimeStop - commandTimeStart)) |
| 3754 | else: |
| 3755 | print("usage: openbmctool.py [-h] -H HOST -U USER [-A | -P PW] [-j]\n" + |
| 3756 | "\t[-t POLICYTABLELOC] [-V]\n" + |
Deepak Kodihalli | 22d4df0 | 2018-09-18 06:52:43 -0500 | [diff] [blame] | 3757 | "\t{fru,sensors,sel,chassis,collect_service_data, \ |
| 3758 | health_check,dump,bmc,mc,gardclear,firmware,logging}\n" + |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 3759 | "\t...\n" + |
| 3760 | "openbmctool.py: error: the following arguments are required: -H/--host, -U/--user") |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3761 | sys.exit() |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3762 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3763 | if __name__ == '__main__': |
Justin Thaler | e412dc2 | 2018-01-12 16:28:24 -0600 | [diff] [blame] | 3764 | """ |
| 3765 | main function when called from the command line |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3766 | |
| 3767 | """ |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3768 | import sys |
Nagaraju Goruganti | c1a00af | 2018-11-07 00:52:11 -0600 | [diff] [blame] | 3769 | |
Justin Thaler | f9aee3e | 2017-12-05 12:11:09 -0600 | [diff] [blame] | 3770 | isTTY = sys.stdout.isatty() |
| 3771 | assert sys.version_info >= (2,7) |
| 3772 | main() |