(no commit message)
[tests.git] / qaapi / qa / tests / plc_configure.py
index 4cc1395..6e4e177 100644 (file)
@@ -1,48 +1,75 @@
-
+#!/usr/bin/python
 import os, sys
 import traceback
 from Test import Test
 from qa import utils
+import tempfile
+from qa.PLCs import PLC, PLCs
 
 class plc_configure(Test):
     """
     Configure the myplc from config options in config file
     """
 
-    def call(self, system_type, root_dir):
-       tmpname = '/tmp/plc-cinfig-tty-%d' % os.getpid()
-       fileconf = open(tmpname, 'w')
-       for var in [ 'PLC_NAME',
-                     'PLC_ROOT_PASSWORD',
-                     'PLC_ROOT_USER',
-                     'PLC_MAIL_ENABLED',
-                     'PLC_MAIL_SUPPORT_ADDRESS',
-                     'PLC_DB_HOST',
-                     'PLC_API_HOST',
-                     'PLC_WWW_HOST',
-                     'PLC_BOOT_HOST',
-                     'PLC_NET_DNS1',
-                     'PLC_NET_DNS2']:
-           fileconf.write('e %s\n%s\n' % (var, getattr(self.config, var)))
-       fileconf.write('w\nq\n')
-       fileconf.close()
-
-       mount_command = "/sbin/service plc mount"
-       full_command = ""
-       if system_type in ['vserv', 'vserver']:
-           full_command += " vserver %(root_dir)s exec " % locals()
-       elif system_type in ['chroot']:
-           full_command += " chroot %(root_dir)s " % locals()
+    def call(self, plc_name, plc_config_options = None):
+       
+       # Get plc configuration from config
+       plc = self.config.get_plc(plc_name)
+       services = ['API', 'DB', 'WWW', 'BOOT']
+       plc_options = [] 
+        
+       # Turn off plc (for good measure)
+       command = "/sbin/service plc stop"
+       if self.config.verbose: utils.header(command, logfile = self.config.logfile)
+        (status, output) = plc.commands(command)
+
+       # mount plc 
+       command = "/sbin/service plc mount"
+       if self.config.verbose: utils.header(command, logfile = self.config.logfile)
+        (status, output) = plc.commands(command)
+
+       # Get plc configuration variables
+       if plc_config_options is not None:
+           for (option, value) in plc_config_options.items():
+               plc_options.append((option, value))
        else:
-           raise Exception, "Invalid system type %(sytem_type)s" % locals()
-
-       full_command += " plc-config-tty < %(tmpname)s" % locals()
-       commands = [mount_command, full_command]
-       for command in commands:
-           if self.config.verbose:
-               utils.header(command)           
-            (stdout, stderr) = utils.popen(command)
-        (stdout, stderr) = utils.popen("rm %s" % tmpname)
+           # Use hostname and ip of host we are running on
+           for service in services:
+               host_option = 'PLC_%(service)s_HOST' % locals()
+               ip_option = 'PLC_%(service)s_IP' % locals() 
+               plc_options.append((host_option, plc['host']))
+               plc_options.append((ip_option, plc['ip']))      
+           # Load any other options found in config file
+           for attr in dir(self.config):
+               if attr.startswith('PLC'):
+                   plc_options.append((attr, getattr(self.config, attr)))
+               
+       # Write temporary plc-config file
+       # XX use plc instance to copy file
+        tmpfconf, tmpfname = tempfile.mkstemp(".config","plc-config-tty", '/usr/tmp/')
+       tmpfname_parts = tmpfname.split(os.sep)
+       if self.config.verbose:
+            utils.header("generating temporary config file %(tmpfname)s"%locals(), logfile = self.config.logfile)
+       for (option, value) in plc_options:
+            os.write(tmpfconf, 'e %s\n%s\n' % (option, value))
+        os.write(tmpfconf,'w\nq\n')
+       os.close(tmpfconf)
+       #plc.scp(tmpfname, "%s:/usr/tmp" % (plc['host']))
+
+        # configure plc
+       command = "plc-config-tty < %(tmpfname)s" % locals()
+       if self.config.verbose: utils.header(command, logfile = self.config.logfile)
+        (status, output) = plc.commands(command)
+
+       # clean up temporary conf file
+       # XX use plc instance to copy file
+       if self.config.verbose: utils.header("removing %(tmpfname)s"%locals(), logfile = self.config.logfile)
+        os.unlink(tmpfname)
+
+       # umount plc (need to do this optionally, as we do not want this for myplc-native)
+       command = "/sbin/service plc umount"
+       if self.config.verbose: utils.header(command, logfile = self.config.logfile)
+        (status, output) = plc.commands(command)
 
        return 1