accept individual plc_config options
[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
7 class plc_configure(Test):
8     """
9     Configure the myplc from config options in config file
10     """
11
12     def call(self, plc_config_option=None, plc_config_value=None):
13         
14         services = ['API', 'DB', 'WWW', 'BOOT']
15         plc_options = [] 
16         # Get plc configuration variables
17         if plc_config_option is not None and \
18            plc_config_value  is not None:
19             # Set option passed in from user
20             plc_options.append((plc_config_option, plc_config_value))
21         else:
22             # Use hostname and ip of host we are running on
23             for service in services:
24                 host_option = 'PLC_%(service)s_HOST' % locals()
25                 ip_option = 'PLC_%(service)s_IP' % locals() 
26                 plc_options.append((host_option, self.config.hostname))
27                 plc_options.append((ip_option, self.config.ip)) 
28             # Load any other options found in config file
29             for attr in dir(self.config):
30                 if attr.startswith('PLC'):
31                     plc_options.append((attr, getattr(self.config, attr)))
32                 
33         # Write temporary plc-config file
34         tmpname = '/tmp/plc-config-tty-%d' % os.getpid()
35         fileconf = open(tmpname, 'w')
36         for (option, value) in plc_options:
37             fileconf.write('e %s\n%s\n' % (option, value) )
38         fileconf.write('w\nq\n')
39         fileconf.close()
40
41         # Update plc config file
42         command = "plc-config-tty < %(tmpname)s" % locals()
43         if self.config.verbose:
44             utils.header(command)               
45         (stdout, stderr) = utils.popen(command)
46         (stdout, stderr) = utils.popen("rm %s" % tmpname)
47
48         return 1
49
50 if __name__ == '__main__':
51     args = tuple(sys.argv[1:])
52     plc_configure()(*args)