-
Notifications
You must be signed in to change notification settings - Fork 0
/
cephfs-cli.py
381 lines (349 loc) · 13 KB
/
cephfs-cli.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
#!/bin/env python
# cephfs-cli supports: config login upload download delete
# based on cephfstool
# 20180824
# 20181203 install easily, add env
from __future__ import print_function
import sys
import os
from errno import *
import argparse
import json
__all__ = ["login","get_user_info","config_handler","upload_handler",
"download_handler","remove_handler","mkdir_handler","pwd_handler",
"chdir_handler","listdir_handler"]
def add_sys_path(path):
if os.path.exists(path):
if path in sys.path:
sys.path.remove(path)
sys.path.insert(1, path)
cur_dir = os.path.dirname(os.path.abspath(__file__))
home_dir = os.getenv('CEPH_CLI_HOME', cur_dir)
version = os.getenv('CEPH_CLI_VERSION', '0.0.1')
add_sys_path(os.path.join(home_dir, 'py_packages'))
default_log_dir = os.path.join(home_dir, 'logs/')
default_info_file = os.path.join(home_dir, 'conf', 'user.info')
last_work_dir = os.path.join(home_dir, ".cephcli.last.lwd")
if sys.version_info[0] == 2:
import codecs
open = codecs.open
try:
import cephfstool as tool
except ImportError:
print("cephcli may be not installed, please reinstall\n" + \
"Or check which python version you use\n" + \
"Or contact with administrator", file=sys.stderr)
sys.exit(EAGAIN)
# -i user_info_file
user_info_file = default_info_file
cephfs_root_dir = None
cephfs_helper = None
verbose = False
def login(cephconf, cephaddr, name=None, key=None, root=None):
configure = locals()
if verbose:
print("login arguments: ", configure)
if not cephconf and not cephaddr:
print("cephconf file or cephaddr is required\nconfig -h for help",\
file=sys.stderr)
return EINVAL
global cephfs_helper
cephfs_helper = tool.CephfsHelper()
if cephconf:
cephfs_helper.set_config_file(cephconf)
if cephaddr:
cephfs_helper.set_mon_addr(cephaddr)
ret = cephfs_helper.login(name, key, root)
if not ret:
print("user login cephfs failed", file=sys.stderr)
return EPERM
try:
with open(user_info_file, 'w', 'utf-8') as f:
json.dump(configure, f, encoding='utf-8')
if verbose:
print("dump user info {0} to {1}".format(configure, user_info_file))
except Exception as e:
print('Warning: unable write configure to file {0} : {1}'\
.format(user_info_file, e), file=sys.stderr)
if os.path.exists(last_work_dir):
with open(last_work_dir, 'r') as f:
lwd = f.read()
ret = cephfs_helper.chdir(lwd)
if not ret:
print("Warning: unable to change to last work dir " + lwd)
if verbose:
print("{0}:{1} login cephfs successfully".format(name if name else 'admin',
root if root else '/'))
return 0
def get_user_info():
try:
with open(user_info_file, 'rb', 'utf-8') as f:
#load will give you unicode str, so convert them to ascii
info = json.load(f, encoding='utf-8', object_hook=ascii_encode_dict)
if verbose:
print("read user info {0} from {1}".format(info, user_info_file))
return info, True
except Exception as e:
if verbose:
print("read user info file error: ", e)
return None, False
def check(func):
def wrapper(*args, **kwargs):
info, ok = get_user_info()
if not ok:
print("Not config correctly, please run install.sh firstly",\
file=sys.stderr)
return EPERM
if cephfs_root_dir is not None:
info["root"] = cephfs_root_dir
ret = login(**info)
if ret != 0:
return ret
return func(*args, **kwargs)
return wrapper
def config_handler(args):
#check user_info_file
info, ok = get_user_info()
if ok and not args.cephconf and not args.cephaddr:
print("current user info:")
print("User: " + info["name"])
print("Root path: " + info["root"])
print("use config -h for help to change config")
return 0
if not args.cephconf and not args.cephaddr:
print("-c or -a is required for ceph addr\nconfig -h for help", file=sys.stderr)
return EINVAL
configure = {"cephconf":None,"cephaddr":None}
if args.cephconf:
configure["cephconf"] = args.cephconf.name
args.cephconf.close()
if args.cephaddr:
configure["cephaddr"] = args.cephaddr
if args.user:
configure["name"] = args.user
if args.keyfile:
configure["key"] = args.keyfile.read()
args.keyfile.close()
if cephfs_root_dir:
configure["root"] = cephfs_root_dir
elif args.user:
configure["root"] = "/" + args.user
if verbose:
print("config: ", configure)
ret = login(**configure)
if ret != 0:
return ret
print("config cephfs successfully\nYou can run upload or download command etc.")
return 0
def ascii_encode_dict(data):
ascii_encode = lambda x: x.encode('ascii') if isinstance(x, unicode) else x
return dict(map(ascii_encode, pair) for pair in data.items())
@check
def upload_handler(args):
src_path, cephfs_path = args.src_path, args.cephfs_path
if verbose:
print('upload arguments: ', src_path, cephfs_path)
for src in src_path:
dst_path = cephfs_path
if not os.path.exists(src):
print("upload local path [{0}] No such file or directory".format(src),\
file=sys.stderr)
continue
elif os.path.isfile(src):
if dst_path == '.' or dst_path == '..':
dst_path += '/'
if dst_path[-1] == '/':
dst_path = dst_path + os.path.basename(src)
elif os.path.isdir(src):
ret = cephfs_helper.stat(dst_path)
if ret == 0: # file
print("upload local dir [{0}] to cephfs exist file [{1}] is not allowed"\
.format(src, dst_path), file=sys.stderr)
return EPERM
else:
if src[-1] == '/':
src = src[:-1]
dirname = os.path.basename(src)
dst_path = os.path.join(dst_path, dirname)
if dst_path[-1] != '/':
dst_path += '/'
ret = cephfs_helper.write_tree(dst_path, src)
if not ret:
print("upload [{0}] failed".format(src), file=sys.stderr)
return EPERM
else:
print("upload local path [{0}] to cephfs path [{1}] successfully".
format(src, dst_path))
return 0
@check
def download_handler(args):
dst_path, cephfs_path = args.dst_path, args.cephfs_path
if verbose:
print('download arguments: ', cephfs_path, dst_path)
ppath = os.path.dirname(dst_path)
if len(ppath)>0 and not os.path.exists(ppath):
os.makedirs(ppath)
ret = cephfs_helper.stat(cephfs_path)
if ret == -1:
print("download path [{0}] No such file or directory".format(cephfs_path),\
file=sys.stderr)
return ENOENT
elif ret == 0: # file
if os.path.isdir(dst_path):
dst_path = os.path.join(dst_path, os.path.basename(cephfs_path))
elif ret == 1:
print("download directory [{0}] is not supported".format(cephfs_path),\
file=sys.stderr)
return EPERM
ret = cephfs_helper.read(cephfs_path, dst_path)
if not ret:
print("download [{0}] failed".format(cephfs_path), file=sys.stderr)
return EPERM
else:
print("download to local path [{0}] from cephfs path [{1}] successfully".
format(dst_path, cephfs_path))
return 0
@check
def remove_handler(args):
cephfs_path = args.cephfs_path
if verbose:
print('remove arguments: ', cephfs_path)
for src in cephfs_path:
st = cephfs_helper.stat(src)
if st == 0:
ret = cephfs_helper.remove(src)
elif st == 1:
ret = cephfs_helper.rmdir(src)
else:
print("remove path [{0}] No such file or directory".format(src),\
file=sys.stderr)
continue
if not ret:
print("remove path [{0}] failed".format(src), file=sys.stderr)
return EPERM
else:
print("remove cephfs path [{0}] successfully".format(src))
return 0
@check
def pwd_handler(args):
print(cephfs_helper.getcwd())
return 0
@check
def mkdir_handler(args):
cephfs_path = args.cephfs_path
if verbose:
print('mkdir arguments: ', cephfs_path)
for src in cephfs_path:
if src[-1] != '/':
src += '/'
ret = cephfs_helper.get_safe_path(src)
if not ret:
print("mkdir path [{0}] failed".format(src), file=sys.stderr)
return EPERM
print("mkdir path [{0}] successfully".format(src))
return 0
@check
def chdir_handler(args):
cephfs_path = args.cephfs_path
if verbose:
print('chdir arguments: ', cephfs_path)
if cephfs_path[-1] != '/':
cephfs_path += '/'
ret = cephfs_helper.chdir(cephfs_path)
if not ret:
print("chdir path [{0}] failed".format(cephfs_path), file=sys.stderr)
return EPERM
with open(last_work_dir, 'w') as f:
f.write(cephfs_helper.getcwd())
print("chdir path [{0}] successfully".format(cephfs_path))
return 0
@check
def listdir_handler(args):
cephfs_path = args.cephfs_path
if verbose:
print('listdir arguments: ', cephfs_path)
if cephfs_path is None:
cephfs_path = "./"
ls = tool.StringVector()
ret = cephfs_helper.listdir_buffer(cephfs_path, ls)
if not ret:
print("listdir path [{0}] failed".format(cephfs_path), file=sys.stderr)
return EPERM
if len(ls):
for l in ls:
print(l,end=' ')
print()
else:
print("empty directory")
return 0
def parse_cmdargs(args=None):
parser = argparse.ArgumentParser(description='cephfs client tool')
parser.add_argument('-v', '--version', action="store_true", help="display version")
parser.add_argument('--verbose', '-vv', action="store_true", help="show verbose")
parser.add_argument('-i', '--userfile', help='user info file',
default=default_info_file)
parser.add_argument('-r', '--root', help='root path in cephfs')
sub = parser.add_subparsers(title='support subcommands')
sub.required = False
config = sub.add_parser('config', help='config cephfs and authentication')
config.add_argument('-c', '--conf', dest='cephconf',
type=argparse.FileType('r'),
help='ceph configuration file')
config.add_argument('-a', '--cephaddr', help='ceph mon addr')
config.add_argument('-n', '--name', dest='user',
help='client name for authentication')
config.add_argument('-k', '--keyfile', type=argparse.FileType('r'),
help='keyfile for authentication')
config.set_defaults(func=config_handler)
upload = sub.add_parser('upload', help='upload files to cephfs')
upload.add_argument('src_path', help='local source path', nargs='+')
upload.add_argument('cephfs_path', help='dst path in cephfs')
upload.set_defaults(func=upload_handler)
download = sub.add_parser('download', help='download files from cephfs')
download.add_argument('cephfs_path', help='source path in cephfs')
download.add_argument('dst_path', help='local dst path')
download.set_defaults(func=download_handler)
remove = sub.add_parser('remove', help='remove files from cephfs')
remove.add_argument('cephfs_path', help='path in cephfs', nargs='+')
remove.set_defaults(func=remove_handler)
pwd = sub.add_parser('pwd', help='print working directory')
pwd.set_defaults(func=pwd_handler)
mkdir = sub.add_parser('mkdir', help='make directory')
mkdir.add_argument('cephfs_path', help='path in cephfs', nargs='+')
mkdir.set_defaults(func=mkdir_handler)
chdir = sub.add_parser('cd', help='change directory')
chdir.add_argument('cephfs_path', help='path in cephfs')
chdir.set_defaults(func=chdir_handler)
listdir = sub.add_parser('ls', help='list directory')
listdir.add_argument('cephfs_path', help='path in cephfs',
nargs='?')
listdir.set_defaults(func=listdir_handler)
parsed_args = parser.parse_args(args)
return parser, parsed_args
def main():
if len(sys.argv)>1 and "-v" in sys.argv:
print('cephcli', version + tool.version())
return 0
parser, parsed_args = parse_cmdargs()
if parsed_args.verbose:
global verbose
verbose = parsed_args.verbose
try:
os.mkdir(default_log_dir)
except OSError:
pass
tool.set_log_dir(default_log_dir)
if verbose:
print('log to path', default_log_dir)
if parsed_args.userfile:
global user_info_file
user_info_file = parsed_args.userfile
global cephfs_root_dir
if parsed_args.root:
cephfs_root_dir = parsed_args.root if parsed_args.root[0]=='/' \
else '/' + parsed_args.root
else:
cephfs_root_dir = None
return parsed_args.func(parsed_args)
if __name__ == "__main__":
sys.exit(main())