coin_daemon_control.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/usr/bin/env python3
  2. from .tests_header import repo_root
  3. from mmgen.common import *
  4. action = g.prog_name.split('-')[0]
  5. opts_data = {
  6. 'sets': [('debug',True,'verbose',True)],
  7. 'text': {
  8. 'desc': '{} coin daemons for the MMGen test suite'.format(action.capitalize()),
  9. 'usage':'[opts] <network IDs>',
  10. 'options': """
  11. -h, --help Print this help message
  12. --, --longhelp Print help message for long options (common options)
  13. -D, --debug Produce debugging output (implies --verbose)
  14. -d, --datadir= Override the default datadir
  15. -n, --no-daemonize Don't fork daemon to background
  16. -p, --port-shift= Shift the RPC port by this number
  17. -s, --get-state Get the state of the daemon(s) and exit
  18. -t, --testing Testing mode. Print commands but don't execute them
  19. -q, --quiet Produce quieter output
  20. -u, --usermode Run the daemon in user (non test-suite) mode
  21. -v, --verbose Produce more verbose output
  22. -W, --no-wait Don't wait for daemons to change state before exiting
  23. """,
  24. 'notes': """
  25. Valid network IDs: {nid}, all, or no_xmr
  26. """
  27. },
  28. 'code': {
  29. 'options': lambda s: s.format(a=action.capitalize(),pn=g.prog_name),
  30. 'notes': lambda s,help_notes: s.format(nid=help_notes('coin_daemon_network_ids'))
  31. }
  32. }
  33. cmd_args = opts.init(opts_data)
  34. from mmgen.daemon import *
  35. def run(network_id=None,proto=None,daemon_id=None):
  36. d = CoinDaemon(
  37. network_id = network_id,
  38. proto = proto,
  39. test_suite = not opt.usermode,
  40. opts = ['no_daemonize'] if opt.no_daemonize else None,
  41. port_shift = int(opt.port_shift or 0),
  42. datadir = opt.datadir,
  43. daemon_id = daemon_id )
  44. d.debug = d.debug or opt.debug
  45. d.wait = not opt.no_wait
  46. if opt.get_state:
  47. print(d.state_msg())
  48. elif opt.testing:
  49. for cmd in d.start_cmds if action == 'start' else [d.stop_cmd]:
  50. print(' '.join(cmd))
  51. else:
  52. d.cmd(action,quiet=opt.quiet)
  53. if 'all' in cmd_args or 'no_xmr' in cmd_args:
  54. if len(cmd_args) != 1:
  55. die(1,"'all' or 'no_xmr' must be the sole argument")
  56. from mmgen.protocol import init_proto
  57. for coin,data in CoinDaemon.coins.items():
  58. if coin == 'XMR' and cmd_args[0] == 'no_xmr':
  59. continue
  60. for daemon_id in data.daemon_ids:
  61. for network in globals()[daemon_id+'_daemon'].networks:
  62. run(proto=init_proto(coin=coin,network=network),daemon_id=daemon_id)
  63. else:
  64. ids = cmd_args
  65. network_ids = CoinDaemon.get_network_ids()
  66. if not ids:
  67. opts.usage()
  68. for i in ids:
  69. if i not in network_ids:
  70. die(1,f'{i!r}: invalid network ID')
  71. for network_id in ids:
  72. run(network_id=network_id.lower())