Statistics
| Branch: | Tag: | Revision:

root / snf-pithos-app / pithos / api / management / commands / pithos-manage-accounts.py @ 99c11993

History | View | Annotate | Download (6.3 kB)

1
# Copyright 2012 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34
from optparse import make_option
35

    
36
from django.core.management.base import NoArgsCommand, CommandError
37

    
38
from pithos.api.swiss_army import SwissArmy
39

    
40

    
41
def double_list_str(l):
42
    return '\n'.join(', '.join(sublist) for sublist in l)
43

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

    
47
    option_list = NoArgsCommand.option_list + (
48
        make_option('--list-duplicate',
49
                    dest='duplicate-accounts',
50
                    action="store_true",
51
                    default=False,
52
                    help="Display case insensitive duplicate accounts."),
53
        make_option('--list-all',
54
                    dest='existing-accounts',
55
                    action="store_true",
56
                    default=False,
57
                    help="Display existing accounts."),
58
        make_option('--merge-accounts',
59
                    dest='merge_accounts',
60
                    action='store_true',
61
                    default=False,
62
                    help="Merge SRC_ACCOUNT and DEST_ACCOUNT."),
63
        make_option('--delete-account',
64
                    dest='delete_account',
65
                    action='store',
66
                    help="Delete DELETE_ACCOUNT."),
67
        make_option('--src-account',
68
                    dest='src_account',
69
                    action='store',
70
                    help="Account to be merged and then deleted."),
71
        make_option('--dest-account',
72
                    dest='dest_account',
73
                    action='store',
74
                    help="Account where SRC_ACCOUNT contents will be copied."),
75
        make_option('--dry',
76
                    dest='dry',
77
                    action="store_true",
78
                    default=False,
79
                    help="Do not commit database changes.")
80
    )
81

    
82
    def handle(self, *args, **options):
83
        try:
84
            utils = SwissArmy()
85
            self.dry = options.get('dry')
86

    
87
            if not options.get('duplicate-accounts') and \
88
                not options.get('existing-accounts') and \
89
                not options.get('merge_accounts') and \
90
                not options.get('delete_account'):
91
                self.print_help('pithos-manage-accounts', '')
92

    
93
            if options.get('duplicate-accounts') and \
94
                    not options.get('existing-accounts') and \
95
                    not options.get('merge_accounts') and \
96
                    not options.get('delete_account'):
97
                duplicates = utils.duplicate_accounts()
98
                if duplicates:
99
                    msg = "The following case insensitive duplicates found:\n%s"
100
                    raise CommandError(msg % double_list_str(duplicates))
101
                else:
102
                    print "No duplicate accounts are found."
103

    
104
            if options.get('existing-accounts') and \
105
                    not options.get('merge_accounts') and \
106
                    not options.get('delete_account'):
107
                accounts = utils.existing_accounts()
108
                print "The following accounts found:"
109
                print "%s" % '\n'.join(accounts)
110

    
111
            if options.get('merge_accounts'):
112
                src_account = options.get('src_account')
113
                dest_account = options.get('dest_account')
114
                if not src_account:
115
                    raise CommandError('Please specify a source account')
116
                if not dest_account:
117
                    raise CommandError('Please specify a destination account')
118
                utils.merge_account(src_account, dest_account,
119
                                         only_stats=True)
120

    
121
                confirm = raw_input("Type 'yes' if you are sure you want"
122
                                    " to move those entries to %s: " %\
123
                                    dest_account)
124
                if not confirm == 'yes':
125
                    return
126
                else:
127
                    utils.merge_account(options.get('src_account'),
128
                                        options.get('dest_account'),
129
                                        only_stats=False,
130
                                        dry=self.dry)
131
                return
132

    
133
            if options.get('delete_account'):
134
                utils.delete_account(options.get('delete_account'),
135
                                     only_stats=True)
136

    
137
                confirm = raw_input("Type 'yes' if you are sure you want"
138
                                    " to remove those entries: ")
139
                if not confirm == 'yes':
140
                    return
141
                else:
142
                    utils.delete_account(options.get('delete_account'),
143
                                         only_stats=False,
144
                                         dry=self.dry)
145
                return
146
        except Exception, e:
147
            raise CommandError(e)
148
        finally:
149
            utils.backend.close()