-
import datetime, os.path, re, types, ConfigParser, tempfile
from base64 import b32decode, b32encode
from allmydata.util import log
from allmydata.util import fileutil, iputil, observer
from allmydata.util.assertutil import precondition, _assert
+from allmydata.util.fileutil import abspath_expanduser_unicode
+from allmydata.util.encodingutil import get_filesystem_encoding, quote_output
# Add our application versions to the data that Foolscap's LogPublisher
# reports.
This directory contains files which contain private data for the Tahoe node,
such as private keys. On Unix-like systems, the permissions on this directory
are set to disallow users other than its owner from reading the contents of
-the files. See the 'configuration.txt' documentation file for details."""
+the files. See the 'configuration.rst' documentation file for details."""
class _None: # used as a marker in get_config()
pass
class MissingConfigEntry(Exception):
+ """ A required config entry was not found. """
+
+class OldConfigError(Exception):
+ """ An obsolete config file was found. See
+ docs/historical/configuration.rst. """
+ def __str__(self):
+ return ("Found pre-Tahoe-LAFS-v1.3 configuration file(s):\n"
+ "%s\n"
+ "See docs/historical/configuration.rst."
+ % "\n".join([quote_output(fname) for fname in self.args[0]]))
+
+class OldConfigOptionError(Exception):
pass
+
class Node(service.MultiService):
# this implements common functionality of both Client nodes and Introducer
# nodes.
NODETYPE = "unknown NODETYPE"
PORTNUMFILE = None
CERTFILE = "node.pem"
+ GENERATED_FILES = []
- def __init__(self, basedir="."):
+ def __init__(self, basedir=u"."):
service.MultiService.__init__(self)
- self.basedir = os.path.abspath(basedir)
+ self.basedir = abspath_expanduser_unicode(unicode(basedir))
self._portnumfile = os.path.join(self.basedir, self.PORTNUMFILE)
self._tub_ready_observerlist = observer.OneShotObserverList()
fileutil.make_dirs(os.path.join(self.basedir, "private"), 0700)
open(os.path.join(self.basedir, "private", "README"), "w").write(PRIV_README)
- # creates self.config, populates from distinct files if necessary
+ # creates self.config
self.read_config()
-
nickname_utf8 = self.get_config("node", "nickname", "<unspecified>")
self.nickname = nickname_utf8.decode("utf-8")
assert type(self.nickname) is unicode
iputil.increase_rlimits()
def init_tempdir(self):
- local_tempdir = "tmp" # default is NODEDIR/tmp/
- tempdir = self.get_config("node", "tempdir", local_tempdir)
+ local_tempdir_utf8 = "tmp" # default is NODEDIR/tmp/
+ tempdir = self.get_config("node", "tempdir", local_tempdir_utf8).decode('utf-8')
tempdir = os.path.join(self.basedir, tempdir)
if not os.path.exists(tempdir):
fileutil.make_dirs(tempdir)
- tempfile.tempdir = os.path.abspath(tempdir)
+ tempfile.tempdir = abspath_expanduser_unicode(tempdir)
# this should cause twisted.web.http (which uses
# tempfile.TemporaryFile) to put large request bodies in the given
# directory. Without this, the default temp dir is usually /tmp/,
return self.config.get(section, option)
except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
if default is _None:
- fn = os.path.join(self.basedir, "tahoe.cfg")
+ fn = os.path.join(self.basedir, u"tahoe.cfg")
raise MissingConfigEntry("%s is missing the [%s]%s entry"
- % (fn, section, option))
+ % (quote_output(fn), section, option))
return default
def set_config(self, section, option, value):
assert self.config.get(section, option) == value
def read_config(self):
+ self.error_about_old_config_files()
self.config = ConfigParser.SafeConfigParser()
- self.config.read([os.path.join(self.basedir, "tahoe.cfg")])
- self.read_old_config_files()
- def read_old_config_files(self):
- # backwards-compatibility: individual files will override the
- # contents of tahoe.cfg
- copy = self._copy_config_from_file
-
- copy("nickname", "node", "nickname")
- copy("webport", "node", "web.port")
+ tahoe_cfg = os.path.join(self.basedir, "tahoe.cfg")
+ try:
+ f = open(tahoe_cfg, "rb")
+ try:
+ # Skip any initial Byte Order Mark. Since this is an ordinary file, we
+ # don't need to handle incomplete reads, and can assume seekability.
+ if f.read(3) != '\xEF\xBB\xBF':
+ f.seek(0)
+ self.config.readfp(f)
+ finally:
+ f.close()
+ except EnvironmentError:
+ if os.path.exists(tahoe_cfg):
+ raise
cfg_tubport = self.get_config("node", "tub.port", "")
if not cfg_tubport:
# For 'tub.port', tahoe.cfg overrides the individual file on
- # disk. So only read self._portnumfile is tahoe.cfg doesn't
+ # disk. So only read self._portnumfile if tahoe.cfg doesn't
# provide a value.
try:
- file_tubport = open(self._portnumfile, "rU").read().strip()
+ file_tubport = fileutil.read(self._portnumfile).strip()
self.set_config("node", "tub.port", file_tubport)
except EnvironmentError:
- pass
-
- copy("keepalive_timeout", "node", "timeout.keepalive")
- copy("disconnect_timeout", "node", "timeout.disconnect")
-
- def _copy_config_from_file(self, config_filename, section, keyname):
- s = self.get_config_from_file(config_filename)
- if s is not None:
- self.set_config(section, keyname, s)
+ if os.path.exists(self._portnumfile):
+ raise
+
+ def error_about_old_config_files(self):
+ """ If any old configuration files are detected, raise OldConfigError. """
+
+ oldfnames = set()
+ for name in [
+ 'nickname', 'webport', 'keepalive_timeout', 'log_gatherer.furl',
+ 'disconnect_timeout', 'advertised_ip_addresses', 'introducer.furl',
+ 'helper.furl', 'key_generator.furl', 'stats_gatherer.furl',
+ 'no_storage', 'readonly_storage', 'sizelimit',
+ 'debug_discard_storage', 'run_helper']:
+ if name not in self.GENERATED_FILES:
+ fullfname = os.path.join(self.basedir, name)
+ if os.path.exists(fullfname):
+ oldfnames.add(fullfname)
+ if oldfnames:
+ e = OldConfigError(oldfnames)
+ twlog.msg(e)
+ raise e
def create_tub(self):
certfile = os.path.join(self.basedir, "private", self.CERTFILE)
def setup_ssh(self):
ssh_port = self.get_config("node", "ssh.port", "")
if ssh_port:
- ssh_keyfile = self.get_config("node", "ssh.authorized_keys_file")
+ ssh_keyfile = self.get_config("node", "ssh.authorized_keys_file").decode('utf-8')
from allmydata import manhole
- m = manhole.AuthorizedKeysManhole(ssh_port, ssh_keyfile)
+ m = manhole.AuthorizedKeysManhole(ssh_port, ssh_keyfile.encode(get_filesystem_encoding()))
m.setServiceParent(self)
self.log("AuthorizedKeysManhole listening on %s" % ssh_port)
from the data."""
fn = os.path.join(self.basedir, name)
try:
- return open(fn, "r").read().strip()
+ return fileutil.read(fn).strip()
except EnvironmentError:
if not required:
return None
def write_private_config(self, name, value):
"""Write the (string) contents of a private config file (which is a
config file that resides within the subdirectory named 'private'), and
- return it. Any leading or trailing whitespace will be stripped from
- the data.
+ return it.
+ """
+ privname = os.path.join(self.basedir, "private", name)
+ open(privname, "w").write(value)
+
+ def get_private_config(self, name, default=_None):
+ """Read the (string) contents of a private config file (which is a
+ config file that resides within the subdirectory named 'private'),
+ and return it. Return a default, or raise an error if one was not
+ given.
"""
privname = os.path.join(self.basedir, "private", name)
- open(privname, "w").write(value.strip())
+ try:
+ return fileutil.read(privname)
+ except EnvironmentError:
+ if os.path.exists(privname):
+ raise
+ if default is _None:
+ raise MissingConfigEntry("The required configuration file %s is missing."
+ % (quote_output(privname),))
+ return default
- def get_or_create_private_config(self, name, default):
+ def get_or_create_private_config(self, name, default=_None):
"""Try to get the (string) contents of a private config file (which
is a config file that resides within the subdirectory named
'private'), and return it. Any leading or trailing whitespace will be
stripped from the data.
- If the file does not exist, try to create it using default, and
- then return the value that was written. If 'default' is a string,
- use it as a default value. If not, treat it as a 0-argument callable
- which is expected to return a string.
+ If the file does not exist, and default is not given, report an error.
+ If the file does not exist and a default is specified, try to create
+ it using that default, and then return the value that was written.
+ If 'default' is a string, use it as a default value. If not, treat it
+ as a zero-argument callable that is expected to return a string.
"""
- privname = os.path.join("private", name)
- value = self.get_config_from_file(privname)
- if value is None:
- if isinstance(default, (str, unicode)):
+ privname = os.path.join(self.basedir, "private", name)
+ try:
+ value = fileutil.read(privname)
+ except EnvironmentError:
+ if os.path.exists(privname):
+ raise
+ if default is _None:
+ raise MissingConfigEntry("The required configuration file %s is missing."
+ % (quote_output(privname),))
+ if isinstance(default, basestring):
value = default
else:
value = default()
- fn = os.path.join(self.basedir, privname)
- try:
- open(fn, "w").write(value)
- except EnvironmentError, e:
- self.log("Unable to write config file '%s'" % fn)
- self.log(e)
- value = value.strip()
- return value
+ fileutil.write(privname, value)
+ return value.strip()
def write_config(self, name, value, mode="w"):
"""Write a string to a config file."""
fn = os.path.join(self.basedir, name)
try:
- open(fn, mode).write(value)
+ fileutil.write(fn, value, mode)
except EnvironmentError, e:
self.log("Unable to write config file '%s'" % fn)
self.log(e)
return self.stopService()
def setup_logging(self):
- # we replace the formatTime() method of the log observer that twistd
- # set up for us, with a method that uses better timestamps.
+ # we replace the formatTime() method of the log observer that
+ # twistd set up for us, with a method that uses our preferred
+ # timestamp format.
for o in twlog.theLogPublisher.observers:
# o might be a FileLogObserver's .emit method
if type(o) is type(self.setup_logging): # bound method
ob.formatTime = newmeth
# TODO: twisted >2.5.0 offers maxRotatedFiles=50
- self.tub.setOption("logport-furlfile",
- os.path.join(self.basedir, "private","logport.furl"))
+ lgfurl_file = os.path.join(self.basedir, "private", "logport.furl").encode(get_filesystem_encoding())
+ self.tub.setOption("logport-furlfile", lgfurl_file)
lgfurl = self.get_config("node", "log_gatherer.furl", "")
if lgfurl:
# this is in addition to the contents of log-gatherer-furlfile
self.tub.setOption("bridge-twisted-logs", True)
incident_dir = os.path.join(self.basedir, "logs", "incidents")
# this doesn't quite work yet: unit tests fail
- foolscap.logging.log.setLogDir(incident_dir)
+ foolscap.logging.log.setLogDir(incident_dir.encode(get_filesystem_encoding()))
def log(self, *args, **kwargs):
return log.msg(*args, **kwargs)
portnum = l.getPortnum()
# record which port we're listening on, so we can grab the same one
# next time
- open(self._portnumfile, "w").write("%d\n" % portnum)
+ fileutil.write_atomically(self._portnumfile, "%d\n" % portnum, mode="")
base_location = ",".join([ "%s:%d" % (addr, portnum)
for addr in local_addresses ])
def add_service(self, s):
s.setServiceParent(self)
return s
-