118 lines
3.4 KiB
Python
118 lines
3.4 KiB
Python
# coding: utf-8
|
|
|
|
import json
|
|
from flask import request
|
|
from flask import Blueprint
|
|
from flask import jsonify
|
|
from app.cloud.controllers.tasks.server import ControllerTasksServer
|
|
from app.cloud.controllers.servers.server import ControllerServerStatistics
|
|
from app.cloud.controllers.containers.server import ControllerContainersServer
|
|
|
|
viewServerAPI = Blueprint('server_api', __name__, url_prefix='/server_api')
|
|
|
|
|
|
@viewServerAPI.route('/tasks', methods=['GET'])
|
|
def tasks_list():
|
|
node_id = request.args.get('node_id')
|
|
node_secret = request.args.get('node_secret')
|
|
server_api = ControllerTasksServer(node_id, node_secret)
|
|
|
|
# auth request
|
|
if not server_api.auth():
|
|
# status: 403 - access denied
|
|
return jsonify({'status': 403})
|
|
|
|
result = dict()
|
|
result['status'] = 0
|
|
result['results'] = []
|
|
|
|
for task in server_api.get():
|
|
result['results'].append({
|
|
'id': task.id,
|
|
'datacenter': task.datacenter.id,
|
|
'server': task.server.id,
|
|
'user': task.user.id,
|
|
'task': task.task,
|
|
'created': task.created,
|
|
'status': task.status,
|
|
'plain': json.loads(task.plain),
|
|
})
|
|
return jsonify(result)
|
|
|
|
|
|
@viewServerAPI.route('/task_status_update', methods=['GET'])
|
|
def task_status_update():
|
|
node_id = request.args.get('node_id')
|
|
node_secret = request.args.get('node_secret')
|
|
server_api = ControllerTasksServer(node_id, node_secret)
|
|
|
|
# auth request
|
|
if not server_api.auth():
|
|
# status: 403 - access denied
|
|
return jsonify({'status': 403})
|
|
|
|
task_id = request.args.get('task_id')
|
|
status = int(request.args.get('status'))
|
|
|
|
server_api.update(task_id, status)
|
|
|
|
return jsonify({'status': 0})
|
|
|
|
|
|
@viewServerAPI.route('/report/container_status', methods=['POST'])
|
|
def report_container_status():
|
|
node_id = request.form['node_id']
|
|
node_secret = request.form['node_secret']
|
|
|
|
# print request.form['node_id']
|
|
# auth request
|
|
if not ControllerTasksServer(node_id, node_secret).auth():
|
|
# status: 403 - access denied
|
|
return jsonify({'status': 403})
|
|
|
|
"""
|
|
{
|
|
"container_id": "16459f60-a1ee-11e5-9108-28d244e159e9",
|
|
"cpu_use": 644394623336,
|
|
"memory_use": 614473728,
|
|
"link": "vethB2RLMU"
|
|
"tx_bytes": 48337383,
|
|
"rx_bytes": 1049439046,
|
|
"total_bytes": 1097776429
|
|
}
|
|
"""
|
|
statistics = json.loads(request.form['status'])
|
|
# print statistics
|
|
container_id = statistics['container_id']
|
|
|
|
if 'cpu_use' not in statistics:
|
|
return False
|
|
if 'memory_use' not in statistics:
|
|
return False
|
|
if 'tx_bytes' not in statistics:
|
|
return False
|
|
if 'rx_bytes' not in statistics:
|
|
return False
|
|
if 'total_bytes' not in statistics:
|
|
return False
|
|
|
|
# print container_id, ,
|
|
print statistics
|
|
if ControllerContainersServer().exists(container_id):
|
|
ControllerServerStatistics().write(
|
|
container_id,
|
|
statistics['cpu_use'],
|
|
statistics['memory_use'],
|
|
int(statistics['size']),
|
|
int(statistics['tx_bytes']),
|
|
int(statistics['rx_bytes']),
|
|
int(statistics['total_bytes'])
|
|
|
|
# int(statistics['rx_bytes']) / 1024 / 1024,
|
|
# int(statistics['total_bytes']) / 1024 / 1024
|
|
)
|
|
# import datetime
|
|
# print container_id
|
|
# print ControllerContainersServer().exists(container_id)
|
|
# print datetime.datetime.now
|
|
return jsonify({})
|