Exemplo n.º 1
0
 def test_commissions(self):
     flavor = mfactory.FlavorFactory(cpu=2, ram=1024, disk=20)
     vm = mfactory.VirtualMachineFactory(flavor=flavor)
     mfactory.VolumeFactory(size=20, machine=vm, deleted=False,
                            status="IN_USE",
                            delete_on_termination=True)
     vm.volumes.update(project=vm.project)
     #commission = quotas.get_commission_info(vm, "BUILD")
     #self.assertEqual({"cyclades.vm": 1,
     #                  "cyclades.cpu": 2,
     #                  "cyclades.cpu": 2,
     #                  "cyclades.ram": 1048576 * 1024,
     #                  "cyclades.ram": 1048576 * 1024,
     #                  "cyclades.disk": 1073741824 * 20}, commission)
     vm.operstate = "STARTED"
     vm.save()
     project = vm.project
     commission = quotas.get_commission_info(vm, "STOP")
     self.assertEqual({(project, "cyclades.cpu"): -2,
                       (project, "cyclades.ram"): 1048576 * -1024}, commission)
     # Check None quotas if vm is already stopped
     vm.operstate = "STOPPED"
     vm.save()
     commission = quotas.get_commission_info(vm, "STOP")
     self.assertEqual(None, commission)
     commission = quotas.get_commission_info(vm, "START")
     self.assertEqual({(project, "cyclades.cpu"): 2,
                       (project, "cyclades.ram"): 1048576 * 1024}, commission)
     vm.operstate = "STARTED"
     vm.save()
     commission = quotas.get_commission_info(vm, "DESTROY")
     self.assertEqual({(project, "cyclades.vm"): -1,
                       (project, "cyclades.total_cpu"): -2,
                       (project, "cyclades.cpu"): -2,
                       (project, "cyclades.total_ram"): 1048576 * -1024,
                       (project, "cyclades.ram"): 1048576 * -1024,
                       (project, "cyclades.disk"): 1073741824 * -20}, commission)
     vm.operstate = "STOPPED"
     vm.save()
     commission = quotas.get_commission_info(vm, "DESTROY")
     self.assertEqual({(project, "cyclades.vm"): -1,
                       (project, "cyclades.total_cpu"): -2,
                       (project, "cyclades.total_ram"): -1024 << 20,
                       (project, "cyclades.disk"): -20 << 30}, commission)
     commission = quotas.get_commission_info(vm, "RESIZE")
     self.assertEqual(None, commission)
     commission = quotas.get_commission_info(vm, "RESIZE",
                                             {"beparams": {"vcpus": 4,
                                                           "maxmem": 2048}})
     self.assertEqual({(project, "cyclades.total_cpu"): 2,
                       (project, "cyclades.total_ram"): 1048576 * 1024}, commission)
     vm.operstate = "STOPPED"
     vm.save()
     commission = quotas.get_commission_info(vm, "REBOOT")
     self.assertEqual({(project, "cyclades.cpu"): 2,
                       (project, "cyclades.ram"): 1048576 * 1024}, commission)
     vm.operstate = "STARTED"
     vm.save()
     commission = quotas.get_commission_info(vm, "REBOOT")
     self.assertEqual(None, commission)
Exemplo n.º 2
0
 def test_commissions(self):
     flavor = mfactory.FlavorFactory(cpu=2, ram=1024, disk=20)
     vm = mfactory.VirtualMachineFactory(flavor=flavor)
     #commission = quotas.get_commission_info(vm, "BUILD")
     #self.assertEqual({"cyclades.vm": 1,
     #                  "cyclades.cpu": 2,
     #                  "cyclades.cpu": 2,
     #                  "cyclades.ram": 1048576 * 1024,
     #                  "cyclades.ram": 1048576 * 1024,
     #                  "cyclades.disk": 1073741824 * 20}, commission)
     vm.operstate = "STARTED"
     vm.save()
     commission = quotas.get_commission_info(vm, "STOP")
     self.assertEqual({"cyclades.cpu": -2,
                       "cyclades.ram": 1048576 * -1024}, commission)
     # Check None quotas if vm is already stopped
     vm.operstate = "STOPPED"
     vm.save()
     commission = quotas.get_commission_info(vm, "STOP")
     self.assertEqual(None, commission)
     commission = quotas.get_commission_info(vm, "START")
     self.assertEqual({"cyclades.cpu": 2,
                       "cyclades.ram": 1048576 * 1024}, commission)
     vm.operstate = "STARTED"
     vm.save()
     commission = quotas.get_commission_info(vm, "DESTROY")
     self.assertEqual({"cyclades.vm": -1,
                       "cyclades.total_cpu": -2,
                       "cyclades.cpu": -2,
                       "cyclades.total_ram": 1048576 * -1024,
                       "cyclades.ram": 1048576 * -1024,
                       "cyclades.disk": 1073741824 * -20}, commission)
     vm.operstate = "STOPPED"
     vm.save()
     commission = quotas.get_commission_info(vm, "DESTROY")
     self.assertEqual({"cyclades.vm": -1,
                       "cyclades.total_cpu": -2,
                       "cyclades.total_ram": 1048576 * -1024,
                       "cyclades.disk": 1073741824 * -20}, commission)
     commission = quotas.get_commission_info(vm, "RESIZE")
     self.assertEqual(None, commission)
     commission = quotas.get_commission_info(vm, "RESIZE",
                                             {"beparams": {"vcpus": 4,
                                                           "maxmem": 2048}})
     self.assertEqual({"cyclades.total_cpu": 2,
                       "cyclades.total_ram": 1048576 * 1024}, commission)
     vm.operstate = "STOPPED"
     vm.save()
     commission = quotas.get_commission_info(vm, "REBOOT")
     self.assertEqual({"cyclades.cpu": 2,
                       "cyclades.ram": 1048576 * 1024}, commission)
     vm.operstate = "STARTED"
     vm.save()
     commission = quotas.get_commission_info(vm, "REBOOT")
     self.assertEqual(None, commission)
Exemplo n.º 3
0
def handle_vm_quotas(vm, job_id, job_opcode, job_status, job_fields):
    """Handle quotas for updated VirtualMachine.

    Update quotas for the updated VirtualMachine based on the job that run on
    the Ganeti backend. If a commission has been already issued for this job,
    then this commission is just accepted or rejected based on the job status.
    Otherwise, a new commission for the given change is issued, that is also in
    force and auto-accept mode. In this case, previous commissions are
    rejected, since they reflect a previous state of the VM.

    """
    if job_status not in rapi.JOB_STATUS_FINALIZED:
        return vm
    print vm
    print vm

    # Check successful completion of a job will trigger any quotable change in
    # the VM state.
    action = utils.get_action_from_opcode(job_opcode, job_fields)
    if action == "BUILD":
        # Quotas for new VMs are automatically accepted by the API
        return vm

    if vm.task_job_id == job_id and vm.serial is not None:
        # Commission for this change has already been issued. So just
        # accept/reject it. Special case is OP_INSTANCE_CREATE, which even
        # if fails, must be accepted, as the user must manually remove the
        # failed server
        serial = vm.serial
        if job_status == rapi.JOB_STATUS_SUCCESS:
            quotas.accept_resource_serial(vm)
        elif job_status in [rapi.JOB_STATUS_ERROR, rapi.JOB_STATUS_CANCELED]:
            log.debug("Job %s failed. Rejecting related serial %s", job_id,
                      serial)
            quotas.reject_resource_serial(vm)
    elif job_status == rapi.JOB_STATUS_SUCCESS:
        commission_info = quotas.get_commission_info(resource=vm,
                                                     action=action,
                                                     action_fields=job_fields)
        if commission_info is not None:
            # 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!
            log.debug("Expected job was %s. Processing job %s. "
                      "Attached serial %s",
                      vm.task_job_id, job_id, vm.serial)
            reason = ("client: dispatcher, resource: %s, ganeti_job: %s"
                      % (vm, job_id))
            serial = quotas.handle_resource_commission(
                vm, action,
                action_fields=job_fields,
                commission_name=reason,
                force=True,
                auto_accept=True)
            log.debug("Issued new commission: %s", serial)
    return vm
Exemplo n.º 4
0
def handle_vm_quotas(vm, job_id, job_opcode, job_status, job_fields):
    """Handle quotas for updated VirtualMachine.

    Update quotas for the updated VirtualMachine based on the job that run on
    the Ganeti backend. If a commission has been already issued for this job,
    then this commission is just accepted or rejected based on the job status.
    Otherwise, a new commission for the given change is issued, that is also in
    force and auto-accept mode. In this case, previous commissions are
    rejected, since they reflect a previous state of the VM.

    """
    if job_status not in rapi.JOB_STATUS_FINALIZED:
        return vm

    # Check successful completion of a job will trigger any quotable change in
    # the VM state.
    action = utils.get_action_from_opcode(job_opcode, job_fields)
    if action == "BUILD":
        # Quotas for new VMs are automatically accepted by the API
        return vm

    if vm.task_job_id == job_id and vm.serial is not None:
        # Commission for this change has already been issued. So just
        # accept/reject it. Special case is OP_INSTANCE_CREATE, which even
        # if fails, must be accepted, as the user must manually remove the
        # failed server
        serial = vm.serial
        if job_status == rapi.JOB_STATUS_SUCCESS:
            quotas.accept_resource_serial(vm)
        elif job_status in [rapi.JOB_STATUS_ERROR, rapi.JOB_STATUS_CANCELED]:
            log.debug("Job %s failed. Rejecting related serial %s", job_id,
                      serial)
            quotas.reject_resource_serial(vm)
    elif job_status == rapi.JOB_STATUS_SUCCESS:
        commission_info = quotas.get_commission_info(resource=vm,
                                                     action=action,
                                                     action_fields=job_fields)
        if commission_info is not None:
            # 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!
            log.debug(
                "Expected job was %s. Processing job %s. "
                "Attached serial %s", vm.task_job_id, job_id, vm.serial)
            reason = ("client: dispatcher, resource: %s, ganeti_job: %s" %
                      (vm, job_id))
            serial = quotas.handle_resource_commission(
                vm,
                action,
                action_fields=job_fields,
                commission_name=reason,
                force=True,
                auto_accept=True)
            log.debug("Issued new commission: %s", serial)
    return vm
Exemplo n.º 5
0
    def test_commissions(self):
        flavor = mfactory.FlavorFactory(cpu=2, ram=1024, disk=20)
        vm = mfactory.VirtualMachineFactory(flavor=flavor)
        mfactory.VolumeFactory(size=20,
                               machine=vm,
                               deleted=False,
                               status="IN_USE",
                               delete_on_termination=True)
        vm.volumes.update(project=vm.project)
        #commission = quotas.get_commission_info(vm, "BUILD")
        #self.assertEqual({"cyclades.vm": 1,
        #                  "cyclades.cpu": 2,
        #                  "cyclades.cpu": 2,
        #                  "cyclades.ram": 1048576 * 1024,
        #                  "cyclades.ram": 1048576 * 1024,
        #                  "cyclades.disk": 1073741824 * 20}, commission)
        vm.operstate = "STARTED"
        vm.save()
        project = vm.project
        commission = quotas.get_commission_info(vm, "STOP")
        self.assertEqual(
            {
                (project, "cyclades.cpu"): -2,
                (project, "cyclades.ram"): 1048576 * -1024
            }, commission)
        # Check None quotas if vm is already stopped
        vm.operstate = "STOPPED"
        vm.save()
        commission = quotas.get_commission_info(vm, "STOP")
        self.assertEqual(None, commission)
        commission = quotas.get_commission_info(vm, "START")
        self.assertEqual(
            {
                (project, "cyclades.cpu"): 2,
                (project, "cyclades.ram"): 1048576 * 1024
            }, commission)
        vm.operstate = "STARTED"
        vm.save()
        commission = quotas.get_commission_info(vm, "DESTROY")
        self.assertEqual(
            {
                (project, "cyclades.vm"): -1,
                (project, "cyclades.total_cpu"): -2,
                (project, "cyclades.cpu"): -2,
                (project, "cyclades.total_ram"): 1048576 * -1024,
                (project, "cyclades.ram"): 1048576 * -1024,
                (project, "cyclades.disk"): 1073741824 * -20
            }, commission)
        vm.operstate = "STOPPED"
        vm.save()
        commission = quotas.get_commission_info(vm, "DESTROY")
        self.assertEqual(
            {
                (project, "cyclades.vm"): -1,
                (project, "cyclades.total_cpu"): -2,
                (project, "cyclades.total_ram"): -1024 << 20,
                (project, "cyclades.disk"): -20 << 30
            }, commission)
        commission = quotas.get_commission_info(vm, "RESIZE")
        self.assertTrue((commission is None) or (not commission.keys()))

        commission = quotas.get_commission_info(
            vm, "RESIZE", {"beparams": {
                "vcpus": 4,
                "maxmem": 2048
            }})
        self.assertEqual(
            {
                (project, "cyclades.total_cpu"): 2,
                (project, "cyclades.total_ram"): 1048576 * 1024
            }, commission)
        vm.operstate = "STOPPED"
        vm.save()
        commission = quotas.get_commission_info(vm, "REBOOT")
        self.assertEqual(
            {
                (project, "cyclades.cpu"): 2,
                (project, "cyclades.ram"): 1048576 * 1024
            }, commission)
        vm.operstate = "STARTED"
        vm.save()
        commission = quotas.get_commission_info(vm, "REBOOT")
        self.assertEqual(None, commission)
Exemplo n.º 6
0
 def test_commissions(self):
     flavor = mfactory.FlavorFactory(cpu=2, ram=1024, disk=20)
     vm = mfactory.VirtualMachineFactory(flavor=flavor)
     #commission = quotas.get_commission_info(vm, "BUILD")
     #self.assertEqual({"cyclades.vm": 1,
     #                  "cyclades.cpu": 2,
     #                  "cyclades.cpu": 2,
     #                  "cyclades.ram": 1048576 * 1024,
     #                  "cyclades.ram": 1048576 * 1024,
     #                  "cyclades.disk": 1073741824 * 20}, commission)
     vm.operstate = "STARTED"
     vm.save()
     commission = quotas.get_commission_info(vm, "STOP")
     self.assertEqual({
         "cyclades.cpu": -2,
         "cyclades.ram": 1048576 * -1024
     }, commission)
     # Check None quotas if vm is already stopped
     vm.operstate = "STOPPED"
     vm.save()
     commission = quotas.get_commission_info(vm, "STOP")
     self.assertEqual(None, commission)
     commission = quotas.get_commission_info(vm, "START")
     self.assertEqual({
         "cyclades.cpu": 2,
         "cyclades.ram": 1048576 * 1024
     }, commission)
     vm.operstate = "STARTED"
     vm.save()
     commission = quotas.get_commission_info(vm, "DESTROY")
     self.assertEqual(
         {
             "cyclades.vm": -1,
             "cyclades.total_cpu": -2,
             "cyclades.cpu": -2,
             "cyclades.total_ram": 1048576 * -1024,
             "cyclades.ram": 1048576 * -1024,
             "cyclades.disk": 1073741824 * -20
         }, commission)
     vm.operstate = "STOPPED"
     vm.save()
     commission = quotas.get_commission_info(vm, "DESTROY")
     self.assertEqual(
         {
             "cyclades.vm": -1,
             "cyclades.total_cpu": -2,
             "cyclades.total_ram": 1048576 * -1024,
             "cyclades.disk": 1073741824 * -20
         }, commission)
     commission = quotas.get_commission_info(vm, "RESIZE")
     self.assertEqual(None, commission)
     commission = quotas.get_commission_info(
         vm, "RESIZE", {"beparams": {
             "vcpus": 4,
             "maxmem": 2048
         }})
     self.assertEqual(
         {
             "cyclades.total_cpu": 2,
             "cyclades.total_ram": 1048576 * 1024
         }, commission)
     vm.operstate = "STOPPED"
     vm.save()
     commission = quotas.get_commission_info(vm, "REBOOT")
     self.assertEqual({
         "cyclades.cpu": 2,
         "cyclades.ram": 1048576 * 1024
     }, commission)
     vm.operstate = "STARTED"
     vm.save()
     commission = quotas.get_commission_info(vm, "REBOOT")
     self.assertEqual(None, commission)