Statistics
| Branch: | Tag: | Revision:

root / contrib / migrate-users @ 6c1c0738

History | View | Annotate | Download (4 kB)

1 98137a34 Sofia Papagiannaki
#!/usr/bin/env python
2 98137a34 Sofia Papagiannaki
3 2e662088 Antony Chazapis
# Copyright 2011-2012 GRNET S.A. All rights reserved.
4 98137a34 Sofia Papagiannaki
# 
5 98137a34 Sofia Papagiannaki
# Redistribution and use in source and binary forms, with or
6 98137a34 Sofia Papagiannaki
# without modification, are permitted provided that the following
7 98137a34 Sofia Papagiannaki
# conditions are met:
8 98137a34 Sofia Papagiannaki
# 
9 98137a34 Sofia Papagiannaki
#   1. Redistributions of source code must retain the above
10 98137a34 Sofia Papagiannaki
#      copyright notice, this list of conditions and the following
11 98137a34 Sofia Papagiannaki
#      disclaimer.
12 98137a34 Sofia Papagiannaki
# 
13 98137a34 Sofia Papagiannaki
#   2. Redistributions in binary form must reproduce the above
14 98137a34 Sofia Papagiannaki
#      copyright notice, this list of conditions and the following
15 98137a34 Sofia Papagiannaki
#      disclaimer in the documentation and/or other materials
16 98137a34 Sofia Papagiannaki
#      provided with the distribution.
17 98137a34 Sofia Papagiannaki
# 
18 98137a34 Sofia Papagiannaki
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
19 98137a34 Sofia Papagiannaki
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 98137a34 Sofia Papagiannaki
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 98137a34 Sofia Papagiannaki
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
22 98137a34 Sofia Papagiannaki
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 98137a34 Sofia Papagiannaki
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 98137a34 Sofia Papagiannaki
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
25 98137a34 Sofia Papagiannaki
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 98137a34 Sofia Papagiannaki
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 98137a34 Sofia Papagiannaki
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 98137a34 Sofia Papagiannaki
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 98137a34 Sofia Papagiannaki
# POSSIBILITY OF SUCH DAMAGE.
30 98137a34 Sofia Papagiannaki
# 
31 98137a34 Sofia Papagiannaki
# The views and conclusions contained in the software and
32 98137a34 Sofia Papagiannaki
# documentation are those of the authors and should not be
33 98137a34 Sofia Papagiannaki
# interpreted as representing official policies, either expressed
34 98137a34 Sofia Papagiannaki
# or implied, of GRNET S.A.
35 98137a34 Sofia Papagiannaki
36 98137a34 Sofia Papagiannaki
from sqlalchemy import Table
37 5fdaf331 Sofia Papagiannaki
from sqlalchemy.sql import select
38 5fdaf331 Sofia Papagiannaki
39 2f14c43c Sofia Papagiannaki
from pithos.im.models import User
40 98137a34 Sofia Papagiannaki
41 5a96180b Antony Chazapis
from migrate import Migration
42 5a96180b Antony Chazapis
43 98137a34 Sofia Papagiannaki
import base64
44 98137a34 Sofia Papagiannaki
45 98137a34 Sofia Papagiannaki
class UserMigration(Migration):
46 98137a34 Sofia Papagiannaki
    def __init__(self, db):
47 98137a34 Sofia Papagiannaki
        Migration.__init__(self, db)
48 98137a34 Sofia Papagiannaki
        self.gss_users = Table('gss_user', self.metadata, autoload=True)
49 98137a34 Sofia Papagiannaki
    
50 98137a34 Sofia Papagiannaki
    def execute(self):
51 2f14c43c Sofia Papagiannaki
        for u in self.retrieve_users():
52 2f14c43c Sofia Papagiannaki
            user = User()
53 98137a34 Sofia Papagiannaki
            user.pk = u['id']
54 98137a34 Sofia Papagiannaki
            user.uniq = u['username']
55 98137a34 Sofia Papagiannaki
            user.realname = u['name']
56 98137a34 Sofia Papagiannaki
            user.affiliation = u['homeorganization'] if u['homeorganization'] else ''
57 98137a34 Sofia Papagiannaki
            user.auth_token = base64.b64encode(u['authtoken'])
