no more lxc-enter-namespace - use ssh
[tests.git] / qaapi / qa / PLCs.py
1 import os
2 import re
3 import utils
4 import xmlrpclib        
5 from Remote import Remote
6 from Table import Table
7 from logger import Logfile
8
9 class PLC(dict, Remote):
10     fields = {
11         'name': 'TestPLC',                              # PLC Name                      
12         'host': 'localhost',                            # Node Hostname
13         'ip':   '127.0.0.1',                            # IP
14         'chroot': None,                                 # Path to the chroot
15         'vserver': None,                                # Vserver where this PLC lives
16         'host_rootkey': None,                           # Root Key
17         'api_path': '/PLCAPI/',                         # PLCAPI path 
18         'port': '443'                                   # PLCAPI port
19         
20         }
21
22     def __init__(self, config, fields = {}):
23         # XX Filter out fields not specified in fields
24         dict.__init__(self, self.fields)
25         
26         # Merge defined fields with defaults
27         self.update(fields)
28         
29         # init config
30         self.config = config
31         self.__init_logfile__()
32
33     def __init_logfile__(self, filename = None):
34         if not filename:
35             filename = '%s/qaapi.log' % (self.config.logdir)
36         self.logfile = Logfile(filename)
37
38     def update_ip(self):
39         try:    
40             command = "/sbin/ifconfig eth0 | grep -v inet6 | grep inet | awk '{print$2;}'"
41             (status, output) = self.commands(command)
42             ip = re.findall(r'[0-9\.]+', output)[0]     
43         except:
44             ip = "127.0.0.1"
45         self['ip'] = ip.strip() 
46         return self['ip']       
47
48     def update_api(self):
49         # Set up API acccess
50         # If plc is specified, find its configuration
51         # and use its API
52         self.update_ip()
53         name, ip, port, path = self['name'], self['ip'], self['port'], self['api_path']
54         if self.config.verbose:
55             utils.header("Updating %(name)s's api to https://%(ip)s:%(port)s/%(path)s" % locals(), logfile = self.config.logfile)   
56         api_server = "https://%(ip)s:%(port)s/%(path)s" % locals()
57         self.config.api = xmlrpclib.Server(api_server, allow_none = 1)
58         self.config.api_type = 'xmlrpc' 
59
60     def update_node_images(self):
61         pass
62
63     def scp_to_webroot(self, localfiles, recursive = False):
64         if self.config.verbose:
65             utils.header("Copying %s to %s webroot" % (localfiles, self['name']), logfile = self.config.logfile)
66         self.scp_to("%(localfiles)s" % locals(), "/var/www/html/")
67         url = 'http://%s/%s' % (self['ip'], localfiles) 
68         
69         return url 
70
71 class PLCs(Table):
72
73     def __init__(self, config, plcs):
74         plclist = [PLC(config, plc) for plc in plcs]
75         Table.__init__(self, plclist)