testing peers - mostly a convenience for cut&paste right now
[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 ### HOWTO
18 ### 
19 ### ----------------------------------------
20 ### # run sql commands - PLC1
21 ### 
22 ### $ chroot /plc1/root psql planetlab4 pgsqluser
23 ###
24 ### # run sql commands - PLC2
25 ### 
26 ### $ chroot /plc2/root psql -h localhost -p 5433 planetlab4 pgsqluser
27 ### but then a password is required
28 ### 9a61ae18-779e-41b6-8a6c-67c439dc73e5
29 ### 
30 ### ----------------------------------------
31 ### # connecting to the API - PLC1
32 ###
33 ### $ chroot /plc1/root
34 ### $ ./Shell.py --config /plc1/root/data/etc/planetlab/plc_config
35 ###
36 ### # connecting to the API - PLC2
37 ###
38 ### $ chroot /plc2/root
39 ### 
40 ### $ ./Shell.py --config /plc2/root/data/etc/planetlab/plc_config -h https://localhost:444/PLCAPI/
41 ### 
42 ### ----------------------------------------
43 ##############################
44
45 import xmlrpclib
46
47 plc1={ 'name':'plc1 in federation',
48        'root':'/plc1/root',
49        'url':'https://lurch.cs.princeton.edu:443/',
50        'admin_id':'plc1@planet-lab.org',
51        'admin_password':'root',
52        'dbport':5432,
53        'nodename':'n11.plc1.org'
54        }
55 plc2={ 'name':'plc2 in federation',
56        'root':'/plc1/root',
57        'url':'https://lurch.cs.princeton.edu:444/',
58        'admin_id':'plc2@planet-lab.org',
59        'admin_password':'root',
60        'dbport':5433,
61        'nodename':'n21.plc2.org'
62        }
63
64 plc=[plc1,plc2]
65 # the server objects
66 s=[None,None]
67 # the authentication objects
68 a=[None,None]
69
70 ### cant use digits in slice login name
71 def plain_name (i):
72     if i==1:
73         return 'one'
74     elif i==2:
75         return 'two'
76     else:
77         raise Exception,"Unexpected input in plain_name"
78
79 def test00_init (args=[0,1]):
80     global plc,s,a
81     for i in args:
82         url=plc[i]['url']+'/PLCAPI/'
83         s[i]=xmlrpclib.Server(url)
84         print 'initializing s[%d]'%i,url
85         a[i]={'Username':plc[i]['admin_id'],
86               'AuthMethod':'password',
87               'AuthString':plc[i]['admin_password'],
88               'Role':'admin'}
89         print 'initialized a[%d]'%i, a[i]
90
91 def test00_check (args=[0,1]):
92     global plc,s,a
93     for i in args:
94         n=len(s[i].GetNodes(a[i]))
95         f=len(s[i].GetForeignNodes(a[i]))
96         print 'Checking connection: got %d local nodes & %d foreign nodes'%(n,f)
97
98 def test01_pi (args=[0,1]):
99     global plc,s,a
100     for i in args:
101         peer=1-i
102         plc[i]['pi_id']=s[i].AddPerson(a[i],{'first_name':'Local', 'last_name':'PI', 'role_ids':[20],
103                                              'email':plc[i]['admin_id'],'password':plc[id]['admin_password']})
104
105 def test01_node (args=[0,1]):
106     global plc,s,a
107     for i in args:
108         n=s[i].AddNode(a[i],1,{'hostname': plc[i]['nodename']})
109         print '%02d: Added node %d %s',(i+1,n,plc[i]['nodename'])
110
111 def test01_peer_person (args=[0,1]):
112     global plc,s,a
113     for i in args:
114         peer=1-i
115         person_id = s[i].AddPerson (a[i], {'first_name':'Peering', 'last_name':plc[peer]['name'], 'role_ids':[3000],
116                                            'email':plc[peer]['admin_id'],'password':plc[peer]['admin_password']})
117         print '02%d:Created person %d as the peer person'%(i+1,person_id)
118         plc[i]['peer_person_id']=person_id
119
120 def test01_peer (args=[0,1]):
121     global plc,s,a
122     for i in args:
123         peer=1-i
124         peer_id=s[i].AddPeer (a[i], {'peername':plc[peer]['name'],'peer_url':plc[peer]['url'],'person_id':plc[i]['peer_person_id']})
125         # NOTE : need to manually reset the encrypted password through SQL at this point
126         print '%02d:Created peer %d'%(i+1,peer_id)
127         plc[i]['peer_id']=peer_id
128         print "Please MANUALLY set passwd for person_id=%d in DB%d"%(person_id,i+1)
129     
130 def test02_refresh (args=[0,1]):
131     global plc,s,a
132     for i in args:
133         print '%02d: Refreshing peer'%(i+1)
134         s[i].RefreshPeer(plc[i]['peer_id'])
135         ###### at this stage both sites know about two nodes, one local and one foreign
136
137 def test03_site (args=[0,1]):
138     global plc,s,a
139     for i in args:
140         peer=1-i
141         ### create a site (required for creating a slice)
142         sitename="site"+str(i+1)
143         abbrev_name="abbr"+str(i+1)
144         plain=plain_name(i+1)
145         site_id=s[i].AddSite (a[i], {'name':plc[i]['name'],
146                                      'abbreviated_name': abbrev_name,
147                                      'login_base': plain,
148                                      'is_public': True,
149                                      'url': 'http://%s.com/'%abbrev_name,
150                                      'max_slices':10})
151         ### max_slices does not seem taken into account at that stage
152         s[i].UpdateSite(a[i],plc[i]['site_id'],{'max_slices':10})
153         print '%02d: Created site %d with max_slices=10'%(i+1,site_id)
154         plc[i]['site_id']=site_id
155
156 def test03_slice (args=[0,1]):
157     global plc,s,a
158     for i in args:
159         peer=1-i
160         plain=plain_name(i+1)
161         ### create a slice
162         slice_name="slic"+str(i+1)
163         slice_id=s[i].AddSlice (a[i],{'name':'%s_%s'%(plain,slice_name),
164                                       'description':'slice %s_%s on plc %s'%(plain,slice_name,plc[i]['name']),
165                                       'url':'http://planet-lab.org/%s'%slice_name,
166                                       'max_nodes':100,
167                                       'instanciation':'plc-instantiated',
168                                       })
169         print '%02d: created slice %d'%(i+1,slice_id)
170         plc[i]['slice_id']=slice_id
171         
172
173 def test04_lnode (args=[0,1]):
174     global plc,s,a
175     for i in args:
176         ### add node to it
177         hostname=plc[i]['nodename']
178         s[i].AddSliceToNodes (a[i], plc[i]['slice_id'],hostname)
179         print '%02d: added local node %s'%(i+1,hostname)
180
181 def test04_fnode (args=[0,1]):
182     global plc,s,a
183     for i in args:
184         peer=1-i
185         ### add node to it
186         hostname=plc[peer]['nodename']
187         s[i].AddSliceToNodes (a[i], plc[i]['slice_id'],hostname)
188         print '%02d: added local node %s'%(i+1,hostname)
189