source file: /home/buildslave/tahoe/edgy/build/src/allmydata/scripts/create_node.py
file stats: 42 lines, 42 executed: 100.0% covered
   1. 
   2. import os, sys
   3. from twisted.python import usage
   4. from allmydata.scripts.common import BasedirMixin, NoDefaultBasedirMixin
   5. 
   6. class CreateClientOptions(BasedirMixin, usage.Options):
   7.     optParameters = [
   8.         ["basedir", "C", None, "which directory to create the client in"],
   9.         ["webport", "p", "tcp:8123:interface=127.0.0.1",
  10.          "which TCP port to run the HTTP interface on. Use 'none' to disable."],
  11.         ]
  12. 
  13. class CreateIntroducerOptions(NoDefaultBasedirMixin, usage.Options):
  14.     optParameters = [
  15.         ["basedir", "C", None, "which directory to create the introducer in"],
  16.         ]
  17. 
  18. client_tac = """
  19. # -*- python -*-
  20. 
  21. from allmydata import client
  22. from twisted.application import service
  23. 
  24. c = client.Client()
  25. 
  26. application = service.Application("allmydata_client")
  27. c.setServiceParent(application)
  28. """
  29. 
  30. introducer_tac = """
  31. # -*- python -*-
  32. 
  33. from allmydata import introducer
  34. from twisted.application import service
  35. 
  36. c = introducer.IntroducerNode()
  37. 
  38. application = service.Application("allmydata_introducer")
  39. c.setServiceParent(application)
  40. """
  41. 
  42. def create_client(basedir, config, out=sys.stdout, err=sys.stderr):
  43.     if os.path.exists(basedir):
  44.         if os.listdir(basedir):
  45.             print >>err, "The base directory \"%s\", which is \"%s\" is not empty." % (basedir, os.path.abspath(basedir))
  46.             print >>err, "To avoid clobbering anything, I am going to quit now."
  47.             print >>err, "Please use a different directory, or empty this one."
  48.             return -1
  49.         # we're willing to use an empty directory
  50.     else:
  51.         os.mkdir(basedir)
  52.     f = open(os.path.join(basedir, "tahoe-client.tac"), "w")
  53.     f.write(client_tac)
  54.     f.close()
  55.     if config.get('webport', "none").lower() != "none":
  56.         f = open(os.path.join(basedir, "webport"), "w")
  57.         f.write(config['webport'] + "\n")
  58.         f.close()
  59.     from allmydata.util import fileutil
  60.     fileutil.make_dirs(os.path.join(basedir, "private"), 0700)
  61.     print >>out, "client created in %s" % basedir
  62.     print >>out, " please copy introducer.furl into the directory"
  63. 
  64. def create_introducer(basedir, config, out=sys.stdout, err=sys.stderr):
  65.     if os.path.exists(basedir):
  66.         if os.listdir(basedir):
  67.             print >>err, "The base directory \"%s\", which is \"%s\" is not empty." % (basedir, os.path.abspath(basedir))
  68.             print >>err, "To avoid clobbering anything, I am going to quit now."
  69.             print >>err, "Please use a different directory, or empty this one."
  70.             return -1
  71.         # we're willing to use an empty directory
  72.     else:
  73.         os.mkdir(basedir)
  74.     f = open(os.path.join(basedir, "tahoe-introducer.tac"), "w")
  75.     f.write(introducer_tac)
  76.     f.close()
  77.     print >>out, "introducer created in %s" % basedir
  78. 
  79. subCommands = [
  80.     ["create-client", None, CreateClientOptions, "Create a client node."],
  81.     ["create-introducer", None, CreateIntroducerOptions, "Create a introducer node."],
  82. 
  83. ]
  84. 
  85. dispatch = {
  86.     "create-client": create_client,
  87.     "create-introducer": create_introducer,
  88.     }