2015-12-01 02:43:10 +03:00
|
|
|
|
# coding: utf-8
|
|
|
|
|
|
|
|
|
|
import json
|
2016-04-20 03:39:13 +03:00
|
|
|
|
from flask import Blueprint, jsonify, request, g
|
|
|
|
|
from flask_httpauth import HTTPBasicAuth
|
2016-04-02 00:42:22 +03:00
|
|
|
|
from SWSCloudCore.controllers.servers.server import ControllerServerStatistics
|
2016-04-20 03:39:13 +03:00
|
|
|
|
from SWSCloudCore.controllers.servers.server import ControllerServerServers
|
2016-04-02 00:42:22 +03:00
|
|
|
|
from SWSCloudCore.controllers.tasks.server import ControllerTasksServer
|
|
|
|
|
from SWSCloudCore.controllers.containers.server import ControllerContainersServer
|
2015-12-01 02:43:10 +03:00
|
|
|
|
|
2016-04-20 03:39:13 +03:00
|
|
|
|
api_auth = HTTPBasicAuth()
|
2016-06-12 01:33:23 +03:00
|
|
|
|
viewServerAPI = Blueprint('ServerAPI', __name__, url_prefix='/server_api')
|
2015-12-01 02:43:10 +03:00
|
|
|
|
|
|
|
|
|
|
2016-04-20 03:39:13 +03:00
|
|
|
|
@api_auth.get_password
|
|
|
|
|
def get_pw(server_id):
|
|
|
|
|
if ControllerServerServers().exists(server_id):
|
|
|
|
|
g.server_id = server_id
|
|
|
|
|
return ControllerServerServers().get_secret(g.server_id)
|
|
|
|
|
return None
|
|
|
|
|
|
2015-12-01 02:43:10 +03:00
|
|
|
|
|
2016-04-20 03:39:13 +03:00
|
|
|
|
@api_auth.error_handler
|
|
|
|
|
def auth_error():
|
|
|
|
|
return jsonify(status='error', description='Unauthorized'), 403
|
2015-12-01 02:43:10 +03:00
|
|
|
|
|
2016-04-20 03:39:13 +03:00
|
|
|
|
|
|
|
|
|
@viewServerAPI.route('/ping')
|
|
|
|
|
@api_auth.login_required
|
|
|
|
|
def ping():
|
|
|
|
|
"""
|
|
|
|
|
Тест. Проверка соединения и авторизации
|
|
|
|
|
"""
|
|
|
|
|
return jsonify(ping='pong', server_id=g.server_id), 200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# TASKS
|
|
|
|
|
@viewServerAPI.route('/tasks', methods=['GET'])
|
|
|
|
|
@api_auth.login_required
|
|
|
|
|
def tasks_list():
|
|
|
|
|
"""
|
|
|
|
|
Список
|
|
|
|
|
"""
|
|
|
|
|
server_api = ControllerTasksServer(g.server_id)
|
|
|
|
|
result = dict(tasks=list())
|
2015-12-01 02:43:10 +03:00
|
|
|
|
|
|
|
|
|
for task in server_api.get():
|
2016-08-01 09:44:55 +03:00
|
|
|
|
result['results'].append(dict(
|
|
|
|
|
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)))
|
2015-12-01 02:43:10 +03:00
|
|
|
|
return jsonify(result)
|
|
|
|
|
|
|
|
|
|
|
2016-04-20 03:39:13 +03:00
|
|
|
|
@viewServerAPI.route('/task', methods=['GET'])
|
|
|
|
|
@api_auth.login_required
|
|
|
|
|
def task_item():
|
|
|
|
|
"""
|
|
|
|
|
Самая первая задача в очереди
|
|
|
|
|
"""
|
|
|
|
|
sapi = ControllerTasksServer(g.server_id)
|
|
|
|
|
|
|
|
|
|
# Если задач нет, то надо вернуть ответ с кодом 204 (no content)
|
|
|
|
|
if sapi.count() == 0:
|
|
|
|
|
return '', 204
|
|
|
|
|
|
|
|
|
|
task = sapi.queue_item_oldest_get(status=0)
|
|
|
|
|
|
|
|
|
|
result = dict(
|
|
|
|
|
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),
|
|
|
|
|
)
|
2016-08-01 09:44:55 +03:00
|
|
|
|
return jsonify(task=result, status='success')
|
2016-04-20 03:39:13 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@viewServerAPI.route('/tasks/<task_id>', methods=['PUT'])
|
|
|
|
|
@api_auth.login_required
|
|
|
|
|
def task_update(task_id):
|
|
|
|
|
"""
|
|
|
|
|
Обновление статуса задачи
|
|
|
|
|
"""
|
|
|
|
|
server_api = ControllerTasksServer(g.server_id)
|
2015-12-01 02:43:10 +03:00
|
|
|
|
|
2016-04-20 03:39:13 +03:00
|
|
|
|
if 'status' in request.form:
|
2016-08-01 09:44:55 +03:00
|
|
|
|
status = int(request.form.get('status'))
|
2016-04-20 03:39:13 +03:00
|
|
|
|
server_api.update(task_id, status)
|
2016-08-01 09:44:55 +03:00
|
|
|
|
return jsonify(status=0)
|
2015-12-14 01:49:38 +03:00
|
|
|
|
|
|
|
|
|
|
2016-04-20 03:39:13 +03:00
|
|
|
|
# CONTAINERS
|
|
|
|
|
@viewServerAPI.route('/containers/stats/<uuid:container_id>', methods=['POST'])
|
|
|
|
|
@api_auth.login_required
|
|
|
|
|
def report_container_status(container_id):
|
2015-12-14 01:49:38 +03:00
|
|
|
|
"""
|
|
|
|
|
{
|
|
|
|
|
"cpu_use": 644394623336,
|
|
|
|
|
"memory_use": 614473728,
|
|
|
|
|
"link": "vethB2RLMU"
|
|
|
|
|
"tx_bytes": 48337383,
|
|
|
|
|
"rx_bytes": 1049439046,
|
|
|
|
|
"total_bytes": 1097776429
|
|
|
|
|
}
|
|
|
|
|
"""
|
2016-04-20 03:39:13 +03:00
|
|
|
|
|
2015-12-14 05:00:33 +03:00
|
|
|
|
statistics = json.loads(request.form['status'])
|
|
|
|
|
|
|
|
|
|
if 'cpu_use' not in statistics:
|
2015-12-16 08:39:53 +03:00
|
|
|
|
return jsonify({})
|
2015-12-14 05:00:33 +03:00
|
|
|
|
if 'memory_use' not in statistics:
|
2015-12-16 08:39:53 +03:00
|
|
|
|
return jsonify({})
|
2015-12-14 05:00:33 +03:00
|
|
|
|
if 'tx_bytes' not in statistics:
|
2015-12-16 08:39:53 +03:00
|
|
|
|
return jsonify({})
|
2015-12-14 05:00:33 +03:00
|
|
|
|
if 'rx_bytes' not in statistics:
|
2015-12-16 08:39:53 +03:00
|
|
|
|
return jsonify({})
|
2015-12-14 05:00:33 +03:00
|
|
|
|
if 'total_bytes' not in statistics:
|
2015-12-16 08:39:53 +03:00
|
|
|
|
return jsonify({})
|
|
|
|
|
|
|
|
|
|
# create or update state
|
|
|
|
|
if ControllerServerStatistics().state_exists(container_id):
|
|
|
|
|
ControllerServerStatistics().state_update(container_id, statistics)
|
|
|
|
|
else:
|
|
|
|
|
ControllerServerStatistics().state_create(container_id, statistics)
|
2015-12-14 05:00:33 +03:00
|
|
|
|
|
|
|
|
|
if ControllerContainersServer().exists(container_id):
|
|
|
|
|
ControllerServerStatistics().write(
|
|
|
|
|
container_id,
|
|
|
|
|
statistics['cpu_use'],
|
|
|
|
|
statistics['memory_use'],
|
2015-12-15 11:23:25 +03:00
|
|
|
|
int(statistics['size']),
|
2015-12-14 05:00:33 +03:00
|
|
|
|
int(statistics['tx_bytes']),
|
|
|
|
|
int(statistics['rx_bytes']),
|
|
|
|
|
int(statistics['total_bytes'])
|
|
|
|
|
)
|
2016-04-20 03:39:13 +03:00
|
|
|
|
return jsonify(status='success')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@viewServerAPI.route('/containers/status/', methods=['GET'])
|
|
|
|
|
@api_auth.login_required
|
|
|
|
|
def containers_status():
|
|
|
|
|
"""
|
|
|
|
|
Состояние всех контейнеров в биллинге, и привести контейнеры к указанному состоянию
|
|
|
|
|
На случай, когда нода была перезагружена.
|
|
|
|
|
"""
|
|
|
|
|
# Get container list (with status) located on node
|
|
|
|
|
return jsonify(
|
|
|
|
|
status='success',
|
|
|
|
|
containers=ControllerContainersServer().get_containers_by_server(g.server_id)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@viewServerAPI.route('/containers/status/<uuid:container_id>', methods=['GET', 'POST'])
|
|
|
|
|
@api_auth.login_required
|
|
|
|
|
def container_status_update(container_id):
|
|
|
|
|
"""
|
|
|
|
|
Если на ноде при работе с контейнером что-то поло не так,
|
|
|
|
|
то нода отправляет сообщение об ошибке и изменяет статус контейнера.
|
|
|
|
|
Отправленное сообщение может быть прочитано администратором или владельцем контейнера
|
|
|
|
|
"""
|
|
|
|
|
# Check exists container
|
|
|
|
|
if not ControllerContainersServer().exists(container_id):
|
|
|
|
|
return '', 404
|
|
|
|
|
|
|
|
|
|
# Get information about container
|
|
|
|
|
if request.method == 'GET':
|
|
|
|
|
return jsonify(
|
|
|
|
|
status='success',
|
|
|
|
|
container=ControllerContainersServer().get_container(container_id)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Set container status
|
|
|
|
|
if 'status' in request.form:
|
|
|
|
|
ControllerContainersServer().set_container_status(
|
|
|
|
|
container_id,
|
|
|
|
|
request.form['status']
|
|
|
|
|
)
|
|
|
|
|
# TODO: 0000001
|
2015-12-16 08:39:53 +03:00
|
|
|
|
|
2016-04-20 03:39:13 +03:00
|
|
|
|
return jsonify(status='success')
|
|
|
|
|
return jsonify(
|
|
|
|
|
status='error',
|
|
|
|
|
message='must contain parameter: status'
|
|
|
|
|
)
|