Skip to content

Commit 7f90878

Browse files
committed
cli:try:generate: avoid calling 'daemon-reload' through NetworkManager integration during 'netplan try'
1 parent 1b06cf2 commit 7f90878

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

netplan_cli/cli/commands/generate.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def run(self):
4141
help='Display the netplan device ID/backend/interface name mapping and exit.')
4242

4343
self.func = self.command_generate
44+
self._rootdir = '/'
4445

4546
self.parse_args()
4647
self.run_command()
@@ -78,12 +79,25 @@ def command_generate(self):
7879
argv = [utils.get_configure_path()]
7980
if self.root_dir:
8081
argv += ['--root-dir', self.root_dir]
82+
self._rootdir = self.root_dir
8183
if self.mapping:
8284
argv += ['--mapping', self.mapping]
8385

86+
self._netplan_try_stamp = os.path.join(self._rootdir, self.try_ready_stamp)
8487
if self.mapping: # XXX: get rid of the legacy "--mapping" option
8588
argv[0] = utils.get_generator_path()
8689
res = subprocess.call(argv)
90+
elif os.path.isfile(self._netplan_try_stamp):
91+
# Avoid calling the Netplan generator if 'netplan try' is restoring
92+
# a previous configuration. See https://github.com/canonical/netplan/pull/548
93+
# This is especially relevant when NetworkManager is calling 'netplan generate'
94+
# before loading connection profiles, as this would trigger a 'systemctl daemon-reload'
95+
# and remove the /run/systemd/generator[.late]/ directories while the sd-generator
96+
# itself would be blocked from re-generating the files due to the
97+
# /run/netplan/netplan-try.ready stamp.
98+
logging.debug('Skipping daemon-reload... \'netplan try\' is restoring configuration, '
99+
'remove %s to force re-run.', self._netplan_try_stamp)
100+
res = 1
87101
else:
88102
logging.debug('executing Netplan systemd-generator via daemon-reload')
89103
if self.root_dir: # for testing purposes

netplan_cli/cli/commands/try_command.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def __init__(self):
5050
self.t_settings = None
5151
self.t = None
5252
self._rootdir = os.environ.get('DBUS_TEST_NETPLAN_ROOT', '/')
53-
self._netplan_try_stamp = os.path.join(self._rootdir, 'run', 'netplan', 'netplan-try.ready')
53+
self._netplan_try_stamp = os.path.join(self._rootdir, self.try_ready_stamp)
5454

5555
@property
5656
def config_manager(self): # pragma: nocover (called by later commands)

netplan_cli/cli/utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ def __init__(self, command_id, description, leaf=True, testing=False):
297297
self.subcommands = {}
298298
self.subcommand = None
299299
self.func = None
300+
self.try_ready_stamp = 'run/netplan/netplan-try.ready'
300301
self.generator_dir = '/run/systemd/generator/'
301302
self.generator_early_dir = '/run/systemd/generator.early/'
302303
self.generator_late_dir = '/run/systemd/generator.late/'

tests/cli_legacy.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,29 @@ def test_mapping_for_renamed_iface(self):
183183
self.assertNotEqual(b'', out)
184184
self.assertIn('renamediface', out.decode('utf-8'))
185185

186+
def test_netplan_try_ready_stamp_skip(self):
187+
os.environ.setdefault('NETPLAN_GENERATE_PATH', os.path.join(rootdir, 'generate'))
188+
os.environ.setdefault('NETPLAN_CONFIGURE_PATH', os.path.join(rootdir, 'configure'))
189+
c = os.path.join(self.workdir.name, 'etc', 'netplan')
190+
os.makedirs(c)
191+
path_a = os.path.join(c, 'a.yaml')
192+
with open(path_a, 'w') as f:
193+
f.write('''network:
194+
version: 2
195+
ethernets:
196+
myif:
197+
dhcp4: yes
198+
''')
199+
os.chmod(path_a, mode=0o600)
200+
201+
stamp_file = os.path.join(self.workdir.name, 'run', 'netplan', 'netplan-try.ready')
202+
os.makedirs(self.workdir.name + '/run/netplan', mode=0o700, exist_ok=True)
203+
open(stamp_file, 'w').close() # create stamp file
204+
res = subprocess.run(exe_cli + ['--debug', 'generate', '--root-dir', self.workdir.name],
205+
text=True, stderr=subprocess.PIPE)
206+
self.assertEqual(res.returncode, 1)
207+
self.assertIn('DEBUG:Skipping daemon-reload... \'netplan try\' is restoring configuration', res.stderr)
208+
186209

187210
class TestIfupdownMigrate(unittest.TestCase):
188211

0 commit comments

Comments
 (0)