added 'host_rootkey' to fields
[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         # Chroot if necessary 
22         if 'chroot' in self and self['chroot']:
23             command = " chroot %s %s" % (self['chroot'], command)
24
25         # Execute vserver exec if necessary
26         if 'vserver' in self and self['vserver']:
27             command = " vserver %s exec %s " % (self['vserver'], command)
28         
29         return command
30
31     def get_remote_command(self, command):
32         (command) = self.get_command(command)
33
34         if 'host' in self and self['host'] not in ['localhost', self.config.hostname, self.config.ip]:
35             options = " -q "
36             options += " -o StrictHostKeyChecking=no "
37             if 'host_rootkey' in self and self['host_rootkey']:
38                 options +=  " -i %s " % self['host_rootkey']
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