Commit fb2eb3c3 authored by Christos Stavrakakis's avatar Christos Stavrakakis
Browse files

cyclades: Helper function for commissioning

Create 'quotas.handle_resource_commission' function for handling
commissions for VMs and Networks.
parent 2f5650df
......@@ -96,20 +96,14 @@ def handle_vm_quotas(vm, job_id, job_opcode, job_status, job_fields):
# Commission for this change has not been issued, or the issued
# commission was unaware of the current change. Reject all previous
# commissions and create a new one in forced mode!
previous_serial = vm.serial
if previous_serial and not previous_serial.resolved:
quotas.resolve_vm_commission(previous_serial)
commission_name = ("client: dispatcher, resource: %s, ganeti_job: %s"
% (vm, job_id))
serial = quotas.issue_commission(user=vm.userid,
source=quotas.DEFAULT_SOURCE,
provisions=commission_info,
name=commission_name,
force=True,
auto_accept=True)
# Clear VM's serial. Expected job may arrive later. However correlated
# serial must not be accepted, since it reflects a previous VM state
vm.serial = None
quotas.handle_resource_commission(vm, action,
commission_info=commission_info,
commission_name=commission_name,
force=True,
auto_accept=True)
log.debug("Issued new commission: %s", vm.serial)
return vm
......
......@@ -81,25 +81,10 @@ def server_command(action):
validate_server_action(vm, action)
vm.action = action
# Resolve(reject) previous serial if it is still pending!!
previous_serial = vm.serial
if previous_serial and not previous_serial.resolved:
quotas.resolve_vm_commission(serial=previous_serial)
# Check if action is quotable and issue the corresponding
# commission
serial = None
commission_info = quotas.get_commission_info(vm, action=action)
if commission_info is not None:
# Issue new commission, associate it with the VM
commission_name = "client: api, resource %s" % vm
serial = quotas.issue_commission(user=user_id,
source=quotas.DEFAULT_SOURCE,
provisions=commission_info,
name=commission_name,
force=False,
auto_accept=False)
vm.serial = serial
commission_name = "client: api, resource: %s" % vm
quotas.handle_resource_commission(vm, action=action,
commission_name=commission_name)
vm.save()
# XXX: Special case for server creation!
if action == "BUILD":
......
......@@ -349,7 +349,39 @@ def reverse_quantities(resources):
return dict((r, -s) for r, s in resources.items())
def resolve_vm_commission(serial):
def handle_resource_commission(resource, action, commission_name,
commission_info=None, force=False,
auto_accept=False):
"""Handle a issuing of a commission for a resource.
Create a new commission for a resource based on the action that
is performed. If the resource has a previous pending commission,
resolved it before issuing the new one.
"""
# Try to resolve previous serial
resolve_commission(resource.serial)
# Check if action is quotable and issue the corresponding commission
serial = None
if commission_info is None:
commission_info = get_commission_info(resource, action=action)
if commission_info is not None:
# Issue new commission, associate it with the resource
if commission_name is None:
commission_name = "client: api, resource %s" % resource
serial = issue_commission(user=resource.userid,
source=DEFAULT_SOURCE,
provisions=commission_info,
name=commission_name,
force=False,
auto_accept=False)
resource.serial = serial
def resolve_commission(serial):
if serial is None or serial.resolved:
return
log.warning("Resolving pending commission: %s", serial)
if not serial.pending and serial.accept:
accept_serial(serial)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment