forked from naver/arcus-python-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharcus_zk_cmd.py
More file actions
181 lines (128 loc) · 5.36 KB
/
arcus_zk_cmd.py
File metadata and controls
181 lines (128 loc) · 5.36 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
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
#!/usr/local/bin/python3
#
# arcus-python-client - Arcus python client drvier
# Copyright 2014 NAVER Corp.
#
# Licensed under the Apache License, Version 2.0 (the "License")
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import sys
import re
import socket
from optparse import OptionParser
from arcus_util import zookeeper
from arcus_util import arcus_node
from kazoo.client import KazooClient
import kazoo
if __name__ == '__main__':
usage = "usage: %prog [options]"
parser = OptionParser(usage=usage, version="%prog 1.0")
parser.add_option('-a', '--address', dest='address', default='', help='zookeeper address')
parser.add_option('-s', '--service', dest='service', default='', help='service code')
parser.add_option('-n', '--node', dest='node', default='', help='node address:port')
parser.add_option('', '--name', dest='name', default='', help='node domain name')
parser.add_option('-c', '--command', dest='command', default='', help='add_service | del_service | add_node | del_node')
parser.add_option('-f', '--force', dest='force', default=False, help='ignore exception', action='store_true')
(options, args) = parser.parse_args()
address = options.address
service = options.service
node = options.node
command = options.command
force = options.force
ret = node.split(':')
name = ret[0]
if len(ret) > 1:
port = ret[1]
else:
port = ''
ip = socket.gethostbyname(name)
if options.name != '':
name = options.name
zoo = zookeeper(address)
print(zoo)
if force:
zoo.set_force()
if zoo.zk_exists('/arcus') == False:
print ('create /arcus')
zoo.zk_create('/arcus', 'arcus')
if zoo.zk_exists('/arcus/cache_list') == False:
print ('create /arcus/cache_list')
zoo.zk_create('/arcus/cache_list', 'cache_list')
if zoo.zk_exists('/arcus/client_list') == False:
print ('create /arcus/client_list')
zoo.zk_create('/arcus/client_list', 'client_list')
if zoo.zk_exists('/arcus/cache_server_mapping') == False:
print ('create /arcus/cache_server_mapping')
zoo.zk_create('/arcus/cache_server_mapping', 'cache_server_mapping')
if zoo.zk_exists('/arcus/service_code_mapping') == False:
print ('create /arcus/service_code_mapping')
zoo.zk_create('/arcus/service_code_mapping', 'service_code_mapping')
if command:
if command == 'add_service' and service:
print ('add /arcus/cache_list/' + service)
zoo.zk_create('/arcus/cache_list/' + service, 'arcus1.8')
print ('add /arcus/client_list/' + service)
zoo.zk_create('/arcus/client_list/' + service, 'arcus1.8')
print ('add /arcus/service_code_mapping/' + service)
zoo.zk_create('/arcus/service_code_mapping/' + service, 'arcus1.8')
elif command == 'del_service' and service:
data, stat, children = zoo.zk_read('/arcus/service_code_mapping/' + service)
print ('delete /arcus/service_code_mapping/' + service)
zoo.zk_delete_tree('/arcus/service_code_mapping/' + service)
for child in children:
ret = '/arcus/cache_server_mapping/' + child
print ('delete node %s' % ret)
zoo.zk_delete_tree(ret)
print ('delete /arcus/client_list/' + service)
zoo.zk_delete_tree('/arcus/client_list/' + service)
elif command == 'add_node' and service and node:
assert port != ''
print ('create /arcus/service_code_mapping/%s/%s:%s' % (service, ip, port))
zoo.zk_create('/arcus/service_code_mapping/%s/%s:%s' % (service, ip, port), name)
print ('create /arcus/cache_server_mapping/%s:%s' % (ip, port))
zoo.zk_create('/arcus/cache_server_mapping/%s:%s' % (ip, port), name)
print('create /arcus/cache_server_mapping/%s:%s/%s' % (ip, port, service))
zoo.zk_create('/arcus/cache_server_mapping/%s:%s/%s' % (ip, port, service), 'arcus1.8')
elif command == 'del_node' and service and node:
if port != '': # delete one
print ('delete /arcus/service_code_mapping/%s/%s:%s' % (service, ip, port))
zoo.zk_delete_tree('/arcus/service_code_mapping/%s/%s:%s' % (service, ip, port))
print ('delete /arcus/cache_server_mapping/%s:%s' % (ip, port))
zoo.zk_delete_tree('/arcus/cache_server_mapping/%s:%s' % (ip, port))
else: # delete all
head = '/arcus/service_code_mapping/' + service
data, stat, children = zoo.zk_read(head)
for child in children:
idx = len(ip)
if child[0:idx] == ip:
ret = head + '/' + child
print ('delete %s' % ret)
zoo.zk_delete_tree(ret)
head = '/arcus/cache_server_mapping'
data, stat, children = zoo.zk_read(head)
for child in children:
idx = len(ip)
if child[0:idx] == ip:
ret = head + '/' + child
print ('delete %s' % ret)
zoo.zk_delete_tree(ret)
elif service:
data, stat, children = zoo.zk_read('/arcus/cache_list/' + service)
print ('## cache_list/' + service)
print (children)
data, stat, children = zoo.zk_read('/arcus/service_code_mapping/' + service)
print ('## cache_server_mapping/' + service)
print (children)
else:
data, stat, children = zoo.zk_read('/arcus/cache_list')
print ('## cache_list')
print (children)