1
0

cjdnsadminmaker.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. os.getenv("HOME") + "/cjdroute.conf",
  11. os.getenv("HOME") + "/cjdns/cjdroute.conf",
  12. "/usr/local/opt/cjdns/cjdroute.conf"]
  13. cjdnslocations = ["/opt/cjdns",
  14. os.getenv("HOME") + "/cjdns",
  15. os.getenv("HOME") + "/cjdns-git",
  16. "/usr/local/opt/cjdns"]
  17. cjdnsadmin = {}
  18. if os.path.isfile(os.getenv("HOME") + "/.cjdnsadmin"):
  19. validjson = False
  20. try:
  21. cjdnsadmin = json.load(open(os.getenv("HOME") + "/.cjdnsadmin"))
  22. validjson = True
  23. except ValueError:
  24. pass
  25. if validjson:
  26. while True:
  27. r = raw_input(os.getenv("HOME") + "/.cjdnsadmin appears to be a valid JSON file. Update? [Y/n] ")
  28. if r.lower() == "n":
  29. sys.exit()
  30. elif r.lower() == "y" or r == "":
  31. break
  32. else:
  33. print "Invalid response, please enter either y or n"
  34. else:
  35. while True:
  36. r = raw_input(os.getenv("HOME") + "/.cjdnsadmin appears to be a file. Overwrite? [y/N] ")
  37. if r.lower() == "n" or r == "":
  38. sys.exit()
  39. elif r.lower() == "y":
  40. break
  41. else:
  42. print "Invalid response, please enter either y or n"
  43. else:
  44. print "This script will attempt to create " + os.getenv("HOME") + "/.cjdnsadmin"
  45. def validjson(conf):
  46. print "Making valid JSON out of " + conf
  47. print "First, we need to find the cleanconfig program"
  48. cleanconfig = ""
  49. i = 0
  50. while not os.path.isfile(cleanconfig):
  51. if i < len(cjdnslocations):
  52. cleanconfig = cjdnslocations[i] + "/build/cleanconfig"
  53. i += 1
  54. else:
  55. print "Failed to find cleanconfig"
  56. print "Please tell me where it is"
  57. cleanconfig = raw_input("HINT: <cjdns git>/build/cleanconfig: ")
  58. print "Using " + cleanconfig
  59. process = subprocess.Popen([cleanconfig], stdin=open(conf), stdout=subprocess.PIPE)
  60. cleanconf = process.stdout.read()
  61. try:
  62. return json.loads(cleanconf)
  63. except ValueError:
  64. open("debug.log", "w+").write(cleanconf)
  65. print "Failed to parse! Check debug.log"
  66. sys.exit(1)
  67. done = False
  68. i = 0
  69. while not done:
  70. if i <= len(conflocations):
  71. conf = conflocations[i]
  72. i += 1
  73. else:
  74. conf = raw_input("Can't find cjdroute.conf, please give the path to it here: ")
  75. sys.exit(1)
  76. if os.path.isfile(conf):
  77. print "Loading " + conf
  78. try:
  79. cjdrouteconf = json.load(open(conf))
  80. except ValueError:
  81. cjdrouteconf = validjson(conf)
  82. except IOError:
  83. print "Error opening " + conf + ". Do we have permission to access it?"
  84. print "Hint: Try running this as root"
  85. sys.exit(1)
  86. addr, port = cjdrouteconf['admin']['bind'].split(":")
  87. cjdnsadmin["addr"] = addr
  88. cjdnsadmin["port"] = int(port)
  89. cjdnsadmin["password"] = cjdrouteconf['admin']['password']
  90. cjdnsadmin["config"] = conf
  91. adminfile = open(os.getenv("HOME") + "/.cjdnsadmin", "w+")
  92. adminfile.write(json.dumps(cjdnsadmin, indent=4))
  93. adminfile.close()
  94. print "Done! Give it a shot, why dont ya"
  95. done = True