saaslayer 0.4.3 → 0.5.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/bin/saaslayer.js +23 -11
- package/package.json +4 -3
- package/src/cdn-aws.js +67 -0
- package/src/cdn-publication.js +146 -0
- package/src/cdn-publisher.js +166 -0
- package/src/commands/cdn.js +66 -0
- package/src/commands/deploy.js +4 -3
- package/src/commands/lifecycle.js +20 -2
- package/src/commands/operations.js +7 -2
- package/src/commands/stacks.js +2 -0
- package/src/deployments.js +5 -2
- package/src/module-catalog.js +15 -5
- package/src/sms/scope.js +4 -2
- package/src/source-branch.js +36 -0
- package/templates/saaslayer-foundation.yml +215 -281
- package/vendor/operations/index.d.ts +13 -1
- package/vendor/operations/index.js +46 -7
|
@@ -14,6 +14,9 @@ const { applySettings, fetchSchema } = require('./settings')
|
|
|
14
14
|
const { signInRefusalHint } = require('../login-methods')
|
|
15
15
|
const { releaseChoice, checkReleaseChoice, describeChoice } = require('../versions')
|
|
16
16
|
|
|
17
|
+
// A deployment whose last update failed or was blocked: `update --retry` re-runs it at its deployed release.
|
|
18
|
+
const FAILED_UPDATE_STATUSES = new Set(['UPDATE_FAILED', 'UPDATE_BLOCKED_DRIFT', 'UPDATE_BLOCKED_DRIFT_CHECK', 'UPDATE_BLOCKED_CHANGESET'])
|
|
19
|
+
|
|
17
20
|
async function status(flags) {
|
|
18
21
|
const scope = resolveScope(flags, { env: 'all' })
|
|
19
22
|
printScope(scope, flags)
|
|
@@ -67,6 +70,12 @@ async function update(flags) {
|
|
|
67
70
|
if (resolved && resolved.compat === 'breaking') console.log(` ${resolved.version} is a breaking release${resolved.notes ? `: ${resolved.notes}` : ''}`)
|
|
68
71
|
// Protected envs (staging/prod) require --reason; --force passes the drift gate (audited with the reason).
|
|
69
72
|
const attributes = { ...choice }
|
|
73
|
+
// The branch an explicit commit was pushed to, when the local checkout proves it: a pure code update may then wait
|
|
74
|
+
// behind an active run on a dev deployment instead of being refused (the deployer re-proves it from the repository)
|
|
75
|
+
if (choice.commitSha) {
|
|
76
|
+
const sourceBranch = require('../source-branch').pushedBranchOf(String(choice.commitSha))
|
|
77
|
+
if (sourceBranch) attributes.sourceBranch = sourceBranch
|
|
78
|
+
}
|
|
70
79
|
if (flags.reason) attributes.reason = flags.reason
|
|
71
80
|
if (flags.force === true || flags.force === 'true') attributes.force = true
|
|
72
81
|
const res = await apiCall('POST', `/api/deployments/${encodeURIComponent(d.deploymentId)}/update`, { data: { type: 'deployments', attributes } }, flags, { headers: requestKeyHeaders() })
|
|
@@ -85,13 +94,16 @@ async function update(flags) {
|
|
|
85
94
|
async function retry(flags, changes) {
|
|
86
95
|
if (changes) throw new Error('--retry redeploys the deployed release: leave out --version, --channel, --commit-sha and --param')
|
|
87
96
|
const d = await resolveDeployment(flags)
|
|
97
|
+
// A failed create released its stage: the control plane refuses to update it in place.
|
|
98
|
+
if (d.status === 'CREATE_FAILED') throw new Error(`${d.deploymentId} is CREATE_FAILED: a failed create cannot be retried in place; destroy it (saaslayer destroy --env ${d.env} --data-custody delete|retain), then run saaslayer deploy again`)
|
|
88
99
|
if (!d.commitSha) throw new Error(`${d.deploymentId} has no deployed release to retry (status ${d.status || '—'}); deploy it first`)
|
|
89
100
|
if (!flags.reason && PROTECTED_ENVS.has(d.env)) throw new Error(`\`update --retry\` on ${d.env} (protected) requires --reason <text>`)
|
|
90
|
-
// Sign-in methods not ready, none of which the deployer says a retry resumes (retryable): none is sent
|
|
101
|
+
// Sign-in methods not ready, none of which the deployer says a retry resumes (retryable): none is sent, unless the
|
|
102
|
+
// last update failed (the retry re-runs it).
|
|
91
103
|
const doc = await apiCall('GET', `/api/deployments/${encodeURIComponent(d.deploymentId)}/settings`, undefined, flags)
|
|
92
104
|
const report = (doc && doc.data && doc.data.attributes && doc.data.attributes.loginMethods) || []
|
|
93
105
|
const notReady = report.flatMap((state) => state.methods.filter((m) => m.stage !== 'ready'))
|
|
94
|
-
if (notReady.length && !notReady.some((m) => m.retryable)) {
|
|
106
|
+
if (notReady.length && !notReady.some((m) => m.retryable) && !FAILED_UPDATE_STATUSES.has(d.status)) {
|
|
95
107
|
console.log('nothing to retry: a retry cannot fix what is pending')
|
|
96
108
|
for (const m of notReady) console.log(` ${m.loginMethod} ${m.role} (${m.reason || m.stage}): ${m.guidance}`)
|
|
97
109
|
return null
|
|
@@ -165,6 +177,7 @@ function printPurgeResult(purgeResult) {
|
|
|
165
177
|
* @param {Record<string, any>} flags
|
|
166
178
|
*/
|
|
167
179
|
async function destroy(flags) {
|
|
180
|
+
if (flags['acknowledge-cdn-outage'] !== undefined && flags['acknowledge-cdn-outage'] !== true) throw new Error('--acknowledge-cdn-outage is a presence flag')
|
|
168
181
|
const purge = flags['purge-retained'] === true || flags['purge-retained'] === 'true'
|
|
169
182
|
const dataCustody = flags['data-custody']
|
|
170
183
|
if (purge && dataCustody !== undefined) throw new Error('--purge-retained purges what a retain destroy kept: it takes no --data-custody')
|
|
@@ -181,10 +194,12 @@ async function destroy(flags) {
|
|
|
181
194
|
console.log(`⚠ This will DESTROY ${target}, data-custody=${dataCustody}.`)
|
|
182
195
|
console.log(dataCustody === 'retain' ? ` ${RETAINED_COST_NOTICE}` : " Delete custody permanently deletes the deployment's data; this cannot be undone.")
|
|
183
196
|
}
|
|
197
|
+
if (flags['acknowledge-cdn-outage']) console.log(' CDN delivery will stop. Its immutable assets, policies and logs remain in your AWS account and may incur charges.')
|
|
184
198
|
const typed = flags.yes ? d.project : await ask(`Type the project name "${d.project}" to confirm: `)
|
|
185
199
|
if (typed !== d.project) { console.log('Aborted (name did not match).'); return null }
|
|
186
200
|
const query = new URLSearchParams(purge ? { purgeRetained: 'true' } : { dataCustody })
|
|
187
201
|
if (reason) query.set('reason', reason)
|
|
202
|
+
if (flags['acknowledge-cdn-outage'] === true) query.set('acknowledgeCdnOutage', 'true')
|
|
188
203
|
let res
|
|
189
204
|
try {
|
|
190
205
|
res = await jsonApiCall('DELETE', `/api/deployments/${encodeURIComponent(d.deploymentId)}?${query}`, undefined, flags)
|
|
@@ -230,6 +245,7 @@ async function serviceAdd(service, flags) {
|
|
|
230
245
|
}
|
|
231
246
|
|
|
232
247
|
async function serviceRemove(service, flags) {
|
|
248
|
+
if (flags['acknowledge-cdn-outage'] !== undefined && flags['acknowledge-cdn-outage'] !== true) throw new Error('--acknowledge-cdn-outage is a presence flag')
|
|
233
249
|
if (!service) throw new Error('`service remove` requires a service name')
|
|
234
250
|
const dataCustody = flags['data-custody']
|
|
235
251
|
if (dataCustody !== 'delete' && dataCustody !== 'retain') throw new Error('`service remove` requires --data-custody delete|retain')
|
|
@@ -239,6 +255,8 @@ async function serviceRemove(service, flags) {
|
|
|
239
255
|
if (isProtected && !flags.reason) throw new Error(`\`service remove\` on ${d.env} (protected) requires --reason <text>`)
|
|
240
256
|
const query = new URLSearchParams({ dataCustody })
|
|
241
257
|
if (flags.reason) query.set('reason', flags.reason)
|
|
258
|
+
if (flags['acknowledge-cdn-outage'] === true) query.set('acknowledgeCdnOutage', 'true')
|
|
259
|
+
if (flags['acknowledge-cdn-outage']) console.log('⚠ CDN delivery will stop. Its immutable assets, policies and logs remain in your AWS account and may incur charges.')
|
|
242
260
|
if (isProtected) {
|
|
243
261
|
console.log(`⚠ This will REMOVE ${service} from ${d.project}/${d.env}/${d.region} (${d.deploymentId}), data-custody=${dataCustody}.`)
|
|
244
262
|
const typed = flags.yes ? service : await ask(`Type the service name "${service}" to confirm: `)
|
|
@@ -60,8 +60,9 @@ async function fetchOperation(deploymentId, operationId, flags) {
|
|
|
60
60
|
}
|
|
61
61
|
|
|
62
62
|
/**
|
|
63
|
-
* Wait for an operation to finish. Succeeded: prints its components and returns it.
|
|
64
|
-
*
|
|
63
|
+
* Wait for an operation to finish. Succeeded: prints its components and returns it. Superseded (a newer request
|
|
64
|
+
* replaced it before it started): prints that and returns it. Failed or partial: throws an error whose message lists
|
|
65
|
+
* each component's outcome and the next supported command.
|
|
65
66
|
* @param {{ deploymentId: string, operationId: string, flags: object }} input
|
|
66
67
|
*/
|
|
67
68
|
async function followOperation({ deploymentId, operationId, flags }) {
|
|
@@ -78,6 +79,10 @@ async function followOperation({ deploymentId, operationId, flags }) {
|
|
|
78
79
|
console.log(lines.join('\n'))
|
|
79
80
|
return final
|
|
80
81
|
}
|
|
82
|
+
if (final.status === 'superseded') {
|
|
83
|
+
console.log([...lines, ' replaced by a newer request before it started; nothing was deployed by this one'].join('\n'))
|
|
84
|
+
return final
|
|
85
|
+
}
|
|
81
86
|
throw Object.assign(new Error(lines.join('\n')), { operation: final })
|
|
82
87
|
}
|
|
83
88
|
|
package/src/commands/stacks.js
CHANGED
|
@@ -10,6 +10,8 @@ async function stacks(flags) {
|
|
|
10
10
|
const a = r.attributes || {}
|
|
11
11
|
console.log(` ${r.id}${a.displayName ? ` ${a.displayName}` : ''}${a.description ? ' — ' + a.description : ''}`)
|
|
12
12
|
console.log(` regions: ${a.supportedRegions ? a.supportedRegions.join(', ') : 'any'}`)
|
|
13
|
+
if (a.hostingModes) console.log(` hosting: ${a.hostingModes.map(mode => mode === 'byoa' ? 'your connected AWS account (BYOA)' : 'SaaSLayer-hosted').join(', ')}`)
|
|
14
|
+
if (a.minimumFoundationVersion) console.log(` foundation: >= ${a.minimumFoundationVersion}; upgrade with saaslayer foundation upgrade`)
|
|
13
15
|
const deps = a.dependencies || []
|
|
14
16
|
if (deps.length) console.log(` requires: ${deps.map((d) => `${d.module} (${d.phase})`).join(', ')}`)
|
|
15
17
|
const stable = (a.outputs || []).filter((o) => o.stability === 'stable').map((o) => o.name)
|
package/src/deployments.js
CHANGED
|
@@ -11,13 +11,16 @@ async function resolveDeployment(flags) {
|
|
|
11
11
|
const scope = resolveScope(flags, { env: 'prod' })
|
|
12
12
|
printScope(scope, flags)
|
|
13
13
|
const project = scope.project.value
|
|
14
|
-
const env = scope.env.value
|
|
15
14
|
const region = flags.region
|
|
16
15
|
const view = await apiCall('GET', `/api/projects/${encodeURIComponent(project)}/deployments`, undefined, flags)
|
|
16
|
+
const selected = typeof flags.deployment === 'string' && flags.deployment.startsWith('dep_') ? flags.deployment : null
|
|
17
|
+
// --deployment <id> without --env names the deployment in whichever environment holds it (not the default prod).
|
|
18
|
+
const holder = selected && typeof flags.env !== 'string' ? (view.environments || []).find((g) => (g.instances || []).some((i) => i.deploymentId === selected)) : null
|
|
19
|
+
if (selected && typeof flags.env !== 'string' && !holder) throw new Error(`no deployment ${selected} for project ${project} (${scope.project.kind})`)
|
|
20
|
+
const env = holder ? holder.environment : scope.env.value
|
|
17
21
|
const group = (view.environments || []).find((g) => g.environment === env)
|
|
18
22
|
let instances = group ? group.instances : []
|
|
19
23
|
if (region) instances = instances.filter((i) => i.region === region)
|
|
20
|
-
const selected = typeof flags.deployment === 'string' && flags.deployment.startsWith('dep_') ? flags.deployment : null
|
|
21
24
|
if (selected) instances = instances.filter((i) => i.deploymentId === selected)
|
|
22
25
|
// A failed or destroyed deployment never shadows a live one in the same env; alone, it still resolves (status, destroy).
|
|
23
26
|
const live = instances.filter((i) => !DEAD_STATUSES.has(i.status))
|
package/src/module-catalog.js
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
// module ids, required dependencies, and the module keys of --param flags.
|
|
3
3
|
'use strict'
|
|
4
4
|
const { apiCall } = require('./api-client')
|
|
5
|
+
const moduleType = id => id.split('@')[0]
|
|
6
|
+
const validRef = id => /^[a-z][a-z0-9-]*(?:@[a-z][a-z0-9]{0,11})?$/.test(id)
|
|
5
7
|
|
|
6
8
|
/**
|
|
7
9
|
* @typedef {{ id: string, attributes: { dependencies?: Array<{ module: string, phase: string }> } & Record<string, unknown> }} CatalogModule
|
|
@@ -35,7 +37,7 @@ function unknownModuleMessage(name, modules) {
|
|
|
35
37
|
*/
|
|
36
38
|
function checkModuleIds(ids, modules) {
|
|
37
39
|
for (const id of ids) {
|
|
38
|
-
if (!modules.some((m) => m.id === id)) throw new Error(unknownModuleMessage(id, modules))
|
|
40
|
+
if (!validRef(id) || !modules.some((m) => m.id === moduleType(id))) throw new Error(unknownModuleMessage(id, modules))
|
|
39
41
|
}
|
|
40
42
|
}
|
|
41
43
|
|
|
@@ -47,9 +49,9 @@ function checkModuleIds(ids, modules) {
|
|
|
47
49
|
*/
|
|
48
50
|
function checkDependencies(ids, modules) {
|
|
49
51
|
for (const id of ids) {
|
|
50
|
-
const m = modules.find((x) => x.id === id)
|
|
52
|
+
const m = modules.find((x) => x.id === moduleType(id))
|
|
51
53
|
for (const dep of ((m && m.attributes.dependencies) || []).filter((d) => d.phase !== 'seed')) {
|
|
52
|
-
if (!ids.
|
|
54
|
+
if (!ids.some(selected => moduleType(selected) === dep.module)) throw new Error(`${id} needs ${dep.module} in the same deployment (${dep.phase}) — add it to --services`)
|
|
53
55
|
}
|
|
54
56
|
}
|
|
55
57
|
}
|
|
@@ -60,8 +62,16 @@ function checkDependencies(ids, modules) {
|
|
|
60
62
|
*/
|
|
61
63
|
function checkParamModules(parameters, modules) {
|
|
62
64
|
for (const [id, values] of Object.entries(parameters)) {
|
|
63
|
-
if (!modules.some((m) => m.id === id)) throw new Error(`--param ${id}.${Object.keys(values)[0]}: ${unknownModuleMessage(id, modules)}`)
|
|
65
|
+
if (!validRef(id) || !modules.some((m) => m.id === moduleType(id))) throw new Error(`--param ${id}.${Object.keys(values)[0]}: ${unknownModuleMessage(id, modules)}`)
|
|
64
66
|
}
|
|
65
67
|
}
|
|
66
68
|
|
|
67
|
-
|
|
69
|
+
/** Refuse an explicitly incompatible hosting choice before sending a deployment mutation. Automatic placement stays server-authoritative. */
|
|
70
|
+
function checkHosting(ids, modules, hosting) {
|
|
71
|
+
if (!hosting) return
|
|
72
|
+
for (const id of ids) {
|
|
73
|
+
const modes = modules.find(m => m.id === moduleType(id))?.attributes.hostingModes
|
|
74
|
+
if (Array.isArray(modes) && !modes.includes(hosting)) throw new Error(`${id} supports ${modes.join(', ')} hosting; use --hosting byoa with your connected AWS account. Hosted CDN publishing is not available in v1.`)
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
module.exports = { fetchModuleCatalog, checkModuleIds, checkDependencies, checkParamModules, checkHosting }
|
package/src/sms/scope.js
CHANGED
|
@@ -16,7 +16,8 @@ const DEFAULT_SMS_SENDER = 'default'
|
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
18
|
/**
|
|
19
|
-
* Resolve the target account and region of `--env` (and `--region` when the env spans several
|
|
19
|
+
* Resolve the target account and region of `--env` (and `--region` when the env spans several, `--target` when the
|
|
20
|
+
* project has several connected accounts). Prints the scope line and
|
|
20
21
|
* the target line on stderr (--json suppresses both).
|
|
21
22
|
* @param {Record<string, any>} flags
|
|
22
23
|
* @param {ScopeDeps} [deps]
|
|
@@ -27,7 +28,8 @@ async function resolveSmsTarget(flags, { api = jsonApiCall, log = console.error,
|
|
|
27
28
|
const scope = resolveScope(flags, { env: flags.env })
|
|
28
29
|
if (!flags.json) log(formatScopeLine(scope))
|
|
29
30
|
const project = String(scope.project.value)
|
|
30
|
-
|
|
31
|
+
// --target names the connected account when the project has several (the same flag as `saaslayer deploy --target`).
|
|
32
|
+
const query = new URLSearchParams({ environment: flags.env, ...(typeof flags.region === 'string' ? { region: flags.region } : {}), ...(typeof flags.target === 'string' ? { targetAccountId: flags.target } : {}) })
|
|
31
33
|
const doc = await api('GET', `/api/projects/${encodeURIComponent(project)}/sms-target?${query}`, undefined, flags)
|
|
32
34
|
const a = doc.data.attributes
|
|
33
35
|
/** @type {SmsTarget} */
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// The branch a commit was pushed to, from the local checkout, only when that is certain: HEAD is the commit, the
|
|
2
|
+
// current branch tracks a remote branch, and that remote branch is at the same commit. Anything else is unknown.
|
|
3
|
+
'use strict'
|
|
4
|
+
const { execFileSync } = require('child_process')
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @param {string} cwd
|
|
8
|
+
* @param {string[]} args
|
|
9
|
+
* @returns {string|null}
|
|
10
|
+
*/
|
|
11
|
+
function git(cwd, args) {
|
|
12
|
+
try {
|
|
13
|
+
return execFileSync('git', args, { cwd, encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }).trim()
|
|
14
|
+
} catch {
|
|
15
|
+
return null
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @param {string} commitSha full 40-character SHA
|
|
21
|
+
* @param {{ cwd?: string }} [opts]
|
|
22
|
+
* @returns {string|null} refs/heads/<name>, or null when not reliably known
|
|
23
|
+
*/
|
|
24
|
+
function pushedBranchOf(commitSha, { cwd = process.cwd() } = {}) {
|
|
25
|
+
if (git(cwd, ['rev-parse', 'HEAD']) !== commitSha) return null
|
|
26
|
+
const branch = git(cwd, ['symbolic-ref', '--quiet', '--short', 'HEAD'])
|
|
27
|
+
if (!branch) return null
|
|
28
|
+
const upstream = git(cwd, ['rev-parse', '--abbrev-ref', '--symbolic-full-name', '@{upstream}'])
|
|
29
|
+
if (!upstream) return null
|
|
30
|
+
if (git(cwd, ['rev-parse', '@{upstream}']) !== commitSha) return null
|
|
31
|
+
// The upstream's branch name on its remote (origin/feat/x -> feat/x)
|
|
32
|
+
const remoteBranch = upstream.slice(upstream.indexOf('/') + 1)
|
|
33
|
+
return remoteBranch ? `refs/heads/${remoteBranch}` : null
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
module.exports = { pushedBranchOf }
|