1
0

cjdnsadminmaker.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/usr/bin/env python
  2. import json
  3. import os
  4. import sys
  5. import subprocess
  6. # possibly search for running cjdroute processes and check the same folder as they're in
  7. # and/or running find on the home folder
  8. ## Wanted: Everyone's favorite place to store their shit.
  9. conflocations = ["/etc/cjdroute.conf",
  10. "~/cjdroute.conf",
  11. "~/cjdns/cjdroute.conf",
  12. "/usr/local/opt/cjdns/cjdroute.conf"]
  13. cjdroutelocations = ["/opt/cjdns",
  14. "~/cjdns",
  15. "~/cjdns-git",
  16. "/usr/local/opt/cjdns"]
  17. cjdroutelocations += os.getenv("PATH").split(":")
  18. cjdnsadmin_path = os.path.expanduser("~/.cjdnsadmin")
  19. def ask(question, default):
  20. while True:
  21. r = raw_input("%s " % question).lower() or default
  22. if r in "yn":
  23. return r == "y"
  24. else:
  25. print "Invalid response, please enter either y or n"
  26. def find_cjdroute_bin():
  27. for path in cjdroutelocations:
  28. path = os.path.expanduser(path) + "/cjdroute"
  29. if os.path.isfile(path):
  30. return path
  31. print "Failed to find cjdroute"
  32. print "Please tell me where it is"
  33. return raw_input("ie. <cjdns git>/cjdroute: ")
  34. def find_cjdroute_conf():
  35. for path in conflocations:
  36. path = os.path.expanduser(path)
  37. if os.path.isfile(path):
  38. return path
  39. return raw_input("Can't find cjdroute.conf, please give the path to it here: ")
  40. def load_cjdroute_conf(conf):
  41. print "Loading " + conf
  42. try:
  43. with open(conf) as conffile:
  44. return json.load(conffile)
  45. except ValueError:
  46. return cleanup_config(conf)
  47. except IOError:
  48. print "Error opening " + conf + ". Do we have permission to access it?"
  49. print "Hint: Try running this as root"
  50. sys.exit(1)
  51. def cleanup_config(conf):
  52. print "Making valid JSON out of " + conf
  53. print "First, we need to find the cleanconfig program"
  54. cjdroute = find_cjdroute_bin()
  55. print "Using " + cjdroute
  56. process = subprocess.Popen([cjdroute, "--cleanconf"], stdin=open(conf), stdout=subprocess.PIPE)
  57. try:
  58. return json.load(process.stdout)
  59. except ValueError:
  60. print "Failed to parse!"
  61. print "-" * 8
  62. print cleanconf
  63. print "-" * 8
  64. sys.exit(1)
  65. try:
  66. with open(cjdnsadmin_path) as cjdnsadmin_file:
  67. json.load(cjdnsadmin_file)
  68. if not ask("%s appears to be a valid JSON file. Update? [Y/n]" % cjdnsadmin_path, "y"):
  69. sys.exit()
  70. except ValueError:
  71. if not ask("%s appears to be a file. Overwrite? [y/N]" % cjdnsadmin_path, "n"):
  72. sys.exit()
  73. except IOError:
  74. print "This script will attempt to create " + cjdnsadmin_path
  75. conf = find_cjdroute_conf()
  76. cjdrouteconf = load_cjdroute_conf(conf)
  77. addr, port = cjdrouteconf['admin']['bind'].split(":")
  78. cjdnsadmin = {}
  79. cjdnsadmin["addr"] = addr
  80. cjdnsadmin["port"] = int(port)
  81. cjdnsadmin["password"] = cjdrouteconf['admin']['password']
  82. cjdnsadmin["config"] = conf
  83. with open(cjdnsadmin_path, "w+") as adminfile:
  84. json.dump(cjdnsadmin, adminfile, indent=4)
  85. print "Done! Give it a shot, why dont ya"