github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/gubernator/main.py (about) 1 #!/usr/bin/env python 2 3 # Copyright 2016 The Kubernetes Authors. 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # you may not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 17 import json 18 import logging 19 import os 20 21 import yaml 22 import webapp2 23 from webapp2_extras import security 24 25 from google.appengine.api import app_identity 26 from google.appengine.api import modules 27 28 import github_auth 29 import view_base 30 import view_build 31 import view_logs 32 import view_pr 33 34 35 hostname = app_identity.get_default_version_hostname() 36 if 'testbed' not in os.environ.get('SERVER_SOFTWARE', 'testbed'): 37 current_version = modules.modules.get_current_version_name() 38 if modules.modules.get_default_version() != current_version: 39 hostname = '%s-dot-%s' % (current_version, hostname) 40 41 def get_secret(key): 42 data = json.load(open('secrets.json')) 43 return data[hostname][key] 44 45 46 def get_session_secret(): 47 try: 48 return str(get_secret('session')) 49 except (IOError, KeyError): 50 if hostname: # no scary error messages when testing 51 logging.error( 52 'unable to load secret key! sessions WILL NOT persist!') 53 # This fallback is enough for testing, but in production 54 # will end up invalidating sessions whenever a different instance 55 # is created. 56 return security.generate_random_string(entropy=256) 57 58 59 def get_github_client(): 60 try: 61 return get_secret('github_client') 62 except (IOError, KeyError): 63 if hostname: 64 logging.warning('no github client id found') 65 return None 66 67 68 def get_app_config(): 69 with open('config.yaml') as config_file: 70 return yaml.load(config_file) 71 72 config = { 73 'webapp2_extras.sessions': { 74 'secret_key': get_session_secret(), 75 'cookie_args': { 76 # we don't have SSL For local development 77 'secure': hostname and 'appspot.com' in hostname, 78 'httponly': True, 79 }, 80 }, 81 'github_client': get_github_client(), 82 } 83 84 config.update(get_app_config()) 85 86 class Warmup(webapp2.RequestHandler): 87 """Warms up gubernator.""" 88 def get(self): 89 """Receives the warmup request.""" 90 # TODO(fejta): warmup something useful 91 self.response.headers['Content-Type'] = 'text/plain' 92 self.response.write('Warmup successful') 93 94 app = webapp2.WSGIApplication([ 95 ('/_ah/warmup', Warmup), 96 (r'/', view_base.IndexHandler), 97 (r'/jobs/(.*)$', view_build.JobListHandler), 98 (r'/builds/(.*)/([^/]+)/?', view_build.BuildListHandler), 99 (r'/build/(.*)/([^/]+)/([-\da-f_:.]+)/?', view_build.BuildHandler), 100 (r'/build/(.*)/([^/]+)/([-\da-f_:.]+)/nodelog*', view_logs.NodeLogHandler), 101 (r'/pr((?:/[^/]+){0,2})/(\d+|batch)', view_pr.PRHandler), 102 (r'/pr/?', view_pr.PRDashboard), 103 (r'/pr/([-\w]+)', view_pr.PRDashboard), 104 (r'/pr/(.*/build-log.txt)', view_pr.PRBuildLogHandler), 105 (r'/github_auth(.*)', github_auth.Endpoint) 106 ], debug=True, config=config)