-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaerpawNodeAgent.py
More file actions
executable file
·131 lines (102 loc) · 4.47 KB
/
Copy pathaerpawNodeAgent.py
File metadata and controls
executable file
·131 lines (102 loc) · 4.47 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
#!/usr/bin/env python3
import http.server
import socketserver
from urllib.parse import urlparse
from urllib.parse import parse_qs
import subprocess
import os
import json
def runCmd(cmd):
shelledResults = subprocess.run(cmd, shell=True)
return shelledResults.returncode
class MyHttpRequestHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
# Sending an '200 OK' response
self.send_response(200)
# Setting the header
self.send_header("Content-type", "text/plain")
# Whenever using 'send_header', you also have to call 'end_headers'
self.end_headers()
print("Handling: " + self.path)
pathPiecesList = self.path.split("/")
containerStr = pathPiecesList[3]
print("zeroeth element =" + pathPiecesList[0])
respond=True
if self.path.startswith("/v1/fetchContainer/") :
respond=True
print("Doing a fetchContainer of " + containerStr)
command = "fetchContainer " + containerStore + " " + containerStr
print (command)
retCode = runCmd(command)
if retCode == 0 :
returned=bytes("OK","utf-8")
else:
returned=bytes("failed "+str(retCode),"utf-8")
elif self.path.startswith("/v1/fetchVM/") :
returned=bytes("OK","utf-8")
print ("Doing a fetchVM of " + containerStr )
command = "fetchVM " + containerStr
runCmd(command)
elif self.path.startswith("/v1/ping/") :
returned=bytes("OK","utf-8")
elif self.path.startswith("/v1/startContainer/") :
returned=bytes("OK","utf-8")
respond=True
print ("Doing a startContainer of " + containerStr )
command = "startContainer " + containerStr
runCmd(command)
elif self.path.startswith("/v1/startVM/") :
returned=bytes("OK","utf-8")
print ("Doing a startVM of " + containerStr )
command = "startVM " + containerStr
runCmd(command)
elif self.path.startswith("/v1/emitDataVolume/") :
print ("Doing an emitDataVolume of " + containerStr )
# 1: tar the directory
# 2: loop, reading a megabyte, writing a MB.
respond=False # we're doing the response in here, no need to at the bottom.
tarCommand = "cd /var/local/aerpawNodeAgent ; tar cf outbound/" + containerStr + ".tar " + containerStr
print(tarCommand)
runCmd(tarCommand)
# OK, TODO: Really need to make this handle large files by blocks,
# not by trying to do this all in one big spasming lurch.
with open("/var/local/aerpawNodeAgent/outbound/" + containerStr + ".tar", 'rb') as file:
tarFileContents=file.read(-1)
self.wfile.write(tarFileContents)
elif self.path.startswith("/v1/deleteDataVolume/") :
returned=bytes("OK","utf-8")
respond=True
print ("Doing a deleteDataVolume of " + containerStr )
rmCommand = "rm -rf /var/local/outputs/" + containerStr + " /var/local/outputs/" + containerStr + "-stdout /var/local/outputs/" + containerStr + "-stderr /var/local/outputs/" + containerStr + ".tar"
runCmd(rmCommand)
elif self.path.startswith("/v1/killContainer/") :
returned=bytes("OK","utf-8")
respond=True
print ("Doing a killContainer of " + containerStr )
killCmd="docker kill " + containerStr
runCmd(killCmd)
elif self.path.startswith("/v1/deleteContainer/") :
returned=bytes("OK","utf-8")
respond=True
print ("Doing a deleteContainer of " + containerStr )
killCmd="docker system prune -f ;docker image rm " + containerStr
runCmd(killCmd)
else :
returned=bytes("Unknown REST entrypoint","utf-8")
respond=True
# Writing the resulting contents with UTF-8
if respond == True:
self.wfile.write(returned)
return
class MyServer(socketserver.TCPServer):
allow_reuse_address=True
# MAIN
with open("/etc/aerpawNodeAgent.json") as f:
options = json.load(f)
handler_object = MyHttpRequestHandler
port = int(options["port"])
print(port)
containerStore = str( options["containerStore"])
my_server = MyServer(("",port), handler_object)
# Release the Kracken!!!
my_server.serve_forever()