-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathdhbox.py
378 lines (317 loc) · 13 KB
/
dhbox.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
import os, os.path, random, string, time, urllib2
from flask import Flask, flash, request, redirect, url_for, render_template, \
make_response, abort
import ast
from flask_sqlalchemy import SQLAlchemy
from flask_security import Security, SQLAlchemyUserDatastore, login_user, logout_user, \
UserMixin, RoleMixin, login_required, roles_required, current_user, LoginForm
from flaskext.markdown import Markdown
from wtforms.validators import DataRequired
from wtforms import TextField, Form
from werkzeug import generate_password_hash, check_password_hash
from werkzeug.contrib.fixers import ProxyFix
import schedule
from threading import Thread
import DockerBackend
# create application
app = Flask('dhbox')
app.wsgi_app = ProxyFix(app.wsgi_app)
Markdown(app)
# install_secret_key(app)
app.config.from_pyfile('config.cfg')
# app.template_folder = 'src/templates' if app.config['TESTING'] else 'dist/templates'
# app.static_folder = 'src/static' if app.config['TESTING'] else 'dist/static'
app.template_folder = 'dist/templates'
app.static_folder = 'dist/static'
# Create database connection object
db = SQLAlchemy(app)
all_apps = [
{'name': 'mallet', 'wiki-page': 'MALLET', 'display-name': 'MALLET'},
{'name': 'ntlk', 'wiki-page': 'NLTK', 'display-name': 'NLTK'},
{'name': 'filemanager', 'port': '8081', 'wiki-page': 'manager', 'display-name': 'File Manager'},
{'name': 'bash', 'port': '4200', 'wiki-page': 'Bash-shell', 'display-name': 'Command Line', 'height': 500},
{'name': 'rstudio', 'port': '8787', 'wiki-page': 'R-Studio', 'display-name': 'R Studio'},
{'name': 'brackets', 'port': '4444', 'wiki-page': 'Brackets', 'display-name': 'Brackets'},
{'name': 'apache', 'port': '80', 'hide': True},
{'name': 'jupyter', 'port': '8888', 'wiki-page': 'ipython', 'display-name': 'Jupyter Notebooks'},
# {'name': 'wordpress', 'port': '80', 'wiki-page': 'wordpress', 'display-name': 'WordPress'}
# {'name': 'website', 'port': '4000', 'wiki-page': 'webpage', 'display-name': 'Your Site'}
]
def get_app(key):
for app in all_apps:
if app['name'] == key:
return app
"""
MODELS
"""
roles_users = db.Table('roles_users',
db.Column('user_id', db.Integer(), db.ForeignKey('user.id')),
db.Column('role_id', db.Integer(), db.ForeignKey('role.id')))
class Role(db.Model, RoleMixin):
id = db.Column(db.Integer(), primary_key=True)
name = db.Column(db.String(80), unique=True)
description = db.Column(db.String(255))
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(255), unique=True)
name = db.Column(db.String(255), unique=True)
pwdhash = db.Column(db.String(160))
active = db.Column(db.Boolean())
dhbox_duration = db.Column(db.Integer)
confirmed_at = db.Column(db.DateTime())
roles = db.relationship('Role', secondary=roles_users,
backref=db.backref('users', lazy='dynamic'))
def __init__(self, name, active, roles, email, password, dhbox_duration):
self.name = name
self.email = email.lower()
self.active = active
self.roles = roles
self.dhbox_duration = dhbox_duration
self.set_password(password)
def set_password(self, password):
self.pwdhash = generate_password_hash(password)
def check_password(self, password):
return check_password_hash(self.pwdhash, password)
class ExtendedLoginForm(LoginForm):
name = TextField('Name', [DataRequired()])
def validate(self):
rv = Form.validate(self)
if not rv:
return False
user = User.query.filter_by(
name=self.name.data).first()
if user is None:
self.name.errors.append('Unknown username')
return False
if not user.check_password(self.password.data):
self.password.errors.append('Invalid password')
return False
self.user = user
return True
# Setup Flask-Security
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
security = Security(app, user_datastore, login_form=ExtendedLoginForm)
# Create an admin user to test with
def create_user_and_role():
first_user = User.query.filter(User.name == 'admin').first()
if not first_user:
user_email = app.config['ADMIN_EMAIL']
username = 'admin'
user_pass = app.config['ADMIN_PASS']
the_user = user_datastore.create_user(email=user_email, name=username, password=user_pass, dhbox_duration=1000000000)
check_admin_role = Role.query.filter(Role.name == 'admin').first()
if not check_admin_role:
the_role = user_datastore.create_role(name='admin', description='The administrator')
user_datastore.add_role_to_user(the_user, the_role)
else:
user_datastore.add_role_to_user(the_user, check_admin_role)
db.session.commit()
try:
DockerBackend.get_container_info(username)
print 'already have a container'
except Exception:
DockerBackend.setup_new_dhbox(username, user_pass, user_email)
def delete_user(user):
try:
user = User.query.filter(User.name == user).first()
db.session.delete(user)
db.session.commit()
except Exception, e:
print e
"""
URLS/VIEWS
"""
@app.route("/")
def index():
return render_template('index.html', institution=app.config['INSTITUTION'], demo=app.config['DEMO_ENABLED'])
@app.route("/signup")
def signup():
return render_template('signup.html')
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/contact')
def contact():
return render_template('contact.html')
@app.route('/our_team')
def our_team():
return render_template('our_team.html')
@app.route('/news')
def news():
news_folder = 'src/templates/news/'
news_list = []
for file in os.listdir(news_folder):
with open(news_folder + file) as f:
content = f.read()
news_list.append(content)
return render_template('news.html', news_list=news_list)
@app.route('/port_4000')
def port_4000():
return render_template('port.html')
@app.route('/get_started')
def get_started():
return render_template('get_started.html')
@app.route("/login", methods=["GET", "POST"])
def login():
# form = LoginForm()
# if form.validate_on_submit():
# # login and validate the user...
# login_user(user)
# flash("Logged in successfully.", 'alert-success')
# return redirect(url_for("user_box", the_user=user.name) or url_for("index"))
return render_template("login.html", form=form)
@app.route('/admin')
@login_required
@roles_required('admin')
def admin():
containers = User.query.all()
containers_list = []
for container in containers:
uptime = DockerBackend.how_long_up(container.name)
time_left = DockerBackend.check_if_over_time(container)
time_left = DockerBackend.display_time(time_left)
containers_list.append({'name': container.name, 'uptime': uptime, 'time_left': time_left})
return render_template('admin.html', containers=containers_list)
@app.route('/demo', methods=["GET"])
def demonstration():
username = 'demo' + ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(10))
demo_user_object = user_datastore.create_user(email=username + '@demo.com', name=username, password='demo', dhbox_duration=3600)
db.session.commit()
login_user(demo_user_object)
DockerBackend.demo_dhbox(username)
return redirect('/dhbox/' + username)
@app.route('/dhbox/<the_user>')
@login_required
def user_box(the_user):
which_user = User.query.filter(User.name == str(the_user)).first()
if current_user.__name__ is 'AnonymousUser':
return redirect(url_for("index"))
if which_user is None or current_user is None:
return redirect(url_for("index"))
login_user(which_user)
email_domain = which_user.email.split("@", 1)[1]
if email_domain == 'demo.com':
demo = True
else:
demo = False
try:
port_4000 = urllib2.urlopen(str(request.url) + '/website')
port_4000 = True
print "port 4000 website found"
except Exception:
print "no port 4000 site"
port_4000 = False
time_left = which_user.dhbox_duration - DockerBackend.how_long_up(which_user.name)
time_left = DockerBackend.display_time(time_left)
resp = make_response(render_template('dhbox.html',
user=the_user,
apps=filter(lambda app: app.get('hide', False) != True, all_apps),
demo=demo,
time_left=time_left,
bootstrap_container='container-fluid',
fixed_scroll='fixed_scroll',
port_4000=port_4000
)
)
return resp
@app.route("/dhbox/<the_user>/<app_name>")
@login_required
def app_box(the_user, app_name):
which_user = User.query.filter(User.name == str(the_user)).first()
dhbox_username = which_user.name
if app_name == 'wordpress':
app_port = get_app(app_name)['port']
port_info = DockerBackend.get_container_port(dhbox_username+'_wp', app_port)
elif app_name == 'website':
app_port = '4000'
port_info = DockerBackend.get_container_port(dhbox_username, app_port)
else:
app_port = get_app(app_name)['port']
port_info = DockerBackend.get_container_port(dhbox_username, app_port)
hostname = DockerBackend.get_hostname()
location = hostname + ":" + port_info
return redirect('http://' + location)
@app.route('/new_dhbox', methods=['POST'])
def new_dhbox():
data = {key: request.form.getlist(key)[0] for key in request.form.keys()}
for key in data:
users_dict = key
users_dict = ast.literal_eval(users_dict)
users = users_dict['users']
for user in users:
if 'name' in user:
if 'email' in user: # Then is DH Box admin
name_check = User.query.filter(User.name == user['name']).first()
email_check = User.query.filter(User.name == user['email']).first()
if name_check or email_check:
print "Username taken. Already has a DH Box."
return str('failure')
admin_user = user['name']
admin_email = user['email']
admin_pass = user['pass']
if user['duration'] == 'day':
duration = 86400
elif user['duration'] == 'week':
duration = 604800
else:
duration = 2592000
# if user['duration'] == 'week':
# duration = 604800
# elif user['duration'] == 'month':
# duration = 2592000
# else:
# duration = 13148730
admin_user_object = user_datastore.create_user(email=user['email'], name=user['name'], password=user['pass'], dhbox_duration=duration)
db.session.commit()
login_user(admin_user_object)
the_new_dhbox = DockerBackend.setup_new_dhbox(admin_user, admin_pass, admin_email)
return str('Successfully created a new DH Box.')
@app.route('/kill_dhbox', methods=['POST'])
@login_required
def kill_dhbox():
the_next = request.form.get('next')
user = request.form.get('user')
print(user)
if current_user.has_role("admin"):
pass
elif user != current_user.name:
# If they're not an admin and they're trying to delete a user that isn't them,
# return a Forbidden error.
return abort(403)
DockerBackend.kill_and_remove_user(user)
flash(message='DH Box and username deleted.', category='alert-success')
return redirect(url_for(the_next) or url_for("index"))
def police():
if os.path.isfile('dhbox-docker.db'):
print "policing"
users = User.query.all()
for user in users:
DockerBackend.check_and_kill(user)
all_containers = DockerBackend.all_containers()
for container in all_containers:
try:
time_up = DockerBackend.how_long_up(container)
info = DockerBackend.get_container_info(container)
name = info['Name'][1:]
if name.startswith('demo') and time_up > 3600:
DockerBackend.kill_and_remove_user(name)
except Exception, e:
print "Tried to check container: ", container
raise e
def run_schedule():
while 1:
schedule.run_pending()
time.sleep(1)
schedule.every(1).minutes.do(police)
t = Thread(target=run_schedule)
t.daemon = True
t.start()
if __name__ == '__main__':
app.debug = app.config['TESTING']
# Make database if it doesn't exist
if not os.path.exists('dhbox-docker.db'):
db.create_all()
create_user_and_role()
# Bind to PORT if defined, otherwise default to 5000.
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port, threaded=True)