Commit 59062143 authored by Cloud User's avatar Cloud User
Browse files

updated app and container manager

parent 13719ade
......@@ -3,87 +3,5 @@ __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
# database file
db.sqllite
......@@ -32,6 +32,8 @@ class User(UserMixin, db.Model):
self.b2drop_pswd = ""
self.b2drop_name= ""
def check_password(self , password):
return self.password == password
@lm.user_loader
def load_user(id):
......@@ -55,11 +57,16 @@ def login():
return render_template('login.html')
username = request.form['username']
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'))
if not registered_user.check_password(password):
flash('Password is invalid','error')
return redirect(url_for('login'))
login_user(registered_user)
flash('Logged in successfully')
return redirect(url_for('index'))
......@@ -70,42 +77,62 @@ def register():
if request.method == 'GET':
return render_template('register.html')
try:
user = current_user
user.b2drop_url=request.form['url']
user.b2drop_name=request.form['uname']
user.b2drop_pswd=request.form['pswd']
request_url = requests.get(request.form['url'], auth=(request.form['uname'], request.form['pswd']))
db.session.commit()
login_user(user, True)
if request_url.status_code is 200:
user = current_user
user.b2drop_url = request.form['url']
user.b2drop_name = request.form['uname']
user.b2drop_pswd = request.form['pswd']
flash('User successfully registered')
return redirect(url_for('index'))
db.session.commit()
login_user(user, True)
flash('User b2drop details successfully registered')
return redirect(url_for('index'))
else:
flash('invalid b2drop details')
return render_template('register.html')
except requests.exceptions.RequestException as e: # This is the correct syntax
flash('invalid b2drop details')
return render_template('register.html')
@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')
notebook_url = create_container(user)
# flash('User Jupyter container successfully created')
return redirect('http://193.167.189.21:8080%s'%notebook_url)
@app.route('/terminal' , methods=['GET'])
@app.route('/webodv' , 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()
terminal_url = create_terminal_container(user)
print (terminal_url)
# flash('User Terminal container successfully created')
return redirect('http://193.167.189.21:8080%s'%terminal_url)
def create_test_users(filename):
pwd = os.path.dirname(__file__)
with open(os.path.join(pwd, filename)) as f:
for line in f:
if not line:
continue
parts = line.split()
name = parts[0]
password = parts[1]
user = User(name, password)
db.session.add(user)
db.session.commit()
if __name__ == '__main__':
db.drop_all()
db.create_all()
create_test_users()
# db.drop_all()
# db.create_all()
# create_test_users('userdata')
app.run(host='0.0.0.0',debug=True)
......@@ -2,8 +2,12 @@ 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 app, client
def create_container(user):
......@@ -11,11 +15,15 @@ def create_container(user):
b2drop_psw = "B2DROP_PASSWORD=%s"%(user.b2drop_pswd)
b2drop_url = "WEBDAV_URL=%s"%(user.b2drop_url)
container_name = "jupyter_%s"%(user.username)
notebook_serve_url = "/jupyter/{}".format(user.username)
notebook = client.containers.list(filters={'name': container_name})
print ("step 1")
if client.containers.list(filters={'name':container_name}):
notebook = client.containers.list(filters={'name':container_name})[0]
notebook.exec_run(
cmd="/etc/init.d/start.sh {} {} {}".format(user.b2drop_name, user.b2drop_pswd, user.b2drop_url))
if (notebook.status != "running"):
notebook.restart()
......@@ -23,32 +31,60 @@ def create_container(user):
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")
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))
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})
# client.containers.prune(filters={'name':container_name})
notebook = client.containers.run('jupyterlab', detach=True, auto_remove=True, 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.token='' ",
ports={'8888/tcp': 8890})
notebook.exec_run(cmd="/etc/init.d/start.sh")
# 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))
time.sleep(5)
print(notebook.status)
container_ip = ""
## proxy user container
path = notebook_serve_url
http_client = AsyncHTTPClient()
headers = {"Authorization": "token {}".format(proxy_token)}
proxy_endpoint = "{}/api/routes{}".format(proxy_url_endpoint, notebook_serve_url)
body = json.dumps({
"target": "http://{}:{}".format(container_name, 8888)
})
requests.post(proxy_endpoint,data=body)
time.sleep(3)
# 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)
return notebook_serve_url
# time.sleep(3)
# notebook.wait('jupyterlab')
......@@ -64,15 +100,20 @@ def create_container(user):
def create_terminal_container(user):
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 = "wetty_%s"%(user.username)
container_name = "webodv_%s"%(user.username)
terminal_serve_url = "/{}/webodv_extract_region".format(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]
notebook.exec_run(
cmd="/etc/init.d/start.sh {} {} {}".format(user.b2drop_name, user.b2drop_pswd, user.b2drop_url))
if (notebook.status != "running"):
notebook.restart()
......@@ -80,26 +121,50 @@ def create_terminal_container(user):
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")
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")
client.containers.prune(filters={'name':container_name})
notebook = client.containers.run('wetty', detach=True, auto_remove=True, name=container_name,
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=[b2drop_user, b2drop_psw, b2drop_url],
ports={'3000/tcp': 8880})
notebook.exec_run(cmd="/etc/init.d/start.sh")
environment=[vre_user])
notebook.exec_run(
cmd="/etc/init.d/start.sh {} {} {}".format(user.b2drop_name, user.b2drop_pswd, user.b2drop_url))
time.sleep(5)
print(notebook.status)
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)
print(notebook.status)
return terminal_serve_url
if __name__ == "__main__":
create_container("user")
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