Commit 13719ade authored by Vathsavayi, Sri Harsha (sriv)'s avatar Vathsavayi, Sri Harsha (sriv)
Browse files

initial commit

parents
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover
.hypothesis/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# IPython Notebook
.ipynb_checkpoints
# pyenv
.python-version
# celery beat schedule file
celerybeat-schedule
# dotenv
.env
# virtualenv
venv/
ENV/
# Spyder project settings
.spyderproject
# Rope project settings
.ropeproject
from flask import Flask, redirect, url_for, render_template, flash, request
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager, UserMixin, login_user, logout_user,\
current_user, login_required
from container_manager import *
app = Flask(__name__)
app.config['SECRET_KEY'] = 'sdcsecret!'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite'
db = SQLAlchemy(app)
lm = LoginManager(app)
lm.login_view = 'login'
class User(UserMixin, db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
username = db.Column('username', db.String(20), unique=True, index=True)
password = db.Column('password', db.String(250))
b2drop_name = db.Column(db.String(64), nullable=True)
b2drop_pswd = db.Column(db.String(64), nullable=True)
b2drop_url = db.Column(db.String(64), nullable=True)
def __init__(self, username, password):
self.username = username
self.password = password
self.b2drop_url = ""
self.b2drop_pswd = ""
self.b2drop_name= ""
@lm.user_loader
def load_user(id):
return User.query.get(int(id))
@app.route('/')
@login_required
def index():
return render_template('index.html')
@app.route('/logout')
def logout():
logout_user()
return redirect(url_for('index'))
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'GET':
return render_template('login.html')
username = request.form['username']
registered_user = User.query.filter_by(username=username).first()
if registered_user is None:
flash('Username is invalid', 'error')
return redirect(url_for('login'))
login_user(registered_user)
flash('Logged in successfully')
return redirect(url_for('index'))
@app.route('/register' , methods=['GET','POST'])
@login_required
def register():
if request.method == 'GET':
return render_template('register.html')
user = current_user
user.b2drop_url=request.form['url']
user.b2drop_name=request.form['uname']
user.b2drop_pswd=request.form['pswd']
db.session.commit()
login_user(user, True)
flash('User successfully registered')
return redirect(url_for('index'))
@app.route('/jupyter' , methods=['GET'])
@login_required
def run_jupyter():
user = current_user
create_container(user)
flash('User Jupyter container successfully created')
return redirect('http://localhost:8890')
@app.route('/terminal' , methods=['GET'])
@login_required
def run_terminal():
user = current_user
create_terminal_container(user)
flash('User Terminal container successfully created')
return redirect('http://localhost:8880/')
def create_test_users():
user = User('harsha', 'test')
db.session.add(user)
db.session.commit()
if __name__ == '__main__':
db.drop_all()
db.create_all()
create_test_users()
app.run(host='0.0.0.0',debug=True)
import io
import os
import time
import docker
from settings import app, client
def create_container(user):
b2drop_user = "B2DROP_USER=%s"%(user.b2drop_name)
b2drop_psw = "B2DROP_PASSWORD=%s"%(user.b2drop_pswd)
b2drop_url = "WEBDAV_URL=%s"%(user.b2drop_url)
container_name = "jupyter_%s"%(user.username)
notebook = client.containers.list(filters={'name': container_name})
if client.containers.list(filters={'name':container_name}):
notebook = client.containers.list(filters={'name':container_name})[0]
if (notebook.status != "running"):
notebook.restart()
else:
try:
notebook = client.services.run('jupyterlab', detach=True, auto_remove=True, name=container_name, network="jupyterhub-network", cap_add=["SYS_ADMIN"], devices=["/dev/fuse"],
environment=[b2drop_user,b2drop_psw,b2drop_url],
command="start-notebook.sh --NotebookApp.base_url=/jupyter/ --NotebookApp.token=''",
ports={'8888/tcp': 8890})
notebook.exec_run(cmd="/etc/init.d/start.sh")
except docker.errors.APIError:
print("Name error")
client.containers.prune(filters={'name':container_name})
notebook = client.containers.run('jupyterlab', detach=True, auto_remove=True, name=container_name,
cap_add=["SYS_ADMIN"], devices=["/dev/fuse"],
environment=[b2drop_user, b2drop_psw, b2drop_url],
command="start-notebook.sh --NotebookApp.token='' ",
ports={'8888/tcp': 8890})
notebook.exec_run(cmd="/etc/init.d/start.sh")
print(notebook.status)
time.sleep(3)
# notebook.wait('jupyterlab')
#return redirect('http://localhost:8890/')
#def get_code_from_docker():
# cli.start('hello_word_from_docker')
# cli.wait('hello_word_from_docker')
# output = cli.logs('hello_word_from_docker')
# cli.remove_container('hello_word_from_docker', force=True)
# return "From docker: {}".format(output.strip())
def create_terminal_container(user):
b2drop_user = "B2DROP_USER=%s"%(user.b2drop_name)
b2drop_psw = "B2DROP_PASSWORD=%s"%(user.b2drop_pswd)
b2drop_url = "WEBDAV_URL=%s"%(user.b2drop_url)
container_name = "wetty_%s"%(user.username)
notebook = client.containers.list(filters={'name': container_name})
if client.containers.list(filters={'name':container_name}):
notebook = client.containers.list(filters={'name':container_name})[0]
if (notebook.status != "running"):
notebook.restart()
else:
try:
notebook = client.containers.run('wetty', detach=True, auto_remove=True, name=container_name, network="jupyterhub-network", cap_add=["SYS_ADMIN"], devices=["/dev/fuse"],
environment=[b2drop_user,b2drop_psw,b2drop_url],
ports={'3000/tcp': 8880})
notebook.exec_run(cmd="/etc/init.d/start.sh")
except docker.errors.APIError:
print("Name error")
client.containers.prune(filters={'name':container_name})
notebook = client.containers.run('wetty', detach=True, auto_remove=True, name=container_name,
network="jupyterhub-network", cap_add=["SYS_ADMIN"], devices=["/dev/fuse"],
environment=[b2drop_user, b2drop_psw, b2drop_url],
ports={'3000/tcp': 8880})
notebook.exec_run(cmd="/etc/init.d/start.sh")
print(notebook.status)
if __name__ == "__main__":
create_container("user")
File added
import io
import os
import time
import docker
from flask import Flask, render_template, request, redirect
from settings import app, client
@app.route("/", methods=['GET'])
def index_page():
return render_template('index.html')
@app.route("/b2dropcredentials", methods=['POST'])
def execute_code():
user= {}
user['b2drop_name'] = request.form['uname']
user['b2drop_pswd'] = request.form['psw']
user['b2drop_url'] = request.form['url']
create_container(user)
return redirect('http://localhost:8890/')
def create_container(user):
b2drop_user = "B2DROP_USER=%s"%(user['b2drop_name'])
b2drop_psw = "B2DROP_PASSWORD=%s"%(user['b2drop_pswd'])
b2drop_url = "WEBDAV_URL=%s"%(user['b2drop_url'])
container_name = "jupyter_%s"%(user['b2drop_name'])
notebook = client.containers.list(filters={'name': container_name})
if client.containers.list(filters={'name':container_name}):
notebook = client.containers.list(filters={'name':container_name})[0]
if (notebook.status != "running"):
notebook.restart()
else:
try:
notebook = client.containers.run('jupyterlab', detach=True, auto_remove=True, name=container_name, cap_add=["SYS_ADMIN"], devices=["/dev/fuse"],
environment=[b2drop_user,b2drop_psw,b2drop_url],
command="start-notebook.sh --NotebookApp.token=''",
ports={'8888/tcp': 8890})
notebook.exec_run(cmd="/etc/init.d/start.sh")
except docker.errors.APIError:
print("Name error")
client.containers.prune(filters={'name':container_name})
notebook = client.containers.run('jupyterlab', detach=True, auto_remove=True, name=container_name,
cap_add=["SYS_ADMIN"], devices=["/dev/fuse"],
environment=[b2drop_user, b2drop_psw, b2drop_url],
command="start-notebook.sh --NotebookApp.token=''",
ports={'8888/tcp': 8890})
notebook.exec_run(cmd="/etc/init.d/start.sh")
print(notebook.status)
time.sleep(5)
# notebook.wait('jupyterlab')
#return redirect('http://localhost:8890/')
#def get_code_from_docker():
# cli.start('hello_word_from_docker')
# cli.wait('hello_word_from_docker')
# output = cli.logs('hello_word_from_docker')
# cli.remove_container('hello_word_from_docker', force=True)
# return "From docker: {}".format(output.strip())
if __name__ == "__main__":
app.run(debug=True)
alembic==0.9.6
attrs==17.3.0
certifi==2017.11.5
chardet==3.0.4
click==6.7
codecov==2.0.10
coverage==4.4.2
decorator==4.1.2
docker==2.7.0
docker-pycreds==0.2.1
escapism==1.0.0
Flask==0.12.2
Flask-Login==0.4.1
Flask-SQLAlchemy==2.3.2
idna==2.6
ipython-genutils==0.2.0
itsdangerous==0.24
Jinja2==2.10
jupyterhub==0.8.1
Mako==1.0.7
MarkupSafe==1.0
pamela==0.3.0
pluggy==0.6.0
py==1.5.2
pyflakes==1.6.0
pytest==3.3.1
pytest-cov==2.5.1
python-dateutil==2.6.1
python-editor==1.0.3
python-oauth2==1.0.1
requests==2.18.4
six==1.11.0
SQLAlchemy==1.1.15
tornado==4.5.2
traitlets==4.3.2
urllib3==1.22
websocket-client==0.44.0
Werkzeug==0.13
wheel==0.24.0
import docker
from flask import Flask
client = docker.from_env()
SECRET_KEY = 'secret!'
app = Flask(__name__)
app.config.from_object(__name__)
user = {}
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
{% extends "layout.html" %}
{% block body %}
<div id="main" class="container">
<h2>All Items</h2>
<a href="/jupyter " target="_blank" class="btn btn-large btn-primary">Jupyter container</a>
<a href="/terminal" target="_blank" class="btn btn-large btn-primary">Ubuntu container</a>
{% endblock %}
</div>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>B2drop prototype</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >
</head>
<body>
<div class="container">
<div class="row" align="center">
<div><h2> Add B2drop app username and password </h2></div>
<div class="col-sm-4">
<div class="container" style="background-color:#f1f1f1">
<form action="/b2dropcredentials" method="post">
<div>
<label><b>Username</b></label>
<input type="text" placeholder="Enter B2drop app username" name="uname" required>
</div>
<div>
<label><b>Password</b></label>
<input type="password" placeholder="Enter B2drop app Password" name="psw" required>
</div>
<div>
<label><b>Enter B2drop webdav url</b></label>
<input type="test" placeholder="https://fi.b2drop.eudat.eu/remote.php/webdav/" name="url" required>
</div></br>
<button type="submit">Login</button>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
<!doctype html>
<title>B2DROP test</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
body {
padding-top: 60px;
padding-bottom: 100px;
}
</style>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<div class="container">
<div class="header clearfix">
<nav>
<ul class="nav nav-pills pull-right">
<li role="presentation" ><a href="/">Home</a></li>
{% if current_user.is_authenticated %}
<li role="presentation"><a href="{{ url_for('register') }}">Register B2DROP details</a></li>
<li role="presentation"><a href="{{ url_for('logout') }}">Logout</a></li>
{% endif %}
{% if not current_user.is_authenticated %}
<li role="presentation"><a href="{{ url_for('login') }}">Login</a></li>
{% endif %}
</ul>
</nav>
</div>
<div id="main" class="container">
{% with messages = get_flashed_messages() %}
{% if messages %}
<div class="alert">
<ul class="flashes">
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
</div>
{% endif %}
{% endwith %}
{% block body %}{% endblock %}
<footer id="footer">
</footer>
</div>
</div>
\ No newline at end of file
{% extends "layout.html" %}
{% block body %}
<form action="" method=post class="form-horizontal">
<h2>Sign in to B2DROP mount application </h2>
<div class="control-group">
<div class="controls">
<input type="text" id="username" name="username" class="input-xlarge"
placeholder="Enter Username" required>
</div>
</div>
<div class="control-group">
<div class="controls">
<button type="submit" class="btn btn-success">Signin</button>
</div>
</div>
</form>
{% endblock %}
\ No newline at end of file
{% extends "layout.html" %}
{% block body %}
{% if not current_user.b2drop_name %}
<form action="" method=post class="form-horizontal">
<h2>Add B2DROP app password details </h2>
<div class="control-group">
<div class="controls">
<label><b>B2DROP username</b></label>
<input type="text" id="username" name="uname" class="input-xlarge"
placeholder="Enter B2DROP app username" value="{{ request.form.uname }}"
required>
</div>
</div>
<div class="control-group">
<div class="controls">
<label><b>B2DROP password</b></label>
<input type="password" id="password" name="pswd" class="input-xlarge"
placeholder="Enter B2DROP app Password" value="{{ request.form.pswd }}"
required>
</div>
</div>
<div class="control-group">
<div class="controls">
<label><b>B2DROP url</b></label>
<input type="text" id="url" name="url" class="input-xlarge"
placeholder="Enter B2DROP webdav url" value="{{ request.form.url }}"
required>
</div>
</div>
<div class="control-group">
<div class="controls">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</form>
{% else %}