forked from echoma/zkui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebWindow.py
executable file
·532 lines (530 loc) · 18 KB
/
WebWindow.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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
from PyQt5 import QtCore, QtWidgets, QtWebKitWidgets, QtNetwork, QtWebKit, QtPrintSupport
from PyQt5.QtCore import QVariant, pyqtSlot
from PyQt5.QtGui import QPainter
import os
import os.path
from kazoo.client import KazooClient
from kazoo.exceptions import NoNodeError, ZookeeperError, NodeExistsError, BadVersionError, KazooException, NoChildrenForEphemeralsError, InvalidACLError, NotEmptyError
import kazoo.security
import time
import logging
import json
import sys
import pathlib
import hashlib
import base64
import traceback
import yaml
class WebPage(QtWebKitWidgets.QWebPage):
def __init__(self, parent=None):
super(WebPage, self).__init__(parent)
self.logger = logging
logging.basicConfig(format='%(levelname)s: %(message)s @ %(filename)s:%(lineno)s', level=logging.ERROR)
def javaScriptConsoleMessage(self, msg, lineNumber, sourceID):
self.logger.info(msg+"("+sourceID+":"+str(lineNumber)+")")
class WebWindow(object):
def setupUi(self, Window):
Window.setObjectName("Window")
self.centralwidget = QtWidgets.QWidget(Window)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.zk = None
self.webView = QtWebKitWidgets.QWebView(self.centralwidget)
self.webView.setObjectName("webView")
self.webView.setRenderHint(QPainter.Antialiasing, True)
self.webView.setRenderHint(QPainter.TextAntialiasing, True)
self.webView.setRenderHint(QPainter.SmoothPixmapTransform, True)
self.webView.setRenderHint(QPainter.HighQualityAntialiasing, True)
self.webView.setPage(WebPage())
frame = self.webView.page().mainFrame()
frame.javaScriptWindowObjectCleared.connect(self.initJsComm)
self.verticalLayout.addWidget(self.webView)
Window.setCentralWidget(self.centralwidget)
self.retranslateUi(Window)
QtCore.QMetaObject.connectSlotsByName(Window)
self.loadDefaultAcl()
def retranslateUi(self, Window):
_translate = QtCore.QCoreApplication.translate
Window.setWindowTitle(_translate("Window", "Zookeeper GUI"))
def loadLocalFile(self, filename):
localdir = os.path.abspath(sys.path[0])
if os.path.isfile(localdir):
localdir = os.path.dirname(localdir)
self.webView.setUrl( QtCore.QUrl.fromLocalFile(localdir+'/'+filename) )
def getCfgVar(self, name):
localdir = os.path.abspath(sys.path[0])
if os.path.isfile(localdir):
localdir = os.path.dirname(localdir)
cfg = {}
obj = None
try:
obj = open(localdir+'/cfg.json','r')
cfg = json.loads(obj.read())
except Exception as e:
logging.info(str(e))
finally:
if obj is not None:
obj.close()
if name in cfg:
return str(cfg[name])
return ''
def setCfgVar(self, name, value):
localdir = os.path.abspath(sys.path[0])
if os.path.isfile(localdir):
localdir = os.path.dirname(localdir)
cfg = {}
obj = None
try:
obj = open(localdir+'/cfg.json','r')
cfg = json.loads(obj.read())
except Exception as e:
pass
finally:
if obj is not None:
obj.close()
cfg[name] = value
obj = None
try:
obj = open(localdir+'/cfg.json','w')
obj.truncate()
obj.write(json.dumps(cfg))
except Exception as e:
logging.info(str(e))
finally:
if obj is not None:
obj.close()
def makeDigestCred(self, user, plain_pass):
m = hashlib.sha1( bytes(user,'utf8') + b':' + bytes(plain_pass,'utf8') ).digest()
return user+':'+base64.b64encode(m).strip().decode('utf8')
def initJsComm(self):
frame = self.webView.page().mainFrame()
frame.addToJavaScriptWindowObject('py',self)
@pyqtSlot(str)
def jsSetWinTitle(self, title):
_translate = QtCore.QCoreApplication.translate
self.setWindowTitle(_translate("Window", title))
@pyqtSlot(str, result=str)
def jsCheckYaml(self, s):
try:
a = yaml.load(s)
except Exception as e:
return str(e)
if a is None:
return 'Failed'
return ''
@pyqtSlot(str,result=str)
def jsGetCfg(self, name):
return self.getCfgVar(name)
@pyqtSlot(str,str)
def jsSetCfg(self, name, value):
self.setCfgVar(name, value)
def loadDefaultAcl(self):
self.updateDefaultAclCache( self.getCfgVar('defaultacl') )
def updateDefaultAclCache(self, list_str):
if list_str is not None and len(list_str)>0:
cache = json.loads(list_str)
self.default_acl_plain = []
self.default_acl = []
for one in cache:
if(one['scheme']=='world'):
self.default_acl_plain.append(one)
acl = kazoo.security.ACL( one['perm'], kazoo.security.Id('world', 'anyone') )
self.default_acl.append(acl)
elif(one['scheme']=='digest'):
self.default_acl_plain.append(one)
if 'id' in one:
acl = kazoo.security.ACL( one['perm'], kazoo.security.Id('digest', one['id']) )
else:
acl = kazoo.security.ACL( one['perm'], kazoo.security.Id('digest', self.makeDigestCred(one['user'],one['pass'])) )
self.default_acl.append(acl)
elif(one['scheme']=='ip'):
self.default_acl_plain.append(one)
acl = kazoo.security.ACL( one['perm'], kazoo.security.Id('ip', one['ip']) )
self.default_acl.append(acl)
else:
self.default_acl_plain = []
self.default_acl = None
@pyqtSlot(str,result=str)
def jsSetDefaultAcl(self, list_str):
self.updateDefaultAclCache(list_str)
self.setCfgVar('defaultacl', json.dumps(self.default_acl_plain))
@pyqtSlot(result=str)
def jsGetDefaultAcl(self):
return json.dumps(self.default_acl_plain)
@pyqtSlot()
def jsGetZk(self):
return self.zk
@pyqtSlot(result=int)
def jsZkIsConnected(self):
if self.zk is not None:
return int(self.zk.state=='CONNECTED')
return 0
@pyqtSlot(str, str, result=str)
def jsZkConnect(self,host, auth_list_str):
try:
if self.zk is not None:
#self.zk.remove_listener(self.onZkStateChange)
self.zk.stop()
self.zk.close()
self.zk = KazooClient(hosts=host)
#self.zk.add_listener(self.onZkStateChange)
self.zk.start(15)
auth_list = json.loads(auth_list_str)
for one in auth_list:
cred = self.makeDigestCred(one['user'], one['pass'])
self.zk.add_auth('digest', one['user']+':'+one['pass'])
except Exception as e:
logging.error("jsZkConnect, "+str(e))
t,v,tb = sys.exc_info()
strlist = traceback.format_tb(tb)
return str(e)+', traceback='+str(strlist)
return ''
#def onZkStateChange(self,state):
# frame = self.webView.page().mainFrame()
# frame.evaluateJavaScript("onPyZkStateChange('"+state+"')")
@pyqtSlot(str, result=QVariant)
def jsZkGetChildren(self, path):
try:
logging.info("jsZkGetChildren, path="+path)
children = self.zk.get_children(path)
except NoNodeError:
logging.error("jsZkGetChildren, NoNodeError")
return QVariant({"err":"node not exists"})
except Exception as e:
logging.error("jsZkGetChildren, "+str(e))
t,v,tb = sys.exc_info()
strlist = traceback.format_tb(tb)
return QVariant({"err":str(e)+', traceback='+str(strlist)})
return QVariant({"err":"", "children":children})
@pyqtSlot(str, result=QVariant)
def jsZkGet(self, path):
try:
logging.info("jsZkGet, path="+path)
ret = self.zk.get(path)
except NoNodeError:
logging.error("jsZkGet, NoNodeError")
return QVariant({"err":"node not exists"})
except Exception as e:
logging.error("jsZkGet, "+str(e))
t,v,tb = sys.exc_info()
strlist = traceback.format_tb(tb)
return QVariant({"err":str(e)+', traceback='+str(strlist)})
ctime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(ret[1].ctime/1000))
mtime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(ret[1].mtime/1000))
stat = {'ctime':ctime,'mtime':mtime,'version':ret[1].version}
data = ''
if ret[0] is not None:
data = ret[0].decode('utf8')
else:
logging.info('jsZkGet data None, path='+path)
return QVariant({"err":"", "data":data, "stat":QVariant(stat)})
@pyqtSlot(str, str, int, result=str)
def jsZkSet(self, path, data, ver):
try:
logging.info("jsZkSet, path="+path+',ver='+str(ver))
self.zk.set(path, bytes(data, 'utf8'),ver)
except NoNodeError as e:
logging.error("jsZkSet, NoNodeError")
return "node not exists"
except BadVersionError as e:
logging.error("jsZkSet, BadVersionError")
return "bad version"
except Exception as e:
logging.error("jsZkSet, "+str(e))
t,v,tb = sys.exc_info()
strlist = traceback.format_tb(tb)
return str(e)+', traceback='+str(strlist)
return ''
@pyqtSlot(str, result=QVariant)
def jsZkGetAcl(self, path):
try:
logging.info("jsZkGetAcl, path="+path)
ret = self.zk.get_acls(path)
except NoNodeError as e:
logging.error("jsZkGetAcl, NoNodeError")
return QVariant({"err":"node not exists"})
except Exception as e:
logging.error("jsZkGetAcl, "+str(e))
t,v,tb = sys.exc_info()
strlist = traceback.format_tb(tb)
return QVariant({"err":str(e)+', traceback='+str(strlist)})
lst = []
for acl in ret[0]:
dacl = {"perm":acl.perms,'scheme':acl.id.scheme,'id':acl.id.id}
lst.append(QVariant(dacl))
stat = {'ctime':ret[1].ctime,'mtime':ret[1].mtime,'version':ret[1].version}
return QVariant({"err":"", "acl_list":QVariant(lst), "stat":QVariant(stat)})
@pyqtSlot(str, str, result=str)
def jsZkSetAcl(self, path, list_str):
try:
acl_list = None
if list_str is not None and len(list_str)>0:
cache = json.loads(list_str)
acl_list = []
for one in cache:
if(one['scheme']=='world'):
acl = kazoo.security.ACL( one['perm'], kazoo.security.Id('world', 'anyone') )
acl_list.append(acl)
elif(one['scheme']=='digest'):
if 'id' in one:
acl = kazoo.security.ACL( one['perm'], kazoo.security.Id('digest', one['id']) )
else:
acl = kazoo.security.ACL( one['perm'], kazoo.security.Id('digest', self.makeDigestCred(one['user'],one['pass'])) )
acl_list.append(acl)
elif(one['scheme']=='ip'):
acl = kazoo.security.ACL( one['perm'], kazoo.security.Id('ip', one['ip']) )
acl_list.append(acl)
self.zk.set_acls(path, acl_list)
except NoNodeError as e:
logging.error("jsZkSetAcl, NoNodeError")
return "node not exists"
except InvalidACLError as e:
logging.error("jsZkSetAcl, InvalidACLError")
t,v,tb = sys.exc_info()
strlist = traceback.format_tb(tb)
return "invalid acl, traceback: "+str(strlist)
except BadVersionError as e:
logging.error("jsZkSetAcl, BadVersionError")
return "bad version error"
except Exception as e:
logging.error("jsZkSetAcl, "+str(e))
t,v,tb = sys.exc_info()
strlist = traceback.format_tb(tb)
return str(e)+', traceback='+str(strlist)
return ''
@pyqtSlot(str,str,int,int,result=str)
def jsZkCreate(self, path, data, ephem, seq):
try:
logging.info("jsZkCreate, path="+path)
self.zk.create(path=path, value=bytes(data,'utf8'), ephemeral=bool(ephem), sequence=bool(seq))
if self.default_acl is not None and len(self.default_acl)>0:
self.zk.set_acls(path, self.default_acl)
except NoNodeError as e:
logging.error("jsZkCreate, NoNodeError")
return "node not exists"
except NodeExistsError as e:
logging.error("jsZkCreate, NodeExistsError")
return "node already exists"
except NoChildrenForEphemeralsError as e:
logging.error("jsZkCreate, NoChildrenForEphemeralsError")
return "ephemeral node can not have child"
except Exception as e:
logging.error("jsZkCreate, "+str(e))
t,v,tb = sys.exc_info()
strlist = traceback.format_tb(tb)
return str(e)+', traceback='+str(strlist)
return ''
@pyqtSlot(str, int, int, result=str)
def jsZkDelete(self, path, ver, recursive):
try:
logging.info("jsZkDelete, path="+path+',ver='+str(ver)+', recursive='+str(recursive))
self.zk.delete(path, ver, bool(recursive))
except NoNodeError as e:
logging.error("jsZkDelete, NoNodeError")
return "node not exists"
except BadVersionError as e:
logging.error("jsZkDelete, BadVersionError")
return "bad version"
except NotEmptyError as e:
logging.error("jsZkDelete, NotEmptyError")
return "node not empty"
except Exception as e:
logging.error("jsZkDelete, "+str(e))
t,v,tb = sys.exc_info()
strlist = traceback.format_tb(tb)
return str(e)+', traceback='+str(strlist)
return ''
@pyqtSlot(str, str, int, int, result=str)
def jsZkCopy(self, dest_path, ori_path, max_depth, children_only):
logging.info("jsZkCopy, dest_path="+dest_path+", ori_path="+ori_path+", children_only="+str(children_only))
#copy node first
if children_only==0:
try:
ori_data = self.zk.get(ori_path)
if self.zk.exists(dest_path) is None:
self.zk.create(dest_path, ori_data[0], acl=self.default_acl)
else:
self.zk.set(dest_path, ori_data[0])
except NoNodeError as e:
logging.error("jsZkCopy, node, NoNodeError, ori_path="+ori_path+', dest_path='+dest_path)
return "node not exists"
except Exception as e:
logging.error("jsZkCopy, "+str(e))
t,v,tb = sys.exc_info()
strlist = traceback.format_tb(tb)
return str(e)+', traceback='+str(strlist)
#copy children
path = ''
try:
max_depth -= 1
path = ori_path
ori_children = self.zk.get_children(ori_path)
for child in ori_children:
path = ori_path+'/'+child
ret = self.jsZkCopy(dest_path+'/'+child, ori_path+'/'+child, max_depth, 0)
if isinstance(ret, QVariant):
return ret
elif len(ret)>0:
return ret
except NoNodeError as e:
logging.error("jsZkCopy, child, NoNodeError")
return "node not exists, path="+path
except Exception as e:
logging.error("jsZkCopy, "+str(e))
t,v,tb = sys.exc_info()
strlist = traceback.format_tb(tb)
return str(e)+', traceback='+str(strlist)
return ''
'''
@pyqtSlot(str, str, int, result=str)
def jsZkCopyChildren(self, dest_path, ori_path, max_depth):
path = ''
try:
max_depth -= 1;
logging.info("jsZkCopyChildren, dest_path="+dest_path+", ori_path="+ori_path)
path = ori_path
ori_children = self.zk.get_children(ori_path)
path = dest_path
dest_children = self.zk.get_children(dest_path)
for child in ori_children:
if child in dest_children:
return 'child ['+child+'] is found in both path'
for child in ori_children:
path = ori_path+'/'+child
data = self.zk.get(path)[0]
path = dest_path+'/'+child
self.zk.create(path, data, acl=self.default_acl)
if max_depth>0:
ret = self.jsZkCopyChildren(dest_path+'/'+child, ori_path+'/'+child, max_depth)
if len(ret)>0:
return ret
except NoNodeError as e:
logging.info("jsZkCopyChildren, NoNodeError")
return "node not exists, path="+path
except ZookeeperError as e:
logging.info("jsZkCopyChildren, ZookeeperError")
return str(e)+', path='+path
return ''
'''
@pyqtSlot(str, int, result=str)
def jsZkDeleteChildren(self, main_path, max_depth):
path = ''
try:
max_depth -= 1
path = main_path
logging.info("jsZkDeleteChildren, path="+main_path)
children = self.zk.get_children(path)
for child in children:
path = main_path+'/'+child
if max_depth>0:
ret = self.jsZkDeleteChildren(path, max_depth)
if len(ret)>0:
return ret
self.zk.delete(path)
except NoNodeError as e:
logging.error("jsZkDeleteChildren, NoNodeError")
return "node not exists, path="+path
except Exception as e:
logging.error("jsZkDeleteChildren, "+str(e))
t,v,tb = sys.exc_info()
strlist = traceback.format_tb(tb)
return str(e)+', traceback='+str(strlist)
return ''
@pyqtSlot(str, str, int, int, result=str)
def jsZkExport(self, local_dir, main_path, max_depth, without_acl):
path = ''
try:
max_depth -= 1
path = main_path
logging.info("jsZkExport, path="+main_path+' to local dir '+local_dir)
data = self.zk.get(main_path)
p = pathlib.Path(local_dir)
if not p.exists():
p.mkdir(parents=True)
elif not p.is_dir():
return 'local '+local_dir+' exists, but not a directory'
for child in p.iterdir():
return 'local path '+local_dir+' is not empty, clear it first'
p = pathlib.Path(local_dir+'/____data')
p.touch()
obj = open(str(p),'wb')
try:
if data[0] is not None:
obj.write(data[0])
finally:
obj.close()
if not without_acl:
ret = self.zk.get_acls(path)
lst = []
if ret is not None:
for acl in ret[0]:
lst.append( {"perm":acl.perms,'scheme':acl.id.scheme,'id':acl.id.id} )
p = pathlib.Path(local_dir+'/____acl')
p.touch()
obj = open(str(p),'w')
try:
obj.write(json.dumps(lst))
finally:
obj.close()
children = self.zk.get_children(path)
if children is not None:
for child in children:
if child=='zookeeper':
continue
path = main_path+'/'+child
if max_depth>0:
ret = self.jsZkExport(local_dir+'/'+child, path, max_depth, without_acl)
if len(ret)>0:
return ret
except NoNodeError as e:
logging.error("jsZkExport, NoNodeError")
return "node not exists, path="+path
except Exception as e:
logging.error("jsZkExport, "+str(e))
t,v,tb = sys.exc_info()
strlist = traceback.format_tb(tb)
return str(e)+', traceback='+str(strlist)
return ''
@pyqtSlot(str, str, int, int, result=str)
def jsZkImport(self, local_dir, main_path, max_depth, without_acl):
path = ''
try:
max_depth -= 1
path = main_path
logging.info("jsZkImport, path="+main_path+' from local dir '+local_dir)
obj = open(local_dir+'/____data', 'rb')
if self.zk.exists(path) is None:
self.zk.create(path, obj.read(), acl=self.default_acl)
else:
self.zk.set(path, obj.read())
if not without_acl:
obj = open(local_dir+'/____acl', 'r')
acl_list = None
list_str = obj.read()
if list_str is not None and len(list_str)>0:
cache = json.loads(list_str)
acl_list = []
for one in cache:
acl = kazoo.security.ACL( one['perm'], kazoo.security.Id(one['scheme'], one['id']) )
acl_list.append(acl)
self.zk.set_acls(path, acl_list)
p = pathlib.Path(local_dir)
for child in p.iterdir():
if not child.is_dir():
continue
if child.name=='zookeeper':
continue
ret = self.jsZkImport(str(child), path+'/'+child.name, max_depth, without_acl)
if len(ret)>0:
return ret
except NoNodeError as e:
logging.error("jsZkImport, NoNodeError")
return "node not exists, path="+path
except Exception as e:
logging.error("jsZkImport, "+str(e))
t,v,tb = sys.exc_info()
strlist = traceback.format_tb(tb)
return str(e)+', traceback='+str(strlist)
return ''