-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathinstall-pipelines-app.js
More file actions
527 lines (483 loc) · 20.2 KB
/
Copy pathinstall-pipelines-app.js
File metadata and controls
527 lines (483 loc) · 20.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
#!/usr/bin/env node
// Installs the Power Platform Pipelines application package on an existing
// Dataverse environment. Replaces the manual PPAC click-through that
// ensure-pipelines-host Phase 4.B used to render. Same shape as the existing
// provision-* helpers (env-scoped POST + Location poll), with a PAC CLI
// fallback when the BAP path fails (e.g. token-audience mismatch in tenants
// where Az → BAP is rejected — the same scenario `pac-bap-shim.js` handles
// for env enumeration).
//
// Resolution order:
// 1. BAP applicationPackages list — discover the Pipelines package by
// uniqueName matching /msdyn_AppDeploymentAnchor|msdyn.*pipeline/i.
// If found and `properties.state === 'Installed'` → already installed,
// return alreadyInstalled=true (idempotent).
// 2. BAP applicationPackages install POST — submit the install. 200 sync,
// 202 + Location poll. Same polling pattern as provision-custom-host.js.
// 3. On 401/403/5xx (or transport error after retry), fall through to PAC:
// `pac application install --environment-id {envId} --application-list
// msdyn_AppDeploymentAnchor`. Same package, different client; PAC's
// first-party client ID has different BAP-RP grants than Az CLI.
// 4. Final verification: GET {instanceApiUrl}/api/data/v9.0/solutions
// ?$filter=uniquename eq 'msdyn_AppDeploymentAnchor'&$top=1 — confirms
// the solution actually landed in Dataverse, not just that the BAP op
// succeeded.
//
// POST {bapBase}/providers/Microsoft.BusinessAppPlatform/scopes/admin/environments/{envId}/applicationPackages/{uniqueName}/install?api-version={apiVersion}
// Headers:
// Authorization: Bearer {bapToken}
// Content-Type: application/json
// x-ms-correlation-id: {uuid v4}
//
// Usage:
// node install-pipelines-app.js
// --bapToken <token> --envId <guid> --instanceApiUrl <url>
// [--hostToken <token>] // for verification step (Dataverse)
// [--allowPacFallback] // try PAC CLI on BAP failure (default: true)
// [--correlationId <uuid>]
// [--timeoutSec 600]
// [--apiVersion 2022-03-01-preview]
// [--bapBase <url>]
//
// Output (JSON to stdout):
// {
// status: 'Succeeded',
// alreadyInstalled: true | false, // 200 (idempotent) vs 202 (newly installed) vs PAC path
// installPath: 'bap' | 'pac' | 'cached', // which route succeeded
// packageUniqueName: 'msdyn_AppDeploymentAnchor',
// pipelinesSolutionVersion: '9.x.y.z' | null,
// durationSec: <number>,
// correlationId: '<uuid>',
// pollAttempts: <number>,
// locationHeader: '<url>' | null,
// pacFallbackReason: '<string>' | null, // populated when installPath === 'pac'
// }
//
// Exit 0 on success, exit 1 on error (stderr includes status + body).
'use strict';
const crypto = require('crypto');
const { execFileSync } = require('child_process');
const helpers = require('./validation-helpers');
const DEFAULT_API_VERSION = '2022-03-01-preview';
const DEFAULT_BAP_BASE = 'https://api.bap.microsoft.com';
const DEFAULT_TIMEOUT_SEC = 600;
const DEFAULT_RETRY_AFTER_SEC = 10;
const POST_TIMEOUT_MS = 60000;
const POLL_TIMEOUT_MS = 30000;
// The Power Platform Pipelines application package's solution uniqueName.
// PPAC's "Install app" picker filters on this value; the BAP /applicationPackages
// list uses the same uniqueName. Listed in priority order — discovery uses the
// first match.
const PIPELINES_PACKAGE_UNIQUE_NAMES = ['msdyn_AppDeploymentAnchor'];
// Defensive secondary filter — if the catalog only exposes a package by
// displayName (some tenants), match these substrings (case-insensitive).
const PIPELINES_PACKAGE_DISPLAY_PATTERNS = [/power platform pipelines/i, /pipelines deployment/i];
const PIPELINES_SOLUTION_UNIQUE_NAME = 'msdyn_AppDeploymentAnchor';
function parseArgs(argv) {
const args = argv.slice(2);
const opts = {
bapToken: null,
envId: null,
instanceApiUrl: null,
hostToken: null,
allowPacFallback: true,
correlationId: null,
timeoutSec: DEFAULT_TIMEOUT_SEC,
apiVersion: DEFAULT_API_VERSION,
bapBase: DEFAULT_BAP_BASE,
};
for (let i = 0; i < args.length; i++) {
const a = args[i];
const next = args[i + 1];
if (a === '--bapToken' && next) opts.bapToken = args[++i];
else if (a === '--envId' && next) opts.envId = args[++i];
else if (a === '--instanceApiUrl' && next) opts.instanceApiUrl = args[++i];
else if (a === '--hostToken' && next) opts.hostToken = args[++i];
else if (a === '--no-pac-fallback') opts.allowPacFallback = false;
else if (a === '--correlationId' && next) opts.correlationId = args[++i];
else if (a === '--timeoutSec' && next) opts.timeoutSec = Number(args[++i]) || DEFAULT_TIMEOUT_SEC;
else if (a === '--apiVersion' && next) opts.apiVersion = args[++i];
else if (a === '--bapBase' && next) opts.bapBase = args[++i];
}
return opts;
}
const defaultSleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
function extractProvisioningState(data) {
if (!data || typeof data !== 'object') return null;
if (data.properties && typeof data.properties.provisioningState === 'string') {
return data.properties.provisioningState;
}
if (typeof data.state === 'string') return data.state;
if (data.status && typeof data.status === 'object' && typeof data.status.code === 'string') {
return data.status.code;
}
if (typeof data.status === 'string') return data.status;
return null;
}
function isTerminalSucceeded(state) {
if (!state) return false;
const s = String(state).toLowerCase();
return s === 'succeeded' || s === 'succeeded.' || s === 'installed';
}
function isTerminalFailed(state) {
if (!state) return false;
const s = String(state).toLowerCase();
return s === 'failed' || s === 'canceled' || s === 'cancelled';
}
function readRetryAfterSec(headers) {
if (!headers) return null;
const v = headers['retry-after'] || headers['Retry-After'];
if (!v) return null;
const n = Number(v);
return isFinite(n) && n > 0 ? n : null;
}
// Discover the Pipelines application package on this env. Returns the
// canonical package object (name + state) or null if no Pipelines package
// is exposed for this env (rare — tenant policy can hide packages).
async function discoverPackage({ bapToken, envId, apiVersion, bapBase, correlationId }) {
const cleanBase = helpers.validateBapUrl(bapBase, { allowPath: false }).replace(/\/+$/, '');
const url = `${cleanBase}/providers/Microsoft.BusinessAppPlatform/scopes/admin/environments/${encodeURIComponent(envId)}/applicationPackages?api-version=${encodeURIComponent(apiVersion)}`;
helpers.validateBapUrl(url);
const res = await helpers.makeRequest({
url,
method: 'GET',
headers: {
Authorization: `Bearer ${bapToken}`,
Accept: 'application/json',
'x-ms-correlation-id': correlationId,
},
timeout: POST_TIMEOUT_MS,
includeHeaders: true,
});
if (res.error) throw new Error(`BAP applicationPackages list failed: ${res.error}`);
if (res.statusCode === 401) throw new Error('BAP applicationPackages list returned 401 — refresh BAP token.');
if (res.statusCode === 403) {
const e = new Error(`BAP applicationPackages list returned 403 — caller lacks Power Platform admin or tenant policy denies discovery. Body: ${(res.body || '').slice(0, 300)}`);
e.statusCode = 403;
throw e;
}
if (res.statusCode !== 200) {
throw new Error(`BAP applicationPackages list returned unexpected status ${res.statusCode}: ${(res.body || '').slice(0, 500)}`);
}
let data = null;
try { data = JSON.parse(res.body); } catch { data = null; }
const items = Array.isArray(data?.value) ? data.value : [];
// Try uniqueName match first (deterministic).
for (const target of PIPELINES_PACKAGE_UNIQUE_NAMES) {
const hit = items.find((p) => (p?.properties?.uniqueName || p?.name) === target);
if (hit) return normalizePackage(hit);
}
// Fall back to displayName / localizedDescription substring match.
for (const pat of PIPELINES_PACKAGE_DISPLAY_PATTERNS) {
const hit = items.find((p) => {
const dn = p?.properties?.localizedDescription || p?.properties?.applicationName || p?.properties?.displayName || '';
return pat.test(dn);
});
if (hit) return normalizePackage(hit);
}
return null;
}
function normalizePackage(pkg) {
const props = pkg?.properties || {};
return {
uniqueName: props.uniqueName || pkg?.name || null,
displayName: props.localizedDescription || props.applicationName || props.displayName || null,
state: props.state || null,
raw: pkg,
};
}
// PAC fallback path: shells out to `pac application install`. Used when the
// BAP install POST returns 401/403/5xx.
function tryPacFallback({ envId, packageUniqueName, execImpl = execFileSync }) {
// Best-effort. PAC's argument names have varied across versions, so we try
// the modern form first and fall through to legacy on stderr signals.
const candidates = [
['application', 'install', '--environment-id', envId, '--application-list', packageUniqueName],
['application', 'install', '--environment', envId, '--application-list', packageUniqueName],
['admin', 'application', 'install', '--environment-id', envId, '--application-name-list', packageUniqueName],
];
let lastErr = null;
for (const argv of candidates) {
try {
const out = execImpl('pac', argv, {
encoding: 'utf8',
timeout: 600000,
stdio: ['ignore', 'pipe', 'pipe'],
shell: false,
});
return { ok: true, command: ['pac', ...argv], stdout: out };
} catch (err) {
lastErr = err;
// Try next candidate if PAC reports an unrecognized arg / subcommand.
const stderr = (err.stderr || err.message || '').toLowerCase();
if (!/unrecognized|unknown|invalid argument/i.test(stderr)) break;
}
}
return { ok: false, error: (lastErr && (lastErr.stderr || lastErr.message)) || 'pac fallback failed' };
}
// Verify the Pipelines solution actually landed in Dataverse after install.
// BAP can report success while the solution is still propagating. This is the
// single round-trip that confirms the install is real.
async function verifySolutionInstalled({ instanceApiUrl, hostToken }) {
if (!instanceApiUrl || !hostToken) {
return { ok: false, reason: 'instanceApiUrl or hostToken not provided — caller should verify separately' };
}
const trustedInstanceApiUrl = helpers.validateDataverseEnvironmentUrl(instanceApiUrl, 'Host Dataverse API URL');
const url = `${trustedInstanceApiUrl}/api/data/v9.0/solutions?$filter=uniquename eq '${PIPELINES_SOLUTION_UNIQUE_NAME}'&$select=uniquename,version&$top=1`;
const res = await helpers.makeRequest({
url,
method: 'GET',
headers: {
Authorization: `Bearer ${hostToken}`,
Accept: 'application/json',
'OData-Version': '4.0',
'OData-MaxVersion': '4.0',
},
timeout: POLL_TIMEOUT_MS,
});
if (res.error) return { ok: false, reason: `solutions probe failed: ${res.error}` };
if (res.statusCode !== 200) return { ok: false, reason: `solutions probe returned ${res.statusCode}` };
let data = null;
try { data = JSON.parse(res.body); } catch { data = null; }
const row = Array.isArray(data?.value) && data.value.length > 0 ? data.value[0] : null;
if (!row) return { ok: false, reason: 'solution not found post-install — propagation may still be in progress' };
return { ok: true, version: row.version || null };
}
async function installPipelinesApp(opts = {}) {
const {
bapToken,
envId,
instanceApiUrl,
hostToken = null,
allowPacFallback = true,
correlationId,
timeoutSec = DEFAULT_TIMEOUT_SEC,
apiVersion = DEFAULT_API_VERSION,
bapBase = DEFAULT_BAP_BASE,
sleepImpl = null,
nowImpl = null,
pacFallbackImpl = null,
} = opts;
if (!bapToken) throw new Error('--bapToken is required');
if (!envId) throw new Error('--envId is required');
const sleep = sleepImpl || defaultSleep;
const now = nowImpl || (() => Date.now());
const pacFallback = pacFallbackImpl || tryPacFallback;
const cleanBase = helpers.validateBapUrl(bapBase, { allowPath: false }).replace(/\/+$/, '');
const cid = correlationId || crypto.randomUUID();
const startedAt = now();
// Discovery — also catches the idempotent "already installed" path.
let pkg;
try {
pkg = await discoverPackage({ bapToken, envId, apiVersion, bapBase, correlationId: cid });
} catch (err) {
// 403 on the LIST endpoint usually means the BAP audience isn't the right
// one for this tenant. Skip straight to PAC fallback when permitted.
if (err.statusCode === 403 && allowPacFallback) {
pkg = null;
} else {
throw err;
}
}
if (pkg && isTerminalSucceeded(pkg.state)) {
// Already installed — idempotent path.
let verifyResult = null;
if (instanceApiUrl && hostToken) {
verifyResult = await verifySolutionInstalled({ instanceApiUrl, hostToken });
}
return {
status: 'Succeeded',
alreadyInstalled: true,
installPath: 'cached',
packageUniqueName: pkg.uniqueName || PIPELINES_PACKAGE_UNIQUE_NAMES[0],
pipelinesSolutionVersion: verifyResult?.version || null,
durationSec: (now() - startedAt) / 1000,
correlationId: cid,
pollAttempts: 0,
locationHeader: null,
pacFallbackReason: null,
};
}
// BAP install POST
const packageUniqueName = pkg?.uniqueName || PIPELINES_PACKAGE_UNIQUE_NAMES[0];
const postUrl = `${cleanBase}/providers/Microsoft.BusinessAppPlatform/scopes/admin/environments/${encodeURIComponent(envId)}/applicationPackages/${encodeURIComponent(packageUniqueName)}/install?api-version=${encodeURIComponent(apiVersion)}`;
helpers.validateBapUrl(postUrl);
const postRes = await helpers.makeRequest({
url: postUrl,
method: 'POST',
headers: {
Authorization: `Bearer ${bapToken}`,
'Content-Type': 'application/json',
Accept: 'application/json',
'x-ms-correlation-id': cid,
},
body: '{}',
timeout: POST_TIMEOUT_MS,
includeHeaders: true,
});
// PAC fallback on transport error / 401 / 403 / 5xx
if (
postRes.error
|| postRes.statusCode === 401
|| postRes.statusCode === 403
|| (postRes.statusCode >= 500 && postRes.statusCode < 600)
) {
if (!allowPacFallback) {
throw new Error(`BAP applicationPackages install failed (status ${postRes.statusCode || 'transport-error'}): ${(postRes.body || postRes.error || '').toString().slice(0, 500)}`);
}
const pacRes = pacFallback({ envId, packageUniqueName });
if (!pacRes.ok) {
throw new Error(`BAP install failed and PAC fallback failed: BAP=${postRes.statusCode || postRes.error}; PAC=${pacRes.error}`);
}
let verifyResult = null;
if (instanceApiUrl && hostToken) {
// PAC returns once the install is committed; still verify in Dataverse.
verifyResult = await verifySolutionInstalled({ instanceApiUrl, hostToken });
}
return {
status: 'Succeeded',
alreadyInstalled: false,
installPath: 'pac',
packageUniqueName,
pipelinesSolutionVersion: verifyResult?.version || null,
durationSec: (now() - startedAt) / 1000,
correlationId: cid,
pollAttempts: 0,
locationHeader: null,
pacFallbackReason: `BAP returned ${postRes.statusCode || postRes.error}`,
};
}
if (postRes.statusCode === 409) {
// Idempotent — install already in progress or already installed.
let verifyResult = null;
if (instanceApiUrl && hostToken) {
verifyResult = await verifySolutionInstalled({ instanceApiUrl, hostToken });
}
return {
status: 'Succeeded',
alreadyInstalled: true,
installPath: 'cached',
packageUniqueName,
pipelinesSolutionVersion: verifyResult?.version || null,
durationSec: (now() - startedAt) / 1000,
correlationId: cid,
pollAttempts: 0,
locationHeader: null,
pacFallbackReason: null,
};
}
if (postRes.statusCode !== 200 && postRes.statusCode !== 202) {
throw new Error(`BAP applicationPackages install returned unexpected status ${postRes.statusCode}: ${(postRes.body || '').slice(0, 500)}`);
}
let respBody = null;
if (postRes.body) {
try { respBody = JSON.parse(postRes.body); } catch { respBody = null; }
}
let provisioningState = extractProvisioningState(respBody) || 'Installing';
const rawLocationHeader = postRes.headers?.location || postRes.headers?.Location || null;
const locationHeader = rawLocationHeader ? helpers.validateBapUrl(rawLocationHeader) : null;
if (locationHeader && new URL(locationHeader).origin !== new URL(cleanBase).origin) {
throw new Error('BAP applicationPackages install returned a Location header for a different host.');
}
let retryAfterSec = readRetryAfterSec(postRes.headers) || DEFAULT_RETRY_AFTER_SEC;
if (postRes.statusCode === 200 && isTerminalSucceeded(provisioningState)) {
let verifyResult = null;
if (instanceApiUrl && hostToken) {
verifyResult = await verifySolutionInstalled({ instanceApiUrl, hostToken });
}
return {
status: 'Succeeded',
alreadyInstalled: false,
installPath: 'bap',
packageUniqueName,
pipelinesSolutionVersion: verifyResult?.version || null,
durationSec: (now() - startedAt) / 1000,
correlationId: cid,
pollAttempts: 0,
locationHeader,
pacFallbackReason: null,
};
}
if (!locationHeader) {
throw new Error('BAP applicationPackages install returned 202 but no Location header — cannot poll for completion.');
}
let pollAttempts = 0;
const deadline = startedAt + timeoutSec * 1000;
while (now() < deadline) {
if (isTerminalSucceeded(provisioningState) || isTerminalFailed(provisioningState)) break;
await sleep(retryAfterSec * 1000);
pollAttempts++;
const pollRes = await helpers.makeRequest({
url: locationHeader,
method: 'GET',
headers: {
Authorization: `Bearer ${bapToken}`,
Accept: 'application/json',
'x-ms-correlation-id': cid,
},
timeout: POLL_TIMEOUT_MS,
includeHeaders: true,
});
if (pollRes.error) continue;
if (pollRes.statusCode === 401) {
throw new Error('Polling returned 401 mid-install — token expired. Re-run after re-authenticating; the install may still be in progress.');
}
if (pollRes.statusCode >= 500) continue;
if (pollRes.statusCode !== 200 && pollRes.statusCode !== 202) {
throw new Error(`Polling returned unexpected status ${pollRes.statusCode}: ${(pollRes.body || '').slice(0, 500)}`);
}
let pollData = null;
try { pollData = JSON.parse(pollRes.body || '{}'); } catch { pollData = null; }
const newState = extractProvisioningState(pollData);
if (newState) provisioningState = newState;
const newRetryAfter = readRetryAfterSec(pollRes.headers);
if (newRetryAfter) retryAfterSec = newRetryAfter;
}
if (isTerminalSucceeded(provisioningState)) {
let verifyResult = null;
if (instanceApiUrl && hostToken) {
verifyResult = await verifySolutionInstalled({ instanceApiUrl, hostToken });
}
return {
status: 'Succeeded',
alreadyInstalled: false,
installPath: 'bap',
packageUniqueName,
pipelinesSolutionVersion: verifyResult?.version || null,
durationSec: (now() - startedAt) / 1000,
correlationId: cid,
pollAttempts,
locationHeader,
pacFallbackReason: null,
};
}
if (isTerminalFailed(provisioningState)) {
throw new Error(`Pipelines app install ended with state "${provisioningState}" after ${pollAttempts} poll(s). Inspect lifecycle op ${locationHeader} for details.`);
}
throw new Error(`Pipelines app install timed out after ${timeoutSec}s (${pollAttempts} polls); last state: ${provisioningState}.`);
}
if (require.main === module) {
const opts = parseArgs(process.argv);
installPipelinesApp(opts)
.then((result) => {
console.log(JSON.stringify(result));
process.exit(0);
})
.catch((err) => {
process.stderr.write(`${err.message}\n`);
process.exit(1);
});
}
module.exports = {
installPipelinesApp,
discoverPackage,
verifySolutionInstalled,
extractProvisioningState,
isTerminalSucceeded,
isTerminalFailed,
readRetryAfterSec,
tryPacFallback,
PIPELINES_PACKAGE_UNIQUE_NAMES,
PIPELINES_PACKAGE_DISPLAY_PATTERNS,
PIPELINES_SOLUTION_UNIQUE_NAME,
};