updated logging
[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_options = 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, logfile = self.config.logfile)
24         (status, output) = plc.commands(command)
25
26         # mount plc 
27         command = "/sbin/service plc mount"
28         if self.config.verbose: utils.header(command, logfile = self.config.logfile)
29         (status, output) = plc.commands(command)
30
31         # Get plc configuration variables
32         if plc_config_options is not None:
33             for (option, value) in plc_config_options.items():
34                 plc_options.append((option, value))
35         else:
36             # Use hostname and ip of host we are running on
37             for service in services:
38                 host_option = 'PLC_%(service)s_HOST' % locals()
39                 ip_option = 'PLC_%(service)s_IP' % locals() 
40                 plc_options.append((host_option, plc['host']))
41                 plc_options.append((ip_option, plc['ip']))      
42             # Load any other options found in config file
43             for attr in dir(self.config):
44                 if attr.startswith('PLC'):
45                     plc_options.append((attr, getattr(self.config, attr)))
46                 
47         # Write temporary plc-config file
48         # XX use plc instance to copy file
49         tmpfconf, tmpfname = tempfile.mkstemp(".config","plc-config-tty", '/usr/tmp/')
50         tmpfname_parts = tmpfname.split(os.sep)
51         if self.config.verbose:
52             utils.header("generating temporary config file %(tmpfname)s"%locals(), logfile = self.config.logfile)
53         for (option, value) in plc_options:
54             os.write(tmpfconf, 'e %s\n%s\n' % (option, value))
55         os.write(tmpfconf,'w\nq\n')
56         os.close(tmpfconf)
57         #plc.scp(tmpfname, "%s:/usr/tmp" % (plc['host']))
58
59         # configure plc
60         command = "plc-config-tty < %(tmpfname)s" % locals()
61         if self.config.verbose: utils.header(command, logfile = self.config.logfile)
62         (status, output) = plc.commands(command)
63
64         # clean up temporary conf file
65         # XX use plc instance to copy file
66         if self.config.verbose: utils.header("removing %(tmpfname)s"%locals(), logfile = self.config.logfile)
67         os.unlink(tmpfname)
68
69         # umount plc (need to do this optionally, as we do not want this for myplc-native)
70         command = "/sbin/service plc umount"
71         if self.config.verbose: utils.header(command, logfile = self.config.logfile)
72         (status, output) = plc.commands(command)
73
74         return 1
75
76 if __name__ == '__main__':
77     args = tuple(sys.argv[1:])
78     plc_configure()(*args)