This repository has been archived by the owner on Feb 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.py
177 lines (148 loc) · 6.58 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import os
import json
import requests
import logging
import tornado.httpserver
import tornado.ioloop
import tornado.web
from tornado.options import define, options
from github import Github
from urlparse import urljoin
port = os.environ.get("PORT", 5000)
root = os.path.dirname(os.path.abspath(__file__))
define("host", default="api.mybinder.org", help="IP address for binder API endpoint")
class Building(tornado.web.RequestHandler):
"""
Show building status for an app
"""
def get(self, org, repo):
repo = org + "/" + repo
self.render('static/status/building.html', repo=repo, host=options.host)
class Validate(tornado.web.RequestHandler):
"""
Handle submissions by checking against repos on GitHub
"""
def post(self):
# setup a github user
github_username = os.environ['GITHUB_USERNAME']
github_pass = os.environ['GITHUB_PASS']
# get the submission and repo
self.set_header("Content-Type", "application/json")
submission = json.loads(self.get_argument("submission"))
repo = self.get_argument("repo")
deps = submission['dependencies']
# get the top-level contents of the repo
from github import UnknownObjectException, GithubException
gituser = Github(github_username, github_pass)
try:
gitrepo = gituser.get_repo(repo)
contents = gitrepo.get_dir_contents('/')
names = [c.name for c in contents]
def missing(filename):
if (deps == [filename]) and (filename not in names):
return True
else:
return False
# check for submission errors
response = {'success': True, 'msg': ''}
for name in ['requirements.txt', 'environment.yml', 'Dockerfile']:
if missing(name):
response = {'success': False, 'msg': "There's no <b>%s</b> in your repo" % name}
except UnknownObjectException, GithubException:
response = {'success': False, 'msg': 'Oops, that repo does not exist'}
self.write(response)
class Redirector(tornado.web.RequestHandler):
"""
Redirect calls to the Binder API depending on status
"""
def get(self, org, repo, location=None):
# strip trailing /
if repo[-1] == '/':
repo = repo[:-1]
# get locations
app_id = org + "/" + repo
baseurl = self.request.protocol + "://" + self.request.host
endpoint = 'http://' + options.host
try:
r = requests.get(urljoin(endpoint + '/apps/', app_id + '/status'))
if r.status_code == 404:
logging.info('cannot get status')
logging.info('sending missing message')
self.redirect(baseurl + '/status/missing.html')
if r.status_code == 200:
logging.info('status retrieved')
blob = r.json()
if 'build_status' in blob:
status = blob['build_status']
if status == 'failed':
logging.debug('sending failed message')
self.redirect(baseurl + '/status/failed.html')
if status == 'building':
logging.debug('sending build message')
self.render('static/status/building.html', repo=app_id, host=options.host)
if status == 'completed':
# check for capacity
r = requests.get(url=endpoint + '/capacity')
check = r.json()
if check['running'] >= check['capacity']:
self.render('static/status/capacity.html')
else:
try:
r = requests.get(url=urljoin(endpoint + '/apps/', app_id), timeout=(20.0, 20.0))
redirectblob = r.json()
if 'redirect_url' in redirectblob:
url = redirectblob['redirect_url']
if location is not None and location != '':
logging.debug('redirecting to: %s' % url + "/notebooks/" + location)
self.redirect(url + "/notebooks/" + location)
else:
logging.debug('redirecting to: %s' % url)
self.redirect(url)
else:
self.redirect(baseurl + '/status/unknown.html')
except Exception as e:
self.timeout_handler(e)
else:
self.redirect(baseurl + '/status/unknown.html')
except Exception as e:
self.error_handler(e)
def timeout_handler(self, e):
"""
Handler that checks for timeouts
(useful if we expect timeouts at particular stages)
"""
baseurl = self.request.protocol + "://" + self.request.host
logging.error(e)
self.redirect(baseurl + '/status/unknown.html')
def error_handler(self, e):
"""
Handler for generic errors
"""
baseurl = self.request.protocol + "://" + self.request.host
logging.error(e)
self.redirect(baseurl + '/status/unknown.html')
class CustomStatic(tornado.web.StaticFileHandler):
"""
Modified static handler to serve a custom 404 and remove caching
"""
def write_error(self, status_code, **kwargs):
baseurl = self.request.protocol + "://" + self.request.host
self.redirect(baseurl + '/status/404.html')
def set_extra_headers(self, path):
self.set_header('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0')
settings = {
"static_path": os.path.join(os.path.dirname(__file__), "static")
}
application = tornado.web.Application([
(r"/repo/(?P<org>[^\/]+)/(?P<repo>[^\/]+)/status", Building),
(r"/repo/(?P<org>[^\/]+)/(?P<repo>[^\/]+)/(?P<location>.*)", Redirector),
(r"/repo/(?P<org>[^\/]+)/(?P<repo>[^\/]+)", Redirector),
(r"/validate/", Validate),
(r"/(.*)", CustomStatic, {'path': root + "/static/", "default_filename": "index.html"})
], **settings)
if __name__ == "__main__":
tornado.log.enable_pretty_logging()
server = tornado.httpserver.HTTPServer(application)
server.bind(port)
server.start(0)
tornado.ioloop.IOLoop.current().start()