Revision c92b310a
b/lib/backend.py | ||
---|---|---|
42 | 42 |
from ganeti import ssconf |
43 | 43 |
|
44 | 44 |
|
45 |
def _GetSshRunner(): |
|
46 |
return ssh.SshRunner() |
|
47 |
|
|
48 |
|
|
45 | 49 |
def StartMaster(): |
46 | 50 |
"""Activate local node as master node. |
47 | 51 |
|
... | ... | |
197 | 201 |
if 'nodelist' in what: |
198 | 202 |
result['nodelist'] = {} |
199 | 203 |
for node in what['nodelist']: |
200 |
success, message = ssh.VerifyNodeHostname(node)
|
|
204 |
success, message = _GetSshRunner().VerifyNodeHostname(node)
|
|
201 | 205 |
if not success: |
202 | 206 |
result['nodelist'][node] = message |
203 | 207 |
return result |
... | ... | |
1188 | 1192 |
|
1189 | 1193 |
destcmd = utils.BuildShellCmd("mkdir -p %s && cat > %s/%s", |
1190 | 1194 |
destdir, destdir, destfile) |
1191 |
remotecmd = ssh.BuildSSHCmd(dest_node, constants.GANETI_RUNAS, destcmd) |
|
1192 |
|
|
1193 |
|
|
1195 |
remotecmd = _GetSshRunner().BuildCmd(dest_node, constants.GANETI_RUNAS, |
|
1196 |
destcmd) |
|
1194 | 1197 |
|
1195 | 1198 |
# all commands have been checked, so we're safe to combine them |
1196 | 1199 |
command = '|'.join([expcmd, comprcmd, utils.ShellQuoteArgs(remotecmd)]) |
... | ... | |
1331 | 1334 |
os.mkdir(constants.LOG_OS_DIR, 0750) |
1332 | 1335 |
|
1333 | 1336 |
destcmd = utils.BuildShellCmd('cat %s', src_image) |
1334 |
remotecmd = ssh.BuildSSHCmd(src_node, constants.GANETI_RUNAS, destcmd) |
|
1337 |
remotecmd = _GetSshRunner().BuildCmd(src_node, constants.GANETI_RUNAS, |
|
1338 |
destcmd) |
|
1335 | 1339 |
|
1336 | 1340 |
comprcmd = "gunzip" |
1337 | 1341 |
impcmd = utils.BuildShellCmd("(cd %s; %s -i %s -b %s -s %s &>%s)", |
b/lib/cmdlib.py | ||
---|---|---|
74 | 74 |
self.op = op |
75 | 75 |
self.cfg = cfg |
76 | 76 |
self.sstore = sstore |
77 |
self.__ssh = None |
|
78 |
|
|
77 | 79 |
for attr_name in self._OP_REQP: |
78 | 80 |
attr_val = getattr(op, attr_name, None) |
79 | 81 |
if attr_val is None: |
... | ... | |
89 | 91 |
raise errors.OpPrereqError("Commands must be run on the master" |
90 | 92 |
" node %s" % master) |
91 | 93 |
|
94 |
def __GetSSH(self): |
|
95 |
"""Returns the SshRunner object |
|
96 |
|
|
97 |
""" |
|
98 |
if not self.__ssh: |
|
99 |
self.__ssh = ssh.SshRunner() |
|
100 |
return self.__ssh |
|
101 |
|
|
102 |
ssh = property(fget=__GetSSH) |
|
103 |
|
|
92 | 104 |
def CheckPrereq(self): |
93 | 105 |
"""Check prerequisites for this LU. |
94 | 106 |
|
... | ... | |
1229 | 1241 |
|
1230 | 1242 |
rpc.call_node_leave_cluster(node.name) |
1231 | 1243 |
|
1232 |
ssh.SSHCall(node.name, 'root', "%s stop" % constants.NODE_INITD_SCRIPT)
|
|
1244 |
self.ssh.Run(node.name, 'root', "%s stop" % constants.NODE_INITD_SCRIPT)
|
|
1233 | 1245 |
|
1234 | 1246 |
logger.Info("Removing node %s from config" % node.name) |
1235 | 1247 |
|
... | ... | |
1539 | 1551 |
constants.SSL_CERT_FILE, gntpem, |
1540 | 1552 |
constants.NODE_INITD_SCRIPT)) |
1541 | 1553 |
|
1542 |
result = ssh.SSHCall(node, 'root', mycommand, batch=False, ask_key=True)
|
|
1554 |
result = self.ssh.Run(node, 'root', mycommand, batch=False, ask_key=True)
|
|
1543 | 1555 |
if result.failed: |
1544 | 1556 |
raise errors.OpExecError("Remote command on node %s, error: %s," |
1545 | 1557 |
" output: %s" % |
... | ... | |
1597 | 1609 |
" you gave (%s). Please fix and re-run this" |
1598 | 1610 |
" command." % new_node.secondary_ip) |
1599 | 1611 |
|
1600 |
success, msg = ssh.VerifyNodeHostname(node) |
|
1612 |
success, msg = self.ssh.VerifyNodeHostname(node)
|
|
1601 | 1613 |
if not success: |
1602 | 1614 |
raise errors.OpExecError("Node '%s' claims it has a different hostname" |
1603 | 1615 |
" than the one the resolver gives: %s." |
... | ... | |
1623 | 1635 |
if self.sstore.GetHypervisorType() == constants.HT_XEN_HVM31: |
1624 | 1636 |
to_copy.append(constants.VNC_PASSWORD_FILE) |
1625 | 1637 |
for fname in to_copy: |
1626 |
if not ssh.CopyFileToNode(node, fname): |
|
1638 |
if not self.ssh.CopyFileToNode(node, fname):
|
|
1627 | 1639 |
logger.Error("could not copy file %s to node %s" % (fname, node)) |
1628 | 1640 |
|
1629 | 1641 |
logger.Info("adding node %s to cluster.conf" % node) |
... | ... | |
1767 | 1779 |
for node in self.nodes: |
1768 | 1780 |
if node == myname: |
1769 | 1781 |
continue |
1770 |
if not ssh.CopyFileToNode(node, filename): |
|
1782 |
if not self.ssh.CopyFileToNode(node, filename):
|
|
1771 | 1783 |
logger.Error("Copy of file %s to node %s failed" % (filename, node)) |
1772 | 1784 |
|
1773 | 1785 |
|
... | ... | |
1810 | 1822 |
""" |
1811 | 1823 |
data = [] |
1812 | 1824 |
for node in self.nodes: |
1813 |
result = ssh.SSHCall(node, "root", self.op.command)
|
|
1825 |
result = self.ssh.Run(node, "root", self.op.command)
|
|
1814 | 1826 |
data.append((node, result.output, result.exit_code)) |
1815 | 1827 |
|
1816 | 1828 |
return data |
b/lib/ssh.py | ||
---|---|---|
32 | 32 |
from ganeti import constants |
33 | 33 |
|
34 | 34 |
|
35 |
__all__ = ["SSHCall", "CopyFileToNode", "VerifyNodeHostname", |
|
36 |
"KNOWN_HOSTS_OPTS", "BATCH_MODE_OPTS", "ASK_KEY_OPTS"] |
|
37 |
|
|
38 |
|
|
39 | 35 |
KNOWN_HOSTS_OPTS = [ |
40 | 36 |
"-oGlobalKnownHostsFile=%s" % constants.SSH_KNOWN_HOSTS_FILE, |
41 | 37 |
"-oUserKnownHostsFile=/dev/null", |
... | ... | |
90 | 86 |
for base in ["id_dsa", "id_dsa.pub", "authorized_keys"]] |
91 | 87 |
|
92 | 88 |
|
93 |
def BuildSSHCmd(hostname, user, command, batch=True, ask_key=False): |
|
94 |
"""Build an ssh string to execute a command on a remote node. |
|
95 |
|
|
96 |
Args: |
|
97 |
hostname: the target host, string |
|
98 |
user: user to auth as |
|
99 |
command: the command |
|
100 |
batch: if true, ssh will run in batch mode with no prompting |
|
101 |
ask_key: if true, ssh will run with StrictHostKeyChecking=ask, so that |
|
102 |
we can connect to an unknown host (not valid in batch mode) |
|
103 |
|
|
104 |
Returns: |
|
105 |
The ssh call to run 'command' on the remote host. |
|
106 |
|
|
107 |
""" |
|
108 |
argv = ["ssh", "-q"] |
|
109 |
argv.extend(KNOWN_HOSTS_OPTS) |
|
110 |
if batch: |
|
111 |
# if we are in batch mode, we can't ask the key |
|
112 |
if ask_key: |
|
113 |
raise errors.ProgrammerError("SSH call requested conflicting options") |
|
114 |
argv.extend(BATCH_MODE_OPTS) |
|
115 |
elif ask_key: |
|
116 |
argv.extend(ASK_KEY_OPTS) |
|
117 |
argv.extend(["%s@%s" % (user, hostname), command]) |
|
118 |
return argv |
|
119 |
|
|
120 |
|
|
121 |
def SSHCall(hostname, user, command, batch=True, ask_key=False): |
|
122 |
"""Execute a command on a remote node. |
|
123 |
|
|
124 |
This method has the same return value as `utils.RunCmd()`, which it |
|
125 |
uses to launch ssh. |
|
126 |
|
|
127 |
Args: |
|
128 |
hostname: the target host, string |
|
129 |
user: user to auth as |
|
130 |
command: the command |
|
131 |
batch: if true, ssh will run in batch mode with no prompting |
|
132 |
ask_key: if true, ssh will run with StrictHostKeyChecking=ask, so that |
|
133 |
we can connect to an unknown host (not valid in batch mode) |
|
134 |
|
|
135 |
Returns: |
|
136 |
`utils.RunResult` as for `utils.RunCmd()` |
|
137 |
|
|
138 |
""" |
|
139 |
return utils.RunCmd(BuildSSHCmd(hostname, user, command, |
|
140 |
batch=batch, ask_key=ask_key)) |
|
141 |
|
|
142 |
|
|
143 |
def CopyFileToNode(node, filename): |
|
144 |
"""Copy a file to another node with scp. |
|
145 |
|
|
146 |
Args: |
|
147 |
node: node in the cluster |
|
148 |
filename: absolute pathname of a local file |
|
149 |
|
|
150 |
Returns: |
|
151 |
success: True/False |
|
89 |
class SshRunner: |
|
90 |
"""Wrapper for SSH commands. |
|
152 | 91 |
|
153 | 92 |
""" |
154 |
if not os.path.isfile(filename): |
|
155 |
logger.Error("file %s does not exist" % (filename)) |
|
156 |
return False |
|
157 |
|
|
158 |
if not os.path.isabs(filename): |
|
159 |
logger.Error("file %s must be an absolute path" % (filename)) |
|
160 |
return False |
|
93 |
def BuildCmd(self, hostname, user, command, batch=True, ask_key=False): |
|
94 |
"""Build an ssh command to execute a command on a remote node. |
|
95 |
|
|
96 |
Args: |
|
97 |
hostname: the target host, string |
|
98 |
user: user to auth as |
|
99 |
command: the command |
|
100 |
batch: if true, ssh will run in batch mode with no prompting |
|
101 |
ask_key: if true, ssh will run with StrictHostKeyChecking=ask, so that |
|
102 |
we can connect to an unknown host (not valid in batch mode) |
|
103 |
|
|
104 |
Returns: |
|
105 |
The ssh call to run 'command' on the remote host. |
|
106 |
|
|
107 |
""" |
|
108 |
argv = ["ssh", "-q"] |
|
109 |
argv.extend(KNOWN_HOSTS_OPTS) |
|
110 |
if batch: |
|
111 |
# if we are in batch mode, we can't ask the key |
|
112 |
if ask_key: |
|
113 |
raise errors.ProgrammerError("SSH call requested conflicting options") |
|
114 |
argv.extend(BATCH_MODE_OPTS) |
|
115 |
elif ask_key: |
|
116 |
argv.extend(ASK_KEY_OPTS) |
|
117 |
argv.extend(["%s@%s" % (user, hostname), command]) |
|
118 |
return argv |
|
119 |
|
|
120 |
def Run(self, hostname, user, command, batch=True, ask_key=False): |
|
121 |
"""Runs a command on a remote node. |
|
122 |
|
|
123 |
This method has the same return value as `utils.RunCmd()`, which it |
|
124 |
uses to launch ssh. |
|
125 |
|
|
126 |
Args: |
|
127 |
hostname: the target host, string |
|
128 |
user: user to auth as |
|
129 |
command: the command |
|
130 |
batch: if true, ssh will run in batch mode with no prompting |
|
131 |
ask_key: if true, ssh will run with StrictHostKeyChecking=ask, so that |
|
132 |
we can connect to an unknown host (not valid in batch mode) |
|
133 |
|
|
134 |
Returns: |
|
135 |
`utils.RunResult` like `utils.RunCmd()` |
|
136 |
|
|
137 |
""" |
|
138 |
return utils.RunCmd(self.BuildCmd(hostname, user, command, batch=batch, |
|
139 |
ask_key=ask_key)) |
|
140 |
|
|
141 |
def CopyFileToNode(self, node, filename): |
|
142 |
"""Copy a file to another node with scp. |
|
143 |
|
|
144 |
Args: |
|
145 |
node: node in the cluster |
|
146 |
filename: absolute pathname of a local file |
|
147 |
|
|
148 |
Returns: |
|
149 |
success: True/False |
|
161 | 150 |
|
162 |
command = ["scp", "-q", "-p"] |
|
163 |
command.extend(KNOWN_HOSTS_OPTS) |
|
164 |
command.extend(BATCH_MODE_OPTS) |
|
165 |
command.append(filename) |
|
166 |
command.append("%s:%s" % (node, filename)) |
|
151 |
""" |
|
152 |
if not os.path.isfile(filename): |
|
153 |
logger.Error("file %s does not exist" % (filename)) |
|
154 |
return False |
|
167 | 155 |
|
168 |
result = utils.RunCmd(command) |
|
156 |
if not os.path.isabs(filename): |
|
157 |
logger.Error("file %s must be an absolute path" % (filename)) |
|
158 |
return False |
|
169 | 159 |
|
170 |
if result.failed: |
|
171 |
logger.Error("copy to node %s failed (%s) error %s," |
|
172 |
" command was %s" % |
|
173 |
(node, result.fail_reason, result.output, result.cmd)) |
|
160 |
command = ["scp", "-q", "-p"] |
|
161 |
command.extend(KNOWN_HOSTS_OPTS) |
|
162 |
command.extend(BATCH_MODE_OPTS) |
|
163 |
command.append(filename) |
|
164 |
command.append("%s:%s" % (node, filename)) |
|
174 | 165 |
|
175 |
return not result.failed
|
|
166 |
result = utils.RunCmd(command)
|
|
176 | 167 |
|
168 |
if result.failed: |
|
169 |
logger.Error("copy to node %s failed (%s) error %s," |
|
170 |
" command was %s" % |
|
171 |
(node, result.fail_reason, result.output, result.cmd)) |
|
177 | 172 |
|
178 |
def VerifyNodeHostname(node): |
|
179 |
"""Verify hostname consistency via SSH. |
|
173 |
return not result.failed |
|
180 | 174 |
|
175 |
def VerifyNodeHostname(self, node): |
|
176 |
"""Verify hostname consistency via SSH. |
|
181 | 177 |
|
182 |
This functions connects via ssh to a node and compares the hostname |
|
183 |
reported by the node to the name with have (the one that we |
|
184 |
connected to). |
|
178 |
This functions connects via ssh to a node and compares the hostname
|
|
179 |
reported by the node to the name with have (the one that we
|
|
180 |
connected to).
|
|
185 | 181 |
|
186 |
This is used to detect problems in ssh known_hosts files |
|
187 |
(conflicting known hosts) and incosistencies between dns/hosts |
|
188 |
entries and local machine names |
|
182 |
This is used to detect problems in ssh known_hosts files
|
|
183 |
(conflicting known hosts) and incosistencies between dns/hosts
|
|
184 |
entries and local machine names
|
|
189 | 185 |
|
190 |
Args: |
|
191 |
node: nodename of a host to check. can be short or full qualified hostname |
|
186 |
Args:
|
|
187 |
node: nodename of a host to check. can be short or full qualified hostname
|
|
192 | 188 |
|
193 |
Returns: |
|
194 |
(success, detail) |
|
195 |
where |
|
196 |
success: True/False |
|
197 |
detail: String with details |
|
189 |
Returns:
|
|
190 |
(success, detail)
|
|
191 |
where
|
|
192 |
success: True/False
|
|
193 |
detail: String with details
|
|
198 | 194 |
|
199 |
""" |
|
200 |
retval = SSHCall(node, 'root', 'hostname')
|
|
195 |
"""
|
|
196 |
retval = self.Run(node, 'root', 'hostname')
|
|
201 | 197 |
|
202 |
if retval.failed: |
|
203 |
msg = "ssh problem" |
|
204 |
output = retval.output |
|
205 |
if output: |
|
206 |
msg += ": %s" % output |
|
207 |
return False, msg |
|
198 |
if retval.failed:
|
|
199 |
msg = "ssh problem"
|
|
200 |
output = retval.output
|
|
201 |
if output:
|
|
202 |
msg += ": %s" % output
|
|
203 |
return False, msg
|
|
208 | 204 |
|
209 |
remotehostname = retval.stdout.strip() |
|
205 |
remotehostname = retval.stdout.strip()
|
|
210 | 206 |
|
211 |
if not remotehostname or remotehostname != node: |
|
212 |
return False, "hostname mismatch, got %s" % remotehostname |
|
207 |
if not remotehostname or remotehostname != node:
|
|
208 |
return False, "hostname mismatch, got %s" % remotehostname
|
|
213 | 209 |
|
214 |
return True, "host matches" |
|
210 |
return True, "host matches"
|
|
215 | 211 |
|
216 | 212 |
|
217 | 213 |
def WriteKnownHostsFile(cfg, sstore, file_name): |
Also available in: Unified diff