mkconfig.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/env python3
  2. # Given a manifest.yaml file, a pubkeys.yaml file, and (optionally) a
  3. # list of nodes to include, generate the config.json file to give to
  4. # each of the nodes. If the list of nodes is missing, include all nodes
  5. # in the manifest.yaml file. It is an error to include a node that is
  6. # missing from either the manifest.yaml or the pubkeys.yaml files.
  7. # Usage: mkconfig [-m manifest.yaml] [-p pubkeys.yaml] [-n node1 node2...]
  8. import argparse
  9. import json
  10. import yaml
  11. # The default manifest file
  12. MANIFEST = "manifest.yaml"
  13. # The default pubkeys file
  14. PUBKEYS = "pubkeys.yaml"
  15. def create_json(manifestfile, pubkeysfile, nodelist, params_override):
  16. """Given a manifest.yaml file, a pubkeys.yaml file, and (optionally) a
  17. list of nodes to include and a dictionary of parameter settings to
  18. override, generate the config.json file to give to each of the
  19. nodes. If the list of nodes is missing, include all nodes in the
  20. manifest.yaml file. It is an error to include a node that is
  21. missing from either the manifest.yaml or the pubkeys.yaml files."""
  22. with open(manifestfile) as mf:
  23. manifest = yaml.safe_load(mf)
  24. with open(pubkeysfile) as pf:
  25. pubkeys = yaml.safe_load(pf)
  26. if nodelist is None or len(nodelist) == 0:
  27. nodelist = manifest.keys()
  28. config = {}
  29. if "params" in manifest:
  30. config['params'] = manifest['params']
  31. for ov in params_override:
  32. if params_override[ov] is not None:
  33. config['params'][ov] = params_override[ov]
  34. config['nodes'] = []
  35. for node in nodelist:
  36. if node == "params":
  37. continue
  38. nodeconf = {}
  39. m = manifest[node]
  40. nodeconf['name'] = node
  41. nodeconf['pubkey'] = pubkeys[node]
  42. nodeconf['listen'] = m['listen']
  43. # Optional fields
  44. for f in ['clisten', 'slisten', 'weight', 'roles']:
  45. if f in m:
  46. nodeconf[f] = m[f]
  47. config['nodes'].append(nodeconf)
  48. return json.dumps(config)
  49. if __name__ == "__main__":
  50. aparse = argparse.ArgumentParser(
  51. description='Create a TEEMS config.json file from a manifest and a pubkeys file'
  52. )
  53. aparse.add_argument('-m', default=MANIFEST,
  54. help='manifest.yaml file')
  55. aparse.add_argument('-p', default=PUBKEYS,
  56. help='pubkeys.yaml file')
  57. aparse.add_argument('-z', default=None,
  58. help='override message size')
  59. aparse.add_argument('-u', default=None,
  60. help='override max number of users')
  61. aparse.add_argument('-B', default=None,
  62. help='override max number of outgoing token channel messages per user per epoch')
  63. aparse.add_argument('-b', default=None,
  64. help='override max number of incoming token channel messages per user per epoch')
  65. aparse.add_argument('-C', default=None,
  66. help='override max number of outgoing ID channel messages per user per epoch')
  67. aparse.add_argument('-c', default=None,
  68. help='override max number of incoming ID channel messages per user per epoch')
  69. aparse.add_argument('-n', nargs='*', help='nodes to include')
  70. args = aparse.parse_args()
  71. params_overrides = {
  72. 'msg_size': args.z,
  73. 'user_count': args.u,
  74. 'token_out': args.B,
  75. 'token_in': args.b,
  76. 'id_out': args.C,
  77. 'id_in': args.c,
  78. }
  79. json = create_json(args.m, args.p, args.n, params_overrides)
  80. if json is not None:
  81. print(json)