No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bootstrap_client.py 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/usr/bin/python
  2. #coding: utf-8
  3. import os
  4. import subprocess
  5. import requests
  6. from time import sleep
  7. BOOTSTRAP_SERVER_ADDRESS = '10.10.10.10:5555'
  8. def retrieve_config():
  9. my_json_config = {"verdict":"oops"}
  10. my_ip = str(subprocess.check_output("echo $MY_IP", shell=True)).rstrip()
  11. print("MY_IP : %s"%(my_ip))
  12. while my_json_config["verdict"] != "yes":
  13. try:
  14. resp = requests.get("http://%s/getmyconfig/%s"%(BOOTSTRAP_SERVER_ADDRESS, my_ip), timeout=2)
  15. except:
  16. print("Unable to join the bootstrap server")
  17. try:
  18. my_json_config = resp.json()
  19. print("Extracted configs [succ] : %s"%(my_json_config))
  20. except:
  21. print("Unable to extract configs from bootstrap server's answer")
  22. print("Extracted configs [fail] : %s"%(my_json_config))
  23. print("request url : ==>%s<=="%("http://%s/getmyconfig/%s"%(BOOTSTRAP_SERVER_ADDRESS, my_ip)))
  24. if "verdict" in my_json_config and my_json_config["verdict"] == "yes":
  25. my_config = config_json_to_string(my_json_config)
  26. subprocess.check_output("node /mydir/*.js %s"%(my_config), shell=True)
  27. else:
  28. my_json_config = {"verdict":"oops"}
  29. sleep(3)
  30. def config_json_to_string(json_config):
  31. config = ""
  32. if "local_ip" in json_config:
  33. config += "--local_ip "+json_config["local_ip"]+" "
  34. if "local_port" in json_config:
  35. config += "--local_port "+str(json_config["local_port"])+" "
  36. if "local_name" in json_config:
  37. config += "--local_name "+json_config["local_name"]+" "
  38. if "remote_ip" in json_config:
  39. config += "--remote_ip "+json_config["remote_ip"]+" "
  40. if "remote_port" in json_config:
  41. config += "--remote_port "+str(json_config["remote_port"])+" "
  42. if "remote_name" in json_config:
  43. config += "--remote_name "+json_config["remote_name"]+" "
  44. if "send_period" in json_config:
  45. config += "--send_period "+str(json_config["send_period"])+" "
  46. return config.strip()
  47. retrieve_config()