-
Notifications
You must be signed in to change notification settings - Fork 3
/
app_v3.py
469 lines (344 loc) · 15.2 KB
/
app_v3.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
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
from flask import Flask, render_template, request, jsonify
import os, time
import base64
import hashlib
import yaml
import threading
from Crypto.Cipher import AES
app = Flask(__name__)
baseImageId = '539c2be37d94' # kasm-1.14.0 이미지
#baseImageId = '1692c5f95a70e' # 로컬 이미지
BS = 16
pad = (lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS).encode())
unpad = (lambda s: s[:-ord(s[len(s)-1:])])
extractNodeInfos = dict()
extractPodInfos = dict()
extractNodeCPUs = dict()
# deployment pod 만드는 함수
def generateDeploymentPodYaml(deploymentName, containerName, imageName, servicePort) :
deploymentDefinition = {
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": {"name": deploymentName},
"spec": {
"replicas": 1,
"selector": {
"matchLabels": {
"app": "webdesktop", # service에서 pod를 선택할 때 구별하는 용도
"port": str(servicePort) # port label 추가
}
},
"template": {
"metadata": {
"labels": {
"app": "webdesktop", # 새로운 pod가 생성될 때 template 정의
"port": str(servicePort) # port label 추가
}
},
"spec": {
"containers": [
{
"name": containerName,
"image": imageName,
"ports": [{"containerPort": 6901}], # container 포트는 이미지 받을 때부터 열려있었던 포트인 6901로 접속해야 가능
}
],
"imagePullSecrets": [{"name": "harbor"}] # harbor라는 이름의 kubeconfig.yaml 파일
}
}
}
}
# YAML로 변환하여 문자열로 반환합니다.
deploymentYaml = yaml.dump(deploymentDefinition, default_flow_style=False)
return deploymentYaml
# service pod를 만드는 함수 (pod를 외부로 노출하기 위함)
def generateServiceYaml(serviceName, servicePort, nodePort):
# Service의 기본 구조를 딕셔너리로 정의합니다.
serviceDefinition = {
"apiVersion": "v1",
"kind": "Service",
"metadata": {"name": serviceName},
"spec": {
"type": "NodePort",
"selector": {
"app": "webdesktop",
"port": str(servicePort) # port label 추가
},
"ports": [
{
"port": int(servicePort),
"targetPort": 6901, # deployment의 containerPort와 일치해야 함
"nodePort": int(nodePort) # node_port는 30000~32768
}
]
}
}
# YAML로 변환하여 문자열로 반환합니다.
serviceYaml = yaml.dump(serviceDefinition, default_flow_style=False)
return serviceYaml
# node의 이름과 ip를 추출하기 위한 용도
def extractNodeInfo():
result = os.popen("kubectl get nodes -o wide --kubeconfig /root/kubeconfig.yml").read()
print("result:", result)
nodeInfoList = result.split('\n')[1:-1]
print("nodeInfo: ", nodeInfoList)
for nodeInfo in nodeInfoList:
node = nodeInfo.split()
nodeName, nodeExternalIp = node[0], node[6]
extractNodeInfos[nodeName] = nodeExternalIp
print("extractN: ", extractNodeInfos)
return extractNodeInfos
# pod의 external ip를 알기 위한 함수
def extractPodInfo():
result = os.popen("kubectl get pods -o wide --kubeconfig /root/kubeconfig.yml").read()
print("result:", result)
podInfoList = result.split('\n')[1:-1]
print("podInfo: ", podInfoList)
for podInfo in podInfoList:
pod = podInfo.split()
podName, nodeName = pod[0], pod[6]
extractPodInfos[podName] = nodeName
return extractPodInfos
# pod의 external ip를 알기 위한 함수
def extractNodeIpOfPod(nodeList):
podList = extractPodInfo()
for _, nodeName in podList.items():
if nodeName in nodeList:
return extractNodeInfos[nodeName]
return "Not Found"
# Pod yaml로 생성하기
def applyPodCmd(yamlFilePath):
return "kubectl apply -f " + yamlFilePath + " --kubeconfig /root/kubeconfig.yml"
# label로 Pod 이름 조회하기
def getPodName(port) :
return "kubectl get pod -l port="+port+" -o name --kubeconfig /root/kubeconfig.yml"
# pod 내부로 start.sh 복사하기
def copyScriptToPod(podName, containerName) :
return "kubectl cp /home/ubuntu/start.sh "+podName+":/tmp/ -c "+containerName+" --kubeconfig /root/kubeconfig.yml"
# deployment Pod 지우기
def deleteDeployPodCmd(deploymentName):
return "kubectl delete deployment " + deploymentName + " --kubeconfig /root/kubeconfig.yml"
# service Pod 지우기
def deleteServicePodCmd(serviceName):
return "kubectl delete service " + serviceName + " --kubeconfig /root/kubeconfig.yml"
# yaml 파일 지우기
def deleteYamlFile(yamlFilePath):
return "rm /home/yaml/ " + yamlFilePath
# 컨테이너 관련 명령어
def createContainerCmd(port, pwd, imageId) : # vm+port 이름의 컨테이너 생성
vmname = "vm"+port
return "docker create --shm-size=512m -p "+port+":6901 -e VNC_PW="+pwd+" --name "+vmname+" "+imageId
def startContainerCmd(containerId) : # containerid로 컨테이너 실행
return "docker start "+containerId
def stopContainerCmd(containerId) : # containerid로 컨테이너 중지
return "docker stop "+containerId
def deleteContainerCmd(containerId) : # containerid로 컨테이너 삭제
return "docker rm "+containerId
def copyScriptToContainer(containerId) :
return "docker cp /home/ubuntu/start.sh "+containerId+":/dockerstartup/"
def changeVncScopeAndControl(containerId, scope, control, pwd) :
return "docker exec -it --user root "+ containerId+" bash /dockerstartup/start.sh "+scope +" "+control+" "+pwd
# 이미지 관련 명령어
def createImgCmd(containerId, userId, port) : # registry.p2kcloud.com/base/userid:port 이름의 새로운 이미지 생성
return "docker commit "+containerId+" registry.p2kcloud.com/base/"+userId+":"+port
def pushImgCmd(userId, port) : # harbor에 이미지 저장
return "docker push registry.p2kcloud.com/base/"+userId+":"+port
def deleteImgCmd(imageId) : # imageid로 이미지 삭제
return "docker rmi -f "+imageId
def pullImgCmd() : # harbor에서 kasm 이미지 pull -> 이미 pull 받아짐
return "docker pull registry.p2kcloud.com/base/vncdesktop"
class AESCipher(object):
def __init__(self, key):
self.key = hashlib.sha256(key.encode()).digest()
def encrypt(self, message):
message = message.encode()
raw = pad(message)
cipher = AES.new(self.key, AES.MODE_CBC, self.__iv().encode('utf8'))
enc = cipher.encrypt(raw)
return base64.b64encode(enc).decode('utf-8')
def decrypt(self, enc):
enc = base64.b64decode(enc)
cipher = AES.new(self.key, AES.MODE_CBC, self.__iv().encode('utf8'))
dec = cipher.decrypt(enc)
return unpad(dec).decode('utf-8')
def __iv(self):
return chr(0) * 16
key = "thisiskey"
aes = AESCipher(key)
# spring 서버에서 컨테이너 생성 요청이 왔을 때, base 이미지로 컨테이너 생성하고 이미지 저장
@app.route('/create', methods=['POST'])
def create():
requestDTO = request.get_json()
print("[create requestDTO] ", requestDTO)
userId, port, pwd = str(requestDTO['id']), str(requestDTO['port']), str(requestDTO['password'])
scope, control = str(requestDTO['scope']), str(requestDTO['control'])
stream1 = os.popen(createContainerCmd(port, pwd, baseImageId))
containerId = stream1.read()[:12]
time.sleep(5)
enContainerId = aes.encrypt(containerId) # containerId 암호화
stream2 = os.popen(createImgCmd(containerId, userId, port))
imageId = stream2.read()[7:20]
enImageId = aes.encrypt(imageId) # imageId 암호화
vmName = "vm"+port
scriptPath = "/dockerstartup/start.sh"
nodePort = str(requestDTO['nodePort'])
imagePath = str(requestDTO['imagePath'])
os.popen(startContainerCmd(containerId))
os.popen(copyScriptToContainer(containerId))
os.popen(stopContainerCmd(containerId))
# Depolyment yaml 파일 생성
deploymentPodYaml = generateDeploymentPodYaml(vmName, vmName, imagePath, port)
deploymentFilePath = "/home/yaml/"+vmName+"Deployment.yaml"
with open(deploymentFilePath, 'w') as deploymentYamlFile:
deploymentYamlFile.write(deploymentPodYaml)
# Service yaml 파일 생성
servicePodYaml = generateServiceYaml(vmName, port, nodePort)
serviceFilePath = "/home/yaml/"+vmName+"Service.yaml"
with open(serviceFilePath, 'w') as serviceYamlFile:
serviceYamlFile.write(servicePodYaml)
print(deploymentPodYaml)
print(servicePodYaml)
os.popen(applyPodCmd(deploymentFilePath))
os.popen(applyPodCmd(serviceFilePath))
nodeList = extractNodeInfo()
time.sleep(60)
externalNodeIp = extractNodeIpOfPod(nodeList)
print("nodes: ", nodeList)
print("externalIp: ", externalNodeIp)
response = {
'port': port,
'containerId' : enContainerId,
'imageId' : enImageId,
'externalNodeIp': externalNodeIp
}
return jsonify(response), 200
#spring 서버에서 가상환경 로드했을 때, 이미지로 컨테이너 생성 후 새로운 이미지로 저장
@app.route('/load', methods=['POST'])
def load() :
requestDTO = request.get_json()
print("[load requestDTO] ", requestDTO)
userId, port, pwd, imageId = str(requestDTO['id']), str(requestDTO['port']), str(requestDTO['password']), str(requestDTO['key'])
deImageId = aes.decrypt(imageId)
stream1 = os.popen(createContainerCmd(port, pwd, deImageId))
newContainerId = stream1.read()[:12]
enContainerId = aes.encrypt(newContainerId)
stream2 = os.popen(createImgCmd(newContainerId, userId, port))
newImageId = stream2.read()[7:20]
enImageId = aes.encrypt(newImageId)
scope, control = str(requestDTO['scope']), str(requestDTO['control'])
vmName = "vm"+port
scriptPath = "/dockerstartup/start.sh"
nodePort = str(requestDTO['nodePort'])
imagePath = str(requestDTO['imagePath'])
os.popen(startContainerCmd(newContainerId))
os.popen(copyScriptToContainer(newContainerId))
os.popen(stopContainerCmd(newContainerId))
# Depolyment yaml 파일 생성
deploymentPodYaml = generateDeploymentPodYaml(vmName, vmName, imagePath, port)
deploymentFilePath = "/home/yaml/"+vmName+"Deployment.yaml"
with open(deploymentFilePath, 'w') as deploymentYamlFile:
deploymentYamlFile.write(deploymentPodYaml)
# Service yaml 파일 생성
servicePodYaml = generateServiceYaml(vmName, port, nodePort)
serviceFilePath = "/home/yaml/"+vmName+"Service.yaml"
with open(serviceFilePath, 'w') as serviceYamlFile:
serviceYamlFile.write(servicePodYaml)
response = {
'containerId' : enContainerId,
'imageId' : enImageId
}
return jsonify(response), 200
# spring 서버에서 컨테이너 실행 요청이 왔을 때, 컨테이너 실행
@app.route('/start', methods=['POST'])
def start():
print("hello")
print(request.get_json())
requestDTO = request.get_json()
print("[start requestDTO] ", requestDTO)
port, containerId = str(requestDTO['port']), str(requestDTO['containerId'])
deContainerId = aes.decrypt(containerId)
pwd = str(requestDTO['password'])
scope, control = str(requestDTO['scope']), str(requestDTO['control'])
vmName = "vm"+port
deploymentFilePath = "/home/yaml/"+vmName+"Deployment.yaml"
serviceFilePath = "/home/yaml/"+vmName+"Service.yaml"
print(applyPodCmd(deploymentFilePath))
print(applyPodCmd(serviceFilePath))
os.popen(applyPodCmd(deploymentFilePath))
os.popen(applyPodCmd(serviceFilePath))
time.sleep(3)
stream1 = os.popen(getPodName(port))
podName = stream1.read()[4:-1]
print("podName:", podName)
os.popen(copyScriptToPod(podName, vmName))
time.sleep(1)
changeVncScopeAndControlCmd = "kubectl exec -it "+podName+" bash /tmp/start.sh "+scope+" "+control+" "+pwd+" --kubeconfig /root/kubeconfig.yml"
os.popen(changeVncScopeAndControlCmd)
response = {
'port' : port,
'containerId' : containerId
}
return jsonify(response), 200
# spring 서버에서 컨테이너 중지 요청이 왔을 때, 컨테이너 중지
@app.route('/stop', methods=['POST'])
def stop():
requestDTO = request.get_json()
print("[stop requestDTO] ", requestDTO)
port, containerId = str(requestDTO['port']), str(requestDTO['containerId'])
deContainerId = aes.decrypt(containerId)
vmName = "vm"+port
print(deleteDeployPodCmd(vmName))
print(deleteServicePodCmd(vmName))
os.popen(deleteDeployPodCmd(vmName))
os.popen(deleteServicePodCmd(vmName))
response = {
'port' : port,
'containerId' : containerId
}
return jsonify(response), 200
# spring 서버에서 컨테이너 저장 요청이 왔을 때, 현재 컨테이너의 이미지 생성 -> 기존 이미지 삭제 -> push
@app.route('/save', methods=['POST'])
def save() :
requestDTO = request.get_json()
print("[save requestDTO] ", requestDTO)
userId, port, pwd = str(requestDTO['id']), str(requestDTO['port']), str(requestDTO['pwd'])
containerId, imageId = str(requestDTO['containerId']), str(requestDTO['imageId'])
deContainerId, deImageId = aes.decrypt(containerId), aes.decrypt(imageId)
stream1 = os.popen(createImgCmd(deContainerId, userId, port))
newImageId = stream1.read()[7:20]
enImageId = aes.encrypt(newImageId)
print("1 : ", stream1.read())
stream2 = os.popen(deleteImgCmd(deImageId))
print("2 : ", stream2.read())
stream3 = os.popen(pushImgCmd(userId, port))
print("3 : ", stream3.read())
time.sleep(3)
print("newImageId : ", newImageId)
print("ennewImageId : ", enImageId)
response = {
'containerId' : containerId,
'imageId' : enImageId
}
return jsonify(response), 200
# spring 서버에서 컨테이너 삭제 요청이 왔을 때, 컨테이너, 이미지 삭제
@app.route('/delete', methods=['POST'])
def delete():
requestDTO = request.get_json()
print("[delete requestDTO] ", requestDTO)
userId, port = str(requestDTO['id']), str(requestDTO['port'])
containerId, imageId = str(requestDTO['containerId']), str(requestDTO['imageId'])
deContainerId, deImageId = aes.decrypt(containerId), aes.decrypt(imageId)
os.popen(deleteContainerCmd(deContainerId))
os.popen(deleteImgCmd(deImageId))
vmName = "vm"+port
deploymentFilePath = "/home/yaml/"+vmName+"Deployment.yaml"
serviceFilePath = "/home/yaml/"+vmName+"Service.yaml"
os.popen(deleteYamlFile(deploymentFilePath))
os.popen(deleteYamlFile(serviceFilePath))
response = {
'port' : port,
'containerId' : containerId
}
return jsonify(response), 200
if __name__ == '__main__':
app.run('0.0.0.0', port=5000, debug=True)