Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
refactor: settings -> conf
Browse files Browse the repository at this point in the history
Issue #632
  • Loading branch information
pjenvey committed Jul 31, 2017
1 parent e53a2ed commit 24c0ec8
Show file tree
Hide file tree
Showing 26 changed files with 241 additions and 248 deletions.
24 changes: 11 additions & 13 deletions autopush/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import autopush.db as db
from autopush.exceptions import (
InvalidSettings,
InvalidConfig,
InvalidTokenException,
VapidAuthException
)
Expand Down Expand Up @@ -233,22 +233,21 @@ def from_argparse(cls, ns, **kwargs):
try:
router_conf["apns"] = json.loads(ns.apns_creds)
except (ValueError, TypeError):
raise InvalidSettings(
raise InvalidConfig(
"Invalid JSON specified for APNS config options")
if ns.gcm_enabled:
# Create a common gcmclient
try:
sender_ids = json.loads(ns.senderid_list)
except (ValueError, TypeError):
raise InvalidSettings(
"Invalid JSON specified for senderid_list")
raise InvalidConfig("Invalid JSON specified for senderid_list")
try:
# This is an init check to verify that things are
# configured correctly. Otherwise errors may creep in
# later that go unaccounted.
sender_ids[sender_ids.keys()[0]]
except (IndexError, TypeError):
raise InvalidSettings("No GCM SenderIDs specified or found.")
raise InvalidConfig("No GCM SenderIDs specified or found.")
router_conf["gcm"] = {"ttl": ns.gcm_ttl,
"dryrun": ns.gcm_dryrun,
"max_data": ns.max_data,
Expand All @@ -261,31 +260,30 @@ def from_argparse(cls, ns, **kwargs):
try:
client_certs_arg = json.loads(ns.client_certs)
except (ValueError, TypeError):
raise InvalidSettings(
"Invalid JSON specified for client_certs")
raise InvalidConfig("Invalid JSON specified for client_certs")
if client_certs_arg:
if not ns.ssl_key:
raise InvalidSettings("client_certs specified without SSL "
"enabled (no ssl_key specified)")
raise InvalidConfig("client_certs specified without SSL "
"enabled (no ssl_key specified)")
client_certs = {}
for name, sigs in client_certs_arg.iteritems():
if not isinstance(sigs, list):
raise InvalidSettings(
raise InvalidConfig(
"Invalid JSON specified for client_certs")
for sig in sigs:
sig = sig.upper()
if (not name or not CLIENT_SHA256_RE.match(sig) or
sig in client_certs):
raise InvalidSettings(
raise InvalidConfig(
"Invalid client_certs argument")
client_certs[sig] = name

if ns.fcm_enabled:
# Create a common gcmclient
if not ns.fcm_auth:
raise InvalidSettings("No Authorization Key found for FCM")
raise InvalidConfig("No Authorization Key found for FCM")
if not ns.fcm_senderid:
raise InvalidSettings("No SenderID found for FCM")
raise InvalidConfig("No SenderID found for FCM")
router_conf["fcm"] = {"ttl": ns.fcm_ttl,
"dryrun": ns.fcm_dryrun,
"max_data": ns.max_data,
Expand Down
10 changes: 5 additions & 5 deletions autopush/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -885,14 +885,14 @@ def __attrs_post_init__(self):
)

@classmethod
def from_config(cls, settings, **kwargs):
def from_config(cls, conf, **kwargs):
# type: (AutopushConfig, **Any) -> DatabaseManager
"""Create a DatabaseManager from the given config"""
metrics = autopush.metrics.from_config(settings)
metrics = autopush.metrics.from_config(conf)
return cls(
storage_conf=settings.storage_table,
router_conf=settings.router_table,
message_conf=settings.message_table,
storage_conf=conf.storage_table,
router_conf=conf.router_table,
message_conf=conf.message_table,
metrics=metrics,
**kwargs
)
Expand Down
10 changes: 5 additions & 5 deletions autopush/diagnostic_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ class EndpointDiagnosticCLI(object):

def __init__(self, sysargs, use_files=True):
ns = self._load_args(sysargs, use_files)
self._settings = settings = AutopushConfig.from_argparse(ns)
settings.statsd_host = None
self.db = DatabaseManager.from_config(settings)
self.db.setup(settings.preflight_uaid)
self._conf = conf = AutopushConfig.from_argparse(ns)
conf.statsd_host = None
self.db = DatabaseManager.from_config(conf)
self.db.setup(conf.preflight_uaid)
self._endpoint = ns.endpoint
self._pp = pprint.PrettyPrinter(indent=4)

Expand Down Expand Up @@ -54,7 +54,7 @@ def run(self):
md = match.groupdict()
api_ver, token = md.get("api_ver", "v1"), md["token"]

parsed = self._settings.parse_endpoint(
parsed = self._conf.parse_endpoint(
self.db.metrics,
token=token,
version=api_ver,
Expand Down
2 changes: 1 addition & 1 deletion autopush/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,5 @@ class LogCheckError(Exception):
pass


class InvalidSettings(Exception):
class InvalidConfig(Exception):
"""Error in initialization of AutopushConfig"""
12 changes: 6 additions & 6 deletions autopush/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def for_handler(cls,
"""Create a cyclone app around a specific handler_cls for tests.
Creates an uninitialized (no setup() called) DatabaseManager
from settings if one isn't specified.
from conf if one isn't specified.
handler_cls must be included in ap_handlers or a ValueError is
thrown.
Expand Down Expand Up @@ -175,8 +175,8 @@ def ssl_cf(self):
values.
"""
settings = self.conf
return settings.ssl.cf(require_peer_certs=settings.enable_tls_auth)
conf = self.conf
return conf.ssl.cf(require_peer_certs=conf.enable_tls_auth)

@classmethod
def _for_handler(cls, conf, db, routers=None, **kwargs):
Expand Down Expand Up @@ -238,11 +238,11 @@ class QuietClientFactory(_HTTP11ClientFactory):
noisy = False


def agent_from_config(settings):
def agent_from_config(conf):
# type: (AutopushConfig) -> Agent
"""Create a twisted.web.client Agent from the given config"""
# Use a persistent connection pool for HTTP requests.
pool = HTTPConnectionPool(reactor)
if not settings.debug:
if not conf.debug:
pool._factory = QuietClientFactory
return Agent(reactor, connectTimeout=settings.connect_timeout, pool=pool)
return Agent(reactor, connectTimeout=conf.connect_timeout, pool=pool)
63 changes: 31 additions & 32 deletions autopush/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import autopush.logging as logging
from autopush.config import AutopushConfig
from autopush.db import DatabaseManager
from autopush.exceptions import InvalidSettings
from autopush.exceptions import InvalidConfig
from autopush.haproxy import HAProxyServerEndpoint
from autopush.logging import PushLogger
from autopush.main_argparse import parse_connection, parse_endpoint
Expand Down Expand Up @@ -58,12 +58,12 @@ class AutopushMultiService(MultiService):

THREAD_POOL_SIZE = 50

def __init__(self, settings):
def __init__(self, conf):
# type: (AutopushConfig) -> None
super(AutopushMultiService, self).__init__()
self.settings = settings
self.db = DatabaseManager.from_config(settings)
self.agent = agent_from_config(settings)
self.conf = conf
self.db = DatabaseManager.from_config(conf)
self.agent = agent_from_config(conf)

@staticmethod
def parse_args(config_files, args):
Expand Down Expand Up @@ -91,9 +91,9 @@ def add_timer(self, *args, **kwargs):

def add_memusage(self):
"""Add the memusage Service"""
factory = MemUsageHTTPFactory(self.settings, None)
factory = MemUsageHTTPFactory(self.conf, None)
self.addService(
TCPServer(self.settings.memusage_port, factory, reactor=reactor))
TCPServer(self.conf.memusage_port, factory, reactor=reactor))

def run(self):
"""Start the services and run the reactor"""
Expand All @@ -112,13 +112,13 @@ def _from_argparse(cls, ns, **kwargs):
"""Create an instance from argparse/additional kwargs"""
# Add some entropy to prevent potential conflicts.
postfix = os.urandom(4).encode('hex').ljust(8, '0')
settings = AutopushConfig.from_argparse(
conf = AutopushConfig.from_argparse(
ns,
debug=ns.debug,
preflight_uaid="deadbeef000000000deadbeef" + postfix,
**kwargs
)
return cls(settings)
return cls(conf)

@classmethod
def main(cls, args=None, use_files=True):
Expand All @@ -141,7 +141,7 @@ def main(cls, args=None, use_files=True):
)
try:
app = cls.from_argparse(ns)
except InvalidSettings as e:
except InvalidConfig as e:
log.critical(str(e))
return 1

Expand All @@ -164,16 +164,16 @@ class EndpointApplication(AutopushMultiService):

endpoint_factory = EndpointHTTPFactory

def __init__(self, settings):
def __init__(self, conf):
# type: (AutopushConfig) -> None
super(EndpointApplication, self).__init__(settings)
self.routers = routers_from_config(settings, self.db, self.agent)
super(EndpointApplication, self).__init__(conf)
self.routers = routers_from_config(conf, self.db, self.agent)

def setup(self, rotate_tables=True):
self.db.setup(self.settings.preflight_uaid)
self.db.setup(self.conf.preflight_uaid)

self.add_endpoint()
if self.settings.memusage_port:
if self.conf.memusage_port:
self.add_memusage()

# Start the table rotation checker/updater
Expand All @@ -182,18 +182,18 @@ def setup(self, rotate_tables=True):

def add_endpoint(self):
"""Start the Endpoint HTTP router"""
settings = self.settings
conf = self.conf

factory = self.endpoint_factory(settings, self.db, self.routers)
factory.protocol.maxData = settings.max_data
factory = self.endpoint_factory(conf, self.db, self.routers)
factory.protocol.maxData = conf.max_data
factory.add_health_handlers()
ssl_cf = factory.ssl_cf()
self.add_maybe_ssl(settings.port, factory, ssl_cf)
self.add_maybe_ssl(conf.port, factory, ssl_cf)

if settings.proxy_protocol_port:
if conf.proxy_protocol_port:
ep = HAProxyServerEndpoint(
reactor,
settings.proxy_protocol_port,
conf.proxy_protocol_port,
ssl_cf
)
self.addService(StreamServerEndpointService(ep, factory))
Expand Down Expand Up @@ -230,16 +230,16 @@ class ConnectionApplication(AutopushMultiService):
websocket_factory = PushServerFactory
websocket_site_factory = ConnectionWSSite

def __init__(self, settings):
def __init__(self, conf):
# type: (AutopushConfig) -> None
super(ConnectionApplication, self).__init__(settings)
super(ConnectionApplication, self).__init__(conf)
self.clients = {} # type: Dict[str, PushServerProtocol]

def setup(self, rotate_tables=True):
self.db.setup(self.settings.preflight_uaid)
self.db.setup(self.conf.preflight_uaid)

self.add_internal_router()
if self.settings.memusage_port:
if self.conf.memusage_port:
self.add_memusage()

self.add_websocket()
Expand All @@ -251,18 +251,17 @@ def setup(self, rotate_tables=True):
def add_internal_router(self):
"""Start the internal HTTP notification router"""
factory = self.internal_router_factory(
self.settings, self.db, self.clients)
self.conf, self.db, self.clients)
factory.add_health_handlers()
self.add_maybe_ssl(self.settings.router_port, factory,
factory.ssl_cf())
self.add_maybe_ssl(self.conf.router_port, factory, factory.ssl_cf())

def add_websocket(self):
"""Start the public WebSocket server"""
settings = self.settings
ws_factory = self.websocket_factory(settings, self.db, self.agent,
conf = self.conf
ws_factory = self.websocket_factory(conf, self.db, self.agent,
self.clients)
site_factory = self.websocket_site_factory(settings, ws_factory)
self.add_maybe_ssl(settings.port, site_factory, site_factory.ssl_cf())
site_factory = self.websocket_site_factory(conf, ws_factory)
self.add_maybe_ssl(conf.port, site_factory, site_factory.ssl_cf())

@classmethod
def from_argparse(cls, ns):
Expand Down
18 changes: 9 additions & 9 deletions autopush/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,18 @@ def timing(self, name, duration, **kwargs):
host=self._host, **kwargs)


def from_config(settings):
def from_config(conf):
# type: (AutopushConfig) -> IMetrics
"""Create an IMetrics from the given config"""
if settings.datadog_api_key:
if conf.datadog_api_key:
return DatadogMetrics(
hostname=get_ec2_instance_id() if settings.ami_id else
settings.hostname,
api_key=settings.datadog_api_key,
app_key=settings.datadog_app_key,
flush_interval=settings.datadog_flush_interval,
hostname=get_ec2_instance_id() if conf.ami_id else
conf.hostname,
api_key=conf.datadog_api_key,
app_key=conf.datadog_app_key,
flush_interval=conf.datadog_flush_interval,
)
elif settings.statsd_host:
return TwistedMetrics(settings.statsd_host, settings.statsd_port)
elif conf.statsd_host:
return TwistedMetrics(conf.statsd_host, conf.statsd_port)
else:
return SinkMetrics()
14 changes: 7 additions & 7 deletions autopush/router/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@
"SimpleRouter"]


def routers_from_config(settings, db, agent):
def routers_from_config(conf, db, agent):
# type: (AutopushConfig, DatabaseManager, Agent) -> Dict[str, IRouter]
"""Create a dict of IRouters for the given config"""
router_conf = settings.router_conf
router_conf = conf.router_conf
routers = dict(
webpush=WebPushRouter(settings, None, db, agent)
webpush=WebPushRouter(conf, None, db, agent)
)
if settings.enable_simplepush:
if conf.enable_simplepush:
routers['simplepush'] = SimpleRouter(
settings, router_conf.get("simplepush"), db, agent)
conf, router_conf.get("simplepush"), db, agent)
if 'apns' in router_conf:
routers["apns"] = APNSRouter(settings, router_conf["apns"], db.metrics)
routers["apns"] = APNSRouter(conf, router_conf["apns"], db.metrics)
if 'gcm' in router_conf:
routers["gcm"] = GCMRouter(settings, router_conf["gcm"], db.metrics)
routers["gcm"] = GCMRouter(conf, router_conf["gcm"], db.metrics)
return routers
4 changes: 2 additions & 2 deletions autopush/router/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ def __init__(self, status_code=200, response_body="", router_data=None,


class IRouter(object):
def __init__(self, settings, router_conf, **kwargs):
def __init__(self, conf, router_conf, **kwargs):
"""Initialize the Router to handle notifications and registrations with
the given settings and router conf."""
the given conf and router conf."""
raise NotImplementedError("__init__ must be implemented")

def register(self, uaid, router_data, app_id, *args, **kwargs):
Expand Down
6 changes: 3 additions & 3 deletions autopush/tests/test_diagnostic_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ def test_bad_endpoint(self):

@patch("autopush.diagnostic_cli.AutopushConfig")
@patch("autopush.diagnostic_cli.DatabaseManager.from_config")
def test_successfull_lookup(self, mock_db_cstr, mock_settings_class):
def test_successfull_lookup(self, mock_db_cstr, mock_conf_class):
from autopush.diagnostic_cli import run_endpoint_diagnostic_cli
mock_settings_class.return_value = mock_settings = Mock()
mock_settings.parse_endpoint.return_value = dict(
mock_conf_class.return_value = mock_conf = Mock()
mock_conf.parse_endpoint.return_value = dict(
uaid="asdf", chid="asdf")

mock_db_cstr.return_value = mock_db = Mock()
Expand Down
Loading

0 comments on commit 24c0ec8

Please sign in to comment.