d625720c4151b8eb10bf5c08aa748ce4217ac52b
[tests.git] / qaapi / qa / tests / plc_configure.py
1 #!/usr/bin/python
2 import os, sys
3 import traceback
4 from Test import Test
5 from qa import utils
6 import tempfile
7 from qa.PLCs import PLC, PLCs
8
9 class plc_configure(Test):
10     """
11     Configure the myplc from config options in config file
12     """
13
14     def call(self, plc_name, plc_config_option=None, plc_config_value=None):
15         
16         # Get plc configuration from config
17         plc = self.config.get_plc(plc_name)
18         services = ['API', 'DB', 'WWW', 'BOOT']
19         plc_options = [] 
20         
21         # Turn off plc (for good measure)
22         command = "/sbin/service plc stop"
23         if self.config.verbose: utils.header(command)
24         (status, output) = plc.commands(command)
25
26         # mount plc (need to do this optionally, as we do not want this for myplc-native)
27         command = "/sbin/service plc mount"
28         if self.config.verbose: utils.header(command)
29         (status, output) = plc.commands(command)
30
31         # Get plc configuration variables
32         if plc_config_option is not None and \
33            plc_config_value  is not None:
34             # Set option passed in from user
35             plc_options.append((plc_config_option, plc_config_value))
36         else:
37             # Use hostname and ip of host we are running on
38             for service in services:
39                 host_option = 'PLC_%(service)s_HOST' % locals()
40                 ip_option = 'PLC_%(service)s_IP' % locals() 
41                 plc_options.append((host_option, plc['host']))
42                 plc_options.append((ip_option, plc['ip']))      
43             # Load any other options found in config file
44             for attr in dir(self.config):
45                 if attr.startswith('PLC'):
46                     plc_options.append((attr, getattr(self.config, attr)))
47                 
48         # Write temporary plc-config file
49         # XX use plc instance to copy file
50         tmpfconf, tmpfname = tempfile.mkstemp(".config","plc-config-tty", '/usr/tmp/')
51         tmpfname_parts = tmpfname.split(os.sep)
52         if self.config.verbose:
53             utils.header("generating temporary config file %(tmpfname)s"%locals())
54         for (option, value) in plc_options:
55             os.write(tmpfconf, 'e %s\n%s\n' % (option, value))
56         os.write(tmpfconf,'w\nq\n')
57         os.close(tmpfconf)
58         #plc.scp(tmpfname, "%s:/usr/tmp" % (plc['host']))
59
60         # configure plc
61         command = "plc-config-tty < %(tmpfname)s" % locals()
62         if self.config.verbose: utils.header(command)
63         (status, output) = plc.commands(command)
64
65         # clean up temporary conf file
66         # XX use plc instance to copy file
67         if self.config.verbose: utils.header("removing %(tmpfname)s"%locals())
68         os.unlink(tmpfname)
69
70         # umount plc (need to do this optionally, as we do not want this for myplc-native)
71         command = "/sbin/service plc umount"
72         if self.config.verbose: utils.header(command)
73         (status, output) = plc.commands(command)
74
75         return 1
76
77 if __name__ == '__main__':
78     args = tuple(sys.argv[1:])
79     plc_configure()(*args)