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