-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc2.py
executable file
·147 lines (129 loc) · 4.11 KB
/
c2.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
#!/usr/bin/env python3
from collections import defaultdict
import os
import time
import sys
import boto3
import minions
from pool import Pool
ec2 = boto3.resource('ec2')
# left here by Ansible who got it from Terraform
cluster_name = open(os.path.expanduser('~/.cluster_name')).read().strip()
large_minions = minions.track_down_minions(
ec2,
dict(cluster=cluster_name,
cluster_groups='minions',
size='large'))
small_minions = minions.track_down_minions(
ec2,
dict(cluster=cluster_name,
cluster_groups='minions',
size='small'))
large_pool = Pool("LARGE", large_minions)
small_pool = Pool("SMALL", small_minions)
# maps host names to list of open claims
CLAIMS = defaultdict(list)
def print_pool(pool):
may_shrink = ", postponing shrinks" if pool.postpone_shrink else ""
print(
f"Pool {pool.name}, actual={pool.actual}, desired={pool.desired}{may_shrink}:", )
for i, (name, state, claims, minion) in enumerate(pool.members()):
state_suffix = f"/{claims}" if claims else ""
print(f"{i+1:2d} {name} state={state}{state_suffix}", end="")
observed = minion.observed_state
desired = minion.desired_state
if observed == desired:
print(f" minion={minion.observed_state}", end="")
else:
print(
f" minion={minion.observed_state}->{minion.desired_state}", end="")
print()
class Delayer:
def __init__(self, delay):
self.next_time = 0
self.delay = delay
def await(self):
now = time.time()
sleep = self.next_time - now
if sleep > 0:
time.sleep(sleep)
self.next_time = now + self.delay
delayer = Delayer(1)
quick = False
while 1:
if quick:
print('---')
quick = False
else:
delayer.await()
print("===")
small_pool.allow_shrink = large_pool.actual >= large_pool.desired
large_pool.allow_shrink = small_pool.actual >= small_pool.desired
large_pool.poll()
small_pool.poll()
print_pool(large_pool)
print()
print_pool(small_pool)
print()
minions_with_claims = []
if any(CLAIMS.values()):
print("Claims:", end="")
for i, (name, claims) in enumerate(sorted(CLAIMS.items())):
if not claims:
continue
minions_with_claims.append(name)
n = len(claims)
print(f" {i+1}=", end="")
if n == 1:
print(name, end="")
elif n > 1:
print(f"{n}*{name}", end="")
print()
cmd = input("> ").strip()
if cmd.startswith("c"):
if cmd.startswith("cl"):
pool = large_pool
elif cmd.startswith("cs"):
pool = small_pool
else:
print("Either cl or cs")
continue
c = pool.claim()
CLAIMS[c.name].append(c)
print(f"Claimed {c.name}")
quick = True
elif cmd.startswith("q"):
sys.exit(0)
elif cmd.startswith("r"):
try:
idx = int(cmd[1:]) - 1
if idx >= len(minions_with_claims):
raise ValueError("")
except ValueError:
print("Invalid index")
continue
name = sorted(minions_with_claims)[idx]
claim = CLAIMS[name].pop()
claim.release()
print(f"Released one claim on {name}")
else:
# grow/shrink the pools
for s in cmd:
if s.startswith("s"):
print("Shrink SMALL")
small_pool.desired -= 1
elif s.startswith("S"):
print("Grow SMALL")
small_pool.desired += 1
elif s.startswith("l"):
print("Shrink LARGE")
large_pool.desired -= 1
elif s.startswith("L"):
print("Grow LARGE")
large_pool.desired += 1
elif s == "":
pass
else:
print("Unknown command", s)
# Do not allow one pool to shrink while the other is growing.
# This way, a minion is killed only when its replacement has started