coin_daemon_control.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. -W, --no-wait Don't wait for daemons to change state before exiting
  24. """,
  25. 'notes': """
  26. Valid network IDs: {nid}, all, or no_xmr
  27. """
  28. },
  29. 'code': {
  30. 'options': lambda s: s.format(a=action.capitalize(),pn=g.prog_name),
  31. 'notes': lambda s,help_notes: s.format(nid=help_notes('coin_daemon_network_ids'))
  32. }
  33. }
  34. cmd_args = opts.init(opts_data)
  35. from mmgen.daemon import *
  36. def run(network_id=None,proto=None,daemon_id=None):
  37. d = CoinDaemon(
  38. network_id = network_id,
  39. proto = proto,
  40. test_suite = not opt.usermode,
  41. opts = ['no_daemonize'] if opt.no_daemonize else None,
  42. port_shift = int(opt.port_shift or 0),
  43. datadir = opt.datadir,
  44. daemon_id = daemon_id )
  45. d.debug = d.debug or opt.debug
  46. d.wait = not opt.no_wait
  47. if opt.get_state:
  48. print(d.state_msg())
  49. elif opt.testing:
  50. for cmd in d.start_cmds if action == 'start' else [d.stop_cmd]:
  51. print(' '.join(cmd))
  52. else:
  53. if action == 'stop' and hasattr(d,'rpc'):
  54. run_session(d.rpc.stop_daemon(quiet=opt.quiet))
  55. else:
  56. d.cmd(action,quiet=opt.quiet)
  57. if opt.daemon_ids:
  58. print('\n'.join(CoinDaemon.all_daemon_ids()))
  59. elif 'all' in cmd_args or 'no_xmr' in cmd_args:
  60. if len(cmd_args) != 1:
  61. die(1,"'all' or 'no_xmr' must be the sole argument")
  62. from mmgen.protocol import init_proto
  63. import mmgen.daemon as daemon_mod
  64. for coin,data in CoinDaemon.coins.items():
  65. if coin == 'XMR' and cmd_args[0] == 'no_xmr':
  66. continue
  67. for daemon_id in data.daemon_ids:
  68. for network in getattr( daemon_mod, daemon_id+'_daemon' ).networks:
  69. run(proto=init_proto(coin=coin,network=network),daemon_id=daemon_id)
  70. else:
  71. ids = cmd_args
  72. network_ids = CoinDaemon.get_network_ids()
  73. if not ids:
  74. opts.usage()
  75. for i in ids:
  76. if i not in network_ids:
  77. die(1,f'{i!r}: invalid network ID')
  78. for network_id in ids:
  79. run(network_id=network_id.lower())