-Created new Slice. -Added new initscripts && associated the initscripts to the...
[tests.git] / system / TestNode.py
1 import os, sys, time, base64
2 import xmlrpclib
3 import pprint
4
5 import TestConfig
6 import utils
7
8 class TestNode:
9
10     def __init__ (self,test_plc,test_site,node_spec):
11         self.test_plc=test_plc
12         self.test_site=test_site
13         self.node_spec=node_spec
14
15     def create_node (self,role):
16         auth = self.test_site.anyuser_auth (role)
17         filter={'boot_state':'rins'}
18         try:
19             if (role=='pi' and self.node_spec['owned']=='pi'):
20                 self.node_id = \
21                     self.test_plc.server.AddNode(auth,
22                                                  self.test_site.site_spec['site_fields']['login_base'],
23                                                  self.node_spec)
24                 self.test_plc.server.AddNodeNetwork(auth,self.node_id,
25                                                     self.node_spec['network'])
26                 self.test_plc.server.UpdateNode(auth, self.node_id, filter)
27                 return self.node_id
28             
29             elif (role=='tech' and self.node_spec['owned']=='tech'):
30                 self.node_id = \
31                     self.test_plc.server.AddNode(auth,
32                                                  self.test_site.site_spec['site_fields']['login_base'],
33                                                  self.node_spec)
34                 self.test_plc.server.AddNodeNetwork(auth,self.node_id,
35                                                     self.node_spec['network'])
36                 self.test_plc.server.UpdateNode(auth, self.node_id, filter)
37                 return self.node_id
38         except Exception, e:
39                 print str(e)
40
41     def add_initscripts(self):
42         try:
43             for initscript in TestConfig.initscripts:
44                 utils.header('Adding Initscripts')
45                 pp = pprint.PrettyPrinter(indent=4)
46                 pp.pprint(initscript)
47                 self.initscript_id=self.test_plc.server.AddInitScript(self.test_plc.auth_root(),
48                                                                       initscript)
49         except Exception, e:
50             print str(e)
51             exit (1)
52
53     def create_slice(self, role):
54         auth = self.test_site.anyuser_auth (role)
55         liste_hosts=[]
56         try:
57             for slicespec in TestConfig.slices_specs :
58                 utils.header('Creating Slice')
59                 pp = pprint.PrettyPrinter(indent=4)
60                 pp.pprint(slicespec)
61                 slice_id=self.test_plc.server.AddSlice(auth,slicespec['slice_spec'])
62                 for sliceuser in slicespec['slice_users']:
63                     self.test_plc.server.AddPersonToSlice(auth, 
64                                                           sliceuser['email'], 
65                                                           slice_id)
66                 for slicenode in slicespec['slice_nodes']:
67                     liste_hosts.append(slicenode['hostname'])
68                     self.test_plc.server.AddSliceToNodes(auth,
69                                                          slice_id,
70                                                          liste_hosts)
71                     
72                 self.test_plc.server.AddSliceAttribute(self.test_plc.auth_root(),
73                                                        slice_id, 'initscript',
74                                                        slicespec['slice_initscript']['name'])
75             
76         except Exception, e:
77             print str(e)
78             sys.exit(1)
79         
80     def conffile(self,image,hostname,path):
81         template='%s/template-vmplayer/node.vmx'%(path)
82         actual='%s/vmplayer-%s/node.vmx'%(path,hostname)
83         sed_command="sed -e s,@BOOTCD@,%s,g %s > %s"%(image,template,actual)
84         utils.header('Creating %s from %s'%(actual,template))
85         os.system('set -x; ' + sed_command)
86
87     def create_boot_cd(self,path):
88         node_spec=self.node_spec
89         hostname=node_spec['hostname']
90         try:
91             utils.header('Initializing vmplayer area for node %s'%hostname)
92             clean_dir="rm -rf %s/vmplayer-%s"%(path,hostname)
93             mkdir_command="mkdir -p %s/vmplayer-%s"%(path,hostname)
94             tar_command="tar -C %s/template-vmplayer -cf - . | tar -C %s/vmplayer-%s -xf -"%(path,path,hostname)
95             os.system('set -x; ' +clean_dir + ';' + mkdir_command + ';' + tar_command);
96             utils.header('Creating boot medium for node %s'%hostname)
97             encoded=self.test_plc.server.GetBootMedium(self.test_plc.auth_root(), hostname, 'node-iso', '')
98             if (encoded == ''):
99                 raise Exception, 'boot.iso not found'
100             file=open(path+'/vmplayer-'+hostname+'/boot_file.iso','w')
101             file.write(base64.b64decode(encoded))
102             file.close()
103             utils.header('boot cd created for %s'%hostname)
104             self.conffile('boot_file.iso',hostname, path)
105         except Exception, e:
106             print str(e)
107             sys.exit(1)