Revision c92b310a lib/ssh.py
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