def send_command(cluster, target=('mon', ''), cmd=None, inbuf=b'', timeout=0, verbose=False): """ Send a command to a daemon using librados's mon_command, osd_command, or pg_command. Any bulk input data comes in inbuf. Returns (ret, outbuf, outs); ret is the return code, outbuf is the outbl "bulk useful output" buffer, and outs is any status or error message (intended for stderr). If target is osd.N, send command to that osd (except for pgid cmds) """ cmd = cmd or [] try: if target[0] == 'osd': osdid = target[1] if verbose: print('submit {0} to osd.{1}'.format(cmd, osdid), file=sys.stderr) ret, outbuf, outs = run_in_thread(cluster.osd_command, osdid, cmd, inbuf, timeout) elif target[0] == 'mgr': ret, outbuf, outs = run_in_thread(cluster.mgr_command, cmd, inbuf, timeout) elif target[0] == 'pg': pgid = target[1] # pgid will already be in the command for the pg <pgid> # form, but for tell <pgid>, we need to put it in if cmd: cmddict = json.loads(cmd[0]) cmddict['pgid'] = pgid else: cmddict = dict(pgid=pgid) cmd = [json.dumps(cmddict)] if verbose: print('submit {0} for pgid {1}'.format(cmd, pgid), file=sys.stderr) ret, outbuf, outs = run_in_thread(cluster.pg_command, pgid, cmd, inbuf, timeout) elif target[0] == 'mon': if verbose: print('{0} to {1}'.format(cmd, target[0]), file=sys.stderr) if len(target) < 2 or target[1] == '': ret, outbuf, outs = run_in_thread(cluster.mon_command, cmd, inbuf, timeout) else: ret, outbuf, outs = run_in_thread(cluster.mon_command, cmd, inbuf, timeout, target[1]) elif target[0] == 'mds': mds_spec = target[1] if verbose: print('submit {0} to mds.{1}'.format(cmd, mds_spec), file=sys.stderr) try: from cephfs import LibCephFS except ImportError: raise RuntimeError( "CephFS unavailable, have you installed libcephfs?") filesystem = LibCephFS(cluster.conf_defaults, cluster.conffile) filesystem.conf_parse_argv(cluster.parsed_args) filesystem.init() ret, outbuf, outs = \ filesystem.mds_command(mds_spec, cmd, inbuf) filesystem.shutdown() else: raise ArgumentValid("Bad target type '{0}'".format(target[0])) except Exception as e: if not isinstance(e, ArgumentError): raise RuntimeError('"{0}": exception {1}'.format(cmd, e)) else: raise return ret, outbuf, outs
def send_command(cluster, target: Optional[Tuple[str, str]] = ('mon', ''), cmd: Optional[List[str]] = None, inbuf: Optional[bytes] = b'', timeout: Optional[int] = 0, verbose: Optional[bool] = False) -> Tuple[int, bytes, str]: """ Send a command to a daemon using librados's mon_command, osd_command, mgr_command, or pg_command. Any bulk input data comes in inbuf. Returns (ret, outbuf, outs); ret is the return code, outbuf is the outbl "bulk useful output" buffer, and outs is any status or error message (intended for stderr). If target is osd.N, send command to that osd (except for pgid cmds) """ cmd = cmd or [] try: if target[0] == 'osd': osdid = target[1] if verbose: print('submit {0} to osd.{1}'.format(cmd, osdid), file=sys.stderr) ret, outbuf, outs = run_in_thread( cluster.osd_command, osdid, cmd, inbuf, timeout=timeout) elif target[0] == 'mgr': name = '' # non-None empty string means "current active mgr" if len(target) > 1 and target[1] is not None: name = target[1] if verbose: print('submit {0} to {1} name {2}'.format(cmd, target[0], name), file=sys.stderr) ret, outbuf, outs = run_in_thread( cluster.mgr_command, cmd, inbuf, timeout=timeout, target=name) elif target[0] == 'mon-mgr': if verbose: print('submit {0} to {1}'.format(cmd, target[0]), file=sys.stderr) ret, outbuf, outs = run_in_thread( cluster.mgr_command, cmd, inbuf, timeout=timeout) elif target[0] == 'pg': pgid = target[1] # pgid will already be in the command for the pg <pgid> # form, but for tell <pgid>, we need to put it in if cmd: cmddict = json.loads(cmd) cmddict['pgid'] = pgid else: cmddict = dict(pgid=pgid) cmd = json.dumps(cmddict) if verbose: print('submit {0} for pgid {1}'.format(cmd, pgid), file=sys.stderr) ret, outbuf, outs = run_in_thread( cluster.pg_command, pgid, cmd, inbuf, timeout=timeout) elif target[0] == 'mon': if verbose: print('{0} to {1}'.format(cmd, target[0]), file=sys.stderr) if len(target) < 2 or target[1] == '': ret, outbuf, outs = run_in_thread( cluster.mon_command, cmd, inbuf, timeout=timeout) else: ret, outbuf, outs = run_in_thread( cluster.mon_command, cmd, inbuf, timeout=timeout, target=target[1]) elif target[0] == 'mds': mds_spec = target[1] if verbose: print('submit {0} to mds.{1}'.format(cmd, mds_spec), file=sys.stderr) try: from cephfs import LibCephFS except ImportError: raise RuntimeError("CephFS unavailable, have you installed libcephfs?") filesystem = LibCephFS(rados_inst=cluster) filesystem.init() ret, outbuf, outs = \ filesystem.mds_command(mds_spec, cmd, inbuf) filesystem.shutdown() else: raise ArgumentValid("Bad target type '{0}'".format(target[0])) except Exception as e: if not isinstance(e, ArgumentError): raise RuntimeError('"{0}": exception {1}'.format(cmd, e)) else: raise return ret, outbuf, outs
def send_command(cluster, target=('mon', ''), cmd=None, inbuf=b'', timeout=0, verbose=False): """ Send a command to a daemon using librados's mon_command, osd_command, mgr_command, or pg_command. Any bulk input data comes in inbuf. Returns (ret, outbuf, outs); ret is the return code, outbuf is the outbl "bulk useful output" buffer, and outs is any status or error message (intended for stderr). If target is osd.N, send command to that osd (except for pgid cmds) """ cmd = cmd or [] try: if target[0] == 'osd': osdid = target[1] if verbose: print('submit {0} to osd.{1}'.format(cmd, osdid), file=sys.stderr) ret, outbuf, outs = run_in_thread( cluster.osd_command, osdid, cmd, inbuf, timeout) elif target[0] == 'mgr': ret, outbuf, outs = run_in_thread( cluster.mgr_command, cmd, inbuf, timeout) elif target[0] == 'pg': pgid = target[1] # pgid will already be in the command for the pg <pgid> # form, but for tell <pgid>, we need to put it in if cmd: cmddict = json.loads(cmd[0]) cmddict['pgid'] = pgid else: cmddict = dict(pgid=pgid) cmd = [json.dumps(cmddict)] if verbose: print('submit {0} for pgid {1}'.format(cmd, pgid), file=sys.stderr) ret, outbuf, outs = run_in_thread( cluster.pg_command, pgid, cmd, inbuf, timeout) elif target[0] == 'mon': if verbose: print('{0} to {1}'.format(cmd, target[0]), file=sys.stderr) if len(target) < 2 or target[1] == '': ret, outbuf, outs = run_in_thread( cluster.mon_command, cmd, inbuf, timeout) else: ret, outbuf, outs = run_in_thread( cluster.mon_command, cmd, inbuf, timeout, target[1]) elif target[0] == 'mds': mds_spec = target[1] if verbose: print('submit {0} to mds.{1}'.format(cmd, mds_spec), file=sys.stderr) try: from cephfs import LibCephFS except ImportError: raise RuntimeError("CephFS unavailable, have you installed libcephfs?") filesystem = LibCephFS(rados_inst=cluster) filesystem.init() ret, outbuf, outs = \ filesystem.mds_command(mds_spec, cmd, inbuf) filesystem.shutdown() else: raise ArgumentValid("Bad target type '{0}'".format(target[0])) except Exception as e: if not isinstance(e, ArgumentError): raise RuntimeError('"{0}": exception {1}'.format(cmd, e)) else: raise return ret, outbuf, outs
def send_command(cluster, target=("mon", ""), cmd=None, inbuf="", timeout=0, verbose=False): """ Send a command to a daemon using librados's mon_command, osd_command, or pg_command. Any bulk input data comes in inbuf. Returns (ret, outbuf, outs); ret is the return code, outbuf is the outbl "bulk useful output" buffer, and outs is any status or error message (intended for stderr). If target is osd.N, send command to that osd (except for pgid cmds) """ cmd = cmd or [] try: if target[0] == "osd": osdid = target[1] if verbose: print >>sys.stderr, "submit {0} to osd.{1}".format(cmd, osdid) ret, outbuf, outs = cluster.osd_command(osdid, cmd, inbuf, timeout) elif target[0] == "pg": pgid = target[1] # pgid will already be in the command for the pg <pgid> # form, but for tell <pgid>, we need to put it in if cmd: cmddict = json.loads(cmd[0]) cmddict["pgid"] = pgid else: cmddict = dict(pgid=pgid) cmd = [json.dumps(cmddict)] if verbose: print >>sys.stderr, "submit {0} for pgid {1}".format(cmd, pgid) ret, outbuf, outs = cluster.pg_command(pgid, cmd, inbuf, timeout) elif target[0] == "mon": if verbose: print >>sys.stderr, "{0} to {1}".format(cmd, target[0]) if target[1] == "": ret, outbuf, outs = cluster.mon_command(cmd, inbuf, timeout) else: ret, outbuf, outs = cluster.mon_command(cmd, inbuf, timeout, target[1]) elif target[0] == "mds": mds_spec = target[1] if verbose: print >>sys.stderr, "submit {0} to mds.{1}".format(cmd, mds_spec) try: from cephfs import LibCephFS except ImportError: raise RuntimeError("CephFS unavailable, have you installed libcephfs?") filesystem = LibCephFS(cluster.conf_defaults, cluster.conffile) filesystem.conf_parse_argv(cluster.parsed_args) filesystem.init() ret, outbuf, outs = filesystem.mds_command(mds_spec, cmd, inbuf) filesystem.shutdown() else: raise ArgumentValid("Bad target type '{0}'".format(target[0])) except Exception as e: if not isinstance(e, ArgumentError): raise RuntimeError('"{0}": exception {1}'.format(cmd, e)) else: raise return ret, outbuf, outs