json_modify.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/usr/bin/python
  2. import json
  3. from ansible.module_utils.basic import AnsibleModule
  4. def main():
  5. fields = {
  6. "chain_spec_path": {"required": True, "type": "str"},
  7. "file_content": {"required": False, "type": "str" },
  8. "prefix": {"required": False, "type": "str" },
  9. "all_nodes": {"required": False, "type": "dict" }
  10. }
  11. module = AnsibleModule(argument_spec=fields)
  12. prefix = module.params["prefix"]
  13. chain_spec_path = module.params["chain_spec_path"]
  14. all_nodes = module.params["all_nodes"]
  15. with open(chain_spec_path) as f:
  16. data = json.load(f)
  17. response = {
  18. "name": f'{data["name"]} {prefix}',
  19. "id": f'{data["id"]}_{prefix}',
  20. "protocolId": f'{data["protocolId"]}{prefix}'
  21. }
  22. boot_node_list = data["bootNodes"]
  23. for key in all_nodes:
  24. if "validators" in all_nodes[key]["group_names"]:
  25. public_key = all_nodes[key]["subkey_output"]["stderr"]
  26. boot_node_list.append(f"/ip4/{key}/tcp/30333/p2p/{public_key}")
  27. telemetry_endpoints = data["telemetryEndpoints"]
  28. telemetry_endpoints.append([
  29. "/dns/telemetry.joystream.org/tcp/443/x-parity-wss/%2Fsubmit%2F", 0])
  30. response["bootNodes"] = boot_node_list
  31. response["telemetryEndpoints"] = telemetry_endpoints
  32. data.update(response)
  33. with open(chain_spec_path, 'w') as outfile:
  34. json.dump(data, outfile, indent=4)
  35. module.exit_json(changed=False, result=response)
  36. if __name__ == '__main__':
  37. main()