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