annotate libervia/backend/plugins/plugin_app_manager_docker/__init__.py @ 4254:2b2a5348bd61

app (peertube): environment variable must be strings (bis)
author Goffi <goffi@goffi.org>
date Fri, 31 May 2024 11:24:12 +0200
parents 4aa62767f501
children 0d7bb4df2343
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3373
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python3
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
2
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
3 # SàT plugin to manage Docker
3479
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3382
diff changeset
4 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
3373
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
5
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # This program is free software: you can redistribute it and/or modify
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # it under the terms of the GNU Affero General Public License as published by
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # the Free Software Foundation, either version 3 of the License, or
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # (at your option) any later version.
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
10
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # This program is distributed in the hope that it will be useful,
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # GNU Affero General Public License for more details.
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
15
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # You should have received a copy of the GNU Affero General Public License
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
18
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
19 from pathlib import Path
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from twisted.python.procutils import which
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 3998
diff changeset
21 from libervia.backend.core.i18n import _
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 3998
diff changeset
22 from libervia.backend.core.constants import Const as C
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 3998
diff changeset
23 from libervia.backend.core import exceptions
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 3998
diff changeset
24 from libervia.backend.core.log import getLogger
4247
4aa62767f501 plugin app manager: various improvements:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
25 from libervia.backend.plugins.plugin_misc_app_manager.models import AppManagerBackend
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 3998
diff changeset
26 from libervia.backend.tools.common import async_process
3373
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
27
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
28 log = getLogger(__name__)
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
29
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
30
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
31 PLUGIN_INFO = {
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
32 C.PI_NAME: "Docker Applications Manager",
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
33 C.PI_IMPORT_NAME: "APP_MANAGER_DOCKER",
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
34 C.PI_TYPE: C.PLUG_TYPE_MISC,
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
35 C.PI_MODES: C.PLUG_MODE_BOTH,
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
36 C.PI_DEPENDENCIES: ["APP_MANAGER"],
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
37 C.PI_MAIN: "AppManagerDocker",
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
38 C.PI_HANDLER: "no",
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
39 C.PI_DESCRIPTION: _(
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
40 """Applications Manager for Docker"""),
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
41 }
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
42
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
43
4247
4aa62767f501 plugin app manager: various improvements:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
44 class AppManagerDocker(AppManagerBackend):
3373
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
45 name = "docker-compose"
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
46 discover_path = Path(__file__).parent
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
47
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
48 def __init__(self, host):
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
49 log.info(_("Docker App Manager initialization"))
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
50 try:
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
51 self.docker_compose_path = which('docker-compose')[0]
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
52 except IndexError:
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
53 raise exceptions.NotFound(
3382
3b08caa805e7 plugin app manager docker: typo
Goffi <goffi@goffi.org>
parents: 3373
diff changeset
54 '"docker-compose" executable not found, Docker can\'t be used with '
3373
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
55 'application manager')
4247
4aa62767f501 plugin app manager: various improvements:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
56 super().__init__(host)
3373
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
57
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
58 async def start(self, app_data: dict) -> None:
3998
402d31527af4 plugin app manager: `start` doesn't wait anymore for actual app start:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
59 await self._am.start_common(app_data)
3373
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
60 working_dir = app_data['_instance_dir_path']
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
61 try:
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
62 override = app_data['override']
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
63 except KeyError:
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
64 pass
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
65 else:
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
66 log.debug("writting override file")
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
67 override_path = working_dir / "docker-compose.override.yml"
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
68 with override_path.open("w") as f:
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
69 self._am.dump(override, f)
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
70 await async_process.run(
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
71 self.docker_compose_path,
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
72 "up",
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
73 "--detach",
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
74 path=str(working_dir),
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
75 )
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
76
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
77 async def stop(self, app_data: dict) -> None:
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
78 working_dir = app_data['_instance_dir_path']
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
79 await async_process.run(
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
80 self.docker_compose_path,
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
81 "down",
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
82 path=str(working_dir),
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
83 )
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
84
4247
4aa62767f501 plugin app manager: various improvements:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
85 async def compute_expose(self, app_data: dict) -> None:
3373
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
86 working_dir = app_data['_instance_dir_path']
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
87 expose = app_data['expose']
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
88 ports = expose.get('ports', {})
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
89 for name, port_data in list(ports.items()):
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
90 try:
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
91 service = port_data['service']
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
92 private = port_data['private']
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
93 int(private)
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
94 except (KeyError, ValueError):
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
95 log.warning(
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
96 f"invalid value found for {name!r} port in {app_data['_file_path']}")
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
97 continue
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
98 exposed_port = await async_process.run(
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
99 self.docker_compose_path,
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
100 "port",
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
101 service,
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
102 str(private),
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
103 path=str(working_dir),
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
104 )
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
105 exposed_port = exposed_port.decode().strip()
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
106 try:
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
107 addr, port = exposed_port.split(':')
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
108 int(port)
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
109 except ValueError:
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
110 log.warning(
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
111 f"invalid exposed port for {name}, ignoring: {exposed_port!r}")
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
112 del ports[name]
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
113 else:
f44402f8a81f plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff changeset
114 ports[name] = exposed_port