-
Notifications
You must be signed in to change notification settings - Fork 3
/
tower_diagnose
executable file
·206 lines (179 loc) · 7.44 KB
/
tower_diagnose
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib3
import requests
import ConfigParser
import datetime
import glob
import tarfile
import os
import json
import shutil
# Importing Ansible Tower python libraries
import tower_cli
from tower_cli import get_resource
from tower_cli import conf
#Disabling Warnings in python requests module
requests.packages.urllib3.disable_warnings()
class AnsibleTower:
def __init__(self, host, username, password):
''' Constructor for this class. '''
self.host = host
self.username = username
self.password = password
# Authenticate to Tower API and get API auth Token
def auth_token(self):
try:
req = urllib3.PoolManager().request(
'POST', self.host + '/api/v2/authtoken/',
headers = {
"Content-Type": "application/json"
},
body = json.dumps({
"username": self.username,
"password": self.password
})
)
results = json.loads(req.data)
self.token = results['token']
except Exception as error:
print('Unable to retrieve AuthToken due to error: {0}'.format(error))
# Get Tower PING endpoint
def endpoint_ping(self):
try:
req = urllib3.PoolManager().request(
'GET', self.host + '/api/v2/ping/',
headers = {
"Content-Type": "application/json",
"Authorization": "Token " + self.token
}
)
results = str(json.dumps(json.loads(req.data), sort_keys=True, indent=4))
f = open('ping.tower', 'w')
f.write(results)
f.close()
except Exception as error:
print('Unable to get api PING endpoint due to error: {0}'.format(error))
# Retrieve Tower Organizations
def tower_organization(self, org_id = None):
try:
if org_id != None:
req = urllib3.PoolManager().request(
'GET', self.host + '/api/v2/organizations/' + str(org_id) + '/',
headers = {
"Content-Type": "application/json",
"Authorization": "Token " + self.token
}
)
results = str(json.dumps(json.loads(req.data), sort_keys=True, indent=4))
else:
with conf.settings.runtime_values(host=self.host, username=self.username, password=self.password):
req = tower_cli.get_resource('organization')
results = str(json.dumps(req.list(all_pages=True), sort_keys=True, indent=4))
f = open('organizations.tower','w')
f.write(results)
f.close()
except Exception as error:
print('Unable to get Tower Organization due to error: {0}'.format(error))
# Get Tower Projects
def tower_project(self, project_id = None):
try:
if project_id != None:
req = urllib3.PoolManager().request(
'GET', self.host + '/api/v2/projects/' + str(project_id) + '/',
headers = {
"Content-Type": "application/json",
"Authorization": "Token " + self.token
}
)
results = str(json.dumps(json.loads(req.data), sort_keys=True, indent=4))
else:
with conf.settings.runtime_values(host=self.host, username=self.username, password=self.password):
req = tower_cli.get_resource('project')
results = str(json.dumps(req.list(all_pages=True), sort_keys=True, indent=4))
f = open('projects.tower','w')
f.write(results)
f.close()
except Exception as error:
print('Unable to get Tower Projects due to error: {0}'.format(error))
# Get Tower Inventories
def tower_inventory(self, inventory_id = None):
try:
if inventory_id != None:
req = urllib3.PoolManager().request(
'GET', self.host + '/api/v2/inventories/' + str(inventory_id) + '/',
headers = {
"Content-Type": "application/json",
"Authorization": "Token " + self.token
}
)
results = str(json.dumps(json.loads(req.data), sort_keys=True, indent=4))
else:
with conf.settings.runtime_values(host=self.host, username=self.username, password=self.password):
req = tower_cli.get_resource('inventory')
results = str(json.dumps(req.list(all_pages=True), sort_keys=True, indent=4))
f = open('inventories.tower','w')
f.write(results)
f.close()
except Exception as error:
print('Unable to get Tower Inventories due to error: {0}'.format(error))
# Get Tower Settings
def tower_settings(self):
try:
with conf.settings.runtime_values(host=self.host, username=self.username, password=self.password):
req = tower_cli.get_resource('setting')
results = str(json.dumps(req.read(), sort_keys=True, indent=4))
f = open( 'settings.tower', 'w')
f.write(results)
f.close()
except Exception as error:
print('Unable to get Tower Settings due to error: {0}'.format(error))
# Get Tower resources based on given resource type
def get_resource(self, key = 'job'):
try:
with conf.settings.runtime_values(host=self.host, username=self.username, password=self.password):
req = tower_cli.get_resource(key)
results = str(json.dumps(req.list(all_pages=True), sort_keys=True, indent=4))
f = open( key + '.tower','w')
f.write(results)
f.close()
except Exception as error:
print('Unable to get Tower ' + key + ' due to error: {0}'.format(error))
def tower_diagnose():
time = datetime.datetime.now()
dest = 'tower-diagnose'
files = glob.glob('*.tower')
tar_name = dest + '-' + time.strftime('%Y-%m-%d-%H-%M-%S') + '.tar.bz2'
tar = tarfile.open(tar_name, 'w:bz2')
if not os.path.exists(dest):
os.makedirs(dest)
for f in files:
shutil.copy2(f, dest)
tar.add(dest)
tar.close()
print('File ' + tar_name + ' is created and stored in ' + os.getcwd())
def clean():
files = glob.glob('*.tower')
dest = 'tower-diagnose'
for item in files:
os.remove(item)
shutil.rmtree(dest)
if __name__ == '__main__':
# Read settings from config.ini
config = ConfigParser.SafeConfigParser()
config.read('config.ini')
host = config.get('tower', 'url')
username = config.get('tower', 'username')
password = config.get('tower', 'password')
tower = AnsibleTower(host, username, password)
tower.auth_token()
tower.endpoint_ping()
tower.tower_organization()
tower.tower_project()
tower.tower_inventory()
tower.tower_settings()
temp = ['credential', 'credential_type', 'instance', 'instance_group', 'project_update', 'role', 'schedule', 'team', 'user', 'job_template', 'workflow', 'workflow_job']
for i in temp:
tower.get_resource(i)
tower_diagnose()
clean()