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
|
"""Manage DCOS services
Usage:
dcos service --info
dcos service [--completed --inactive --json]
dcos service log [--follow --lines=N --ssh-config-file=<path>]
<service> [<file>]
dcos service shutdown <service-id>
Options:
-h, --help Show this screen
--completed Show completed services in addition to active
ones. Completed services are those that have
been disconnected from master, and have reached
their failover timeout, or have been explicitly
shutdown via the /shutdown endpoint.
--inactive Show inactive services in addition to active
ones. Inactive services are those that have
been disconnected from master, but haven't yet
reached their failover timeout.
--info Show a short description of this subcommand
--follow Print data as the file grows
--json Print json-formatted services
--lines=N Print the last N lines [default: 10]
--ssh-config-file=<path> Path to SSH config file. Used to access
marathon logs.
--version Show version
Positional Arguments:
<file> Output this file. [default: stdout]
<service> The DCOS Service name.
<service-id> The DCOS Service ID
"""
import subprocess
import dcoscli
import docopt
from dcos import cmds, emitting, marathon, mesos, package, util
from dcos.errors import DCOSException, DefaultError
from dcoscli import log, tables
from dcoscli.main import decorate_docopt_usage
logger = util.get_logger(__name__)
emitter = emitting.FlatEmitter()
def main():
try:
return _main()
except DCOSException as e:
emitter.publish(e)
return 1
@decorate_docopt_usage
def _main():
util.configure_process_from_environ()
args = docopt.docopt(
__doc__,
version="dcos-service version {}".format(dcoscli.version))
return cmds.execute(_cmds(), args)
def _cmds():
"""
:returns: All of the supported commands
:rtype: [Command]
"""
return [
cmds.Command(
hierarchy=['service', 'log'],
arg_keys=['--follow', '--lines', '--ssh-config-file', '<service>',
'<file>'],
function=_log),
cmds.Command(
hierarchy=['service', 'shutdown'],
arg_keys=['<service-id>'],
function=_shutdown),
cmds.Command(
hierarchy=['service', '--info'],
arg_keys=[],
function=_info),
cmds.Command(
hierarchy=['service'],
arg_keys=['--inactive', '--completed', '--json'],
function=_service),
]
def _info():
"""Print services cli information.
:returns: process return code
:rtype: int
"""
emitter.publish(__doc__.split('\n')[0])
return 0
def _service(inactive, completed, is_json):
"""List dcos services
:param inactive: If True, include completed tasks
:type inactive: bool
:param is_json: If true, output json.
Otherwise, output a human readable table.
:type is_json: bool
:returns: process return code
:rtype: int
"""
services = mesos.get_master().frameworks(
inactive=inactive,
completed=completed)
if is_json:
emitter.publish([service.dict() for service in services])
else:
table = tables.service_table(services)
output = str(table)
if output:
emitter.publish(output)
return 0
def _shutdown(service_id):
"""Shuts down a service
:param service_id: the id for the service
:type service_id: str
:returns: process return code
:rtype: int
"""
mesos.DCOSClient().shutdown_framework(service_id)
return 0
def _log(follow, lines, ssh_config_file, service, file_):
"""Prints the contents of the logs for a given service. The service
task is located by first identifying the marathon app running a
framework named `service`.
:param follow: same as unix tail's -f
:type follow: bool
:param lines: number of lines to print
:type lines: int
:param ssh_config_file: SSH config file. Used for marathon.
:type ssh_config_file: str | None
:param service: service name
:type service: str
:param file_: file path to read
:type file_: str
:returns: process return code
:rtype: int
"""
lines = util.parse_int(lines)
if service == 'marathon':
if file_:
raise DCOSException('The <file> argument is invalid for marathon.'
' The systemd journal is always used for the'
' marathon log.')
return _log_marathon(follow, lines, ssh_config_file)
else:
if ssh_config_file:
raise DCOSException(
'The `--ssh-config-file` argument is invalid for non-marathon '
'services. SSH is not used.')
return _log_service(follow, lines, service, file_)
def _log_service(follow, lines, service, file_):
"""Prints the contents of the logs for a given service. Used for
non-marathon services.
:param follow: same as unix tail's -f
:type follow: bool
:param lines: number of lines to print
:type lines: int
:param service: service name
:type service: str
:param file_: file path to read
:type file_: str
:returns: process return code
:rtype: int
"""
if file_ is None:
file_ = 'stdout'
task = _get_service_task(service)
return _log_task(task['id'], follow, lines, file_)
def _log_task(task_id, follow, lines, file_):
"""Prints the contents of the logs for a given task ID.
:param task_id: task ID
:type task_id: str
:param follow: same as unix tail's -f
:type follow: bool
:param lines: number of lines to print
:type lines: int
:param file_: file path to read
:type file_: str
:returns: process return code
:rtype: int
"""
dcos_client = mesos.DCOSClient()
task = mesos.get_master(dcos_client).task(task_id)
mesos_file = mesos.MesosFile(file_, task=task, dcos_client=dcos_client)
return log.log_files([mesos_file], follow, lines)
def _get_service_task(service_name):
"""Gets the task running the given service. If there is more than one
such task, throws an exception.
:param service_name: service name
:type service_name: str
:returns: The marathon task dict
:rtype: dict
"""
marathon_client = marathon.create_client()
app = _get_service_app(marathon_client, service_name)
tasks = marathon_client.get_app(app['id'])['tasks']
if len(tasks) != 1:
raise DCOSException(
('We expected marathon app [{}] to be running 1 task, but we ' +
'instead found {} tasks').format(app['id'], len(tasks)))
return tasks[0]
def _get_service_app(marathon_client, service_name):
"""Gets the marathon app running the given service. If there is not
exactly one such app, throws an exception.
:param marathon_client: marathon client
:type marathon_client: marathon.Client
:param service_name: service name
:type service_name: str
:returns: marathon app
:rtype: dict
"""
apps = package.get_apps_for_framework(service_name, marathon_client)
if len(apps) > 1:
raise DCOSException(
'Multiple marathon apps found for service name [{}]: {}'.format(
service_name,
', '.join('[{}]'.format(app['id']) for app in apps)))
elif len(apps) == 0:
raise DCOSException(
'No marathon apps found for service [{}]'.format(service_name))
else:
return apps[0]
def _log_marathon(follow, lines, ssh_config_file):
"""Prints the contents of the marathon logs.
:param follow: same as unix tail's -f
:type follow: bool
:param lines: number of lines to print
:type lines: int
:param ssh_config_file: SSH config file.
:type ssh_config_file: str | None
;:returns: process return code
:rtype: int
"""
ssh_options = util.get_ssh_options(ssh_config_file, [])
journalctl_args = ''
if follow:
journalctl_args += '-f '
if lines:
journalctl_args += '-n {} '.format(lines)
leader_ip = marathon.create_client().get_leader().split(':')[0]
cmd = ("ssh {0}core@{1} " +
"journalctl {2}-u dcos-marathon").format(
ssh_options,
leader_ip,
journalctl_args)
emitter.publish(DefaultError("Running `{}`".format(cmd)))
return subprocess.call(cmd, shell=True)
|