|
1 | 1 | import { EventEmitter } from "node:events"; |
2 | 2 | import type { ContainerInfo } from "~/typings/docker"; |
3 | | -import type { Plugin, Hooks, PluginInfo } from "~/typings/plugin"; |
| 3 | +import type { Hooks, Plugin, PluginInfo } from "~/typings/plugin"; |
4 | 4 | import { logger } from "../utils/logger"; |
5 | 5 |
|
6 | 6 | class PluginManager extends EventEmitter { |
7 | | - private plugins: Map<string, Plugin> = new Map(); |
8 | | - |
9 | | - register(plugin: Plugin) { |
10 | | - try { |
11 | | - this.plugins.set(plugin.name, plugin); |
12 | | - logger.debug(`Registered plugin: ${plugin.name}`); |
13 | | - } catch (error) { |
14 | | - logger.error( |
15 | | - `Registering plugin ${plugin.name} failed: ${error as string}` |
16 | | - ); |
17 | | - } |
18 | | - } |
19 | | - |
20 | | - unregister(name: string) { |
21 | | - this.plugins.delete(name); |
22 | | - } |
23 | | - |
24 | | - getLoadedPlugins(): PluginInfo[] { |
25 | | - return Array.from(this.plugins.values()).map((plugin) => { |
26 | | - const hooks: Hooks = { |
27 | | - onContainerStart: !!plugin.onContainerStart, |
28 | | - onContainerStop: !!plugin.onContainerStop, |
29 | | - onContainerExit: !!plugin.onContainerExit, |
30 | | - onContainerCreate: !!plugin.onContainerCreate, |
31 | | - onContainerKill: !!plugin.onContainerKill, |
32 | | - handleContainerDie: !!plugin.handleContainerDie, |
33 | | - onContainerDestroy: !!plugin.onContainerDestroy, |
34 | | - onContainerPause: !!plugin.onContainerPause, |
35 | | - onContainerUnpause: !!plugin.onContainerUnpause, |
36 | | - onContainerRestart: !!plugin.onContainerRestart, |
37 | | - onContainerUpdate: !!plugin.onContainerUpdate, |
38 | | - onContainerRename: !!plugin.onContainerRename, |
39 | | - onContainerHealthStatus: !!plugin.onContainerHealthStatus, |
40 | | - onHostUnreachable: !!plugin.onHostUnreachable, |
41 | | - onHostReachableAgain: !!plugin.onHostReachableAgain, |
42 | | - }; |
43 | | - |
44 | | - return { |
45 | | - name: plugin.name, |
46 | | - version: plugin.version, |
47 | | - status: "active", |
48 | | - usedHooks: hooks, |
49 | | - }; |
50 | | - }); |
51 | | - } |
52 | | - |
53 | | - // Trigger plugin flows: |
54 | | - handleContainerStop(containerInfo: ContainerInfo) { |
55 | | - for (const [, plugin] of this.plugins) { |
56 | | - plugin.onContainerStop?.(containerInfo); |
57 | | - } |
58 | | - } |
59 | | - |
60 | | - handleContainerStart(containerInfo: ContainerInfo) { |
61 | | - for (const [, plugin] of this.plugins) { |
62 | | - plugin.onContainerStart?.(containerInfo); |
63 | | - } |
64 | | - } |
65 | | - |
66 | | - handleContainerExit(containerInfo: ContainerInfo) { |
67 | | - for (const [, plugin] of this.plugins) { |
68 | | - plugin.onContainerExit?.(containerInfo); |
69 | | - } |
70 | | - } |
71 | | - |
72 | | - handleContainerCreate(containerInfo: ContainerInfo) { |
73 | | - for (const [, plugin] of this.plugins) { |
74 | | - plugin.onContainerCreate?.(containerInfo); |
75 | | - } |
76 | | - } |
77 | | - |
78 | | - handleContainerDestroy(containerInfo: ContainerInfo) { |
79 | | - for (const [, plugin] of this.plugins) { |
80 | | - plugin.onContainerDestroy?.(containerInfo); |
81 | | - } |
82 | | - } |
83 | | - |
84 | | - handleContainerPause(containerInfo: ContainerInfo) { |
85 | | - for (const [, plugin] of this.plugins) { |
86 | | - plugin.onContainerPause?.(containerInfo); |
87 | | - } |
88 | | - } |
89 | | - |
90 | | - handleContainerUnpause(containerInfo: ContainerInfo) { |
91 | | - for (const [, plugin] of this.plugins) { |
92 | | - plugin.onContainerUnpause?.(containerInfo); |
93 | | - } |
94 | | - } |
95 | | - |
96 | | - handleContainerRestart(containerInfo: ContainerInfo) { |
97 | | - for (const [, plugin] of this.plugins) { |
98 | | - plugin.onContainerRestart?.(containerInfo); |
99 | | - } |
100 | | - } |
101 | | - |
102 | | - handleContainerUpdate(containerInfo: ContainerInfo) { |
103 | | - for (const [, plugin] of this.plugins) { |
104 | | - plugin.onContainerUpdate?.(containerInfo); |
105 | | - } |
106 | | - } |
107 | | - |
108 | | - handleContainerRename(containerInfo: ContainerInfo) { |
109 | | - for (const [, plugin] of this.plugins) { |
110 | | - plugin.onContainerRename?.(containerInfo); |
111 | | - } |
112 | | - } |
113 | | - |
114 | | - handleContainerHealthStatus(containerInfo: ContainerInfo) { |
115 | | - for (const [, plugin] of this.plugins) { |
116 | | - plugin.onContainerHealthStatus?.(containerInfo); |
117 | | - } |
118 | | - } |
119 | | - |
120 | | - handleHostUnreachable(host: string, err: string) { |
121 | | - for (const [, plugin] of this.plugins) { |
122 | | - plugin.onHostUnreachable?.(host, err); |
123 | | - } |
124 | | - } |
125 | | - |
126 | | - handleHostReachableAgain(host: string) { |
127 | | - for (const [, plugin] of this.plugins) { |
128 | | - plugin.onHostReachableAgain?.(host); |
129 | | - } |
130 | | - } |
131 | | - |
132 | | - handleContainerKill(containerInfo: ContainerInfo) { |
133 | | - for (const [, plugin] of this.plugins) { |
134 | | - plugin.onContainerKill?.(containerInfo); |
135 | | - } |
136 | | - } |
137 | | - |
138 | | - handleContainerDie(containerInfo: ContainerInfo) { |
139 | | - for (const [, plugin] of this.plugins) { |
140 | | - plugin.handleContainerDie?.(containerInfo); |
141 | | - } |
142 | | - } |
| 7 | + private plugins: Map<string, Plugin> = new Map(); |
| 8 | + |
| 9 | + register(plugin: Plugin) { |
| 10 | + try { |
| 11 | + this.plugins.set(plugin.name, plugin); |
| 12 | + logger.debug(`Registered plugin: ${plugin.name}`); |
| 13 | + } catch (error) { |
| 14 | + logger.error( |
| 15 | + `Registering plugin ${plugin.name} failed: ${error as string}`, |
| 16 | + ); |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + unregister(name: string) { |
| 21 | + this.plugins.delete(name); |
| 22 | + } |
| 23 | + |
| 24 | + getLoadedPlugins(): PluginInfo[] { |
| 25 | + return Array.from(this.plugins.values()).map((plugin) => { |
| 26 | + const hooks: Hooks = { |
| 27 | + onContainerStart: !!plugin.onContainerStart, |
| 28 | + onContainerStop: !!plugin.onContainerStop, |
| 29 | + onContainerExit: !!plugin.onContainerExit, |
| 30 | + onContainerCreate: !!plugin.onContainerCreate, |
| 31 | + onContainerKill: !!plugin.onContainerKill, |
| 32 | + handleContainerDie: !!plugin.handleContainerDie, |
| 33 | + onContainerDestroy: !!plugin.onContainerDestroy, |
| 34 | + onContainerPause: !!plugin.onContainerPause, |
| 35 | + onContainerUnpause: !!plugin.onContainerUnpause, |
| 36 | + onContainerRestart: !!plugin.onContainerRestart, |
| 37 | + onContainerUpdate: !!plugin.onContainerUpdate, |
| 38 | + onContainerRename: !!plugin.onContainerRename, |
| 39 | + onContainerHealthStatus: !!plugin.onContainerHealthStatus, |
| 40 | + onHostUnreachable: !!plugin.onHostUnreachable, |
| 41 | + onHostReachableAgain: !!plugin.onHostReachableAgain, |
| 42 | + }; |
| 43 | + |
| 44 | + return { |
| 45 | + name: plugin.name, |
| 46 | + version: plugin.version, |
| 47 | + status: "active", |
| 48 | + usedHooks: hooks, |
| 49 | + }; |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + // Trigger plugin flows: |
| 54 | + handleContainerStop(containerInfo: ContainerInfo) { |
| 55 | + for (const [, plugin] of this.plugins) { |
| 56 | + plugin.onContainerStop?.(containerInfo); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + handleContainerStart(containerInfo: ContainerInfo) { |
| 61 | + for (const [, plugin] of this.plugins) { |
| 62 | + plugin.onContainerStart?.(containerInfo); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + handleContainerExit(containerInfo: ContainerInfo) { |
| 67 | + for (const [, plugin] of this.plugins) { |
| 68 | + plugin.onContainerExit?.(containerInfo); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + handleContainerCreate(containerInfo: ContainerInfo) { |
| 73 | + for (const [, plugin] of this.plugins) { |
| 74 | + plugin.onContainerCreate?.(containerInfo); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + handleContainerDestroy(containerInfo: ContainerInfo) { |
| 79 | + for (const [, plugin] of this.plugins) { |
| 80 | + plugin.onContainerDestroy?.(containerInfo); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + handleContainerPause(containerInfo: ContainerInfo) { |
| 85 | + for (const [, plugin] of this.plugins) { |
| 86 | + plugin.onContainerPause?.(containerInfo); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + handleContainerUnpause(containerInfo: ContainerInfo) { |
| 91 | + for (const [, plugin] of this.plugins) { |
| 92 | + plugin.onContainerUnpause?.(containerInfo); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + handleContainerRestart(containerInfo: ContainerInfo) { |
| 97 | + for (const [, plugin] of this.plugins) { |
| 98 | + plugin.onContainerRestart?.(containerInfo); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + handleContainerUpdate(containerInfo: ContainerInfo) { |
| 103 | + for (const [, plugin] of this.plugins) { |
| 104 | + plugin.onContainerUpdate?.(containerInfo); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + handleContainerRename(containerInfo: ContainerInfo) { |
| 109 | + for (const [, plugin] of this.plugins) { |
| 110 | + plugin.onContainerRename?.(containerInfo); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + handleContainerHealthStatus(containerInfo: ContainerInfo) { |
| 115 | + for (const [, plugin] of this.plugins) { |
| 116 | + plugin.onContainerHealthStatus?.(containerInfo); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + handleHostUnreachable(host: string, err: string) { |
| 121 | + for (const [, plugin] of this.plugins) { |
| 122 | + plugin.onHostUnreachable?.(host, err); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + handleHostReachableAgain(host: string) { |
| 127 | + for (const [, plugin] of this.plugins) { |
| 128 | + plugin.onHostReachableAgain?.(host); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + handleContainerKill(containerInfo: ContainerInfo) { |
| 133 | + for (const [, plugin] of this.plugins) { |
| 134 | + plugin.onContainerKill?.(containerInfo); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + handleContainerDie(containerInfo: ContainerInfo) { |
| 139 | + for (const [, plugin] of this.plugins) { |
| 140 | + plugin.handleContainerDie?.(containerInfo); |
| 141 | + } |
| 142 | + } |
143 | 143 | } |
144 | 144 |
|
145 | 145 | export const pluginManager = new PluginManager(); |
0 commit comments