coin_daemon_control.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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': f'{action.capitalize()} coin daemons for the MMGen test suite',
  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. if action == 'stop' and hasattr(d,'rpc'):
  53. run_session(d.rpc.stop_daemon(quiet=opt.quiet))
  54. else:
  55. d.cmd(action,quiet=opt.quiet)
  56. if 'all' in cmd_args or 'no_xmr' in cmd_args:
  57. if len(cmd_args) != 1:
  58. die(1,"'all' or 'no_xmr' must be the sole argument")
  59. from mmgen.protocol import init_proto
  60. for coin,data in CoinDaemon.coins.items():
  61. if coin == 'XMR' and cmd_args[0] == 'no_xmr':
  62. continue
  63. for daemon_id in data.daemon_ids:
  64. for network in globals()[daemon_id+'_daemon'].networks:
  65. run(proto=init_proto(coin=coin,network=network),daemon_id=daemon_id)
  66. else:
  67. ids = cmd_args
  68. network_ids = CoinDaemon.get_network_ids()
  69. if not ids:
  70. opts.usage()
  71. for i in ids:
  72. if i not in network_ids:
  73. die(1,f'{i!r}: invalid network ID')
  74. for network_id in ids:
  75. run(network_id=network_id.lower())