Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / scripts / upgrade / migrate_014.py @ 50f74340

History | View | Annotate | Download (3.6 kB)

1 1f5565c3 Giorgos Korfiatis
# Copyright 2013 GRNET S.A. All rights reserved.
2 1f5565c3 Giorgos Korfiatis
#
3 1f5565c3 Giorgos Korfiatis
# Redistribution and use in source and binary forms, with or
4 1f5565c3 Giorgos Korfiatis
# without modification, are permitted provided that the following
5 1f5565c3 Giorgos Korfiatis
# conditions are met:
6 1f5565c3 Giorgos Korfiatis
#
7 1f5565c3 Giorgos Korfiatis
#   1. Redistributions of source code must retain the above
8 1f5565c3 Giorgos Korfiatis
#      copyright notice, this list of conditions and the following
9 1f5565c3 Giorgos Korfiatis
#      disclaimer.
10 1f5565c3 Giorgos Korfiatis
#
11 1f5565c3 Giorgos Korfiatis
#   2. Redistributions in binary form must reproduce the above
12 1f5565c3 Giorgos Korfiatis
#      copyright notice, this list of conditions and the following
13 1f5565c3 Giorgos Korfiatis
#      disclaimer in the documentation and/or other materials
14 1f5565c3 Giorgos Korfiatis
#      provided with the distribution.
15 1f5565c3 Giorgos Korfiatis
#
16 1f5565c3 Giorgos Korfiatis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 1f5565c3 Giorgos Korfiatis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 1f5565c3 Giorgos Korfiatis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1f5565c3 Giorgos Korfiatis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 1f5565c3 Giorgos Korfiatis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 1f5565c3 Giorgos Korfiatis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 1f5565c3 Giorgos Korfiatis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 1f5565c3 Giorgos Korfiatis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 1f5565c3 Giorgos Korfiatis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 1f5565c3 Giorgos Korfiatis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 1f5565c3 Giorgos Korfiatis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 1f5565c3 Giorgos Korfiatis
# POSSIBILITY OF SUCH DAMAGE.
28 1f5565c3 Giorgos Korfiatis
#
29 1f5565c3 Giorgos Korfiatis
# The views and conclusions contained in the software and
30 1f5565c3 Giorgos Korfiatis
# documentation are those of the authors and should not be
31 1f5565c3 Giorgos Korfiatis
# interpreted as representing official policies, either expressed
32 1f5565c3 Giorgos Korfiatis
# or implied, of GRNET S.A.
33 1f5565c3 Giorgos Korfiatis
34 ae883db3 Giorgos Korfiatis
from subprocess import call
35 1f5565c3 Giorgos Korfiatis
import os
36 1f5565c3 Giorgos Korfiatis
os.environ['DJANGO_SETTINGS_MODULE'] = 'synnefo.settings'
37 1f5565c3 Giorgos Korfiatis
from astakos.im.models import UserSetting, AstakosUserQuota, Resource
38 1f5565c3 Giorgos Korfiatis
from astakos.im.quotas import qh_sync_users
39 1f5565c3 Giorgos Korfiatis
40 ae883db3 Giorgos Korfiatis
from optparse import OptionParser
41 ae883db3 Giorgos Korfiatis
42 ae883db3 Giorgos Korfiatis
parser = OptionParser()
43 ae883db3 Giorgos Korfiatis
parser.add_option("--usersetting", dest="usersetting",
44 ae883db3 Giorgos Korfiatis
                  action="store_true", default=False,
45 ae883db3 Giorgos Korfiatis
                  help="Only migrate user-specific settings",
46 ae883db3 Giorgos Korfiatis
                  )
47 ae883db3 Giorgos Korfiatis
48 1f5565c3 Giorgos Korfiatis
SETTING = 'PENDING_APPLICATION_LIMIT'
49 1f5565c3 Giorgos Korfiatis
RESOURCE = 'astakos.pending_app'
50 1f5565c3 Giorgos Korfiatis
51 1f5565c3 Giorgos Korfiatis
52 ae883db3 Giorgos Korfiatis
def migrate_user_setting():
53 ae883db3 Giorgos Korfiatis
    print 'Migrating user-specific settings...'
54 1f5565c3 Giorgos Korfiatis
    try:
