nicer
[tests.git] / qaapi / qa / Remote.py
1 import utils
2 import os
3
4 class Remote:
5
6     def get_path(self, user = 'root'):
7         path = ""
8         if 'host' in self and self['host']:
9             if self['host'] not in ['localhost', self.config.hostname, None]:  
10                 path += "%s@%s:" % (user, self['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 = "/usr/sbin/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 = ""
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, self.logfile)
45
46     def popen3(self, command):
47         command = self.get_remote_command(command)
48         return utils.popen3(command, self.config.verbose, self.logfile) 
49
50     def commands(self, command, fatal = True):
51         command = self.get_remote_command(command) 
52         return utils.commands(command, fatal, self.config.verbose, self.logfile)
53
54     def get_scp_command(self, localfile, remotefile, direction, recursive = False, user = 'root'):
55         options = " -q "
56         options += " -o StrictHostKeyChecking=no "
57         path = ""
58         host = self['host']
59         if 'host_rootkey' in self and self['host_rootkey']: 
60             options += ' -i %s ' % self['host_rootkey']
61         if recursive:
62             options += ' -r '
63  
64         path = self.get_path(user)
65         if direction in ['to']:
66             command = "scp %(options)s %(localfile)s %(path)s/%(remotefile)s" % locals()
67         elif direction in ['from']:
68             command = "scp %(options)s %(path)s/%(remotefile)s %(localfile)s" % locals()
69         else:
70             raise Error, "Invalid direction, must be 'to' or 'from'."  
71         return command
72  
73     def scp_to(self, localfile, remotefile, recursive = False):
74         command = self.get_scp_command(localfile, remotefile, 'to', recursive)
75         return utils.commands(command, logfile =  self.logfile)
76
77     def scp_from(self, localfile, remotefile, recursive = False):
78         command  = self.get_scp_command(localfile, remotefile, 'from', recursive)
79         return utils.commands(command, logfile = self.logfile)
80
81     def wget(self, url, targetdir, user = 'root'):
82         if self.config.verbose:
83             utils.header("Downloading %(url)s to %(targetdir)s" % locals())
84
85         cmd_prefix = ""
86         if user not in ['root']:
87             cmd_prefix = " su - user -c "
88         fileparts = url.split(os.sep)
89         filename = fileparts[-1:][0]
90         cleanup_cmd = "%(cmd_prefix)s rm -f %(targetdir)s/%(filename)s" % locals()
91         print >> self.logfile, cleanup_cmd
92         self.commands(cleanup_cmd, False)
93
94         wget_cmd = "%(cmd_prefix)s wget -nH -P %(targetdir)s %(url)s" % locals()
95         print >> self.logfile, wget_cmd
96         self.commands(wget_cmd)
97
98 class VRemote(Remote):
99     def get_remote_command(self, command, user = 'root', key = None):
100         if key is None and 'rootkey' in self:
101             key = self['rootkey']
102         options = ""
103         options += " -o StrictHostKeyChecking=no "
104         if key:
105             options += " -i %(key)s" % locals()
106         host = self['hostname']
107         if 'type' in self and self['type'] in ['vm']:
108             if 'redir_ssh_port' in self and self['redir_ssh_port']:
109                 options += " -p %s " % self['redir_ssh_port']
110             host = self.get_host_ip()
111         command = "ssh %(options)s %(user)s@%(host)s \'%(command)s\'" % locals()
112         return self.get_host_command(command)
113
114     def get_scp_command(self, localfile, remotefile, direction, recursive = False, user = 'root', key = None):
115         # scp options
116         options = ""
117         options += " -o StrictHostKeyChecking=no "
118         if recursive:
119             options += " -r "
120         if key:
121             options += " -i %(key)s "% locals()
122         elif self['rootkey']:
123             options += " -i %s " % self['rootkey']
124
125         # Are we copying to a real node or a virtual node hosted
126         # at another machine 
127         host = self['hostname']
128         if 'type' in self and self['type'] in ['vm']:
129             if 'redir_ssh_port' in self and self['redir_ssh_port']:
130                 options += " -P %s " % self['redir_ssh_port']
131             host = self.get_host_ip()
132
133         if direction in ['to']:
134             command = "scp %(options)s %(localfile)s %(user)s@%(host)s:/%(remotefile)s" % locals()
135         elif direction in ['from']:
136             command = "scp %(options)s %(user)s@%(host)s:/%(remotefile)s %(localfile)s" % locals()
137         else:
138             raise Error, "Invalid direction, must be 'to' or 'from'."
139         return command
140
141     # Host remote commands
142     def host_popen(self, command, fatal = True, logfile = None):
143         if logfile is None:
144             logfile = self.logfile
145         command = self.get_host_command(command)
146         return utils.popen(command, fatal, self.config.verbose, logfile)
147
148     def host_popen3(self, command, logfile = None):
149         if logfile is None:
150             logfile = self.logfile
151         command = self.get_host_command(command)
152         return utils.popen3(command, self.config.verbose, logfile)
153
154     def host_commands(self, command, fatal = True, logfile = None):
155         if logfile is None:
156             logfiile = self.logfile
157         command = self.get_host_command(command)
158         return utils.commands(command, fatal, self.config.verbose, logfile)
159
160     # Slice remote commands
161     def slice_popen(self, command, user = 'root', key = None, fatal = True):
162         command = self.get_remote_command(command, user, key)
163         return utils.popen(command, fatal, logfile = self.logfile)
164
165     def slice_popen3(self, command, user = 'root', key = None, fatal = True):
166         command = self.get_remote_command(command, user, key)
167         return utils.popen3(command, fatal, self.logfile)
168
169     def slice_commands(self, command, user = 'root', key = None, fatal = True):
170         command = self.get_remote_command(command, user, key)
171         return utils.commands(command, fatal, logfile = self.logfile)
172
173     # Host scp 
174     def scp_to_host(self, localfile, remotefile, recursive = False):
175         command = self.get_host_scp_command(localfile, remotefile, 'to', recursive)
176         return utils.commands(command, logfile = self.logfile)
177
178     def scp_from_host(self, remotefile, localfile, recursive = False):
179         command = self.get_host_scp_command(localfile, remotefile, 'from', recursive)
180         return utils.commands(command, logfile = self.logfile)
181
182     # Node scp
183     def scp_to(self, localfile, remotefile, recursive = False, user = 'root', key = None):
184         # if node is vm, we must scp file(s) to host machine first
185         # then run scp from there
186         if 'type' in self and self['type'] in ['vm']:
187             fileparts = localfile.split(os.sep)
188             filename = fileparts[-1:][0]
189             tempfile = '/var/tmp/%(filename)s' % locals()
190             self.scp_to_host(localfile, tempfile, recursive)
191             command = self.get_scp_command(tempfile, remotefile, 'to', recursive, user, key)
192             return self.host_commands(command, logfile = self.logfile)
193         else:
194
195             command = self.get_scp_command(localfile, remotefile, 'to', recursive, user, key)
196             return utils.commands(command, logfile = self.logfile)
197
198     def scp_from(self, remotefile, localfile, recursive = False, user = 'root', key = None):
199         # if node is vm, we must scp file(s) onto host machine first
200         # then run scp from there
201         if 'type' in self and self['type'] in ['vm']:
202             fileparts = remotefile.split(os.sep)
203             filename = fileparts[-1:][0]
204             tempfile = '/var/tmp/%(filename)s' % locals()
205             command = self.get_scp_command(tempfile, remotefile, 'from', recursive, user, key)
206             self.host_commands(command, logfile = self.logfile)
207             return self.scp_from_host(tempfile, localfile, recursive)
208         else:
209
210             command = self.get_scp_command(localfile, remotefile, 'from', recursive, user, key)
211             return utils.commands(command, logfile = self.logfile)
212