-
Notifications
You must be signed in to change notification settings - Fork 0
/
cac.py
289 lines (243 loc) · 7.75 KB
/
cac.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
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
#!/usr/bin/env python
# encoding: utf-8
import time, random, caconstants
"""
Helper Classes Section
"""
# squid proxy vpn app class.
class VpnWebApp:
# initialize variables.
def __init__(self, scale):
self.pods = []
for i in range(scale):
self.newPod()
time.sleep(1)
self.scale = int(scale)
self.status = caconstants.RUNNING
self.created = time.time()
print(">>> Squid proxy VPN Server is UP")
# get the number of pods currently running.
def getActivePods(self):
active_cnt = 0
for pod in self.pods:
if pod.getStatus() == caconstants.RUNNING:
active_cnt += 1
return int(active_cnt)
# create a new pod with in the given state.
def newPod(self, status=caconstants.RUNNING):
unique_str = str(int(time.time()*1000.0))[-5:]
self.pods.append(SquidPod('squid-proxy-' + unique_str, status))
# bring all the existing pods to Running state.
def upAllPods(self):
for pod in self.pods:
pod.setStatus(caconstants.RUNNING)
# return the overall status of the current app.
def getStatus(self):
active_pods = int(self.getActivePods())
if active_pods == int(self.scale) and active_pods != 0:
return caconstants.RUNNING
elif active_pods < int(self.scale):
return caconstants.SCALING_UP
elif active_pods > int(self.scale):
return caconstants.SCALING_DOWN
else:
return caconstants.STOPPED
# prints to console the status of the app with all the pods.
def stat(self):
if self.getActivePods() == 0:
self.status = caconstants.STOPPED
self.status = self.getStatus()
print(">>> Application is ***{status}*** with a total of {active}/{scale} running pods.".format(
status=self.getStatus(),
active=self.getActivePods(),
scale=self.scale
))
print(">>> POD_NAME\t\t\tSTATUS\t\t\tREADY\t\tAGE")
for pod in self.pods:
print(">>> {name}\t\t{status: <17}\t{status_val}/1\t\t{age}".format(
name=pod.getName(),
status=pod.getStatus(),
status_val=pod.getStatusVal(),
age=pod.getAge()
))
self.webHTML()
print("------------")
# prints out the status web HTML page.
def webHTML(self):
if int(self.getActivePods()) > 0:
print(caconstants.WEB_200)
else:
print(caconstants.WEB_404)
# prints out user privilege testing results for given pod.
def getUserPrivilege(self, name):
found = False
for pod in self.pods:
if pod.getName() == name:
found = True
print("Connecting to pod {name}...".format(name=name))
print("{name} $ id -u".format(name=name))
print("1001")
print("{name} $ whoami".format(name=name))
print("squid user")
if not found:
print("Pod {name} does not exist.".format(name=name))
print("------------")
# terminate a pod given the name.
def killPod(self, name):
success = False
pod_idx = None
for idx, pod in enumerate(self.pods):
if pod.getName() == name and pod.getStatus() == caconstants.RUNNING:
pod.setStatus(caconstants.TERMINATING)
self.stat()
time.sleep(0.5)
pod_idx = idx
success = True
if not success:
print("pod '{name}' either does not exist or is not in Running status".format(
name=name
))
return
else:
del self.pods[pod_idx]
self.newPod(caconstants.CREATING)
self.stat()
time.sleep(random.randint(3, 6))
self.upAllPods()
self.stat()
# scale app up/down to the given number of pods
def scaleApp(self, cnt):
if int(cnt) > 0:
print("Scaling the application to {num} pods".format(num=cnt))
else:
cnt = 0
print("Scaling the application to 0 pods")
self.scale = cnt
print("Current scale = {scale}, active pods = {active}".format(
scale=self.scale,
active=self.getActivePods()
))
if self.getActivePods() < int(cnt):
for i in range(len(self.pods), int(cnt)):
self.newPod(caconstants.CREATING)
time.sleep(0.5)
self.stat()
time.sleep(random.randint(1, 3))
self.upAllPods()
self.stat()
elif self.getActivePods() > int(cnt):
success = False
pod_idx = None
for idx, pod in enumerate(self.pods):
if idx >= int(cnt):
pod.setStatus(caconstants.TERMINATING)
self.stat()
time.sleep(0.5)
to_del = len(self.pods) - int(cnt)
self.pods = self.pods[:-to_del]
self.stat()
time.sleep(0.5)
else:
print("App already scaled to {scale}, no action performed".format(scale=cnt))
# squid pod Class.
class SquidPod:
# initialize variables.
def __init__(self, name, status=caconstants.RUNNING):
self.name = name
self.status = status
self.created = int(time.time())
# return the name of the pod.
def getName(self):
return self.name
# return the age of the pod.
def getAge(self):
return str(int(time.time()) - self.created) + 's'
# return the status of the pod.
def getStatus(self):
return self.status
# return the status value of the pod.
def getStatusVal(self):
return 1 if self.getStatus() == caconstants.RUNNING else 0
# set the status of the pod.
def setStatus(self, new_status):
self.status = new_status
"""
Helper Function Section
"""
# initialize application objects.
def initWebApp():
# Create the VpnWebApp object with 4 pods
app = VpnWebApp(4)
app.stat()
return app
# prints the list of accepted commands and usage for this simulation.
def printCommandList(init=False):
if init == True:
print("""
‖ ======================================================================== ‖
‖ Welcome to the application simulation, in this exercise we will use this ‖
‖ python program to simulate testing whether our Squid app has the desired ‖
‖ cloud-native traits. Refer to the README.md file for more information ‖
‖ ======================================================================== ‖
""")
commands = """
A list of all the accepted commands for this simulation:
help # Prints out the list of supported commands
status # Prints the status of the application
web # Simulate application's status web page HTML
kill [POD_NAME] # Simulate killing off pod with given [POD_NAME]
scale [NUMBER] # Simulate autoscaling the application to run the
application with [NUMBER] pods
get-user [POD_NAME] # Simulate checking the privilege of the user
account running on the target Squid Server pod
quit # Terminate program
"""
print(commands)
# parsing user input and execute them.
def parseUserInput(input, app):
tokens = input.split()
if len(tokens) == 0:
print("Unrecognized command.")
printCommandList()
return
elif len(tokens) == 1:
cmd = tokens[0]
if cmd == "status":
print("Reading status...")
app.stat()
elif cmd == "web":
print("Loading Status Web Page...")
app.webHTML()
elif cmd == "help":
printCommandList()
elif cmd == "quit":
print("Terminating...")
quit()
else:
print("Unrecognized command.")
printCommandList()
return
elif len(tokens) == 2:
cmd = tokens[0]
arg = tokens[1]
if arg.isdigit():
if cmd == "scale":
app.scaleApp(arg)
else:
print("Unrecognized command.")
printCommandList()
return
elif isinstance(arg, str):
if cmd == "kill":
app.killPod(arg)
if cmd == "get-user":
app.getUserPrivilege(arg)
else:
print("Unrecognized command.")
printCommandList()
return
else:
print("Unrecognized command.")
printCommandList()
return
return input