Load static server configuration from connections.yaml

This commit is contained in:
David Stainton 2016-05-17 16:47:57 +00:00 committed by Daira Hopwood
parent 30df8667eb
commit e4d93a745c

View File

@ -1,4 +1,4 @@
import os, stat, time, weakref
import os, stat, time, weakref, yaml
from allmydata import node
from base64 import urlsafe_b64encode
@ -125,6 +125,7 @@ class Client(node.Node, pollmixin.PollMixin):
self.started_timestamp = time.time()
self.logSource="Client"
self.encoding_params = self.DEFAULT_ENCODING_PARAMETERS.copy()
self.load_connections()
self.init_introducer_client()
self.init_stats_provider()
self.init_secrets()
@ -182,6 +183,25 @@ class Client(node.Node, pollmixin.PollMixin):
self.introducer_client = ic
ic.setServiceParent(self)
def load_connections(self):
"""
Load the connections.yaml file if it exists, otherwise
create a default configuration.
"""
self.warn_flag = False
connections_filepath = FilePath(os.path.join(self.basedir, "private", "connections.yaml"))
def construct_unicode(loader, node):
return node.value
yaml.SafeLoader.add_constructor("tag:yaml.org,2002:str",
construct_unicode)
try:
with connections_filepath.open() as f:
self.connections_config = yaml.safe_load(f)
except EnvironmentError:
exists = False
self.connections_config = { 'servers' : {} }
connections_filepath.setContent(yaml.safe_dump(self.connections_config))
def init_stats_provider(self):
gatherer_furl = self.get_config("client", "stats_gatherer.furl", None)
self.stats_provider = StatsProvider(self, gatherer_furl)
@ -360,32 +380,15 @@ class Client(node.Node, pollmixin.PollMixin):
helper = storage_client.ConnectedEnough(sb, connection_threshold)
self.upload_ready_d = helper.when_connected_enough()
# load static server specifications from tahoe.cfg, if any.
# Not quite ready yet.
#if self.config.has_section("client-server-selection"):
# server_params = {} # maps serverid to dict of parameters
# for (name, value) in self.config.items("client-server-selection"):
# pieces = name.split(".")
# if pieces[0] == "server":
# serverid = pieces[1]
# if serverid not in server_params:
# server_params[serverid] = {}
# server_params[serverid][pieces[2]] = value
# for serverid, params in server_params.items():
# server_type = params.pop("type")
# if server_type == "tahoe-foolscap":
# s = storage_client.NativeStorageClient(*params)
# else:
# msg = ("unrecognized server type '%s' in "
# "tahoe.cfg [client-server-selection]server.%s.type"
# % (server_type, serverid))
# raise storage_client.UnknownServerTypeError(msg)
# sb.add_server(s.serverid, s)
# utilize the loaded static server specifications
servers = self.connections_config['servers']
for server_id in servers.keys():
if self.testing:
self.storage_broker.got_static_announcement(servers[server_id]['key_s'], servers[server_id]['announcement'])
else:
eventually(self.storage_broker.got_static_announcement, servers[server_id]['key_s'], servers[server_id]['announcement'])
# check to see if we're supposed to use the introducer too
if self.get_config("client-server-selection", "use_introducer",
default=True, boolean=True):
sb.use_introducer(self.introducer_client)
sb.use_introducer(self.introducer_client)
def get_storage_broker(self):
return self.storage_broker