From 5c15b6558d870021e240f9f93b1a5bceda9aee21 Mon Sep 17 00:00:00 2001 From: Alex Lowe Date: Tue, 14 May 2024 14:13:11 +0200 Subject: [PATCH] EXPERIMENTAL feat: keep lxc instances running --- craft_providers/lxd/launcher.py | 28 +++++++++++++++------------- craft_providers/lxd/lxd_instance.py | 9 +++++++-- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/craft_providers/lxd/launcher.py b/craft_providers/lxd/launcher.py index 836b2d12..fa0af2fa 100644 --- a/craft_providers/lxd/launcher.py +++ b/craft_providers/lxd/launcher.py @@ -394,23 +394,25 @@ def _launch_existing_instance( if instance.is_running(): logger.debug("Instance exists and is running.") + logger.debug("Cancelling any pending shutdown.") + instance.execute_run(["shutdown", "-c"], capture_output=True) else: logger.debug("Instance exists and is not running. Starting instance.") instance.start() - try: - base_configuration.warmup(executor=instance) - except bases.BaseCompatibilityError as error: - # delete the instance so a new instance can be created - if auto_clean: - logger.debug( - "Cleaning incompatible instance %r (reason: %s).", - instance.instance_name, - error.reason, - ) - instance.delete() - return False - raise + try: + base_configuration.warmup(executor=instance) + except bases.BaseCompatibilityError as error: + # delete the instance so a new instance can be created + if auto_clean: + logger.debug( + "Cleaning incompatible instance %r (reason: %s).", + instance.instance_name, + error.reason, + ) + instance.delete() + return False + raise return True diff --git a/craft_providers/lxd/lxd_instance.py b/craft_providers/lxd/lxd_instance.py index 47d02e32..d16687ed 100644 --- a/craft_providers/lxd/lxd_instance.py +++ b/craft_providers/lxd/lxd_instance.py @@ -551,8 +551,13 @@ def stop(self) -> None: :raises LXDError: on unexpected error. """ - self.lxc.stop( - instance_name=self.instance_name, project=self.project, remote=self.remote + # self.lxc.stop( + # instance_name=self.instance_name, project=self.project, remote=self.remote + # ) + logger.debug("Leaving instance running for 15 minutes in case it's needed again.") + self.execute_run( + ["shutdown", "+15"], # TODO: make the time configurable. + capture_output=True, ) def supports_mount(self) -> bool: