2021-01-02 23:10:01 +00:00
|
|
|
|
|
|
|
from flask import Blueprint
|
|
|
|
from flask import current_app
|
|
|
|
from flask import request
|
|
|
|
from werkzeug.exceptions import abort
|
|
|
|
|
|
|
|
from capsulflask.db import get_model, my_exec_info_message
|
|
|
|
|
2021-01-03 01:07:43 +00:00
|
|
|
bp = Blueprint("hub", __name__, url_prefix="/hub")
|
2021-01-02 23:10:01 +00:00
|
|
|
|
|
|
|
def authorized_for_host(id):
|
|
|
|
auth_header_value = request.headers.get('Authorization').replace("Bearer ", "")
|
|
|
|
return get_model().authorized_for_host(id, auth_header_value)
|
|
|
|
|
2021-01-04 01:17:30 +00:00
|
|
|
@bp.route("/heartbeat/<string:host_id>", methods=("POST"))
|
|
|
|
def heartbeat(host_id):
|
|
|
|
if authorized_for_host(host_id):
|
|
|
|
get_model().host_heartbeat(host_id)
|
2021-01-02 23:10:01 +00:00
|
|
|
else:
|
2021-01-04 01:17:30 +00:00
|
|
|
current_app.logger.info(f"/hub/heartbeat/{host_id} returned 401: invalid token")
|
|
|
|
return abort(401, "invalid host id or token")
|
|
|
|
|
|
|
|
@bp.route("/claim-operation/<int:operation_id>/<string:host_id>", methods=("POST"))
|
|
|
|
def claim_operation(operation_id: int, host_id: str):
|
|
|
|
if authorized_for_host(host_id):
|
|
|
|
exists = get_model().host_operation_exists(operation_id, host_id)
|
|
|
|
if not exists:
|
|
|
|
return abort(404, "host operation not found")
|
|
|
|
claimed = get_model().claim_operation(operation_id, host_id)
|
|
|
|
if claimed:
|
|
|
|
return "ok"
|
|
|
|
else:
|
|
|
|
return abort(409, "operation was already assigned to another host")
|
|
|
|
else:
|
|
|
|
current_app.logger.info(f"/hub/claim-operation/{operation_id}/{host_id} returned 401: invalid token")
|
2021-01-02 23:10:01 +00:00
|
|
|
return abort(401, "invalid host id or token")
|
|
|
|
|