145
149
except Exception as e:
146
print("Could not remove corrupted Crossbar.io PID file {} - {}".format(fp, e))
150
log.info("Could not remove corrupted Crossbar.io PID file {} - {}".format(fp, e))
148
print("Corrupted Crossbar.io PID file {} removed".format(fp))
152
log.info("Corrupted Crossbar.io PID file {} removed".format(fp))
150
154
if sys.platform == 'win32' and not _HAS_PSUTIL:
151
155
# when on Windows, and we can't actually determine if the PID exists,
162
166
nicecmdline = ' '.join(cmdline)
163
167
if len(nicecmdline) > 76:
164
168
nicecmdline = nicecmdline[:38] + ' ... ' + nicecmdline[-38:]
165
print('"{}" points to PID {} which is not a crossbar process:'.format(fp, pid))
166
print(' ' + nicecmdline)
167
print('Verify manually and either kill {} or delete {}'.format(pid, fp))
169
log.info('"{}" points to PID {} which is not a crossbar process:'.format(fp, pid))
170
log.info(' ' + nicecmdline)
171
log.info('Verify manually and either kill {} or delete {}'.format(pid, fp))
173
177
except Exception as e:
174
print("Could not remove stale Crossbar.io PID file {} (pointing to non-existing process with PID {}) - {}".format(fp, pid, e))
178
log.info("Could not remove stale Crossbar.io PID file {} (pointing to non-existing process with PID {}) - {}".format(fp, pid, e))
176
print("Stale Crossbar.io PID file {} (pointing to non-existing process with PID {}) removed".format(fp, pid))
180
log.info("Stale Crossbar.io PID file {} (pointing to non-existing process with PID {}) removed".format(fp, pid))
180
def run_command_version(options):
184
def run_command_version(options, **kwargs):
182
186
Subcommand "crossbar version".
281
def run_command_init(options):
285
def run_command_init(options, **kwargs):
283
287
Subcommand "crossbar init".
285
291
from crossbar.controller.template import Templates
287
293
templates = Templates()
289
295
if options.template not in templates:
290
print("Huh, sorry. There is no template named '{}'. Try 'crossbar templates' to list the templates available.".format(options.template))
296
log.info("Huh, sorry. There is no template named '{options.template}'. Try 'crossbar templates' to list the templates available.",
293
300
if options.appdir is None:
301
308
except Exception as e:
302
309
raise Exception("could not create application directory '{}' ({})".format(options.appdir, e))
304
print("Crossbar.io application directory '{}' created".format(options.appdir))
311
log.info("Crossbar.io application directory '{options.appdir}' created",
306
314
options.appdir = os.path.abspath(options.appdir)
308
print("Initializing application template '{}' in directory '{}'".format(options.template, options.appdir))
316
log.info("Initializing application template '{options.template}' in directory '{options.appdir}'",
309
318
get_started_hint = templates.init(options.appdir, options.template)
312
# templates.init(options.appdir, options.template)
313
# except Exception as e:
315
# shutil.rmtree(options.appdir)
320
log.info("Application template initialized")
320
print("Application template initialized")
321
322
if get_started_hint:
322
print("\n{}\n".format(get_started_hint))
323
log.info("\n{}\n".format(get_started_hint))
324
print("\nTo start your node, run 'crossbar start --cbdir {}'\n".format(os.path.abspath(os.path.join(options.appdir, '.crossbar'))))
327
def run_command_status(options):
325
log.info("\nTo start your node, run 'crossbar start --cbdir {cbdir}'\n",
326
cbdir=os.path.abspath(os.path.join(options.appdir, '.crossbar')))
329
def run_command_status(options, **kwargs):
329
331
Subcommand "crossbar status".
331
335
# check if there is a Crossbar.io instance currently running from
332
336
# the Crossbar.io node directory at all
335
339
if pid_data is None:
336
340
# https://docs.python.org/2/library/os.html#os.EX_UNAVAILABLE
337
341
# https://www.freebsd.org/cgi/man.cgi?query=sysexits&sektion=3
338
print("No Crossbar.io instance is currently running from node directory {}.".format(options.cbdir))
342
log.info("No Crossbar.io instance is currently running from node directory {cbdir}.",
339
344
sys.exit(getattr(os, 'EX_UNAVAILABLE', 1))
341
print("A Crossbar.io instance is running from node directory {} (PID {}).".format(options.cbdir, pid_data['pid']))
346
log.info("A Crossbar.io instance is running from node directory {cbdir} (PID {pid}).",
347
cbdir=options.cbdir, pid=pid_data['pid'])
345
def run_command_stop(options, exit=True):
351
def run_command_stop(options, exit=True, **kwargs):
347
353
Subcommand "crossbar stop".
379
385
sys.exit(getattr(os, 'EX_UNAVAILABLE', 1))
382
def run_command_start(options):
388
def _startlog(options):
390
Start the logging in a way that all the subcommands can use it.
392
from crossbar._logging import log_publisher, start_logging
393
from crossbar._logging import set_global_log_level
395
loglevel = getattr(options, "loglevel", "info")
396
logformat = getattr(options, "logformat", "none")
398
set_global_log_level(loglevel)
400
if getattr(options, "logtofile", False):
401
# We want to log to a file
402
from crossbar._logging import make_logfile_observer
404
if not options.logdir:
405
logdir = options.cbdir
407
logdir = options.logdir
409
logfile = os.path.join(logdir, "node.log")
411
if loglevel in ["error", "warn", "info"]:
416
log_publisher.addObserver(make_logfile_observer(logfile, show_source))
418
# We want to log to stdout/stderr.
419
from crossbar._logging import make_stdout_observer
420
from crossbar._logging import make_stderr_observer
422
if loglevel == "none":
425
elif loglevel in ["error", "warn", "info"]:
426
# Print info to stdout, warn+ to stderr
427
log_publisher.addObserver(make_stdout_observer(show_source=False,
429
log_publisher.addObserver(make_stderr_observer(show_source=False,
431
elif loglevel == "debug":
432
# Print debug+info to stdout, warn+ to stderr, with the class
434
log_publisher.addObserver(make_stdout_observer(show_source=True,
436
log_publisher.addObserver(make_stderr_observer(show_source=True,
438
elif loglevel == "trace":
439
# Print trace+, with the class source
440
log_publisher.addObserver(make_stdout_observer(show_source=True,
443
log_publisher.addObserver(make_stderr_observer(show_source=True,
446
assert False, "Shouldn't ever get here."
448
# Actually start the logger.
452
def run_command_start(options, reactor=None):
384
454
Subcommand "crossbar start".
395
465
with open(fp, 'w') as fd:
396
466
argv = options.argv
397
467
options_dump = vars(options)
398
del options_dump['func']
399
del options_dump['argv']
401
469
'pid': os.getpid(),
403
'options': options_dump
471
'options': {x: y for x, y in options_dump.items()
472
if x not in ["func", "argv"]}
405
474
fd.write("{}\n".format(json.dumps(pid_data, sort_keys=False, indent=3, separators=(',', ': '))))
407
# we use an Autobahn utility to import the "best" available Twisted reactor
409
reactor = install_reactor(options.reactor, options.debug)
477
# we use an Autobahn utility to import the "best" available Twisted reactor
479
reactor = install_reactor(options.reactor, options.debug)
411
481
# remove node PID file when reactor exits
417
487
reactor.addSystemEventTrigger('after', 'shutdown', remove_pid_file)
419
# start Twisted logging
421
from crossbar._logging import log_publisher, make_logger
422
from crossbar._logging import start_logging, set_global_log_level
424
set_global_log_level(options.loglevel)
426
489
log = make_logger()
428
if options.logtofile:
429
# We want to log to a file
430
from crossbar._logging import make_legacy_daily_logfile_observer
432
if not options.logdir:
433
logdir = options.cbdir
435
logdir = options.logdir
437
log_publisher.addObserver(
438
make_legacy_daily_logfile_observer(logdir))
440
# We want to log to stdout/stderr.
441
from crossbar._logging import make_stdout_observer
442
from crossbar._logging import make_stderr_observer
444
if options.loglevel == "none":
447
elif options.loglevel in ["error", "warn", "info"]:
448
# Print info to stdout, warn+ to stderr
449
log_publisher.addObserver(make_stdout_observer(show_source=False, format=options.logformat))
450
log_publisher.addObserver(make_stderr_observer(show_source=False, format=options.logformat))
451
elif options.loglevel == "debug":
452
# Print debug+info to stdout, warn+ to stderr, with the class
454
log_publisher.addObserver(make_stdout_observer(show_source=True, format=options.logformat))
455
log_publisher.addObserver(make_stderr_observer(show_source=True, format=options.logformat))
456
elif options.loglevel == "trace":
457
# Print trace+, with the class source
458
log_publisher.addObserver(make_stdout_observer(show_source=True, format=options.logformat, trace=True))
459
log_publisher.addObserver(make_stderr_observer(show_source=True, format=options.logformat))
461
assert False, "Shouldn't ever get here."
463
# Actually start the logger.
466
492
for line in BANNER.splitlines():
467
493
log.info(click.style(("{:>40}").format(line), fg='yellow', bold=True))