def handle(self, *args, **options):
        write = self.stderr.write
        userid = options['userid']
        project = options["project"]

        # Get holdings from Cyclades DB
        db_holdings = util.get_db_holdings(user=userid, project=project)
        db_project_holdings = util.get_db_holdings(project=project,
                                                   for_users=False)

        # Get holdings from QuotaHolder
        try:
            qh_holdings = util.get_qh_users_holdings(
                [userid] if userid is not None else None,
                [project] if project is not None else None)
            qh_project_holdings = util.get_qh_project_holdings(
                [project] if project is not None else None)
        except errors.AstakosClientException as e:
            raise CommandError(e)

        unsynced_users, users_pending, users_unknown =\
            reconcile.check_users(self.stderr, quotas.RESOURCES,
                                  db_holdings, qh_holdings)

        unsynced_projects, projects_pending, projects_unknown =\
            reconcile.check_projects(self.stderr, quotas.RESOURCES,
                                     db_project_holdings, qh_project_holdings)
        pending_exists = users_pending or projects_pending
        unknown_exists = users_unknown or projects_unknown

        headers = ("Type", "Holder", "Source", "Resource",
                   "Database", "Quotaholder")
        unsynced = unsynced_users + unsynced_projects
        if unsynced:
            pprint_table(self.stdout, unsynced, headers)
            if options["fix"]:
                qh = quotas.Quotaholder.get()
                force = options["force"]
                name = ("client: reconcile-resources-cyclades, time: %s"
                        % datetime.now())
                user_provisions = reconcile.create_user_provisions(
                    unsynced_users)
                project_provisions = reconcile.create_project_provisions(
                    unsynced_projects)
                try:
                    qh.issue_commission_generic(
                        user_provisions, project_provisions,
                        name=name, force=force,
                        auto_accept=True)
                except quotas.errors.QuotaLimit:
                    write("Reconciling failed because a limit has been "
                          "reached. Use --force to ignore the check.\n")
                    return
                write("Fixed unsynced resources\n")

        if pending_exists:
            write("Found pending commissions. Run 'snf-manage"
                  " reconcile-commissions-cyclades'\n")
        elif not (unsynced or unknown_exists):
            write("Everything in sync.\n")
    def handle(self, **options):
        write = self.stdout.write
        try:
            backend.pre_exec()
            userid = options['userid']
            project = options['project']

            # Get holding from Pithos DB
            db_usage = backend.node.node_account_usage(userid, project)
            db_project_usage = backend.node.node_project_usage(project)

            users = set(db_usage.keys())
            if userid and userid not in users:
                if backend._lookup_account(userid) is None:
                    write("User '%s' does not exist in DB!\n" % userid)
                    return

            # Get holding from Quotaholder
            try:
                qh_result = backend.astakosclient.service_get_quotas(userid)
            except NotFound:
                write("User '%s' does not exist in Quotaholder!\n" % userid)
                return

            try:
                qh_project_result = \
                    backend.astakosclient.service_get_project_quotas(project)
            except NotFound:
                write("Project '%s' does not exist in Quotaholder!\n" %
                      project)

            unsynced_users, users_pending, users_unknown =\
                reconcile.check_users(self.stderr, RESOURCES,
                                      db_usage, qh_result)

            unsynced_projects, projects_pending, projects_unknown =\
                reconcile.check_projects(self.stderr, RESOURCES,
                                         db_project_usage, qh_project_result)
            pending_exists = users_pending or projects_pending
            unknown_exists = users_unknown or projects_unknown

            headers = ("Type", "Holder", "Source", "Resource",
                       "Database", "Quotaholder")
            unsynced = unsynced_users + unsynced_projects
            if unsynced:
                utils.pprint_table(self.stdout, unsynced, headers)
                if options["fix"]:
                    force = options["force"]
                    name = ("client: reconcile-resources-pithos, time: %s"
                            % datetime.now())
                    user_provisions = reconcile.create_user_provisions(
                        unsynced_users)
                    project_provisions = reconcile.create_project_provisions(
                        unsynced_projects)
                    try:
                        backend.astakosclient.issue_commission_generic(
                            user_provisions, project_provisions, name=name,
                            force=force, auto_accept=True)
                    except QuotaLimit:
                        write("Reconciling failed because a limit has been "
                              "reached. Use --force to ignore the check.\n")
                        return
                    write("Fixed unsynced resources\n")

            if pending_exists:
                write("Found pending commissions. Run 'snf-manage"
                      " reconcile-commissions-pithos'\n")
            elif not (unsynced or unknown_exists):
                write("Everything in sync.\n")
        except BaseException as e:
            backend.post_exec(False)
            raise CommandError(e)
        else:
            backend.post_exec(True)
        finally:
            backend.close()
    def handle(self, *args, **options):
        write = self.stderr.write
        userid = options['userid']
        project = options["project"]

        # Get holdings from Cyclades DB
        db_holdings = util.get_db_holdings(user=userid, project=project)
        db_project_holdings = util.get_db_holdings(project=project,
                                                   for_users=False)

        # Get holdings from QuotaHolder
        try:
            qh_holdings = util.get_qh_users_holdings(
                [userid] if userid is not None else None,
                [project] if project is not None else None)
            qh_project_holdings = util.get_qh_project_holdings(
                [project] if project is not None else None)
        except errors.AstakosClientException as e:
            raise CommandError(e)

        unsynced_users, users_pending, users_unknown =\
            reconcile.check_users(self.stderr, quotas.RESOURCES,
                                  db_holdings, qh_holdings)

        unsynced_projects, projects_pending, projects_unknown =\
            reconcile.check_projects(self.stderr, quotas.RESOURCES,
                                     db_project_holdings, qh_project_holdings)
        pending_exists = users_pending or projects_pending
        unknown_exists = users_unknown or projects_unknown

        headers = ("Type", "Holder", "Source", "Resource", "Database",
                   "Quotaholder")
        unsynced = unsynced_users + unsynced_projects
        if unsynced:
            pprint_table(self.stdout, unsynced, headers)
            if options["fix"]:
                qh = quotas.Quotaholder.get()
                force = options["force"]
                name = ("client: reconcile-resources-cyclades, time: %s" %
                        datetime.now())
                user_provisions = reconcile.create_user_provisions(
                    unsynced_users)
                project_provisions = reconcile.create_project_provisions(
                    unsynced_projects)
                try:
                    qh.issue_commission_generic(user_provisions,
                                                project_provisions,
                                                name=name,
                                                force=force,
                                                auto_accept=True)
                except quotas.errors.QuotaLimit:
                    write("Reconciling failed because a limit has been "
                          "reached. Use --force to ignore the check.\n")
                    return
                write("Fixed unsynced resources\n")

        if pending_exists:
            write("Found pending commissions. Run 'snf-manage"
                  " reconcile-commissions-cyclades'\n")
        elif not (unsynced or unknown_exists):
            write("Everything in sync.\n")
    def handle(self, *args, **options):
        write = self.stderr.write
        force = options['force']
        userid = options['userid']
        project = options['project']

        resources = [quotas.PENDING_APP_RESOURCE]

        try:
            astakos = Component.objects.get(name="astakos")
        except Component.DoesNotExist:
            raise CommandError("Component 'astakos' not found.")

        query = [userid] if userid is not None else None
        qh_holdings = quotas.service_get_quotas(astakos, query)
        query = [project] if project is not None else None
        qh_project_holdings = quotas.service_get_project_quotas(astakos, query)

        if userid is None:
            users = AstakosUser.objects.accepted().select_related(
                'base_project')
        else:
            try:
                user = AstakosUser.objects.get(uuid=userid)
            except AstakosUser.DoesNotExist:
                raise CommandError("There is no user with uuid '%s'." % userid)
            if not user.is_accepted():
                raise CommandError("%s is not an accepted user." % userid)
            users = [user]

        db_holdings = count_pending_app(users)

        db_project_holdings = {}
        for user, user_holdings in db_holdings.iteritems():
            db_project_holdings.update(user_holdings)

        unsynced_users, users_pending, users_unknown =\
            reconcile.check_users(self.stderr, resources,
                                  db_holdings, qh_holdings)

        unsynced_projects, projects_pending, projects_unknown =\
            reconcile.check_projects(self.stderr, resources,
                                     db_project_holdings, qh_project_holdings)
        pending_exists = users_pending or projects_pending
        unknown_exists = users_unknown or projects_unknown

        headers = ("Type", "Holder", "Source", "Resource", "Astakos",
                   "Quotaholder")
        unsynced = unsynced_users + unsynced_projects
        if unsynced:
            pprint_table(self.stdout, unsynced, headers)
            if options["fix"]:
                user_provisions = create_user_provisions(unsynced_users)
                project_provisions = create_project_provisions(
                    unsynced_projects)
                provisions = user_provisions + project_provisions
                name = ("client: reconcile-resources-astakos, time: %s" %
                        datetime.now())
                try:
                    s = qh.issue_commission('astakos',
                                            provisions,
                                            name=name,
                                            force=force)
                except qh_exception.NoCapacityError:
                    write("Reconciling failed because a limit has been "
                          "reached. Use --force to ignore the check.\n")
                    return

                qh.resolve_pending_commission('astakos', s)
                write("Fixed unsynced resources\n")

        if pending_exists:
            write("Found pending commissions. "
                  "This is probably a bug. Please report.\n")
        elif not (unsynced or unknown_exists):
            write("Everything in sync.\n")
    def handle(self, *args, **options):
        write = self.stderr.write
        force = options['force']
        userid = options['userid']
        project = options['project']

        resources = [quotas.PENDING_APP_RESOURCE]

        try:
            astakos = Component.objects.get(name="astakos")
        except Component.DoesNotExist:
            raise CommandError("Component 'astakos' not found.")

        query = [userid] if userid is not None else None
        qh_holdings = quotas.service_get_quotas(astakos, query)
        query = [project] if project is not None else None
        qh_project_holdings = quotas.service_get_project_quotas(astakos, query)

        if userid is None:
            users = AstakosUser.objects.accepted().select_related(
                'base_project')
        else:
            try:
                user = AstakosUser.objects.get(uuid=userid)
            except AstakosUser.DoesNotExist:
                raise CommandError("There is no user with uuid '%s'." % userid)
            if not user.is_accepted():
                raise CommandError("%s is not an accepted user." % userid)
            users = [user]

        db_holdings = count_pending_app(users)

        db_project_holdings = {}
        for user, user_holdings in db_holdings.iteritems():
            db_project_holdings.update(user_holdings)

        unsynced_users, users_pending, users_unknown =\
            reconcile.check_users(self.stderr, resources,
                                  db_holdings, qh_holdings)

        unsynced_projects, projects_pending, projects_unknown =\
            reconcile.check_projects(self.stderr, resources,
                                     db_project_holdings, qh_project_holdings)
        pending_exists = users_pending or projects_pending
        unknown_exists = users_unknown or projects_unknown

        headers = ("Type", "Holder", "Source", "Resource",
                   "Astakos", "Quotaholder")
        unsynced = unsynced_users + unsynced_projects
        if unsynced:
            pprint_table(self.stdout, unsynced, headers)
            if options["fix"]:
                user_provisions = create_user_provisions(unsynced_users)
                project_provisions = create_project_provisions(
                    unsynced_projects)
                provisions = user_provisions + project_provisions
                name = ("client: reconcile-resources-astakos, time: %s"
                        % datetime.now())
                try:
                    s = qh.issue_commission('astakos', provisions,
                                            name=name, force=force)
                except qh_exception.NoCapacityError:
                    write("Reconciling failed because a limit has been "
                          "reached. Use --force to ignore the check.\n")
                    return

                qh.resolve_pending_commission('astakos', s)
                write("Fixed unsynced resources\n")

        if pending_exists:
            write("Found pending commissions. "
                  "This is probably a bug. Please report.\n")
        elif not (unsynced or unknown_exists):
            write("Everything in sync.\n")
