107 lines
2.3 KiB
Python
107 lines
2.3 KiB
Python
"""Get the status of DCOS tasks
|
|
|
|
Usage:
|
|
dcos task --info
|
|
dcos task [--completed --json <task>]
|
|
|
|
Options:
|
|
-h, --help Show this screen
|
|
--info Show a short description of this subcommand
|
|
--json Print json-formatted tasks
|
|
--completed Show completed tasks as well
|
|
--version Show version
|
|
|
|
Positional Arguments:
|
|
|
|
<task> Only match tasks whose ID matches <task>. <task> may be
|
|
a substring of the ID, or a unix glob pattern.
|
|
"""
|
|
|
|
import dcoscli
|
|
import docopt
|
|
from dcos import cmds, emitting, mesos, util
|
|
from dcos.errors import DCOSException
|
|
from dcoscli import tables
|
|
|
|
logger = util.get_logger(__name__)
|
|
emitter = emitting.FlatEmitter()
|
|
|
|
|
|
def main():
|
|
try:
|
|
return _main()
|
|
except DCOSException as e:
|
|
emitter.publish(e)
|
|
return 1
|
|
|
|
|
|
def _main():
|
|
util.configure_logger_from_environ()
|
|
|
|
args = docopt.docopt(
|
|
__doc__,
|
|
version="dcos-task version {}".format(dcoscli.version))
|
|
|
|
return cmds.execute(_cmds(), args)
|
|
|
|
|
|
def _cmds():
|
|
"""
|
|
:returns: All of the supported commands
|
|
:rtype: [Command]
|
|
"""
|
|
|
|
return [
|
|
cmds.Command(
|
|
hierarchy=['task', '--info'],
|
|
arg_keys=[],
|
|
function=_info),
|
|
|
|
cmds.Command(
|
|
hierarchy=['task'],
|
|
arg_keys=['<task>', '--completed', '--json'],
|
|
function=_task),
|
|
]
|
|
|
|
|
|
def _info():
|
|
"""Print task cli information.
|
|
|
|
:returns: process return code
|
|
:rtype: int
|
|
"""
|
|
|
|
emitter.publish(__doc__.split('\n')[0])
|
|
return 0
|
|
|
|
|
|
def _task(fltr, completed, json_):
|
|
"""List DCOS tasks
|
|
|
|
:param fltr: task id filter
|
|
:type fltr: str
|
|
:param completed: If True, include completed tasks
|
|
:type completed: bool
|
|
:param json_: If True, output json. Otherwise, output a human
|
|
readable table.
|
|
:type json_: bool
|
|
:returns: process return code
|
|
|
|
"""
|
|
|
|
if fltr is None:
|
|
fltr = ""
|
|
|
|
tasks = sorted(mesos.get_master().tasks(completed=completed, fltr=fltr),
|
|
key=lambda task: task['name'])
|
|
|
|
if json_:
|
|
emitter.publish([task.dict() for task in tasks])
|
|
else:
|
|
table = tables.task_table(tasks)
|
|
output = str(table)
|
|
if output:
|
|
emitter.publish(output)
|
|
|
|
return 0
|