add date to log's filename by default
[tests.git] / qaapi / qa / Nodes.py
1 import utils
2 import os
3 import re
4 from Remote import Remote
5 from Table import Table
6
7
8 class Node(dict, Remote):
9
10     fields = {
11         'plcs': ['TestPLC'],
12         'hostname': None,               # Node Hostname
13         'host': 'localhost',            # host where node lives
14         'redir_ssh_port': '51022',      # Port on host where ssh is redirected to virtual node
15         'vserver': None,                # vserver where this node lives
16         'type': 'vm',                   # type of node
17         'model': '/minhw',
18         'nodenetworks': [],             # node networks
19         'homedir': '/var/VirtualMachines/',
20         'rootkey': None,                 # path to root ssh key
21         'host_rootkey': None
22         }
23
24     def __init__(self, config, fields = {}):
25
26         # XX Filter out fields not specified in fields
27         dict.__init__(self, self.fields)
28         
29         # Merge defined fields with defaults
30         self.update(fields)
31         self.config = config
32
33     get_host_command = Remote.get_remote_command
34
35     def get_remote_command(self, command, user = 'root', key = None):
36         if key is None:
37             key = self['rootkey']
38         options = " -q "
39         options += " -o StrictHostKeyChecking=no "
40         options += " -i %(key)s" % locals() 
41         host = self['hostname']
42         if 'type' in self and self['type'] in ['vm']:
43             if 'redir_ssh_port' in self and self['redir_ssh_port']:
44                 options += " -p %s " % self['redir_ssh_port']
45             ip_command = "/sbin/ifconfig eth0 | grep -v inet6 | grep inet | awk '{print$2;}'"
46             (status, output) = self.host_commands(ip_command)
47             host = re.findall(r'[0-9\.]+', output)[0]
48              
49         command = "ssh %(options)s %(user)s@%(host)s \'%(command)s\'" % locals()
50         return self.get_host_command(command)
51
52     
53     def host_popen(self, command, fatal = True):
54         command = self.get_host_command(command)
55         return utils.popen(command, fatal, self.config.verbose)
56
57     def host_popen3(self, command):
58         command = self.get_host_command(command)
59         return utils.popen3(command, self.config.verbose)
60
61     def host_commands(self, command, fatal = True):
62         command = self.get_host_command(command)
63         return utils.commands(command, fatal, self.config.verbose)    
64
65          
66 class Nodes(list, Table):
67
68     def __init__(self, config, nodes):
69         nodelist = [Node(config, node) for node in nodes]
70         list.__init__(self, nodelist)
71         self.config = config
72