From e301137130ed3b8d5630aea0595a63b4ee8d2cd9 Mon Sep 17 00:00:00 2001
From: Kento HASEGAWA
Date: Thu, 16 Aug 2018 16:26:02 +0900
Subject: [PATCH] Add adcclilib.py module to access the adc server (#14)
---
comm/server/adcclilib.py | 50 ++++++++++++++++++++
comm/server/main.py | 34 +++++++++++++
comm/server/static/js/pynq-manager.js | 37 +++++++++++++++
comm/server/templates/part_client_table.html | 5 ++
4 files changed, 126 insertions(+)
create mode 100644 comm/server/adcclilib.py
diff --git a/comm/server/adcclilib.py b/comm/server/adcclilib.py
new file mode 100644
index 0000000..a413e64
--- /dev/null
+++ b/comm/server/adcclilib.py
@@ -0,0 +1,50 @@
+#!/usr/bin/env python3
+
+import subprocess
+
+PYTHON2 = "/usr/bin/python"
+ADCCLI = "/home/pi/adc2018/conmgr/client/adccli"
+
+def _exec_adccli(cmd):
+ exec_cmd = "{} {} {}".format(PYTHON2, ADCCLI, cmd).strip()
+ p = subprocess.run(exec_cmd, stdout=subprocess.PIPE, shell=True)
+ res = p.stdout.decode().strip()
+ return res
+
+def login(url, username, password):
+ cmd = "--URL='{}' --username='{}' --password='{}' login".format(url, username, password)
+ return _exec_adccli(cmd)
+
+def logout():
+ cmd = "logout"
+ return _exec_adccli(cmd)
+
+def whoami():
+ cmd = "whoami"
+ return _exec_adccli(cmd)
+
+def put_message(message):
+ cmd = "put-user-alive '{}'"
+ return _exec_adccli(cmd)
+
+def post_user_q(qnum, filepath):
+ cmd = "post-user-q {} {}".format(qnum, filepath)
+ return _exec_adccli(cmd)
+
+def get_q_all(outpath):
+ cmd = "get-q"
+ question_list = _exec_adccli(cmd).split("\n")
+
+ for v in question_list:
+ out_file_path = "{}/NL_Q{:02d}.txt".format(outpath, int(v))
+ cmd = "--output {} get-q {}".format(out_file_path, v.rstrip())
+ _exec_adccli(cmd)
+
+def put_a(qnum, filepath):
+ cmd = "put-a {} {}".format(qnum, filepath)
+ return _exec_adccli(cmd)
+
+def put_a_info(qnum, cpu, mem, misc):
+ cmd = "put-a-info {} {} {} '{}'".format(qnum, cpu, mem, misc)
+ return _exec_adccli(cmd)
+
diff --git a/comm/server/main.py b/comm/server/main.py
index 4511d98..2b097e8 100644
--- a/comm/server/main.py
+++ b/comm/server/main.py
@@ -22,6 +22,8 @@ from queue import Queue
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../../solver')
import BoardStr
+import adcclilib
+
app = Flask(__name__)
app_args = {}
questions = None
@@ -440,6 +442,37 @@ def get_clients():
return json.dumps(res)
+@app.route("/adccli-login")
+def adccli_login():
+
+ global app_args
+
+ with open(app_args['adccli'], 'r') as fp:
+ d = json.load(fp)
+
+ r = adcclilib.login(d['url'], d['username'], d['password'])
+ res = {'message': r}
+
+ return json.dumps(res)
+
+@app.route("/adccli-logout")
+def adccli_logout():
+ r = adcclilib.logout()
+ res = {'message': r}
+ return json.dumps(res)
+
+@app.route("/adccli-whoami")
+def adccli_whoami():
+ global app_args
+
+ with open(app_args['adccli'], 'r') as fp:
+ d = json.load(fp)
+
+ r = adcclilib.whoami()
+
+ res = {'message': r, 'status': r == d['username']}
+ return json.dumps(res)
+
@app.route("/board-viewer", methods=["GET"])
def show_board_viewer():
@@ -524,6 +557,7 @@ if __name__ == "__main__":
parser = argparse.ArgumentParser(description="PYNQ control panel.")
parser.add_argument("-c", "--client", action="store", type=str, default=None, required=True, help="Client list.")
+ parser.add_argument("-a", "--adccli", action="store", type=str, default="./adccli.json", help="Path to the ADCCLI configuration json file.")
parser.add_argument("-q", "--question", action="store", type=str, default="./problems", help="Path to the question folder.")
parser.add_argument("-o", "--out", action="store", type=str, default="./answers", help="Path to the output folder.")
parser.add_argument("--debug", action="store_true", default=False, help="Debug mode.")
diff --git a/comm/server/static/js/pynq-manager.js b/comm/server/static/js/pynq-manager.js
index a48aa8d..fa801c1 100644
--- a/comm/server/static/js/pynq-manager.js
+++ b/comm/server/static/js/pynq-manager.js
@@ -145,6 +145,43 @@ $(function(){
}).done(function(d){
$("#client-control-pane").html("");
$("#client-control-pane").html(d);
+
+ $("#adccli-login-button").click(function(){
+ var $obj = $(this);
+ $obj.prop("disabled", "disabled");
+ $.ajax({
+ type: "GET",
+ url: "/adccli-login",
+ dataType: "json",
+ }).done((data)=>{
+ $obj.prop("disabled", false);
+ alert(data['message']);
+ });
+ });
+ $("#adccli-logout-button").click(function(){
+ var $obj = $(this);
+ $obj.prop("disabled", "disabled");
+ $.ajax({
+ type: "GET",
+ url: "/adccli-logout",
+ dataType: "json",
+ }).done((data)=>{
+ $obj.prop("disabled", false);
+ alert(data['message']);
+ });
+ });
+
+ $.ajax({
+ type: "GET",
+ url: "/adccli-whoami",
+ dataType: "json",
+ }).done((data)=>{
+ if(data['status'])
+ $("#adccli-status").text("ログイン中");
+ else
+ $("#adccli-status").text("ログアウト");
+ });
+
pm.getStatus();
});
}
diff --git a/comm/server/templates/part_client_table.html b/comm/server/templates/part_client_table.html
index 6211db7..af8fe2e 100644
--- a/comm/server/templates/part_client_table.html
+++ b/comm/server/templates/part_client_table.html
@@ -9,6 +9,11 @@ Viewer Mode
{% endif %}
+自動運営システム
+
+
+
+
クライアント