58 98137a34 Sofia Papagiannaki
            user.auth_token_created = u['creationdate']
59 98137a34 Sofia Papagiannaki
            user.auth_token_expires = u['authtokenexpirydate']
60 98137a34 Sofia Papagiannaki
            user.created = u['creationdate']
61 98137a34 Sofia Papagiannaki
            user.updated = u['modificationdate']
62 2f14c43c Sofia Papagiannaki
            user.email = u['email']
63 2f14c43c Sofia Papagiannaki
            user.active = 'ACTIVE' if u['active'] else 'SUSPENDED'
64 98137a34 Sofia Papagiannaki
            print '#', user
65 98137a34 Sofia Papagiannaki
            user.save(update_timestamps=False)
66 5fdaf331 Sofia Papagiannaki
            
67 5fdaf331 Sofia Papagiannaki
            #create user groups
68 5fdaf331 Sofia Papagiannaki
            for (owner, group, members) in self.retrieve_groups(u['username']):
69 5fdaf331 Sofia Papagiannaki
                self.backend.permissions.group_addmany(owner, group, members)
70 5fdaf331 Sofia Papagiannaki
    
71 2f14c43c Sofia Papagiannaki
    
72 2f14c43c Sofia Papagiannaki
    def retrieve_users(self):
73 2f14c43c Sofia Papagiannaki
        s = self.gss_users.select()
74 2f14c43c Sofia Papagiannaki
        rp = self.conn.execute(s)
75 2f14c43c Sofia Papagiannaki
        user = rp.fetchone()
76 2f14c43c Sofia Papagiannaki
        while user:
77 2f14c43c Sofia Papagiannaki
            yield user
78 2f14c43c Sofia Papagiannaki
            user = rp.fetchone()
79 2f14c43c Sofia Papagiannaki
        rp.close()
80 5fdaf331 Sofia Papagiannaki
    
81 5fdaf331 Sofia Papagiannaki
    def retrieve_groups(self, owner):
82 5fdaf331 Sofia Papagiannaki
        gss_group = Table('gss_group', self.metadata, autoload=True)
83 5fdaf331 Sofia Papagiannaki
        gss_user = Table('gss_user', self.metadata, autoload=True)
84 5fdaf331 Sofia Papagiannaki
        group_user = Table('gss_group_gss_user', self.metadata, autoload=True)
85 5fdaf331 Sofia Papagiannaki
        j1 = gss_group.join(gss_user, gss_group.c.owner_id == gss_user.c.id)
86 5fdaf331 Sofia Papagiannaki
        j2 = group_user.join(gss_user, group_user.c.members_id == gss_user.c.id)
87 5fdaf331 Sofia Papagiannaki
        s = select([gss_group.c.id, gss_group.c.name, gss_user.c.username], from_obj=j1)
88 5fdaf331 Sofia Papagiannaki
        s = s.where(gss_user.c.username == owner)
89 5fdaf331 Sofia Papagiannaki
        rp = self.conn.execute(s)
90 5fdaf331 Sofia Papagiannaki
        gr = rp.fetchone()
91 5fdaf331 Sofia Papagiannaki
        while gr:
92 5fdaf331 Sofia Papagiannaki
            id, group, owner = gr
93 5fdaf331 Sofia Papagiannaki
            s = select([gss_user.c.username], from_obj=j2)
94 5fdaf331 Sofia Papagiannaki
            s = s.where(group_user.c.groupsmember_id == id)
95 5fdaf331 Sofia Papagiannaki
            rp2 = self.conn.execute(s)
96 5fdaf331 Sofia Papagiannaki
            members = rp2.fetchall()
97 5fdaf331 Sofia Papagiannaki
            rp2.close()
98 5fdaf331 Sofia Papagiannaki
            yield owner, group, (m[0] for m in members)
99 5fdaf331 Sofia Papagiannaki
            gr = rp.fetchone()
100 5fdaf331 Sofia Papagiannaki
        rp.close()
101 98137a34 Sofia Papagiannaki
102 98137a34 Sofia Papagiannaki
if __name__ == "__main__":
103 5fdaf331 Sofia Papagiannaki
    db = ''
104 98137a34 Sofia Papagiannaki
    m = UserMigration(db)
105 5a96180b Antony Chazapis
    m.execute()