- keep up to date with recent changes in planetlab4.sql schema
[plcapi.git] / TestPeers.py
1 #!/usr/bin/env python
2 ###
3 ##############################
4 ###
5 ### preparation / requirements
6 ###
7 ### two separate instances of myplc
8 ### for now they are located on the same box on lurch
9 ###
10 ### expectations :
11 ### your myplcs should more or less come out of the box, 
12 ### I prefer not to alter the default PLC_ROOT_USER value,
13 ### instead we create a PI account on the site_id=1
14 ###
15 ##############################
16
17 import xmlrpclib
18 import os
19
20 plc1={ 'name':'plc1 in federation',
21        'hostname':'lurch.cs.princeton.edu',
22        'url-format':'https://%s:443/PLCAPI/',
23        'builtin_admin_id':'root@localhost.localdomain',
24        'builtin_admin_password':'root',
25        'peer_admin_name':'plc1@planet-lab.org',
26        'peer_admin_password':'peer',
27        'nodename':'n11.plc1.org'
28        }
29 plc2={ 'name':'plc2 in federation',
30        'hostname':'planetlab-devbox.inria.fr',
31        'url-format':'https://%s:443/PLCAPI/',
32        'builtin_admin_id':'root@localhost.localdomain',
33        'builtin_admin_password':'root',
34        'peer_admin_name':'plc2@planet-lab.org',
35        'peer_admin_password':'peer',
36        'nodename':'n21.plc2.org'
37        }
38
39 ## we use indexes 1 and 2 
40 plc=[None,None,None]
41 # the server objects
42 s=[None,None,None]
43 # the authentication objects
44 a=[None,None,None]
45 aa=[None,None,None]
46
47 def peer_index(i):
48     return 3-i
49
50 ### cant use digits in slice login name
51 def plain_name (i):
52     if i==1:
53         return 'one'
54     elif i==2:
55         return 'two'
56     else:
57         raise Exception,"Unexpected input in plain_name"
58
59 def test00_init (args=[1,2]):
60     global plc,s,a,aa
61     ## have you loaded this file already (support for reload)
62     if plc[1]:
63         pass
64     else:
65         plc=[None,plc1,plc2]
66         for i in args:
67             url=plc[i]['url-format']%plc[i]['hostname']
68             plc[i]['url']=url
69             s[i]=xmlrpclib.Server(url)
70             print 'initializing s[%d]'%i,url
71             aa[i]={'Username':plc[i]['builtin_admin_id'],
72                    'AuthMethod':'password',
73                    'AuthString':plc[i]['builtin_admin_password'],
74                    'Role':'admin'}
75             print 'initialized aa[%d]'%i, aa[i]
76             a[i]={'Username':plc[i]['peer_admin_name'],
77                   'AuthMethod':'password',
78                   'AuthString':plc[i]['peer_admin_password'],
79                   'Role':'admin'}
80             print 'initialized a[%d]'%i, a[i]
81
82 def test00_print (args=[1,2]):
83     global plc,s,a,aa
84     for i in args:
85         print 's[%d]'%i,s[i]
86         print 'aa[%d]'%i, aa[i]
87         print 'a[%d]'%i, a[i]
88
89 def test00_admin (args=[1,2]):
90     global plc,s,a
91     for i in args:
92         peer=peer_index(i)
93         person_id=s[i].AddPerson(aa[i],{'first_name':'Local', 'last_name':'PeerPoint', 'role_ids':[10],
94                                               'email':plc[i]['peer_admin_name'],'password':plc[i]['peer_admin_password']})
95         print '%02d: created peer admin account %d, %s - %s'%(i,person_id,plc[i]['peer_admin_name'],plc[i]['peer_admin_password'])
96         plc[i]['peer_admin_id']=person_id
97
98 def test00_enable (args=[1,2]):
99     global plc,s,a
100     for i in args:
101         peer=peer_index(i)
102         s[i].AdmSetPersonEnabled(aa[i],plc[i]['peer_admin_id'],True)
103         s[i].AddRoleToPerson(aa[i],'admin',plc[i]['peer_admin_id'])
104         print '%02d: enabled+admin on account %d:%s'%(i,plc[i]['peer_admin_id'],plc[i]['peer_admin_name'])
105
106 def test01_check (args=[1,2]):
107     global plc,s,a
108     for i in args:
109         n=len(s[i].GetNodes(aa[i]))
110         f=len(s[i].GetForeignNodes(a[i]))
111         print '%02d: Checking connection: got %d local nodes & %d foreign nodes'%(i,n,f)
112
113 def test01_node (args=[1,2]):
114     global plc,s,a
115     for i in args:
116         n=s[i].AddNode(a[i],1,{'hostname': plc[i]['nodename']})
117         print '%02d: Added node %d %s'%(i,n,plc[i]['nodename'])
118
119 def test01_peer_person (args=[1,2]):
120     global plc,s,a
121     for i in args:
122         peer=peer_index(i)
123         person_id = s[i].AddPerson (a[i], {'first_name':'Peering(plain passwd)', 'last_name':plc[peer]['name'], 'role_ids':[3000],
124                                            'email':plc[peer]['peer_admin_name'],'password':plc[peer]['peer_admin_password']})
125         print '02%d:Created person %d as the peer person'%(i,person_id)        hostname=plc[i]['nodename']
126
127         plc[i]['peer_person_id']=person_id
128
129 def test01_peer (args=[1,2]):
130     global plc,s,a
131     for i in args:
132         peer=peer_index(i)
133         peer_id=s[i].AddPeer (a[i], {'peername':plc[peer]['name'],'peer_url':plc[peer]['url'],'person_id':plc[i]['peer_person_id']})
134         # NOTE : need to manually reset the encrypted password through SQL at this point
135         print '%02d:Created peer %d'%(i,peer_id)
136         plc[i]['peer_id']=peer_id
137         print "PLEASE manually set password for person_id=%d in DB%d"%(plc[i]['peer_person_id'],i)
138
139 def test01_peer_passwd (args=[1,2]):
140     global plc,s,a
141     for i in args:
142         # using an ad-hoc local command for now - never could get quotes to reach sql....
143         os.system("ssh root@%s new_plc_api/person-password.sh %d"%(plc[i]['hostname'],plc[i]['peer_person_id']))
144         print "Attempted to set passwd for person_id=%d in DB%d"%(plc[i]['peer_person_id'],i)
145     
146 def test02_refresh (args=[1,2]):
147     global plc,s,a
148     for i in args:
149         print '%02d: Refreshing peer'%(i)
150         s[i].RefreshPeer(a[i],plc[i]['peer_id'])
151
152 def test03_site (args=[1,2]):
153     global plc,s,a
154     for i in args:
155         peer=peer_index(i)
156         ### create a site (required for creating a slice)
157         sitename="site"+str(i)
158         abbrev_name="abbr"+str(i)
159         plain=plain_name(i)
160         site_id=s[i].AddSite (a[i], {'name':plc[i]['name'],
161                                      'abbreviated_name': abbrev_name,
162                                      'login_base': plain,
163                                      'is_public': True,
164                                      'url': 'http://%s.com/'%abbrev_name,
165                                      'max_slices':10})
166         ### max_slices does not seem taken into account at that stage
167         s[i].UpdateSite(a[i],site_id,{'max_slices':10})
168         print '%02d: Created site %d with max_slices=10'%(i,site_id)
169         plc[i]['site_id']=site_id
170
171 def test03_slice (args=[1,2]):
172     global plc,s,a
173     for i in args:
174         peer=peer_index(i)
175         plain=plain_name(i)
176         ### create a slice
177         slice_name="slic"+str(i)
178         slice_id=s[i].AddSlice (a[i],{'name':'%s_%s'%(plain,slice_name),
179                                       'description':'slice %s_%s on plc %s'%(plain,slice_name,plc[i]['name']),
180                                       'url':'http://planet-lab.org/%s'%slice_name,
181                                       'max_nodes':100,
182                                       'instanciation':'plc-instantiated',
183                                       })
184         print '%02d: created slice %d'%(i,slice_id)
185         plc[i]['slice_id']=slice_id
186         
187
188 def test04_nodeslice (args=[1,2]):
189     global plc,s,a
190     for i in args:
191         peer=peer_index(i)
192         ### add node to it
193         hostname=plc[peer]['nodename']
194         s[i].AddSliceToNodes (a[i], plc[i]['slice_id'],[hostname])
195         print '%02d: added local node %s'%(i,hostname)
196
197 def catch_up (args=[1,2]):
198     for i in args:
199         plc[i]['peer_admin_id']=3
200         plc[i]['peer_person_id']=4
201         plc[i]['peer_id']=1
202         plc[i]['slice_id']=1
203
204 ###
205 def test_all (args=[1,2]):
206     test00_init (args)
207     test00_print (args)
208     test00_admin (args)
209     test00_enable (args)
210     test01_check (args)
211     test01_node (args)
212     test01_peer_person (args)
213     test01_peer (args)
214     test01_peer_passwd (args)
215     test01_check (args)
216     test02_refresh (args)
217     test01_check (args)
218     test03_site (args)
219     test03_slice (args)
220     test04_nodeslice (args)
221
222 if __name__ == '__main__':
223     test_all()
224