-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathawsAPI.py
More file actions
615 lines (500 loc) · 19.6 KB
/
Copy pathawsAPI.py
File metadata and controls
615 lines (500 loc) · 19.6 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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
import boto3
import os, stat
from time import sleep
from random import randint
ACCESS_ID = os.environ.get("aws_access_key_id")
SECRET_KEY = os.environ.get("aws_secret_access_key")
class AWS():
def __init__(self,region):
self.client = boto3.client('ec2',
aws_access_key_id=ACCESS_ID,
aws_secret_access_key=SECRET_KEY,
region_name=region)
self.ldblcr = boto3.client('elb',
aws_access_key_id=ACCESS_ID,
aws_secret_access_key=SECRET_KEY,
region_name=region)
self.autoscale = boto3.client('autoscaling',
aws_access_key_id=ACCESS_ID,
aws_secret_access_key=SECRET_KEY,
region_name=region)
self.used_private_ip = []
def create_keypair(self,name):
check_exists = self.check_key_pair(name)
if check_exists:
self.client.delete_key_pair(KeyName=name)
print("\033[1;31;49mAPAGOU KEY PAIR\033[0;49;49m")
response = self.client.create_key_pair(KeyName=name)
try:
os.chmod(name, stat.S_IWRITE)
except:
pass
with open(name,'w') as private:
private.write(response['KeyMaterial'])
os.chmod(name, stat.S_IREAD)
print("\033[1;32;49mCRIOU KEY PAIR\033[0;49;49m")
def create_sec_group(self,name,desc,ports):
check_exists = self.check_sec_group(name)
while check_exists:
try:
self.client.delete_security_group(GroupName=name)
sleep(1)
except:
pass
check_exists = self.check_sec_group(name)
print("\033[1;31;49mAPAGOU SECURITY GROUP\033[0;49;49m",name)
response = self.client.create_security_group(Description=desc, GroupName=name)
sleep(2)
print("\033[1;32;49mCRIOU SECURITY GROUP\033[0;49;49m",name)
self.liberate_port(name,ports)
return response['GroupId']
def liberate_port(self,name,ports):
self.client.authorize_security_group_ingress(
GroupName=name,
IpPermissions=ports
)
print("\033[1;32;49mLIBEROU PORTAS SECURITY GROUP\033[0;49;49m")
def check_sec_group(self,name):
print('\033[1;33;49mVERIFICA EXISTENCIA SECURITY GROUP\033[0;49;49m',name)
response = self.client.describe_security_groups()
for i in response['SecurityGroups']:
if i['GroupName'] == name:
return (True)
def check_key_pair(self,name):
print('\033[1;33;49mVERIFICA EXISTENCIA KEY PAIR\033[0;49;49m')
response = self.client.describe_key_pairs()
for i in response['KeyPairs']:
if i['KeyName'] == name:
return (True)
def create_instance(self,key_pair_name,sec_group_name_id,userData,imageID,privateAddress,subnet_id):
print('\033[1;32;49mCRIANDO INSTÂNCIA\033[0;49;49m',sec_group_name_id)
print('\033[1;36;49mISSO PODE LEVAR ALGUNS MINUTOS\033[0;49;49m')
if not privateAddress == None:
response = self.client.run_instances(
InstanceType='t2.micro',
ImageId=imageID,
KeyName=key_pair_name,
MaxCount=1,
MinCount=1,
SecurityGroupIds=[
sec_group_name_id
],
SubnetId=subnet_id,
TagSpecifications=[
{
'ResourceType': 'instance',
'Tags': [
{
'Key': 'Owner',
'Value': 'Andre'
},
]
},
],
UserData=userData,
PrivateIpAddress=privateAddress
)
else:
response = self.client.run_instances(
InstanceType='t2.micro',
ImageId=imageID,
KeyName=key_pair_name,
MaxCount=1,
MinCount=1,
SecurityGroupIds=[
sec_group_name_id
],
TagSpecifications=[
{
'ResourceType': 'instance',
'Tags': [
{
'Key': 'Owner',
'Value': 'Andre'
},
]
},
],
UserData=userData
)
waiter = self.client.get_waiter('instance_status_ok')
waiter.wait(InstanceIds=[response['Instances'][0]['InstanceId']])
return (response['Instances'][0]['InstanceId'])
def delete_instances(self):
instances_ids = self.get_instance_id()
if len(instances_ids) > 0:
instances_ids = self.instance_filter(instances_ids)
try:
self.client.terminate_instances(
InstanceIds=instances_ids
)
print('\033[1;31;49mTERMINANDO...\033[0;49;49m')
waiter = self.client.get_waiter('instance_terminated')
waiter.wait(InstanceIds=instances_ids)
except:
pass
def get_instance_id(self):
print("\033[1;33;49mCHECANDO INSTÂNCIAS\033[0;49;49m")
response = self.client.describe_instances(
Filters=[
{
'Name': 'tag:Owner',
'Values': [
'Andre',
]
},
]
)
instances_ids = []
for i in response['Reservations']:
instances_ids.append(i['Instances'][0]['InstanceId'])
return instances_ids
def instance_filter(self,instances_ids):
print('\033[1;33;49mCHECANDO STATUS\033[0;49;49m')
response = self.client.describe_instance_status(
InstanceIds=instances_ids,
IncludeAllInstances=True
)
instances_ids = []
for i in response['InstanceStatuses']:
if(not i['InstanceState']['Name'] == 'terminated'):
instances_ids.append(i['InstanceId'])
return instances_ids
def create_image(self,instance_id, image_name):
print('\033[1;32;49mINICIALIZANDO\033[0;49;49m')
waiter = self.client.get_waiter('instance_running')
waiter.wait(InstanceIds=[instance_id])
print('\033[1;33;49mAGUARDANDO STATUS OK\033[0;49;49m')
waiter = self.client.get_waiter('instance_status_ok')
waiter.wait(InstanceIds=[instance_id])
print('\033[1;31;49mSTOPPING...\033[0;49;49m')
response = self.client.stop_instances(InstanceIds=[instance_id])
waiter = self.client.get_waiter('instance_stopped')
waiter.wait(InstanceIds=[instance_id])
print('\033[1;32;49mCRIANDO IMAGEM\033[0;49;49m')
response = self.client.create_image(
InstanceId=instance_id,
Name=image_name
)
waiter = self.client.get_waiter('image_available')
waiter.wait(ImageIds=[response['ImageId']])
self.client.terminate_instances(InstanceIds=[instance_id])
def delete_image(self,img_name):
print('\033[1;31;49mAPAGANDO IMAGENS\033[0;49;49m')
response = self.client.describe_images(
Filters=[
{
'Name': 'name',
'Values': [
img_name,
]
},
]
)
try:
img_id = response['Images'][0]['ImageId']
response = self.client.deregister_image(ImageId=img_id)
except:
pass
def create_load_balancer(self,name,sec_inst_id,inst_port):
print('\033[1;32;49mCRIANDO LOAD BALANCER\033[0;49;49m')
response = self.ldblcr.create_load_balancer(
LoadBalancerName=name,
Listeners=[{
'Protocol': 'HTTP',
'LoadBalancerPort': 80,
'InstancePort': inst_port
}],
AvailabilityZones=['us-east-1a','us-east-1b','us-east-1c','us-east-1d','us-east-1e','us-east-1f'],
SecurityGroups=[sec_inst_id],
Tags=[
{
'Key': 'name',
'Value': 'DecoLB'
},
]
)
return response['DNSName']
def delete_ld_balancer(self,name):
try:
print('\033[1;31;49mDELETANDO LOAD BALANCER\033[0;49;49m')
self.ldblcr.delete_load_balancer(LoadBalancerName=name)
except:
pass
def create_l_config(self,name,img_name,key_p_name,sec_g_id):
print('\033[1;32;49mCRIANDO LAUNCH CONFIGURATION\033[0;49;49m')
response = self.client.describe_images(
Filters=[
{
'Name': 'name',
'Values': [
img_name,
]
},
]
)
img_id = response['Images'][0]['ImageId']
self.autoscale.create_launch_configuration(
LaunchConfigurationName=name,
ImageId=img_id,
KeyName=key_p_name,
SecurityGroups=sec_g_id,
InstanceType='t2.micro'
)
def delete_l_config(self,name):
try:
print('\033[1;31;49mDELETANDO LAUNCH CONFIGURATION\033[0;49;49m')
self.autoscale.delete_launch_configuration(LaunchConfigurationName=name)
except:
pass
def create_auto_scaling(self,name,l_config_name,load_name,availability_zone):
print('\033[1;32;49mCRIANDO AUTO SCALING GROUP\033[0;49;49m')
self.autoscale.create_auto_scaling_group(
AutoScalingGroupName=name,
LaunchConfigurationName=l_config_name,
MinSize=1,
MaxSize=5,
AvailabilityZones=[availability_zone],
LoadBalancerNames=load_name
)
def delete_auto_scaling(self,name):
try:
print('\033[1;31;49mDELETANDO AUTO SCALING GROUP\033[0;49;49m')
self.autoscale.delete_auto_scaling_group(
AutoScalingGroupName=name,
ForceDelete=True
)
while self.check_autoscalling(name):
sleep(10)
except:
pass
def check_autoscalling(self,name):
print('\033[1;33;49mVERIFICA EXISTENCIA AUTOSCALLING GROUP\033[0;49;49m',name)
response = self.autoscale.describe_auto_scaling_groups(
AutoScalingGroupNames=[name]
)
return (not(len(response['AutoScalingGroups']) == 0))
def create_elastic_ip(self):
print('\033[1;32;49mCRIANDO ELASTIC IP\033[0;49;49m')
response = self.client.allocate_address(Domain='vpc')
self.client.create_tags(
Resources=[
response['AllocationId'],
],
Tags=[
{
'Key': 'Owner',
'Value': 'Andre'
},
]
)
return(response['PublicIp'])
def destroy_elastic_ip(self):
print('\033[1;31;49mDELETANDO ELASTIC IP\033[0;49;49m')
response = self.client.describe_addresses(
Filters=[
{
'Name': 'tag:Owner',
'Values': [
'Andre',
]
},
]
)
if not len(response['Addresses']) == 0:
for i in response['Addresses']:
response = self.client.release_address(
AllocationId=i['AllocationId']
)
def alocate_elastic_ip(self, pub_ip, inst_id):
print('\033[1;32;49mALOCANDO INSTANCIA COM IP ELASTICO\033[0;49;49m')
response = self.client.associate_address(
InstanceId=inst_id,
PublicIp=pub_ip,
AllowReassociation=True
)
def get_thr_ocur(self,w_string,w_char,times):
count = 0
for i in range(len(w_string)):
if w_string[i] == w_char:
count += 1
if count == times:
return i
def get_local_fix_ip(self):
print('\033[1;32;49mALOCANDO IP PRIVADO FIXO\033[0;49;49m')
response = self.client.describe_vpcs()
vpc_id = response['Vpcs'][0]['VpcId']
response = self.client.describe_subnets(
Filters=[
{
'Name': 'vpc-id',
'Values': [
vpc_id,
]
},
],
)
cidr_vpc = response['Subnets'][0]['CidrBlock']
subnet_id = response['Subnets'][0]['SubnetId']
availability_zone = response['Subnets'][0]['AvailabilityZone']
sec_dot_place = self.get_thr_ocur(cidr_vpc,'.',3)
new_fixed_ip = cidr_vpc[:sec_dot_place]+"."+str(randint(0,254))
while new_fixed_ip in self.used_private_ip:
new_fixed_ip = cidr_vpc[:sec_dot_place]+"."+str(randint(0,254))
self.used_private_ip.append(new_fixed_ip)
return new_fixed_ip, subnet_id, availability_zone
r1 = "us-east-1"
r2 = "us-east-2"
print("\n\nCOMECOU REGIÃO",r1)
open_port_ws = 8080
northVirginia = AWS(r1)
ohio = AWS(r2)
ohio.destroy_elastic_ip()
ws_elastic_ip_ohio = ohio.create_elastic_ip()
private_ip_red_nv,subnet_id_red_nv, availability_zone_red_nv = northVirginia.get_local_fix_ip()
user_data_ws_nv = '''#!/bin/bash
git clone https://github.com/decoejz/redirect_ws_cloud.git
cd redirect_ws_cloud
source comandos.sh
touch /etc/init.d/runWebServer.sh
echo '#!/bin/bash
export IP_REDIRECT=\"'''+str(private_ip_red_nv)+'''\"
export R_PORT='''+str(open_port_ws)+'''
python3 /redirect_ws_cloud/webServer.py' >> /etc/init.d/runWebServer.sh
chmod 755 /etc/init.d/runWebServer.sh
echo '
[Service]
ExecStart=/etc/init.d/runWebServer.sh
[Install]
WantedBy=default.target' >> /etc/systemd/system/runWebServer.service
systemctl enable runWebServer
reboot'''
user_data_red_nv='''#!/bin/bash
git clone https://github.com/decoejz/redirect_ws_cloud.git
cd redirect_ws_cloud
source comandos.sh
touch /etc/init.d/runWebServer.sh
echo '#!/bin/bash
export IP_REDIRECT=\"'''+str(ws_elastic_ip_ohio)+'''\"
export R_PORT='''+str(open_port_ws)+'''
python3 /redirect_ws_cloud/webServer.py' >> /etc/init.d/runWebServer.sh
chmod 755 /etc/init.d/runWebServer.sh
echo '
[Service]
ExecStart=/etc/init.d/runWebServer.sh
[Install]
WantedBy=default.target' >> /etc/systemd/system/runWebServer.service
systemctl enable runWebServer
reboot'''
key_pair_name = "key-pair-north-virginia"
sec_group_name = 'secgroup-instancia-deco'
img_name = 'P1 Deco'
load_name = 'LoadProjDeco'
launch_name = 'LaunchConfigDeco'
auto_name = 'AutoScaleDeco'
inst_ports = [{'FromPort': open_port_ws,'IpProtocol': 'tcp','IpRanges': [{'CidrIp': '0.0.0.0/0'},],'ToPort': open_port_ws}]
load_ports = [{'FromPort': 80,'IpProtocol': 'tcp','IpRanges': [{'CidrIp': '0.0.0.0/0'},],'ToPort': 80}]
betweness_port = [{'FromPort': 0,'IpProtocol': 'tcp','IpRanges': [{'CidrIp': '0.0.0.0/0'},],'ToPort': 65000}]
northVirginia.destroy_elastic_ip()
northVirginia.delete_auto_scaling(auto_name)
northVirginia.delete_l_config(launch_name)
northVirginia.delete_ld_balancer(load_name)
northVirginia.delete_instances()
northVirginia.delete_image(img_name)
betw_el_ip = northVirginia.create_elastic_ip()
northVirginia.create_keypair(key_pair_name)
sec_inst_id = northVirginia.create_sec_group(sec_group_name,'Security Group Instancia projeto',inst_ports)
sec_load_id = northVirginia.create_sec_group('sec-group-load-deco','Security Group Load Balancer projeto',load_ports)
sec_betwenn_id = northVirginia.create_sec_group('sec-group-betwenn-deco','Security Group para a instancia intermediaria',betweness_port)
ins_id = northVirginia.create_instance(key_pair_name, sec_inst_id,user_data_ws_nv,'ami-07d0cf3af28718ef8',None,None)
betwen_id = northVirginia.create_instance(key_pair_name, sec_betwenn_id,user_data_red_nv,'ami-07d0cf3af28718ef8',private_ip_red_nv,subnet_id_red_nv)
northVirginia.alocate_elastic_ip(betw_el_ip,betwen_id)
northVirginia.create_image(ins_id,img_name)
acess_ip = northVirginia.create_load_balancer(load_name,sec_load_id,open_port_ws)
northVirginia.create_l_config(launch_name,img_name,key_pair_name,[sec_inst_id])
northVirginia.create_auto_scaling(auto_name,launch_name,[load_name],availability_zone_red_nv)
print('FINALIZOU',r1)
##########REGIAO 2################REGIAO 2####################REGIAO 2################REGIAO 2#######
print("\n\nCOMECOU REGIÃO",r2)
ohio.delete_instances()
private_ip_ws_ohio, subnet_id_ws_ohio, availability_zone_ws_ohio = ohio.get_local_fix_ip()
private_ip_db_ohio, subnet_id_db_ohio, availability_zone_db_ohio = ohio.get_local_fix_ip()
#Fixo para todas as maquinas de Ohio
key_pair_name_ohio = 'key-pair-ohio'
inst_type_ohio = 'ami-0d5d9d301c853a04a' #t2.micro
#Especifico para a maquina de WebServer
user_data_ws_ohio = '''#!/bin/bash
git clone https://github.com/decoejz/APS-cloud-comp.git
cd APS-cloud-comp
echo '{
\"db_host\": \"'''+str(private_ip_db_ohio)+'''\",
\"hostname\": \"0.0.0.0\",
\"port\": '''+str(open_port_ws)+'''
}' >> /APS-cloud-comp/hosts.json
echo '{\"id\": 1}' >> /APS-cloud-comp/id.json
chmod 777 /APS-cloud-comp/id.json
chmod 777 /APS-cloud-comp/hosts.json
source comandos.sh
touch /etc/init.d/runWebServer.sh
echo '#!/bin/bash
python3 /APS-cloud-comp/webServer.py' >> /etc/init.d/runWebServer.sh
chmod 755 /etc/init.d/runWebServer.sh
echo '[Service]
ExecStart=/etc/init.d/runWebServer.sh
[Install]
WantedBy=default.target' >> /etc/systemd/system/runWebServer.service
systemctl enable runWebServer
reboot'''
sec_group_name_ws_ohio = 'web_server'
inst_ports_ws_ohio = [{'FromPort': open_port_ws,'IpProtocol': 'tcp','IpRanges': [{'CidrIp': betw_el_ip+'/32'},],'ToPort': open_port_ws}]
#Especifico para a maquina com o banco de dados
user_data_db_ohio = '''#!/bin/bash
wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list
sudo apt-get update
sudo apt-get install -y mongodb-org
echo '# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: /var/lib/mongodb
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0
# how the process runs
processManagement:
timeZoneInfo: /usr/share/zoneinfo
#security:
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options:
#auditLog:
#snmp:' >> ~/temp.conf
cp ~/temp.conf /etc/mongod.conf
rm ~/temp.conf
systemctl enable mongod
reboot'''
sec_group_name_db_ohio = 'database'
inst_ports_db_ohio = [{'FromPort': 27017,'IpProtocol': 'tcp','IpRanges': [{'CidrIp': private_ip_ws_ohio+'/32'},],'ToPort': 27017}]
ohio.create_keypair(key_pair_name_ohio)
sec_ws_id_ohio = ohio.create_sec_group(sec_group_name_ws_ohio,'Security Group Instancia WS',inst_ports_ws_ohio)
sec_db_id_ohio = ohio.create_sec_group(sec_group_name_db_ohio,'Security Group Instancia DB',inst_ports_db_ohio)
ws_inst_id = ohio.create_instance(key_pair_name_ohio, sec_ws_id_ohio,user_data_ws_ohio,inst_type_ohio,private_ip_ws_ohio,subnet_id_ws_ohio)
ohio.alocate_elastic_ip(ws_elastic_ip_ohio,ws_inst_id)
db_inst_id = ohio.create_instance(key_pair_name_ohio, sec_db_id_ohio,user_data_db_ohio,inst_type_ohio,private_ip_db_ohio,subnet_id_db_ohio)
print('FINALIZOU',r2)
print("TERMINOU\n\n")
print('Entre no endereço abaixo para acessar a aplicacao:\n{}\n\n'.format(acess_ip))