forked from looprock/zkNavigator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfe.py
executable file
·127 lines (113 loc) · 3.29 KB
/
fe.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
#!/usr/bin/env python
import sys
import os
import json
import re
from ConfigParser import SafeConfigParser
from bottle import route, run, get, abort, post, request, template, redirect, default_app, debug, TEMPLATE_PATH, response
from kazoo.client import KazooClient
from kazoo.retry import KazooRetry
from time import time, localtime, strftime
if os.path.exists('./zk.conf'):
parser = SafeConfigParser()
parser.read('./zk.conf')
elif os.path.exists('/etc/zktools/zk.conf'):
parser = SafeConfigParser()
parser.read('/etc/zktools/zk.conf')
else:
print "No config file found! Please create: ./zk.conf or /etc/zktools/zk.conf"
sys.exit(1)
env = parser.get('default', 'env').strip()
zkserver = parser.get(env, 'server').strip()
zkport = parser.get(env, 'port').strip()
defaultroot = parser.get(env, 'root').strip().replace("/","|")
enabledelete = parser.get('fe', 'enabledelete').strip()
# these don't do anything yet
enableedit = parser.get('fe', 'enabledelete').strip()
enablecreate = parser.get('fe', 'enabledelete').strip()
enablerename = parser.get('fe', 'enabledelete').strip()
enabledenvs = parser.get('fe', 'enabledelete').strip()
server = "%s:%s" % (zkserver,zkport)
kr = KazooRetry(max_tries=3)
zk = KazooClient(hosts=server)
zk.start()
@route('/')
@route('/<root>')
def list(root=defaultroot):
d = {}
rr = root.replace("|","/")
if rr:
if rr != "favicon.ico":
x = kr(zk.get_children, rr)
for i in x:
try:
p = "%s/%s" % (rr,i)
y = kr(zk.get,p)[0]
d[i] = y
except:
print "Couldn't get child from: %s" % p
b = [ rr, d, enabledelete ]
return template('list', res=b)
@route('/json/')
@route('/json/<root>')
def list(root=defaultroot):
response.content_type = 'application/json'
rr = root.replace("|","/")
if rr:
if rr != "favicon.ico":
x = kr(zk.get_children, rr)
return json.dumps(x)
@route('/content/')
@route('/content/<root>')
def list(root=defaultroot):
rr = root.replace("|","/")
if rr:
if rr != "favicon.ico":
x = kr(zk.get, rr)[0]
return x
@route('/edit/<path>')
def edit(path):
p = path.replace("|","/")
x = kr(zk.get,p)[0]
y = [ p, x]
return template('edit', res=y)
@post('/editsub')
def editsub():
node = request.forms.get('node').replace("|","/")
uf = node.replace("/","|")
rp = node.replace("|","/")
content = request.forms.get('content')
try:
kr(zk.set,rp,content)
x = [ uf, rp ]
return template('editsub', res=x)
except:
return "ERROR: unable to update %s" % node
@route('/create/<path>')
def create(path):
return template('create', res=path)
@post('/createsub')
def createsub():
path = request.forms.get('path').replace("|","/")
node = request.forms.get('node')
content = request.forms.get('content')
fpath = "%s/%s" % (path,node)
try:
if content:
kr(zk.create,fpath,value=content,makepath=True)
else:
kr(zk.create,fpath,makepath=True)
return template('createsub', res=fpath)
except:
return "ERROR: unable to create %s!" % fpath
@route('/delete/<path>')
def delete(path):
p = path.replace("|","/")
try:
p = path.replace("|","/")
kr(zk.delete,p,recursive=True)
return template('delete', res=p)
except:
return "ERROR: unable to delete %s!" % p
#run(host='0.0.0.0', port=8080, debug=True)
run(port=8080, debug=True)