Skip to content

Commit ec561f0

Browse files
committed
lxd: do not restart instances via stop+start
Ephemeral instanced do not survive being stopped, while they do survive being restarted.
1 parent 6eee33c commit ec561f0

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

pycloudlib/instance.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def restart(self, wait=True, **kwargs):
8989
self._sync_filesystem()
9090
# If we're not waiting, just call subclass's restart and return.
9191
if not wait:
92-
self._do_restart()
92+
self._do_restart(**kwargs)
9393
return
9494

9595
pre_boot_id = None

pycloudlib/lxd/instance.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,15 +315,18 @@ def push_file(self, local_path, remote_path):
315315
def _do_restart(self, force=False, **kwargs):
316316
"""Restart an instance.
317317
318-
For LXD this means stopping the instance, and then starting it.
319-
320318
Args:
321319
force: boolean, force instance to shutdown before restart
322320
"""
323321
self._log.debug("restarting %s", self.name)
324322

325-
self.shutdown(wait=True, force=force)
326-
self.start(wait=False)
323+
# Note: even if slightly faster in some cases, do not replace
324+
# `lxc restart` with stop + start, as ephemeral instances do
325+
# not survive being stopped, while they do survive restarts.
326+
cmd = ["lxc", "restart", self.name]
327+
if force:
328+
cmd.append("--force")
329+
subp(cmd)
327330

328331
def restore(self, snapshot_name):
329332
"""Restore instance from a specific snapshot.

pycloudlib/lxd/tests/test_instance.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,23 @@ class TestRestart:
1212
"""Tests covering pycloudlib.lxd.instance.Instance.restart."""
1313

1414
@pytest.mark.parametrize("force", (False, True))
15-
@mock.patch("pycloudlib.lxd.instance.LXDInstance.start")
16-
@mock.patch("pycloudlib.lxd.instance.LXDInstance.shutdown")
17-
@mock.patch("pycloudlib.lxd.instance.LXDInstance.wait")
1815
@mock.patch("pycloudlib.lxd.instance.subp")
19-
def test_restart_calls_lxc_cmd_with_force_param(
20-
self, _m_subp, _m_wait, m_shutdown, m_start, force
21-
):
22-
"""Honor force param on shutdown."""
16+
def test_restart_calls_lxc_cmd_with_force_param(self, m_subp, force):
17+
"""Honor force param on restart."""
2318
instance = LXDInstance(name="my_vm")
2419
instance._do_restart(force=force) # pylint: disable=protected-access
25-
assert [mock.call(wait=True, force=force)] == m_shutdown.call_args_list
26-
assert [mock.call(wait=False)] == m_start.call_args_list
20+
if force:
21+
assert "--force" in m_subp.call_args[0][0]
22+
else:
23+
assert "--force" not in m_subp.call_args[0][0]
24+
25+
@mock.patch("pycloudlib.lxd.instance.LXDInstance.shutdown")
26+
@mock.patch("pycloudlib.lxd.instance.subp")
27+
def test_restart_does_not_shutdown(self, _m_subp, m_shutdown):
28+
"""Don't shutdown (stop) instance on restart."""
29+
instance = LXDInstance(name="my_vm")
30+
instance._do_restart() # pylint: disable=protected-access
31+
assert not m_shutdown.called
2732

2833

2934
class TestExecute:

0 commit comments

Comments
 (0)