plugin-workflow-portainer 0.0.0-stage → 0.1.1
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.5012da93825a6e5c.js +10 -0
- package/dist/client/57.ace9538136438718.js +10 -0
- package/dist/client/65.ec4a55173511c06d.js +10 -0
- package/dist/client/660.546c702d1ddf7eff.js +10 -0
- package/dist/client/index.js +10 -0
- package/dist/client-v2/127.0ec4dce7b13750e9.js +10 -0
- package/dist/client-v2/57.e7cfc50fb651ac14.js +10 -0
- package/dist/client-v2/65.9c745ea32ac96e98.js +10 -0
- package/dist/client-v2/660.29ae3c3b4040039d.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 +151 -0
- package/dist/locale/vi-VN.json +151 -0
- package/dist/locale/zh-CN.json +151 -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 +192 -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 +170 -0
- package/dist/server/services/PortainerClient.js +497 -0
- package/dist/shared/collection.js +57 -0
- package/dist/shared/constants.js +66 -0
- package/dist/shared/env.js +221 -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 +132 -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 +671 -0
- package/src/client-v2/components/PortainerTargetFields.tsx +317 -0
- package/src/client-v2/components/RedeployFieldset.tsx +123 -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 +151 -0
- package/src/locale/vi-VN.json +151 -0
- package/src/locale/zh-CN.json +151 -0
- package/src/server/__tests__/portainer-workflow.test.ts +455 -0
- package/src/server/actions/portainer.ts +196 -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 +167 -0
- package/src/server/services/PortainerClient.ts +589 -0
- package/src/shared/collection.ts +32 -0
- package/src/shared/constants.ts +25 -0
- package/src/shared/env.ts +228 -0
- package/src/shared/types.ts +105 -0
|
@@ -0,0 +1,254 @@
|
|
|
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
|
+
var __create = Object.create;
|
|
11
|
+
var __defProp = Object.defineProperty;
|
|
12
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
13
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
14
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
15
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
16
|
+
var __export = (target, all) => {
|
|
17
|
+
for (var name in all)
|
|
18
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
19
|
+
};
|
|
20
|
+
var __copyProps = (to, from, except, desc) => {
|
|
21
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
22
|
+
for (let key of __getOwnPropNames(from))
|
|
23
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
24
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
25
|
+
}
|
|
26
|
+
return to;
|
|
27
|
+
};
|
|
28
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
29
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
30
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
31
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
32
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
33
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
34
|
+
mod
|
|
35
|
+
));
|
|
36
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
37
|
+
var PortainerStackRedeployInstruction_exports = {};
|
|
38
|
+
__export(PortainerStackRedeployInstruction_exports, {
|
|
39
|
+
PortainerStackRedeployInstruction: () => PortainerStackRedeployInstruction,
|
|
40
|
+
default: () => PortainerStackRedeployInstruction_default,
|
|
41
|
+
executePortainerRedeploy: () => executePortainerRedeploy
|
|
42
|
+
});
|
|
43
|
+
module.exports = __toCommonJS(PortainerStackRedeployInstruction_exports);
|
|
44
|
+
var import_joi = __toESM(require("joi"));
|
|
45
|
+
var import_plugin_workflow = require("@nocobase/plugin-workflow");
|
|
46
|
+
var import_env = require("../../shared/env");
|
|
47
|
+
var import_PortainerClient = require("../services/PortainerClient");
|
|
48
|
+
var import_helpers = require("./helpers");
|
|
49
|
+
const numOrVar = import_joi.default.alternatives().try(import_joi.default.number(), import_joi.default.string().allow(""), import_joi.default.allow(null));
|
|
50
|
+
function resolveGitAuthPayload(stack, instance) {
|
|
51
|
+
var _a;
|
|
52
|
+
const stackAuth = (_a = stack.GitConfig) == null ? void 0 : _a.Authentication;
|
|
53
|
+
const stackCredId = Number((stackAuth == null ? void 0 : stackAuth.GitCredentialID) ?? 0);
|
|
54
|
+
const effectiveCredId = stackCredId > 0 ? stackCredId : instance.gitCredentialId;
|
|
55
|
+
if (effectiveCredId && effectiveCredId > 0) {
|
|
56
|
+
return {
|
|
57
|
+
repositoryAuthentication: true,
|
|
58
|
+
repositoryGitCredentialID: effectiveCredId,
|
|
59
|
+
...(stackAuth == null ? void 0 : stackAuth.Username) ? { repositoryUsername: String(stackAuth.Username) } : {}
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
if (instance.gitAccessToken) {
|
|
63
|
+
return {
|
|
64
|
+
repositoryAuthentication: true,
|
|
65
|
+
repositoryUsername: String((stackAuth == null ? void 0 : stackAuth.Username) || instance.gitUsername || "oauth2"),
|
|
66
|
+
repositoryPassword: instance.gitAccessToken
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
if (stackAuth == null ? void 0 : stackAuth.Username) {
|
|
70
|
+
return {
|
|
71
|
+
repositoryAuthentication: true,
|
|
72
|
+
repositoryUsername: String(stackAuth.Username)
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
return {
|
|
76
|
+
repositoryAuthentication: false
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
async function executePortainerRedeploy(app, config, signal) {
|
|
80
|
+
const startedAt = /* @__PURE__ */ new Date();
|
|
81
|
+
const instance = await (0, import_helpers.resolvePortainerInstance)(app, config.instanceId ?? null);
|
|
82
|
+
const timeoutMs = (0, import_PortainerClient.clampTimeoutMs)(config.timeoutMs, instance.defaultTimeoutMs);
|
|
83
|
+
const mode = config.mode === "webhook" ? "webhook" : "stack";
|
|
84
|
+
if (mode === "webhook") {
|
|
85
|
+
await instance.client.triggerWebhook(config.webhookId ?? "", config.webhookTag, { timeoutMs, signal });
|
|
86
|
+
const finishedAt2 = /* @__PURE__ */ new Date();
|
|
87
|
+
return {
|
|
88
|
+
ok: true,
|
|
89
|
+
instanceId: instance.id,
|
|
90
|
+
instanceName: instance.name,
|
|
91
|
+
endpointId: null,
|
|
92
|
+
stackId: null,
|
|
93
|
+
stackName: null,
|
|
94
|
+
stackType: "webhook",
|
|
95
|
+
stackStatus: 1,
|
|
96
|
+
gitReference: null,
|
|
97
|
+
webhookTag: config.webhookTag ? String(config.webhookTag).trim() : null,
|
|
98
|
+
appliedEnv: {},
|
|
99
|
+
appliedEnvCount: 0,
|
|
100
|
+
runningCount: 0,
|
|
101
|
+
totalCount: 0,
|
|
102
|
+
healthy: true,
|
|
103
|
+
containers: [],
|
|
104
|
+
startedAt: startedAt.toISOString(),
|
|
105
|
+
finishedAt: finishedAt2.toISOString(),
|
|
106
|
+
durationMs: Math.max(0, finishedAt2.getTime() - startedAt.getTime())
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
const stackId = (0, import_PortainerClient.assertPositiveInt)(config.stackId ?? void 0, "Stack ID");
|
|
110
|
+
const rawStack = await instance.client.getRawStack(stackId, signal);
|
|
111
|
+
const stackDetail = (0, import_PortainerClient.toStackDetail)(rawStack);
|
|
112
|
+
const hasExplicitEndpoint = config.endpointId !== null && typeof config.endpointId !== "undefined" && String(config.endpointId).trim() !== "";
|
|
113
|
+
const endpointId = hasExplicitEndpoint ? (0, import_PortainerClient.assertPositiveInt)(config.endpointId ?? void 0, "Environment (Endpoint) ID") : (0, import_PortainerClient.assertPositiveInt)(rawStack.EndpointId, "Stack Endpoint ID");
|
|
114
|
+
const { env, maskedEnv } = (0, import_env.buildEffectiveStackEnv)({
|
|
115
|
+
existingEnv: stackDetail.env,
|
|
116
|
+
strategy: config.envStrategy ?? "merge",
|
|
117
|
+
dynamicEnv: config.dynamicEnv,
|
|
118
|
+
envText: config.envText,
|
|
119
|
+
envPairs: config.envPairs
|
|
120
|
+
});
|
|
121
|
+
const prune = Boolean(config.prune);
|
|
122
|
+
const repullImage = Boolean(config.repullImage);
|
|
123
|
+
let updatedStack;
|
|
124
|
+
let gitReferenceUsed = null;
|
|
125
|
+
if (stackDetail.isGitStack) {
|
|
126
|
+
gitReferenceUsed = (0, import_env.normalizeGitReference)(config.gitReference, stackDetail.gitReference);
|
|
127
|
+
const gitAuth = resolveGitAuthPayload(rawStack, instance);
|
|
128
|
+
updatedStack = await instance.client.redeployGitStack(
|
|
129
|
+
stackId,
|
|
130
|
+
endpointId,
|
|
131
|
+
{
|
|
132
|
+
env,
|
|
133
|
+
prune,
|
|
134
|
+
repullImage,
|
|
135
|
+
repositoryReferenceName: gitReferenceUsed,
|
|
136
|
+
...gitAuth
|
|
137
|
+
},
|
|
138
|
+
{ timeoutMs, signal }
|
|
139
|
+
);
|
|
140
|
+
} else {
|
|
141
|
+
const stackFileContent = await instance.client.getStackFile(stackId, signal);
|
|
142
|
+
if (!stackFileContent.trim()) {
|
|
143
|
+
throw new import_PortainerClient.PortainerApiError("Portainer stack file content is empty; cannot update non-Git stack", {
|
|
144
|
+
code: "EMPTY_STACK_FILE"
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
updatedStack = await instance.client.updateComposeStack(
|
|
148
|
+
stackId,
|
|
149
|
+
endpointId,
|
|
150
|
+
{
|
|
151
|
+
stackFileContent,
|
|
152
|
+
env,
|
|
153
|
+
prune,
|
|
154
|
+
repullImage
|
|
155
|
+
},
|
|
156
|
+
{ timeoutMs, signal }
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
updatedStack = await instance.client.waitForStackSettlement(stackId, updatedStack, { timeoutMs, signal });
|
|
160
|
+
const stackName = String((updatedStack == null ? void 0 : updatedStack.Name) ?? rawStack.Name ?? "");
|
|
161
|
+
let containers = [];
|
|
162
|
+
try {
|
|
163
|
+
containers = await instance.client.listStackContainers(endpointId, stackName, signal);
|
|
164
|
+
} catch {
|
|
165
|
+
containers = [];
|
|
166
|
+
}
|
|
167
|
+
const health = (0, import_PortainerClient.summarizeContainersHealth)(containers);
|
|
168
|
+
const finishedAt = /* @__PURE__ */ new Date();
|
|
169
|
+
return {
|
|
170
|
+
ok: true,
|
|
171
|
+
instanceId: instance.id,
|
|
172
|
+
instanceName: instance.name,
|
|
173
|
+
endpointId,
|
|
174
|
+
stackId,
|
|
175
|
+
stackName,
|
|
176
|
+
stackType: stackDetail.isGitStack ? "git" : "compose",
|
|
177
|
+
stackStatus: Number((updatedStack == null ? void 0 : updatedStack.Status) ?? rawStack.Status ?? 1),
|
|
178
|
+
gitReference: gitReferenceUsed,
|
|
179
|
+
appliedEnv: maskedEnv,
|
|
180
|
+
appliedEnvCount: env.length,
|
|
181
|
+
runningCount: health.runningCount,
|
|
182
|
+
totalCount: health.totalCount,
|
|
183
|
+
healthy: containers.length > 0 ? health.healthy : true,
|
|
184
|
+
containers,
|
|
185
|
+
startedAt: startedAt.toISOString(),
|
|
186
|
+
finishedAt: finishedAt.toISOString(),
|
|
187
|
+
durationMs: Math.max(0, finishedAt.getTime() - startedAt.getTime())
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
class PortainerStackRedeployInstruction extends import_plugin_workflow.Instruction {
|
|
191
|
+
configSchema = import_joi.default.object({
|
|
192
|
+
instanceId: numOrVar,
|
|
193
|
+
mode: import_joi.default.string().valid("stack", "webhook").default("stack"),
|
|
194
|
+
endpointId: numOrVar,
|
|
195
|
+
stackId: numOrVar,
|
|
196
|
+
webhookId: import_joi.default.string().allow("", null),
|
|
197
|
+
webhookTag: import_joi.default.string().allow("", null),
|
|
198
|
+
gitReference: import_joi.default.string().allow("", null),
|
|
199
|
+
repullImage: import_joi.default.boolean().default(false),
|
|
200
|
+
prune: import_joi.default.boolean().default(false),
|
|
201
|
+
envStrategy: import_joi.default.string().valid("merge", "replace").default("merge"),
|
|
202
|
+
dynamicEnv: import_joi.default.any(),
|
|
203
|
+
envText: import_joi.default.string().allow("", null),
|
|
204
|
+
envPairs: import_joi.default.array().items(
|
|
205
|
+
import_joi.default.object({
|
|
206
|
+
name: import_joi.default.string().allow("", null),
|
|
207
|
+
value: import_joi.default.any(),
|
|
208
|
+
secret: import_joi.default.boolean()
|
|
209
|
+
}).unknown(true)
|
|
210
|
+
),
|
|
211
|
+
timeoutMs: numOrVar,
|
|
212
|
+
ignoreFail: import_joi.default.boolean().default(false)
|
|
213
|
+
}).unknown(true);
|
|
214
|
+
async run(node, prevJob, processor, options) {
|
|
215
|
+
const config = processor.getParsedValue(node.config ?? {}, node.id);
|
|
216
|
+
return (0, import_helpers.runPortainerWorkflowTask)({
|
|
217
|
+
workflow: this.workflow,
|
|
218
|
+
node,
|
|
219
|
+
prevJob,
|
|
220
|
+
processor,
|
|
221
|
+
ignoreFail: config.ignoreFail,
|
|
222
|
+
signal: options == null ? void 0 : options.signal,
|
|
223
|
+
execute: (signal) => executePortainerRedeploy(this.workflow.app, config, signal)
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
async resume(node, job) {
|
|
227
|
+
const config = node.config ?? {};
|
|
228
|
+
if (config.ignoreFail) {
|
|
229
|
+
job.set("status", import_plugin_workflow.JOB_STATUS.RESOLVED);
|
|
230
|
+
}
|
|
231
|
+
return job;
|
|
232
|
+
}
|
|
233
|
+
async test(config) {
|
|
234
|
+
const startedAt = /* @__PURE__ */ new Date();
|
|
235
|
+
try {
|
|
236
|
+
const result = await executePortainerRedeploy(this.workflow.app, config);
|
|
237
|
+
return {
|
|
238
|
+
status: import_plugin_workflow.JOB_STATUS.RESOLVED,
|
|
239
|
+
result
|
|
240
|
+
};
|
|
241
|
+
} catch (error) {
|
|
242
|
+
return {
|
|
243
|
+
status: config.ignoreFail ? import_plugin_workflow.JOB_STATUS.RESOLVED : import_plugin_workflow.JOB_STATUS.FAILED,
|
|
244
|
+
result: (0, import_helpers.formatFailureResult)(error, startedAt)
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
var PortainerStackRedeployInstruction_default = PortainerStackRedeployInstruction;
|
|
250
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
251
|
+
0 && (module.exports = {
|
|
252
|
+
PortainerStackRedeployInstruction,
|
|
253
|
+
executePortainerRedeploy
|
|
254
|
+
});
|
|
@@ -0,0 +1,211 @@
|
|
|
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
|
+
var __defProp = Object.defineProperty;
|
|
11
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
12
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
13
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
|
+
var __export = (target, all) => {
|
|
15
|
+
for (var name in all)
|
|
16
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
17
|
+
};
|
|
18
|
+
var __copyProps = (to, from, except, desc) => {
|
|
19
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
20
|
+
for (let key of __getOwnPropNames(from))
|
|
21
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
22
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
23
|
+
}
|
|
24
|
+
return to;
|
|
25
|
+
};
|
|
26
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
27
|
+
var helpers_exports = {};
|
|
28
|
+
__export(helpers_exports, {
|
|
29
|
+
decryptIfNeeded: () => decryptIfNeeded,
|
|
30
|
+
formatFailureResult: () => formatFailureResult,
|
|
31
|
+
resolvePortainerInstance: () => resolvePortainerInstance,
|
|
32
|
+
runPortainerWorkflowTask: () => runPortainerWorkflowTask
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(helpers_exports);
|
|
35
|
+
var import_plugin_workflow = require("@nocobase/plugin-workflow");
|
|
36
|
+
var import_constants = require("../../shared/constants");
|
|
37
|
+
var import_PortainerClient = require("../services/PortainerClient");
|
|
38
|
+
async function decryptIfNeeded(app, value) {
|
|
39
|
+
const raw = String(value ?? "").trim();
|
|
40
|
+
if (!raw) {
|
|
41
|
+
return "";
|
|
42
|
+
}
|
|
43
|
+
const decryptor = app.aesEncryptor;
|
|
44
|
+
if (!decryptor) {
|
|
45
|
+
return raw;
|
|
46
|
+
}
|
|
47
|
+
try {
|
|
48
|
+
return await decryptor.decrypt(raw);
|
|
49
|
+
} catch {
|
|
50
|
+
return raw;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
async function resolvePortainerInstance(app, instanceId) {
|
|
54
|
+
const repo = app.db.getRepository(import_constants.PORTAINER_INSTANCES_COLLECTION);
|
|
55
|
+
if (!repo) {
|
|
56
|
+
throw new import_PortainerClient.PortainerApiError("Portainer instances collection is not initialized", { code: "NO_COLLECTION" });
|
|
57
|
+
}
|
|
58
|
+
let record = null;
|
|
59
|
+
const trimmedId = instanceId !== null && typeof instanceId !== "undefined" ? String(instanceId).trim() : "";
|
|
60
|
+
if (trimmedId) {
|
|
61
|
+
const numericId = Number(trimmedId);
|
|
62
|
+
if (!Number.isSafeInteger(numericId) || numericId <= 0) {
|
|
63
|
+
throw new import_PortainerClient.PortainerApiError(`Invalid Portainer instance ID "${trimmedId}"`, { code: "INVALID_INSTANCE" });
|
|
64
|
+
}
|
|
65
|
+
record = await repo.findOne({ filter: { id: numericId, enabled: true } });
|
|
66
|
+
if (!record) {
|
|
67
|
+
throw new import_PortainerClient.PortainerApiError(`Portainer instance #${numericId} was not found or is disabled`, {
|
|
68
|
+
code: "INSTANCE_NOT_FOUND"
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
} else {
|
|
72
|
+
record = await repo.findOne({ filter: { isDefault: true, enabled: true } });
|
|
73
|
+
if (!record) {
|
|
74
|
+
record = await repo.findOne({ filter: { enabled: true }, sort: ["id"] });
|
|
75
|
+
}
|
|
76
|
+
if (!record) {
|
|
77
|
+
throw new import_PortainerClient.PortainerApiError("No enabled Portainer instance is configured", { code: "NO_INSTANCE" });
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
const baseUrl = String(record.get("baseUrl") ?? "").trim();
|
|
81
|
+
const apiKey = await decryptIfNeeded(app, record.get("apiKey"));
|
|
82
|
+
const gitAccessToken = await decryptIfNeeded(app, record.get("gitAccessToken"));
|
|
83
|
+
const gitUsername = String(record.get("gitUsername") ?? "").trim() || void 0;
|
|
84
|
+
const rawCredId = Number(record.get("gitCredentialId") ?? 0);
|
|
85
|
+
const gitCredentialId = Number.isSafeInteger(rawCredId) && rawCredId > 0 ? rawCredId : void 0;
|
|
86
|
+
const allowInsecureTls = Boolean(record.get("allowInsecureTls"));
|
|
87
|
+
const defaultTimeoutMs = (0, import_PortainerClient.clampTimeoutMs)(Number(record.get("defaultTimeoutMs") ?? import_constants.DEFAULT_TIMEOUT_MS));
|
|
88
|
+
const client = new import_PortainerClient.PortainerClient({
|
|
89
|
+
baseUrl,
|
|
90
|
+
apiKey,
|
|
91
|
+
allowInsecureTls,
|
|
92
|
+
timeoutMs: defaultTimeoutMs
|
|
93
|
+
});
|
|
94
|
+
return {
|
|
95
|
+
id: Number(record.get("id")),
|
|
96
|
+
name: String(record.get("name") ?? ""),
|
|
97
|
+
baseUrl,
|
|
98
|
+
apiKey,
|
|
99
|
+
gitUsername,
|
|
100
|
+
gitAccessToken: gitAccessToken || void 0,
|
|
101
|
+
gitCredentialId,
|
|
102
|
+
allowInsecureTls,
|
|
103
|
+
defaultTimeoutMs,
|
|
104
|
+
client
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
function formatFailureResult(error, startedAt) {
|
|
108
|
+
const finishedAt = /* @__PURE__ */ new Date();
|
|
109
|
+
const durationMs = Math.max(0, finishedAt.getTime() - startedAt.getTime());
|
|
110
|
+
if (error instanceof import_PortainerClient.PortainerApiError) {
|
|
111
|
+
return {
|
|
112
|
+
ok: false,
|
|
113
|
+
errorMessage: error.message,
|
|
114
|
+
errorCode: error.code ?? "PORTAINER_ERROR",
|
|
115
|
+
httpStatus: error.status ?? null,
|
|
116
|
+
startedAt: startedAt.toISOString(),
|
|
117
|
+
finishedAt: finishedAt.toISOString(),
|
|
118
|
+
durationMs
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
122
|
+
return {
|
|
123
|
+
ok: false,
|
|
124
|
+
errorMessage: message,
|
|
125
|
+
errorCode: "EXECUTION_ERROR",
|
|
126
|
+
httpStatus: null,
|
|
127
|
+
startedAt: startedAt.toISOString(),
|
|
128
|
+
finishedAt: finishedAt.toISOString(),
|
|
129
|
+
durationMs
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
function resolveFailureJobStatus(ignoreFail, error) {
|
|
133
|
+
if (ignoreFail) {
|
|
134
|
+
return import_plugin_workflow.JOB_STATUS.RESOLVED;
|
|
135
|
+
}
|
|
136
|
+
if (error instanceof import_PortainerClient.PortainerApiError && error.code === "ECONNABORTED") {
|
|
137
|
+
return import_plugin_workflow.JOB_STATUS.ABORTED;
|
|
138
|
+
}
|
|
139
|
+
return import_plugin_workflow.JOB_STATUS.FAILED;
|
|
140
|
+
}
|
|
141
|
+
async function runPortainerWorkflowTask(params) {
|
|
142
|
+
const { workflow, node, prevJob, processor, ignoreFail, execute } = params;
|
|
143
|
+
const startedAt = /* @__PURE__ */ new Date();
|
|
144
|
+
const sync = processor.isInstructionSync(node);
|
|
145
|
+
if (sync) {
|
|
146
|
+
try {
|
|
147
|
+
const result = await execute(params.signal);
|
|
148
|
+
return {
|
|
149
|
+
status: import_plugin_workflow.JOB_STATUS.RESOLVED,
|
|
150
|
+
result
|
|
151
|
+
};
|
|
152
|
+
} catch (error) {
|
|
153
|
+
return {
|
|
154
|
+
status: resolveFailureJobStatus(ignoreFail, error),
|
|
155
|
+
result: formatFailureResult(error, startedAt)
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
const savedJob = processor.saveJob({
|
|
160
|
+
status: import_plugin_workflow.JOB_STATUS.PENDING,
|
|
161
|
+
nodeId: node.id,
|
|
162
|
+
nodeKey: node.key,
|
|
163
|
+
upstreamId: (prevJob == null ? void 0 : prevJob.id) ?? null
|
|
164
|
+
});
|
|
165
|
+
const jobId = savedJob.id;
|
|
166
|
+
await processor.exit();
|
|
167
|
+
const abortHandle = processor.createBackgroundAbortHandle();
|
|
168
|
+
const jobDone = { status: import_plugin_workflow.JOB_STATUS.PENDING };
|
|
169
|
+
const settle = async () => {
|
|
170
|
+
try {
|
|
171
|
+
const result = await execute(abortHandle.signal);
|
|
172
|
+
jobDone.status = import_plugin_workflow.JOB_STATUS.RESOLVED;
|
|
173
|
+
jobDone.result = result;
|
|
174
|
+
} catch (error) {
|
|
175
|
+
processor.logger.error(
|
|
176
|
+
`[plugin-workflow-portainer] node (#${node.id}) execution failed: ${error instanceof Error ? error.message : String(error)}`
|
|
177
|
+
);
|
|
178
|
+
jobDone.status = abortHandle.signal.aborted ? import_plugin_workflow.JOB_STATUS.ABORTED : resolveFailureJobStatus(ignoreFail, error);
|
|
179
|
+
jobDone.result = formatFailureResult(error, startedAt);
|
|
180
|
+
} finally {
|
|
181
|
+
abortHandle.dispose();
|
|
182
|
+
const job = await workflow.app.db.getRepository("jobs").findOne({
|
|
183
|
+
filterByTk: jobId
|
|
184
|
+
});
|
|
185
|
+
if (job) {
|
|
186
|
+
const execution = await job.getExecution();
|
|
187
|
+
const aborted = await workflow.abortExecutionIfExpired(execution);
|
|
188
|
+
if (!aborted) {
|
|
189
|
+
await execution.reload();
|
|
190
|
+
await job.reload();
|
|
191
|
+
}
|
|
192
|
+
if (!aborted && execution.status === import_plugin_workflow.EXECUTION_STATUS.STARTED && job.status === import_plugin_workflow.JOB_STATUS.PENDING) {
|
|
193
|
+
job.set(jobDone);
|
|
194
|
+
await workflow.resume(job);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
settle().catch((caught) => {
|
|
200
|
+
processor.logger.error(
|
|
201
|
+
`[plugin-workflow-portainer] async job settle failed (#${node.id}): ${caught instanceof Error ? caught.message : String(caught)}`
|
|
202
|
+
);
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
206
|
+
0 && (module.exports = {
|
|
207
|
+
decryptIfNeeded,
|
|
208
|
+
formatFailureResult,
|
|
209
|
+
resolvePortainerInstance,
|
|
210
|
+
runPortainerWorkflowTask
|
|
211
|
+
});
|
|
@@ -0,0 +1,170 @@
|
|
|
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
|
+
var __defProp = Object.defineProperty;
|
|
11
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
12
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
13
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
|
+
var __export = (target, all) => {
|
|
15
|
+
for (var name in all)
|
|
16
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
17
|
+
};
|
|
18
|
+
var __copyProps = (to, from, except, desc) => {
|
|
19
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
20
|
+
for (let key of __getOwnPropNames(from))
|
|
21
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
22
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
23
|
+
}
|
|
24
|
+
return to;
|
|
25
|
+
};
|
|
26
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
27
|
+
var plugin_exports = {};
|
|
28
|
+
__export(plugin_exports, {
|
|
29
|
+
PluginWorkflowPortainerServer: () => PluginWorkflowPortainerServer,
|
|
30
|
+
default: () => plugin_default
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(plugin_exports);
|
|
33
|
+
var import_node_path = require("node:path");
|
|
34
|
+
var import_server = require("@nocobase/server");
|
|
35
|
+
var import_constants = require("../shared/constants");
|
|
36
|
+
var import_portainer = require("./actions/portainer");
|
|
37
|
+
var import_PortainerStackControlInstruction = require("./instructions/PortainerStackControlInstruction");
|
|
38
|
+
var import_PortainerStackRedeployInstruction = require("./instructions/PortainerStackRedeployInstruction");
|
|
39
|
+
var import_PortainerClient = require("./services/PortainerClient");
|
|
40
|
+
const ENCRYPTED_FIELDS = ["apiKey", "gitAccessToken"];
|
|
41
|
+
const METADATA_READ_ACTIONS = ["listInstances", "listEndpoints", "listStacks", "getStackDetail"];
|
|
42
|
+
function maskSecretFields(payload) {
|
|
43
|
+
if (!payload || typeof payload !== "object") {
|
|
44
|
+
return payload;
|
|
45
|
+
}
|
|
46
|
+
if (Array.isArray(payload)) {
|
|
47
|
+
return payload.map(maskSecretFields);
|
|
48
|
+
}
|
|
49
|
+
const obj = payload;
|
|
50
|
+
if (typeof obj.toJSON === "function") {
|
|
51
|
+
return maskSecretFields(obj.toJSON());
|
|
52
|
+
}
|
|
53
|
+
const clone = { ...obj };
|
|
54
|
+
for (const field of ENCRYPTED_FIELDS) {
|
|
55
|
+
if (typeof clone[field] === "string" && clone[field]) {
|
|
56
|
+
clone[field] = import_constants.SECRET_MASK;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
if (Array.isArray(clone.data)) {
|
|
60
|
+
clone.data = clone.data.map(maskSecretFields);
|
|
61
|
+
}
|
|
62
|
+
if (clone.data && typeof clone.data === "object" && !Array.isArray(clone.data)) {
|
|
63
|
+
clone.data = maskSecretFields(clone.data);
|
|
64
|
+
}
|
|
65
|
+
if (Array.isArray(clone.rows)) {
|
|
66
|
+
clone.rows = clone.rows.map(maskSecretFields);
|
|
67
|
+
}
|
|
68
|
+
return clone;
|
|
69
|
+
}
|
|
70
|
+
class PluginWorkflowPortainerServer extends import_server.Plugin {
|
|
71
|
+
canReadWorkflowMetadata(ctx, action) {
|
|
72
|
+
var _a, _b, _c;
|
|
73
|
+
const userId = (_b = (_a = ctx.state) == null ? void 0 : _a.currentUser) == null ? void 0 : _b.id;
|
|
74
|
+
const roles = (_c = ctx.state) == null ? void 0 : _c.currentRoles;
|
|
75
|
+
if (!userId || !Array.isArray(roles)) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
return roles.some((role) => {
|
|
79
|
+
if (typeof role !== "string") {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
if (role === "root") {
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
if (this.app.acl.can({ role, resource: import_constants.PORTAINER_RESOURCE, action })) {
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
if (this.app.acl.can({ role, resource: "workflows", action: "update" })) {
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
if (this.app.acl.can({ role, resource: "flow_nodes", action: "update" })) {
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
return false;
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
async beforeLoad() {
|
|
98
|
+
await this.db.import({
|
|
99
|
+
directory: (0, import_node_path.resolve)(__dirname, "collections")
|
|
100
|
+
});
|
|
101
|
+
this.db.on(`${import_constants.PORTAINER_INSTANCES_COLLECTION}.beforeSave`, async (model) => {
|
|
102
|
+
if (model.changed("baseUrl") && model.get("baseUrl")) {
|
|
103
|
+
(0, import_PortainerClient.assertValidPortainerUrl)(String(model.get("baseUrl")));
|
|
104
|
+
}
|
|
105
|
+
if (model.changed("defaultTimeoutMs") && model.get("defaultTimeoutMs") !== null) {
|
|
106
|
+
model.set("defaultTimeoutMs", (0, import_PortainerClient.clampTimeoutMs)(Number(model.get("defaultTimeoutMs"))));
|
|
107
|
+
}
|
|
108
|
+
const encryptor = this.app.aesEncryptor;
|
|
109
|
+
for (const field of ENCRYPTED_FIELDS) {
|
|
110
|
+
if (!model.changed(field)) {
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
const rawVal = model.get(field);
|
|
114
|
+
if (rawVal === import_constants.SECRET_MASK) {
|
|
115
|
+
model.set(field, model.previous(field));
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
if (typeof rawVal === "string" && rawVal.trim() && encryptor) {
|
|
119
|
+
const encrypted = await encryptor.encrypt(rawVal.trim());
|
|
120
|
+
model.set(field, encrypted);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
this.db.on(`${import_constants.PORTAINER_INSTANCES_COLLECTION}.afterSave`, async (model) => {
|
|
125
|
+
if (model.get("isDefault")) {
|
|
126
|
+
const currentId = Number(model.get("id"));
|
|
127
|
+
const repo = this.db.getRepository(import_constants.PORTAINER_INSTANCES_COLLECTION);
|
|
128
|
+
const others = await repo.find({
|
|
129
|
+
filter: {
|
|
130
|
+
isDefault: true,
|
|
131
|
+
id: { $ne: currentId }
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
for (const item of others) {
|
|
135
|
+
await item.update({ isDefault: false });
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
async load() {
|
|
141
|
+
const workflow = this.app.pm.get("workflow") || this.app.pm.get("@nocobase/plugin-workflow");
|
|
142
|
+
if (workflow == null ? void 0 : workflow.registerInstruction) {
|
|
143
|
+
workflow.registerInstruction(import_constants.INSTRUCTION_TYPES.redeploy, new import_PortainerStackRedeployInstruction.PortainerStackRedeployInstruction(workflow));
|
|
144
|
+
workflow.registerInstruction(import_constants.INSTRUCTION_TYPES.control, new import_PortainerStackControlInstruction.PortainerStackControlInstruction(workflow));
|
|
145
|
+
}
|
|
146
|
+
this.app.resourceManager.define({
|
|
147
|
+
name: import_constants.PORTAINER_RESOURCE,
|
|
148
|
+
actions: (0, import_portainer.createPortainerResourceActions)(this.app)
|
|
149
|
+
});
|
|
150
|
+
this.app.resourceManager.use(async (ctx, next) => {
|
|
151
|
+
var _a;
|
|
152
|
+
await next();
|
|
153
|
+
if (((_a = ctx.action) == null ? void 0 : _a.resourceName) === import_constants.PORTAINER_INSTANCES_COLLECTION && ctx.body) {
|
|
154
|
+
ctx.body = maskSecretFields(ctx.body);
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
this.app.acl.registerSnippet({
|
|
158
|
+
name: import_constants.PORTAINER_ACL_SNIPPET,
|
|
159
|
+
actions: [`${import_constants.PORTAINER_INSTANCES_COLLECTION}:*`, `${import_constants.PORTAINER_RESOURCE}:*`]
|
|
160
|
+
});
|
|
161
|
+
for (const action of METADATA_READ_ACTIONS) {
|
|
162
|
+
this.app.acl.allow(import_constants.PORTAINER_RESOURCE, action, (ctx) => this.canReadWorkflowMetadata(ctx, action));
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
var plugin_default = PluginWorkflowPortainerServer;
|
|
167
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
168
|
+
0 && (module.exports = {
|
|
169
|
+
PluginWorkflowPortainerServer
|
|
170
|
+
});
|