Revision c8a0948f lib/cmdlib.py
b/lib/cmdlib.py | ||
---|---|---|
163 | 163 |
return {}, [], [] |
164 | 164 |
|
165 | 165 |
|
166 |
def _RemoveHostFromEtcHosts(hostname): |
|
167 |
"""Wrapper around utils.RemoteEtcHostsEntry. |
|
168 |
|
|
169 |
""" |
|
170 |
hi = utils.HostInfo(name=hostname) |
|
171 |
utils.RemoveEtcHostsEntry(constants.ETC_HOSTS, hi.name) |
|
172 |
utils.RemoveEtcHostsEntry(constants.ETC_HOSTS, hi.ShortName()) |
|
173 |
|
|
174 |
|
|
166 | 175 |
def _GetWantedNodes(lu, nodes): |
167 | 176 |
"""Returns list of checked and expanded node names. |
168 | 177 |
|
... | ... | |
285 | 294 |
return _BuildInstanceHookEnv(**args) |
286 | 295 |
|
287 | 296 |
|
288 |
def _UpdateEtcHosts(fullnode, ip): |
|
289 |
"""Ensure a node has a correct entry in /etc/hosts. |
|
290 |
|
|
291 |
Args: |
|
292 |
fullnode - Fully qualified domain name of host. (str) |
|
293 |
ip - IPv4 address of host (str) |
|
294 |
|
|
295 |
""" |
|
296 |
node = fullnode.split(".", 1)[0] |
|
297 |
|
|
298 |
f = open('/etc/hosts', 'r+') |
|
299 |
|
|
300 |
inthere = False |
|
301 |
|
|
302 |
save_lines = [] |
|
303 |
add_lines = [] |
|
304 |
removed = False |
|
305 |
|
|
306 |
while True: |
|
307 |
rawline = f.readline() |
|
308 |
|
|
309 |
if not rawline: |
|
310 |
# End of file |
|
311 |
break |
|
312 |
|
|
313 |
line = rawline.split('\n')[0] |
|
314 |
|
|
315 |
# Strip off comments |
|
316 |
line = line.split('#')[0] |
|
317 |
|
|
318 |
if not line: |
|
319 |
# Entire line was comment, skip |
|
320 |
save_lines.append(rawline) |
|
321 |
continue |
|
322 |
|
|
323 |
fields = line.split() |
|
324 |
|
|
325 |
haveall = True |
|
326 |
havesome = False |
|
327 |
for spec in [ ip, fullnode, node ]: |
|
328 |
if spec not in fields: |
|
329 |
haveall = False |
|
330 |
if spec in fields: |
|
331 |
havesome = True |
|
332 |
|
|
333 |
if haveall: |
|
334 |
inthere = True |
|
335 |
save_lines.append(rawline) |
|
336 |
continue |
|
337 |
|
|
338 |
if havesome and not haveall: |
|
339 |
# Line (old, or manual?) which is missing some. Remove. |
|
340 |
removed = True |
|
341 |
continue |
|
342 |
|
|
343 |
save_lines.append(rawline) |
|
344 |
|
|
345 |
if not inthere: |
|
346 |
add_lines.append('%s\t%s %s\n' % (ip, fullnode, node)) |
|
347 |
|
|
348 |
if removed: |
|
349 |
if add_lines: |
|
350 |
save_lines = save_lines + add_lines |
|
351 |
|
|
352 |
# We removed a line, write a new file and replace old. |
|
353 |
fd, tmpname = tempfile.mkstemp('tmp', 'hosts_', '/etc') |
|
354 |
newfile = os.fdopen(fd, 'w') |
|
355 |
newfile.write(''.join(save_lines)) |
|
356 |
newfile.close() |
|
357 |
os.rename(tmpname, '/etc/hosts') |
|
358 |
|
|
359 |
elif add_lines: |
|
360 |
# Simply appending a new line will do the trick. |
|
361 |
f.seek(0, 2) |
|
362 |
for add in add_lines: |
|
363 |
f.write(add) |
|
364 |
|
|
365 |
f.close() |
|
366 |
|
|
367 |
|
|
368 | 297 |
def _UpdateKnownHosts(fullnode, ip, pubkey): |
369 | 298 |
"""Ensure a node has a correct known_hosts entry. |
370 | 299 |
|
... | ... | |
645 | 574 |
f.close() |
646 | 575 |
sshkey = sshline.split(" ")[1] |
647 | 576 |
|
648 |
_UpdateEtcHosts(hostname.name, hostname.ip) |
|
577 |
hi = utils.HostInfo(name=hostname.name) |
|
578 |
utils.AddEtcHostsEntry(constants.ETC_HOSTS, hostname.name, hi.ip) |
|
579 |
utils.AddEtcHostsEntry(constants.ETC_HOSTS, hi.ShortName(), hi.ip) |
|
580 |
del hi |
|
649 | 581 |
|
650 | 582 |
_UpdateKnownHosts(hostname.name, hostname.ip, sshkey) |
651 | 583 |
|
... | ... | |
687 | 619 |
"""Destroys the cluster. |
688 | 620 |
|
689 | 621 |
""" |
622 |
master = self.sstore.GetMasterNode() |
|
690 | 623 |
priv_key, pub_key, _ = ssh.GetUserFiles(constants.GANETI_RUNAS) |
691 | 624 |
utils.CreateBackup(priv_key) |
692 | 625 |
utils.CreateBackup(pub_key) |
693 |
rpc.call_node_leave_cluster(self.sstore.GetMasterNode()) |
|
626 |
rpc.call_node_leave_cluster(master) |
|
627 |
_RemoveHostFromEtcHosts(master) |
|
694 | 628 |
|
695 | 629 |
|
696 | 630 |
class LUVerifyCluster(NoHooksLU): |
... | ... | |
1205 | 1139 |
|
1206 | 1140 |
self.cfg.RemoveNode(node.name) |
1207 | 1141 |
|
1142 |
_RemoveHostFromEtcHosts(node.name) |
|
1143 |
|
|
1208 | 1144 |
|
1209 | 1145 |
class LUQueryNodes(NoHooksLU): |
1210 | 1146 |
"""Logical unit for querying nodes. |
... | ... | |
1548 | 1484 |
raise errors.OpExecError("Cannot transfer ssh keys to the new node") |
1549 | 1485 |
|
1550 | 1486 |
# Add node to our /etc/hosts, and add key to known_hosts |
1551 |
_UpdateEtcHosts(new_node.name, new_node.primary_ip) |
|
1487 |
hi = utils.HostInfo(name=new_node.name) |
|
1488 |
utils.AddEtcHostsEntry(constants.ETC_HOSTS, new_node.name, hi.ip) |
|
1489 |
utils.AddEtcHostsEntry(constants.ETC_HOSTS, hi.ShortName(), hi.ip) |
|
1490 |
del hi |
|
1491 |
|
|
1552 | 1492 |
_UpdateKnownHosts(new_node.name, new_node.primary_ip, |
1553 | 1493 |
self.cfg.GetHostKey()) |
1554 | 1494 |
|
Also available in: Unified diff