Commit be225f08 authored by Giorgos Korfiatis's avatar Giorgos Korfiatis Committed by Georgios D. Tsoukalas

Unify and rename project management commands

parent c387f922
# Copyright 2011-2012 GRNET S.A. All rights reserved.
# Copyright 2011, 2012, 2013 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
......@@ -109,6 +109,22 @@ def quota_limits_per_user_from_get(lst):
quotas[holder] = userquotas
return quotas
def quotas_per_user_from_get(lst):
limits = {}
counters = {}
for holder, resource, q, c, il, el, imp, exp, ret, rel, flags in lst:
userlimits = limits.get(holder, {})
userlimits[resource] = QuotaValues(quantity=q, capacity=c,
import_limit=il, export_limit=el)
limits[holder] = userlimits
usercounters = counters.get(holder, {})
usercounters[resource] = QuotaCounters(imported=imp, exported=exp,
returned=ret, released=rel)
counters[holder] = usercounters
return limits, counters
def qh_get_quota(users, resources):
c = get_client()
if not c:
......@@ -127,6 +143,10 @@ def qh_get_quota_limits(users, resources):
result = qh_get_quota(users, resources)
return quota_limits_per_user_from_get(result)
def qh_get_quotas(users, resources):
result = qh_get_quota(users, resources)
return quotas_per_user_from_get(result)
def create_entity(payload):
c = get_client()
if not c:
......@@ -156,9 +176,15 @@ CreateEntityPayload = namedtuple('CreateEntityPayload', ('entity',
QuotaLimits = namedtuple('QuotaLimits', ('holder',
'resource',
'capacity',
'import_limit',
'import_limit',
'export_limit'))
QuotaCounters = namedtuple('QuotaCounters', ('imported',
'exported',
'returned',
'released'))
class QuotaValues(namedtuple('QuotaValues', ('quantity',
'capacity',
'import_limit',
......
# encoding: utf-8
# Copyright 2012 GRNET S.A. All rights reserved.
# Copyright 2012, 2013 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
......@@ -33,15 +33,28 @@
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
from optparse import make_option
from astakos.im.models import load_service_resources
from django.core.management.base import BaseCommand, CommandError
import logging
logger = logging.getLogger(__name__)
class Command(BaseCommand):
args = ""
help = "Register service resources"
option_list = BaseCommand.option_list + (
make_option('--load-service-resources',
action='store_true',
dest='load',
default=False,
help=("Load initial data for services and their resources "
"on quotaholder")),
)
def handle(self, *args, **options):
load_service_resources()
if options['load']:
load_service_resources()
# Copyright 2012 GRNET S.A. All rights reserved.
# Copyright 2012, 2013 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
......@@ -41,15 +41,21 @@ from astakos.im.functions import get_user_by_uuid
import logging
logger = logging.getLogger(__name__)
class Command(BaseCommand):
help = "Inspect quotaholder status"
option_list = BaseCommand.option_list + (
make_option('--check',
make_option('--list',
action='store_true',
dest='list',
default=False,
help="List all quotas (default)"),
make_option('--verify',
action='store_true',
dest='check',
default=True,
help="Check if quotaholder is in sync with astakos (default)"),
dest='verify',
default=False,
help="Check if quotaholder is in sync with astakos"),
make_option('--sync',
action='store_true',
dest='sync',
......@@ -57,13 +63,59 @@ class Command(BaseCommand):
help="Sync quotaholder"),
)
@transaction.commit_on_success
def handle(self, *args, **options):
sync = options['sync']
verify = options['verify'] or sync
ex, nonex, qh_l, qh_c, astakos_i, astakos_q, info = self.run(sync)
if verify:
self.print_verify(nonex, qh_l, astakos_q)
else:
self.list_quotas(qh_l, qh_c, astakos_i, info)
@transaction.commit_on_success
def run(self, sync):
try:
log = sync_all_users(sync=sync)
existing, nonexisting, registered_quotas, astakos_quotas = log
return sync_all_users(sync=sync)
except BaseException, e:
logger.exception(e)
raise CommandError("Syncing failed.")
def list_quotas(self, qh_limits, qh_counters, astakos_initial, info):
labels = ('uuid', 'email', 'resource', 'initial', 'total', 'usage')
columns = (36, 30, 24, 12, 12, 12)
line = ' '.join(l.rjust(w) for l, w in zip(labels, columns))
self.stdout.write(line + '\n')
sep = '-' * len(line)
self.stdout.write(sep + '\n')
for holder, resources in qh_limits.iteritems():
h_counters = qh_counters[holder]
h_initial = astakos_initial[holder]
email = info[holder]
for resource, limits in resources.iteritems():
initials = h_initial[resource]
initial = str(initials.capacity)
capacity = str(limits.capacity)
c = h_counters[resource]
used = str(c.imported - c.exported + c.returned - c.released)
fields = holder, email, resource, initial, capacity, used
output = []
for field, width in zip(fields, columns):
s = field.rjust(width)
output.append(s)
line = ' '.join(output)
self.stdout.write(line + '\n')
def print_verify(self,
nonexisting,
qh_limits,
astakos_quotas):
if nonexisting:
self.stdout.write("Users not registered in quotaholder:\n")
......@@ -73,14 +125,15 @@ class Command(BaseCommand):
diffs = 0
for holder, local in astakos_quotas.iteritems():
registered = registered_quotas.pop(holder, None)
registered = qh_limits.pop(holder, None)
if registered is None:
diffs += 1
self.stdout.write("No quotas for %s in quotaholder.\n\n" %
(get_user_by_uuid(holder)))
elif local != registered:
diffs += 1
self.stdout.write("Quotas differ for %s:\n" % (get_user_by_uuid(holder)))
self.stdout.write("Quotas differ for %s:\n" %
(get_user_by_uuid(holder)))
self.stdout.write("Quotas according to quotaholder:\n")
self.stdout.write("%s\n" % (registered))
self.stdout.write("Quotas according to astakos:\n")
......@@ -88,6 +141,3 @@ class Command(BaseCommand):
if diffs:
self.stdout.write("Quotas differ for %d users.\n" % (diffs))
except BaseException, e:
logger.exception(e)
raise CommandError("Syncing failed.")
# Copyright 2012 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
# 1. Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
#
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
from optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from astakos.im.models import ProjectApplication
from astakos.im.functions import approve_application
from astakos.im.project_xctx import cmd_project_transaction_context
class Command(BaseCommand):
args = "<project application id>"
help = """
Approve a pending project application
You can discover projects with a pending application with
(the last column <AppID> is the application to be approved):
snf-manage project-list --pending
You can examine a specific application with:
snf-manage project-show --app <AppId>
For a given project, you can examine a pending application with:
snf-manage project-show <project> --pending
"""
def handle(self, *args, **options):
if len(args) < 1:
raise CommandError("Please provide an application id")
try:
id = int(args[0])
except ValueError:
raise CommandError('Invalid id')
else:
approve(id)
@cmd_project_transaction_context(sync=True)
def approve(app, ctx=None):
try:
approve_application(app)
except BaseException as e:
if ctx:
ctx.mark_rollback()
raise CommandError(e)
# Copyright 2012 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
# 1. Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
#
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
from optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from django.core.exceptions import PermissionDenied
from django.db import transaction
from astakos.im.models import ProjectApplication
from astakos.im.functions import deny_application
class Command(BaseCommand):
args = "<project application id>"
help = """
Deny a project application
You can discover projects with a pending application with
(the last column <AppID> is the application to be denied):
snf-manage project-list --pending
You can examine a specific application with:
snf-manage project-show --app <AppId>
For a given project, you can examine a pending application with:
snf-manage project-show <project> --pending
"""
@transaction.commit_on_success
def handle(self, *args, **options):
if len(args) < 1:
raise CommandError("Please provide an application identifier")
try:
app_id = int(args[0])
except ValueError:
raise CommandError('Invalid id')
else:
try:
deny_application(app_id)
except (PermissionDenied, IOError):
raise CommandError('Invalid id')
# Copyright 2012 GRNET S.A. All rights reserved.
# Copyright 2012, 2013 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
......@@ -32,29 +32,91 @@
# or implied, of GRNET S.A.
from optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from astakos.im.functions import check_expiration
from django.core.management.base import BaseCommand, CommandError
from astakos.im.functions import (terminate, suspend, resume, check_expiration,
approve_application, deny_application)
from astakos.im.project_xctx import cmd_project_transaction_context
class Command(BaseCommand):
help = """
Check for and perform due administration tasks (e.g. termination)"
help = "Manage projects and applications"
"""
option_list = BaseCommand.option_list + (
make_option('--approve',
dest='approve',
metavar='<application id>',
help="Approve a project application"),
make_option('--deny',
dest='deny',
metavar='<application id>',
help="Deny a project application"),
make_option('--terminate',
dest='terminate',
metavar='<project id>',
help="Terminate a project"),
make_option('--suspend',
dest='suspend',
metavar='<project id>',
help="Suspend a project"),
make_option('--unsuspend',
dest='resume',
metavar='<project id>',
help="Resume a suspended project"),
make_option('--check-expired',
action='store_true',
dest='expire',
dest='check_expired',
default=False,
help="Check projects for expiration"),
make_option('--execute',
make_option('--terminate-expired',
action='store_true',
dest='execute',
dest='terminate_expired',
default=False,
help="Perform the actual operation"),
help="Terminate all expired projects"),
)
def handle(self, *args, **options):
pid = options['terminate']
if pid is not None:
self.run_command(terminate, pid)
return
pid = options['resume']
if pid is not None:
self.run_command(resume, pid)
return
pid = options['suspend']
if pid is not None:
self.run_command(suspend, pid)
return
appid = options['approve']
if appid is not None:
self.run_command(approve_application, appid)
return
appid = options['deny']
if appid is not None:
self.run_command(deny_application, appid)
return
if options['check_expired']:
self.expire(execute=False)
return
if options['terminate_expired']:
self.expire(execute=True)
@cmd_project_transaction_context(sync=True)
def run_command(self, func, id, ctx=None):
try:
func(id)
except BaseException as e:
if ctx:
ctx.mark_rollback()
raise CommandError(e)
def print_expired(self, projects, execute):
length = len(projects)
......@@ -63,7 +125,7 @@ class Command(BaseCommand):
elif length == 1:
s = '1 expired project:\n'
else:
s = '%d expired projects:\n' %(length,)
s = '%d expired projects:\n' % (length,)
self.stdout.write(s)
if length > 0:
......@@ -80,13 +142,8 @@ class Command(BaseCommand):
self.stdout.write(line + '\n')
if execute:
self.stdout.write('%d projects have been terminated.\n' %(length,))
def handle(self, *args, **options):
execute = options['execute']
if options['expire']:
self.expire(execute=execute)
self.stdout.write('%d projects have been terminated.\n' % (
length,))
@cmd_project_transaction_context(sync=True)
def expire(self, execute=False, ctx=None):
......
......@@ -70,11 +70,27 @@ class Command(NoArgsCommand):
"""
option_list = NoArgsCommand.option_list + (
make_option('-c',
make_option('--all',
action='store_true',
dest='csv',
dest='all',
default=False,
help="Use pipes to separate values"),
help="List all projects (default)"),
make_option('--new',
action='store_true',
dest='new',
default=False,
help="List only new project requests"),
make_option('--modified',
action='store_true',
dest='modified',
default=False,
help="List only projects with pending modification"),
make_option('--pending',
action='store_true',
dest='pending',
default=False,
help=("Show only projects with a pending application "
"(equiv. --modified --new)")),
make_option('--skip',
action='store_true',
dest='skip',
......@@ -85,32 +101,47 @@ class Command(NoArgsCommand):
dest='full',
default=False,
help="Do not shorten long names"),
make_option('--pending',
make_option('-c',
action='store_true',
dest='pending',
dest='csv',
default=False,
help="Show only projects with a pending application"),
)
help="Use pipes to separate values"),
)
def handle_noargs(self, **options):
allow_shorten = not options['full']
csv = options['csv']
chain_dict = Chain.objects.all_full_state()
if not options['all']:
f_states = []
if options['new']:
f_states.append(Chain.PENDING)
if options['modified']:
f_states += Chain.MODIFICATION_STATES
if options['pending']:
f_states.append(Chain.PENDING)
f_states += Chain.MODIFICATION_STATES
if options['skip']:
if not f_states:
f_states = Chain.RELEVANT_STATES
if f_states:
chain_dict = filter_by_state(chain_dict, f_states)
self.show(csv, allow_shorten, chain_dict)
def show(self, csv, allow_shorten, chain_dict):
labels = ('ProjID', 'Name', 'Applicant', 'Email', 'Status', 'AppID')
columns = (7, 23, 20, 20, 17, 7)
if not options['csv']:
if not csv:
line = ' '.join(l.rjust(w) for l, w in zip(labels, columns))
self.stdout.write(line + '\n')
sep = '-' * len(line)
self.stdout.write(sep + '\n')
chain_dict = Chain.objects.all_full_state()
if options['skip']:
chain_dict = do_skip(chain_dict)
if options['pending']:
chain_dict = pending_only(chain_dict)
allow_shorten = not options['full']
for info in chain_info(chain_dict):
fields = [
......@@ -124,7 +155,7 @@ class Command(NoArgsCommand):
fields = [(format(elem), flag) for (elem, flag) in fields]
if options['csv']:
if csv:
line = '|'.join(fields)
else:
output = []
......@@ -138,17 +169,10 @@ class Command(NoArgsCommand):
self.stdout.write(line + '\n')
def pending_only(chain_dict):
d = {}
for chain, (state, project, app) in chain_dict.iteritems():
if state in Chain.PENDING_STATES:
d[chain] = (state, project, app)
return d
def do_skip(chain_dict):
def filter_by_state(chain_dict, states):
d = {}
for chain, (state, project, app) in chain_dict.iteritems():
if state not in Chain.SKIP_STATES:
if state in states:
d[chain] = (state, project, app)
return d
......
# Copyright 2012 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
# 1. Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
#
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.