forked from 3wordchant/capsul-flask
fixing capsul creation after I broke it with the pre-allocated IP
address changes
This commit is contained in:
@ -54,9 +54,14 @@ def claim_operation(operation_id: int, host_id: str):
|
||||
current_app.logger.error(error_message)
|
||||
return abort(404, error_message)
|
||||
|
||||
# right now if there is a can_claim_handler there needs to be a corresponding on_claimed_handler.
|
||||
# this is sole-ly due to laziness in error handling.
|
||||
can_claim_handlers = {
|
||||
"create": can_claim_create,
|
||||
}
|
||||
on_claimed_handlers = {
|
||||
"create": on_create_claimed,
|
||||
}
|
||||
error_message = ""
|
||||
payload = None
|
||||
payload_is_dict = False
|
||||
@ -77,7 +82,7 @@ def claim_operation(operation_id: int, host_id: str):
|
||||
except:
|
||||
error_message = "could not parse payload as json"
|
||||
|
||||
if error_message is not "":
|
||||
if error_message != "":
|
||||
error_message = f"{host_id} can't claim operation {operation_id} because {error_message}"
|
||||
current_app.logger.error(error_message)
|
||||
return abort(400, error_message)
|
||||
@ -87,20 +92,22 @@ def claim_operation(operation_id: int, host_id: str):
|
||||
|
||||
# invoke the appropriate can_claim_handler for this operation type
|
||||
result_tuple = can_claim_handlers[payload['type']](payload, host_id)
|
||||
payload_json = result_tuple[0]
|
||||
modified_payload = result_tuple[0]
|
||||
error_message = result_tuple[1]
|
||||
|
||||
if error_message is not "":
|
||||
if error_message != "":
|
||||
error_message = f"{host_id} can't claim operation {operation_id} because {error_message}"
|
||||
current_app.logger.error(error_message)
|
||||
return abort(400, error_message)
|
||||
|
||||
claimed = get_model().claim_operation(operation_id, host_id)
|
||||
if claimed:
|
||||
get_model().update_operation(operation_id, payload_json)
|
||||
modified_payload_json = json.dumps(modified_payload)
|
||||
get_model().update_operation(operation_id, modified_payload_json)
|
||||
on_claimed_handlers[payload['type']](modified_payload, host_id)
|
||||
|
||||
response = make_response(payload_json)
|
||||
response.header.set("Content-Type", "application/json")
|
||||
response = make_response(modified_payload_json)
|
||||
response.headers.set("Content-Type", "application/json")
|
||||
|
||||
return response
|
||||
else:
|
||||
@ -116,7 +123,7 @@ def can_claim_create(payload, host_id) -> (str, str):
|
||||
if host_id not in hosts:
|
||||
return "", f"the host \"{host_id}\" does not appear to have any networks."
|
||||
|
||||
networks = hosts[host_id].networks
|
||||
networks = hosts[host_id]['networks']
|
||||
|
||||
vms_by_host_and_network = get_model().non_deleted_vms_by_host_and_network(host_id)
|
||||
|
||||
@ -151,6 +158,18 @@ def can_claim_create(payload, host_id) -> (str, str):
|
||||
return "", f"host \"{host_id}\" does not have any avaliable IP addresses on any of its networks."
|
||||
|
||||
payload["network_name"] = allocated_network_name
|
||||
payload["public_ipv4_address"] = allocated_ipv4_address
|
||||
payload["public_ipv4"] = allocated_ipv4_address
|
||||
|
||||
return json.dumps(payload), ""
|
||||
return payload, ""
|
||||
|
||||
def on_create_claimed(payload, host_id):
|
||||
get_model().create_vm(
|
||||
email=payload['email'],
|
||||
id=payload['id'],
|
||||
size=payload['size'],
|
||||
os=payload['os'],
|
||||
host=host_id,
|
||||
network_name=payload['network_name'],
|
||||
public_ipv4=payload['public_ipv4'],
|
||||
ssh_authorized_keys=list(map(lambda x: x["name"], payload['ssh_authorized_keys'])),
|
||||
)
|
Reference in New Issue
Block a user