1
0

cjdnsadminmaker.py 3.5 KB

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