6 def get_path(self, user = 'root'):
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
20 def get_command(self, command):
22 if 'chroot' in self and self['chroot']:
23 command = " chroot %s %s" % (self['chroot'], command)
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)
31 def get_remote_command(self, command):
32 (command) = self.get_command(command)
34 if 'host' in self and self['host'] not in ['localhost', self.config.hostname, self.config.ip]:
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)
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)
46 def popen3(self, command):
47 command = self.get_remote_command(command)
48 return utils.popen3(command, self.config.verbose, self.logfile)
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)
54 def get_scp_command(self, localfile, remotefile, direction, recursive = False, user = 'root'):
56 options += " -o StrictHostKeyChecking=no "
59 if 'host_rootkey' in self and self['host_rootkey']:
60 options += ' -i %s ' % self['host_rootkey']
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()
70 raise Error, "Invalid direction, must be 'to' or 'from'."
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)
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)
81 def wget(self, url, targetdir, user = 'root'):
82 if self.config.verbose:
83 utils.header("Downloading %(url)s to %(targetdir)s" % locals())
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)
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)
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']
103 options += " -o StrictHostKeyChecking=no "
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)
114 def get_scp_command(self, localfile, remotefile, direction, recursive = False, user = 'root', key = None):
117 options += " -o StrictHostKeyChecking=no "
121 options += " -i %(key)s "% locals()
122 elif self['rootkey']:
123 options += " -i %s " % self['rootkey']
125 # Are we copying to a real node or a virtual node hosted
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()
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()
138 raise Error, "Invalid direction, must be 'to' or 'from'."
141 # Host remote commands
142 def host_popen(self, command, fatal = True, logfile = None):
144 logfile = self.logfile
145 command = self.get_host_command(command)
146 return utils.popen(command, fatal, self.config.verbose, logfile)
148 def host_popen3(self, command, logfile = None):
150 logfile = self.logfile
151 command = self.get_host_command(command)
152 return utils.popen3(command, self.config.verbose, logfile)
154 def host_commands(self, command, fatal = True, logfile = None):
156 logfiile = self.logfile
157 command = self.get_host_command(command)
158 return utils.commands(command, fatal, self.config.verbose, logfile)
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)
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)
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)
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)
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)
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)
195 command = self.get_scp_command(localfile, remotefile, 'to', recursive, user, key)
196 return utils.commands(command, logfile = self.logfile)
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)
210 command = self.get_scp_command(localfile, remotefile, 'from', recursive, user, key)
211 return utils.commands(command, logfile = self.logfile)