-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlist_gw.py
167 lines (131 loc) · 6.43 KB
/
list_gw.py
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
#! /usr/bin/python3
import boto3
import requests, json, urllib3, getpass, sys
import pandas as pd
import time
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
### Setting variables
all_gateways = []
instances_to_resize = []
instances_to_replace = []
init_table = {}
older_amis = {
'hvm-cloudx-aws-011519',
'hvm-cloudx-aws-041519',
'hvm-cloudx-aws-071519',
'hvm-cloudx-aws-093019'}
### Create a directory with empty values
def start_table(gw):
for i in range(1,gw):
init_table['Gateway ' + str(i)] = {}
return init_table
def create_table():
for i in range(1,len(all_gateways)):
init_table['Gateway ' + str(i)]['Number'] = ""
init_table['Gateway ' + str(i)]['Gateway Name'] = ""
init_table['Gateway ' + str(i)]['Status'] = ""
init_table['Gateway ' + str(i)]['Account'] = ""
init_table['Gateway ' + str(i)]['HA-GW'] = ""
init_table['Gateway ' + str(i)]["Instance Size"] = ""
init_table['Gateway ' + str(i)]["AMI Id"] = ""
init_table['Gateway ' + str(i)]["GW Zone"] = ""
init_table['Gateway ' + str(i)]["t3.medium supported"] = ""
init_table['Gateway ' + str(i)]["Resize"] = ""
init_table['Gateway ' + str(i)]["Replace"] = ""
return init_table
# login and store CID
def login(controller, username, password):
url = "https://" + controller + "/v1/api"
payload = {'action': 'login', 'username': username, 'password': password}
response = requests.request("POST", url, headers={}, data = payload, files = [], verify = False)
cid = response.json()["CID"]
return cid
# Create list of all Gateways
def get_all_gateways(controller, cid):
url = "https://" + controller + "/v1/api?action=list_vpcs_summary&CID=" + cid + "&acx_gw_only=no"
response = requests.request("GET", url, headers={}, data = {}, verify = False)
gw = response.json()
for i in gw['results']:
all_gateways.append(i['gw_name'])
# Get details about GWs
def populate_table(controller, cid, aws_access_key, aws_secret_key):
for idx, gateway in enumerate(all_gateways, 1):
url = "https://" + controller + "/v1/api?action=get_gateway_info&&CID=" + cid + "&gateway_name=" + gateway
response = requests.request("GET", url, headers={}, data = {}, verify = False)
gateway_desc = response.json()
print("Creating report for: ", gateway )
init_table['Gateway ' + str(idx)]['Number'] = idx
init_table['Gateway ' + str(idx)]['Gateway Name'] = gateway
init_table['Gateway ' + str(idx)]['Status'] = gateway_desc['results']['vpc_state']
init_table['Gateway ' + str(idx)]['Account'] = gateway_desc['results']['account_name']
init_table['Gateway ' + str(idx)]['HA-GW'] = gateway_desc['results']['is_hagw']
init_table['Gateway ' + str(idx)]["Instance Size"] = gateway_desc['results']['vpc_size']
init_table['Gateway ' + str(idx)]["AMI Id"] = gateway_desc['results']['gw_image_name']
init_table['Gateway ' + str(idx)]["GW Zone"] = gateway_desc['results']['gw_zone']
## Adding an extra check. Not all AWS aviability zones offer t3.medium
session = boto3.Session(aws_access_key_id=aws_access_key, aws_secret_access_key=aws_secret_key, region_name="us-east-1")
ec2_client = session.client('ec2')
response = ec2_client.describe_instance_type_offerings(LocationType='availability-zone',
Filters=
[
{'Name': 'instance-type','Values': ['t3.medium']},
{'Name': 'location', 'Values': [gateway_desc['results']['gw_zone']]}
]
)
if response['InstanceTypeOfferings'] != []:
init_table['Gateway ' + str(idx)]["t3.medium supported"] = "Yes"
else:
init_table['Gateway ' + str(idx)]["t3.medium supported"] = '-'
if (("micro" in gateway_desc['results']['vpc_size'] or "small" in gateway_desc['results']['vpc_size']) and (response['InstanceTypeOfferings'] != [])):
init_table['Gateway ' + str(idx)]["Resize"] = "Yes"
instances_to_resize.append(gateway_desc['results']['gw_name'])
else:
init_table['Gateway ' + str(idx)]["Resize"] = "-"
if gateway_desc['results']['gw_image_name'] in older_amis:
init_table['Gateway ' + str(idx)]["Replace"] = "Yes"
instances_to_replace.append(gateway_desc['results']['gw_name'])
else:
init_table['Gateway ' + str(idx)]["Replace"] = "-"
return init_table
### Resizing to t3.medum function
def resize_instance(controller, cid, instances_to_resize):
for gw in instances_to_resize:
print("Resizing GW: ", gw)
url = "https://" + controller + "/v1/api"
payload = {'action': 'change_gateway_size','CID': cid, 'gw_name': gw, 'gw_size': 't3.medium'}
response = requests.request("POST", url, headers={}, data = payload, files = [], verify = False)
gateway_size = response.json()
print(gateway_size)
### AMI replacing function
def replace_instance(controller, cid, instances_to_replace):
for gw in instances_to_replace:
print("Replacing GW: ", gw)
url = "https://" + controller + "/v1/api"
payload = {'action': 'replace_gateway', 'CID': cid,'gateway_name': gw}
response = requests.request("POST", url, headers={}, data = payload, files = [], verify = False)
gateway_ami = response.json()
def main():
controller = input("Enter Controller IP: ")
username = input("Enter Controller username: ")
password = getpass.getpass(prompt='Enter Controller password: ')
aws_access_key = input("Enter AWS Access Key: ")
aws_secret_key = getpass.getpass(prompt="Enter AWS Secret Key: ")
try:
cid = login(controller, username, password)
except:
print("Unable to connect to Controller: ", controller)
sys.exit(1)
gateways = get_all_gateways(controller, cid)
empty_table = start_table(len(all_gateways)+1)
main_table = create_table()
final_table = populate_table(controller, cid, aws_access_key, aws_secret_key)
df = pd.DataFrame(final_table).T
df_index = df.set_index('Number')
df_index.to_csv('gw_list.csv')
df_index.to_html('gw_list.html', justify='center')
print("Created file gw_list.html")
print("Created file gw_list.csv")
# resize_instance(controller, cid, instances_to_resize)
# replace_instance(controller, cid, instances_to_replace)
if __name__ == "__main__":
main()