Skip to content

Commit

Permalink
apply: compare full configuration to decide to restart networkd
Browse files Browse the repository at this point in the history
Copy the current networkd configuration and then call generate so we
know more accurately if systemd-networkd needs to be restarted.
  • Loading branch information
alfonsosanchezbeato authored and slyon committed Jun 26, 2024
1 parent a530acf commit a9c1433
Showing 1 changed file with 24 additions and 21 deletions.
45 changes: 24 additions & 21 deletions netplan_cli/cli/commands/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import glob
import subprocess
import shutil
import tempfile
import filecmp
import netifaces
import time

Expand Down Expand Up @@ -109,7 +111,6 @@ def command_apply(self, run_generate=True, sync=False, exit_on_error=True, state
return

ovs_cleanup_service = '/run/systemd/system/netplan-ovs-cleanup.service'
old_files_networkd = bool(glob.glob('/run/systemd/network/*netplan-*'))
old_ovs_glob = glob.glob('/run/systemd/system/netplan-ovs-*')
# Ignore netplan-ovs-cleanup.service, as it can always be there
if ovs_cleanup_service in old_ovs_glob:
Expand All @@ -119,30 +120,32 @@ def command_apply(self, run_generate=True, sync=False, exit_on_error=True, state
nm_ifaces = utils.nm_interfaces(old_nm_glob, netifaces.interfaces())
old_files_nm = bool(old_nm_glob)

generator_call = []
generate_out = None
if 'NETPLAN_PROFILE' in os.environ:
generator_call.extend(['valgrind', '--leak-check=full'])
generate_out = subprocess.STDOUT
restart_networkd = False
with tempfile.TemporaryDirectory() as tmp_dir:
# needs to be a subfolder as copytree wants to create it
old_files_dir = os.path.join(tmp_dir, 'cfg')
shutil.copytree('/run/systemd/network', old_files_dir)

generator_call.append(utils.get_generator_path())
if run_generate and subprocess.call(generator_call, stderr=generate_out) != 0:
if exit_on_error:
sys.exit(os.EX_CONFIG)
else:
raise ConfigurationError("the configuration could not be generated")
generator_call = []
generate_out = None
if 'NETPLAN_PROFILE' in os.environ:
generator_call.extend(['valgrind', '--leak-check=full'])
generate_out = subprocess.STDOUT

generator_call.append(utils.get_generator_path())
if run_generate and subprocess.call(generator_call, stderr=generate_out) != 0:
if exit_on_error:
sys.exit(os.EX_CONFIG)
else:
raise ConfigurationError("the configuration could not be generated")

# Restart networkd if something in the configuration changed
comp = filecmp.dircmp('/run/systemd/network', old_files_dir)
if comp.left_only or comp.right_only or comp.diff_files:
restart_networkd = True

devices = netifaces.interfaces()

# Re-start service when
# 1. We have configuration files for it
# 2. Previously we had config files for it but not anymore
# Ideally we should compare the content of the *netplan-* files before and
# after generation to minimize the number of re-starts, but the conditions
# above works too.
restart_networkd = bool(glob.glob('/run/systemd/network/*netplan-*'))
if not restart_networkd and old_files_networkd:
restart_networkd = True
restart_ovs_glob = glob.glob('/run/systemd/system/netplan-ovs-*')
# Ignore netplan-ovs-cleanup.service, as it can always be there
if ovs_cleanup_service in restart_ovs_glob:
Expand Down

0 comments on commit a9c1433

Please sign in to comment.