default some value types
[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):
143         command = self.get_host_command(command)
144         return utils.popen(command, fatal, self.config.verbose, self.logfile)
145
146     def host_popen3(self, command):
147         command = self.get_host_command(command)
148         return utils.popen3(command, self.config.verbose, self.logfile)
149
150     def host_commands(self, command, fatal = True):
151         command = self.get_host_command(command)
152         return utils.commands(command, fatal, self.config.verbose, self.logfile)
153
154     # Slice remote commands
155     def slice_popen(self, command, user = 'root', key = None, fatal = True):
156         command = self.get_remote_command(command, user, key)
157         return utils.popen(command, fatal, logfile = self.logfile)
158
159     def slice_popen3(self, command, user = 'root', key = None, fatal = True):
160         command = self.get_remote_command(command, user, key)
161         return utils.popen3(command, fatal, self.logfile)
162
163     def slice_commands(self, command, user = 'root', key = None, fatal = True):
164         command = self.get_remote_command(command, user, key)
165         return utils.commands(command, fatal, logfile = self.logfile)
166
167     # Host scp 
168     def scp_to_host(self, localfile, remotefile, recursive = False):
169         command = self.get_host_scp_command(localfile, remotefile, 'to', recursive)
170         return utils.commands(command, logfile = self.logfile)
171
172     def scp_from_host(self, remotefile, localfile, recursive = False):
173         command = self.get_host_scp_command(localfile, remotefile, 'from', recursive)
174         return utils.commands(command, logfile = self.logfile)
175
176     # Node scp
177     def scp_to(self, localfile, remotefile, recursive = False, user = 'root', key = None):
178         # if node is vm, we must scp file(s) to host machine first
179         # then run scp from there
180         if 'type' in self and self['type'] in ['vm']:
181             fileparts = localfile.split(os.sep)
182             filename = fileparts[-1:][0]
183             tempfile = '/var/tmp/%(filename)s' % locals()
184             self.scp_to_host(localfile, tempfile, recursive)
185             command = self.get_scp_command(tempfile, remotefile, 'to', recursive, user, key)
186             return self.host_commands(command, logfile = self.logfile)
187         else:
188
189             command = self.get_scp_command(localfile, remotefile, 'to', recursive, user, key)
190             return utils.commands(command, logfile = self.logfile)
191
192     def scp_from(self, remotefile, localfile, recursive = False, user = 'root', key = None):
193         # if node is vm, we must scp file(s) onto host machine first
194         # then run scp from there
195         if 'type' in self and self['type'] in ['vm']:
196             fileparts = remotefile.split(os.sep)
197             filename = fileparts[-1:][0]
198             tempfile = '/var/tmp/%(filename)s' % locals()
199             command = self.get_scp_command(tempfile, remotefile, 'from', recursive, user, key)
200             self.host_commands(command, logfile = self.logfile)
201             return self.scp_from_host(tempfile, localfile, recursive)
202         else:
203
204             command = self.get_scp_command(localfile, remotefile, 'from', recursive, user, key)
205             return utils.commands(command, logfile = self.logfile)
206