-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcheck_heat_stacks.py
More file actions
executable file
·134 lines (106 loc) · 4.89 KB
/
check_heat_stacks.py
File metadata and controls
executable file
·134 lines (106 loc) · 4.89 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import requests
import json
def authenticate(method=None,ip=None, user=None, opass=None, proj=None, renew=None):
if method == None:
token_file="token_"+user+"_"+proj
if renew == 0:
try:
file=open(token_file,"r")
fout=file.readline().split(",")
token=fout[0]
proj_id=fout[1]
file.close()
print("INFO: Got token from file: "+ token_file)
except:
print("INFO: Failed to get token from file, renewing token")
token, proj_id=authenticate(ip=ip, user=user, opass=opass, proj=proj, renew=1)
if renew == 1:
url = "http://"+ip+":5000/v3/auth/tokens"
payload = "{ \"auth\": { \"identity\": { \"methods\": [ \"password\" ], \"password\": { \"user\": { \"domain\": { \"name\": \"Default\" }, \"name\": \""+user+"\", \"password\": \""+opass+"\" } } }, \"scope\": { \"project\": { \"domain\": { \"name\": \"Default\" }, \"name\": \""+proj+"\" } } } }"
headers = {
'content-type': "application/json",
'cache-control': "no-cache",
}
response = requests.request("POST", url, data=payload, headers=headers)
token=response.headers['X-Subject-Token']
data_json=response.text
data=json.loads(data_json)
if data['token']['project']['name'] == proj:
proj_id=(data['token']['project']['id'])
file=open(token_file,"w")
file.write(token+","+proj_id)
file.close()
return token, proj_id
else:
return method
@authenticate
def printStackInfo(ip, user, opass, proj):
token, proj_id=authenticate(ip=ip, user=user, opass=opass, proj=proj, renew=0)
get_instances = "http://"+ip+":8774/v2.1/servers/detail"
get_stacks = "http://"+ip+":8004/v1/"+proj_id+"/stacks"
headers = {
'accept': "application/json",
'x-auth-token': token,
'cache-control': "no-cache",
}
instance_response=requests.request("GET", get_instances, headers=headers).status_code
if instance_response == 401:
print("INFO: Token got from token file expired, renewing")
headers['x-auth-token'], proj_id=authenticate(ip=ip, user=user, opass=opass, proj=proj, renew=1)
stack_data_json=requests.request("GET", get_stacks, headers=headers).text
stack_data=json.loads(stack_data_json)
print("OUTPUT: Stacks Information")
for stack in stack_data['stacks']:
print("Stack Name: ",stack['stack_name'], " Stack Link: ",stack['links'][0]['href'])
@authenticate
def printInstanceInfo(ip, user, opass, proj):
token, proj_id=authenticate(ip=ip, user=user, opass=opass, proj=proj, renew=0)
get_instances = "http://"+ip+":8774/v2.1/servers/detail"
headers = {
'accept': "application/json",
'x-auth-token': token,
'cache-control': "no-cache",
}
instance_response=requests.request("GET", get_instances, headers=headers).status_code
if instance_response == 401:
print("INFO: Token got from token file expired, renewing")
headers['x-auth-token'], proj_id=authenticate(ip=ip, user=user, opass=opass, proj=proj, renew=1)
instance_data_json=requests.request("GET", get_instances, headers=headers).text
instance_data=json.loads(instance_data_json)
print("OUTPUT: Instances Information")
for server in instance_data['servers']:
try:
print("Instance Name: ", server['name']," Internal Ip: ",server['addresses']['int'][0]['addr'], " Floating Ip: ",server['addresses']['int'][1]['addr'], " Instance Metadata Type: ",server['metadata']['type'])
except IndexError:
print("Instance Name: ",server['name']," Internal Ip: ",server['addresses']['int'][0]['addr']," Instance Metadata Type: ", server['metadata']['type'])
except:
pass
@authenticate
def printLoadBalancerInfo(ip, user, opass, proj):
token, proj_id=authenticate(ip=ip, user=user, opass=opass, proj=proj, renew=0)
get_instances = "http://"+ip+":8774/v2.1/servers/detail"
get_lbaasinfo = "http://"+ip+":9696/v2.0/lb/pools"
headers = {
'accept': "application/json",
'x-auth-token': token,
'cache-control': "no-cache",
}
instance_response=requests.request("GET", get_instances, headers=headers).status_code
if instance_response == 401:
print("INFO: Token got from token file expired, renewing")
headers['x-auth-token'], proj_id=authenticate(ip=ip, user=user, opass=opass, proj=proj, renew=1)
lbaas_data_json=requests.request("GET", get_lbaasinfo, headers=headers).text
lbaas_data=json.loads(lbaas_data_json)
print ("OUTPUT: Load Balancer Information")
for lb in lbaas_data['pools']:
print("LB Name: ", lb['name'], "LB Pool ID: ", lb['id'])
def main():
openstack_ip=""
project_user_name=""
project_user_pass=""
project_name=""
printStackInfo(openstack_ip, project_user_name, project_user_pass, project_name)
printLoadBalancerInfo(openstack_ip, project_user_name, project_user_pass, project_name)
printInstanceInfo(openstack_ip, project_user_name, project_user_pass, project_name)
if __name__ == "__main__":
main()