mkconfig.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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):
  16. """Given a manifest.yaml file, a pubkeys.yaml file, and (optionally) a
  17. list of nodes to include, generate the config.json file to give to
  18. each of the nodes. If the list of nodes is missing, include all nodes
  19. in the manifest.yaml file. It is an error to include a node that is
  20. missing from either the manifest.yaml or the pubkeys.yaml files."""
  21. with open(manifestfile) as mf:
  22. manifest = yaml.safe_load(mf)
  23. with open(pubkeysfile) as pf:
  24. pubkeys = yaml.safe_load(pf)
  25. if nodelist is None or len(nodelist) == 0:
  26. nodelist = manifest.keys()
  27. config = {}
  28. if "params" in nodelist:
  29. config['params'] = manifest['params']
  30. config['nodes'] = []
  31. for node in nodelist:
  32. if node == "params":
  33. continue
  34. nodeconf = {}
  35. m = manifest[node]
  36. nodeconf['name'] = node
  37. nodeconf['pubkey'] = pubkeys[node]
  38. nodeconf['listen'] = m['listen']
  39. # Optional fields
  40. for f in ['clisten', 'weight']:
  41. if f in m:
  42. nodeconf[f] = m[f]
  43. config['nodes'].append(nodeconf)
  44. return json.dumps(config)
  45. if __name__ == "__main__":
  46. aparse = argparse.ArgumentParser(
  47. description='Create a TEEMS config.json file from a manifest and a pubkeys file'
  48. )
  49. aparse.add_argument('-m', default=MANIFEST,
  50. help='manifest.yaml file')
  51. aparse.add_argument('-p', default=PUBKEYS,
  52. help='pubkeys.yaml file')
  53. aparse.add_argument('-n', nargs='*', help='nodes to include')
  54. args = aparse.parse_args()
  55. json = create_json(args.m, args.p, args.n)
  56. if json is not None:
  57. print(json)