-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdl-httprest
executable file
·183 lines (165 loc) · 6.39 KB
/
dl-httprest
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
#!/usr/bin/python3
import traceback, re, random
import os, subprocess, json, sys
import posixpath, urllib, cgi, shutil, mimetypes
import subprocess as commands
from http.server import BaseHTTPRequestHandler, HTTPServer
class DockletHandler(BaseHTTPRequestHandler):
def sys_append_log(self, text):
f = open('/var/log/dl-httprest.log', 'a')
f.write(text)
f.write('\n')
f.close()
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
self.wfile.write("Un-supported.\n".encode())
#self.wfile.close()
return
def authenticate_with_headers(self, provider=None):
[username, password] = provider if provider!=None else self.headers['Auth'].split('/', 1)
if re.match('^[a-z0-9A-Z\-\_\@]{1,20}$',username)==None:
raise Exception('illegal username!')
if username=='root':
loggedIn = False
else:
rsa_buff = commands.getoutput("cat /var/lib/docklet/global/users/%s/ssh_keys/id_rsa 2>/dev/null" % username).strip()
loggedIn = (rsa_buff != '' and password == rsa_buff)
if not loggedIn:
try:
from pam import pam
loggedIn = pam().authenticate(username, password)
except:
loggedIn = 'docklet' == username and 'docklet' == password
if not loggedIn:
raise Exception("authentication failed")
return username
def etcd_get_machines(self):
return os.listdir('/var/lib/docklet/global/machines/')
def etcd_get_random_machine(self):
cl = self.etcd_get_machines()
return cl[random.randint(0, len(cl)-1)]
def etcd_list_clusters(self, user):
try:
nodes = commands.getoutput("ls /var/lib/docklet/global/machines/*/%s-* 2>/dev/null | awk -F\- '{print $2}'" % user).split()
clusters = []
for item in nodes:
try:
datas = self.etcd_list_single_cluster(user, int(item))
clusters.append(datas)
break
except Exception as e:
pass
return clusters
except Exception as e:
self.sys_append_log(e)
return {}
def etcd_list_single_cluster(self, user, cl_id):
datas = commands.getoutput("ls /var/lib/docklet/global/machines/*/%s-%s-* 2>/dev/null | cut -b 31-" % (user, cl_id)).split()
if len(datas) > 0:
nodes = []
for node in datas:
[work_on,uuid] = node.split('/')
[owner,nat_id,node_id] = uuid.split('-')
nodes.append({'work_on':work_on, 'uuid':uuid, 'nat_id':nat_id})
return {'id': cl_id, 'owner': user, 'nodes': nodes}
raise Exception("cluster not found!")
def on_post_request(self, context, user, form):
if context.startswith('/clusters/'):
parts = context[10:].strip().split('/')
if parts[0]=="":
cls = self.etcd_list_clusters(user)
[openssh, config] = commands.getoutput("USER_NAME=%s ./dl-key" % user).split("========", 1)
return {'clusters': cls, 'openssh': openssh, 'config': config}
elif parts[0]=="create":
cls = self.etcd_list_clusters(user)
if len(cls)>0:
raise Exception("your cluster is already booted")
NAT_ID = 1
nat_list = commands.getoutput("ls /var/lib/docklet/global/machines/*/* 2>/dev/null | awk -F\- '{print $2}'")
if nat_list != None:
nat_list = nat_list.split()
while str(NAT_ID) in nat_list:
NAT_ID += 1
if NAT_ID > 255:
raise Exception("create operation failed")
uuid = '%s-%s-0' % (user, NAT_ID)
ipaddr = commands.getoutput('./dl-weave start %s' % uuid)
return {'ip': ipaddr, 'uuid': uuid, 'work_on': DockletHandler.localaddr}
elif parts[0] == "remove":
cls = self.etcd_list_clusters(user)
obj = cls[0]['nodes']
if len(obj)!=1:
raise Exception("please scalein the cluster first")
clusterInt = int(cls[0]['id'])
assert( DockletHandler.localaddr == obj[0]['work_on'] )
if commands.getoutput('./dl-weave stop %s-%s-0' % (user, clusterInt)) == None:
raise Exception("exit operation failed")
return {'master': DockletHandler.localaddr, 'natid': clusterInt}
elif parts[0] == "restart":
cls = self.etcd_list_clusters(user)
obj = cls[0]['nodes']
status = []
for s in obj:
[mac, uuid, ipaddr] = [s['work_on'], s['uuid'], s['nat_id']]
assert( DockletHandler.localaddr == mac)
status.append({"uuid": uuid, "status": commands.getoutput('./dl-weave start %s' % uuid) != None})
return {"status": status}
elif parts[0] == "scalein":
cls = self.etcd_list_clusters(user)
obj = cls[0]['nodes']
if len(obj)<=1:
raise Exception("no more slaves to scale in")
s = max(obj, key=lambda x: int(x['uuid'].split('-')[-1]))
[mac, uuid, ipaddr] = [s['work_on'], s['uuid'], s['nat_id']]
commands.getoutput('./dl-weave stop %s' % uuid)
assert( DockletHandler.localaddr == mac)
return {'ip':ipaddr, 'uuid':uuid, 'work_on':DockletHandler.localaddr}
elif parts[0] == "scaleout":
cls = self.etcd_list_clusters(user)
obj = cls[0]['nodes']
limit = 4 * len(self.etcd_get_machines())
if len(obj)>=limit:
raise Exception("no more resources to scale out")
node_id = len(obj)
clusterInt = int(cls[0]['id'])
uuid = '%s-%s-%s' % (user, clusterInt, node_id)
ipaddr = commands.getoutput('./dl-weave start %s' % uuid)
return {'ip':ipaddr, 'uuid':uuid, 'work_on': DockletHandler.localaddr}
raise Exception('unsupported request!')
def do_POST(self):
try:
self.sys_append_log("[REQ] POST %s" % self.path)
length = int(self.headers['content-length'])
if length>10240000:
raise Exception("data too large, handler cancelled")
form = cgi.FieldStorage(fp=self.rfile, headers=self.headers,environ={'REQUEST_METHOD':'POST','CONTENT_TYPE': "text/html"})
try:
key = form['key'].file.read().strip()
except:
key = form['key'].value
try:
key = key.decode()
except:
pass
username = self.authenticate_with_headers([form['user'].value, key.strip()])
context = self.path.split('?')[0]
if not context.endswith("/"):
context = context + "/"
obj = {'success':True, 'data': self.on_post_request(context, username, form)}
except Exception as e:
self.sys_append_log(traceback.format_exc())
obj = {'success':False, 'message': str(e)}
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
self.wfile.write(json.dumps(obj).encode())
self.wfile.write('\n'.encode())
#self.wfile.close()
return
if __name__ == '__main__':
os.chdir(os.path.dirname(sys.argv[0]))
DockletHandler.localaddr = commands.getoutput('./dl-join network')
server = HTTPServer(('0.0.0.0', 8086), DockletHandler)
server.serve_forever()