Commit 899db8e5 authored by Daniele Venzano's avatar Daniele Venzano

Use only user_id in the API

parent 7aa1b732
......@@ -37,8 +37,7 @@
<primary-key name="PRIMARY" columns="id"/>
</table>
<table name="proxy" schema="" catalog="caaas" type="TABLE">
<column name="id" sqlType="INT" precision="10" scale="0" nullable="false" jdbcType="4" autoIncrement="true"/>
<column name="proxy_id" sqlType="VARCHAR" precision="512" scale="0" nullable="false" jdbcType="12"/>
<column name="id" sqlType="VARCHAR" precision="128" scale="0" nullable="false" jdbcType="12"/>
<column name="internal_url" sqlType="VARCHAR" precision="512" scale="0" nullable="true" jdbcType="12"/>
<column name="cluster_id" sqlType="INT" precision="10" scale="0" nullable="false" jdbcType="4"/>
<column name="service_name" sqlType="VARCHAR" precision="64" scale="0" nullable="true" jdbcType="12"/>
......@@ -48,7 +47,7 @@
</table>
<table name="users" schema="" catalog="caaas" type="TABLE">
<column name="id" sqlType="INT" precision="10" scale="0" nullable="false" jdbcType="4" autoIncrement="true"/>
<column name="username" sqlType="VARCHAR" precision="32" scale="0" nullable="false" jdbcType="12"/>
<column name="email" sqlType="VARCHAR" precision="128" scale="0" nullable="false" jdbcType="12"/>
<primary-key name="PRIMARY" columns="id"/>
</table>
</data-source>
......
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DataSourceManagerImpl" format="xml" hash="779648770">
<component name="DataSourceManagerImpl" format="xml" hash="3694309854">
<data-source source="LOCAL" name="MySQL - @m1" uuid="a32fd6de-3ffa-40c0-9ec8-8953a89c53e0">
<driver-ref>mysql</driver-ref>
<synchronize>true</synchronize>
......
This diff is collapsed.
......@@ -48,11 +48,11 @@ def generate_file(proxy_entries):
node_list = []
for p in proxy_entries:
netloc = urlparse(p["internal_url"])[1]
node_list.append((netloc, p["proxy_id"]))
node_list.append((netloc, p["id"]))
for p in proxy_entries:
netloc = urlparse(p["internal_url"])[1]
jinja_dict = {
"proxy_id": p["proxy_id"],
"proxy_id": p["id"],
"proxy_url": p["internal_url"],
"service_name": p["service_name"],
"netloc": netloc,
......@@ -100,7 +100,7 @@ def update_proxy_access_timestamps():
state = CAaaState()
for proxy in state.get_proxies():
proxy_id = proxy['proxy_id']
proxy_id = proxy['id']
if proxy_id in last_accesses:
state.update_proxy_access(proxy_id, last_accesses[proxy_id])
......
......@@ -21,68 +21,71 @@ def api_status():
return jsonify(**data)
@app.route("/api/<username>/cluster/<cluster_id>/terminate")
def api_terminate_cluster(username, cluster_id):
@app.route("/api/<user_id>/cluster/<cluster_id>/terminate")
def api_terminate_cluster(user_id, cluster_id):
db = CAaaState()
user_id = db.get_user_id(username)
cluster_list = db.get_clusters(user_id)
ret = {}
if not db.check_user_id(user_id):
ret["status"] = "unauthorized"
return jsonify(**ret)
cluster_list = db.get_clusters(user_id)
if cluster_list[cluster_id]["user_id"] != user_id:
ret["status"] = "unauthorized"
return jsonify(**ret)
if sm.terminate_cluster(cluster_id):
ret["status"] = "ok"
else:
if sm.terminate_cluster(cluster_id):
ret["status"] = "ok"
else:
ret["status"] = "error"
ret["status"] = "error"
return jsonify(**ret)
@app.route("/api/<username>/container/<container_id>/logs")
def api_container_logs(username, container_id):
@app.route("/api/<user_id>/container/<container_id>/logs")
def api_container_logs(user_id, container_id):
db = CAaaState()
user_id = db.get_user_id(username)
# FIXME: check user_id
ret = {}
if not db.check_user_id(user_id):
ret["status"] = "unauthorized"
return jsonify(**ret)
logs = sm.get_log(container_id)
if logs is None:
ret = {
"status": "no such container",
"logs": ""
}
ret["status"] = "no such container"
ret["logs"] = ''
else:
logs = logs.decode("ascii").split("\n")
ret = {
"status": "ok",
"logs": logs
}
ret["status"] = "ok"
ret["logs"] = logs
return jsonify(**ret)
@app.route("/api/<username>/spark-submit", methods=['POST'])
def api_spark_submit(username):
@app.route("/api/<user_id>/spark-submit", methods=['POST'])
def api_spark_submit(user_id):
state = CAaaState()
ret = {}
if not state.check_user_id(user_id):
ret["status"] = "unauthorized"
return jsonify(**ret)
file_data = request.files['file']
form_data = request.form
state = CAaaState()
user_id = state.get_user_id(username)
# FIXME: check user_id
if not is_zipfile(file_data.stream):
ret = {
"status": "not a zip file"
}
ret["status"] = "not a zip file"
return jsonify(**ret)
app_id = application_submitted(user_id, form_data["exec_name"], form_data["spark_options"], form_data["cmd_line"], file_data)
setup_volume(user_id, app_id, file_data.stream)
sm.spark_submit(user_id, app_id)
ret = {
"status": "ok"
}
ret["status"] = "ok"
return jsonify(**ret)
@app.route("/api/<username>/history/<app_id>/logs")
def api_history_log_archive(username, app_id):
@app.route("/api/<user_id>/history/<app_id>/logs")
def api_history_log_archive(user_id, app_id):
state = CAaaState()
user_id = state.get_user_id(username)
# FIXME: check user_id
if not state.check_user_id(user_id):
return jsonify(status="unauthorized")
ah = AppHistory(user_id)
path = ah.get_log_archive_path(app_id)
return send_file(path, mimetype="application/zip")
......@@ -11,7 +11,7 @@ def get_container_addresses(container_id):
proxy_list = state.get_proxies(container_id=container_id)
urls = []
for p in proxy_list:
external_url = _generate_proxied_url(p["proxy_id"])
external_url = _generate_proxied_url(p["id"])
urls.append((p["service_name"], external_url))
return urls
......
......@@ -35,7 +35,7 @@ class CAaaState:
def _check_user(self, username):
cursor = self._get_cursor(dictionary=True)
q = "SELECT id FROM users WHERE username=%s"
q = "SELECT id FROM users WHERE email=%s"
cursor.execute(q, (username,))
if cursor.rowcount == 0:
......@@ -48,18 +48,26 @@ class CAaaState:
def _create_user(self, username):
cursor = self._get_cursor()
q = "INSERT INTO users (username) VALUES (%s)"
q = "INSERT INTO users (email) VALUES (%s)"
cursor.execute(q, (username,))
user_id = cursor.lastrowid
self._close_cursor(cursor)
return user_id
def check_user_id(self, user_id):
cursor = self._get_cursor()
q = "SELECT COUNT(*) FROM users WHERE id=%s"
cursor.execute(q, (user_id,))
count = cursor.fetchone()[0]
self._close_cursor(cursor)
return count == 1
def get_user_id(self, username):
return self._check_user(username)
def get_user_email(self, user_id):
cursor = self._get_cursor()
q = "SELECT username FROM users WHERE id=%s"
q = "SELECT email FROM users WHERE id=%s"
cursor.execute(q, (user_id,))
row = cursor.fetchone()
self._close_cursor(cursor)
......@@ -67,7 +75,7 @@ class CAaaState:
def get_all_users(self):
cursor = self._get_cursor()
q = "SELECT id, username FROM users"
q = "SELECT id, email FROM users"
user_list = []
cursor.execute(q)
......@@ -137,7 +145,7 @@ class CAaaState:
def get_url_proxy(self, proxy_id):
cursor = self._get_cursor()
q = "SELECT internal_url FROM proxy WHERE proxy_id=%s"
q = "SELECT internal_url FROM proxy WHERE id=%s"
cursor.execute(q, (proxy_id,))
if cursor.rowcount == 0:
self._close_cursor(cursor)
......@@ -149,7 +157,7 @@ class CAaaState:
def get_proxy_for_service(self, cluster_id, service_name):
cursor = self._get_cursor()
q = "SELECT proxy_id FROM proxy WHERE cluster_id=%s AND service_name=%s"
q = "SELECT id FROM proxy WHERE cluster_id=%s AND service_name=%s"
cursor.execute(q, (cluster_id, service_name))
if cursor.rowcount == 0:
self._close_cursor(cursor)
......@@ -184,7 +192,7 @@ class CAaaState:
def new_proxy_entry(self, proxy_id, cluster_id, address, service_name, container_id):
cursor = self._get_cursor()
q = "INSERT INTO proxy (proxy_id, internal_url, cluster_id, service_name, container_id) VALUES (%s, %s, %s, %s, %s)"
q = "INSERT INTO proxy (id, internal_url, cluster_id, service_name, container_id) VALUES (%s, %s, %s, %s, %s)"
cursor.execute(q, (proxy_id, address, cluster_id, service_name, container_id))
self._close_cursor(cursor)
return proxy_id
......@@ -384,7 +392,7 @@ class CAaaState:
def update_proxy_access(self, proxy_id, access_ts):
cursor = self._get_cursor()
q = "UPDATE proxy SET last_access=%s WHERE proxy_id=%s"
q = "UPDATE proxy SET last_access=%s WHERE id=%s"
cursor.execute(q, (access_ts, proxy_id))
self._close_cursor(cursor)
......
......@@ -13,7 +13,7 @@
<body>
<script>
function update_status() {
$.getJSON( api_endpoint + "/status")
$.getJSON("{{ url_for("api_status") }}")
.done(function( data ) {
$("#num_nodes").text(data.num_nodes);
$("#num_containers").text(data.num_containers);
......
......@@ -5,9 +5,9 @@
<script>
function process()
{
var url = "http://" + window.location.hostname + "/web/" + $("#user").val();
location.href = url;
return false;
var url = "http://" + window.location.hostname + "/web/" + $("#user").val();
location.href = url;
return false;
}
</script>
......
......@@ -40,7 +40,7 @@
var formData = new FormData($('#app_upload')[0]);
$("#progress").show();
$.ajax({
url: '/api/{{ user }}/spark-submit', //Server script to process data
url: '{{ url_for("api_spark_submit", user_id=user_id) }}', //Server script to process data
type: 'POST',
xhr: function() { // Custom XMLHttpRequest
var myXhr = $.ajaxSettings.xhr();
......
......@@ -3,7 +3,7 @@
{% block content %}
<script type="application/javascript">
(function() {
$.getJSON( api_endpoint + "/{{ user }}/cluster/{{ cluster_id }}/terminate")
$.getJSON("{{ url_for("api_terminate_cluster", user_id, cluster_id) }}")
.done(function( data ) {
if (data.status == "ok") {
$("#result").empty().append("Cluster terminated succesfully");
......
......@@ -86,7 +86,8 @@ def web_terminate(username, cluster_id):
template_vars = {
"cluster_name": cluster["name"],
"cluster_id": cluster_id,
"user": username
"user": username,
"user_id": user_id
}
return render_template('terminate.html', **template_vars)
......@@ -118,6 +119,7 @@ def web_spark_submit(username):
user_id = state.get_user_id(username)
# FIXME: check user_id
template_vars = {
'user_id': user_id,
'user': username
}
return render_template('submit.html', **template_vars)
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