From 99df134b10a4edc701c896f83552c9885f5d0284 Mon Sep 17 00:00:00 2001 From: Chad Whitacre Date: Wed, 2 Apr 2014 11:51:51 -0400 Subject: [PATCH 1/7] Switch to new environment.py library for config I factored out a new library called environment.py to manage configuration from environment variables. This commit replaces our former usage of the envvar function with environment. The next step is ot replace all of our direct os.environ access. --- gittip/wireup.py | 157 ++++++++++++++++++-------------- requirements.txt | 1 + vendor/environment-1.0.0.tar.gz | Bin 0 -> 5336 bytes 3 files changed, 92 insertions(+), 66 deletions(-) create mode 100644 vendor/environment-1.0.0.tar.gz diff --git a/gittip/wireup.py b/gittip/wireup.py index 83808aca89..d34bc7240f 100644 --- a/gittip/wireup.py +++ b/gittip/wireup.py @@ -2,13 +2,13 @@ """ from __future__ import absolute_import, division, print_function, unicode_literals import os -import sys import aspen import balanced import gittip import raven import stripe +from environment import Environment, is_yesish from gittip.elsewhere import PlatformRegistry from gittip.elsewhere.bitbucket import Bitbucket from gittip.elsewhere.bountysource import Bountysource @@ -147,26 +147,7 @@ class BadEnvironment(SystemExit): def envvars(website): - - missing_keys = [] - malformed_values = [] - - def envvar(key, cast=None): - if key not in os.environ: - missing_keys.append(key) - return "" - value = os.environ[key].decode('ASCII') - if cast is not None: - try: - value = cast(value) - except: - err = str(sys.exc_info()[1]) - malformed_values.append((key, err)) - return "" - return value - - def is_yesish(val): - return val.lower() in ('1', 'true', 'yes') + env = _env() # Accounts Elsewhere @@ -174,43 +155,43 @@ def is_yesish(val): twitter = Twitter( website.db, - envvar('TWITTER_CONSUMER_KEY'), - envvar('TWITTER_CONSUMER_SECRET'), - envvar('TWITTER_CALLBACK'), + env.twitter_consumer_key, + env.twitter_consumer_secret, + env.twitter_callback, ) github = GitHub( website.db, - envvar('GITHUB_CLIENT_ID'), - envvar('GITHUB_CLIENT_SECRET'), - envvar('GITHUB_CALLBACK'), + env.github_client_id, + env.github_client_secret, + env.github_callback, ) bitbucket = Bitbucket( website.db, - envvar('BITBUCKET_CONSUMER_KEY'), - envvar('BITBUCKET_CONSUMER_SECRET'), - envvar('BITBUCKET_CALLBACK'), + env.bitbucket_consumer_key, + env.bitbucket_consumer_secret, + env.bitbucket_callback, ) openstreetmap = OpenStreetMap( website.db, - envvar('OPENSTREETMAP_CONSUMER_KEY'), - envvar('OPENSTREETMAP_CONSUMER_SECRET'), - envvar('OPENSTREETMAP_CALLBACK'), - envvar('OPENSTREETMAP_API_URL'), - envvar('OPENSTREETMAP_AUTH_URL'), + env.openstreetmap_consumer_key, + env.openstreetmap_consumer_secret, + env.openstreetmap_callback, + env.openstreetmap_api_url, + env.openstreetmap_auth_url, ) bountysource = Bountysource( website.db, None, - envvar('BOUNTYSOURCE_API_SECRET'), - envvar('BOUNTYSOURCE_CALLBACK'), - envvar('BOUNTYSOURCE_API_HOST'), - envvar('BOUNTYSOURCE_WWW_HOST'), + env.bountysource_api_secret, + env.bountysource_callback, + env.bountysource_api_host, + env.bountysource_www_host, ) venmo = Venmo( website.db, - envvar('VENMO_CLIENT_ID'), - envvar('VENMO_CLIENT_SECRET'), - envvar('VENMO_CALLBACK'), + env.venmo_client_id, + env.venmo_client_secret, + env.venmo_callback, ) signin_platforms = [twitter, github, bitbucket, openstreetmap] @@ -227,51 +208,93 @@ def is_yesish(val): # Other Stuff # =========== - website.asset_version_url = envvar('GITTIP_ASSET_VERSION_URL') \ - .replace('%version', website.version) - website.asset_url = envvar('GITTIP_ASSET_URL') - website.cache_static = is_yesish(envvar('GITTIP_CACHE_STATIC')) - website.compress_assets = is_yesish(envvar('GITTIP_COMPRESS_ASSETS')) - - website.google_analytics_id = envvar('GOOGLE_ANALYTICS_ID') - website.sentry_dsn = envvar('SENTRY_DSN') - - website.min_threads = envvar('MIN_THREADS', int) - website.log_busy_threads_every = envvar('LOG_BUSY_THREADS_EVERY', int) - website.log_metrics = is_yesish(envvar('LOG_METRICS')) + website.asset_version_url = env.gittip_asset_version_url.replace('%version', website.version) + website.asset_url = env.gittip_asset_url + website.cache_static = env.gittip_cache_static + website.compress_assets = env.gittip_compress_assets + + website.google_analytics_id = env.google_analytics_id + website.sentry_dsn = env.sentry_dsn + + website.min_threads = env.min_threads + website.log_busy_threads_every = env.log_busy_threads_every + website.log_metrics = env.log_metrics + + +def _env(): + env = Environment( + DATABASE_URL = unicode, + CANONICAL_HOST = unicode, + CANONICAL_SCHEME = unicode, + MIN_THREADS = int, + DATABASE_MAXCONN = unicode, + GITTIP_ASSET_VERSION_URL = unicode, + GITTIP_ASSET_URL = unicode, + GITTIP_CACHE_STATIC = is_yesish, + GITTIP_COMPRESS_ASSETS = is_yesish, + STRIPE_SECRET_API_KEY = unicode, + STRIPE_PUBLISHABLE_API_KEY = unicode, + BALANCED_API_SECRET = unicode, + DEBUG = unicode, + GITHUB_CLIENT_ID = unicode, + GITHUB_CLIENT_SECRET = unicode, + GITHUB_CALLBACK = unicode, + BITBUCKET_CONSUMER_KEY = unicode, + BITBUCKET_CONSUMER_SECRET = unicode, + BITBUCKET_CALLBACK = unicode, + TWITTER_CONSUMER_KEY = unicode, + TWITTER_CONSUMER_SECRET = unicode, + TWITTER_CALLBACK = unicode, + BOUNTYSOURCE_API_SECRET = unicode, + BOUNTYSOURCE_CALLBACK = unicode, + BOUNTYSOURCE_API_HOST = unicode, + BOUNTYSOURCE_WWW_HOST = unicode, + VENMO_CLIENT_ID = unicode, + VENMO_CLIENT_SECRET = unicode, + VENMO_CALLBACK = unicode, + OPENSTREETMAP_CONSUMER_KEY = unicode, + OPENSTREETMAP_CONSUMER_SECRET = unicode, + OPENSTREETMAP_CALLBACK = unicode, + OPENSTREETMAP_API_URL = unicode, + OPENSTREETMAP_AUTH_URL = unicode, + NANSWERS_THRESHOLD = unicode, + UPDATE_HOMEPAGE_EVERY = unicode, + GOOGLE_ANALYTICS_ID = unicode, + SENTRY_DSN = unicode, + LOG_BUSY_THREADS_EVERY = int, + LOG_METRICS = is_yesish, + ) # Error Checking # ============== - if malformed_values: - malformed_values.sort() - these = len(malformed_values) != 1 and 'these' or 'this' - plural = len(malformed_values) != 1 and 's' or '' + if env.malformed: + these = len(env.malformed) != 1 and 'these' or 'this' + plural = len(env.malformed) != 1 and 's' or '' aspen.log_dammit("=" * 42) aspen.log_dammit( "Oh no! Gittip.com couldn't understand %s " % these , "environment variable%s:" % plural ) aspen.log_dammit(" ") - for key, err in malformed_values: + for key, err in env.malformed: aspen.log_dammit(" {} ({})".format(key, err)) aspen.log_dammit(" ") aspen.log_dammit("See ./default_local.env for hints.") aspen.log_dammit("=" * 42) - keys = ', '.join([key for key in malformed_values]) + keys = ', '.join([key for key in env.malformed]) raise BadEnvironment("Malformed envvar{}: {}.".format(plural, keys)) - if missing_keys: - missing_keys.sort() - these = len(missing_keys) != 1 and 'these' or 'this' - plural = len(missing_keys) != 1 and 's' or '' + if env.missing: + these = len(env.missing) != 1 and 'these' or 'this' + plural = len(env.missing) != 1 and 's' or '' aspen.log_dammit("=" * 42) aspen.log_dammit( "Oh no! Gittip.com needs %s missing " % these , "environment variable%s:" % plural ) aspen.log_dammit(" ") - for key in missing_keys: + for key in env.missing: aspen.log_dammit(" " + key) aspen.log_dammit(" ") aspen.log_dammit( "(Sorry, we must've started looking for " @@ -284,5 +307,7 @@ def is_yesish(val): aspen.log_dammit("See ./default_local.env for hints.") aspen.log_dammit("=" * 42) - keys = ', '.join([key for key in missing_keys]) + keys = ', '.join([key for key in env.missing]) raise BadEnvironment("Missing envvar{}: {}.".format(plural, keys)) + + return env diff --git a/requirements.txt b/requirements.txt index 28bc95e2a2..a3d61d550e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -40,3 +40,4 @@ ./vendor/honcho-0.5.0.tar.gz ./vendor/vcrpy-0.6.0.tar.gz +./vendor/environment-1.0.0.tar.gz diff --git a/vendor/environment-1.0.0.tar.gz b/vendor/environment-1.0.0.tar.gz new file mode 100644 index 0000000000000000000000000000000000000000..a09cf4b629981c48620ac6b94e543464262f23a5 GIT binary patch literal 5336 zcmV;}6esH+iwFoWEId;J|72-%bT4IYc4=~NZf#|5bS*J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env function and used it under the hood in wireup to replace some older code. This commit brings configure-aspen into the mix. By introducing the env object into configure-aspen I'm setting us up to replace os.environ calls in other wireup functions with the env object. --- configure-aspen.py | 5 ++++- gittip/wireup.py | 13 +++---------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/configure-aspen.py b/configure-aspen.py index a31bd7f967..7f06264fe7 100644 --- a/configure-aspen.py +++ b/configure-aspen.py @@ -15,6 +15,7 @@ from aspen import log_dammit + # Wireup Algorithm # ================ @@ -36,12 +37,14 @@ } +env = gittip.wireup.env() gittip.wireup.canonical() website.db = gittip.wireup.db() gittip.wireup.billing() gittip.wireup.username_restrictions(website) gittip.wireup.nanswers() -gittip.wireup.envvars(website) +gittip.wireup.accounts_elsewhere(website, env) +gittip.wireup.other_stuff(website, env) tell_sentry = gittip.wireup.make_sentry_teller(website) # The homepage wants expensive queries. Let's periodically select into an diff --git a/gittip/wireup.py b/gittip/wireup.py index d34bc7240f..eaa68ddd76 100644 --- a/gittip/wireup.py +++ b/gittip/wireup.py @@ -146,12 +146,7 @@ class BadEnvironment(SystemExit): pass -def envvars(website): - env = _env() - - - # Accounts Elsewhere - # ================== +def accounts_elsewhere(website, env): twitter = Twitter( website.db, @@ -205,9 +200,7 @@ def envvars(website): website.platforms = AccountElsewhere.platforms = PlatformRegistry(all_platforms) - # Other Stuff - # =========== - +def other_stuff(website, env): website.asset_version_url = env.gittip_asset_version_url.replace('%version', website.version) website.asset_url = env.gittip_asset_url website.cache_static = env.gittip_cache_static @@ -221,7 +214,7 @@ def envvars(website): website.log_metrics = env.log_metrics -def _env(): +def env(): env = Environment( DATABASE_URL = unicode, CANONICAL_HOST = unicode, From d351a7b6619c9fdd57f53a3a2ea043bc04cacce7 Mon Sep 17 00:00:00 2001 From: Chad Whitacre Date: Wed, 2 Apr 2014 14:09:12 -0400 Subject: [PATCH 3/7] Switch wireup.canonical to use env This was a little complicated because in test_hooks we do a dance with canonical, and I had to update that to work with env instead of os.environ directly. --- configure-aspen.py | 4 ++-- gittip/wireup.py | 6 +++--- tests/py/test_hooks.py | 22 ++++++++++++---------- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/configure-aspen.py b/configure-aspen.py index 7f06264fe7..098c292563 100644 --- a/configure-aspen.py +++ b/configure-aspen.py @@ -37,8 +37,8 @@ } -env = gittip.wireup.env() -gittip.wireup.canonical() +env = website.env = gittip.wireup.env() +gittip.wireup.canonical(env) website.db = gittip.wireup.db() gittip.wireup.billing() gittip.wireup.username_restrictions(website) diff --git a/gittip/wireup.py b/gittip/wireup.py index eaa68ddd76..f20bf2d859 100644 --- a/gittip/wireup.py +++ b/gittip/wireup.py @@ -22,9 +22,9 @@ from gittip.models import GittipDB -def canonical(): - gittip.canonical_scheme = os.environ['CANONICAL_SCHEME'] - gittip.canonical_host = os.environ['CANONICAL_HOST'] +def canonical(env): + gittip.canonical_scheme = env.canonical_scheme + gittip.canonical_host = env.canonical_host def db(): diff --git a/tests/py/test_hooks.py b/tests/py/test_hooks.py index 0fa9157346..de589f8fe9 100644 --- a/tests/py/test_hooks.py +++ b/tests/py/test_hooks.py @@ -1,9 +1,9 @@ from __future__ import absolute_import, division, print_function, unicode_literals -import os from gittip import wireup from gittip.testing import Harness from gittip.models.participant import Participant +from environment import Environment class Tests(Harness): @@ -11,18 +11,20 @@ class Tests(Harness): def setUp(self): Harness.setUp(self) - self._blech = ( os.environ['CANONICAL_SCHEME'] - , os.environ['CANONICAL_HOST'] - ) - os.environ['CANONICAL_SCHEME'] = 'https' - os.environ['CANONICAL_HOST'] = 'www.gittip.com' - wireup.canonical() + # Grab configuration from os.environ, storing for later. + env = Environment(CANONICAL_SCHEME=unicode, CANONICAL_HOST=unicode) + self.environ = env.environ + + # Change env, doesn't change self.environ. + env.canonical_scheme = 'https' + env.canonical_host = 'www.gittip.com' + + wireup.canonical(env) def tearDown(self): Harness.tearDown(self) - os.environ['CANONICAL_SCHEME'] = self._blech[0] - os.environ['CANONICAL_HOST'] = self._blech[1] - wireup.canonical() + reset = Environment(CANONICAL_SCHEME=unicode, CANONICAL_HOST=unicode, environ=self.environ) + wireup.canonical(reset) def test_canonize_canonizes(self): From fceff3ef33ab60dc2bddef209d881ea39e458845 Mon Sep 17 00:00:00 2001 From: Chad Whitacre Date: Wed, 2 Apr 2014 15:03:25 -0400 Subject: [PATCH 4/7] Remove os.environ from configure-aspen.py This removes the instances of os.environ that are actually in configure-aspen.py. There are still instances in wireup that are called from configure-aspen. --- configure-aspen.py | 5 ++--- gittip/wireup.py | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/configure-aspen.py b/configure-aspen.py index 098c292563..982d24e8de 100644 --- a/configure-aspen.py +++ b/configure-aspen.py @@ -20,8 +20,7 @@ # ================ version_file = os.path.join(website.www_root, 'version.txt') -__version__ = open(version_file).read().strip() -website.version = os.environ['__VERSION__'] = __version__ +website.version = open(version_file).read().strip() website.renderer_default = "jinja2" @@ -50,7 +49,7 @@ # The homepage wants expensive queries. Let's periodically select into an # intermediate table. -UPDATE_HOMEPAGE_EVERY = int(os.environ['UPDATE_HOMEPAGE_EVERY']) +UPDATE_HOMEPAGE_EVERY = env.update_homepage_every def update_homepage_queries(): from gittip import utils while 1: diff --git a/gittip/wireup.py b/gittip/wireup.py index f20bf2d859..7be7553ba7 100644 --- a/gittip/wireup.py +++ b/gittip/wireup.py @@ -251,7 +251,7 @@ def env(): OPENSTREETMAP_API_URL = unicode, OPENSTREETMAP_AUTH_URL = unicode, NANSWERS_THRESHOLD = unicode, - UPDATE_HOMEPAGE_EVERY = unicode, + UPDATE_HOMEPAGE_EVERY = int, GOOGLE_ANALYTICS_ID = unicode, SENTRY_DSN = unicode, LOG_BUSY_THREADS_EVERY = int, From f2b2a702915f35544a7c8bcba967a28fbb5839fc Mon Sep 17 00:00:00 2001 From: Chad Whitacre Date: Wed, 2 Apr 2014 15:05:07 -0400 Subject: [PATCH 5/7] Remove os.environ from wireup.nanswers --- configure-aspen.py | 2 +- gittip/wireup.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/configure-aspen.py b/configure-aspen.py index 982d24e8de..a3a33c7e5b 100644 --- a/configure-aspen.py +++ b/configure-aspen.py @@ -41,7 +41,7 @@ website.db = gittip.wireup.db() gittip.wireup.billing() gittip.wireup.username_restrictions(website) -gittip.wireup.nanswers() +gittip.wireup.nanswers(env) gittip.wireup.accounts_elsewhere(website, env) gittip.wireup.other_stuff(website, env) tell_sentry = gittip.wireup.make_sentry_teller(website) diff --git a/gittip/wireup.py b/gittip/wireup.py index 7be7553ba7..aad7fe956d 100644 --- a/gittip/wireup.py +++ b/gittip/wireup.py @@ -137,9 +137,9 @@ def tell_sentry(exception, request=None): return tell_sentry -def nanswers(): +def nanswers(env): from gittip.models import participant - participant.NANSWERS_THRESHOLD = int(os.environ['NANSWERS_THRESHOLD']) + participant.NANSWERS_THRESHOLD = env.nanswers_threshold class BadEnvironment(SystemExit): @@ -250,7 +250,7 @@ def env(): OPENSTREETMAP_CALLBACK = unicode, OPENSTREETMAP_API_URL = unicode, OPENSTREETMAP_AUTH_URL = unicode, - NANSWERS_THRESHOLD = unicode, + NANSWERS_THRESHOLD = int, UPDATE_HOMEPAGE_EVERY = int, GOOGLE_ANALYTICS_ID = unicode, SENTRY_DSN = unicode, From 91bf606a9326b6a0014d7e04d3a02e1aa257924c Mon Sep 17 00:00:00 2001 From: Chad Whitacre Date: Wed, 2 Apr 2014 15:10:23 -0400 Subject: [PATCH 6/7] Remove os.environ from wireup.db --- configure-aspen.py | 2 +- gittip/cli.py | 6 +++--- gittip/wireup.py | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/configure-aspen.py b/configure-aspen.py index a3a33c7e5b..17583d181b 100644 --- a/configure-aspen.py +++ b/configure-aspen.py @@ -38,7 +38,7 @@ env = website.env = gittip.wireup.env() gittip.wireup.canonical(env) -website.db = gittip.wireup.db() +website.db = gittip.wireup.db(env) gittip.wireup.billing() gittip.wireup.username_restrictions(website) gittip.wireup.nanswers(env) diff --git a/gittip/cli.py b/gittip/cli.py index 109062e327..6d1badd077 100644 --- a/gittip/cli.py +++ b/gittip/cli.py @@ -1,6 +1,5 @@ """This is installed as `payday`. """ -import os from gittip import wireup @@ -13,8 +12,9 @@ def payday(): # connection it's easier to trust changes to statement_timeout. The point # here is that we want to turn off statement_timeout for payday. - os.environ['DATABASE_MAXCONN'] = '1' - db = wireup.db() + env = wireup.env() + env.database_maxconn = 1 + db = wireup.db(env) db.run("SET statement_timeout = 0") wireup.billing() diff --git a/gittip/wireup.py b/gittip/wireup.py index aad7fe956d..91570a94a8 100644 --- a/gittip/wireup.py +++ b/gittip/wireup.py @@ -27,9 +27,9 @@ def canonical(env): gittip.canonical_host = env.canonical_host -def db(): - dburl = os.environ['DATABASE_URL'] - maxconn = int(os.environ['DATABASE_MAXCONN']) +def db(env): + dburl = env.database_url + maxconn = env.database_maxconn db = GittipDB(dburl, maxconn=maxconn) db.register_model(Community) @@ -220,7 +220,7 @@ def env(): CANONICAL_HOST = unicode, CANONICAL_SCHEME = unicode, MIN_THREADS = int, - DATABASE_MAXCONN = unicode, + DATABASE_MAXCONN = int, GITTIP_ASSET_VERSION_URL = unicode, GITTIP_ASSET_URL = unicode, GITTIP_CACHE_STATIC = is_yesish, From a3100a98605fc0a0e0ed9dda0fe9b4ea4b8152dd Mon Sep 17 00:00:00 2001 From: Chad Whitacre Date: Wed, 2 Apr 2014 15:45:55 -0400 Subject: [PATCH 7/7] Take out configure_payments hack With the upgrade to the balanced library in #2036 we no longer need this workaround for https://github.com/balanced/balanced-python/issues/5. Removing at this point because I'm ripping out all of our direct os.environ usage and this applies. --- configure-aspen.py | 3 +-- gittip/__init__.py | 7 ------- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/configure-aspen.py b/configure-aspen.py index 17583d181b..5d9bda7b5a 100644 --- a/configure-aspen.py +++ b/configure-aspen.py @@ -8,7 +8,7 @@ import gittip import gittip.wireup -from gittip import canonize, configure_payments +from gittip import canonize from gittip.security import authentication, csrf, x_frame_options from gittip.utils import cache_static, timer @@ -145,7 +145,6 @@ def scab_body_onto_response(response): , algorithm['raise_200_for_OPTIONS'] , canonize - , configure_payments , authentication.inbound , csrf.inbound , add_stuff_to_context diff --git a/gittip/__init__.py b/gittip/__init__.py index 0df7c4fb23..b37014192f 100644 --- a/gittip/__init__.py +++ b/gittip/__init__.py @@ -2,7 +2,6 @@ """ import datetime import locale -import os from decimal import Decimal import aspen @@ -93,12 +92,6 @@ def canonize(request): request.redirect(url) -def configure_payments(request): - # Work-around for https://github.com/balanced/balanced-python/issues/5 - import balanced - balanced.configure(os.environ['BALANCED_API_SECRET']) - - def outbound(request, response, website): version = website.version