-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathController.py
63 lines (48 loc) · 2.39 KB
/
Controller.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
import boto3
from Sqs_utils import get_queue_length
from ec2_utils import create_new_instance, terminate_instance
def control():
Access_key_ID = 'AKIARULFMIFKR62ZIRNX'
Secret_access_key = '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'
ec2 = boto3.resource('ec2', region_name='us-east-1',
aws_access_key_id=Access_key_ID,
aws_secret_access_key=Secret_access_key)
worker_names = ['worker-1','worker-2','worker-3','worker-4','worker-5','worker-6','worker-7','worker-8','worker-9','worker-10','worker-11','worker-12','worker-13','worker-14', 'worker-15', 'worker-16', 'worker-17','worker-18','worker-19']
current_workers = set()
web_tier_instance = set(["i-006eff98f4ca14aac", "i-05b326253e4bb6a9a"])
states = ["running" , "stopped" , "pending"]
running_instances = []
start_and_pending = 0
for instance in ec2.instances.all():
if instance.id not in web_tier_instance and instance.state['Name'] == "running" or instance.state['Name'] == "pending":
try:
current_workers.add(instance.tags[0]['Value'])
except:
pass
start_and_pending +=1
if instance.id != web_tier_instance and instance.state['Name'] == "running":
running_instances.append(instance.id)
available_names = [i for i in worker_names if i not in current_workers]
length_of_queue = get_queue_length()
if length_of_queue == 0:
#print("Here")
if len(running_instances) > 0:
terminate_instance(running_instances.pop())
else:
pass
else:
ideal_number_of_instaces = min( max(1,length_of_queue// 4) , 19)
if start_and_pending < ideal_number_of_instaces:
start_instances = ideal_number_of_instaces - start_and_pending
for i in range(start_instances):
if len(available_names)> 0:
resp = create_new_instance(available_names.pop(0))
else:
resp = create_new_instance("Some Worker")
elif start_and_pending == ideal_number_of_instaces:
pass
else:
if len(running_instances) > 0:
terminate_instance(running_instances.pop())
else:
pass