-added get_path() method
[tests.git] / qaapi / qa / Remote.py
1 import utils
2 import os
3
4 class Remote:
5
6     def get_path(self):
7         path = ""
8         if 'host' in self and self['host']:
9             if self['host'] not in ['localhost', self.config.hostname, None]:  
10                 path += "root@host:" 
11         if 'vserver' in self and self['vserver']:
12             path += '/vservers/%s/' % self['vserver'] 
13         if 'chroot' in self and self['chroot']:
14             path += self['chroot'] + os.sep
15         if 'homedir' in self and self['homedir']:
16             path += self['homedir'] + os.sep
17         
18         return path  
19
20     def get_command(self, command):
21         options = " -o StrictHostKeyChecking=no "
22         # Chroot if necessary 
23         if 'chroot' in self and self['chroot']:
24             command = " chroot %s %s" % (self['chroot'], command)
25
26         # Execute vserver exec if necessary
27         if 'vserver' in self and self['vserver']:
28             command = " vserver %s exec %s " % (self['vserver'], command)
29         
30         # Use root key if necessary     
31         if 'host' in self and self['host'] not in ['localhost', self.config.hostname]:
32             if 'rootkey' in self and self['rootkey']:
33                 options +=  " -i %s " % self['rootkey']
34
35         return (command, options)
36
37     def get_host_command(self, command):
38         (command, options) = self.get_command(command)
39         if 'host' in self and self['host'] not in ['localhost', self.config.hostname]:
40             command = "ssh %s root@%s \"%s\" " % (options, self['host'], command)
41         return command
42
43     def get_remote_command(self, command):
44         (command, options) = self.get_command(command)
45         if 'type' in self and self['type'] in ['vm']:
46             if 'redir_port' in self and self['redir_port']:
47                 options += " -p %s " % self['redir_port']       
48         
49         # attempt ssh self['host'] is not the machine we are running on or
50         # if this is a virtual node 
51         if 'host' in self and self['host'] not in ['localhost', self.config.hostname] or \
52            'type' in self and self['type'] in ['vm']:
53             command = "ssh %s root@%s \"%s\" " % (options, self['host'], command)
54         return command 
55
56     def popen(self, command, fatal = True):
57         command = self.get_remote_command(command)
58         return utils.popen(command, fatal, self.config.verbose)
59
60     def popen3(self, command):
61         command = self.get_remote_command(command)
62         return utils.popen3(command, self.config.verbose) 
63
64     def commands(self, command, fatal = True):
65         command = self.get_remote_command(command) 
66         return utils.commands(command, fatal, self.config.verbose)
67
68     def scp(self, src, dest):
69         options = "" 
70         if 'rootkey' in self and self['rootkey'] is not None:
71              options += " -i %s " % (self['rootkey']) 
72         path = ""
73         if 'chroot' in self and self['chroot'] is not None:
74             path += "/plc/root/"
75         if 'vserver' in self and self['vserver'] is not None:
76             path += '/vservers/%s/' % self['vserver']           
77
78         src_cmd = ""
79         src_parts = src.split(":")
80         dest_cmd = ""
81         dest_parts = dest.split(":")
82         command = "scp "
83         if len(src_parts) == 1:
84             src_cmd = src
85         elif src_parts[0].find('localhost')  != -1: 
86             src_cmd = path + os.sep + src_parts[1]
87         else:
88             host, file  = src_parts[0], src_parts[1]
89             src_cmd = 'root@%(host)s:%(path)s%(file)s ' % locals()
90
91         if len(dest_parts) == 1:
92             dest_cmd = dest
93         elif dest_parts[0].find('localhost') != -1:
94             dest_cmd = path +os.sep+ dest_parts[1]
95         else:
96             host, file = dest_parts[0], dest_parts[1]
97             dest_cmd = 'root@%(host)s:%(path)s%(file)s'  % locals()
98
99         command = 'scp %(options)s %(src_cmd)s %(dest_cmd)s' % locals()
100         if self.config.verbose:
101             utils.header(command)
102         return utils.commands(command)      
103