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