no longr build 31/sfapy2
[infrastructure.git] / migration_scripts / Migration.py
1 #!/usr/bin/env python
2 #this script is for migrating sites, user from PLC to PLE
3 import os,sys, time
4 import xmlrpclib
5 from optparse import OptionParser
6 from pprint import PrettyPrinter
7
8
9
10 class Migration:
11     subversion_id = "$Id Amine"
12
13     def __init__(self):
14         try:
15         #Remote(PLC) Api connexion
16             self.remote_name="PLC"
17             self.remote_url="https://tac.inria.fr:443/PLCAPI/"
18             self.remote_server=xmlrpclib.Server(self.remote_url,allow_none=True)
19         #local(PLE) Api connexion
20             self.local_name="PLE"
21             self.local_url="https://garfield.inria.fr:443/PLCAPI/"
22             self.local_server=xmlrpclib.Server(self.local_url,allow_none=True)
23         except : raise
24         
25     def local_auth(self):
26         return {'Username': 'root@plc2.inria.fr',
27                 'AuthMethod':'password',
28                 'AuthString':'*****',
29                 'Role' : 'root'
30                 }
31
32     def remote_auth(self):
33         return {'Username': 'root@plc1.inria.fr',
34                 'AuthMethod':'password',
35                 'AuthString':'******',
36                 'Role' : 'root'
37                 }
38     
39     def header(self,message):
40         now=time.strftime("%H:%M:%S", time.localtime())
41         print "*",now,'--',message
42
43     def migrate_persons(self, person_ids, site_id):
44         self.header("Dealing with persons migration")
45         for person_id in person_ids:
46             remote_person=self.remote_server.GetPersons(self.remote_auth(),{'person_id':person_id})
47             remote_person[0]['peer_id']= None
48             self.header("Migrating from %s to %s the person %s \n"%(self.remote_name,self.local_name,
49                                                                     remote_person[0]))
50             local_person=self.local_server.AddPerson(self.local_auth(),remote_person[0])
51             self.header("Add %s to Site %d \n"%(remote_person[0]['email'],site_id))
52             status=self.local_server.AddPersonToSite(self.local_auth(),
53                                                      remote_person[0]['email'],site_id)
54             #to be coded
55                #Add roles to person the same that he has in his remote platform
56                #Adding keys
57                #Adding person to slice
58                
59     def migrate_sites(self,sites_names):
60         self.header("Dealing with sites migration")
61         for site_name in sites_names:
62             remote_site=self.remote_server.GetSites(self.remote_auth(),{'name':site_name})
63             self.header("Migrating from %s the site %s \n"%(self.remote_name,remote_site))
64             remote_persons_ids=remote_site[0]['person_ids']
65             peer_site_id=remote_site[0]['site_id']
66             #make some changes to the new incoming site
67             remote_site[0]['peer_site_id']=peer_site_id
68             remote_site[0]['peer_id']= None
69             remote_site[0]['login_base']=remote_site[0]['login_base']+'ple'
70             remote_site[0]['enabled']=True
71             local_site=self.local_server.AddSite(self.local_auth(),remote_site[0])
72             #May be we have to manage the site address too.... to be coded
73             self.header("Getting in %s a new  site %s\n"%(self.local_name,
74                                                           self.local_server.GetSites(self.local_auth(),
75                                                                                      {'site_id':local_site})))
76             #getting person involved in the remote site
77             self.migrate_persons(remote_persons_ids,local_site)
78             return True
79         
80     def run (self):
81         usage = """usage: %%prog  sites_names [site_name1][site_name2].......
82         """
83         parser=OptionParser(usage=usage,version=self.subversion_id)
84         parser.add_option("-v","--verbose", action="store_true", dest="verbose", default=False, 
85                           help="Run in verbose mode")
86         (self.options, self.args) = parser.parse_args()
87         
88         if len(self.args) == 0:
89             print usage
90         else:
91             self.migrate_sites(self.args)
92         
93     
94     def main(self):
95         try:
96             success=self.run()
97             if success:
98                 return 0
99             else:
100                 return 1 
101         except SystemExit:
102             raise
103   
104 if __name__ == "__main__":
105     sys.exit(Migration().main())