plugin app manager, app manager docker: update implementation:
A new `application_update` bridge method is added to easily update images. Running container are automatically updated if necessary.
This commit is contained in:
parent
6119c37c0f
commit
6b4f7252e3
1 changed files with 144 additions and 7 deletions
|
|
@ -17,12 +17,21 @@
|
|||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from pathlib import Path
|
||||
from typing import Literal
|
||||
|
||||
from pydantic import Field
|
||||
from twisted.python.procutils import which
|
||||
from libervia.backend.core.i18n import _
|
||||
from libervia.backend.core.constants import Const as C
|
||||
|
||||
from libervia.backend import G
|
||||
from libervia.backend.core import exceptions
|
||||
from libervia.backend.core.constants import Const as C
|
||||
from libervia.backend.core.i18n import _
|
||||
from libervia.backend.core.log import getLogger
|
||||
from libervia.backend.plugins.plugin_misc_app_manager.models import AppManagerBackend
|
||||
from libervia.backend.plugins.plugin_misc_app_manager.models import (
|
||||
AppManagerBackend,
|
||||
AppUpdateResult,
|
||||
AppUpdateServiceInfo,
|
||||
)
|
||||
from libervia.backend.tools.common import async_process
|
||||
from libervia.backend.tools.config import PluginSettings
|
||||
|
||||
|
|
@ -108,6 +117,63 @@ class AppManagerDocker(AppManagerBackend):
|
|||
path=str(working_dir),
|
||||
)
|
||||
|
||||
async def update(self, app_data: dict) -> AppUpdateResult | None:
|
||||
working_dir = app_data["_instance_dir_path"]
|
||||
old_versions = await self._get_images(working_dir)
|
||||
await async_process.run(
|
||||
self.runner_path,
|
||||
"compose",
|
||||
"pull",
|
||||
path=str(working_dir),
|
||||
)
|
||||
new_versions = await self._get_images(working_dir)
|
||||
updated = [
|
||||
AppUpdateServiceInfo(
|
||||
service=name, old_version=old_id, new_version=new_versions[name]
|
||||
)
|
||||
for name, old_id in old_versions.items()
|
||||
if name in new_versions and old_id != new_versions[name]
|
||||
]
|
||||
if not updated:
|
||||
return None
|
||||
if app_data.get("_started"):
|
||||
# Running: recreate containers with new images
|
||||
await async_process.run(
|
||||
self.runner_path,
|
||||
"compose",
|
||||
"up",
|
||||
"--detach",
|
||||
path=str(working_dir),
|
||||
)
|
||||
elif old_versions:
|
||||
# Stopped: recreate containers but don't start them
|
||||
await async_process.run(
|
||||
self.runner_path,
|
||||
"compose",
|
||||
"up",
|
||||
"--detach",
|
||||
"--no-start",
|
||||
path=str(working_dir),
|
||||
)
|
||||
return AppUpdateResult(updated=updated)
|
||||
|
||||
async def _get_images(self, working_dir: Path) -> dict[str, str]:
|
||||
output = await async_process.run(
|
||||
self.runner_path,
|
||||
"compose",
|
||||
"images",
|
||||
"--format",
|
||||
"{{.Name}}:{{.Image}}",
|
||||
path=str(working_dir),
|
||||
)
|
||||
images: dict[str, str] = {}
|
||||
for line in output.decode().strip().splitlines():
|
||||
if ":" not in line:
|
||||
continue
|
||||
name, image = line.split(":", 1)
|
||||
images[name] = image
|
||||
return images
|
||||
|
||||
async def compute_expose(self, app_data: dict) -> None:
|
||||
working_dir = app_data["_instance_dir_path"]
|
||||
expose = app_data["expose"]
|
||||
|
|
@ -132,7 +198,7 @@ class AppManagerDocker(AppManagerBackend):
|
|||
)
|
||||
exposed_port = exposed_port.decode().strip()
|
||||
try:
|
||||
addr, port = exposed_port.split(":")
|
||||
__, port = exposed_port.split(":")
|
||||
int(port)
|
||||
except ValueError:
|
||||
log.warning(
|
||||
|
|
|
|||
Loading…
Reference in a new issue