/
ringodisco.py
85 lines (75 loc) · 3.12 KB
/
ringodisco.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
import ringogw
def ringo_reader(fd, sze, fname):
import struct, zlib
MAGIC_HEAD = (0x47da66b5,)
MAGIC_TAIL = (0xacc50f5d,)
def read_really(s):
t = 0
buf = ""
while t < s:
r = fd.read(s - t)
if not r:
return buf
t += len(r)
buf += r
return buf
def check_body(head_body):
time, entryid, flags, keycrc, keylen, valcrc, vallen =\
struct.unpack("<IIIIIII", head_body)
tot = keylen + vallen + 4
body = read_really(tot)
if len(body) < tot:
return False, head_body + body
key = body[:keylen]
val = body[keylen:-4]
if zlib.crc32(key) != keycrc or zlib.crc32(val) != valcrc or\
struct.unpack("<I", body[-4:]) != MAGIC_TAIL:
return False, head_body + body
else:
return True, (entryid, flags, key, val)
def read_entry():
head = read_really(8)
while len(head) >= 8:
if struct.unpack("<I", head[:4]) == MAGIC_HEAD:
if len(head) < 36:
head += read_really(36 - len(head))
if len(head) < 36:
return None
head_crc = struct.unpack("<I", head[4:8])[0]
head_body = head[8:36]
if zlib.crc32(head_body) == head_crc:
ok, cont = check_body(head_body)
if ok:
return cont
head = cont
head = head[1:]
if len(head) < 8:
head += fd.read(1)
else:
return None
prev_id = None
while True:
entry = read_entry()
if not entry:
break
entryid, flags, key, val = entry
if flags & 1 or flags & 2:
continue
if entryid == prev_id:
continue
prev_id = entryid
yield key, val
def input_domain(ringo_host, name):
ringo = ringogw.Ringo(ringo_host)
code, res = ringo.request("/mon/domains/domain?name=" + name)
if code != 200:
return []
urls = []
for domainid, name, nodeid, chunk, owner, nrepl in res:
nodename, node = nodeid.split('@')
urls.append("disco://%s/_ringo/%s/rdomain-%s/data"\
% (node, nodename[6:], domainid))
return urls
if __name__ == "__main__":
import sys
print "\n".join(input_domain(sys.argv[1], sys.argv[2]))