plugin-workflow-portainer 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +176 -0
- package/README.md +31 -3
- package/client-v2.js +1 -0
- package/client.js +1 -0
- package/dist/client/127.09e6675863ac8138.js +10 -0
- package/dist/client/57.67f04a547ead06b8.js +10 -0
- package/dist/client/660.546c702d1ddf7eff.js +10 -0
- package/dist/client/981.e38099c59bf992f0.js +10 -0
- package/dist/client/index.js +10 -0
- package/dist/client-v2/127.9f65dc2e3d0df10a.js +10 -0
- package/dist/client-v2/57.a60e16e4b4e4fa28.js +10 -0
- package/dist/client-v2/660.29ae3c3b4040039d.js +10 -0
- package/dist/client-v2/981.8627062881ad83d1.js +10 -0
- package/dist/client-v2/index.js +10 -0
- package/dist/externalVersion.js +20 -0
- package/dist/index.js +48 -0
- package/dist/locale/en-US.json +193 -0
- package/dist/locale/vi-VN.json +193 -0
- package/dist/locale/zh-CN.json +193 -0
- package/dist/node_modules/joi/dist/joi-browser.min.js +1 -0
- package/dist/node_modules/joi/lib/annotate.js +175 -0
- package/dist/node_modules/joi/lib/base.js +1069 -0
- package/dist/node_modules/joi/lib/cache.js +143 -0
- package/dist/node_modules/joi/lib/common.js +216 -0
- package/dist/node_modules/joi/lib/compile.js +283 -0
- package/dist/node_modules/joi/lib/errors.js +271 -0
- package/dist/node_modules/joi/lib/extend.js +312 -0
- package/dist/node_modules/joi/lib/index.d.ts +2365 -0
- package/dist/node_modules/joi/lib/index.js +1 -0
- package/dist/node_modules/joi/lib/manifest.js +476 -0
- package/dist/node_modules/joi/lib/messages.js +207 -0
- package/dist/node_modules/joi/lib/modify.js +267 -0
- package/dist/node_modules/joi/lib/ref.js +414 -0
- package/dist/node_modules/joi/lib/schemas.js +302 -0
- package/dist/node_modules/joi/lib/state.js +166 -0
- package/dist/node_modules/joi/lib/template.js +463 -0
- package/dist/node_modules/joi/lib/trace.js +346 -0
- package/dist/node_modules/joi/lib/types/alternatives.js +364 -0
- package/dist/node_modules/joi/lib/types/any.js +174 -0
- package/dist/node_modules/joi/lib/types/array.js +809 -0
- package/dist/node_modules/joi/lib/types/binary.js +100 -0
- package/dist/node_modules/joi/lib/types/boolean.js +150 -0
- package/dist/node_modules/joi/lib/types/date.js +233 -0
- package/dist/node_modules/joi/lib/types/function.js +93 -0
- package/dist/node_modules/joi/lib/types/keys.js +1084 -0
- package/dist/node_modules/joi/lib/types/link.js +184 -0
- package/dist/node_modules/joi/lib/types/number.js +363 -0
- package/dist/node_modules/joi/lib/types/object.js +22 -0
- package/dist/node_modules/joi/lib/types/string.js +850 -0
- package/dist/node_modules/joi/lib/types/symbol.js +102 -0
- package/dist/node_modules/joi/lib/validator.js +750 -0
- package/dist/node_modules/joi/lib/values.js +263 -0
- package/dist/node_modules/joi/node_modules/@hapi/topo/lib/index.d.ts +60 -0
- package/dist/node_modules/joi/node_modules/@hapi/topo/lib/index.js +225 -0
- package/dist/node_modules/joi/node_modules/@hapi/topo/package.json +30 -0
- package/dist/node_modules/joi/package.json +1 -0
- package/dist/server/actions/portainer.js +317 -0
- package/dist/server/collections/portainer-instances.js +54 -0
- package/dist/server/index.js +54 -0
- package/dist/server/instructions/PortainerStackControlInstruction.js +156 -0
- package/dist/server/instructions/PortainerStackRedeployInstruction.js +254 -0
- package/dist/server/instructions/helpers.js +211 -0
- package/dist/server/plugin.js +177 -0
- package/dist/server/services/PortainerClient.js +669 -0
- package/dist/shared/collection.js +57 -0
- package/dist/shared/constants.js +66 -0
- package/dist/shared/env.js +238 -0
- package/dist/shared/types.js +24 -0
- package/package.json +50 -4
- package/server.js +1 -0
- package/src/client/index.tsx +50 -0
- package/src/client/instructions.tsx +76 -0
- package/src/client-v2/__tests__/workflow-registration.test.tsx +242 -0
- package/src/client-v2/components/ControlFieldset.tsx +46 -0
- package/src/client-v2/components/PortainerEnvEditor.tsx +223 -0
- package/src/client-v2/components/PortainerSettingsPage.tsx +707 -0
- package/src/client-v2/components/PortainerTargetFields.tsx +309 -0
- package/src/client-v2/components/RedeployFieldset.tsx +124 -0
- package/src/client-v2/components/StackDetailDrawer.tsx +790 -0
- package/src/client-v2/index.tsx +50 -0
- package/src/client-v2/instructions.tsx +162 -0
- package/src/client-v2/locale.ts +24 -0
- package/src/index.ts +11 -0
- package/src/locale/en-US.json +193 -0
- package/src/locale/vi-VN.json +193 -0
- package/src/locale/zh-CN.json +193 -0
- package/src/server/__tests__/portainer-workflow.test.ts +559 -0
- package/src/server/actions/portainer.ts +361 -0
- package/src/server/collections/portainer-instances.ts +34 -0
- package/src/server/index.ts +13 -0
- package/src/server/instructions/PortainerStackControlInstruction.ts +139 -0
- package/src/server/instructions/PortainerStackRedeployInstruction.ts +255 -0
- package/src/server/instructions/helpers.ts +237 -0
- package/src/server/plugin.ts +174 -0
- package/src/server/services/PortainerClient.ts +846 -0
- package/src/shared/collection.ts +32 -0
- package/src/shared/constants.ts +25 -0
- package/src/shared/env.ts +243 -0
- package/src/shared/types.ts +164 -0
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { Context, Next } from '@nocobase/actions';
|
|
11
|
+
import type Application from '@nocobase/server';
|
|
12
|
+
import { PLUGIN_NAMESPACE, PORTAINER_INSTANCES_COLLECTION, SECRET_MASK } from '../../shared/constants';
|
|
13
|
+
import { isLikelySensitiveKey } from '../../shared/env';
|
|
14
|
+
import type {
|
|
15
|
+
ID,
|
|
16
|
+
PortainerContainerSummary,
|
|
17
|
+
PortainerInstanceSummary,
|
|
18
|
+
PortainerStackOverview,
|
|
19
|
+
} from '../../shared/types';
|
|
20
|
+
import { decryptIfNeeded, resolvePortainerInstance } from '../instructions/helpers';
|
|
21
|
+
import {
|
|
22
|
+
assertPositiveInt,
|
|
23
|
+
assertValidContainerId,
|
|
24
|
+
clampTimeoutMs,
|
|
25
|
+
PortainerApiError,
|
|
26
|
+
PortainerClient,
|
|
27
|
+
summarizeContainersHealth,
|
|
28
|
+
} from '../services/PortainerClient';
|
|
29
|
+
|
|
30
|
+
function getRequestParam(ctx: Context, key: string): unknown {
|
|
31
|
+
const params = (ctx.action?.params ?? {}) as Record<string, unknown>;
|
|
32
|
+
const values =
|
|
33
|
+
typeof params.values === 'object' && params.values !== null ? (params.values as Record<string, unknown>) : {};
|
|
34
|
+
if (typeof values[key] !== 'undefined') {
|
|
35
|
+
return values[key];
|
|
36
|
+
}
|
|
37
|
+
return params[key];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function sendPortainerError(ctx: Context, error: unknown): never {
|
|
41
|
+
if (error instanceof PortainerApiError) {
|
|
42
|
+
const status =
|
|
43
|
+
error.code === 'INVALID_URL' || error.code === 'INVALID_PARAM' || error.code === 'MISSING_API_KEY'
|
|
44
|
+
? 400
|
|
45
|
+
: error.code === 'INSTANCE_NOT_FOUND'
|
|
46
|
+
? 404
|
|
47
|
+
: error.status && error.status >= 400 && error.status < 500
|
|
48
|
+
? error.status
|
|
49
|
+
: 502;
|
|
50
|
+
ctx.throw(status, error.message);
|
|
51
|
+
}
|
|
52
|
+
throw error;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function createPortainerResourceActions(app: Application) {
|
|
56
|
+
return {
|
|
57
|
+
async listInstances(ctx: Context, next: Next) {
|
|
58
|
+
const repo = app.db.getRepository(PORTAINER_INSTANCES_COLLECTION);
|
|
59
|
+
const rows = await repo.find({
|
|
60
|
+
filter: { enabled: true },
|
|
61
|
+
sort: ['-isDefault', 'id'],
|
|
62
|
+
});
|
|
63
|
+
const items: PortainerInstanceSummary[] = rows.map((row) => ({
|
|
64
|
+
id: Number(row.get('id')),
|
|
65
|
+
name: String(row.get('name') ?? ''),
|
|
66
|
+
baseUrl: String(row.get('baseUrl') ?? ''),
|
|
67
|
+
isDefault: Boolean(row.get('isDefault')),
|
|
68
|
+
enabled: Boolean(row.get('enabled')),
|
|
69
|
+
defaultTimeoutMs: Number(row.get('defaultTimeoutMs') ?? 600_000),
|
|
70
|
+
lastHealthStatus: (row.get('lastHealthStatus') as string | null) ?? null,
|
|
71
|
+
lastVersion: (row.get('lastVersion') as string | null) ?? null,
|
|
72
|
+
}));
|
|
73
|
+
ctx.body = items;
|
|
74
|
+
await next();
|
|
75
|
+
},
|
|
76
|
+
|
|
77
|
+
async testConnection(ctx: Context, next: Next) {
|
|
78
|
+
const filterByTk = (getRequestParam(ctx, 'filterByTk') ?? getRequestParam(ctx, 'instanceId')) as ID | undefined;
|
|
79
|
+
try {
|
|
80
|
+
const instance = await resolvePortainerInstance(app, filterByTk ?? null);
|
|
81
|
+
const status = await instance.client.getStatus();
|
|
82
|
+
const endpoints = await instance.client.listEndpoints();
|
|
83
|
+
const version = String(status?.Version ?? 'unknown');
|
|
84
|
+
|
|
85
|
+
await app.db.getRepository(PORTAINER_INSTANCES_COLLECTION).update({
|
|
86
|
+
filterByTk: instance.id,
|
|
87
|
+
values: {
|
|
88
|
+
lastHealthStatus: 'ok',
|
|
89
|
+
lastVersion: version,
|
|
90
|
+
lastCheckedAt: new Date(),
|
|
91
|
+
},
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
ctx.body = {
|
|
95
|
+
ok: true,
|
|
96
|
+
instanceId: instance.id,
|
|
97
|
+
version,
|
|
98
|
+
endpointCount: endpoints.length,
|
|
99
|
+
endpoints,
|
|
100
|
+
};
|
|
101
|
+
} catch (error) {
|
|
102
|
+
if (filterByTk) {
|
|
103
|
+
const numId = Number(filterByTk);
|
|
104
|
+
if (Number.isSafeInteger(numId) && numId > 0) {
|
|
105
|
+
await app.db
|
|
106
|
+
.getRepository(PORTAINER_INSTANCES_COLLECTION)
|
|
107
|
+
.update({
|
|
108
|
+
filterByTk: numId,
|
|
109
|
+
values: {
|
|
110
|
+
lastHealthStatus: 'error',
|
|
111
|
+
lastCheckedAt: new Date(),
|
|
112
|
+
},
|
|
113
|
+
})
|
|
114
|
+
.catch(() => {});
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
sendPortainerError(ctx, error);
|
|
118
|
+
}
|
|
119
|
+
await next();
|
|
120
|
+
},
|
|
121
|
+
|
|
122
|
+
async testConnectionDraft(ctx: Context, next: Next) {
|
|
123
|
+
try {
|
|
124
|
+
const baseUrl = String(getRequestParam(ctx, 'baseUrl') ?? '').trim();
|
|
125
|
+
let apiKey = String(getRequestParam(ctx, 'apiKey') ?? '').trim();
|
|
126
|
+
const instanceId = getRequestParam(ctx, 'id') ?? getRequestParam(ctx, 'instanceId');
|
|
127
|
+
const allowInsecureTls = Boolean(getRequestParam(ctx, 'allowInsecureTls'));
|
|
128
|
+
const timeoutMs = clampTimeoutMs(Number(getRequestParam(ctx, 'defaultTimeoutMs') ?? 15_000), 15_000);
|
|
129
|
+
|
|
130
|
+
if ((!apiKey || apiKey === SECRET_MASK) && instanceId) {
|
|
131
|
+
const existing = await app.db
|
|
132
|
+
.getRepository(PORTAINER_INSTANCES_COLLECTION)
|
|
133
|
+
.findOne({ filterByTk: Number(instanceId) });
|
|
134
|
+
if (existing) {
|
|
135
|
+
apiKey = await decryptIfNeeded(app, existing.get('apiKey'));
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const client = new PortainerClient({ baseUrl, apiKey, allowInsecureTls, timeoutMs });
|
|
140
|
+
const status = await client.getStatus();
|
|
141
|
+
const endpoints = await client.listEndpoints();
|
|
142
|
+
ctx.body = {
|
|
143
|
+
ok: true,
|
|
144
|
+
version: String(status?.Version ?? 'unknown'),
|
|
145
|
+
endpointCount: endpoints.length,
|
|
146
|
+
endpoints,
|
|
147
|
+
};
|
|
148
|
+
} catch (error) {
|
|
149
|
+
sendPortainerError(ctx, error);
|
|
150
|
+
}
|
|
151
|
+
await next();
|
|
152
|
+
},
|
|
153
|
+
|
|
154
|
+
async listEndpoints(ctx: Context, next: Next) {
|
|
155
|
+
const instanceId = getRequestParam(ctx, 'instanceId') as ID | undefined;
|
|
156
|
+
try {
|
|
157
|
+
const instance = await resolvePortainerInstance(app, instanceId ?? null);
|
|
158
|
+
ctx.body = await instance.client.listEndpoints();
|
|
159
|
+
} catch (error) {
|
|
160
|
+
sendPortainerError(ctx, error);
|
|
161
|
+
}
|
|
162
|
+
await next();
|
|
163
|
+
},
|
|
164
|
+
|
|
165
|
+
async listStacks(ctx: Context, next: Next) {
|
|
166
|
+
const instanceId = getRequestParam(ctx, 'instanceId') as ID | undefined;
|
|
167
|
+
const rawEndpointId = getRequestParam(ctx, 'endpointId');
|
|
168
|
+
try {
|
|
169
|
+
const instance = await resolvePortainerInstance(app, instanceId ?? null);
|
|
170
|
+
const endpointId =
|
|
171
|
+
rawEndpointId !== null && typeof rawEndpointId !== 'undefined' && String(rawEndpointId).trim() !== ''
|
|
172
|
+
? assertPositiveInt(rawEndpointId as ID, 'Environment (Endpoint) ID')
|
|
173
|
+
: null;
|
|
174
|
+
ctx.body = await instance.client.listStacks(endpointId);
|
|
175
|
+
} catch (error) {
|
|
176
|
+
sendPortainerError(ctx, error);
|
|
177
|
+
}
|
|
178
|
+
await next();
|
|
179
|
+
},
|
|
180
|
+
|
|
181
|
+
async getStackDetail(ctx: Context, next: Next) {
|
|
182
|
+
const instanceId = getRequestParam(ctx, 'instanceId') as ID | undefined;
|
|
183
|
+
const rawStackId = getRequestParam(ctx, 'stackId');
|
|
184
|
+
const maskValues =
|
|
185
|
+
getRequestParam(ctx, 'includeValues') !== true && getRequestParam(ctx, 'includeValues') !== 'true';
|
|
186
|
+
try {
|
|
187
|
+
if (rawStackId === null || typeof rawStackId === 'undefined' || String(rawStackId).trim() === '') {
|
|
188
|
+
ctx.throw(400, ctx.t('Stack ID is required', { ns: PLUGIN_NAMESPACE }));
|
|
189
|
+
}
|
|
190
|
+
const instance = await resolvePortainerInstance(app, instanceId ?? null);
|
|
191
|
+
const stackId = assertPositiveInt(rawStackId as ID, 'Stack ID');
|
|
192
|
+
const detail = await instance.client.getStackDetail(stackId);
|
|
193
|
+
ctx.body = {
|
|
194
|
+
...detail,
|
|
195
|
+
env: detail.env.map((pair) => ({
|
|
196
|
+
name: pair.name,
|
|
197
|
+
value: maskValues || isLikelySensitiveKey(pair.name) ? '' : pair.value,
|
|
198
|
+
hasExistingValue: pair.value.length > 0,
|
|
199
|
+
sensitive: isLikelySensitiveKey(pair.name),
|
|
200
|
+
})),
|
|
201
|
+
};
|
|
202
|
+
} catch (error) {
|
|
203
|
+
sendPortainerError(ctx, error);
|
|
204
|
+
}
|
|
205
|
+
await next();
|
|
206
|
+
},
|
|
207
|
+
|
|
208
|
+
async getStackOverview(ctx: Context, next: Next) {
|
|
209
|
+
const instanceId = getRequestParam(ctx, 'instanceId') as ID | undefined;
|
|
210
|
+
const rawStackId = getRequestParam(ctx, 'stackId');
|
|
211
|
+
const includeStatsParam = getRequestParam(ctx, 'includeStats');
|
|
212
|
+
const includeFileParam = getRequestParam(ctx, 'includeFile');
|
|
213
|
+
const includeStats = includeStatsParam !== false && includeStatsParam !== 'false';
|
|
214
|
+
const includeFile = includeFileParam !== false && includeFileParam !== 'false';
|
|
215
|
+
|
|
216
|
+
try {
|
|
217
|
+
if (rawStackId === null || typeof rawStackId === 'undefined' || String(rawStackId).trim() === '') {
|
|
218
|
+
ctx.throw(400, ctx.t('Stack ID is required', { ns: PLUGIN_NAMESPACE }));
|
|
219
|
+
}
|
|
220
|
+
const instance = await resolvePortainerInstance(app, instanceId ?? null);
|
|
221
|
+
const stackId = assertPositiveInt(rawStackId as ID, 'Stack ID');
|
|
222
|
+
|
|
223
|
+
const [detail, stackFileContent] = await Promise.all([
|
|
224
|
+
instance.client.getStackDetail(stackId),
|
|
225
|
+
includeFile ? instance.client.getStackFile(stackId).catch(() => '') : Promise.resolve(''),
|
|
226
|
+
]);
|
|
227
|
+
|
|
228
|
+
const baseContainers = await instance.client
|
|
229
|
+
.listStackContainers(detail.endpointId, detail.name)
|
|
230
|
+
.catch((): PortainerContainerSummary[] => []);
|
|
231
|
+
|
|
232
|
+
const containers: PortainerContainerSummary[] = includeStats
|
|
233
|
+
? await Promise.all(
|
|
234
|
+
baseContainers.map(async (container) => {
|
|
235
|
+
if (container.state !== 'running') {
|
|
236
|
+
return { ...container, stats: null };
|
|
237
|
+
}
|
|
238
|
+
const stats = await instance.client
|
|
239
|
+
.getContainerLiveStats(detail.endpointId, container.id)
|
|
240
|
+
.catch(() => null);
|
|
241
|
+
return { ...container, stats };
|
|
242
|
+
}),
|
|
243
|
+
)
|
|
244
|
+
: baseContainers;
|
|
245
|
+
|
|
246
|
+
const health = summarizeContainersHealth(containers);
|
|
247
|
+
let totalCpuPercent = 0;
|
|
248
|
+
let totalMemUsageBytes = 0;
|
|
249
|
+
let memLimitBytes = 0;
|
|
250
|
+
let totalNetRxBytes = 0;
|
|
251
|
+
let totalNetTxBytes = 0;
|
|
252
|
+
|
|
253
|
+
for (const item of containers) {
|
|
254
|
+
if (item.stats) {
|
|
255
|
+
totalCpuPercent += item.stats.cpuPercent;
|
|
256
|
+
totalMemUsageBytes += item.stats.memUsageBytes;
|
|
257
|
+
if (item.stats.memLimitBytes > memLimitBytes) {
|
|
258
|
+
memLimitBytes = item.stats.memLimitBytes;
|
|
259
|
+
}
|
|
260
|
+
totalNetRxBytes += item.stats.netRxBytes;
|
|
261
|
+
totalNetTxBytes += item.stats.netTxBytes;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const totalMemPercent =
|
|
266
|
+
memLimitBytes > 0 ? Math.round(((totalMemUsageBytes / memLimitBytes) * 100 + Number.EPSILON) * 100) / 100 : 0;
|
|
267
|
+
|
|
268
|
+
const overview: PortainerStackOverview = {
|
|
269
|
+
detail: {
|
|
270
|
+
...detail,
|
|
271
|
+
env: detail.env.map((pair) => {
|
|
272
|
+
const sensitive = isLikelySensitiveKey(pair.name);
|
|
273
|
+
return {
|
|
274
|
+
name: pair.name,
|
|
275
|
+
value: sensitive ? SECRET_MASK : pair.value,
|
|
276
|
+
hasExistingValue: pair.value.length > 0,
|
|
277
|
+
sensitive,
|
|
278
|
+
};
|
|
279
|
+
}),
|
|
280
|
+
},
|
|
281
|
+
stackFileContent,
|
|
282
|
+
containers,
|
|
283
|
+
summary: {
|
|
284
|
+
...health,
|
|
285
|
+
totalCpuPercent: Math.round((totalCpuPercent + Number.EPSILON) * 100) / 100,
|
|
286
|
+
totalMemUsageBytes,
|
|
287
|
+
memLimitBytes,
|
|
288
|
+
totalMemPercent,
|
|
289
|
+
totalNetRxBytes,
|
|
290
|
+
totalNetTxBytes,
|
|
291
|
+
},
|
|
292
|
+
sampledAt: new Date().toISOString(),
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
ctx.body = overview;
|
|
296
|
+
} catch (error) {
|
|
297
|
+
sendPortainerError(ctx, error);
|
|
298
|
+
}
|
|
299
|
+
await next();
|
|
300
|
+
},
|
|
301
|
+
|
|
302
|
+
async getContainerLogs(ctx: Context, next: Next) {
|
|
303
|
+
const instanceId = getRequestParam(ctx, 'instanceId') as ID | undefined;
|
|
304
|
+
const rawEndpointId = getRequestParam(ctx, 'endpointId');
|
|
305
|
+
const rawContainerId = getRequestParam(ctx, 'containerId');
|
|
306
|
+
const rawTail = getRequestParam(ctx, 'tail');
|
|
307
|
+
|
|
308
|
+
try {
|
|
309
|
+
const instance = await resolvePortainerInstance(app, instanceId ?? null);
|
|
310
|
+
const endpointId = assertPositiveInt(rawEndpointId as ID, 'Environment (Endpoint) ID');
|
|
311
|
+
const containerId = assertValidContainerId(String(rawContainerId ?? ''));
|
|
312
|
+
const tail = Math.min(1000, Math.max(10, Math.trunc(Number(rawTail) || 100)));
|
|
313
|
+
const logs = await instance.client.getContainerLogs(endpointId, containerId, tail);
|
|
314
|
+
ctx.body = {
|
|
315
|
+
containerId,
|
|
316
|
+
tail,
|
|
317
|
+
logs,
|
|
318
|
+
fetchedAt: new Date().toISOString(),
|
|
319
|
+
};
|
|
320
|
+
} catch (error) {
|
|
321
|
+
sendPortainerError(ctx, error);
|
|
322
|
+
}
|
|
323
|
+
await next();
|
|
324
|
+
},
|
|
325
|
+
|
|
326
|
+
async controlStack(ctx: Context, next: Next) {
|
|
327
|
+
const instanceId = getRequestParam(ctx, 'instanceId') as ID | undefined;
|
|
328
|
+
const rawEndpointId = getRequestParam(ctx, 'endpointId');
|
|
329
|
+
const rawStackId = getRequestParam(ctx, 'stackId');
|
|
330
|
+
const operation = String(getRequestParam(ctx, 'operation') ?? '')
|
|
331
|
+
.trim()
|
|
332
|
+
.toLowerCase();
|
|
333
|
+
|
|
334
|
+
try {
|
|
335
|
+
if (operation !== 'start' && operation !== 'stop') {
|
|
336
|
+
ctx.throw(400, ctx.t('Operation must be start or stop', { ns: PLUGIN_NAMESPACE }));
|
|
337
|
+
}
|
|
338
|
+
const instance = await resolvePortainerInstance(app, instanceId ?? null);
|
|
339
|
+
const endpointId = assertPositiveInt(rawEndpointId as ID, 'Environment (Endpoint) ID');
|
|
340
|
+
const stackId = assertPositiveInt(rawStackId as ID, 'Stack ID');
|
|
341
|
+
|
|
342
|
+
const initial =
|
|
343
|
+
operation === 'start'
|
|
344
|
+
? await instance.client.startStack(stackId, endpointId)
|
|
345
|
+
: await instance.client.stopStack(stackId, endpointId);
|
|
346
|
+
const settled = await instance.client.waitForStackSettlement(stackId, initial, { timeoutMs: 60_000 });
|
|
347
|
+
|
|
348
|
+
ctx.body = {
|
|
349
|
+
ok: true,
|
|
350
|
+
stackId,
|
|
351
|
+
endpointId,
|
|
352
|
+
operation,
|
|
353
|
+
status: Number(settled?.Status ?? (operation === 'start' ? 1 : 2)),
|
|
354
|
+
};
|
|
355
|
+
} catch (error) {
|
|
356
|
+
sendPortainerError(ctx, error);
|
|
357
|
+
}
|
|
358
|
+
await next();
|
|
359
|
+
},
|
|
360
|
+
};
|
|
361
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { CollectionOptions } from '@nocobase/database';
|
|
11
|
+
import { DEFAULT_TIMEOUT_MS, PORTAINER_INSTANCES_COLLECTION } from '../../shared/constants';
|
|
12
|
+
|
|
13
|
+
export default {
|
|
14
|
+
name: PORTAINER_INSTANCES_COLLECTION,
|
|
15
|
+
title: 'Portainer Instances',
|
|
16
|
+
fields: [
|
|
17
|
+
{ name: 'id', type: 'bigInt', autoIncrement: true, primaryKey: true },
|
|
18
|
+
{ name: 'name', type: 'string', length: 200 },
|
|
19
|
+
{ name: 'baseUrl', type: 'string', length: 500 },
|
|
20
|
+
{ name: 'apiKey', type: 'text' },
|
|
21
|
+
{ name: 'gitUsername', type: 'string', length: 200 },
|
|
22
|
+
{ name: 'gitAccessToken', type: 'text' },
|
|
23
|
+
{ name: 'gitCredentialId', type: 'integer' },
|
|
24
|
+
{ name: 'allowInsecureTls', type: 'boolean', defaultValue: false },
|
|
25
|
+
{ name: 'defaultTimeoutMs', type: 'integer', defaultValue: DEFAULT_TIMEOUT_MS },
|
|
26
|
+
{ name: 'isDefault', type: 'boolean', defaultValue: false },
|
|
27
|
+
{ name: 'enabled', type: 'boolean', defaultValue: true },
|
|
28
|
+
{ name: 'lastHealthStatus', type: 'string', length: 32 },
|
|
29
|
+
{ name: 'lastVersion', type: 'string', length: 64 },
|
|
30
|
+
{ name: 'lastCheckedAt', type: 'date' },
|
|
31
|
+
{ name: 'createdAt', type: 'date' },
|
|
32
|
+
{ name: 'updatedAt', type: 'date' },
|
|
33
|
+
],
|
|
34
|
+
} as CollectionOptions;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export { default, PluginWorkflowPortainerServer } from './plugin';
|
|
11
|
+
export * from './services/PortainerClient';
|
|
12
|
+
export * from './instructions/PortainerStackRedeployInstruction';
|
|
13
|
+
export * from './instructions/PortainerStackControlInstruction';
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import Joi from 'joi';
|
|
11
|
+
import { Instruction, JOB_STATUS, type FlowNodeModel, type JobModel, type Processor } from '@nocobase/plugin-workflow';
|
|
12
|
+
import { buildEffectiveStackEnv } from '../../shared/env';
|
|
13
|
+
import type { PortainerContainerSummary, PortainerStackControlConfig, StackControlAction } from '../../shared/types';
|
|
14
|
+
import { assertPositiveInt, summarizeContainersHealth, toStackDetail } from '../services/PortainerClient';
|
|
15
|
+
import { formatFailureResult, resolvePortainerInstance, runPortainerWorkflowTask } from './helpers';
|
|
16
|
+
|
|
17
|
+
const numOrVar = Joi.alternatives().try(Joi.number(), Joi.string().allow(''), Joi.allow(null));
|
|
18
|
+
|
|
19
|
+
export async function executePortainerControl(
|
|
20
|
+
app: Parameters<typeof resolvePortainerInstance>[0],
|
|
21
|
+
config: PortainerStackControlConfig,
|
|
22
|
+
signal?: AbortSignal,
|
|
23
|
+
) {
|
|
24
|
+
const startedAt = new Date();
|
|
25
|
+
const instance = await resolvePortainerInstance(app, config.instanceId ?? null);
|
|
26
|
+
const stackId = assertPositiveInt(config.stackId ?? undefined, 'Stack ID');
|
|
27
|
+
const action: StackControlAction = config.action === 'start' || config.action === 'stop' ? config.action : 'inspect';
|
|
28
|
+
|
|
29
|
+
let rawStack = await instance.client.getRawStack(stackId, signal);
|
|
30
|
+
const hasExplicitEndpoint =
|
|
31
|
+
config.endpointId !== null && typeof config.endpointId !== 'undefined' && String(config.endpointId).trim() !== '';
|
|
32
|
+
const endpointId = hasExplicitEndpoint
|
|
33
|
+
? assertPositiveInt(config.endpointId ?? undefined, 'Environment (Endpoint) ID')
|
|
34
|
+
: assertPositiveInt(rawStack.EndpointId, 'Stack Endpoint ID');
|
|
35
|
+
|
|
36
|
+
if (action === 'start') {
|
|
37
|
+
rawStack = await instance.client.startStack(stackId, endpointId, signal);
|
|
38
|
+
rawStack = await instance.client.waitForStackSettlement(stackId, rawStack, {
|
|
39
|
+
timeoutMs: config.timeoutMs ? Number(config.timeoutMs) : instance.defaultTimeoutMs,
|
|
40
|
+
signal,
|
|
41
|
+
});
|
|
42
|
+
} else if (action === 'stop') {
|
|
43
|
+
rawStack = await instance.client.stopStack(stackId, endpointId, signal);
|
|
44
|
+
rawStack = await instance.client.waitForStackSettlement(stackId, rawStack, {
|
|
45
|
+
timeoutMs: config.timeoutMs ? Number(config.timeoutMs) : instance.defaultTimeoutMs,
|
|
46
|
+
signal,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const detail = toStackDetail(rawStack);
|
|
51
|
+
const { maskedEnv } = buildEffectiveStackEnv({
|
|
52
|
+
existingEnv: detail.env,
|
|
53
|
+
strategy: 'merge',
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
let containers: PortainerContainerSummary[] = [];
|
|
57
|
+
try {
|
|
58
|
+
containers = await instance.client.listStackContainers(endpointId, detail.name, signal);
|
|
59
|
+
} catch {
|
|
60
|
+
containers = [];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const summary = summarizeContainersHealth(containers);
|
|
64
|
+
const stackStatus = Number(rawStack.Status ?? detail.status ?? 1);
|
|
65
|
+
const healthy =
|
|
66
|
+
action === 'stop' ? summary.runningCount === 0 : containers.length > 0 ? summary.healthy : stackStatus === 1;
|
|
67
|
+
|
|
68
|
+
const finishedAt = new Date();
|
|
69
|
+
return {
|
|
70
|
+
ok: true,
|
|
71
|
+
action,
|
|
72
|
+
instanceId: instance.id,
|
|
73
|
+
instanceName: instance.name,
|
|
74
|
+
endpointId,
|
|
75
|
+
stackId,
|
|
76
|
+
stackName: detail.name,
|
|
77
|
+
stackType: detail.isGitStack ? ('git' as const) : ('compose' as const),
|
|
78
|
+
stackStatus,
|
|
79
|
+
gitReference: detail.gitReference ?? null,
|
|
80
|
+
healthy,
|
|
81
|
+
runningCount: summary.runningCount,
|
|
82
|
+
totalCount: summary.totalCount,
|
|
83
|
+
containers,
|
|
84
|
+
currentEnv: maskedEnv,
|
|
85
|
+
startedAt: startedAt.toISOString(),
|
|
86
|
+
finishedAt: finishedAt.toISOString(),
|
|
87
|
+
durationMs: Math.max(0, finishedAt.getTime() - startedAt.getTime()),
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export class PortainerStackControlInstruction extends Instruction {
|
|
92
|
+
configSchema = Joi.object({
|
|
93
|
+
instanceId: numOrVar,
|
|
94
|
+
endpointId: numOrVar,
|
|
95
|
+
stackId: numOrVar,
|
|
96
|
+
action: Joi.string().valid('inspect', 'start', 'stop').default('inspect'),
|
|
97
|
+
timeoutMs: numOrVar,
|
|
98
|
+
ignoreFail: Joi.boolean().default(false),
|
|
99
|
+
}).unknown(true);
|
|
100
|
+
|
|
101
|
+
async run(node: FlowNodeModel, prevJob: JobModel, processor: Processor, options?: { signal?: AbortSignal }) {
|
|
102
|
+
const config = processor.getParsedValue(node.config ?? {}, node.id) as PortainerStackControlConfig;
|
|
103
|
+
return runPortainerWorkflowTask({
|
|
104
|
+
workflow: this.workflow,
|
|
105
|
+
node,
|
|
106
|
+
prevJob,
|
|
107
|
+
processor,
|
|
108
|
+
ignoreFail: config.ignoreFail,
|
|
109
|
+
signal: options?.signal,
|
|
110
|
+
execute: (signal) => executePortainerControl(this.workflow.app, config, signal),
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async resume(node: FlowNodeModel, job: JobModel) {
|
|
115
|
+
const config = (node.config ?? {}) as PortainerStackControlConfig;
|
|
116
|
+
if (config.ignoreFail) {
|
|
117
|
+
job.set('status', JOB_STATUS.RESOLVED);
|
|
118
|
+
}
|
|
119
|
+
return job;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async test(config: PortainerStackControlConfig) {
|
|
123
|
+
const startedAt = new Date();
|
|
124
|
+
try {
|
|
125
|
+
const result = await executePortainerControl(this.workflow.app, config);
|
|
126
|
+
return {
|
|
127
|
+
status: JOB_STATUS.RESOLVED,
|
|
128
|
+
result,
|
|
129
|
+
};
|
|
130
|
+
} catch (error) {
|
|
131
|
+
return {
|
|
132
|
+
status: config.ignoreFail ? JOB_STATUS.RESOLVED : JOB_STATUS.FAILED,
|
|
133
|
+
result: formatFailureResult(error, startedAt),
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export default PortainerStackControlInstruction;
|