Merge "Allow disabling DHCP fallback"

This commit is contained in:
Zuul 2021-04-27 01:21:34 +00:00 committed by Gerrit Code Review
commit ee93fde849
7 changed files with 50 additions and 14 deletions

View File

@ -310,6 +310,11 @@ def write_redhat_interfaces(interfaces, sys_interfaces, args):
if interface['type'] == 'manual': if interface['type'] == 'manual':
files_to_write.update( files_to_write.update(
_write_rh_manual(interface_name, interface, args)) _write_rh_manual(interface_name, interface, args))
if args.no_dhcp_fallback:
log.debug('DHCP fallback disabled')
return files_to_write
for mac, iname in sorted( for mac, iname in sorted(
sys_interfaces.items(), key=lambda x: x[1]): sys_interfaces.items(), key=lambda x: x[1]):
if _exists_rh_interface(iname, args.distro): if _exists_rh_interface(iname, args.distro):
@ -569,6 +574,9 @@ def write_networkd_interfaces(interfaces, sys_interfaces, args):
# We have a config drive config, move on # We have a config drive config, move on
log.debug("%s configured via config-drive" % mac) log.debug("%s configured via config-drive" % mac)
continue continue
if args.no_dhcp_fallback:
log.debug('DHCP fallback disabled')
continue
interface = {'type': 'ipv4_dhcp', 'mac_address': mac} interface = {'type': 'ipv4_dhcp', 'mac_address': mac}
files_struct = _write_networkd_interface( files_struct = _write_networkd_interface(
iname, [interface], args, files_struct) iname, [interface], args, files_struct)
@ -731,7 +739,7 @@ def _create_gentoo_net_symlink_and_enable(interface_name):
_enable_gentoo_interface(interface_name) _enable_gentoo_interface(interface_name)
def write_gentoo_interfaces(interfaces, sys_interfaces): def write_gentoo_interfaces(interfaces, sys_interfaces, args):
files_to_write = dict() files_to_write = dict()
gen_intfs = {} gen_intfs = {}
# Sort the interfaces by id so that we'll have consistent output order # Sort the interfaces by id so that we'll have consistent output order
@ -774,6 +782,10 @@ def write_gentoo_interfaces(interfaces, sys_interfaces):
_write_gentoo_interface(interface_name, interfs)) _write_gentoo_interface(interface_name, interfs))
_setup_gentoo_network_init(interface_name, interfs) _setup_gentoo_network_init(interface_name, interfs)
if args.no_dhcp_fallback:
log.debug('DHCP fallback disabled')
return files_to_write
for mac, iname in sorted( for mac, iname in sorted(
sys_interfaces.items(), key=lambda x: x[1]): sys_interfaces.items(), key=lambda x: x[1]):
if _exists_gentoo_interface(iname): if _exists_gentoo_interface(iname):
@ -812,7 +824,7 @@ def _write_debian_bond_conf(interface_name, interface, sys_interfaces):
return result return result
def write_debian_interfaces(interfaces, sys_interfaces): def write_debian_interfaces(interfaces, sys_interfaces, args):
eni_path = '/etc/network/interfaces' eni_path = '/etc/network/interfaces'
eni_d_path = eni_path + '.d' eni_d_path = eni_path + '.d'
files_to_write = dict() files_to_write = dict()
@ -924,6 +936,10 @@ def write_debian_interfaces(interfaces, sys_interfaces):
sys_interfaces) sys_interfaces)
files_to_write[iface_path] = result files_to_write[iface_path] = result
if args.no_dhcp_fallback:
log.debug('DHCP fallback disabled')
return files_to_write
# Configure any interfaces not mentioned in the config drive data for DHCP. # Configure any interfaces not mentioned in the config drive data for DHCP.
for mac, iname in sorted( for mac, iname in sorted(
sys_interfaces.items(), key=lambda x: x[1]): sys_interfaces.items(), key=lambda x: x[1]):
@ -1069,14 +1085,14 @@ def write_static_network_info(
if args.distro in ('debian', 'ubuntu'): if args.distro in ('debian', 'ubuntu'):
files_to_write.update( files_to_write.update(
write_debian_interfaces(interfaces, sys_interfaces)) write_debian_interfaces(interfaces, sys_interfaces, args))
elif args.distro in ('redhat', 'centos', 'fedora') or \ elif args.distro in ('redhat', 'centos', 'fedora') or \
_is_suse(args.distro): _is_suse(args.distro):
files_to_write.update( files_to_write.update(
write_redhat_interfaces(interfaces, sys_interfaces, args)) write_redhat_interfaces(interfaces, sys_interfaces, args))
elif args.distro in 'gentoo': elif args.distro in 'gentoo':
files_to_write.update( files_to_write.update(
write_gentoo_interfaces(interfaces, sys_interfaces) write_gentoo_interfaces(interfaces, sys_interfaces, args)
) )
elif args.distro in 'networkd': elif args.distro in 'networkd':
files_to_write.update( files_to_write.update(
@ -1522,6 +1538,9 @@ def main(argv=None):
parser.add_argument( parser.add_argument(
'--debug', dest='debug', action='store_true', '--debug', dest='debug', action='store_true',
help="Enable debugging output") help="Enable debugging output")
parser.add_argument(
"--no-dhcp-fallback", action="store_true",
help="Do not fall back to DHCP")
args = parser.parse_args(argv) args = parser.parse_args(argv)
if args.debug: if args.debug:

View File

@ -6,7 +6,7 @@ Requires=systemd-networkd.service
[Service] [Service]
Type=oneshot Type=oneshot
User=root User=root
ExecStart=%%GLEANSH_PATH%%/glean.sh --distro networkd ExecStart=%%GLEANSH_PATH%%/glean.sh --distro networkd %%EXTRA_ARGS%%
RemainAfterExit=true RemainAfterExit=true
[Install] [Install]

View File

@ -13,7 +13,7 @@ ConditionPathExists=!/etc/sysconfig/network/ifcfg-%I
Type=oneshot Type=oneshot
User=root User=root
Environment="ARGS=--interface %I" Environment="ARGS=--interface %I"
ExecStart=%%GLEANSH_PATH%%/glean.sh --use-nm --debug $ARGS ExecStart=%%GLEANSH_PATH%%/glean.sh --use-nm --debug $ARGS %%EXTRA_ARGS%%
RemainAfterExit=true RemainAfterExit=true
StandardOutput=journal+console StandardOutput=journal+console

View File

@ -11,4 +11,4 @@ start on starting networking
task task
exec /usr/local/bin/glean.sh exec /usr/local/bin/glean.sh %%EXTRA_ARGS%%

View File

@ -18,7 +18,7 @@ SCRIPT_NAME=/usr/local/bin/${NAME}.sh
case "$1" in case "$1" in
start) start)
$SCRIPT_NAME $SCRIPT_NAME %%EXTRA_ARGS%%
;; ;;
stop) stop)
;; ;;

