coin_daemon_control.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. -i, --daemon-ids Print all known daemon IDs
  16. -n, --no-daemonize Don't fork daemon to background
  17. -p, --port-shift= Shift the RPC port by this number
  18. -s, --get-state Get the state of the daemon(s) and exit
  19. -t, --testing Testing mode. Print commands but don't execute them
  20. -q, --quiet Produce quieter output
  21. -u, --usermode Run the daemon in user (non test-suite) mode
  22. -v, --verbose Produce more verbose output
  23. -V, --print-version Print version strings from exec’ed daemons (not RPC)
  24. -W, --no-wait Don't wait for daemons to change state before exiting
  25. """,
  26. 'notes': """
  27. Valid network IDs: {nid}, all, or no_xmr
  28. """
  29. },
  30. 'code': {
  31. 'options': lambda s: s.format(a=action.capitalize(),pn=g.prog_name),
  32. 'notes': lambda s,help_notes: s.format(nid=help_notes('coin_daemon_network_ids'))
  33. }
  34. }
  35. cmd_args = opts.init(opts_data)
  36. from mmgen.daemon import *
  37. def run(network_id=None,proto=None,daemon_id=None):
  38. d = CoinDaemon(
  39. network_id = network_id,
  40. proto = proto,
  41. test_suite = not opt.usermode,
  42. opts = ['no_daemonize'] if opt.no_daemonize else None,
  43. port_shift = int(opt.port_shift or 0),
  44. datadir = opt.datadir,
  45. daemon_id = daemon_id )
  46. d.debug = d.debug or opt.debug
  47. d.wait = not opt.no_wait
  48. if opt.print_version:
  49. msg('{:16} {}'.format( d.exec_fn+':', d.get_exec_version_str() ))
  50. elif opt.get_state:
  51. print(d.state_msg())
  52. elif opt.testing:
  53. for cmd in d.start_cmds if action == 'start' else [d.stop_cmd]:
  54. print(' '.join(cmd))
  55. else:
  56. if action == 'stop' and hasattr(d,'rpc'):
  57. run_session(d.rpc.stop_daemon(quiet=opt.quiet))
  58. else:
  59. d.cmd(action,quiet=opt.quiet)
  60. if opt.daemon_ids:
  61. print('\n'.join(CoinDaemon.all_daemon_ids()))
  62. elif 'all' in cmd_args or 'no_xmr' in cmd_args:
  63. if len(cmd_args) != 1:
  64. die(1,"'all' or 'no_xmr' must be the sole argument")
  65. from mmgen.protocol import init_proto
  66. import mmgen.daemon as daemon_mod
  67. for coin,data in CoinDaemon.coins.items():
  68. if coin == 'XMR' and cmd_args[0] == 'no_xmr':
  69. continue
  70. for daemon_id in data.daemon_ids:
  71. for network in getattr( daemon_mod, daemon_id+'_daemon' ).networks:
  72. run(proto=init_proto(coin=coin,network=network),daemon_id=daemon_id)
  73. else:
  74. ids = cmd_args
  75. network_ids = CoinDaemon.get_network_ids()
  76. if not ids:
  77. opts.usage()
  78. for i in ids:
  79. if i not in network_ids:
  80. die(1,f'{i!r}: invalid network ID')
  81. for network_id in ids:
  82. run(network_id=network_id.lower())