4

I'm working on an App Engine project (Python) where we'd like to make certain changes to the app's behavior when debugging/developing (most often locally). For example, when debugging, we'd like to disable our rate-limiting decorators, turn on the debug param in the WSGIApplication, maybe add some asserts.

As far as I can tell, App Engine doesn't naturally have any concept of a global dev-mode or debug-mode, so I'm wondering how best to implement such a mode. The options I've been able to come up with so far:

  1. Use google.appengine.api.app_identity.get_default_version_hostname() to get the hostname and check if it begins with localhost. This seems... unreliable, and doesn't allow for using the debug mode in a deployed app instance.

  2. Use os.environ.get('APPLICATION_ID') to get the application id, which according to this page is automatically prepended with dev~ by the development server. Worryingly, the very source of this information is in a box warning:

    Do not get the App ID from the environment variable. The development server simulates the production App Engine service. One way in which it does this is to prepend a string (dev~) to the APPLICATION_ID environment variable, which is similar to the string prepended in production for applications using the High Replication Datastore. You can modify this behavior with the --default_partition flag, choosing a value of "" to match the master-slave option in production. Google recommends always getting the application ID using get_application_id, as described above.

    Not sure if this is an acceptable use of the environment variable. Either way it's probably equally hacky, and suffers the same problem of only working with a locally running instance.

  3. Use a custom app-id for development (locally and deployed), use the -A flag in dev_appserver.py, and use google.appengine.api.app_identity.get_application_id() in the code. I don't like this for a number of reasons (namely having to have two separate app engine projects).

  4. Use a dev app engine version for development and detect with os.environ.get('CURRENT_VERSION_ID').split('.')[0] in code. When deployed this is easy, but I'm not sure how to make dev_appserver.py use a custom version without modifying app.yaml. I suppose I could sed app.yaml to a temp file in /tmp/ with the version replaced and the relative paths resolved (or just create a persistent dev-app.yaml), then pass that into dev_appserver.py. But that seems also kinda dirty and prone to error/sync issues.

Am I missing any other approaches? Any considerations I failed to acknowledge? Any other advice?

0x24a537r9
  • 968
  • 1
  • 10
  • 24
  • you can specify the app.yaml file you want to use when using dev_appserver.py just by specifying it on the command line. I have, for example, local SQL settings in development.yaml but when deployed it automatically switches to app.yaml – Paul Collingwood Aug 13 '14 at 08:12
  • Yep, that's assumed as part of #4. – 0x24a537r9 Aug 13 '14 at 08:15
  • Why does option 1 "seem" unreliable. And how does that preclude using debug mode. Surely the control of debug mode would be independent of where it's running dev or production? – Tim Hoffman Aug 13 '14 at 10:44
  • 1
    why not use just the answer here: http://stackoverflow.com/questions/1916579/in-python-how-can-i-test-if-im-in-google-app-engine-sdk – Paul Collingwood Aug 13 '14 at 11:02
  • @TimHoffman: I guess I just don't know how the app is run in prod and method #1 defines dev-mode in terms of where it's running. I agree it should be independent, though. – 0x24a537r9 Aug 16 '14 at 05:51
  • @PaulCollingwood: That's probably the most correct if I'm ok with not supporting a deployed dev mode. Thanks! – 0x24a537r9 Aug 16 '14 at 05:52

1 Answers1

3

In regards to "detecting" localhost development we use the following in our applications settings / config file.

IS_DEV_APPSERVER = 'development' in os.environ.get('SERVER_SOFTWARE', '').lower()

That used in conjunction with the debug flag should do the trick for you.

Jesse
  • 8,223
  • 6
  • 49
  • 81