root / snf-astakos-app / astakos / scripts / upgrade / migrate_014.py @ 05c1b849
History | View | Annotate | Download (3.6 kB)
1 |
# Copyright 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 |
from subprocess import call |
35 |
import os |
36 |
os.environ['DJANGO_SETTINGS_MODULE'] = 'synnefo.settings' |
37 |
from astakos.im.models import UserSetting, AstakosUserQuota, Resource |
38 |
from astakos.im.quotas import qh_sync_users |
39 |
|
40 |
from optparse import OptionParser |
41 |
|
42 |
parser = OptionParser() |
43 |
parser.add_option("--usersetting", dest="usersetting", |
44 |
action="store_true", default=False, |
45 |
help="Only migrate user-specific settings",
|
46 |
) |
47 |
|
48 |
SETTING = 'PENDING_APPLICATION_LIMIT'
|
49 |
RESOURCE = 'astakos.pending_app'
|
50 |
|
51 |
|
52 |
def migrate_user_setting(): |
53 |
print 'Migrating user-specific settings...' |
54 |
try:
|
55 |
resource = Resource.objects.get(name=RESOURCE) |
56 |
except Resource.DoesNotExist:
|
57 |
print "Resource '%s' not found." % RESOURCE |
58 |
exit()
|
59 |
|
60 |
users = set()
|
61 |
settings = UserSetting.objects.filter(setting=SETTING) |
62 |
for setting in settings: |
63 |
user = setting.user |
64 |
value = setting.value |
65 |
q, created = AstakosUserQuota.objects.get_or_create( |
66 |
user=user, resource=resource, |
67 |
defaults={'capacity': value})
|
68 |
if not created: |
69 |
print "Base quota already exists: %s %s" % (user.uuid, RESOURCE) |
70 |
continue
|
71 |
print "Migrated base quota: %s %s %s" % (user.uuid, RESOURCE, value) |
72 |
users.add(user) |
73 |
|
74 |
qh_sync_users(users) |
75 |
|
76 |
|
77 |
def modify_limit(): |
78 |
command = ['snf-manage', 'resource-modify', |
79 |
RESOURCE, '--limit-interactive']
|
80 |
print 'Running "%s"...' % ' '.join(command) |
81 |
r = call(command) |
82 |
|
83 |
if r != 0: |
84 |
print ('Setting resource limit failed. Have you registered ' |
85 |
'service astakos?')
|
86 |
print 'Aborting.' |
87 |
exit()
|
88 |
|
89 |
|
90 |
def reconcile(): |
91 |
command = ['snf-manage', 'reconcile-resources-astakos', '--fix', '--force'] |
92 |
print 'Running "%s"...' % ' '.join(command) |
93 |
r = call(command) |
94 |
|
95 |
|
96 |
def main(): |
97 |
(options, args) = parser.parse_args() |
98 |
usersetting = options.usersetting |
99 |
|
100 |
if not usersetting: |
101 |
modify_limit() |
102 |
print
|
103 |
migrate_user_setting() |
104 |
if not usersetting: |
105 |
print
|
106 |
reconcile() |
107 |
|
108 |
|
109 |
if __name__ == '__main__': |
110 |
main() |