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,497 @@
|
|
|
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 PortainerClient_exports = {};
|
|
38
|
+
__export(PortainerClient_exports, {
|
|
39
|
+
PortainerApiError: () => PortainerApiError,
|
|
40
|
+
PortainerClient: () => PortainerClient,
|
|
41
|
+
assertPositiveInt: () => assertPositiveInt,
|
|
42
|
+
assertValidPortainerUrl: () => assertValidPortainerUrl,
|
|
43
|
+
assertValidWebhookId: () => assertValidWebhookId,
|
|
44
|
+
clampTimeoutMs: () => clampTimeoutMs,
|
|
45
|
+
summarizeContainersHealth: () => summarizeContainersHealth,
|
|
46
|
+
toStackDetail: () => toStackDetail,
|
|
47
|
+
toStackSummary: () => toStackSummary
|
|
48
|
+
});
|
|
49
|
+
module.exports = __toCommonJS(PortainerClient_exports);
|
|
50
|
+
var import_node_http = __toESM(require("node:http"));
|
|
51
|
+
var import_node_https = __toESM(require("node:https"));
|
|
52
|
+
var import_node_url = require("node:url");
|
|
53
|
+
var import_constants = require("../../shared/constants");
|
|
54
|
+
class PortainerApiError extends Error {
|
|
55
|
+
status;
|
|
56
|
+
code;
|
|
57
|
+
details;
|
|
58
|
+
constructor(message, options) {
|
|
59
|
+
super(message);
|
|
60
|
+
this.name = "PortainerApiError";
|
|
61
|
+
this.status = options == null ? void 0 : options.status;
|
|
62
|
+
this.code = options == null ? void 0 : options.code;
|
|
63
|
+
this.details = options == null ? void 0 : options.details;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
function assertValidPortainerUrl(rawUrl) {
|
|
67
|
+
const trimmed = String(rawUrl ?? "").trim();
|
|
68
|
+
if (!trimmed) {
|
|
69
|
+
throw new PortainerApiError("Portainer base URL is required", { code: "INVALID_URL" });
|
|
70
|
+
}
|
|
71
|
+
let parsed;
|
|
72
|
+
try {
|
|
73
|
+
parsed = new import_node_url.URL(trimmed);
|
|
74
|
+
} catch {
|
|
75
|
+
throw new PortainerApiError("Portainer base URL is invalid", { code: "INVALID_URL" });
|
|
76
|
+
}
|
|
77
|
+
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
|
|
78
|
+
throw new PortainerApiError("Portainer base URL must use http or https", { code: "INVALID_URL" });
|
|
79
|
+
}
|
|
80
|
+
if (parsed.username || parsed.password) {
|
|
81
|
+
throw new PortainerApiError("Portainer base URL must not embed credentials", { code: "INVALID_URL" });
|
|
82
|
+
}
|
|
83
|
+
return parsed;
|
|
84
|
+
}
|
|
85
|
+
function assertPositiveInt(value, label) {
|
|
86
|
+
const num = typeof value === "number" ? value : Number(String(value ?? "").trim());
|
|
87
|
+
if (!Number.isSafeInteger(num) || num <= 0) {
|
|
88
|
+
throw new PortainerApiError(`${label} must be a positive integer`, { code: "INVALID_PARAM" });
|
|
89
|
+
}
|
|
90
|
+
return num;
|
|
91
|
+
}
|
|
92
|
+
function assertValidWebhookId(rawWebhookId) {
|
|
93
|
+
const trimmed = String(rawWebhookId ?? "").trim();
|
|
94
|
+
if (!trimmed || !/^[A-Za-z0-9_-]{8,128}$/.test(trimmed)) {
|
|
95
|
+
throw new PortainerApiError("Webhook token must be 8-128 alphanumeric, dash, or underscore characters", {
|
|
96
|
+
code: "INVALID_WEBHOOK"
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
return trimmed;
|
|
100
|
+
}
|
|
101
|
+
function clampTimeoutMs(timeoutMs, fallback = import_constants.DEFAULT_TIMEOUT_MS) {
|
|
102
|
+
const num = Number(timeoutMs);
|
|
103
|
+
if (!Number.isFinite(num) || num <= 0) {
|
|
104
|
+
return fallback;
|
|
105
|
+
}
|
|
106
|
+
return Math.min(import_constants.MAX_TIMEOUT_MS, Math.max(import_constants.MIN_TIMEOUT_MS, Math.trunc(num)));
|
|
107
|
+
}
|
|
108
|
+
function normalizeEnvArray(raw) {
|
|
109
|
+
if (!Array.isArray(raw)) {
|
|
110
|
+
return [];
|
|
111
|
+
}
|
|
112
|
+
const pairs = [];
|
|
113
|
+
for (const item of raw) {
|
|
114
|
+
const name = String((item == null ? void 0 : item.name) ?? "").trim();
|
|
115
|
+
if (!name) {
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
pairs.push({ name, value: String((item == null ? void 0 : item.value) ?? "") });
|
|
119
|
+
}
|
|
120
|
+
return pairs;
|
|
121
|
+
}
|
|
122
|
+
function toStackSummary(stack) {
|
|
123
|
+
const env = normalizeEnvArray(stack.Env);
|
|
124
|
+
const git = stack.GitConfig;
|
|
125
|
+
return {
|
|
126
|
+
id: Number(stack.Id),
|
|
127
|
+
name: String(stack.Name ?? ""),
|
|
128
|
+
endpointId: Number(stack.EndpointId),
|
|
129
|
+
type: Number(stack.Type ?? 2),
|
|
130
|
+
status: Number(stack.Status ?? 1),
|
|
131
|
+
isGitStack: Boolean(git && git.URL),
|
|
132
|
+
gitRepoUrl: (git == null ? void 0 : git.URL) ? String(git.URL) : void 0,
|
|
133
|
+
gitReference: (git == null ? void 0 : git.ReferenceName) ? String(git.ReferenceName) : void 0,
|
|
134
|
+
envCount: env.length
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
function toStackDetail(stack) {
|
|
138
|
+
var _a;
|
|
139
|
+
const summary = toStackSummary(stack);
|
|
140
|
+
const auth = (_a = stack.GitConfig) == null ? void 0 : _a.Authentication;
|
|
141
|
+
const credId = Number((auth == null ? void 0 : auth.GitCredentialID) ?? 0);
|
|
142
|
+
return {
|
|
143
|
+
...summary,
|
|
144
|
+
env: normalizeEnvArray(stack.Env),
|
|
145
|
+
hasSavedGitCredential: credId > 0,
|
|
146
|
+
gitCredentialId: credId > 0 ? credId : 0,
|
|
147
|
+
gitUsername: (auth == null ? void 0 : auth.Username) ? String(auth.Username) : void 0
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
class PortainerClient {
|
|
151
|
+
baseUrl;
|
|
152
|
+
apiKey;
|
|
153
|
+
allowInsecureTls;
|
|
154
|
+
timeoutMs;
|
|
155
|
+
constructor(options) {
|
|
156
|
+
this.baseUrl = assertValidPortainerUrl(options.baseUrl);
|
|
157
|
+
this.apiKey = String(options.apiKey ?? "").trim();
|
|
158
|
+
this.allowInsecureTls = Boolean(options.allowInsecureTls);
|
|
159
|
+
this.timeoutMs = clampTimeoutMs(options.timeoutMs);
|
|
160
|
+
}
|
|
161
|
+
buildUrl(pathname, query) {
|
|
162
|
+
const basePath = this.baseUrl.pathname.replace(/\/+$/, "");
|
|
163
|
+
const suffix = pathname.startsWith("/") ? pathname : `/${pathname}`;
|
|
164
|
+
const target = new import_node_url.URL(this.baseUrl.toString());
|
|
165
|
+
target.pathname = `${basePath}${suffix}`;
|
|
166
|
+
target.search = "";
|
|
167
|
+
if (query) {
|
|
168
|
+
for (const [key, value] of Object.entries(query)) {
|
|
169
|
+
if (typeof value !== "undefined" && value !== "") {
|
|
170
|
+
target.searchParams.set(key, String(value));
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return target;
|
|
175
|
+
}
|
|
176
|
+
async request(options) {
|
|
177
|
+
if (!options.skipAuth && !this.apiKey) {
|
|
178
|
+
throw new PortainerApiError("Portainer API key is required", { code: "MISSING_API_KEY" });
|
|
179
|
+
}
|
|
180
|
+
const targetUrl = this.buildUrl(options.path, options.query);
|
|
181
|
+
const payload = typeof options.body !== "undefined" ? Buffer.from(JSON.stringify(options.body), "utf8") : null;
|
|
182
|
+
const timeout = clampTimeoutMs(options.timeoutMs, this.timeoutMs);
|
|
183
|
+
const headers = {
|
|
184
|
+
Accept: "application/json"
|
|
185
|
+
};
|
|
186
|
+
if (!options.skipAuth) {
|
|
187
|
+
headers["X-API-Key"] = this.apiKey;
|
|
188
|
+
}
|
|
189
|
+
if (payload) {
|
|
190
|
+
headers["Content-Type"] = "application/json";
|
|
191
|
+
headers["Content-Length"] = String(payload.byteLength);
|
|
192
|
+
}
|
|
193
|
+
const transport = targetUrl.protocol === "https:" ? import_node_https.default : import_node_http.default;
|
|
194
|
+
return new Promise((resolve, reject) => {
|
|
195
|
+
var _a;
|
|
196
|
+
if ((_a = options.signal) == null ? void 0 : _a.aborted) {
|
|
197
|
+
reject(new PortainerApiError("Portainer request aborted", { code: "ECONNABORTED" }));
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
let settled = false;
|
|
201
|
+
const finish = (fn) => {
|
|
202
|
+
if (settled) return;
|
|
203
|
+
settled = true;
|
|
204
|
+
if (options.signal) {
|
|
205
|
+
options.signal.removeEventListener("abort", onAbort);
|
|
206
|
+
}
|
|
207
|
+
fn();
|
|
208
|
+
};
|
|
209
|
+
const req = transport.request(
|
|
210
|
+
targetUrl,
|
|
211
|
+
{
|
|
212
|
+
method: options.method,
|
|
213
|
+
headers,
|
|
214
|
+
timeout,
|
|
215
|
+
...targetUrl.protocol === "https:" ? { rejectUnauthorized: !this.allowInsecureTls } : {}
|
|
216
|
+
},
|
|
217
|
+
(res) => {
|
|
218
|
+
const chunks = [];
|
|
219
|
+
res.on("data", (chunk) => chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)));
|
|
220
|
+
res.on("end", () => {
|
|
221
|
+
const rawText = Buffer.concat(chunks).toString("utf8");
|
|
222
|
+
const status = res.statusCode ?? 500;
|
|
223
|
+
let parsedBody = rawText;
|
|
224
|
+
if (rawText.trim()) {
|
|
225
|
+
try {
|
|
226
|
+
parsedBody = JSON.parse(rawText);
|
|
227
|
+
} catch {
|
|
228
|
+
parsedBody = rawText;
|
|
229
|
+
}
|
|
230
|
+
} else {
|
|
231
|
+
parsedBody = null;
|
|
232
|
+
}
|
|
233
|
+
if (status >= 200 && status < 300) {
|
|
234
|
+
finish(() => resolve(parsedBody));
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
const bodyObj = typeof parsedBody === "object" && parsedBody !== null ? parsedBody : null;
|
|
238
|
+
const remoteMessage = typeof (bodyObj == null ? void 0 : bodyObj.message) === "string" && bodyObj.message || typeof (bodyObj == null ? void 0 : bodyObj.details) === "string" && bodyObj.details || typeof parsedBody === "string" && parsedBody.slice(0, 300) || `Portainer responded with HTTP ${status}`;
|
|
239
|
+
finish(
|
|
240
|
+
() => reject(
|
|
241
|
+
new PortainerApiError(remoteMessage, {
|
|
242
|
+
status,
|
|
243
|
+
code: `HTTP_${status}`,
|
|
244
|
+
details: bodyObj ?? void 0
|
|
245
|
+
})
|
|
246
|
+
)
|
|
247
|
+
);
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
);
|
|
251
|
+
function onAbort() {
|
|
252
|
+
req.destroy();
|
|
253
|
+
finish(() => reject(new PortainerApiError("Portainer request aborted", { code: "ECONNABORTED" })));
|
|
254
|
+
}
|
|
255
|
+
if (options.signal) {
|
|
256
|
+
options.signal.addEventListener("abort", onAbort, { once: true });
|
|
257
|
+
}
|
|
258
|
+
req.on("timeout", () => {
|
|
259
|
+
req.destroy();
|
|
260
|
+
finish(
|
|
261
|
+
() => reject(new PortainerApiError(`Portainer request timed out after ${timeout}ms`, { code: "ECONNABORTED" }))
|
|
262
|
+
);
|
|
263
|
+
});
|
|
264
|
+
req.on("error", (err) => {
|
|
265
|
+
const safeMsg = (err.message || "Network error calling Portainer").replace(this.apiKey || "__none__", "***");
|
|
266
|
+
finish(() => reject(new PortainerApiError(safeMsg, { code: err.code || "NETWORK_ERROR" })));
|
|
267
|
+
});
|
|
268
|
+
if (payload) {
|
|
269
|
+
req.write(payload);
|
|
270
|
+
}
|
|
271
|
+
req.end();
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
async getStatus(signal) {
|
|
275
|
+
return this.request({
|
|
276
|
+
method: "GET",
|
|
277
|
+
path: "/api/status",
|
|
278
|
+
signal
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
async listEndpoints(signal) {
|
|
282
|
+
const raw = await this.request({
|
|
283
|
+
method: "GET",
|
|
284
|
+
path: "/api/endpoints",
|
|
285
|
+
signal
|
|
286
|
+
});
|
|
287
|
+
if (!Array.isArray(raw)) {
|
|
288
|
+
return [];
|
|
289
|
+
}
|
|
290
|
+
return raw.map((item) => ({
|
|
291
|
+
id: Number(item.Id),
|
|
292
|
+
name: String(item.Name ?? `Endpoint #${item.Id}`),
|
|
293
|
+
type: Number(item.Type ?? 1),
|
|
294
|
+
status: Number(item.Status ?? 1),
|
|
295
|
+
url: typeof item.URL === "string" ? item.URL : void 0
|
|
296
|
+
}));
|
|
297
|
+
}
|
|
298
|
+
async listStacks(endpointId, signal) {
|
|
299
|
+
const query = endpointId && endpointId > 0 ? { filters: JSON.stringify({ EndpointID: endpointId }) } : void 0;
|
|
300
|
+
const raw = await this.request({
|
|
301
|
+
method: "GET",
|
|
302
|
+
path: "/api/stacks",
|
|
303
|
+
query,
|
|
304
|
+
signal
|
|
305
|
+
});
|
|
306
|
+
if (!Array.isArray(raw)) {
|
|
307
|
+
return [];
|
|
308
|
+
}
|
|
309
|
+
const mapped = raw.map(toStackSummary);
|
|
310
|
+
return endpointId && endpointId > 0 ? mapped.filter((item) => item.endpointId === endpointId) : mapped;
|
|
311
|
+
}
|
|
312
|
+
async getRawStack(stackId, signal) {
|
|
313
|
+
const validId = assertPositiveInt(stackId, "Stack ID");
|
|
314
|
+
return this.request({
|
|
315
|
+
method: "GET",
|
|
316
|
+
path: `/api/stacks/${encodeURIComponent(String(validId))}`,
|
|
317
|
+
signal
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
async getStackDetail(stackId, signal) {
|
|
321
|
+
const raw = await this.getRawStack(stackId, signal);
|
|
322
|
+
return toStackDetail(raw);
|
|
323
|
+
}
|
|
324
|
+
async getStackFile(stackId, signal) {
|
|
325
|
+
const validId = assertPositiveInt(stackId, "Stack ID");
|
|
326
|
+
const res = await this.request({
|
|
327
|
+
method: "GET",
|
|
328
|
+
path: `/api/stacks/${encodeURIComponent(String(validId))}/file`,
|
|
329
|
+
signal
|
|
330
|
+
});
|
|
331
|
+
return String((res == null ? void 0 : res.StackFileContent) ?? "");
|
|
332
|
+
}
|
|
333
|
+
async redeployGitStack(stackId, endpointId, payload, options) {
|
|
334
|
+
const validStackId = assertPositiveInt(stackId, "Stack ID");
|
|
335
|
+
const validEndpointId = assertPositiveInt(endpointId, "Environment (Endpoint) ID");
|
|
336
|
+
return this.request({
|
|
337
|
+
method: "PUT",
|
|
338
|
+
path: `/api/stacks/${encodeURIComponent(String(validStackId))}/git/redeploy`,
|
|
339
|
+
query: { endpointId: validEndpointId },
|
|
340
|
+
body: {
|
|
341
|
+
env: payload.env,
|
|
342
|
+
prune: payload.prune,
|
|
343
|
+
pullImage: payload.repullImage,
|
|
344
|
+
repullImageAndRedeploy: payload.repullImage,
|
|
345
|
+
repositoryReferenceName: payload.repositoryReferenceName,
|
|
346
|
+
repositoryAuthentication: payload.repositoryAuthentication,
|
|
347
|
+
...payload.repositoryGitCredentialID && payload.repositoryGitCredentialID > 0 ? { repositoryGitCredentialID: payload.repositoryGitCredentialID } : {},
|
|
348
|
+
...payload.repositoryUsername ? { repositoryUsername: payload.repositoryUsername } : {},
|
|
349
|
+
...payload.repositoryPassword ? { repositoryPassword: payload.repositoryPassword } : {}
|
|
350
|
+
},
|
|
351
|
+
timeoutMs: options == null ? void 0 : options.timeoutMs,
|
|
352
|
+
signal: options == null ? void 0 : options.signal
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
async updateComposeStack(stackId, endpointId, payload, options) {
|
|
356
|
+
const validStackId = assertPositiveInt(stackId, "Stack ID");
|
|
357
|
+
const validEndpointId = assertPositiveInt(endpointId, "Environment (Endpoint) ID");
|
|
358
|
+
return this.request({
|
|
359
|
+
method: "PUT",
|
|
360
|
+
path: `/api/stacks/${encodeURIComponent(String(validStackId))}`,
|
|
361
|
+
query: { endpointId: validEndpointId },
|
|
362
|
+
body: {
|
|
363
|
+
stackFileContent: payload.stackFileContent,
|
|
364
|
+
env: payload.env,
|
|
365
|
+
prune: payload.prune,
|
|
366
|
+
pullImage: payload.repullImage
|
|
367
|
+
},
|
|
368
|
+
timeoutMs: options == null ? void 0 : options.timeoutMs,
|
|
369
|
+
signal: options == null ? void 0 : options.signal
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
async triggerWebhook(webhookId, tag, options) {
|
|
373
|
+
const validWebhook = assertValidWebhookId(webhookId);
|
|
374
|
+
const trimmedTag = String(tag ?? "").trim();
|
|
375
|
+
return this.request({
|
|
376
|
+
method: "POST",
|
|
377
|
+
path: `/api/stacks/webhooks/${encodeURIComponent(validWebhook)}`,
|
|
378
|
+
query: trimmedTag ? { tag: trimmedTag } : void 0,
|
|
379
|
+
skipAuth: true,
|
|
380
|
+
timeoutMs: options == null ? void 0 : options.timeoutMs,
|
|
381
|
+
signal: options == null ? void 0 : options.signal
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
async startStack(stackId, endpointId, signal) {
|
|
385
|
+
const validStackId = assertPositiveInt(stackId, "Stack ID");
|
|
386
|
+
const validEndpointId = assertPositiveInt(endpointId, "Environment (Endpoint) ID");
|
|
387
|
+
return this.request({
|
|
388
|
+
method: "POST",
|
|
389
|
+
path: `/api/stacks/${encodeURIComponent(String(validStackId))}/start`,
|
|
390
|
+
query: { endpointId: validEndpointId },
|
|
391
|
+
signal
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
async stopStack(stackId, endpointId, signal) {
|
|
395
|
+
const validStackId = assertPositiveInt(stackId, "Stack ID");
|
|
396
|
+
const validEndpointId = assertPositiveInt(endpointId, "Environment (Endpoint) ID");
|
|
397
|
+
return this.request({
|
|
398
|
+
method: "POST",
|
|
399
|
+
path: `/api/stacks/${encodeURIComponent(String(validStackId))}/stop`,
|
|
400
|
+
query: { endpointId: validEndpointId },
|
|
401
|
+
signal
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* Wait until an asynchronous Portainer deployment (`Status === 3`, Deploying) settles into Active (`1`),
|
|
406
|
+
* Inactive (`2`), or Error (`4`), throwing `PortainerApiError` when deployment fails.
|
|
407
|
+
*/
|
|
408
|
+
async waitForStackSettlement(stackId, initialStack, options) {
|
|
409
|
+
var _a;
|
|
410
|
+
let current = initialStack;
|
|
411
|
+
const timeout = clampTimeoutMs(options == null ? void 0 : options.timeoutMs, this.timeoutMs);
|
|
412
|
+
const pollInterval = Math.max(50, (options == null ? void 0 : options.pollIntervalMs) ?? 1500);
|
|
413
|
+
const deadline = Date.now() + timeout;
|
|
414
|
+
while (Number(current == null ? void 0 : current.Status) === 3) {
|
|
415
|
+
if ((_a = options == null ? void 0 : options.signal) == null ? void 0 : _a.aborted) {
|
|
416
|
+
throw new PortainerApiError("Portainer stack deployment aborted", { code: "ECONNABORTED" });
|
|
417
|
+
}
|
|
418
|
+
if (Date.now() >= deadline) {
|
|
419
|
+
throw new PortainerApiError(`Timed out waiting for Portainer Stack #${stackId} deployment after ${timeout}ms`, {
|
|
420
|
+
code: "ECONNABORTED"
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
await new Promise((resolve, reject) => {
|
|
424
|
+
var _a2;
|
|
425
|
+
const timer = setTimeout(() => {
|
|
426
|
+
var _a3;
|
|
427
|
+
(_a3 = options == null ? void 0 : options.signal) == null ? void 0 : _a3.removeEventListener("abort", onAbort);
|
|
428
|
+
resolve();
|
|
429
|
+
}, pollInterval);
|
|
430
|
+
function onAbort() {
|
|
431
|
+
clearTimeout(timer);
|
|
432
|
+
reject(new PortainerApiError("Portainer stack deployment aborted", { code: "ECONNABORTED" }));
|
|
433
|
+
}
|
|
434
|
+
(_a2 = options == null ? void 0 : options.signal) == null ? void 0 : _a2.addEventListener("abort", onAbort, { once: true });
|
|
435
|
+
});
|
|
436
|
+
current = await this.getRawStack(stackId, options == null ? void 0 : options.signal);
|
|
437
|
+
}
|
|
438
|
+
if (Number(current == null ? void 0 : current.Status) === 4) {
|
|
439
|
+
const entries = Array.isArray(current.DeploymentStatus) ? current.DeploymentStatus : [];
|
|
440
|
+
const lastError = [...entries].reverse().find((item) => (item == null ? void 0 : item.Status) === 4 && (item == null ? void 0 : item.Message));
|
|
441
|
+
const message = (lastError == null ? void 0 : lastError.Message) ? `Portainer deployment failed: ${lastError.Message}` : `Portainer Stack #${stackId} entered error status`;
|
|
442
|
+
throw new PortainerApiError(message, { code: "DEPLOYMENT_FAILED", status: 502 });
|
|
443
|
+
}
|
|
444
|
+
return current;
|
|
445
|
+
}
|
|
446
|
+
async listStackContainers(endpointId, stackName, signal) {
|
|
447
|
+
const validEndpointId = assertPositiveInt(endpointId, "Environment (Endpoint) ID");
|
|
448
|
+
const trimmedName = String(stackName ?? "").trim();
|
|
449
|
+
if (!trimmedName) {
|
|
450
|
+
return [];
|
|
451
|
+
}
|
|
452
|
+
const filters = JSON.stringify({
|
|
453
|
+
label: [`com.docker.compose.project=${trimmedName}`]
|
|
454
|
+
});
|
|
455
|
+
const raw = await this.request({
|
|
456
|
+
method: "GET",
|
|
457
|
+
path: `/api/endpoints/${encodeURIComponent(String(validEndpointId))}/docker/containers/json`,
|
|
458
|
+
query: { all: 1, filters },
|
|
459
|
+
signal
|
|
460
|
+
});
|
|
461
|
+
if (!Array.isArray(raw)) {
|
|
462
|
+
return [];
|
|
463
|
+
}
|
|
464
|
+
return raw.map((container) => {
|
|
465
|
+
const state = String(container.State ?? "unknown").toLowerCase();
|
|
466
|
+
const status = String(container.Status ?? "");
|
|
467
|
+
const unhealthy = /\(unhealthy\)/i.test(status) || state === "restarting" || state === "dead";
|
|
468
|
+
const firstName = Array.isArray(container.Names) && container.Names[0] ? container.Names[0] : "";
|
|
469
|
+
return {
|
|
470
|
+
id: String(container.Id ?? "").slice(0, 12),
|
|
471
|
+
name: firstName.replace(/^\/+/, ""),
|
|
472
|
+
image: String(container.Image ?? ""),
|
|
473
|
+
state,
|
|
474
|
+
status,
|
|
475
|
+
healthy: state === "running" && !unhealthy
|
|
476
|
+
};
|
|
477
|
+
});
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
function summarizeContainersHealth(containers) {
|
|
481
|
+
const totalCount = containers.length;
|
|
482
|
+
const runningCount = containers.filter((c) => c.state === "running").length;
|
|
483
|
+
const healthy = totalCount > 0 && containers.every((c) => c.healthy);
|
|
484
|
+
return { totalCount, runningCount, healthy };
|
|
485
|
+
}
|
|
486
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
487
|
+
0 && (module.exports = {
|
|
488
|
+
PortainerApiError,
|
|
489
|
+
PortainerClient,
|
|
490
|
+
assertPositiveInt,
|
|
491
|
+
assertValidPortainerUrl,
|
|
492
|
+
assertValidWebhookId,
|
|
493
|
+
clampTimeoutMs,
|
|
494
|
+
summarizeContainersHealth,
|
|
495
|
+
toStackDetail,
|
|
496
|
+
toStackSummary
|
|
497
|
+
});
|
|
@@ -0,0 +1,57 @@
|
|
|
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 collection_exports = {};
|
|
28
|
+
__export(collection_exports, {
|
|
29
|
+
portainerInstancesCollection: () => portainerInstancesCollection
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(collection_exports);
|
|
32
|
+
var import_constants = require("./constants");
|
|
33
|
+
const portainerInstancesCollection = {
|
|
34
|
+
name: import_constants.PORTAINER_INSTANCES_COLLECTION,
|
|
35
|
+
title: "Portainer Instances",
|
|
36
|
+
hidden: true,
|
|
37
|
+
fields: [
|
|
38
|
+
{ name: "id", type: "bigInt", autoIncrement: true, primaryKey: true },
|
|
39
|
+
{ name: "name", type: "string" },
|
|
40
|
+
{ name: "baseUrl", type: "string" },
|
|
41
|
+
{ name: "apiKey", type: "text" },
|
|
42
|
+
{ name: "gitUsername", type: "string" },
|
|
43
|
+
{ name: "gitAccessToken", type: "text" },
|
|
44
|
+
{ name: "gitCredentialId", type: "integer" },
|
|
45
|
+
{ name: "allowInsecureTls", type: "boolean", defaultValue: false },
|
|
46
|
+
{ name: "defaultTimeoutMs", type: "integer", defaultValue: import_constants.DEFAULT_TIMEOUT_MS },
|
|
47
|
+
{ name: "isDefault", type: "boolean", defaultValue: false },
|
|
48
|
+
{ name: "enabled", type: "boolean", defaultValue: true },
|
|
49
|
+
{ name: "lastHealthStatus", type: "string" },
|
|
50
|
+
{ name: "lastVersion", type: "string" },
|
|
51
|
+
{ name: "lastCheckedAt", type: "date" }
|
|
52
|
+
]
|
|
53
|
+
};
|
|
54
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
55
|
+
0 && (module.exports = {
|
|
56
|
+
portainerInstancesCollection
|
|
57
|
+
});
|
|
@@ -0,0 +1,66 @@
|
|
|
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 constants_exports = {};
|
|
28
|
+
__export(constants_exports, {
|
|
29
|
+
DEFAULT_TIMEOUT_MS: () => DEFAULT_TIMEOUT_MS,
|
|
30
|
+
INSTRUCTION_GROUP_KEY: () => INSTRUCTION_GROUP_KEY,
|
|
31
|
+
INSTRUCTION_TYPES: () => INSTRUCTION_TYPES,
|
|
32
|
+
MAX_TIMEOUT_MS: () => MAX_TIMEOUT_MS,
|
|
33
|
+
MIN_TIMEOUT_MS: () => MIN_TIMEOUT_MS,
|
|
34
|
+
PLUGIN_NAMESPACE: () => PLUGIN_NAMESPACE,
|
|
35
|
+
PORTAINER_ACL_SNIPPET: () => PORTAINER_ACL_SNIPPET,
|
|
36
|
+
PORTAINER_INSTANCES_COLLECTION: () => PORTAINER_INSTANCES_COLLECTION,
|
|
37
|
+
PORTAINER_RESOURCE: () => PORTAINER_RESOURCE,
|
|
38
|
+
SECRET_MASK: () => SECRET_MASK
|
|
39
|
+
});
|
|
40
|
+
module.exports = __toCommonJS(constants_exports);
|
|
41
|
+
const PLUGIN_NAMESPACE = "plugin-workflow-portainer";
|
|
42
|
+
const PORTAINER_INSTANCES_COLLECTION = "portainerInstances";
|
|
43
|
+
const PORTAINER_RESOURCE = "portainerWorkflow";
|
|
44
|
+
const PORTAINER_ACL_SNIPPET = "pm.plugin-workflow-portainer";
|
|
45
|
+
const INSTRUCTION_GROUP_KEY = "portainer";
|
|
46
|
+
const INSTRUCTION_TYPES = {
|
|
47
|
+
redeploy: "portainer-stack-redeploy",
|
|
48
|
+
control: "portainer-stack-control"
|
|
49
|
+
};
|
|
50
|
+
const SECRET_MASK = "\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022";
|
|
51
|
+
const DEFAULT_TIMEOUT_MS = 6e5;
|
|
52
|
+
const MIN_TIMEOUT_MS = 1e3;
|
|
53
|
+
const MAX_TIMEOUT_MS = 36e5;
|
|
54
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
55
|
+
0 && (module.exports = {
|
|
56
|
+
DEFAULT_TIMEOUT_MS,
|
|
57
|
+
INSTRUCTION_GROUP_KEY,
|
|
58
|
+
INSTRUCTION_TYPES,
|
|
59
|
+
MAX_TIMEOUT_MS,
|
|
60
|
+
MIN_TIMEOUT_MS,
|
|
61
|
+
PLUGIN_NAMESPACE,
|
|
62
|
+
PORTAINER_ACL_SNIPPET,
|
|
63
|
+
PORTAINER_INSTANCES_COLLECTION,
|
|
64
|
+
PORTAINER_RESOURCE,
|
|
65
|
+
SECRET_MASK
|
|
66
|
+
});
|