forked from 3wordchant/capsul-flask
trying to set up assignment of create operation
This commit is contained in:
@ -18,9 +18,20 @@ def authorized_as_hub(id):
|
||||
@bp.route("/heartbeat", methods=("POST"))
|
||||
def heartbeat():
|
||||
if authorized_as_hub(id):
|
||||
# make request to hub-domain.com/hub/heartbeat/{current_app.config["SPOKE_HOST_ID"]}
|
||||
# succeed or fail based on whether the request succeeds or fails.
|
||||
pass
|
||||
url = f"{current_app.config['HUB_URL']}/hub/heartbeat/{current_app.config['SPOKE_HOST_ID']}"
|
||||
authorization_header = f"Bearer {current_app.config['SPOKE_HOST_TOKEN']}"
|
||||
result = current_app.config['HTTP_CLIENT'].post_json_sync(url, body=None, authorization_header=authorization_header)
|
||||
if result.status_code == -1:
|
||||
current_app.logger.info(f"/hosts/heartbeat returned 503: hub at {url} timed out or cannot be reached")
|
||||
return abort(503, "Service Unavailable: hub timed out or cannot be reached")
|
||||
if result.status_code == 401:
|
||||
current_app.logger.info(f"/hosts/heartbeat returned 502: hub at {url} rejected our token")
|
||||
return abort(502, "hub rejected our token")
|
||||
if result.status_code != 200:
|
||||
current_app.logger.info(f"/hosts/heartbeat returned 502: hub at {url} returned {result.status_code}")
|
||||
return abort(502, "Bad Gateway: hub did not return 200")
|
||||
|
||||
return "OK"
|
||||
else:
|
||||
current_app.logger.info(f"/hosts/heartbeat returned 401: invalid hub token")
|
||||
return abort(401, "invalid hub token")
|
||||
@ -90,9 +101,21 @@ def handle_create(request_body):
|
||||
current_app.logger.info(f"/hosts/operation returned 400: {error_message}")
|
||||
return abort(400, f"bad request; {error_message}")
|
||||
|
||||
# try to aquire operation_id
|
||||
assignment_status = "assigned"
|
||||
# only one host should create the vm, so we first race to assign this create operation to ourselves.
|
||||
# only one host will win this race
|
||||
authorization_header = f"Bearer {current_app.config['SPOKE_HOST_TOKEN']}"
|
||||
url = f"{current_app.config['HUB_URL']}/hub/claim-operation/{request_body['operation_id']}/{current_app.config['SPOKE_HOST_ID']}"
|
||||
result = current_app.config['HTTP_CLIENT'].post_json_sync(url, body=None, authorization_header=authorization_header)
|
||||
|
||||
assignment_status = ""
|
||||
if result.status_code == 200:
|
||||
assignment_status = "assigned"
|
||||
elif result.status_code == 409:
|
||||
assignment_status = "assigned_to_other_host"
|
||||
else:
|
||||
current_app.logger.info(f"{url} returned {result.status_code}: {result.body}")
|
||||
return abort(503, f"hub did not cleanly handle our request to claim the create operation")
|
||||
|
||||
if assignment_status == "assigned":
|
||||
try:
|
||||
current_app.config['SPOKE_MODEL'].create(
|
||||
@ -108,7 +131,7 @@ def handle_create(request_body):
|
||||
params = f"email='{request_body['email']}', id='{request_body['id']}', "
|
||||
params = f"{params}, template_image_file_name='{request_body['template_image_file_name']}', vcpus='{request_body['vcpus']}'"
|
||||
params = f"{params}, memory_mb='{request_body['memory_mb']}', ssh_public_keys='{request_body['ssh_public_keys']}'"
|
||||
current_app.logger.error(f"current_app.config['SPOKE_MODEL'].create({params}) failed: {error_message}")
|
||||
current_app.logger.error(f"spoke_model.create({params}) failed: {error_message}")
|
||||
return jsonify(dict(assignment_status=assignment_status, error_message=error_message))
|
||||
|
||||
return jsonify(dict(assignment_status=assignment_status))
|
||||
|
Reference in New Issue
Block a user