Mercurial > libervia-backend
annotate libervia/backend/plugins/plugin_app_manager_docker/__init__.py @ 4309:b56b1eae7994
component email gateway: add multicasting:
XEP-0033 multicasting is now supported both for incoming and outgoing messages. XEP-0033
metadata are converted to suitable Email headers and vice versa.
Email address and JID are both supported, and delivery is done by the gateway when
suitable on incoming messages.
rel 450
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 26 Sep 2024 16:12:01 +0200 |
parents | 0d7bb4df2343 |
children |
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 | 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", |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4247
diff
changeset
|
39 C.PI_DESCRIPTION: _("""Applications Manager for Docker"""), |
3373
f44402f8a81f
plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 } |
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 |
4247
4aa62767f501
plugin app manager: various improvements:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
43 class AppManagerDocker(AppManagerBackend): |
3373
f44402f8a81f
plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 name = "docker-compose" |
f44402f8a81f
plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 discover_path = Path(__file__).parent |
f44402f8a81f
plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 |
f44402f8a81f
plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 def __init__(self, host): |
f44402f8a81f
plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 log.info(_("Docker App Manager initialization")) |
f44402f8a81f
plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 try: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4247
diff
changeset
|
50 self.docker_compose_path = which("docker-compose")[0] |
3373
f44402f8a81f
plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 except IndexError: |
f44402f8a81f
plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 raise exceptions.NotFound( |
3382 | 53 '"docker-compose" executable not found, Docker can\'t be used with ' |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4247
diff
changeset
|
54 "application manager" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4247
diff
changeset
|
55 ) |
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) |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4247
diff
changeset
|
60 working_dir = app_data["_instance_dir_path"] |
3373
f44402f8a81f
plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 try: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4247
diff
changeset
|
62 override = app_data["override"] |
3373
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: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4247
diff
changeset
|
78 working_dir = app_data["_instance_dir_path"] |
3373
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: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4247
diff
changeset
|
86 working_dir = app_data["_instance_dir_path"] |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4247
diff
changeset
|
87 expose = app_data["expose"] |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4247
diff
changeset
|
88 ports = expose.get("ports", {}) |
3373
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: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4247
diff
changeset
|
91 service = port_data["service"] |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4247
diff
changeset
|
92 private = port_data["private"] |
3373
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( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4247
diff
changeset
|
96 f"invalid value found for {name!r} port in {app_data['_file_path']}" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4247
diff
changeset
|
97 ) |
3373
f44402f8a81f
plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 continue |
f44402f8a81f
plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 exposed_port = await async_process.run( |
f44402f8a81f
plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 self.docker_compose_path, |
f44402f8a81f
plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 "port", |
f44402f8a81f
plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 service, |
f44402f8a81f
plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 str(private), |
f44402f8a81f
plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 path=str(working_dir), |
f44402f8a81f
plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 ) |
f44402f8a81f
plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
106 exposed_port = exposed_port.decode().strip() |
f44402f8a81f
plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 try: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4247
diff
changeset
|
108 addr, port = exposed_port.split(":") |
3373
f44402f8a81f
plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
109 int(port) |
f44402f8a81f
plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
110 except ValueError: |
f44402f8a81f
plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
111 log.warning( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4247
diff
changeset
|
112 f"invalid exposed port for {name}, ignoring: {exposed_port!r}" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4247
diff
changeset
|
113 ) |
3373
f44402f8a81f
plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
114 del ports[name] |
f44402f8a81f
plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 else: |
f44402f8a81f
plugin app managed docker: handle Docker application with App Manager
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
116 ports[name] = exposed_port |