7 from qa.PLCs import PLC, PLCs
9 class plc_configure(Test):
11 Configure the myplc from config options in config file
14 def call(self, plc_name, plc_config_options = None):
16 # Get plc configuration from config
17 plc = self.config.get_plc(plc_name)
18 services = ['API', 'DB', 'WWW', 'BOOT']
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)
27 command = "/sbin/service plc mount"
28 if self.config.verbose: utils.header(command, logfile = self.config.logfile)
29 (status, output) = plc.commands(command)
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))
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)))
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')
57 #plc.scp(tmpfname, "%s:/usr/tmp" % (plc['host']))
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)
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)
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)
76 if __name__ == '__main__':
77 args = tuple(sys.argv[1:])
78 plc_configure()(*args)