55 1f5565c3 Giorgos Korfiatis
        resource = Resource.objects.get(name=RESOURCE)
56 1f5565c3 Giorgos Korfiatis
    except Resource.DoesNotExist:
57 ae883db3 Giorgos Korfiatis
        print "Resource '%s' not found." % RESOURCE
58 ae883db3 Giorgos Korfiatis
        exit()
59 1f5565c3 Giorgos Korfiatis
60 103086a4 Giorgos Korfiatis
    users = set()
61 1f5565c3 Giorgos Korfiatis
    settings = UserSetting.objects.filter(setting=SETTING)
62 1f5565c3 Giorgos Korfiatis
    for setting in settings:
63 1f5565c3 Giorgos Korfiatis
        user = setting.user
64 1f5565c3 Giorgos Korfiatis
        value = setting.value
65 1f5565c3 Giorgos Korfiatis
        q, created = AstakosUserQuota.objects.get_or_create(
66 1f5565c3 Giorgos Korfiatis
            user=user, resource=resource,
67 1f5565c3 Giorgos Korfiatis
            defaults={'capacity': value})
68 1f5565c3 Giorgos Korfiatis
        if not created:
69 1f5565c3 Giorgos Korfiatis
            print "Base quota already exists: %s %s" % (user.uuid, RESOURCE)
70 1f5565c3 Giorgos Korfiatis
            continue
71 1f5565c3 Giorgos Korfiatis
        print "Migrated base quota: %s %s %s" % (user.uuid, RESOURCE, value)
72 103086a4 Giorgos Korfiatis
        users.add(user)
73 1f5565c3 Giorgos Korfiatis
74 103086a4 Giorgos Korfiatis
    qh_sync_users(users)
75 1f5565c3 Giorgos Korfiatis
76 1f5565c3 Giorgos Korfiatis
77 ae883db3 Giorgos Korfiatis
def modify_limit():
78 ae883db3 Giorgos Korfiatis
    command = ['snf-manage', 'resource-modify',
79 ae883db3 Giorgos Korfiatis
               RESOURCE, '--limit-interactive']
80 ae883db3 Giorgos Korfiatis
    print 'Running "%s"...' % ' '.join(command)
81 ae883db3 Giorgos Korfiatis
    r = call(command)
82 ae883db3 Giorgos Korfiatis
83 ae883db3 Giorgos Korfiatis
    if r != 0:
84 ae883db3 Giorgos Korfiatis
        print ('Setting resource limit failed. Have you registered '
85 ae883db3 Giorgos Korfiatis
               'service astakos?')
86 ae883db3 Giorgos Korfiatis
        print 'Aborting.'
87 ae883db3 Giorgos Korfiatis
        exit()
88 ae883db3 Giorgos Korfiatis
89 ae883db3 Giorgos Korfiatis
90 ae883db3 Giorgos Korfiatis
def reconcile():
91 05c1b849 Giorgos Korfiatis
    command = ['snf-manage', 'reconcile-resources-astakos', '--fix', '--force']
92 ae883db3 Giorgos Korfiatis
    print 'Running "%s"...' % ' '.join(command)
93 ae883db3 Giorgos Korfiatis
    r = call(command)
94 ae883db3 Giorgos Korfiatis
95 ae883db3 Giorgos Korfiatis
96 ae883db3 Giorgos Korfiatis
def main():
97 ae883db3 Giorgos Korfiatis
    (options, args) = parser.parse_args()
98 ae883db3 Giorgos Korfiatis
    usersetting = options.usersetting
99 ae883db3 Giorgos Korfiatis
100 ae883db3 Giorgos Korfiatis
    if not usersetting:
101 ae883db3 Giorgos Korfiatis
        modify_limit()
102 ae883db3 Giorgos Korfiatis
        print
103 ae883db3 Giorgos Korfiatis
    migrate_user_setting()
104 ae883db3 Giorgos Korfiatis
    if not usersetting:
105 ae883db3 Giorgos Korfiatis
        print
106 ae883db3 Giorgos Korfiatis
        reconcile()
107 ae883db3 Giorgos Korfiatis
108 ae883db3 Giorgos Korfiatis
109 1f5565c3 Giorgos Korfiatis
if __name__ == '__main__':
110 1f5565c3 Giorgos Korfiatis
    main()