View File

@ -15,7 +15,7 @@ ConditionPathExists=!/etc/network/interfaces.d/%I.cfg
Type=oneshot Type=oneshot
User=root User=root
Environment="ARGS=--interface %I" Environment="ARGS=--interface %I"
ExecStartPre=%%GLEANSH_PATH%%/glean.sh ${ARGS} ExecStartPre=%%GLEANSH_PATH%%/glean.sh ${ARGS} %%EXTRA_ARGS%%
ExecStart=/sbin/ifup %I ExecStart=/sbin/ifup %I
RemainAfterExit=true RemainAfterExit=true

View File

@ -85,9 +85,21 @@ def main():
action="store_true") action="store_true")
parser.add_argument("-q", "--quiet", help="Be very quiet", parser.add_argument("-q", "--quiet", help="Be very quiet",
action="store_true") action="store_true")
# NOTE(dtantsur): there may be two reasons to disable the fallback:
# 1) Remote edge deployments where DHCP (if present at all) is likely
# incorrect and should not be used.
# 2) Co-existing with another tool that handles DHCP differently (IPv6,
# SLAAC) or does a more sophisticated configuration (like NetworkManager
# or, in case of ironic-python-agent, dhcp-all-interfaces, which is **not**
# recommended, but is done nonetheless, mostly for legacy reasons).
parser.add_argument("--no-dhcp-fallback", action="store_true",
help="Do not fall back to DHCP. If this is on, "
"something else must configure networking or "
"it will be left unconfigured.")
args = parser.parse_args() args = parser.parse_args()
p = _find_gleansh_path() p = _find_gleansh_path()
extra_args = '--no-dhcp-fallback' if args.no_dhcp_fallback else ''
if args.quiet: if args.quiet:
logging.basicConfig(level=logging.ERROR) logging.basicConfig(level=logging.ERROR)
@ -98,7 +110,8 @@ def main():
if os.path.exists('/etc/gentoo-release'): if os.path.exists('/etc/gentoo-release'):
log.info('installing openrc services') log.info('installing openrc services')
install('glean.openrc', '/etc/init.d/glean', install('glean.openrc', '/etc/init.d/glean',
replacements={'GLEANSH_PATH': p}) replacements={'GLEANSH_PATH': p,
'EXTRA_ARGS': extra_args})
# Needs to check for the presence of systemd and systemctl # Needs to check for the presence of systemd and systemctl
# as apparently some packages may stage systemd init files # as apparently some packages may stage systemd init files
# when systemd is not present. # when systemd is not present.
@ -118,7 +131,8 @@ def main():
'glean-networkd.service', 'glean-networkd.service',
'/lib/systemd/system/glean.service', '/lib/systemd/system/glean.service',
mode='0644', mode='0644',
replacements={'GLEANSH_PATH': p}) replacements={'GLEANSH_PATH': p,
'EXTRA_ARGS': extra_args})
subprocess.call(['systemctl', 'enable', 'glean.service']) subprocess.call(['systemctl', 'enable', 'glean.service'])
else: else:
log.info("Installing %s NetworkManager support" % log.info("Installing %s NetworkManager support" %
@ -131,7 +145,8 @@ def main():
service_file, service_file,
'/usr/lib/systemd/system/glean@.service', '/usr/lib/systemd/system/glean@.service',
mode='0644', mode='0644',
replacements={'GLEANSH_PATH': p}) replacements={'GLEANSH_PATH': p,
'EXTRA_ARGS': extra_args})
install( install(
'glean-udev.rules', 'glean-udev.rules',
'/etc/udev/rules.d/99-glean.rules', '/etc/udev/rules.d/99-glean.rules',
@ -161,12 +176,14 @@ def main():
elif os.path.exists('/etc/init'): elif os.path.exists('/etc/init'):
log.info("Installing upstart services") log.info("Installing upstart services")
install('glean.conf', '/etc/init/glean.conf') install('glean.conf', '/etc/init/glean.conf',
replacements={'EXTRA_ARGS': extra_args})
elif os.path.exists('/sbin/rc-update'): elif os.path.exists('/sbin/rc-update'):
subprocess.call(['rc-update', 'add', 'glean', 'boot']) subprocess.call(['rc-update', 'add', 'glean', 'boot'])
else: else:
log.info("Installing sysv services") log.info("Installing sysv services")
install('glean.init', '/etc/init.d/glean') install('glean.init', '/etc/init.d/glean',
replacements={'EXTRA_ARGS': extra_args})
os.system('update-rc.d glean defaults') os.system('update-rc.d glean defaults')
if __name__ == '__main__': if __name__ == '__main__':