*No need anymore to check nodes from the TestSite class.
[tests.git] / system / TestSite.py
1 import os.path
2 import datetime
3 import time
4 import traceback
5
6 import utils
7 from TestNode import TestNode
8 from TestUser import TestUser
9 from TestKey import TestKey
10
11 class TestSite:
12
13     def __init__ (self,test_plc,site_spec):
14         self.test_plc=test_plc
15         self.site_spec=site_spec
16         
17     def name(self):
18         return self.site_spec['site_fields']['login_base']
19
20     def create_site (self):
21         print self.test_plc.auth_root()
22         self.test_plc.server.AddSite(self.test_plc.auth_root(),
23                                                     self.site_spec['site_fields'])
24         self.test_plc.server.AddSiteAddress(self.test_plc.auth_root(),self.name(),
25                                             self.site_spec['address_fields'])
26             
27     def create_users (self):
28         for user_spec in self.site_spec['users']:
29             test_user=TestUser(self.test_plc,self,user_spec)
30             test_user.create_user()
31             test_user.add_keys()            
32
33     def delete_site (self):
34         print self.test_plc.auth_root()
35         self.test_plc.server.DeleteSite(self.test_plc.auth_root(),self.name())
36         return True
37             
38     def delete_users(self):
39         for user_spec in self.site_spec['users']:
40             test_user=TestUser(self.test_plc,self,user_spec)
41             test_user.delete_user()
42
43     def locate_user (self,username):
44         for user in self.site_spec['users']:
45             if user['name'] == username:
46                 return user
47             if user['user_fields']['email'] == username:
48                 return user
49         raise Exception,"Cannot locate user %s"%username
50         
51     def locate_node (self,nodename):
52         for node in self.site_spec['nodes']:
53             if node['node_fields']['hostname'] == nodename:
54                 return node
55         raise Exception,"Cannot locate node %s"%nodename
56         
57     def start_nodes (self,options):
58         for node_spec in self.site_spec['nodes']:
59             TestNode(self.test_plc, self, node_spec).start_node(options)
60         return True
61            
62     def delete_known_hosts(self):
63         utils.header("Messing with known_hosts (cleaning hostnames starting with 'test[0-9]')")
64         sed_command="sed -i -e '/^test[0-9]/d' /root/.ssh/known_hosts"
65         utils.system(sed_command)
66
67     # xxx should be attached to TestPlc
68     def check_slices(self):
69         
70         bool=True
71         bool1=True
72         secondes=15
73         self.delete_known_hosts()
74         start_time = datetime.datetime.now()
75         dead_time=start_time + datetime.timedelta(minutes=3)##adding 3minutes
76         for slice_spec in self.test_plc.plc_spec['slices']:
77             for hostname in slice_spec['nodenames']:
78                 slicename=slice_spec['slice_fields']['name']
79                 # locate the first avail. key
80                 found=False
81                 for username in slice_spec['usernames']:
82                     user_spec=self.locate_user(username)
83                     for keyname in user_spec['keynames']:
84                         key_spec=self.test_plc.locate_key(keyname)
85                         publickey=TestKey(self.test_plc,key_spec).publicpath()
86                         privatekey=TestKey(self.test_plc,key_spec).privatepath()
87                         if os.path.isfile(publickey) and os.path.isfile(privatekey):
88                             found=True
89                             break
90                 if not found:
91                     raise Exception,"Cannot find a valid key for slice %s"%slicename
92
93                 while(bool):
94                     utils.header('restarting nm on %s'%hostname)
95                     access=utils.system('ssh -i /etc/planetlab/root_ssh_key.rsa root@%s service nm restart'%hostname )
96                     if (access==0):
97                         utils.header('nm restarted on %s'%hostname)
98                         while(bool1):
99                             utils.header('trying to connect to %s@%s'%(slicename,hostname))
100                             ### should use saved keys instead of this hard-coded stuff
101                             Date=utils.system('ssh -i %s %s@%s date'%(privatekey,slicename,hostname))
102                             if (Date==0):
103                                 break
104                             elif ( start_time  <= dead_time ) :
105                                 start_time=datetime.datetime.now()+ datetime.timedelta(seconds=30)
106                                 time.sleep(secondes)
107                             else:
108                                 bool1=False
109                         if(bool1):
110                             utils.header('connected to %s@%s -->'%(slicename,hostname))
111                         else:
112                             utils.header('%s@%s : last chance - restarting nm on %s'%(slicename,hostname,hostname))
113                             access=utils.system('ssh -i /etc/planetlab/root_ssh_key.rsa  root@%s service nm restart'%hostname)
114                             if (access==0):
115                                 utils.header('trying to connect (2) to %s@%s'%(slicename,hostname))
116                                 Date=utils.system('ssh -i ~/.ssh/slices.rsa %s@%s date'%(slicename,hostname))
117                                 if (Date==0):
118                                     utils.header('connected to %s@%s -->'%(slicename,hostname))
119                                 else:
120                                     utils.header('giving up with to %s@%s -->'%(slicename,hostname))
121                                     return False
122                             else :
123                                 utils.header('Last chance failed on %s@%s -->'%(slicename,hostname))
124                         break
125                     elif ( start_time  <= dead_time ) :
126                         start_time=datetime.datetime.now()+ datetime.timedelta(minutes=1)
127                         time.sleep(secondes)
128                     else:
129                         bool=False
130                             
131         return bool