Merge "add hosts entries and ssh keys only once"

This commit is contained in:
Zuul 2021-01-06 23:52:53 +00:00 committed by Gerrit Code Review
commit 47e59ab5cd
2 changed files with 9 additions and 10 deletions

View File

@ -22,7 +22,6 @@ import errno
import json
import logging
import os
import re
import subprocess
import sys
import time
@ -1419,7 +1418,7 @@ def write_ssh_keys(args):
if key_title not in keys_to_write:
keys_to_write.append(key_title)
if key not in keys_to_write:
if key.rstrip() not in keys_to_write:
keys_to_write.append(key)
files_to_write = {
@ -1492,14 +1491,14 @@ def set_hostname_from_config_drive(args):
for host in hosts_to_add:
host_value = hosts_to_add[host]
# See if we already have a hosts entry for hostname
prog = re.compile('^%s .*%s\n' % (host_value, host))
match = None
if os.path.isfile('/etc/hosts'):
with open('/etc/hosts') as fh:
match = prog.match(fh.read())
# Write out a hosts entry for hostname
if match is None:
with safe_open('/etc/hosts', 'r+') as fh:
for line in fh:
if line.startswith('%s %s' % (host_value, host)):
break
else:
fh.write(u'%s %s\n' % (host_value, host))
else:
with safe_open('/etc/hosts', 'a+') as fh:
fh.write(u'%s %s\n' % (host_value, host))

View File

@ -92,7 +92,7 @@ class TestGlean(base.BaseTestCase):
# note; don't use spec=file here ... it's not py3
# compatible. It really just limits the allowed
# mocked functions.
mock_handle = mock.Mock()
mock_handle = mock.MagicMock()
mock_handle.__enter__ = mock.Mock()
mock_handle.__exit__ = mock.Mock()
mock_handle.name = path