Alanny Lopez | 1c8e574 | 2017-05-16 08:20:33 -0500 | [diff] [blame] | 1 | ***Settings*** |
| 2 | Documentation This module is for generating an inventory file using lshw |
| 3 | ... commands. It will create a JSON file and a YAML file. it |
| 4 | ... will get the processor, memory and specified I/O devices. |
| 5 | ... Requires access to lshw, and json2yaml OS commands. This |
| 6 | ... robot file should be run as root or sudo for lshw. |
| 7 | |
| 8 | Library String |
| 9 | Library Collections |
| 10 | Library OperatingSystem |
| 11 | |
| 12 | ***Variables*** |
| 13 | # List of I/O Devices to Collect |
| 14 | @{I/O} communication disk display generic input multimedia |
| 15 | ... network printer tape |
| 16 | |
| 17 | # Paths of the JSON and YAML files |
| 18 | ${json_file_path} inventory.json |
| 19 | ${yaml_file_path} inventory.yaml |
| 20 | |
| 21 | ***Test Case*** |
| 22 | |
| 23 | Create YAML Inventory File |
| 24 | [Documentation] Create a JSON inventory file, and make a YAML copy. |
| 25 | [Tags] Create_YAML_Inventory_File |
| 26 | Compile Inventory JSON |
| 27 | RUN json2yaml ${json_file_path} ${yaml_file_path} |
| 28 | |
| 29 | ***Keywords*** |
| 30 | |
| 31 | Compile Inventory JSON |
| 32 | [Documentation] Compile the Inventory into a JSON file. |
| 33 | Create File ${json_file_path} |
| 34 | Write New JSON List ${json_file_path} Inventory |
| 35 | Retrieve HW Info And Write processor ${json_file_path} |
| 36 | Retrieve HW Info And Write memory ${json_file_path} |
| 37 | Retrieve HW Info And Write List ${I/O} ${json_file_path} I/O last |
| 38 | Close New JSON List ${json_file_path} |
| 39 | |
| 40 | Write New JSON List |
| 41 | [Documentation] Start a new JSON list element in file. |
| 42 | [Arguments] ${json_file_path} ${json_field_name} |
| 43 | # Description of argument(s): |
| 44 | # json_file_path Name of file to write to. |
| 45 | # json_field_name Name to give json list element. |
| 46 | Append to File ${json_file_path} { "${json_field_name}" : [ |
| 47 | |
| 48 | Close New JSON List |
| 49 | [Documentation] Close JSON list element in file. |
| 50 | [Arguments] ${json_file_path} |
| 51 | # Description of argument(s): |
| 52 | # json_file_path Path of file to write to. |
| 53 | Append to File ${json_file_path} ]} |
| 54 | |
| 55 | Retrieve HW Info And Write |
| 56 | [Documentation] Retrieve and write info, add a comma if not last item. |
| 57 | [Arguments] ${class} ${json_file_path} ${last}=false |
| 58 | # Description of argument(s): |
| 59 | # class Device class to retrieve with lshw. |
| 60 | # json_file_path Path of file to write to. |
| 61 | # last Is this the last element in the parent JSON? |
| 62 | Write New JSON List ${json_file_path} ${class} |
| 63 | ${output} = Retrieve Hardware Info ${class} |
| 64 | ${output} = Clean Up String ${output} |
| 65 | Run Keyword if ${output.__class__ is not type(None)} |
| 66 | ... Append To File ${json_file_path} ${output} |
| 67 | Close New JSON List ${json_file_path} |
| 68 | Run Keyword if '${last}' == 'false' |
| 69 | ... Append to File ${json_file_path} , |
| 70 | |
| 71 | Retrieve HW Info And Write List |
| 72 | [Documentation] Does a Retrieve/Write with a list of classes and |
| 73 | ... encapsulates them into one large JSON element. |
| 74 | [Arguments] ${list} ${json_file_path} ${json_field_name} ${last}=false |
| 75 | # Description of argument(s): |
| 76 | # list The list of devices classes to retrieve with lshw. |
| 77 | # json_file_path Path of file to write to. |
| 78 | # json_field_name Name of the JSON element to encapsulate this list. |
| 79 | # last Is this the last element in the parent JSON? |
| 80 | Write New JSON List ${json_file_path} ${json_field_name} |
| 81 | : FOR ${class} IN @{list} |
| 82 | \ ${tail} Get From List ${list} -1 |
| 83 | \ Run Keyword if '${tail}' == '${class}' |
| 84 | \ ... Retrieve HW Info And Write ${class} ${json_file_path} true |
| 85 | \ ... ELSE Retrieve HW Info And Write ${class} ${json_file_path} |
| 86 | Close New JSON List ${json_file_path} |
| 87 | Run Keyword if '${last}' == 'false' |
| 88 | ... Append to File ${json_file_path} , |
| 89 | |
| 90 | Retrieve Hardware Info |
| 91 | [Documentation] Retrieves the lshw output of the device class as JSON. |
| 92 | [Arguments] ${class} |
| 93 | # Description of argument(s): |
| 94 | # class Device class to retrieve with lshw. |
| 95 | ${output} = Run lshw -c ${class} -json |
| 96 | ${output} = Verify JSON string ${output} |
| 97 | [Return] ${output} |
| 98 | |
| 99 | Verify JSON String |
| 100 | [Documentation] Ensure the JSON string content is seperated by commas. |
| 101 | [Arguments] ${unver_string} |
| 102 | # Description of argument(s): |
| 103 | # unver_string JSON String we will be checking for lshw comma errors. |
| 104 | ${unver_string} = Convert to String ${unver_string} |
| 105 | ${ver_string} = Replace String Using Regexp ${unver_string} }\\s*{ },{ |
| 106 | [Return] ${ver_string} |
| 107 | |
| 108 | Clean Up String |
| 109 | [Documentation] Remove extra whitespace and trailing commas. |
| 110 | [Arguments] ${dirty_string} |
| 111 | # Description of argument(s): |
| 112 | # dirty_string String that will be space stripped and have comma removed. |
| 113 | ${clean_string} = Strip String ${dirty_string} |
| 114 | ${last_char} = Get Substring ${clean_string} -1 |
| 115 | ${trimmed_string} = Get Substring ${clean_string} 0 -1 |
| 116 | ${clean_string} = Set Variable If '${last_char}' == ',' |
| 117 | ... ${trimmed_string} ${clean_string} |
| 118 | [Return] ${clean_string} |