Skip to content

Commit c851001

Browse files
[FEATURE] adding dump_current feature under ts menu
* changed inventory_list_all to collect details in batches of 50 * extra option to dump just current files * updated usage * removed debug messages * fixed tests
1 parent b51f361 commit c851001

File tree

7 files changed

+126
-61
lines changed

7 files changed

+126
-61
lines changed

dist/crhc

10.7 MB
Binary file not shown.

docs/usage.rst

+2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ The main idea of this script is to collect the information from console.redhat.c
5656
- This option will show some information regarding to the user who requested the token
5757
* - crhc ts dump
5858
- Export the whole Inventory and Subscription information in json format. Some files will be created.
59+
* - crhc ts dump_current
60+
- Export the current Inventory and Subscription information in json format. Some files will be created.
5961
* - crhc ts match
6062
- If the files mentioned above are not around, this feature will call the dump and after that will check both files and will create the 3rd one with the whole information correlated accordingly.
6163
* - crhc ts clean

execution/execution.py

+95-47
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def inventory_list():
128128
return inventory_full_detail
129129

130130

131-
def inventory_list_all():
131+
def inventory_list_all(current_only=False):
132132
"""
133133
This def will collect all the HBI entries
134134
"""
@@ -157,7 +157,8 @@ def inventory_list_all():
157157

158158
# For debugin purposes
159159
# num_of_pages = 2
160-
160+
161+
161162

162163
for page in range(1, num_of_pages):
163164
url = (
@@ -169,50 +170,59 @@ def inventory_list_all():
169170
)
170171
response = connection_request(url)
171172

173+
inventory_batch = []
174+
is_first_server = True
175+
server_detail_url = "https://console.redhat.com/api/inventory/v1/hosts/"
172176
inventory_batch = []
173177
is_first_server = True
174178
server_detail_url = "https://console.redhat.com/api/inventory/v1/hosts/"
175179
for server in response.json()["results"]:
176180
server_id = server["id"]
177-
inventory_batch.append(server_id)
178-
# if its the first entry
179-
if (len(inventory_batch) == 1):
180-
server_detail_url = server_detail_url + server_id
181-
else:
182-
server_detail_url = server_detail_url + "," + server_id
183-
184-
# now call the server details request with up to 50 ids
185-
url = (
186-
server_detail_url
187-
+ "/system_profile"
188-
+ FIELDS_TO_RETRIEVE
189-
)
190-
response_system_profile = connection_request(url)
191-
192-
193-
# now loop through the original server request
194-
for server in response.json()["results"]:
195-
196-
try:
197-
stage_dic["server"] = server
198-
except json.decoder.JSONDecodeError:
199-
stage_dic["server"] = {}
200-
201-
server_id = server["id"]
202-
203-
try:
204-
server_details_list = response_system_profile.json()["results"]
205-
# loop through all the server details - finding the one that matches the id we're looping through
206-
for server_details in server_details_list:
207-
if (server_details["id"] == server_id ):
208-
stage_dic["system_profile"] = server_details["system_profile"]
209-
except json.decoder.JSONDecodeError:
210-
stage_dic["system_profile"] = {}
211-
except KeyError:
212-
stage_dic["system_profile"] = {}
213-
214-
list_of_servers.append(stage_dic)
215-
stage_dic = {}
181+
stale_timestamp = server["stale_timestamp"]
182+
# if you want all systems, or just if you want current systems ,and thisone is current
183+
if (not current_only or (current_only and is_fresh(stale_timestamp))):
184+
inventory_batch.append(server_id)
185+
# if its the first entry
186+
if (len(inventory_batch) == 1):
187+
server_detail_url = server_detail_url + server_id
188+
else:
189+
server_detail_url = server_detail_url + "," + server_id
190+
191+
# now call the server details request with up to 50 ids, assuming that we have some server ids in this batch
192+
if (len(inventory_batch) >0):
193+
url = (
194+
server_detail_url
195+
+ "/system_profile"
196+
+ FIELDS_TO_RETRIEVE
197+
)
198+
response_system_profile = connection_request(url)
199+
200+
201+
# now loop through the original server request
202+
for server in response.json()["results"]:
203+
# check whether we're getting everything - or whether the system is current or not
204+
stale_timestamp = server["stale_timestamp"]
205+
if (not current_only or (current_only and is_fresh(stale_timestamp))):
206+
try:
207+
stage_dic["server"] = server
208+
except json.decoder.JSONDecodeError:
209+
stage_dic["server"] = {}
210+
211+
server_id = server["id"]
212+
213+
try:
214+
server_details_list = response_system_profile.json()["results"]
215+
# loop through all the server details - finding the one that matches the id we're looping through
216+
for server_details in server_details_list:
217+
if (server_details["id"] == server_id ):
218+
stage_dic["system_profile"] = server_details["system_profile"]
219+
except json.decoder.JSONDecodeError:
220+
stage_dic["system_profile"] = {}
221+
except KeyError:
222+
stage_dic["system_profile"] = {}
223+
224+
list_of_servers.append(stage_dic)
225+
stage_dic = {}
216226

