Statistics
| Branch: | Tag: | Revision:

root / snf-pithos-app / pithos / api / management / commands / pithos-export-quota.py @ 6674f31d

History | View | Annotate | Download (2.9 kB)

1 6c997921 Sofia Papagiannaki
# Copyright 2012 GRNET S.A. All rights reserved.
2 6c997921 Sofia Papagiannaki
#
3 6c997921 Sofia Papagiannaki
# Redistribution and use in source and binary forms, with or
4 6c997921 Sofia Papagiannaki
# without modification, are permitted provided that the following
5 6c997921 Sofia Papagiannaki
# conditions are met:
6 6c997921 Sofia Papagiannaki
#
7 6c997921 Sofia Papagiannaki
#   1. Redistributions of source code must retain the above
8 6c997921 Sofia Papagiannaki
#      copyright notice, this list of conditions and the following
9 6c997921 Sofia Papagiannaki
#      disclaimer.
10 6c997921 Sofia Papagiannaki
#
11 6c997921 Sofia Papagiannaki
#   2. Redistributions in binary form must reproduce the above
12 6c997921 Sofia Papagiannaki
#      copyright notice, this list of conditions and the following
13 6c997921 Sofia Papagiannaki
#      disclaimer in the documentation and/or other materials
14 6c997921 Sofia Papagiannaki
#      provided with the distribution.
15 6c997921 Sofia Papagiannaki
#
16 6c997921 Sofia Papagiannaki
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 6c997921 Sofia Papagiannaki
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 6c997921 Sofia Papagiannaki
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 6c997921 Sofia Papagiannaki
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 6c997921 Sofia Papagiannaki
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 6c997921 Sofia Papagiannaki
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 6c997921 Sofia Papagiannaki
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 6c997921 Sofia Papagiannaki
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 6c997921 Sofia Papagiannaki
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 6c997921 Sofia Papagiannaki
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 6c997921 Sofia Papagiannaki
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 6c997921 Sofia Papagiannaki
# POSSIBILITY OF SUCH DAMAGE.
28 6c997921 Sofia Papagiannaki
#
29 6c997921 Sofia Papagiannaki
# The views and conclusions contained in the software and
30 6c997921 Sofia Papagiannaki
# documentation are those of the authors and should not be
31 6c997921 Sofia Papagiannaki
# interpreted as representing official policies, either expressed
32 6c997921 Sofia Papagiannaki
# or implied, of GRNET S.A.
33 6c997921 Sofia Papagiannaki
34 6c997921 Sofia Papagiannaki
from optparse import make_option
35 6c997921 Sofia Papagiannaki
36 6c997921 Sofia Papagiannaki
from django.core.management.base import NoArgsCommand, CommandError
37 6c997921 Sofia Papagiannaki
38 6c997921 Sofia Papagiannaki
from sqlalchemy.sql import select, and_
39 6c997921 Sofia Papagiannaki
40 6c997921 Sofia Papagiannaki
from pithos.api.util import get_backend
41 6c997921 Sofia Papagiannaki
42 6c997921 Sofia Papagiannaki
import os
43 6c997921 Sofia Papagiannaki
44 6c997921 Sofia Papagiannaki
backend = get_backend()
45 6c997921 Sofia Papagiannaki
table = {}
46 6c997921 Sofia Papagiannaki
table['nodes'] = backend.node.nodes
47 6c997921 Sofia Papagiannaki
table['policy'] = backend.node.policy
48 6c997921 Sofia Papagiannaki
conn = backend.node.conn
49 6c997921 Sofia Papagiannaki
50 6c997921 Sofia Papagiannaki
class Command(NoArgsCommand):
51 6c997921 Sofia Papagiannaki
    help = "Export account quota policies"
52 6c997921 Sofia Papagiannaki
53 6c997921 Sofia Papagiannaki
    option_list = NoArgsCommand.option_list + (
54 6c997921 Sofia Papagiannaki
        make_option('--location',
55 6c997921 Sofia Papagiannaki
                    dest='location',
56 6c997921 Sofia Papagiannaki
                    default='exported_policies',
57 6c997921 Sofia Papagiannaki
                    help="Where to save the output file"),
58 6c997921 Sofia Papagiannaki
    )
59 6c997921 Sofia Papagiannaki
60 6c997921 Sofia Papagiannaki
    def handle_noargs(self, **options):
61 6c997921 Sofia Papagiannaki
        # retrieve account policies
62 6c997921 Sofia Papagiannaki
        s = select([table['nodes'].c.path, table['policy'].c.value])
63 6c997921 Sofia Papagiannaki
        s = s.where(and_(table['nodes'].c.node != 0,
64 6c997921 Sofia Papagiannaki
                         table['nodes'].c.parent == 0))
65 6c997921 Sofia Papagiannaki
        s = s.where(table['nodes'].c.node == table['policy'].c.node)
66 6c997921 Sofia Papagiannaki
        s = s.where(table['policy'].c.key == 'quota')
67 6c997921 Sofia Papagiannaki
    
68 6c997921 Sofia Papagiannaki
        location = os.path.abspath(options['location'])
69 6c997921 Sofia Papagiannaki
        try:
70 6c997921 Sofia Papagiannaki
            f = open(location, 'w')
71 6c997921 Sofia Papagiannaki
        except IOError, e:
72 6c997921 Sofia Papagiannaki
            raise CommandError(e)
73 6c997921 Sofia Papagiannaki
74 6c997921 Sofia Papagiannaki
        for p in conn.execute(s).fetchall():
75 6c997921 Sofia Papagiannaki
            f.write(' '.join(
76 6c997921 Sofia Papagiannaki
                [p.path, 'pithos+.diskspace', p.value, '0', '0', '0']))
77 6c997921 Sofia Papagiannaki
            f.write('\n')
78 6c997921 Sofia Papagiannaki
        f.close()
79 6c997921 Sofia Papagiannaki
        backend.close()