root / snf-cyclades-app / synnefo / logic / management / commands / cyclades-astakos-migrate-013.py @ b6426ead
History | View | Annotate | Download (9.8 kB)
1 |
# Copyright 2012, 2013 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 |
import itertools |
35 |
import functools |
36 |
|
37 |
from optparse import make_option |
38 |
|
39 |
from django.core.management.base import CommandError |
40 |
from django.db import transaction |
41 |
from django.conf import settings |
42 |
|
43 |
from snf_django.management.commands import SynnefoCommand |
44 |
from synnefo.api.util import get_existing_users |
45 |
from synnefo.lib.utils import case_unique |
46 |
from synnefo.db.models import Network, VirtualMachine |
47 |
from synnefo.userdata.models import PublicKeyPair |
48 |
|
49 |
from snf_django.lib import astakos |
50 |
|
51 |
|
52 |
get_displayname = functools.partial( |
53 |
astakos.get_displayname, settings.CYCLADES_SERVICE_TOKEN, |
54 |
url=settings.ASTAKOS_URL.replace('im/authenticate',
|
55 |
'service/api/user_catalogs'))
|
56 |
get_user_uuid = functools.partial( |
57 |
astakos.get_user_uuid, settings.CYCLADES_SERVICE_TOKEN, |
58 |
url=settings.ASTAKOS_URL.replace('im/authenticate',
|
59 |
'service/api/user_catalogs'))
|
60 |
|
61 |
|
62 |
@transaction.commit_on_success
|
63 |
def merge_user(username): |
64 |
vms = VirtualMachine.objects.filter(userid__iexact=username) |
65 |
networks = Network.objects.filter(userid__iexact=username) |
66 |
keys = PublicKeyPair.objects.filter(user__iexact=username) |
67 |
|
68 |
for o in itertools.chain(vms, networks): |
69 |
o.userid = username.lower() |
70 |
o.save() |
71 |
|
72 |
for key in keys: |
73 |
key.user = username.lower() |
74 |
key.save() |
75 |
|
76 |
|
77 |
def migrate_user(username, uuid): |
78 |
"""
|
79 |
Warn: no transaction handling. Consider wrapping within another function.
|
80 |
"""
|
81 |
vms = VirtualMachine.objects.filter(userid__exact=username) |
82 |
networks = Network.objects.filter(userid__exact=username) |
83 |
keys = PublicKeyPair.objects.filter(user__exact=username) |
84 |
|
85 |
for o in itertools.chain(vms, networks): |
86 |
o.userid = uuid or o.userid
|
87 |
o.save() |
88 |
|
89 |
for key in keys: |
90 |
key.user = uuid |
91 |
key.save() |
92 |
|
93 |
|
94 |
class Command(SynnefoCommand): |
95 |
help = "Quotas migration helper"
|
96 |
|
97 |
option_list = SynnefoCommand.option_list + ( |
98 |
make_option('--strict',
|
99 |
dest='strict',
|
100 |
action="store_false",
|
101 |
default=True,
|
102 |
help="Exit on warnings."),
|
103 |
make_option('--validate-db',
|
104 |
dest='validate',
|
105 |
action="store_true",
|
106 |
default=True,
|
107 |
help=("Check if cyclades database contents are valid for "
|
108 |
"migration.")),
|
109 |
make_option('--migrate-users',
|
110 |
dest='migrate_users',
|
111 |
action="store_true",
|
112 |
default=False,
|
113 |
help=("Convert emails to uuids for all users stored in "
|
114 |
"database.")),
|
115 |
make_option('--merge-user',
|
116 |
dest='merge_user',
|
117 |
default=False,
|
118 |
help="Merge case insensitive duplicates of a user."),
|
119 |
make_option('--delete-user',
|
120 |
dest='delete_user',
|
121 |
action='store',
|
122 |
default=False,
|
123 |
help="Delete user entries."),
|
124 |
make_option('--user-entries',
|
125 |
dest='user_entries',
|
126 |
action='store',
|
127 |
default=False,
|
128 |
help="Display user summary."),
|
129 |
make_option('--dry',
|
130 |
dest='dry',
|
131 |
action="store_true",
|
132 |
default=False,
|
133 |
help="Do not commit database changes. Do not communicate "
|
134 |
"with quotaholder"),
|
135 |
make_option('--user',
|
136 |
dest='user',
|
137 |
action="store",
|
138 |
default=False,)
|
139 |
) |
140 |
|
141 |
def resolve_conflicts(self, options): |
142 |
conflicting = map(options.get, ['migrate_users', |
143 |
'merge_user'])
|
144 |
if len(filter(bool, conflicting)) > 1: |
145 |
raise CommandError('You can use only one of --validate,' |
146 |
'--migrate-users')
|
147 |
|
148 |
def handle(self, *args, **options): |
149 |
self.resolve_conflicts(options)
|
150 |
self.strict = options.get('strict') |
151 |
self.dry = options.get('dry') |
152 |
|
153 |
if options.get('validate') and not options.get('merge_user') and not \ |
154 |
options.get('delete_user') and not options.get('user_entries'): |
155 |
usernames = get_existing_users() |
156 |
self._validate_db_state(usernames)
|
157 |
|
158 |
if options.get('migrate_users'): |
159 |
self.migrate_users(usernames, dry=self.dry) |
160 |
|
161 |
if options.get('merge_user'): |
162 |
merge_user(options.get('merge_user'))
|
163 |
self.stderr.write("Merge finished.") |
164 |
|
165 |
if options.get('delete_user'): |
166 |
entries = self.delete_user(options.get('delete_user'), |
167 |
only_stats=True)
|
168 |
if entries == -1: |
169 |
return
|
170 |
|
171 |
confirm = raw_input("Type 'yes of course' if you are sure you want" |
172 |
" to remove those entries: ")
|
173 |
if not confirm == 'yes of course': |
174 |
return
|
175 |
else:
|
176 |
self.delete_user(options.get('delete_user'), only_stats=False, |
177 |
dry=self.dry)
|
178 |
|
179 |
if options.get('user_entries'): |
180 |
self.delete_user(options.get('user_entries')) |
181 |
|
182 |
@transaction.commit_manually
|
183 |
def delete_user(self, username, only_stats=True, dry=True): |
184 |
vms = VirtualMachine.objects.filter(userid__exact=username) |
185 |
networks = Network.objects.filter(userid__exact=username) |
186 |
keys = PublicKeyPair.objects.filter(user__exact=username) |
187 |
|
188 |
if not len(list(itertools.ifilter(bool, |
189 |
map(lambda q: q.count(), |
190 |
[vms, networks, keys])))): |
191 |
self.stderr.write("No entries exist for '%s'" % username) |
192 |
return -1 |
193 |
|
194 |
if only_stats:
|
195 |
self.stderr.write("The following entries will be deleted if " |
196 |
"you decide to remove this user")
|
197 |
self.stderr.write("%d Virtual Machines" |
198 |
% vms.exclude(operstate='DESTROYED').count())
|
199 |
self.stderr.write("%d Destroyed Virtual Machines" |
200 |
% vms.filter(operstate='DESTROYED').count())
|
201 |
self.stderr.write("%d Networks" % networks.count()) |
202 |
self.stderr.write("%d PublicKeyPairs" % keys.count()) |
203 |
return
|
204 |
|
205 |
for o in itertools.chain(vms, networks): |
206 |
o.delete() |
207 |
|
208 |
for key in keys: |
209 |
key.delete() |
210 |
|
211 |
if dry:
|
212 |
self.stderr.write("Skipping database commit.") |
213 |
transaction.rollback() |
214 |
else:
|
215 |
transaction.commit() |
216 |
self.stderr.write("User entries removed.") |
217 |
|
218 |
def warn(self, *msgs): |
219 |
self.stderr.write("WARNING: %s" % ' '.join(msgs)) |
220 |
|
221 |
def _validate_db_state(self, usernames): |
222 |
|
223 |
usernames = filter(bool, usernames) |
224 |
invalid_case_users = case_unique(usernames) |
225 |
if invalid_case_users:
|
226 |
invalid_case_users.append(invalid_case_users[0].lower())
|
227 |
raise CommandError(
|
228 |
"Duplicate case insensitive user identifiers exist %r"
|
229 |
% invalid_case_users) |
230 |
|
231 |
uuidusers = filter(lambda uid: '@' in uid or uid is None, usernames) |
232 |
if len(uuidusers) != len(usernames): |
233 |
self.warn("It seems that mixed uuid/email user " |
234 |
"identifiers exist in database.")
|
235 |
return False |
236 |
|
237 |
return True |
238 |
|
239 |
@transaction.commit_manually
|
240 |
def migrate_users(self, usernames, dry=True): |
241 |
usernames = filter(bool, usernames) |
242 |
count = 0
|
243 |
for u in usernames: |
244 |
if not '@' in u: |
245 |
self.warn('Skipping %s. It doesn\'t seem to be an email' % u) |
246 |
continue
|
247 |
|
248 |
try:
|
249 |
uuid = get_user_uuid(u) |
250 |
self.stderr.write("%s -> %s" % (u, uuid)) |
251 |
if not uuid: |
252 |
raise Exception("No uuid for %s" % u) |
253 |
migrate_user(u, uuid) |
254 |
count += 1
|
255 |
except Exception, e: |
256 |
self.stderr.write("ERROR: User id migration failed (%s)" % e) |
257 |
|
258 |
if dry:
|
259 |
self.stderr.write("Skipping database commit.") |
260 |
transaction.rollback() |
261 |
else:
|
262 |
transaction.commit() |
263 |
self.stderr.write("Migrated %d users" % count) |