217227
return inventory_full_detail
218228

@@ -407,7 +417,7 @@ def swatch_list():
407417
return response.json()
408418

409419

410-
def swatch_list_all():
420+
def swatch_list_all(current_only=False):
411421
"""
412422
This def will collect all the entries from Subscription Watch
413423
"""
@@ -427,10 +437,7 @@ def swatch_list_all():
427437
)
428438
# num_of_pages = round(response.json()['meta']['count'] / 100 + 1)
429439

430-
dic_full_list = {
431-
"data": "",
432-
"meta": {"count": response.json()["meta"]["count"]},
433-
}
440+
434441
full_list = []
435442
dup_kvm_servers = []
436443
server_with_no_dupes = []
@@ -449,7 +456,15 @@ def swatch_list_all():
449456
# count = count + 100
450457

451458
for entry in response.json()["data"]:
452-
full_list.append(entry)
459+
last_seen = entry.get("last_seen")
460+
# either get all systems, or if getting current, check the last seen date
461+
if (not current_only or (current_only and seen_recently(last_seen))):
462+
full_list.append(entry)
463+
464+
dic_full_list = {
465+
"data": "",
466+
"meta": {"count": len(full_list)},
467+
}
453468

454469
# The piece below is just to check/remove the duplicate entries
455470
# caused by kvm/libvirt hypervisors. At this moment, swatch is
@@ -532,6 +547,39 @@ def swatch_list_all():
532547

533548
return dic_full_list
534549

550+
def is_fresh(stale_timestamp):
551+
stale_date_string = stale_timestamp
552+
is_fresh=True
553+
if (len(stale_date_string) > 19):
554+
stale_date_string = stale_timestamp[:19]
555+
try:
556+
stale_date = datetime.datetime.strptime(stale_date_string,"%Y-%m-%dT%H:%M:%S")
557+
current_date = datetime.datetime.now()
558+
if (stale_date < current_date):
559+
is_fresh = False
560+
except Exception as e:
561+
is_fresh=True
562+
print("Exception in is_fresh : " + str(e))
563+
564+
return is_fresh
565+
566+
def seen_recently(last_seen):
567+
stale_date_string = last_seen
568+
seen_recently=True
569+
if (len(stale_date_string) > 19):
570+
stale_date_string = last_seen[:19]
571+
try:
572+
last_seen_date = datetime.datetime.strptime(stale_date_string,"%Y-%m-%dT%H:%M:%S")
573+
stale_date = last_seen_date + datetime.timedelta(days=1)
574+
current_date = datetime.datetime.now()
575+
if (stale_date < current_date):
576+
seen_recently = False
577+
except Exception as e:
578+
seen_recently=True
579+
print("Exception in seen_recently : " + str(e))
580+
581+
return seen_recently
582+
535583

