Statistics
| Branch: | Tag: | Revision:

root / snf-pithos-app / pithos / api / management / commands / pithos-manage-accounts.py @ 94bff756

History | View | Annotate | Download (5.9 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
class Command(NoArgsCommand):
42
    help = "Quotas migration helper"
43

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

    
79
    def handle(self, *args, **options):
80
        try:
81
            utils = SwissArmy()
82
            self.strict = options.get('strict')
83
            self.dry = options.get('dry')
84

    
85
            if options.get('duplicate-accounts') and \
86
                    not options.get('existing-accounts') and \
87
                    not options.get('merge_accounts') and \
88
                    not options.get('delete_account'):
89
                duplicates = utils.duplicate_accounts()
90
                if duplicates:
91
                    msg = "The following case insensitive duplicates found: %r"
92
                    raise CommandError(msg % duplicates)
93
                else:
94
                    print "No duplicate accounts are found."
95

    
96
            if options.get('existing-accounts') and \
97
                    not options.get('merge_accounts') and \
98
                    not options.get('delete_account'):
99
                accounts = utils.existing_accounts()
100
                print "The following accounts found:"
101
                print "%s" % '\n'.join(accounts)
102

    
103
            if options.get('merge_accounts'):
104
                src_account = options.get('src_account')
105
                dest_account = options.get('dest_account')
106
                if not src_account:
107
                    raise CommandError('Please specify a source account')
108
                if not dest_account:
109
                    raise CommandError('Please specify a destination account')
110
                utils.merge_account(src_account, dest_account,
111
                                         only_stats=True)
112

    
113
                confirm = raw_input("Type 'yes' if you are sure you want"
114
                                    " to remove those entries: ")
115
                if not confirm == 'yes':
116
                    return
117
                else:
118
                    utils.merge_account(options.get('src_account'),
119
                                        options.get('dest_account'),
120
                                        only_stats=False,
121
                                        dry=self.dry)
122
                return
123

    
124
            if options.get('delete_account'):
125
                utils.delete_account(options.get('delete_account'),
126
                                     only_stats=True)
127

    
128
                confirm = raw_input("Type 'yes' if you are sure you want"
129
                                    " to remove those entries: ")
130
                if not confirm == 'yes':
131
                    return
132
                else:
133
                    utils.delete_account(options.get('delete_account'),
134
                                         only_stats=False,
135
                                         dry=self.dry)
136
                return
137
        except Exception, e:
138
            raise CommandError(e)
139
        finally:
140
            utils.backend.close()