Also factor out common code in basedir creation between node types. refs #2011, #2325
Signed-off-by: Daira Hopwood <daira@jacaranda.org>
import codecs
from twisted.python import usage
from allmydata.util.assertutil import precondition
-from allmydata.util.encodingutil import unicode_to_url, quote_output, argv_to_abspath
+from allmydata.util.encodingutil import listdir_unicode, unicode_to_url, quote_output, argv_to_abspath
+from allmydata.util import fileutil
from allmydata.util.fileutil import abspath_expanduser_unicode
+class NonEmptyBasedirException(Exception):
+ pass
+
+_DUMMY_TAC = """
+# %s
+import sys
+print >>sys.stderr, "Tahoe-LAFS v1.10 or earlier cannot run nodes with"
+print >>sys.stderr, "base directories created by versions after v1.11."
+sys.exit(1)
+"""
+
+def create_basedir(basedir, nodetype, err=sys.stderr):
+ # This should always be called with an absolute Unicode basedir.
+ precondition(isinstance(basedir, unicode), basedir)
+
+ if os.path.exists(basedir):
+ if listdir_unicode(basedir):
+ print >>err, "The base directory %s is not empty." % quote_output(basedir)
+ print >>err, "To avoid clobbering anything, I am going to quit now."
+ print >>err, "Please use a different directory, or empty this one."
+ raise NonEmptyBasedirException()
+ # we're willing to use an empty directory
+ else:
+ os.mkdir(basedir)
+
+ fileutil.write(os.path.join(basedir, u"tahoe-%s.tac" % (nodetype,)),
+ _DUMMY_TAC % (nodetype,), mode="w")
+
+
_default_nodedir = None
if sys.platform == 'win32':
from allmydata.windows import registry
import os, sys
-from allmydata.scripts.common import BasedirOptions, NoDefaultBasedirOptions
-from allmydata.util.assertutil import precondition
-from allmydata.util.encodingutil import listdir_unicode, argv_to_unicode, quote_output
-import allmydata
+from allmydata.scripts.common import BasedirOptions, NoDefaultBasedirOptions, create_basedir, \
+ NonEmptyBasedirException
+from allmydata.util.encodingutil import argv_to_unicode, quote_output
+from allmydata.util import fileutil
+
class CreateClientOptions(BasedirOptions):
optParameters = [
subcommand_name = "create-introducer"
-client_tac = """
-# -*- python -*-
-
-import pkg_resources
-pkg_resources.require('%s')
-pkg_resources.require('twisted')
-from allmydata import client
-from twisted.application import service
-
-c = client.Client()
-
-application = service.Application("allmydata_client")
-c.setServiceParent(application)
-""" % (allmydata.__appname__,)
-
-introducer_tac = """
-# -*- python -*-
-
-import pkg_resources
-pkg_resources.require('%s')
-pkg_resources.require('twisted')
-from allmydata import introducer
-from twisted.application import service
-
-c = introducer.IntroducerNode()
-
-application = service.Application("allmydata_introducer")
-c.setServiceParent(application)
-""" % (allmydata.__appname__,)
-
def write_node_config(c, config):
# this is shared between clients and introducers
c.write("# -*- mode: conf; coding: utf-8 -*-\n")
def create_node(config, out=sys.stdout, err=sys.stderr):
basedir = config['basedir']
- # This should always be called with an absolute Unicode basedir.
- precondition(isinstance(basedir, unicode), basedir)
-
- if os.path.exists(basedir):
- if listdir_unicode(basedir):
- print >>err, "The base directory %s is not empty." % quote_output(basedir)
- print >>err, "To avoid clobbering anything, I am going to quit now."
- print >>err, "Please use a different directory, or empty this one."
- return -1
- # we're willing to use an empty directory
- else:
- os.mkdir(basedir)
- f = open(os.path.join(basedir, "tahoe-client.tac"), "w")
- f.write(client_tac)
- f.close()
+ try:
+ create_basedir(basedir, "client", err=err)
+ except NonEmptyBasedirException:
+ return -1
c = open(os.path.join(basedir, "tahoe.cfg"), "w")
c.close()
- from allmydata.util import fileutil
fileutil.make_dirs(os.path.join(basedir, "private"), 0700)
print >>out, "Node created in %s" % quote_output(basedir)
if not config.get("introducer", ""):
def create_introducer(config, out=sys.stdout, err=sys.stderr):
basedir = config['basedir']
- # This should always be called with an absolute Unicode basedir.
- precondition(isinstance(basedir, unicode), basedir)
-
- if os.path.exists(basedir):
- if listdir_unicode(basedir):
- print >>err, "The base directory %s is not empty." % quote_output(basedir)
- print >>err, "To avoid clobbering anything, I am going to quit now."
- print >>err, "Please use a different directory, or empty this one."
- return -1
- # we're willing to use an empty directory
- else:
- os.mkdir(basedir)
- f = open(os.path.join(basedir, "tahoe-introducer.tac"), "w")
- f.write(introducer_tac)
- f.close()
+ try:
+ create_basedir(basedir, "introducer", err=err)
+ except NonEmptyBasedirException:
+ return -1
c = open(os.path.join(basedir, "tahoe.cfg"), "w")
write_node_config(c, config)
-import os, sys
-from allmydata.scripts.common import NoDefaultBasedirOptions
-from allmydata.util.assertutil import precondition
-from allmydata.util.encodingutil import listdir_unicode, quote_output
+import sys
+from allmydata.scripts.common import NoDefaultBasedirOptions, create_basedir, NonEmptyBasedirException
+from allmydata.util.encodingutil import quote_output
class CreateKeyGeneratorOptions(NoDefaultBasedirOptions):
subcommand_name = "create-key-generator"
-keygen_tac = """
-# -*- python -*-
-
-import pkg_resources
-pkg_resources.require('allmydata-tahoe')
-
-from allmydata import key_generator
-from twisted.application import service
-
-k = key_generator.KeyGeneratorService(default_key_size=2048)
-#k.key_generator.verbose = False
-#k.key_generator.pool_size = 16
-#k.key_generator.pool_refresh_delay = 6
-
-application = service.Application("allmydata_key_generator")
-k.setServiceParent(application)
-"""
-
def create_key_generator(config, out=sys.stdout, err=sys.stderr):
basedir = config['basedir']
- # This should always be called with an absolute Unicode basedir.
- precondition(isinstance(basedir, unicode), basedir)
-
- if os.path.exists(basedir):
- if listdir_unicode(basedir):
- print >>err, "The base directory %s is not empty." % quote_output(basedir)
- print >>err, "To avoid clobbering anything, I am going to quit now."
- print >>err, "Please use a different directory, or empty this one."
- return -1
- # we're willing to use an empty directory
- else:
- os.mkdir(basedir)
- f = open(os.path.join(basedir, "tahoe-key-generator.tac"), "wb")
- f.write(keygen_tac)
- f.close()
+ try:
+ create_basedir(basedir, "key-generator", err=err)
+ except NonEmptyBasedirException:
+ return -1
+
+ print >>out, "Key generator created in %s" % quote_output(basedir)
return 0
subCommands = [
-import os, sys
-from allmydata.scripts.common import NoDefaultBasedirOptions
-from allmydata.util.assertutil import precondition
-from allmydata.util.encodingutil import listdir_unicode, quote_output
+import sys
+from allmydata.scripts.common import NoDefaultBasedirOptions, create_basedir, NonEmptyBasedirException
+from allmydata.util.encodingutil import quote_output
class CreateStatsGathererOptions(NoDefaultBasedirOptions):
subcommand_name = "create-stats-gatherer"
-stats_gatherer_tac = """
-# -*- python -*-
-
-from allmydata import stats
-from twisted.application import service
-
-verbose = True
-g = stats.StatsGathererService(verbose=verbose)
-
-application = service.Application('allmydata_stats_gatherer')
-g.setServiceParent(application)
-"""
-
-
def create_stats_gatherer(config, out=sys.stdout, err=sys.stderr):
basedir = config['basedir']
- # This should always be called with an absolute Unicode basedir.
- precondition(isinstance(basedir, unicode), basedir)
-
- if os.path.exists(basedir):
- if listdir_unicode(basedir):
- print >>err, "The base directory %s is not empty." % quote_output(basedir)
- print >>err, "To avoid clobbering anything, I am going to quit now."
- print >>err, "Please use a different directory, or empty this one."
- return -1
- # we're willing to use an empty directory
- else:
- os.mkdir(basedir)
- f = open(os.path.join(basedir, "tahoe-stats-gatherer.tac"), "wb")
- f.write(stats_gatherer_tac)
- f.close()
+ try:
+ create_basedir(basedir, "stats-gatherer", err=err)
+ except NonEmptyBasedirException:
+ return -1
+
+ print >>out, "Stats gatherer created in %s" % quote_output(basedir)
return 0
subCommands = [