536584
def swatch_socket_summary():
537585
"""

help/help_opt.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,10 @@ def help_ts_menu():
153153
crhc ts [command]\n\
154154
\n\
155155
Available Commands:\n\
156-
dump dump the json files, Inventory and Subscription\n\
157-
match match the Inventory and Subscription information\n\
158-
clean cleanup the local 'cache/temporary/dump' files\
156+
dump dump the json files, Inventory and Subscription\n\
157+
dump_current dump the json files with current systems only, Inventory and Subscription\n\
158+
match match the Inventory and Subscription information\n\
159+
clean cleanup the local 'cache/temporary/dump' files\
159160
"
160161
print(content)
161162
return content

parse/parse.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -530,8 +530,21 @@ def troubleshoot_sub_menu():
530530

531531
try:
532532
if (sys.argv[1] == "ts") and (sys.argv[2] == "dump"):
533-
ts.dump_inv_json()
534-
ts.dump_sw_json()
533+
ts.dump_inv_json(False)
534+
ts.dump_sw_json(False)
535+
ts.dump_patch_json()
536+
ts.dump_vulnerability_json()
537+
ts.dump_advisor_json()
538+
ts.compress_json_files()
539+
sys.exit()
540+
except IndexError as e:
541+
# print("Error1: {}".format(e))
542+
...
543+
544+
try:
545+
if (sys.argv[1] == "ts") and (sys.argv[2] == "dump_current"):
546+
ts.dump_inv_json(True)
547+
ts.dump_sw_json(True)
535548
ts.dump_patch_json()
536549
ts.dump_vulnerability_json()
537550
ts.dump_advisor_json()

tests/test_help.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,10 @@ def test_check_ts_help_menu():
137137
crhc ts [command]\n\
138138
\n\
139139
Available Commands:\n\
140-
dump dump the json files, Inventory and Subscription\n\
141-
match match the Inventory and Subscription information\n\
142-
clean cleanup the local 'cache/temporary/dump' files\
140+
dump dump the json files, Inventory and Subscription\n\
141+
dump_current dump the json files with current systems only, Inventory and Subscription\n\
142+
match match the Inventory and Subscription information\n\
143+
clean cleanup the local 'cache/temporary/dump' files\
143144
"
144145
assert response == content
145146

troubleshoot/ts.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from conf import conf
1414

1515

16-
def dump_inv_json():
16+
def dump_inv_json(current_only):
1717
"""
1818
Function to dump only Inventory information
1919
"""
@@ -25,13 +25,13 @@ def dump_inv_json():
2525
conf.INV_JSON_FILE
2626
)
2727
)
28-
inventory = execution.inventory_list_all()
28+
inventory = execution.inventory_list_all(current_only)
2929

3030
file_obj = open(conf.INV_JSON_FILE, "w")
3131
file_obj.write(json.dumps(inventory, indent=4))
3232

3333

34-
def dump_sw_json():
34+
def dump_sw_json(current_only):
3535
"""
3636
Function to dump only Swatch information
3737
"""
@@ -41,7 +41,7 @@ def dump_sw_json():
4141
conf.SW_JSON_FILE
4242
)
4343
)
44-
swatch = execution.swatch_list_all()
44+
swatch = execution.swatch_list_all(current_only)
4545

4646
file_obj = open(conf.SW_JSON_FILE, "w")
4747
file_obj.write(json.dumps(swatch, indent=4))
@@ -135,7 +135,7 @@ def match_hbi_sw():
135135
"File {} already in place, using it.".format(conf.INV_JSON_FILE)
136136
)
137137
except FileNotFoundError:
138-
dump_inv_json()
138+
dump_inv_json(False)
139139
file_obj = open(conf.INV_JSON_FILE, "r")
140140
inventory = json.load(file_obj)
141141

@@ -144,7 +144,7 @@ def match_hbi_sw():
144144
swatch = json.load(file_obj)
145145
print("File {} already in place, using it.".format(conf.SW_JSON_FILE))
146146
except FileNotFoundError:
147-
dump_sw_json()
147+
dump_sw_json(False)
148148
file_obj = open(conf.SW_JSON_FILE, "r")
149149
swatch = json.load(file_obj)
150150

0 commit comments

Comments
 (0)