-
Notifications
You must be signed in to change notification settings - Fork 1
/
softsan-cli
executable file
·312 lines (288 loc) · 8.72 KB
/
softsan-cli
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
#!/usr/bin/python
import Client, config, util
import sys, os, cmd
from mds import Object
CHUNKSIZE = 64
#branches of volume tree
#vertical, up and right, vertical and right, horizontal, horizontal and down
branch = [
'\342\224\202',\
'\342\224\224',\
'\342\224\234',\
'\342\224\200',\
'\342\224\254' \
]
cmdstruct=(\
('name', 'n', '', 'create a volume'),\
('params', 'p', '', 'volume parameters'),\
('size', 's', '0', 'volume size'),\
('count', 'c', '0', 'subvolumes or chunk count'),\
('type', 't', 'linear', 'volume type'),\
('tree', '', '0', 'display a volume in from of tree'),\
('all', 'a', '0', 'all of the volumes or not'),\
('help', 'h', False, 'this help'),\
)
argstruct=(\
('mdsaddress', 'a', '0.0.0.0', 'metadata server address'),\
('mdsport', 'p', 0x6789, 'metadata server port'),\
('logging-level', 'l', 'info', 'logging level, can be "debug", "info", "warning", "error" or "critical"'), \
('config', 'c', 'softsan.conf', 'config file'),\
('logging-file', 'f', 'stdout', 'logging appends to this file'),\
('help', 'h', False, 'this help'),\
)
def ListVolume(mds, volume_name, tree):
if volume_name == '':
volumes = mds.ListDirectory('/')
for volume in volumes:
if not ListVolume(mds, volume.name, tree):
return False
return True
try:
volume = mds.ReadVolumeInfo(volume_name)
except IOError as ex:
if str(ex).find('No such file or directory') > -1:
print >>sys.stderr,'Volume '+volume_name+' does not exist'
return False
else:
raise ex
if volume.assembler != 'chunk':
print 'Name: ',volume.parameters[0]
print 'Path: ',volume.parameters[1]
print 'Type: ',volume.assembler
print 'Size: ',volume.size, 'MB'
print 'Status: ',volume.parameters[3]
print 'Number of subvolumes: ',len(volume.subvolumes),'\n'
if tree == '1':
print_tree(volume, '')
print '\n'
return True
def print_tree(volume, prefix):
print os.path.basename(volume.parameters[1])
if hasattr(volume, 'subvolumes'):
num = len(volume.subvolumes)
else:
num = 0
i = 0
for i in range(0, num):
if i == num-1:
print prefix+' '+branch[1],
add = ' '
else:
print prefix+' '+branch[2],
add = ' '+branch[0]
print_tree(volume.subvolumes[i], prefix+add)
def ParseTable(args, subvolumes):
ret = Object()
ret.name = ''
ret.size = 0
ret.type = ''
ret.chunksizes = []
ret.subvolumes = []
ret.parameters = []
tableFormat = 'name size [type] [num] [sizes|volumenames]'
if args.name == '':
print >>sys.stderr,'volume name must be specified'
return None
ret.name = args.name
if args.size.isdigit() == False:
print >>sys.stderr,'volume size must be digit'
return None
if args.size != '0':
ret.size = int(args.size)
if ret.size < 0:
print >>sys.stderr,'Volume size must not be negative'
return None
if args.type != 'linear' and args.type != 'striped' and\
args.type != 'mirror' and args.type != 'gfs':
print >>sys.stderr,'volume type can only be linear/striped/mirror/gfs'
return None
ret.type = args.type
if args.params != '':
if args.type != 'striped':
print >>sys.stdwar,'does not need parameters'
else:
strsize = int(args.params)
if strsize != 256 and strsize != 512 and strsize != 1024 and strsize != 2048:
print >>sys.stderr,'stripe size only can be: 256/512/1024/2048'
return None
ret.parameters.append(args.params)
if args.count.isdigit() == False:
print >>sys.stderr,'count must be digit'
return None
count = int(args.count)
if count < 0:
print >>sys.stderr,'count must not be negative'
return None
if count == 0:
if ret.size == 0:
print >>sys.stderr,'Volume size must be specified'
return None
if ret.type == 'linear' or ret.type == 'gfs':
chksize = CHUNKSIZE
totsize = ret.size
while totsize > chksize:
ret.chunksizes.append(chksize)
totsize -= chksize
if totsize > 0:
ret.chunksizes.append(totsize)
else:
count = 2
if count > 0:
num = len(subvolumes)
if num == 0:
if ret.size == 0:
print >>sys.stderr,'Volume size must be specified'
return None
chksize = ret.size/count
if chksize < 4:
print >>sys.stderr,'too many chunks, single chunk size too samll'
return None
totsize = ret.size
while totsize > chksize:
ret.chunksizes.append(chksize)
totsize -= chksize
if totsize > 0:
ret.chunksizes.append(totsize)
return ret
if num != count:
print >>sys.stderr,'incorrect number of arguments for count'
return None
if subvolumes[0].isdigit() == True:
totsize = 0
for i in range(0, count):
if subvolumes[i].isdigit() == False:
print >>sys.stderr,'chunk size must be digit'
return None
ret.chunksizes.append(int(subvolumes[i]))
totsize += int(int(subvolumes[i]))
if ret.size == 0:
ret.size = totsize
if totsize > ret.size:
print >>sys.stderr,'chunk size summation is larger than voluem size'
return None
if totsize < ret.size:
print >>sys.stdwar,'warning: chunk size summation is less than voluem size'
remain = ret.size-totsize
while remain > CHUNKSIZE:
ret.chunksizes.append(CHUNKSIZE)
remain -= CHUNKSIZE
if remain > 0:
ret.chunksizes.append(remain)
else:
ret.subvolumes = subvolumes
return ret
def ParseArg():
conf,_ = config.config(argstruct, 'client')
util.setupLogging(conf)
client = Client.Client(conf['mdsaddress'], int(conf['mdsport']))
cli = CLI(client)
cli.cmdloop()
client.Clear()
class CLI(cmd.Cmd):
def __init__(self, client):
cmd.Cmd.__init__(self)
self.prompt = 'softsan# '
self.client = client
def get_params(self, cmdline):
cmdline = 'softsan '+cmdline
sys.argv = cmdline.split()
params, remains = config.config(cmdstruct, None)
return Object(params), remains
def check_remains(self, remains):
if len(remains) > 0:
print >>sys.stderr,'Unrecongnized arguments: ',
for arg in remains:
print arg,
print ''
return False
return True
def do_create(self, cmdline):
params, remains = self.get_params(cmdline)
ret = ParseTable(params, remains)
if ret == None:
return
if self.client.CreateVolume(ret.name, ret.size, ret.type, ret.chunksizes,\
ret.subvolumes, ret.parameters):
print 'Volume '+ret.name+' successfully created'
else:
print 'Create volume '+ret.name+' failed'
def help_create(self):
print 'create a new volume'
def do_remove(self, cmdline):
params,remains = self.get_params(cmdline)
if self.check_remains(remains):
if self.client.DeleteVolume(params.name):
print 'Volume '+params.name+' successfully removed'
else:
print 'Remove volume '+params.name+' failed'
def help_remove(self):
print 'remove volume'
def do_list(self, cmdline):
params,remains = self.get_params(cmdline)
if self.check_remains(remains):
ListVolume(self.client.mds, params.name, params.tree)
def help_list(self):
print 'list volume'
def do_ls(self, cmdline):
self.do_list(cmdline)
def help_ls(self):
print 'list volume'
def do_split(self, cmdline):
params,remains = self.get_params(cmdline)
if self.check_remains(remains):
if self.client.SplitVolume(params.name):
print 'Volume '+params.name+' successfully splited'
else:
print 'Split volume '+params.name+' failed'
def help_split(self):
print 'split a volume into subvolume(s)'
def do_mount(self, cmdline):
params,remains = self.get_params(cmdline)
if self.check_remains(remains):
if self.client.MountVolume(params.name):
print 'Volume '+params.name+' successfully mounted'
else:
print 'Mount volume '+params.name+' failed'
def help_mount(self):
print 'mount volume'
def do_unmount(self, cmdline):
params,remains = self.get_params(cmdline)
if self.check_remains(remains):
if self.client.UnmountVolume(params.name):
print 'Volume '+params.name+' successfully unmounted'
else:
print 'Unmount volume '+params.name+' failed'
def help_unmount(self):
print 'unmount volume'
def do_checkvolume(self, cmdline):
params,remains = self.get_params(cmdline)
if self.check_remains(remains):
self.client.CheckVolume()
def help_checkvolume(self):
print 'Check volume status'
def do_exit(self, cmdline):
exit(0)
def help_exit(self):
print 'exit program'
def print_data(args):
if isinstance(args, Object):
print args.name
print args.size
print args.type
print args.chunksizes, len(args.chunksizes)
print args.subvolumes, len(args.subvolumes)
print args.parameters, len(args.parameters)
else:
print args
def test():
sys.argv = 'softsan-cli.py -a 192.168.0.12'.split()
# sys.argv = 'create no1 60 striped 2'.split()
# sys.argv = 'softsan-cli.py --info no1'.split()
# sys.argv = 'softsan-cli.py --unmount no1'.split()
# sys.argv = 'softsan-cli.py --mount no1'.split()
# sys.argv = 'softsan-cli.py --remove no1'.split()
args = ParseArg()
print_data(args)
if __name__=='__main__':
#test()
ParseArg()