]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | #!@PYTHON_EXECUTABLE@ |
2 | # -*- mode:python -*- | |
3 | # vim: ts=4 sw=4 smarttab expandtab | |
4 | # | |
5 | # Processed in Makefile to add python #! line and version variable | |
6 | # | |
7 | # | |
8 | ||
9 | ||
10 | """ | |
11 | ceph.in becomes ceph, the command-line management tool for Ceph clusters. | |
12 | This is a replacement for tools/ceph.cc and tools/common.cc. | |
13 | ||
14 | Copyright (C) 2013 Inktank Storage, Inc. | |
15 | ||
16 | This is free software; you can redistribute it and/or | |
17 | modify it under the terms of the GNU General Public | |
18 | License version 2, as published by the Free Software | |
19 | Foundation. See file COPYING. | |
20 | """ | |
21 | ||
22 | from __future__ import print_function | |
11fdf7f2 | 23 | from time import sleep |
7c673cae | 24 | import codecs |
a8e16298 | 25 | import grp |
7c673cae | 26 | import os |
a8e16298 | 27 | import pwd |
7c673cae | 28 | import sys |
11fdf7f2 | 29 | import time |
7c673cae FG |
30 | import platform |
31 | ||
32 | try: | |
33 | input = raw_input | |
34 | except NameError: | |
35 | pass | |
36 | ||
31f18b77 FG |
37 | CEPH_GIT_VER = "@CEPH_GIT_VER@" |
38 | CEPH_GIT_NICE_VER = "@CEPH_GIT_NICE_VER@" | |
39 | CEPH_RELEASE = "@CEPH_RELEASE@" | |
40 | CEPH_RELEASE_NAME = "@CEPH_RELEASE_NAME@" | |
41 | CEPH_RELEASE_TYPE = "@CEPH_RELEASE_TYPE@" | |
7c673cae | 42 | |
7c673cae FG |
43 | # priorities from src/common/perf_counters.h |
44 | PRIO_CRITICAL = 10 | |
45 | PRIO_INTERESTING = 8 | |
46 | PRIO_USEFUL = 5 | |
47 | PRIO_UNINTERESTING = 2 | |
48 | PRIO_DEBUGONLY = 0 | |
49 | ||
3efd9988 | 50 | PRIO_DEFAULT = PRIO_INTERESTING |
7c673cae FG |
51 | |
52 | # Make life easier on developers: | |
224ce89b WB |
53 | # If our parent dir contains CMakeCache.txt and bin/init-ceph, |
54 | # assume we're running from a build dir (i.e. src/build/bin/ceph) | |
55 | # and tweak sys.path and LD_LIBRARY_PATH to use built files. | |
56 | # Since this involves re-execing, if CEPH_DBG is set in the environment | |
57 | # re-exec with -mpdb. Also, if CEPH_DEV is in the env, suppress | |
58 | # the warning message about the DEVELOPER MODE. | |
7c673cae FG |
59 | |
60 | MYPATH = os.path.abspath(__file__) | |
61 | MYDIR = os.path.dirname(MYPATH) | |
224ce89b | 62 | MYPDIR = os.path.dirname(MYDIR) |
7c673cae FG |
63 | DEVMODEMSG = '*** DEVELOPER MODE: setting PATH, PYTHONPATH and LD_LIBRARY_PATH ***' |
64 | ||
31f18b77 | 65 | |
11fdf7f2 TL |
66 | def respawn_in_path(lib_path, pybind_path, pythonlib_path, asan_lib_path): |
67 | execv_cmd = [] | |
7c673cae | 68 | if 'CEPH_DBG' in os.environ: |
11fdf7f2 | 69 | execv_cmd += ['@PYTHON_EXECUTABLE@', '-mpdb'] |
7c673cae FG |
70 | |
71 | if platform.system() == "Darwin": | |
72 | lib_path_var = "DYLD_LIBRARY_PATH" | |
73 | else: | |
74 | lib_path_var = "LD_LIBRARY_PATH" | |
75 | ||
11fdf7f2 TL |
76 | execv_cmd += sys.argv |
77 | if asan_lib_path: | |
78 | os.environ['LD_PRELOAD'] = asan_lib_path | |
7c673cae FG |
79 | if lib_path_var in os.environ: |
80 | if lib_path not in os.environ[lib_path_var]: | |
81 | os.environ[lib_path_var] += ':' + lib_path | |
31f18b77 FG |
82 | if "CEPH_DEV" not in os.environ: |
83 | print(DEVMODEMSG, file=sys.stderr) | |
11fdf7f2 | 84 | os.execvp(execv_cmd[0], execv_cmd) |
7c673cae FG |
85 | else: |
86 | os.environ[lib_path_var] = lib_path | |
31f18b77 FG |
87 | if "CEPH_DEV" not in os.environ: |
88 | print(DEVMODEMSG, file=sys.stderr) | |
11fdf7f2 | 89 | os.execvp(execv_cmd[0], execv_cmd) |
7c673cae FG |
90 | sys.path.insert(0, os.path.join(MYDIR, pybind_path)) |
91 | sys.path.insert(0, os.path.join(MYDIR, pythonlib_path)) | |
92 | ||
31f18b77 | 93 | |
7c673cae FG |
94 | def get_pythonlib_dir(): |
95 | """Returns the name of a distutils build directory""" | |
96 | return "lib.{version[0]}".format(version=sys.version_info) | |
97 | ||
11fdf7f2 | 98 | |
eafe8130 | 99 | def get_cmake_variables(*names): |
11fdf7f2 TL |
100 | vars = dict((name, None) for name in names) |
101 | for line in open(os.path.join(MYPDIR, "CMakeCache.txt")): | |
102 | # parse lines like "WITH_ASAN:BOOL=ON" | |
103 | for name in names: | |
104 | if line.startswith("{}:".format(name)): | |
eafe8130 TL |
105 | type_value = line.split(":")[1].strip() |
106 | t, v = type_value.split("=") | |
107 | if t == 'BOOL': | |
108 | v = v.upper() in ('TRUE', '1', 'Y', 'YES', 'ON') | |
109 | vars[name] = v | |
11fdf7f2 TL |
110 | break |
111 | if all(vars.values()): | |
112 | break | |
eafe8130 | 113 | return [vars[name] for name in names] |
11fdf7f2 TL |
114 | |
115 | ||
224ce89b WB |
116 | if os.path.exists(os.path.join(MYPDIR, "CMakeCache.txt")) \ |
117 | and os.path.exists(os.path.join(MYPDIR, "bin/init-ceph")): | |
eafe8130 TL |
118 | src_path, with_asan, asan_lib_path = \ |
119 | get_cmake_variables("ceph_SOURCE_DIR", "WITH_ASAN", "ASAN_LIBRARY") | |
7c673cae FG |
120 | if src_path is None: |
121 | # Huh, maybe we're not really in a cmake environment? | |
122 | pass | |
123 | else: | |
124 | # Developer mode, but in a cmake build dir instead of the src dir | |
224ce89b WB |
125 | lib_path = os.path.join(MYPDIR, "lib") |
126 | bin_path = os.path.join(MYPDIR, "bin") | |
7c673cae FG |
127 | pybind_path = os.path.join(src_path, "src", "pybind") |
128 | pythonlib_path = os.path.join(lib_path, | |
129 | "cython_modules", | |
130 | get_pythonlib_dir()) | |
eafe8130 TL |
131 | respawn_in_path(lib_path, pybind_path, pythonlib_path, |
132 | asan_lib_path if with_asan else None) | |
7c673cae FG |
133 | |
134 | if 'PATH' in os.environ and bin_path not in os.environ['PATH']: | |
11fdf7f2 | 135 | os.environ['PATH'] = os.pathsep.join([bin_path, os.environ['PATH']]) |
7c673cae FG |
136 | |
137 | import argparse | |
138 | import errno | |
139 | import json | |
140 | import rados | |
141 | import shlex | |
142 | import signal | |
143 | import string | |
144 | import subprocess | |
145 | ||
146 | from ceph_argparse import \ | |
147 | concise_sig, descsort_key, parse_json_funcsigs, \ | |
11fdf7f2 TL |
148 | validate_command, find_cmd_target, \ |
149 | json_command, run_in_thread, Flag | |
7c673cae | 150 | |
31f18b77 | 151 | from ceph_daemon import admin_socket, DaemonWatcher, Termsize |
7c673cae FG |
152 | |
153 | # just a couple of globals | |
154 | ||
155 | verbose = False | |
156 | cluster_handle = None | |
157 | ||
158 | # Always use Unicode (UTF-8) for stdout | |
159 | if sys.version_info[0] >= 3: | |
160 | raw_stdout = sys.stdout.buffer | |
161 | raw_stderr = sys.stderr.buffer | |
162 | else: | |
163 | raw_stdout = sys.__stdout__ | |
164 | raw_stderr = sys.__stderr__ | |
165 | sys.stdout = codecs.getwriter('utf-8')(raw_stdout) | |
166 | sys.stderr = codecs.getwriter('utf-8')(raw_stderr) | |
167 | ||
31f18b77 | 168 | |
7c673cae FG |
169 | def raw_write(buf): |
170 | sys.stdout.flush() | |
11fdf7f2 | 171 | raw_stdout.write(rados.cstr(buf, '')) |
7c673cae | 172 | |
7c673cae FG |
173 | |
174 | def osdids(): | |
175 | ret, outbuf, outs = json_command(cluster_handle, prefix='osd ls') | |
7c673cae FG |
176 | if ret: |
177 | raise RuntimeError('Can\'t contact mon for osd list') | |
178 | return [line.decode('utf-8') for line in outbuf.split(b'\n') if line] | |
179 | ||
31f18b77 | 180 | |
7c673cae FG |
181 | def monids(): |
182 | ret, outbuf, outs = json_command(cluster_handle, prefix='mon dump', | |
31f18b77 | 183 | argdict={'format': 'json'}) |
7c673cae FG |
184 | if ret: |
185 | raise RuntimeError('Can\'t contact mon for mon list') | |
186 | d = json.loads(outbuf.decode('utf-8')) | |
187 | return [m['name'] for m in d['mons']] | |
188 | ||
31f18b77 | 189 | |
7c673cae | 190 | def mdsids(): |
181888fb | 191 | ret, outbuf, outs = json_command(cluster_handle, prefix='fs dump', |
31f18b77 | 192 | argdict={'format': 'json'}) |
7c673cae FG |
193 | if ret: |
194 | raise RuntimeError('Can\'t contact mon for mds list') | |
195 | d = json.loads(outbuf.decode('utf-8')) | |
196 | l = [] | |
181888fb FG |
197 | for info in d['standbys']: |
198 | l.append(info['name']) | |
199 | for fs in d['filesystems']: | |
200 | for info in fs['mdsmap']['info'].values(): | |
201 | l.append(info['name']) | |
7c673cae FG |
202 | return l |
203 | ||
31f18b77 FG |
204 | |
205 | def mgrids(): | |
206 | ret, outbuf, outs = json_command(cluster_handle, prefix='mgr dump', | |
207 | argdict={'format': 'json'}) | |
208 | if ret: | |
209 | raise RuntimeError('Can\'t contact mon for mgr list') | |
210 | ||
211 | d = json.loads(outbuf.decode('utf-8')) | |
212 | l = [] | |
213 | l.append(d['active_name']) | |
214 | for i in d['standbys']: | |
215 | l.append(i['name']) | |
216 | return l | |
217 | ||
218 | ||
181888fb FG |
219 | def ids_by_service(service): |
220 | ids = {"mon": monids, | |
221 | "osd": osdids, | |
222 | "mds": mdsids, | |
223 | "mgr": mgrids} | |
224 | return ids[service]() | |
225 | ||
226 | ||
31f18b77 FG |
227 | def validate_target(target): |
228 | """ | |
229 | this function will return true iff target is a correct | |
230 | target, such as mon.a/osd.2/mds.a/mgr. | |
231 | ||
232 | target: array, likes ['osd', '2'] | |
233 | return: bool, or raise RuntimeError | |
234 | """ | |
235 | ||
236 | if len(target) == 2: | |
237 | # for case "service.id" | |
238 | service_name, service_id = target[0], target[1] | |
181888fb FG |
239 | try: |
240 | exist_ids = ids_by_service(service_name) | |
241 | except KeyError: | |
31f18b77 FG |
242 | print('WARN: {0} is not a legal service name, should be one of mon/osd/mds/mgr'.format(service_name), |
243 | file=sys.stderr) | |
244 | return False | |
245 | ||
3efd9988 | 246 | if service_id in exist_ids or len(exist_ids) > 0 and service_id == '*': |
31f18b77 FG |
247 | return True |
248 | else: | |
249 | print('WARN: the service id you provided does not exist. service id should ' | |
250 | 'be one of {0}.'.format('/'.join(exist_ids)), file=sys.stderr) | |
251 | return False | |
252 | ||
253 | elif len(target) == 1 and target[0] in ['mgr', 'mon']: | |
254 | return True | |
255 | else: | |
256 | print('WARN: \"{0}\" is not a legal target. it should be one of mon.<id>/osd.<int>/mds.<id>/mgr'.format('.'.join(target)), file=sys.stderr) | |
257 | return False | |
258 | ||
259 | ||
7c673cae FG |
260 | # these args must be passed to all child programs |
261 | GLOBAL_ARGS = { | |
262 | 'client_id': '--id', | |
263 | 'client_name': '--name', | |
264 | 'cluster': '--cluster', | |
265 | 'cephconf': '--conf', | |
266 | } | |
267 | ||
31f18b77 | 268 | |
7c673cae | 269 | def parse_cmdargs(args=None, target=''): |
11fdf7f2 TL |
270 | """ |
271 | Consume generic arguments from the start of the ``args`` | |
272 | list. Call this first to handle arguments that are not | |
273 | handled by a command description provided by the server. | |
274 | ||
275 | :returns: three tuple of ArgumentParser instance, Namespace instance | |
276 | containing parsed values, and list of un-handled arguments | |
277 | """ | |
7c673cae FG |
278 | # alias: let the line-wrapping be sane |
279 | AP = argparse.ArgumentParser | |
280 | ||
281 | # format our own help | |
282 | parser = AP(description='Ceph administration tool', add_help=False) | |
283 | ||
284 | parser.add_argument('--completion', action='store_true', | |
285 | help=argparse.SUPPRESS) | |
286 | ||
287 | parser.add_argument('-h', '--help', help='request mon help', | |
288 | action='store_true') | |
289 | ||
290 | parser.add_argument('-c', '--conf', dest='cephconf', | |
291 | help='ceph configuration file') | |
292 | parser.add_argument('-i', '--in-file', dest='input_file', | |
c07f9fc5 | 293 | help='input file, or "-" for stdin') |
7c673cae | 294 | parser.add_argument('-o', '--out-file', dest='output_file', |
c07f9fc5 | 295 | help='output file, or "-" for stdout') |
a8e16298 TL |
296 | parser.add_argument('--setuser', dest='setuser', |
297 | help='set user file permission') | |
298 | parser.add_argument('--setgroup', dest='setgroup', | |
299 | help='set group file permission') | |
7c673cae FG |
300 | parser.add_argument('--id', '--user', dest='client_id', |
301 | help='client id for authentication') | |
302 | parser.add_argument('--name', '-n', dest='client_name', | |
303 | help='client name for authentication') | |
304 | parser.add_argument('--cluster', help='cluster name') | |
305 | ||
306 | parser.add_argument('--admin-daemon', dest='admin_socket', | |
307 | help='submit admin-socket commands (\"help\" for help') | |
7c673cae FG |
308 | |
309 | parser.add_argument('-s', '--status', action='store_true', | |
310 | help='show cluster status') | |
311 | ||
312 | parser.add_argument('-w', '--watch', action='store_true', | |
313 | help='watch live cluster changes') | |
314 | parser.add_argument('--watch-debug', action='store_true', | |
315 | help='watch debug events') | |
316 | parser.add_argument('--watch-info', action='store_true', | |
317 | help='watch info events') | |
318 | parser.add_argument('--watch-sec', action='store_true', | |
319 | help='watch security events') | |
320 | parser.add_argument('--watch-warn', action='store_true', | |
321 | help='watch warn events') | |
322 | parser.add_argument('--watch-error', action='store_true', | |
323 | help='watch error events') | |
324 | ||
224ce89b | 325 | parser.add_argument('--watch-channel', dest="watch_channel", |
11fdf7f2 | 326 | choices=['cluster', 'audit', '*'], |
224ce89b | 327 | help="which log channel to follow " \ |
11fdf7f2 | 328 | "when using -w/--watch. One of ['cluster', 'audit', '*']", |
224ce89b WB |
329 | default='cluster') |
330 | ||
7c673cae FG |
331 | parser.add_argument('--version', '-v', action="store_true", help="display version") |
332 | parser.add_argument('--verbose', action="store_true", help="make verbose") | |
333 | parser.add_argument('--concise', dest='verbose', action="store_false", | |
334 | help="make less verbose") | |
335 | ||
336 | parser.add_argument('-f', '--format', choices=['json', 'json-pretty', | |
337 | 'xml', 'xml-pretty', 'plain'], dest='output_format') | |
338 | ||
339 | parser.add_argument('--connect-timeout', dest='cluster_timeout', | |
340 | type=int, | |
341 | help='set a timeout for connecting to the cluster') | |
342 | ||
11fdf7f2 TL |
343 | parser.add_argument('--block', action='store_true', |
344 | help='block until completion (scrub and deep-scrub only)') | |
345 | parser.add_argument('--period', '-p', default=1, type=float, | |
346 | help='polling period, default 1.0 second (for ' \ | |
347 | 'polling commands only)') | |
348 | ||
7c673cae FG |
349 | # returns a Namespace with the parsed args, and a list of all extras |
350 | parsed_args, extras = parser.parse_known_args(args) | |
351 | ||
352 | return parser, parsed_args, extras | |
353 | ||
354 | ||
355 | def hdr(s): | |
356 | print('\n', s, '\n', '=' * len(s)) | |
357 | ||
31f18b77 | 358 | |
7c673cae FG |
359 | def do_basic_help(parser, args): |
360 | """ | |
361 | Print basic parser help | |
362 | If the cluster is available, get and print monitor help | |
363 | """ | |
364 | hdr('General usage:') | |
365 | parser.print_help() | |
366 | print_locally_handled_command_help() | |
367 | ||
31f18b77 | 368 | |
7c673cae FG |
369 | def print_locally_handled_command_help(): |
370 | hdr("Local commands:") | |
371 | print(""" | |
372 | ping <mon.id> Send simple presence/life test to a mon | |
373 | <mon.id> may be 'mon.*' for all mons | |
374 | daemon {type.id|path} <cmd> | |
375 | Same as --admin-daemon, but auto-find admin socket | |
376 | daemonperf {type.id | path} [stat-pats] [priority] [<interval>] [<count>] | |
377 | daemonperf {type.id | path} list|ls [stat-pats] [priority] | |
378 | Get selected perf stats from daemon/admin socket | |
379 | Optional shell-glob comma-delim match string stat-pats | |
380 | Optional selection priority (can abbreviate name): | |
381 | critical, interesting, useful, noninteresting, debug | |
382 | List shows a table of all available stats | |
383 | Run <count> times (default forever), | |
384 | once per <interval> seconds (default 1) | |
385 | """, file=sys.stdout) | |
386 | ||
387 | ||
31f18b77 | 388 | def do_extended_help(parser, args, target, partial): |
7c673cae FG |
389 | def help_for_sigs(sigs, partial=None): |
390 | sys.stdout.write(format_help(parse_json_funcsigs(sigs, 'cli'), | |
391 | partial=partial)) | |
392 | ||
393 | def help_for_target(target, partial=None): | |
d2e6a577 FG |
394 | # wait for osdmap because we know this is sent after the mgrmap |
395 | # and monmap (it's alphabetical). | |
396 | cluster_handle.wait_for_latest_osdmap() | |
7c673cae FG |
397 | ret, outbuf, outs = json_command(cluster_handle, target=target, |
398 | prefix='get_command_descriptions', | |
399 | timeout=10) | |
400 | if ret: | |
11fdf7f2 TL |
401 | if ret == -errno.EPERM and target[0] in ('osd', 'mds'): |
402 | print("Permission denied. Check that your user has 'allow *' " | |
403 | "capabilities for the target daemon type.", file=sys.stderr) | |
404 | elif ret == -errno.EPERM: | |
405 | print("Permission denied. Check your user has proper " | |
406 | "capabilities configured", file=sys.stderr) | |
407 | else: | |
408 | print("couldn't get command descriptions for {0}: {1} ({2})". | |
409 | format(target, outs, ret), file=sys.stderr) | |
31f18b77 | 410 | return ret |
7c673cae | 411 | else: |
31f18b77 | 412 | return help_for_sigs(outbuf.decode('utf-8'), partial) |
7c673cae | 413 | |
31f18b77 FG |
414 | assert(cluster_handle.state == "connected") |
415 | return help_for_target(target, partial) | |
7c673cae FG |
416 | |
417 | DONTSPLIT = string.ascii_letters + '{[<>]}' | |
418 | ||
31f18b77 | 419 | |
7c673cae FG |
420 | def wrap(s, width, indent): |
421 | """ | |
422 | generator to transform s into a sequence of strings width or shorter, | |
423 | for wrapping text to a specific column width. | |
424 | Attempt to break on anything but DONTSPLIT characters. | |
425 | indent is amount to indent 2nd-through-nth lines. | |
426 | ||
427 | so "long string long string long string" width=11 indent=1 becomes | |
428 | 'long string', ' long string', ' long string' so that it can be printed | |
429 | as | |
430 | long string | |
431 | long string | |
432 | long string | |
433 | ||
434 | Consumes s. | |
435 | """ | |
436 | result = '' | |
437 | leader = '' | |
438 | while len(s): | |
439 | ||
31f18b77 | 440 | if len(s) <= width: |
7c673cae FG |
441 | # no splitting; just possibly indent |
442 | result = leader + s | |
443 | s = '' | |
444 | yield result | |
445 | ||
446 | else: | |
447 | splitpos = width | |
448 | while (splitpos > 0) and (s[splitpos-1] in DONTSPLIT): | |
449 | splitpos -= 1 | |
450 | ||
451 | if splitpos == 0: | |
452 | splitpos = width | |
453 | ||
454 | if result: | |
455 | # prior result means we're mid-iteration, indent | |
456 | result = leader | |
457 | else: | |
458 | # first time, set leader and width for next | |
459 | leader = ' ' * indent | |
460 | width -= 1 # for subsequent space additions | |
461 | ||
462 | # remove any leading spaces in this chunk of s | |
463 | result += s[:splitpos].lstrip() | |
464 | s = s[splitpos:] | |
465 | ||
466 | yield result | |
467 | ||
31f18b77 | 468 | |
7c673cae FG |
469 | def format_help(cmddict, partial=None): |
470 | """ | |
471 | Formats all the cmdsigs and helptexts from cmddict into a sorted-by- | |
472 | cmdsig 2-column display, with each column wrapped and indented to | |
31f18b77 | 473 | fit into (terminal_width / 2) characters. |
7c673cae FG |
474 | """ |
475 | ||
476 | fullusage = '' | |
477 | for cmd in sorted(cmddict.values(), key=descsort_key): | |
478 | ||
479 | if not cmd['help']: | |
480 | continue | |
31f18b77 | 481 | flags = cmd.get('flags', 0) |
11fdf7f2 | 482 | if flags & (Flag.OBSOLETE | Flag.DEPRECATED | Flag.HIDDEN): |
7c673cae FG |
483 | continue |
484 | concise = concise_sig(cmd['sig']) | |
485 | if partial and not concise.startswith(partial): | |
486 | continue | |
31f18b77 FG |
487 | width = Termsize().cols - 1 # 1 for the line between sig and help |
488 | sig_width = int(width / 2) | |
489 | # make sure width == sig_width + help_width, even (width % 2 > 0) | |
490 | help_width = int(width / 2) + (width % 2) | |
491 | siglines = [l for l in wrap(concise, sig_width, 1)] | |
492 | helplines = [l for l in wrap(cmd['help'], help_width, 1)] | |
7c673cae FG |
493 | |
494 | # make lists the same length | |
495 | maxlen = max(len(siglines), len(helplines)) | |
496 | siglines.extend([''] * (maxlen - len(siglines))) | |
497 | helplines.extend([''] * (maxlen - len(helplines))) | |
498 | ||
499 | # so we can zip them for output | |
31f18b77 FG |
500 | for s, h in zip(siglines, helplines): |
501 | fullusage += '{s:{w}s} {h}\n'.format(s=s, h=h, w=sig_width) | |
7c673cae FG |
502 | |
503 | return fullusage | |
504 | ||
505 | ||
506 | def ceph_conf(parsed_args, field, name): | |
31f18b77 | 507 | args = ['ceph-conf'] |
7c673cae FG |
508 | |
509 | if name: | |
510 | args.extend(['--name', name]) | |
511 | ||
512 | # add any args in GLOBAL_ARGS | |
513 | for key, val in GLOBAL_ARGS.items(): | |
514 | # ignore name in favor of argument name, if any | |
515 | if name and key == 'client_name': | |
516 | continue | |
517 | if getattr(parsed_args, key): | |
518 | args.extend([val, getattr(parsed_args, key)]) | |
519 | ||
520 | args.extend(['--show-config-value', field]) | |
521 | p = subprocess.Popen( | |
522 | args, | |
523 | stdout=subprocess.PIPE, | |
524 | stderr=subprocess.PIPE) | |
525 | outdata, errdata = p.communicate() | |
92f5a8d4 | 526 | if p.returncode != 0: |
7c673cae FG |
527 | raise RuntimeError('unable to get conf option %s for %s: %s' % (field, name, errdata)) |
528 | return outdata.rstrip() | |
529 | ||
530 | PROMPT = 'ceph> ' | |
531 | ||
532 | if sys.stdin.isatty(): | |
533 | def read_input(): | |
534 | while True: | |
535 | line = input(PROMPT).rstrip() | |
536 | if line in ['q', 'quit', 'Q', 'exit']: | |
537 | return None | |
538 | if line: | |
539 | return line | |
540 | else: | |
541 | def read_input(): | |
542 | while True: | |
543 | line = sys.stdin.readline() | |
544 | if not line: | |
545 | return None | |
546 | line = line.rstrip() | |
547 | if line: | |
548 | return line | |
549 | ||
550 | ||
11fdf7f2 TL |
551 | def do_command(parsed_args, target, cmdargs, sigdict, inbuf, verbose): |
552 | ''' Validate a command, and handle the polling flag ''' | |
553 | ||
554 | valid_dict = validate_command(sigdict, cmdargs, verbose) | |
555 | # Validate input args against list of sigs | |
556 | if valid_dict: | |
557 | if parsed_args.output_format: | |
558 | valid_dict['format'] = parsed_args.output_format | |
559 | if verbose: | |
560 | print("Submitting command: ", valid_dict, file=sys.stderr) | |
561 | else: | |
562 | return -errno.EINVAL, '', 'invalid command' | |
563 | ||
564 | next_header_print = 0 | |
565 | # Set extra options for polling commands only: | |
566 | if valid_dict.get('poll', False): | |
567 | valid_dict['width'] = Termsize().cols | |
568 | while True: | |
569 | try: | |
570 | # Only print the header for polling commands | |
571 | if next_header_print == 0 and valid_dict.get('poll', False): | |
572 | valid_dict['print_header'] = True | |
573 | next_header_print = Termsize().rows - 3 | |
574 | next_header_print -= 1 | |
575 | ret, outbuf, outs = json_command(cluster_handle, target=target, | |
576 | argdict=valid_dict, inbuf=inbuf) | |
577 | if valid_dict.get('poll', False): | |
578 | valid_dict['print_header'] = False | |
579 | if not valid_dict.get('poll', False): | |
580 | # Don't print here if it's not a polling command | |
581 | break | |
582 | if ret: | |
583 | ret = abs(ret) | |
584 | print('Error: {0} {1}'.format(ret, errno.errorcode.get(ret, 'Unknown')), | |
585 | file=sys.stderr) | |
586 | break | |
587 | if outbuf: | |
588 | print(outbuf.decode('utf-8')) | |
589 | if outs: | |
590 | print(outs, file=sys.stderr) | |
591 | if parsed_args.period <= 0: | |
592 | break | |
593 | sleep(parsed_args.period) | |
594 | except KeyboardInterrupt: | |
595 | print('Interrupted') | |
596 | return ret, '', '' | |
597 | if ret == errno.ETIMEDOUT: | |
598 | ret = -ret | |
599 | if not outs: | |
600 | outs = ("Connection timed out. Please check the client's " + | |
601 | "permission and connection.") | |
602 | return ret, outbuf, outs | |
603 | ||
604 | ||
7c673cae FG |
605 | def new_style_command(parsed_args, cmdargs, target, sigdict, inbuf, verbose): |
606 | """ | |
607 | Do new-style command dance. | |
608 | target: daemon to receive command: mon (any) or osd.N | |
609 | sigdict - the parsed output from the new monitor describing commands | |
610 | inbuf - any -i input file data | |
611 | verbose - bool | |
612 | """ | |
613 | if verbose: | |
614 | for cmdtag in sorted(sigdict.keys()): | |
615 | cmd = sigdict[cmdtag] | |
616 | sig = cmd['sig'] | |
617 | print('{0}: {1}'.format(cmdtag, concise_sig(sig))) | |
618 | ||
619 | if True: | |
620 | if cmdargs: | |
11fdf7f2 TL |
621 | # Non interactive mode |
622 | ret, outbuf, outs = do_command(parsed_args, target, cmdargs, sigdict, inbuf, verbose) | |
7c673cae | 623 | else: |
11fdf7f2 | 624 | # Interactive mode (ceph cli) |
7c673cae FG |
625 | if sys.stdin.isatty(): |
626 | # do the command-interpreter looping | |
627 | # for input to do readline cmd editing | |
628 | import readline # noqa | |
629 | ||
630 | while True: | |
11fdf7f2 TL |
631 | try: |
632 | interactive_input = read_input() | |
633 | except EOFError: | |
634 | # leave user an uncluttered prompt | |
635 | return 0, '\n', '' | |
7c673cae FG |
636 | if interactive_input is None: |
637 | return 0, '', '' | |
638 | cmdargs = parse_cmdargs(shlex.split(interactive_input))[2] | |
639 | try: | |
640 | target = find_cmd_target(cmdargs) | |
641 | except Exception as e: | |
31f18b77 FG |
642 | print('error handling command target: {0}'.format(e), |
643 | file=sys.stderr) | |
7c673cae FG |
644 | continue |
645 | if len(cmdargs) and cmdargs[0] == 'tell': | |
31f18b77 FG |
646 | print('Can not use \'tell\' in interactive mode.', |
647 | file=sys.stderr) | |
7c673cae | 648 | continue |
11fdf7f2 TL |
649 | ret, outbuf, outs = do_command(parsed_args, target, cmdargs, |
650 | sigdict, inbuf, verbose) | |
651 | if ret < 0: | |
652 | ret = -ret | |
653 | errstr = errno.errorcode.get(ret, 'Unknown') | |
654 | print(u'Error {0}: {1}'.format(errstr, outs), file=sys.stderr) | |
655 | else: | |
656 | if outs: | |
657 | print(outs, file=sys.stderr) | |
7c673cae FG |
658 | if outbuf: |
659 | print(outbuf) | |
7c673cae | 660 | |
11fdf7f2 | 661 | return ret, outbuf, outs |
7c673cae FG |
662 | |
663 | ||
664 | def complete(sigdict, args, target): | |
665 | """ | |
666 | Command completion. Match as much of [args] as possible, | |
667 | and print every possible match separated by newlines. | |
668 | Return exitcode. | |
669 | """ | |
670 | # XXX this looks a lot like the front of validate_command(). Refactor? | |
671 | ||
7c673cae FG |
672 | # Repulsive hack to handle tell: lop off 'tell' and target |
673 | # and validate the rest of the command. 'target' is already | |
674 | # determined in our callers, so it's ok to remove it here. | |
675 | if len(args) and args[0] == 'tell': | |
676 | args = args[2:] | |
677 | # look for best match, accumulate possibles in bestcmds | |
678 | # (so we can maybe give a more-useful error message) | |
679 | ||
680 | match_count = 0 | |
681 | comps = [] | |
682 | for cmdtag, cmd in sigdict.items(): | |
683 | sig = cmd['sig'] | |
684 | j = 0 | |
685 | # iterate over all arguments, except last one | |
686 | for arg in args[0:-1]: | |
687 | if j > len(sig)-1: | |
688 | # an out of argument definitions | |
689 | break | |
690 | found_match = arg in sig[j].complete(arg) | |
691 | if not found_match and sig[j].req: | |
692 | # no elements that match | |
693 | break | |
694 | if not sig[j].N: | |
695 | j += 1 | |
696 | else: | |
697 | # successfully matched all - except last one - arguments | |
698 | if j < len(sig) and len(args) > 0: | |
699 | comps += sig[j].complete(args[-1]) | |
700 | ||
701 | match_count += 1 | |
702 | match_cmd = cmd | |
703 | ||
704 | if match_count == 1 and len(comps) == 0: | |
705 | # only one command matched and no hints yet => add help | |
706 | comps = comps + [' ', '#'+match_cmd['help']] | |
707 | print('\n'.join(sorted(set(comps)))) | |
708 | return 0 | |
709 | ||
710 | ||
7c673cae FG |
711 | def ping_monitor(cluster_handle, name, timeout): |
712 | if 'mon.' not in name: | |
713 | print('"ping" expects a monitor to ping; try "ping mon.<id>"', file=sys.stderr) | |
714 | return 1 | |
715 | ||
716 | mon_id = name[len('mon.'):] | |
31f18b77 | 717 | if mon_id == '*': |
7c673cae | 718 | run_in_thread(cluster_handle.connect, timeout=timeout) |
31f18b77 | 719 | for m in monids(): |
7c673cae FG |
720 | s = run_in_thread(cluster_handle.ping_monitor, m) |
721 | if s is None: | |
722 | print("mon.{0}".format(m) + '\n' + "Error connecting to monitor.") | |
723 | else: | |
724 | print("mon.{0}".format(m) + '\n' + s) | |
31f18b77 | 725 | else: |
7c673cae FG |
726 | s = run_in_thread(cluster_handle.ping_monitor, mon_id) |
727 | print(s) | |
728 | return 0 | |
729 | ||
730 | ||
731 | def maybe_daemon_command(parsed_args, childargs): | |
732 | """ | |
733 | Check if --admin-socket, daemon, or daemonperf command | |
734 | if it is, returns (boolean handled, return code if handled == True) | |
735 | """ | |
736 | ||
737 | daemon_perf = False | |
738 | sockpath = None | |
739 | if parsed_args.admin_socket: | |
740 | sockpath = parsed_args.admin_socket | |
741 | elif len(childargs) > 0 and childargs[0] in ["daemon", "daemonperf"]: | |
742 | daemon_perf = (childargs[0] == "daemonperf") | |
743 | # Treat "daemon <path>" or "daemon <name>" like --admin_daemon <path> | |
744 | # Handle "daemonperf <path>" the same but requires no trailing args | |
745 | require_args = 2 if daemon_perf else 3 | |
746 | if len(childargs) >= require_args: | |
747 | if childargs[1].find('/') >= 0: | |
748 | sockpath = childargs[1] | |
749 | else: | |
750 | # try resolve daemon name | |
751 | try: | |
752 | sockpath = ceph_conf(parsed_args, 'admin_socket', | |
753 | childargs[1]) | |
754 | except Exception as e: | |
755 | print('Can\'t get admin socket path: ' + str(e), file=sys.stderr) | |
756 | return True, errno.EINVAL | |
757 | # for both: | |
758 | childargs = childargs[2:] | |
759 | else: | |
31f18b77 FG |
760 | print('{0} requires at least {1} arguments'.format(childargs[0], require_args), |
761 | file=sys.stderr) | |
7c673cae FG |
762 | return True, errno.EINVAL |
763 | ||
764 | if sockpath and daemon_perf: | |
765 | return True, daemonperf(childargs, sockpath) | |
766 | elif sockpath: | |
767 | try: | |
768 | raw_write(admin_socket(sockpath, childargs, parsed_args.output_format)) | |
769 | except Exception as e: | |
770 | print('admin_socket: {0}'.format(e), file=sys.stderr) | |
771 | return True, errno.EINVAL | |
772 | return True, 0 | |
773 | ||
774 | return False, 0 | |
775 | ||
776 | ||
777 | def isnum(s): | |
778 | try: | |
779 | float(s) | |
780 | return True | |
781 | except ValueError: | |
782 | return False | |
783 | ||
31f18b77 | 784 | |
7c673cae FG |
785 | def daemonperf(childargs, sockpath): |
786 | """ | |
787 | Handle daemonperf command; returns errno or 0 | |
788 | ||
789 | daemonperf <daemon> [priority string] [statpats] [interval] [count] | |
790 | daemonperf <daemon> list|ls [statpats] | |
791 | """ | |
792 | ||
793 | interval = 1 | |
794 | count = None | |
795 | statpats = None | |
796 | priority = None | |
797 | do_list = False | |
798 | ||
799 | def prio_from_name(arg): | |
800 | ||
801 | PRIOMAP = { | |
802 | 'critical': PRIO_CRITICAL, | |
803 | 'interesting': PRIO_INTERESTING, | |
804 | 'useful': PRIO_USEFUL, | |
805 | 'uninteresting': PRIO_UNINTERESTING, | |
806 | 'debugonly': PRIO_DEBUGONLY, | |
807 | } | |
808 | ||
809 | if arg in PRIOMAP: | |
810 | return PRIOMAP[arg] | |
811 | # allow abbreviation | |
812 | for name, val in PRIOMAP.items(): | |
813 | if name.startswith(arg): | |
814 | return val | |
815 | return None | |
816 | ||
817 | # consume and analyze non-numeric args | |
818 | while len(childargs) and not isnum(childargs[0]): | |
819 | arg = childargs.pop(0) | |
820 | # 'list'? | |
821 | if arg in ['list', 'ls']: | |
31f18b77 | 822 | do_list = True |
7c673cae FG |
823 | continue |
824 | # prio? | |
825 | prio = prio_from_name(arg) | |
826 | if prio is not None: | |
827 | priority = prio | |
828 | continue | |
829 | # statpats | |
830 | statpats = arg.split(',') | |
831 | ||
832 | if priority is None: | |
833 | priority = PRIO_DEFAULT | |
834 | ||
835 | if len(childargs) > 0: | |
836 | try: | |
837 | interval = float(childargs.pop(0)) | |
838 | if interval < 0: | |
839 | raise ValueError | |
840 | except ValueError: | |
841 | print('daemonperf: interval should be a positive number', file=sys.stderr) | |
842 | return errno.EINVAL | |
843 | ||
844 | if len(childargs) > 0: | |
845 | arg = childargs.pop(0) | |
846 | if (not isnum(arg)) or (int(arg) < 0): | |
847 | print('daemonperf: count should be a positive integer', file=sys.stderr) | |
848 | return errno.EINVAL | |
849 | count = int(arg) | |
850 | ||
851 | watcher = DaemonWatcher(sockpath, statpats, priority) | |
852 | if do_list: | |
853 | watcher.list() | |
854 | else: | |
855 | watcher.run(interval, count) | |
856 | ||
857 | return 0 | |
858 | ||
11fdf7f2 TL |
859 | def get_scrub_timestamps(childargs): |
860 | last_scrub_stamp = "last_" + childargs[1].replace('-', '_') + "_stamp" | |
861 | results = dict() | |
862 | scruball = False | |
863 | if childargs[2] in ['all', 'any', '*']: | |
864 | scruball = True | |
865 | devnull = open(os.devnull, 'w') | |
866 | out = subprocess.check_output(['ceph', 'pg', 'dump', '--format=json-pretty'], | |
867 | stderr=devnull) | |
868 | try: | |
869 | pgstats = json.loads(out)['pg_map']['pg_stats'] | |
870 | except KeyError: | |
871 | pgstats = json.loads(out)['pg_stats'] | |
872 | for stat in pgstats: | |
873 | if scruball or stat['up_primary'] == int(childargs[2]): | |
874 | scrub_tuple = (stat['up_primary'], stat[last_scrub_stamp]) | |
875 | results[stat['pgid']] = scrub_tuple | |
876 | return results | |
877 | ||
878 | def check_scrub_stamps(waitdata, currdata): | |
879 | for pg in waitdata.keys(): | |
880 | # Try to handle the case where a pg may not exist in current results | |
881 | if pg in currdata and waitdata[pg][1] == currdata[pg][1]: | |
882 | return False | |
883 | return True | |
884 | ||
885 | def waitscrub(childargs, waitdata): | |
886 | print(u'Waiting for {0} to complete...'.format(childargs[1]), file=sys.stdout) | |
887 | currdata = get_scrub_timestamps(childargs) | |
888 | while not check_scrub_stamps(waitdata, currdata): | |
889 | time.sleep(3) | |
890 | currdata = get_scrub_timestamps(childargs) | |
891 | print(u'{0} completed'.format(childargs[1]), file=sys.stdout) | |
892 | ||
893 | def wait(childargs, waitdata): | |
894 | if childargs[1] in ['scrub', 'deep-scrub']: | |
895 | waitscrub(childargs, waitdata) | |
896 | ||
7c673cae FG |
897 | |
898 | def main(): | |
899 | ceph_args = os.environ.get('CEPH_ARGS') | |
900 | if ceph_args: | |
901 | if "injectargs" in sys.argv: | |
902 | i = sys.argv.index("injectargs") | |
903 | sys.argv = sys.argv[:i] + ceph_args.split() + sys.argv[i:] | |
904 | else: | |
c07f9fc5 FG |
905 | sys.argv.extend([arg for arg in ceph_args.split() |
906 | if '--admin-socket' not in arg]) | |
7c673cae FG |
907 | parser, parsed_args, childargs = parse_cmdargs() |
908 | ||
909 | if parsed_args.version: | |
31f18b77 FG |
910 | print('ceph version {0} ({1}) {2} ({3})'.format( |
911 | CEPH_GIT_NICE_VER, | |
912 | CEPH_GIT_VER, | |
913 | CEPH_RELEASE_NAME, | |
914 | CEPH_RELEASE_TYPE)) # noqa | |
7c673cae FG |
915 | return 0 |
916 | ||
917 | global verbose | |
918 | verbose = parsed_args.verbose | |
919 | ||
920 | if verbose: | |
921 | print("parsed_args: {0}, childargs: {1}".format(parsed_args, childargs), file=sys.stderr) | |
922 | ||
7c673cae FG |
923 | # pass on --id, --name, --conf |
924 | name = 'client.admin' | |
925 | if parsed_args.client_id: | |
926 | name = 'client.' + parsed_args.client_id | |
927 | if parsed_args.client_name: | |
928 | name = parsed_args.client_name | |
929 | ||
930 | # default '' means default conf search | |
931 | conffile = '' | |
932 | if parsed_args.cephconf: | |
933 | conffile = parsed_args.cephconf | |
934 | # For now, --admin-daemon is handled as usual. Try it | |
935 | # first in case we can't connect() to the cluster | |
936 | ||
7c673cae FG |
937 | done, ret = maybe_daemon_command(parsed_args, childargs) |
938 | if done: | |
939 | return ret | |
940 | ||
941 | timeout = None | |
942 | if parsed_args.cluster_timeout: | |
943 | timeout = parsed_args.cluster_timeout | |
944 | ||
945 | # basic help | |
946 | if parsed_args.help: | |
947 | do_basic_help(parser, childargs) | |
948 | ||
949 | # handle any 'generic' ceph arguments that we didn't parse here | |
950 | global cluster_handle | |
951 | ||
952 | # rados.Rados() will call rados_create2, and then read the conf file, | |
953 | # and then set the keys from the dict. So we must do these | |
954 | # "pre-file defaults" first (see common_preinit in librados) | |
955 | conf_defaults = { | |
31f18b77 FG |
956 | 'log_to_stderr': 'true', |
957 | 'err_to_stderr': 'true', | |
958 | 'log_flush_on_exit': 'true', | |
7c673cae FG |
959 | } |
960 | ||
961 | if 'injectargs' in childargs: | |
962 | position = childargs.index('injectargs') | |
963 | injectargs = childargs[position:] | |
964 | childargs = childargs[:position] | |
965 | if verbose: | |
31f18b77 FG |
966 | print('Separate childargs {0} from injectargs {1}'.format(childargs, injectargs), |
967 | file=sys.stderr) | |
7c673cae FG |
968 | else: |
969 | injectargs = None | |
970 | ||
971 | clustername = None | |
972 | if parsed_args.cluster: | |
973 | clustername = parsed_args.cluster | |
974 | ||
975 | try: | |
976 | cluster_handle = run_in_thread(rados.Rados, | |
977 | name=name, clustername=clustername, | |
978 | conf_defaults=conf_defaults, | |
979 | conffile=conffile) | |
980 | retargs = run_in_thread(cluster_handle.conf_parse_argv, childargs) | |
981 | except rados.Error as e: | |
982 | print('Error initializing cluster client: {0!r}'.format(e), file=sys.stderr) | |
983 | return 1 | |
984 | ||
985 | childargs = retargs | |
986 | if not childargs: | |
987 | childargs = [] | |
988 | ||
989 | # -- means "stop parsing args", but we don't want to see it either | |
990 | if '--' in childargs: | |
991 | childargs.remove('--') | |
992 | if injectargs and '--' in injectargs: | |
993 | injectargs.remove('--') | |
994 | ||
995 | # special deprecation warning for 'ceph <type> tell' | |
996 | # someday 'mds' will be here too | |
31f18b77 FG |
997 | if (len(childargs) >= 2 and |
998 | childargs[0] in ['mon', 'osd'] and | |
999 | childargs[1] == 'tell'): | |
1000 | print('"{0} tell" is deprecated; try "tell {0}.<id> <command> [options...]" instead (id can be "*") '.format(childargs[0]), | |
1001 | file=sys.stderr) | |
7c673cae FG |
1002 | return 1 |
1003 | ||
11fdf7f2 TL |
1004 | block = False |
1005 | waitdata = dict() | |
1006 | if parsed_args.block: | |
1007 | if (len(childargs) >= 2 and | |
1008 | childargs[0] == 'osd' and | |
1009 | childargs[1] in ['deep-scrub', 'scrub']): | |
1010 | block = True | |
1011 | waitdata = get_scrub_timestamps(childargs) | |
1012 | ||
7c673cae FG |
1013 | if parsed_args.help: |
1014 | # short default timeout for -h | |
1015 | if not timeout: | |
1016 | timeout = 5 | |
1017 | ||
224ce89b | 1018 | if childargs and childargs[0] == 'ping' and not parsed_args.help: |
7c673cae FG |
1019 | if len(childargs) < 2: |
1020 | print('"ping" requires a monitor name as argument: "ping mon.<id>"', file=sys.stderr) | |
1021 | return 1 | |
1022 | if parsed_args.completion: | |
31f18b77 | 1023 | # for completion let timeout be really small |
7c673cae FG |
1024 | timeout = 3 |
1025 | try: | |
224ce89b | 1026 | if childargs and childargs[0] == 'ping' and not parsed_args.help: |
7c673cae | 1027 | return ping_monitor(cluster_handle, childargs[1], timeout) |
224ce89b WB |
1028 | result = run_in_thread(cluster_handle.connect, timeout=timeout) |
1029 | if type(result) is tuple and result[0] == -errno.EINTR: | |
1030 | print('Cluster connection interrupted or timed out', file=sys.stderr) | |
1031 | return 1 | |
7c673cae FG |
1032 | except KeyboardInterrupt: |
1033 | print('Cluster connection aborted', file=sys.stderr) | |
1034 | return 1 | |
1035 | except rados.PermissionDeniedError as e: | |
1036 | print(str(e), file=sys.stderr) | |
1037 | return errno.EACCES | |
1038 | except Exception as e: | |
1039 | print(str(e), file=sys.stderr) | |
1040 | return 1 | |
224ce89b | 1041 | |
7c673cae | 1042 | if parsed_args.help: |
224ce89b WB |
1043 | hdr('Monitor commands:') |
1044 | if verbose: | |
1045 | print('[Contacting monitor, timeout after %d seconds]' % timeout) | |
1046 | ||
31f18b77 | 1047 | return do_extended_help(parser, childargs, ('mon', ''), ' '.join(childargs)) |
7c673cae | 1048 | |
31f18b77 FG |
1049 | # implement "tell service.id help" |
1050 | if len(childargs) >= 3 and childargs[0] == 'tell' and childargs[2] == 'help': | |
1051 | target = childargs[1].split('.') | |
1052 | if validate_target(target): | |
1053 | return do_extended_help(parser, childargs, target, None) | |
1054 | else: | |
1055 | print('target {0} doesn\'t exists, please pass correct target to tell command, such as mon.a/' | |
1056 | 'osd.1/mds.a/mgr'.format(childargs[1]), file=sys.stderr) | |
1057 | return 1 | |
7c673cae FG |
1058 | # implement -w/--watch_* |
1059 | # This is ugly, but Namespace() isn't quite rich enough. | |
1060 | level = '' | |
1061 | for k, v in parsed_args._get_kwargs(): | |
1062 | if k.startswith('watch') and v: | |
1063 | if k == 'watch': | |
1064 | level = 'info' | |
224ce89b | 1065 | elif k != "watch_channel": |
7c673cae FG |
1066 | level = k.replace('watch_', '') |
1067 | if level: | |
7c673cae | 1068 | # an awfully simple callback |
224ce89b WB |
1069 | def watch_cb(arg, line, channel, name, who, stamp_sec, stamp_nsec, seq, level, msg): |
1070 | # Filter on channel | |
11fdf7f2 TL |
1071 | if sys.version_info[0] >= 3: |
1072 | channel = channel.decode('utf-8') | |
224ce89b WB |
1073 | if (channel == parsed_args.watch_channel or \ |
1074 | parsed_args.watch_channel == "*"): | |
11fdf7f2 | 1075 | print(line.decode('utf-8')) |
224ce89b | 1076 | sys.stdout.flush() |
7c673cae FG |
1077 | |
1078 | # first do a ceph status | |
1079 | ret, outbuf, outs = json_command(cluster_handle, prefix='status') | |
7c673cae FG |
1080 | if ret: |
1081 | print("status query failed: ", outs, file=sys.stderr) | |
1082 | return ret | |
11fdf7f2 | 1083 | print(outbuf.decode('utf-8')) |
7c673cae FG |
1084 | |
1085 | # this instance keeps the watch connection alive, but is | |
1086 | # otherwise unused | |
224ce89b | 1087 | run_in_thread(cluster_handle.monitor_log2, level, watch_cb, 0) |
7c673cae FG |
1088 | |
1089 | # loop forever letting watch_cb print lines | |
1090 | try: | |
1091 | signal.pause() | |
1092 | except KeyboardInterrupt: | |
1093 | # or until ^C, at least | |
1094 | return 0 | |
1095 | ||
1096 | # read input file, if any | |
1097 | inbuf = b'' | |
1098 | if parsed_args.input_file: | |
1099 | try: | |
c07f9fc5 FG |
1100 | if parsed_args.input_file == '-': |
1101 | inbuf = sys.stdin.read() | |
1102 | else: | |
1103 | with open(parsed_args.input_file, 'rb') as f: | |
1104 | inbuf = f.read() | |
7c673cae FG |
1105 | except Exception as e: |
1106 | print('Can\'t open input file {0}: {1}'.format(parsed_args.input_file, e), file=sys.stderr) | |
1107 | return 1 | |
1108 | ||
1109 | # prepare output file, if any | |
1110 | if parsed_args.output_file: | |
1111 | try: | |
c07f9fc5 FG |
1112 | if parsed_args.output_file == '-': |
1113 | outf = sys.stdout | |
1114 | else: | |
1115 | outf = open(parsed_args.output_file, 'wb') | |
7c673cae FG |
1116 | except Exception as e: |
1117 | print('Can\'t open output file {0}: {1}'.format(parsed_args.output_file, e), file=sys.stderr) | |
1118 | return 1 | |
a8e16298 TL |
1119 | if parsed_args.setuser: |
1120 | try: | |
1121 | ownerid = pwd.getpwnam(parsed_args.setuser).pw_uid | |
1122 | os.fchown(outf.fileno(), ownerid, -1) | |
1123 | except OSError as e: | |
1124 | print('Failed to change user ownership of {0} to {1}: {2}'.format(outf, parsed_args.setuser, e)) | |
1125 | return 1 | |
1126 | if parsed_args.setgroup: | |
1127 | try: | |
1128 | groupid = grp.getgrnam(parsed_args.setgroup).gr_gid | |
1129 | os.fchown(outf.fileno(), -1, groupid) | |
1130 | except OSError as e: | |
1131 | print('Failed to change group ownership of {0} to {1}: {2}'.format(outf, parsed_args.setgroup, e)) | |
1132 | return 1 | |
7c673cae FG |
1133 | |
1134 | # -s behaves like a command (ceph status). | |
1135 | if parsed_args.status: | |
1136 | childargs.insert(0, 'status') | |
1137 | ||
1138 | try: | |
1139 | target = find_cmd_target(childargs) | |
1140 | except Exception as e: | |
1141 | print('error handling command target: {0}'.format(e), file=sys.stderr) | |
1142 | return 1 | |
1143 | ||
1144 | # Repulsive hack to handle tell: lop off 'tell' and target | |
1145 | # and validate the rest of the command. 'target' is already | |
1146 | # determined in our callers, so it's ok to remove it here. | |
1147 | is_tell = False | |
1148 | if len(childargs) and childargs[0] == 'tell': | |
1149 | childargs = childargs[2:] | |
1150 | is_tell = True | |
1151 | ||
1152 | if is_tell: | |
1153 | if injectargs: | |
1154 | childargs = injectargs | |
1155 | if not len(childargs): | |
1156 | print('"{0} tell" requires additional arguments.'.format(sys.argv[0]), | |
31f18b77 FG |
1157 | 'Try "{0} tell <name> <command> [options...]" instead.'.format(sys.argv[0]), |
1158 | file=sys.stderr) | |
7c673cae FG |
1159 | return errno.EINVAL |
1160 | ||
1161 | # fetch JSON sigs from command | |
1162 | # each line contains one command signature (a placeholder name | |
1163 | # of the form 'cmdNNN' followed by an array of argument descriptors) | |
1164 | # as part of the validated argument JSON object | |
1165 | ||
7c673cae | 1166 | if target[1] == '*': |
181888fb FG |
1167 | service = target[0] |
1168 | targets = [(service, o) for o in ids_by_service(service)] | |
1169 | else: | |
1170 | targets = [target] | |
7c673cae FG |
1171 | |
1172 | final_ret = 0 | |
1173 | for target in targets: | |
1174 | # prettify? prefix output with target, if there was a wildcard used | |
1175 | prefix = '' | |
1176 | suffix = '' | |
1177 | if not parsed_args.output_file and len(targets) > 1: | |
1178 | prefix = '{0}.{1}: '.format(*target) | |
1179 | suffix = '\n' | |
1180 | ||
1181 | ret, outbuf, outs = json_command(cluster_handle, target=target, | |
1182 | prefix='get_command_descriptions') | |
31f18b77 FG |
1183 | if ret: |
1184 | where = '{0}.{1}'.format(*target) | |
1185 | if ret > 0: | |
11fdf7f2 | 1186 | raise RuntimeError('Unexpected return code from {0}: {1}'. |
31f18b77 FG |
1187 | format(where, ret)) |
1188 | outs = 'problem getting command descriptions from {0}'.format(where) | |
1189 | else: | |
1190 | sigdict = parse_json_funcsigs(outbuf.decode('utf-8'), 'cli') | |
7c673cae | 1191 | |
31f18b77 FG |
1192 | if parsed_args.completion: |
1193 | return complete(sigdict, childargs, target) | |
7c673cae | 1194 | |
31f18b77 FG |
1195 | ret, outbuf, outs = new_style_command(parsed_args, childargs, |
1196 | target, sigdict, inbuf, | |
1197 | verbose) | |
7c673cae | 1198 | |
31f18b77 FG |
1199 | # debug tool: send any successful command *again* to |
1200 | # verify that it is idempotent. | |
1201 | if not ret and 'CEPH_CLI_TEST_DUP_COMMAND' in os.environ: | |
1202 | ret, outbuf, outs = new_style_command(parsed_args, childargs, | |
1203 | target, sigdict, inbuf, | |
1204 | verbose) | |
1205 | if ret < 0: | |
1206 | ret = -ret | |
1207 | print(prefix + | |
1208 | 'Second attempt of previously successful command ' | |
1209 | 'failed with {0}: {1}'.format( | |
1210 | errno.errorcode.get(ret, 'Unknown'), outs), | |
1211 | file=sys.stderr) | |
7c673cae FG |
1212 | |
1213 | if ret < 0: | |
1214 | ret = -ret | |
31f18b77 FG |
1215 | errstr = errno.errorcode.get(ret, 'Unknown') |
1216 | print(u'Error {0}: {1}'.format(errstr, outs), file=sys.stderr) | |
7c673cae FG |
1217 | if len(targets) > 1: |
1218 | final_ret = ret | |
1219 | else: | |
1220 | return ret | |
1221 | ||
31f18b77 FG |
1222 | if outs: |
1223 | print(prefix + outs, file=sys.stderr) | |
7c673cae FG |
1224 | |
1225 | sys.stdout.flush() | |
1226 | ||
31f18b77 | 1227 | if parsed_args.output_file: |
7c673cae FG |
1228 | outf.write(outbuf) |
1229 | else: | |
1230 | # hack: old code printed status line before many json outputs | |
1231 | # (osd dump, etc.) that consumers know to ignore. Add blank line | |
1232 | # to satisfy consumers that skip the first line, but not annoy | |
1233 | # consumers that don't. | |
1234 | if parsed_args.output_format and \ | |
31f18b77 | 1235 | parsed_args.output_format.startswith('json'): |
7c673cae FG |
1236 | print() |
1237 | ||
1238 | # if we are prettifying things, normalize newlines. sigh. | |
1239 | if suffix: | |
1240 | outbuf = outbuf.rstrip() | |
1241 | if outbuf: | |
1242 | try: | |
1243 | print(prefix, end='') | |
1244 | # Write directly to binary stdout | |
1245 | raw_write(outbuf) | |
1246 | print(suffix, end='') | |
1247 | except IOError as e: | |
1248 | if e.errno != errno.EPIPE: | |
1249 | raise e | |
1250 | ||
1251 | sys.stdout.flush() | |
1252 | ||
11fdf7f2 TL |
1253 | # Block until command completion (currently scrub and deep_scrub only) |
1254 | if block: | |
1255 | wait(childargs, waitdata) | |
1256 | ||
c07f9fc5 | 1257 | if parsed_args.output_file and parsed_args.output_file != '-': |
7c673cae FG |
1258 | outf.close() |
1259 | ||
1260 | if final_ret: | |
1261 | return final_ret | |
1262 | ||
1263 | return 0 | |
1264 | ||
1265 | if __name__ == '__main__': | |
1266 | retval = main() | |
1267 | # shutdown explicitly; Rados() does not | |
1268 | if cluster_handle: | |
1269 | run_in_thread(cluster_handle.shutdown) | |
1270 | sys.exit(retval) |