-added methods to help scp files to / from Remotes
[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
8 class PLC(dict, Remote):
9     fields = {
10         'name': 'TestPLC',                              # PLC Name                      
11         'host': 'localhost',                            # Node Hostname
12         'ip':   '127.0.0.1',                            # IP
13         'chroot': None,                                 # Path to the chroot
14         'vserver': None,                                # Vserver where this PLC lives
15         'host_rootkey': None,                           # Root Key
16         'api_path': '/PLCAPI/',                         # PLCAPI path 
17         'port': '443'                                   # PLCAPI port
18         
19         }
20
21     def __init__(self, config, fields = {}):
22         # XX Filter out fields not specified in fields
23         dict.__init__(self, self.fields)
24         
25         # Merge defined fields with defaults
26         self.update(fields)
27         
28         # init config
29         self.config = config
30
31
32     def update_ip(self):
33         try:    
34             command = "/sbin/ifconfig eth0 | grep -v inet6 | grep inet | awk '{print$2;}'"
35             (status, output) = self.commands(command)
36             ip = re.findall(r'[0-9\.]+', output)[0]     
37         except:
38             ip = "127.0.0.1"
39         self['ip'] = ip.strip() 
40         return self['ip']       
41
42     def update_api(self):
43         # Set up API acccess
44         # If plc is specified, find its configuration
45         # and use its API
46         self.update_ip()
47         name, ip, port, path = self['name'], self['ip'], self['port'], self['api_path']
48         if self.config.verbose:
49             utils.header("Updating %(name)s's api to https://%(ip)s:%(port)s/%(path)s" % locals())   
50         api_server = "https://%(ip)s:%(port)s/%(path)s" % locals()
51         self.config.api = xmlrpclib.Server(api_server, allow_none = 1)
52         self.config.api_type = 'xmlrpc' 
53
54 class PLCs(list, Table):
55
56     def __init__(self, config, plcs):
57         plclist = [PLC(config, plc) for plc in plcs]
58         list.__init__(self, plclist)