Improve logging in the dnsmasq DHCP provider

Currently, it logs a lot of entries without context, which won't be
readable when several nodes are deployed at the same time, nor when
someone greps for a node UUID.

Make fewer log entries and add node UUID for context.

While here, modernize the code a bit.

Change-Id: I3a840e47a09e77a9f8d35a7cf400c4bdd4111f91
This commit is contained in:
Dmitry Tantsur 2023-11-28 16:40:18 +01:00
parent b44d16a155
commit 40edeed615
No known key found for this signature in database
GPG Key ID: 315B2AF9FD216C60

View File

@ -50,35 +50,41 @@ class DnsmasqDHCPApi(base.BaseDHCP):
node = task.node
macs = set(self._pxe_enabled_macs(task.ports))
opt_file = self._opt_file_path(node)
tag = node.driver_internal_info.get('dnsmasq_tag')
if not tag:
tag = uuidutils.generate_uuid()
node.set_driver_internal_info('dnsmasq_tag', tag)
node.save()
LOG.debug('Writing to %s:', opt_file)
with open(opt_file, 'w') as f:
# Apply each option by tag
option_entries = []
for option in options:
entry = 'tag:{tag},{opt_name},{opt_value}\n'.format(
tag=tag,
opt_name=option.get('opt_name'),
opt_value=option.get('opt_value'),
)
LOG.debug(entry)
f.write(entry)
try:
option_entries.append(
f'tag:{tag},{option["opt_name"]},{option["opt_value"]}')
except KeyError as missing:
LOG.warning('Ignoring option %(opt)s for node %(node)s: '
'missing %(missing)s',
{'opt': option, 'node': node.uuid,
'missing': missing})
opt_file = self._opt_file_path(node)
LOG.debug('Writing DHCP options for node %(node)s to %(dest)s: '
'%(opts)s', {'node': node.uuid, 'dest': opt_file,
'opts': '; '.join(option_entries)})
with open(opt_file, 'w') as f:
f.write('\n'.join(option_entries) + '\n')
for mac in macs:
host_file = self._host_file_path(mac)
LOG.debug('Writing to %s:', host_file)
with open(host_file, 'w') as f:
# Tag each address with the unique uuid scoped to
# this node and DHCP transaction
entry = '{mac},set:{tag},set:ironic\n'.format(
mac=mac, tag=tag)
LOG.debug(entry)
f.write(entry)
host_file = self._host_file_path(mac)
entry = f'{mac},set:{tag},set:ironic'
LOG.debug('Writing DHCP host file for node %(node)s to %(dest)s: '
'%(entry)s', {'node': node.uuid, 'dest': host_file,
'entry': entry})
with open(host_file, 'w') as f:
f.write(entry + '\n')
def _opt_file_path(self, node):
return os.path.join(CONF.dnsmasq.dhcp_optsdir,
@ -131,15 +137,20 @@ class DnsmasqDHCPApi(base.BaseDHCP):
macs = set(self._pxe_enabled_macs(task.ports))
for mac in macs:
host_file = self._host_file_path(mac)
entry = f'{mac},ignore'
LOG.debug('Writing DHCP host file for node %(node)s to %(dest)s: '
'%(entry)s', {'node': node.uuid, 'dest': host_file,
'entry': entry})
with open(host_file, 'w') as f:
entry = '{mac},ignore\n'.format(mac=mac)
f.write(entry)
f.write(entry + '\n')
# Deleting the file containing dhcp-option won't remove the rules from
# dnsmasq but no requests will be tagged with the dnsmasq_tag uuid so
# these rules will not apply.
opt_file = self._opt_file_path(node)
if os.path.exists(opt_file):
LOG.debug('Removing DHCP options file for node %(node)s at '
'%(dest)s', {'node': node.uuid, 'dest': opt_file})
os.remove(opt_file)
def supports_ipxe_tag(self):