-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubscriptionsByHost.py
More file actions
110 lines (95 loc) · 3.31 KB
/
subscriptionsByHost.py
File metadata and controls
110 lines (95 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/python
import json
import collections
from pyexcel_ods import save_data
import argparse
import xlsxwriter
from satellite import Satellite
parser = argparse.ArgumentParser()
parser.add_argument("-d", "--debug", help="Print debugging messages", action="count")
parser.add_argument("-o", "--outfmt", help="Output format", choices=['xlsx', 'ods'], default='ods')
args = parser.parse_args()
errataWorkbook = collections.OrderedDict()
listOfHosts = []
def printDBG(level, message):
if level>args.debug:
return
print(message)
with open('credentials.json') as credentialfile:
credentials = json.load(credentialfile)
printDBG(3, "Satellite credentials loaded.")
def collectData():
printDBG(1, "Collecting data from Satellite")
s = Satellite(credentials['hostname'])
s.setUsername(credentials['username'])
s.setPassword(credentials['password'])
h=s.listHosts()
for host in h:
hostItem = {}
hostItem['name']=host['name']
hostItem['subs']=[]
subItems=s.getHostSubscriptions(str(host['id']))
if ( type(None) == type (subItems) ) :
continue
for item in subItems:
si={}
if ( 'name' not in item):
continue
if ( item['name'] in [ 'EPEL', 'FPLInternal' ] ):
continue
si['accountNum'] = item['account_number']
si['contractNum'] = item['contract_number']
si['endDate'] = item['end_date']
si['name'] = item['name']
hostItem['subs'].append(si)
listOfHosts.append(hostItem)
## REDO OUPUT
def generateODS():
printDBG(1, "Generating report in ODS format")
sheet1 = {"Hosts Report":[]}
sheet1['Hosts Report'].append(["HOSTNAME"])
sheet1['Hosts Report'].append(["Account number", "Contract number", "End Date", "Subscription Name"])
printDBG(2, "Generating host sheets")
for host in sorted(listOfHosts):
sheet1['Hosts Report'].append(["Host name "+host['name']])
for sub in host['subs']:
sheet1['Hosts Report'].append([ sub['accountNum'], sub['contractNum'], sub['endDate'], sub['name'] ] )
errataWorkbook.update(sheet1)
errataWorkbook.update(sheet1)
printDBG(2, "Saving workbook")
save_data('subscriptioninfo.ods', errataWorkbook)
def generateXLSX():
printDBG(1, "Generating report in XLSX format")
workbook = xlsxwriter.Workbook('subscriptioninfo.xlsx')
topSheet = workbook.add_worksheet('Hosts Report')
topSheetRow = 0
printDBG(2, "Generating host sheets")
for hostName in sorted(listOfHosts):
topSheet.write(0, 0, "HOSTNAME")
topSheet.write(1, 0, "Account Number")
topSheet.write(1, 1, "Contract Number")
topSheet.write(1, 2, "End date")
topSheet.write(1, 3, "Subscription name")
topSheetRow = 2
for host in sorted(listOfHosts):
colNum=0
topSheet.write(topSheetRow, colNum, host['name'])
topSheetRow+=1
colNum+=1
for sub in host['subs']:
topSheet.write(topSheetRow, colNum, sub['accountNum'])
colNum+=1
topSheet.write(topSheetRow, colNum, sub['contractNum'])
colNum+=1
topSheet.write(topSheetRow, colNum, sub['endDate'])
colNum+=1
topSheet.write(topSheetRow, colNum, sub['name'])
colNum+=1
printDBG(2, "Saving workbook")
workbook.close()
if __name__ == '__main__':
collectData()
if args.outfmt == 'ods':
generateODS()
elif args.outfmt == 'xlsx':
generateXLSX()