Revision f408b346
b/lib/cmdlib.py | ||
---|---|---|
316 | 316 |
return _BuildInstanceHookEnv(**args) |
317 | 317 |
|
318 | 318 |
|
319 |
def _UpdateKnownHosts(fullnode, ip, pubkey): |
|
320 |
"""Ensure a node has a correct known_hosts entry. |
|
321 |
|
|
322 |
Args: |
|
323 |
fullnode - Fully qualified domain name of host. (str) |
|
324 |
ip - IPv4 address of host (str) |
|
325 |
pubkey - the public key of the cluster |
|
326 |
|
|
327 |
""" |
|
328 |
if os.path.exists(constants.SSH_KNOWN_HOSTS_FILE): |
|
329 |
f = open(constants.SSH_KNOWN_HOSTS_FILE, 'r+') |
|
330 |
else: |
|
331 |
f = open(constants.SSH_KNOWN_HOSTS_FILE, 'w+') |
|
332 |
|
|
333 |
inthere = False |
|
334 |
|
|
335 |
save_lines = [] |
|
336 |
add_lines = [] |
|
337 |
removed = False |
|
338 |
|
|
339 |
for rawline in f: |
|
340 |
logger.Debug('read %s' % (repr(rawline),)) |
|
341 |
|
|
342 |
parts = rawline.rstrip('\r\n').split() |
|
343 |
|
|
344 |
# Ignore unwanted lines |
|
345 |
if len(parts) >= 3 and not rawline.lstrip()[0] == '#': |
|
346 |
fields = parts[0].split(',') |
|
347 |
key = parts[2] |
|
348 |
|
|
349 |
haveall = True |
|
350 |
havesome = False |
|
351 |
for spec in [ ip, fullnode ]: |
|
352 |
if spec not in fields: |
|
353 |
haveall = False |
|
354 |
if spec in fields: |
|
355 |
havesome = True |
|
356 |
|
|
357 |
logger.Debug("key, pubkey = %s." % (repr((key, pubkey)),)) |
|
358 |
if haveall and key == pubkey: |
|
359 |
inthere = True |
|
360 |
save_lines.append(rawline) |
|
361 |
logger.Debug("Keeping known_hosts '%s'." % (repr(rawline),)) |
|
362 |
continue |
|
363 |
|
|
364 |
if havesome and (not haveall or key != pubkey): |
|
365 |
removed = True |
|
366 |
logger.Debug("Discarding known_hosts '%s'." % (repr(rawline),)) |
|
367 |
continue |
|
368 |
|
|
369 |
save_lines.append(rawline) |
|
370 |
|
|
371 |
if not inthere: |
|
372 |
add_lines.append('%s,%s ssh-rsa %s\n' % (fullnode, ip, pubkey)) |
|
373 |
logger.Debug("Adding known_hosts '%s'." % (repr(add_lines[-1]),)) |
|
374 |
|
|
375 |
if removed: |
|
376 |
save_lines = save_lines + add_lines |
|
377 |
|
|
378 |
# Write a new file and replace old. |
|
379 |
fd, tmpname = tempfile.mkstemp('.tmp', 'known_hosts.', |
|
380 |
constants.DATA_DIR) |
|
381 |
newfile = os.fdopen(fd, 'w') |
|
382 |
try: |
|
383 |
newfile.write(''.join(save_lines)) |
|
384 |
finally: |
|
385 |
newfile.close() |
|
386 |
logger.Debug("Wrote new known_hosts.") |
|
387 |
os.rename(tmpname, constants.SSH_KNOWN_HOSTS_FILE) |
|
388 |
|
|
389 |
elif add_lines: |
|
390 |
# Simply appending a new line will do the trick. |
|
391 |
f.seek(0, 2) |
|
392 |
for add in add_lines: |
|
393 |
f.write(add) |
|
394 |
|
|
395 |
f.close() |
|
396 |
|
|
397 |
|
|
398 | 319 |
def _HasValidVG(vglist, vgname): |
399 | 320 |
"""Checks if the volume group list is valid. |
400 | 321 |
|
... | ... | |
607 | 528 |
sshkey = sshline.split(" ")[1] |
608 | 529 |
|
609 | 530 |
_AddHostToEtcHosts(hostname.name) |
610 |
|
|
611 |
_UpdateKnownHosts(hostname.name, hostname.ip, sshkey) |
|
612 |
|
|
613 | 531 |
_InitSSHSetup(hostname.name) |
614 | 532 |
|
615 | 533 |
# init of cluster config file |
... | ... | |
618 | 536 |
sshkey, self.op.mac_prefix, |
619 | 537 |
self.op.vg_name, self.op.def_bridge) |
620 | 538 |
|
539 |
ssh.WriteKnownHostsFile(cfgw, ss, constants.SSH_KNOWN_HOSTS_FILE) |
|
540 |
|
|
621 | 541 |
|
622 | 542 |
class LUDestroyCluster(NoHooksLU): |
623 | 543 |
"""Logical unit for destroying the cluster. |
... | ... | |
1596 | 1516 |
# Add node to our /etc/hosts, and add key to known_hosts |
1597 | 1517 |
_AddHostToEtcHosts(new_node.name) |
1598 | 1518 |
|
1599 |
_UpdateKnownHosts(new_node.name, new_node.primary_ip, |
|
1600 |
self.cfg.GetHostKey()) |
|
1601 |
|
|
1602 | 1519 |
if new_node.secondary_ip != new_node.primary_ip: |
1603 | 1520 |
if not rpc.call_node_tcp_ping(new_node.name, |
1604 | 1521 |
constants.LOCALHOST_IP_ADDRESS, |
Also available in: Unified diff