Skip to content
This repository has been archived by the owner on Apr 27, 2021. It is now read-only.

Commit

Permalink
use GAE lib_config for configuration, all config in settings.py
Browse files Browse the repository at this point in the history
  • Loading branch information
briandorsey committed Dec 28, 2012
1 parent 822bcae commit 08c0ab9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 21 deletions.
35 changes: 17 additions & 18 deletions main.py
Expand Up @@ -22,18 +22,22 @@
import jinja2
import webapp2
from apiclient.discovery import build
from google.appengine.api import lib_config
from google.appengine.api import memcache
from oauth2client.appengine import AppAssertionCredentials

SAMPLE_NAME = 'Instance timeout helper'
# Be careful, this application will delete instances unless they're tagged
# with one of the SAFE_TAGS below.
GCE_PROJECT_ID = 'replace-with-your-compute-engine-project-id'
TIMEOUT = 60 * 8 # in minutes, defaulting to 8 hours
SAFE_TAGS = "production safetag".lower().split()
# In pretend mode, deletes are only logged. Set this to False after you've
# double-checked the status page and you're ready to enable the deletes.
DRY_RUN = True

# To make configuration changes, update these values in settings.py
CONFIG_DEFAULTS = {
'DRY_RUN': True,
'GCE_PROJECT_ID': 'replace-with-your-compute-engine-project-id',
'SAFE_TAGS': ['production', 'safetag'],
'TIMEOUT': 60 * 8, # in minutes, defaulting to 8 hours
}
registry = lib_config.LibConfigRegistry('settings')
config = registry.register('main', CONFIG_DEFAULTS)

This comment has been minimized.

Copy link
@dhermes

dhermes Dec 28, 2012

Using lowercase as global is a bit scary, especially because you end up using it in MainHandler.get

config.SAFE_TAGS = [t.lower() for t in config.SAFE_TAGS]

# Obtain App Engine AppAssertion credentials and authorize HTTP connection.
# https://developers.google.com/appengine/docs/python/appidentity/overview
Expand All @@ -54,7 +58,7 @@ def annotate_instances(instances):
# set _excluded
excluded = False
for tag in instance.get('tags', []):
if tag.lower() in SAFE_TAGS:
if tag.lower() in config.SAFE_TAGS:
excluded = True
break
instance['_excluded'] = excluded
Expand All @@ -65,15 +69,15 @@ def annotate_instances(instances):
now = datetime.datetime.now()
delta = now - creation
instance['_age_hours'] = delta.seconds / 60
if delta.seconds > TIMEOUT * 60 and not instance['_excluded']:
if delta.seconds > config.TIMEOUT * 60 and not instance['_excluded']:
instance['_timeout_expired'] = True
else:
instance['_timeout_expired'] = False


def list_instances():
"""returns a list of dictionaries containing GCE instance data"""
request = compute.instances().list(project=GCE_PROJECT_ID)
request = compute.instances().list(project=config.GCE_PROJECT_ID)
response = request.execute()
instances = response.get('items', [])
annotate_instances(instances)
Expand All @@ -85,11 +89,6 @@ class MainHandler(webapp2.RequestHandler):
def get(self):
instances = list_instances()

config = {}
config['DRY_RUN'] = DRY_RUN
config['SAFE_TAGS'] = ', '.join(SAFE_TAGS)
config['TIMEOUT'] = TIMEOUT
config['GCE_PROJECT_ID'] = GCE_PROJECT_ID
data = {}
data['config'] = config
data['title'] = SAMPLE_NAME
Expand All @@ -112,11 +111,11 @@ def delete_expired_instances():

for instance in instances:
name = instance['name']
if DRY_RUN:
if config.DRY_RUN:
logging.info("DRY_RUN, not deleted: %s", name)
else:
logging.info("DELETE: %s", name)
request = compute.instances().delete(project=GCE_PROJECT_ID,
request = compute.instances().delete(project=config.GCE_PROJECT_ID,
instance=name)
response = request.execute()
logging.info(response)
Expand Down
13 changes: 13 additions & 0 deletions settings.py
@@ -0,0 +1,13 @@
# In DRY_RUN mode, deletes are only logged. Set this to False after you've
# double-checked the status page and you're ready to enable the deletes.
main_DRY_RUN = True

# Be careful, this application will delete all instances in this project
# unless they're tagged with one of the SAFE_TAGS below.
main_GCE_PROJECT_ID = 'replace-with-your-compute-engine-project-id'

# Instance tags which will never be deleted.
main_SAFE_TAGS = ['production', 'safetag']

# Instances are deleted after they have been running for TIMEOUT minutes.
main_TIMEOUT = 60 * 8 # in minutes, defaulting to 8 hours
9 changes: 6 additions & 3 deletions templates/index.html
Expand Up @@ -21,9 +21,12 @@
<body>
<h1>{{title}}</h1>
<h2>Configuration:</h2>
{% for key, value in config.items() %}
<li>{{key}} : {{value}}
{% endfor %}
<ul>
<li>DRY_RUN: {{config.DRY_RUN}}
<li>GCE_PROJECT_ID: {{config.GCE_PROJECT_ID}}
<li>SAFE_TAGS: {{', '.join(config.SAFE_TAGS)}}
<li>TIMEOUT: {{config.TIMEOUT}}
</ul>
<h2>Instances:</h2>
<table>
<thead>
Expand Down

0 comments on commit 08c0ab9

Please sign in to comment.