392e9a70f935061c57c85490be5596b47050cb01
[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
8 class plc_configure(Test):
9     """
10     Configure the myplc from config options in config file
11     """
12
13     def call(self, plc_config_option=None, plc_config_value=None):
14         
15         services = ['API', 'DB', 'WWW', 'BOOT']
16         plc_options = [] 
17         # Turn off plc (for good measure)
18         command = "/sbin/service plc stop"
19         if self.config.verbose: utils.header(command)
20         (stdout, stderr) = utils.popen(command)
21
22         # mount plc (need to do this optionally, as we do not want this for myplc-native)
23         command = "/sbin/service plc mount"
24         if self.config.verbose: utils.header(command)
25         (stdout, stderr) = utils.popen(command)
26
27         # Get plc configuration variables
28         if plc_config_option is not None and \
29            plc_config_value  is not None:
30             # Set option passed in from user
31             plc_options.append((plc_config_option, plc_config_value))
32         else:
33             # Use hostname and ip of host we are running on
34             for service in services:
35                 host_option = 'PLC_%(service)s_HOST' % locals()
36                 ip_option = 'PLC_%(service)s_IP' % locals() 
37                 plc_options.append((host_option, self.config.hostname))
38                 plc_options.append((ip_option, self.config.ip)) 
39             # Load any other options found in config file
40             for attr in dir(self.config):
41                 if attr.startswith('PLC'):
42                     plc_options.append((attr, getattr(self.config, attr)))
43                 
44         # Write temporary plc-config file
45         tmpfconf, tmpfname = tempfile.mkstemp(".config","plc-config-tty")
46         if self.config.verbose:
47             utils.header("generate temporary config file %(tmpfname)s"%locals())
48         for (option, value) in plc_vars:
49             os.write(tmpfconf, 'e %s\n%s\n' % (optin, value))
50         os.write(tmpfconf,'w\nq\n')
51         os.close(tmpfconf)
52
53         # configure plc
54         command = "plc-config-tty < %(tmpfname)s" % locals()
55         if self.config.verbose: utils.header(command)
56         (stdout, stderr) = utils.popen(command)
57
58         # clean up temporary conf file
59         if self.config.verbose: utils.header("removing %(tmpfname)s"%locals())
60         os.unlink(tmpfname)
61
62         # umount plc (need to do this optionally, as we do not want this for myplc-native)
63         command = "/sbin/service plc umount"
64         if self.config.verbose: utils.header(command)
65         (stdout, stderr) = utils.popen(command)
66
67         return 1
68
69 if __name__ == '__main__':
70     args = tuple(sys.argv[1:])
71     plc_configure()(*args)