Esempio n. 6
0
    def handle(self, **options):
        write = self.stdout.write
        try:
            backend.pre_exec()
            userid = options['userid']
            project = options['project']

            # Get holding from Pithos DB
            db_usage = backend.node.node_account_usage(userid, project)
            db_project_usage = backend.node.node_project_usage(project)

            users = set(db_usage.keys())
            if userid and userid not in users:
                if backend._lookup_account(userid) is None:
                    write("User '%s' does not exist in DB!\n" % userid)
                    return

            # Get holding from Quotaholder
            try:
                qh_result = backend.astakosclient.service_get_quotas(userid)
            except NotFound:
                write("User '%s' does not exist in Quotaholder!\n" % userid)
                return

            try:
                qh_project_result = \
                    backend.astakosclient.service_get_project_quotas(project)
            except NotFound:
                write("Project '%s' does not exist in Quotaholder!\n" %
                      project)

            unsynced_users, users_pending, users_unknown =\
                reconcile.check_users(self.stderr, RESOURCES,
                                      db_usage, qh_result)

            unsynced_projects, projects_pending, projects_unknown =\
                reconcile.check_projects(self.stderr, RESOURCES,
                                         db_project_usage, qh_project_result)
            pending_exists = users_pending or projects_pending
            unknown_exists = users_unknown or projects_unknown

            headers = ("Type", "Holder", "Source", "Resource", "Database",
                       "Quotaholder")
            unsynced = unsynced_users + unsynced_projects
            if unsynced:
                utils.pprint_table(self.stdout, unsynced, headers)
                if options["fix"]:
                    force = options["force"]
                    name = ("client: reconcile-resources-pithos, time: %s" %
                            datetime.now())
                    user_provisions = reconcile.create_user_provisions(
                        unsynced_users)
                    project_provisions = reconcile.create_project_provisions(
                        unsynced_projects)
                    try:
                        backend.astakosclient.issue_commission_generic(
                            user_provisions,
                            project_provisions,
                            name=name,
                            force=force,
                            auto_accept=True)
                    except QuotaLimit:
                        write("Reconciling failed because a limit has been "
                              "reached. Use --force to ignore the check.\n")
                        return
                    write("Fixed unsynced resources\n")

            if pending_exists:
                write("Found pending commissions. Run 'snf-manage"
                      " reconcile-commissions-pithos'\n")
            elif not (unsynced or unknown_exists):
                write("Everything in sync.\n")
        except BaseException as e:
            backend.post_exec(False)
            raise CommandError(e)
        else:
            backend.post_exec(True)
        finally:
            backend.close()