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 = " 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)
46 def popen3(self, command):
47 command = self.get_remote_command(command)
48 return utils.popen3(command, self.config.verbose)
50 def commands(self, command, fatal = True):
51 command = self.get_remote_command(command)
52 return utils.commands(command, fatal, self.config.verbose)
54 def scp(self, src, dest):
56 if 'rootkey' in self and self['rootkey'] is not None:
57 options += " -i %s " % (self['rootkey'])
59 if 'chroot' in self and self['chroot'] is not None:
61 if 'vserver' in self and self['vserver'] is not None:
62 path += '/vservers/%s/' % self['vserver']
65 src_parts = src.split(":")
67 dest_parts = dest.split(":")
69 if len(src_parts) == 1:
71 elif src_parts[0].find('localhost') != -1:
72 src_cmd = path + os.sep + src_parts[1]
74 host, file = src_parts[0], src_parts[1]
75 src_cmd = 'root@%(host)s:%(path)s%(file)s ' % locals()
77 if len(dest_parts) == 1:
79 elif dest_parts[0].find('localhost') != -1:
80 dest_cmd = path +os.sep+ dest_parts[1]
82 host, file = dest_parts[0], dest_parts[1]
83 dest_cmd = 'root@%(host)s:%(path)s%(file)s' % locals()
85 command = 'scp %(options)s %(src_cmd)s %(dest_cmd)s' % locals()
86 if self.config.verbose:
88 return utils.commands(command)
90 def get_scp_command(self, localfile, remotefile, direction, recursive = False, user = 'root'):
92 options += " -o StrictHostKeyChecking=no "
95 if 'host_rootkey' in self and self['host_rootkey']:
96 options += ' -i %s ' % self['host_rootkey']
100 path = self.get_path(user)
101 if direction in ['to']:
102 command = "scp %(options)s %(localfile)s %(path)s/%(remotefile)s" % locals()
103 elif direction in ['from']:
104 command = "scp %(options)s %(path)s/%(remotefile)s %(localfile)s" % locals()
106 raise Error, "Invalid direction, must be 'to' or 'from'."
109 def scp_to(self, localfile, remotefile, recursive = False):
110 command = self.get_scp_command(localfile, remotefile, 'to', recursive)
111 return utils.commands(command)
113 def scp_from(self, localfile, remotefile, recursive = False):
114 command = self.get_scp_command(localfile, remotefile, 'from', recursive)
115 return utils.commands(command)