From 395f7cd4f0fc31d8a2560d20f61fde3bc792ab4a Mon Sep 17 00:00:00 2001 From: Brian Warner Date: Sat, 8 Oct 2016 21:53:41 -0400 Subject: [PATCH] create_node: prep for slow write_node_config() This puts the right inlineCallbacks in place to allow write_node_config() to return a Deferred. The upcoming Tor support will need this (since it must wait for an .onion address to be allocated before it can write tahoe.cfg's tub.port and tub.location lines). --- src/allmydata/scripts/create_node.py | 20 ++++++++++++-------- src/allmydata/test/cli/test_create.py | 25 +++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/src/allmydata/scripts/create_node.py b/src/allmydata/scripts/create_node.py index 7cd81b38d..0e3ce2c67 100644 --- a/src/allmydata/scripts/create_node.py +++ b/src/allmydata/scripts/create_node.py @@ -1,4 +1,5 @@ import os +from twisted.internet import defer from twisted.python.usage import UsageError from allmydata.scripts.common import BasedirOptions, NoDefaultBasedirOptions from allmydata.scripts.default_nodedir import _default_nodedir @@ -116,6 +117,7 @@ class CreateIntroducerOptions(NoDefaultBasedirOptions): NoDefaultBasedirOptions.parseArgs(self, basedir) validate_where_options(self) +@defer.inlineCallbacks def write_node_config(c, config): # this is shared between clients and introducers c.write("# -*- mode: conf; coding: utf-8 -*-\n") @@ -174,6 +176,7 @@ def write_node_config(c, config): c.write("#ssh.port = 8022\n") c.write("#ssh.authorized_keys_file = ~/.ssh/authorized_keys\n") c.write("\n") + yield None def write_client_config(c, config): c.write("[client]\n") @@ -207,6 +210,7 @@ def write_client_config(c, config): c.write("enabled = false\n") c.write("\n") +@defer.inlineCallbacks def create_node(config): out = config.stdout err = config.stderr @@ -219,14 +223,14 @@ def create_node(config): print >>err, "The base directory %s is not empty." % quote_local_unicode_path(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 + defer.returnValue(-1) # we're willing to use an empty directory else: os.mkdir(basedir) write_tac(basedir, "client") with open(os.path.join(basedir, "tahoe.cfg"), "w") as c: - write_node_config(c, config) + yield write_node_config(c, config) write_client_config(c, config) from allmydata.util import fileutil @@ -237,7 +241,7 @@ def create_node(config): print >>out, " The node cannot connect to a grid without it." if not config.get("nickname", ""): print >>out, " Please set [node]nickname= in tahoe.cfg" - return 0 + defer.returnValue(0) def create_client(config): config['no-storage'] = True @@ -245,6 +249,7 @@ def create_client(config): return create_node(config) +@defer.inlineCallbacks def create_introducer(config): out = config.stdout err = config.stderr @@ -257,18 +262,17 @@ def create_introducer(config): print >>err, "The base directory %s is not empty." % quote_local_unicode_path(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 + defer.returnValue(-1) # we're willing to use an empty directory else: os.mkdir(basedir) write_tac(basedir, "introducer") - c = open(os.path.join(basedir, "tahoe.cfg"), "w") - write_node_config(c, config) - c.close() + with open(os.path.join(basedir, "tahoe.cfg"), "w") as c: + yield write_node_config(c, config) print >>out, "Introducer created in %s" % quote_local_unicode_path(basedir) - return 0 + defer.returnValue(0) subCommands = [ diff --git a/src/allmydata/test/cli/test_create.py b/src/allmydata/test/cli/test_create.py index 2a2e40fda..69b70be05 100644 --- a/src/allmydata/test/cli/test_create.py +++ b/src/allmydata/test/cli/test_create.py @@ -160,6 +160,18 @@ class Config(unittest.TestCase): "create-node", "--location=tor:myservice.onion:12345") self.assertEqual(str(e), "--location must be used with --port") + @defer.inlineCallbacks + def test_node_basedir_exists(self): + basedir = self.mktemp() + os.mkdir(basedir) + with open(os.path.join(basedir, "foo"), "w") as f: + f.write("blocker") + rc, out, err = yield run_cli("create-node", "--hostname=foo", basedir) + self.assertEqual(rc, -1) + self.assertIn(basedir, err) + self.assertIn("is not empty", err) + self.assertIn("To avoid clobbering anything, I am going to quit now", err) + def test_introducer_no_hostname(self): basedir = self.mktemp() e = self.assertRaises(usage.UsageError, parse_cli, @@ -182,3 +194,16 @@ class Config(unittest.TestCase): cfg = self.read_config(basedir) self.assertTrue("foo" in cfg.get("node", "tub.location")) self.assertEqual(cfg.getboolean("node", "reveal-IP-address"), True) + + @defer.inlineCallbacks + def test_introducer_basedir_exists(self): + basedir = self.mktemp() + os.mkdir(basedir) + with open(os.path.join(basedir, "foo"), "w") as f: + f.write("blocker") + rc, out, err = yield run_cli("create-introducer", "--hostname=foo", + basedir) + self.assertEqual(rc, -1) + self.assertIn(basedir, err) + self.assertIn("is not empty", err) + self.assertIn("To avoid clobbering anything, I am going to quit now", err)