blob: 733fabe697092239667afbc1c6bcb5e26e4c614f [file] [log] [blame]
Alanny Lopez1c8e5742017-05-16 08:20:33 -05001***Settings***
2Documentation 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
8Library String
9Library Collections
10Library OperatingSystem
George Keishingdaade9c2017-06-05 13:41:12 -050011Resource ../syslib/utils_os.robot
Alanny Lopez1c8e5742017-05-16 08:20:33 -050012
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 Keishingdaade9c2017-06-05 13:41:12 -050019${json_tmp_file_path} ${EXECDIR}${/}inventory.json
20${json_file_path} ${EXECDIR}${/}data${/}os_inventory.json
21${yaml_file_path} ${EXECDIR}${/}inventory.yaml
Alanny Lopez1c8e5742017-05-16 08:20:33 -050022
23***Test Case***
24
25Create YAML Inventory File
26 [Documentation] Create a JSON inventory file, and make a YAML copy.
27 [Tags] Create_YAML_Inventory_File
George Keishingdaade9c2017-06-05 13:41:12 -050028 Login To OS
Alanny Lopez1c8e5742017-05-16 08:20:33 -050029 Compile Inventory JSON
George Keishingdaade9c2017-06-05 13:41:12 -050030 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 Lopez1c8e5742017-05-16 08:20:33 -050033
34***Keywords***
35
36Compile Inventory JSON
37 [Documentation] Compile the Inventory into a JSON file.
George Keishingdaade9c2017-06-05 13:41:12 -050038 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 Lopez1c8e5742017-05-16 08:20:33 -050044
45Write New JSON List
46 [Documentation] Start a new JSON list element in file.
George Keishingdaade9c2017-06-05 13:41:12 -050047 [Arguments] ${json_tmp_file_path} ${json_field_name}
Alanny Lopez1c8e5742017-05-16 08:20:33 -050048 # Description of argument(s):
George Keishingdaade9c2017-06-05 13:41:12 -050049 # 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 Lopez1c8e5742017-05-16 08:20:33 -050052
53Close New JSON List
54 [Documentation] Close JSON list element in file.
George Keishingdaade9c2017-06-05 13:41:12 -050055 [Arguments] ${json_tmp_file_path}
Alanny Lopez1c8e5742017-05-16 08:20:33 -050056 # Description of argument(s):
George Keishingdaade9c2017-06-05 13:41:12 -050057 # json_tmp_file_path Path of file to write to.
58 Append to File ${json_tmp_file_path} ]}
Alanny Lopez1c8e5742017-05-16 08:20:33 -050059
60Retrieve HW Info And Write
61 [Documentation] Retrieve and write info, add a comma if not last item.
George Keishingdaade9c2017-06-05 13:41:12 -050062 [Arguments] ${class} ${json_tmp_file_path} ${last}=false
Alanny Lopez1c8e5742017-05-16 08:20:33 -050063 # Description of argument(s):
George Keishingdaade9c2017-06-05 13:41:12 -050064 # 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 Lopez1c8e5742017-05-16 08:20:33 -050068 ${output} = Retrieve Hardware Info ${class}
69 ${output} = Clean Up String ${output}
70 Run Keyword if ${output.__class__ is not type(None)}
George Keishingdaade9c2017-06-05 13:41:12 -050071 ... Append To File ${json_tmp_file_path} ${output}
72 Close New JSON List ${json_tmp_file_path}
Alanny Lopez1c8e5742017-05-16 08:20:33 -050073 Run Keyword if '${last}' == 'false'
George Keishingdaade9c2017-06-05 13:41:12 -050074 ... Append to File ${json_tmp_file_path} ,
Alanny Lopez1c8e5742017-05-16 08:20:33 -050075
76Retrieve 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 Keishingdaade9c2017-06-05 13:41:12 -050079 [Arguments] ${list} ${json_tmp_file_path} ${json_field_name}
80 ... ${last}=false
Alanny Lopez1c8e5742017-05-16 08:20:33 -050081 # Description of argument(s):
George Keishingdaade9c2017-06-05 13:41:12 -050082 # 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 Lopez1c8e5742017-05-16 08:20:33 -050087 : FOR ${class} IN @{list}
88 \ ${tail} Get From List ${list} -1
89 \ Run Keyword if '${tail}' == '${class}'
George Keishingdaade9c2017-06-05 13:41:12 -050090 \ ... 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 Lopez1c8e5742017-05-16 08:20:33 -050093 Run Keyword if '${last}' == 'false'
George Keishingdaade9c2017-06-05 13:41:12 -050094 ... Append to File ${json_tmp_file_path} ,
Alanny Lopez1c8e5742017-05-16 08:20:33 -050095
96Retrieve 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 Keishingdaade9c2017-06-05 13:41:12 -0500101 ${output} = Execute Command On OS lshw -c ${class} -json
Alanny Lopez1c8e5742017-05-16 08:20:33 -0500102 ${output} = Verify JSON string ${output}
103 [Return] ${output}
104
105Verify 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
114Clean 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 Keishingdaade9c2017-06-05 13:41:12 -0500124 [Return] ${clean_string}