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

refactored code

removed unnecessary files
parent 701e25ab
from flask import Flask, redirect, url_for, render_template, flash, request
from flask_sqlalchemy import SQLAlchemy
from flask import redirect, url_for, render_template, flash, request
from flask_login import LoginManager, UserMixin, login_user, logout_user,\
current_user, login_required
from container_manager import *
from settings import app, db
import os
app = Flask(__name__)
app.config['SECRET_KEY'] = 'sdcsecret!'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite'
db = SQLAlchemy(app)
# initialize login manager
lm = LoginManager(app)
lm.login_view = 'login'
# Create database to hold user data
class User(UserMixin, db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
......@@ -60,6 +54,7 @@ def login():
password = request.form['password']
registered_user = User.query.filter_by(username=username).first()
if registered_user is None:
flash('Username is invalid', 'error')
return redirect(url_for('login'))
......@@ -104,7 +99,7 @@ def register():
@login_required
def run_jupyter():
user = current_user
notebook_url = create_container(user)
notebook_url = create_jupyter_container(user)
# flash('User Jupyter container successfully created')
return redirect('http://193.167.189.21:8080%s'%notebook_url)
......@@ -112,12 +107,12 @@ def run_jupyter():
@login_required
def run_terminal():
user = current_user
terminal_url = create_terminal_container(user)
terminal_url = create_webodv_container(user)
print (terminal_url)
# flash('User Terminal container successfully created')
return redirect('http://193.167.189.21:8080%s'%terminal_url)
## reads usernames and passwords from a file and populates database
def create_test_users(filename):
pwd = os.path.dirname(__file__)
with open(os.path.join(pwd, filename)) as f:
......@@ -132,7 +127,5 @@ def create_test_users(filename):
db.session.commit()
if __name__ == '__main__':
# db.drop_all()
# db.create_all()
# create_test_users('userdata')
app.run(host='0.0.0.0',debug=True)
import io
import os
import time
import docker
import json
from tornado.httpclient import HTTPRequest, HTTPError, AsyncHTTPClient
import requests
from settings import client, proxy_token, proxy_url_endpoint
from settings import client, proxy_url_endpoint
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)
def create_jupyter_container(user):
"""
If jupyter container is already running, just executes the b2drop script.
Otherwise, create the container for user
:param user:
:return:
"""
container_name = "jupyter_%s"%(user.username)
notebook_serve_url = "/jupyter/{}".format(user.username)
print ("step 1")
container_port = 8888
if client.containers.list(filters={'name':container_name}):
notebook = client.containers.list(filters={'name':container_name})[0]
......@@ -27,88 +25,61 @@ def create_container(user):
if (notebook.status != "running"):
notebook.restart()
else:
try:
notebook = client.containers.run('vathsava/diva-test', 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=%s --NotebookApp.token=''"%(
notebook_serve_url))
# launch a container
notebook = client.containers.run('vathsava/diva-test', detach=True, auto_remove=True,
name=container_name, network="jupyterhub-network",
cap_add=["SYS_ADMIN"], devices=["/dev/fuse"],
command="start-notebook.sh --NotebookApp.base_url=%s --NotebookApp.token=''"%(
notebook_serve_url))
# execute the b2drop script
notebook.exec_run(
cmd="/etc/init.d/start.sh {} {} {}".format(user.b2drop_name, user.b2drop_pswd, user.b2drop_url))
except docker.errors.APIError:
print("Name error")
# client.containers.prune(filters={'name':container_name})
notebook = client.containers.run('vathsava/diva-test', 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.base_url=%s --NotebookApp.token=''" %(
notebook_serve_url)
)
notebook.exec_run(
cmd="/etc/init.d/start.sh {} {} {}".format(user.b2drop_name, user.b2drop_pswd, user.b2drop_url))
#TODO: not ideal and need to be updated
# wait until container starts
time.sleep(5)
container_ip = ""
## proxy user container
add_proxy_route(container_name, notebook_serve_url, container_port)
return notebook_serve_url
## proxy user container
path = notebook_serve_url
def add_proxy_route(container_name, notebook_serve_url, port):
http_client = AsyncHTTPClient()
headers = {"Authorization": "token {}".format(proxy_token)}
#http_client = AsyncHTTPClient()
#headers = {"Authorization": "token {}".format(proxy_token)}
# create proxy endpoint
proxy_endpoint = "{}/api/routes{}".format(proxy_url_endpoint, notebook_serve_url)
body = json.dumps({
"target": "http://{}:{}".format(container_name, 8888)
"target": "http://{}:{}".format(container_name, port)
})
requests.post(proxy_endpoint,data=body)
# req = HTTPRequest(proxy_endpoint,
# method="POST",
# body=body)
# try:
# http_client.fetch(req)
# print("Proxied path [%s] to port [%s].", path, '8888')
# except HTTPError as e:
# print("Failed to create proxy route to [%s]: %s", path, e)
#post a request to proxy
requests.post(proxy_endpoint, data=body)
return notebook_serve_url
# 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):
def create_webodv_container(user):
"""
If webodv container is already running, just executes the b2drop script.
Otherwise, create the container for user
:param user:
:return:
"""
vre_user = "VRE_USER=%s"%(user.username)
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 = "webodv_%s"%(user.username)
terminal_serve_url = "/{}/webodv_extract_region".format(user.username)
webodv_serve_url = "/{}/webodv_extract_region".format(user.username)
container_port = 80
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]
......@@ -121,15 +92,6 @@ def create_terminal_container(user):
else:
try:
notebook = client.containers.run('vathsava/webodv_proto', detach=True, auto_remove=True, name=container_name, network="jupyterhub-network", cap_add=["SYS_ADMIN"], devices=["/dev/fuse"],
environment=[vre_user])
notebook.exec_run(
cmd="/etc/init.d/start.sh {} {} {}".format(user.b2drop_name, user.b2drop_pswd, user.b2drop_url))
except docker.errors.APIError:
print("Name error")
notebook = client.containers.run('vathsava/webodv_proto', detach=True, auto_remove=True, name=container_name,
network="jupyterhub-network", cap_add=["SYS_ADMIN"], devices=["/dev/fuse"],
environment=[vre_user])
......@@ -137,34 +99,14 @@ def create_terminal_container(user):
notebook.exec_run(
cmd="/etc/init.d/start.sh {} {} {}".format(user.b2drop_name, user.b2drop_pswd, user.b2drop_url))
time.sleep(5)
path = terminal_serve_url
http_client = AsyncHTTPClient()
headers = {"Authorization": "token {}".format(proxy_token)}
proxy_endpoint = "{}/api/routes{}".format(proxy_url_endpoint, terminal_serve_url)
body = json.dumps({
"target": "http://{}".format(container_name)
})
requests.post(proxy_endpoint,data=body)
# proxy_endpoint = "{}/api/routes{}".format(proxy_url_endpoint, '/wetty')
# body = json.dumps({
# "target": "http://{}".format(container_name)
# })
# requests.post(proxy_endpoint,data=body)
except docker.errors.APIError:
print("Name error")
time.sleep(5)
print(notebook.status)
return terminal_serve_url
## proxy user container
add_proxy_route(container_name, webodv_serve_url, container_port)
if __name__ == "__main__":
create_container("user")
return webodv_serve_url
No preview for this file type
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 docker import DockerClient
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
# initialize flask app
app = Flask(__name__)
app.config['SECRET_KEY'] = 'sdcsecret!'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite'
#client = 'unix://docker.sock'#docker.from_env()
client = docker.DockerClient(base_url='unix://docker.sock')
#Client(base_url='unix://docker.sock')
SECRET_KEY = 'secret!'
# database
db = SQLAlchemy(app)
app = Flask(__name__)
app.config.from_object(__name__)
user = {}
# docker socket
client = docker.DockerClient(base_url='unix://docker.sock')
# proxy settings
proxy_url_endpoint = 'http://localhost:8001'
proxy_token = "716238957362948752139417234"
<!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>
test1 password1
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment