コード例 #1
0
def delete_user(username, only_stats=True, dry=True):
    vms = VirtualMachine.objects.filter(userid__exact=username)
    networks = Network.objects.filter(userid__exact=username)
    keys = PublicKeyPair.objects.filter(user__exact=username)

    if not len(
            list(
                itertools.ifilter(
                    bool, map(lambda q: q.count(), [vms, networks, keys])))):
        print "No entries exist for '%s'" % username
        return -1

    if only_stats:
        print "The following entries will be deleted if you decide to remove this user"
        print "%d Virtual Machines" % vms.exclude(
            operstate='DESTROYED').count()
        print "%d Destroyed Virtual Machines" % vms.filter(
            operstate='DESTROYED').count()
        print "%d Networks" % networks.count()
        print "%d PublicKeyPairs" % keys.count()
        return

    for o in itertools.chain(vms, networks):
        o.delete()

    for key in keys:
        key.delete()

    if dry:
        print "Skipping database commit."
        transaction.rollback()
    else:
        transaction.commit()
        print "User entries removed."
コード例 #2
0
def delete_user(username, only_stats=True, dry=True):
    vms = VirtualMachine.objects.filter(userid__exact=username)
    networks = Network.objects.filter(userid__exact=username)
    keys = PublicKeyPair.objects.filter(user__exact=username)

    if not len(list(itertools.ifilter(bool, map(lambda q: q.count(), [vms,
                                                                      networks,
                                                                      keys])))):
        print "No entries exist for '%s'" % username
        return -1

    if only_stats:
        print "The following entries will be deleted if you decide to remove this user"
        print "%d Virtual Machines" % vms.exclude(operstate='DESTROYED').count()
        print "%d Destroyed Virtual Machines" % vms.filter(operstate='DESTROYED').count()
        print "%d Networks" % networks.count()
        print "%d PublicKeyPairs" % keys.count()
        return

    for o in itertools.chain(vms, networks):
        o.delete()

    for key in keys:
        key.delete()

    if dry:
        print "Skipping database commit."
        transaction.rollback()
    else:
        transaction.commit()
        print "User entries removed."
コード例 #3
0
        def wrapper(vm, *args, **kwargs):
            user_id = for_user
            if user_id is None:
                user_id = vm.userid

            validate_server_action(vm, action)
            vm.action = action

            commission_name = "client: api, resource: %s" % vm
            quotas.handle_resource_commission(vm,
                                              action=action,
                                              action_fields=action_fields,
                                              commission_name=commission_name,
                                              for_user=user_id)
            vm.save()

            # XXX: Special case for server creation!
            if action == "BUILD":
                serial = vm.serial
                serial.pending = False
                serial.accept = True
                serial.save()
                # Perform a commit, because the VirtualMachine must be saved to
                # DB before the OP_INSTANCE_CREATE job in enqueued in Ganeti.
                # Otherwise, messages will arrive from snf-dispatcher about
                # this instance, before the VM is stored in DB.
                transaction.commit()
                # After committing the locks are released. Refetch the instance
                # to guarantee x-lock.
                vm = VirtualMachine.objects.select_for_update().get(id=vm.id)
                # XXX: Special case for server creation: we must accept the
                # commission because the VM has been stored in DB. Also, if
                # communication with Ganeti fails, the job will never reach
                # Ganeti, and the commission will never be resolved.
                quotas.accept_resource_serial(vm)

            # Send the job to Ganeti and get the associated jobID
            try:
                job_id = func(vm, *args, **kwargs)
            except Exception as e:
                if vm.serial is not None and action != "BUILD":
                    # Since the job never reached Ganeti, reject the commission
                    log.debug("Rejecting commission: '%s', could not perform"
                              " action '%s': %s" % (vm.serial, action, e))
                    transaction.rollback()
                    quotas.reject_serial(vm.serial)
                    transaction.commit()
                raise

            log.info("user: %s, vm: %s, action: %s, job_id: %s, serial: %s",
                     user_id, vm.id, action, job_id, vm.serial)

            # store the new task in the VM
            if job_id is not None:
                vm.task = action
                vm.task_job_id = job_id
            vm.save()

            return vm
コード例 #4
0
ファイル: commands.py プロジェクト: AthinaB/synnefo
        def wrapper(vm, *args, **kwargs):
            user_id = vm.userid
            validate_server_action(vm, action)
            vm.action = action

            commission_name = "client: api, resource: %s" % vm
            quotas.handle_resource_commission(vm, action=action,
                                              action_fields=action_fields,
                                              commission_name=commission_name)
            vm.save()

            # XXX: Special case for server creation!
            if action == "BUILD":
                serial = vm.serial
                serial.pending = False
                serial.accept = True
                serial.save()
                # Perform a commit, because the VirtualMachine must be saved to
                # DB before the OP_INSTANCE_CREATE job in enqueued in Ganeti.
                # Otherwise, messages will arrive from snf-dispatcher about
                # this instance, before the VM is stored in DB.
                transaction.commit()
                # After committing the locks are released. Refetch the instance
                # to guarantee x-lock.
                vm = VirtualMachine.objects.select_for_update().get(id=vm.id)
                # XXX: Special case for server creation: we must accept the
                # commission because the VM has been stored in DB. Also, if
                # communication with Ganeti fails, the job will never reach
                # Ganeti, and the commission will never be resolved.
                quotas.accept_resource_serial(vm)

            # Send the job to Ganeti and get the associated jobID
            try:
                job_id = func(vm, *args, **kwargs)
            except Exception as e:
                if vm.serial is not None and action != "BUILD":
                    # Since the job never reached Ganeti, reject the commission
                    log.debug("Rejecting commission: '%s', could not perform"
                              " action '%s': %s" % (vm.serial,  action, e))
                    transaction.rollback()
                    quotas.reject_serial(vm.serial)
                    transaction.commit()
                raise

            log.info("user: %s, vm: %s, action: %s, job_id: %s, serial: %s",
                     user_id, vm.id, action, job_id, vm.serial)

            # store the new task in the VM
            if job_id is not None:
                vm.task = action
                vm.task_job_id = job_id
            vm.save()

            return vm
コード例 #5
0
            warn('Skipping %s. It doesn\'t seem to be an email' % u)
            continue

        try:
            uuid = get_user_uuid(u)
            print "%s -> %s" % (u, uuid)
            if not uuid:
                raise Exception("No uuid for %s" % u)
            migrate_user(u, uuid)
            count += 1
        except Exception, e:
            print "ERROR: User id migration failed (%s)" % e

    if dry:
        print "Skipping database commit."
        transaction.rollback()
    else:
        transaction.commit()
        print "Migrated %d users" % count


class Command(NoArgsCommand):
    help = "Quotas migration helper"

    option_list = BaseCommand.option_list + (
        make_option('--strict',
                    dest='strict',
                    action="store_false",
                    default=True,
                    help="Exit on warnings."),
        make_option('--validate-db',
コード例 #6
0
            warn('Skipping %s. It doesn\'t seem to be an email' % u)
            continue

        try:
            uuid = get_user_uuid(u)
            print "%s -> %s" % (u, uuid)
            if not uuid:
                raise Exception("No uuid for %s" % u)
            migrate_user(u, uuid)
            count += 1
        except Exception, e:
            print "ERROR: User id migration failed (%s)" % e

    if dry:
        print "Skipping database commit."
        transaction.rollback()
    else:
        transaction.commit()
        print "Migrated %d users" % count


class Command(NoArgsCommand):
    help = "Quotas migration helper"

    option_list = BaseCommand.option_list + (
        make_option('--strict',
                    dest='strict',
                    action="store_false",
                    default=True,
                    help="Exit on warnings."),
        make_option('--validate-db',