@faable/faable 4.29.0 → 4.30.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/dist/api/FaableApi.js +23 -0
- package/dist/api/context.js +19 -17
- package/dist/api/strategies/session.strategy.js +31 -0
- package/dist/commands/deploy/volumes/clone.js +140 -0
- package/dist/commands/deploy/volumes/index.js +3 -1
- package/dist/commands/deploy/volumes/restore.js +1 -1
- package/package.json +1 -1
package/dist/api/FaableApi.js
CHANGED
|
@@ -400,6 +400,29 @@ class FaableApi {
|
|
|
400
400
|
async undoVolumeOperation(team, operation_id) {
|
|
401
401
|
return data(this.client.post(`/volume-operation/${operation_id}/undo`, undefined, { headers: projectHeader(team) }));
|
|
402
402
|
}
|
|
403
|
+
async listDatabases(team, app_id) {
|
|
404
|
+
return data(this.client.get(`/database`, {
|
|
405
|
+
params: { app_id },
|
|
406
|
+
headers: projectHeader(team)
|
|
407
|
+
}));
|
|
408
|
+
}
|
|
409
|
+
async exportDatabase(team, database_id) {
|
|
410
|
+
return data(this.client.post(`/database/${database_id}/export`, undefined, { headers: projectHeader(team) }));
|
|
411
|
+
}
|
|
412
|
+
async getDatabaseExport(team, database_id, export_id) {
|
|
413
|
+
return data(this.client.get(`/database/${database_id}/export/${export_id}`, { headers: projectHeader(team) }));
|
|
414
|
+
}
|
|
415
|
+
async importDatabase(team, database_id) {
|
|
416
|
+
return data(this.client.post(`/database/${database_id}/import`, undefined, {
|
|
417
|
+
headers: projectHeader(team)
|
|
418
|
+
}));
|
|
419
|
+
}
|
|
420
|
+
async getDatabaseImport(team, database_id, import_id) {
|
|
421
|
+
return data(this.client.get(`/database/${database_id}/import/${import_id}`, { headers: projectHeader(team) }));
|
|
422
|
+
}
|
|
423
|
+
async applyDatabaseImport(team, database_id, import_id) {
|
|
424
|
+
return data(this.client.post(`/database/${database_id}/import/${import_id}/apply`, undefined, { headers: projectHeader(team) }));
|
|
425
|
+
}
|
|
403
426
|
async restoreVolume(team, volume_id, backup_id) {
|
|
404
427
|
return data(this.client.post(`/volume/${volume_id}/restore`, { backup_id }, { headers: projectHeader(team) }));
|
|
405
428
|
}
|
package/dist/api/context.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import { FaableApi } from './FaableApi.js';
|
|
2
|
-
import { apikey_strategy } from './strategies/apikey.strategy.js';
|
|
3
1
|
import { getIDToken } from '@actions/core';
|
|
4
|
-
import { oidc_strategy } from './strategies/oidc.strategy.js';
|
|
5
|
-
import { bearer_strategy } from './strategies/bearer.strategy.js';
|
|
6
2
|
import { CredentialsStore } from '../lib/CredentialsStore.js';
|
|
7
|
-
import { loadLiveCredentials } from './session.js';
|
|
8
|
-
import { configuredProject, resolveProjectRef } from './project.js';
|
|
9
3
|
import { CliError, NOT_LOGGED_IN } from '../lib/errors.js';
|
|
4
|
+
import { FaableApi } from './FaableApi.js';
|
|
5
|
+
import { configuredProject, resolveProjectRef } from './project.js';
|
|
6
|
+
import { loadLiveCredentials } from './session.js';
|
|
7
|
+
import { apikey_strategy } from './strategies/apikey.strategy.js';
|
|
8
|
+
import { bearer_strategy } from './strategies/bearer.strategy.js';
|
|
9
|
+
import { oidc_strategy } from './strategies/oidc.strategy.js';
|
|
10
|
+
import { session_strategy } from './strategies/session.strategy.js';
|
|
10
11
|
|
|
11
12
|
const context = async (targetAppId) => {
|
|
12
13
|
let api;
|
|
@@ -23,22 +24,22 @@ const context = async (targetAppId) => {
|
|
|
23
24
|
const apikey = process.env.FAABLE_API_KEY;
|
|
24
25
|
api = FaableApi.create({ authStrategy: apikey_strategy, auth: { apikey } });
|
|
25
26
|
}
|
|
26
|
-
else if (process.env.GITHUB_ACTIONS ===
|
|
27
|
+
else if (process.env.GITHUB_ACTIONS === 'true') {
|
|
27
28
|
// Github actions environment
|
|
28
29
|
try {
|
|
29
|
-
const idToken = process.env.FAABLE_ID_TOKEN || (await getIDToken(
|
|
30
|
+
const idToken = process.env.FAABLE_ID_TOKEN || (await getIDToken('https://faable.com'));
|
|
30
31
|
// targetAppId disambiguates a monorepo (several apps, one repo) in the
|
|
31
32
|
// OIDC exchange — from `faable deploy --app <app_id>`.
|
|
32
33
|
api = FaableApi.create({
|
|
33
34
|
authStrategy: oidc_strategy,
|
|
34
|
-
auth: { idToken, appId: targetAppId }
|
|
35
|
+
auth: { idToken, appId: targetAppId }
|
|
35
36
|
});
|
|
36
37
|
}
|
|
37
38
|
catch (_) {
|
|
38
39
|
console.error("Error fetching token, configure 'permissions: id-token: write'");
|
|
39
40
|
}
|
|
40
41
|
}
|
|
41
|
-
else if (process.env.GITLAB_CI ===
|
|
42
|
+
else if (process.env.GITLAB_CI === 'true' && process.env.FAABLE_ID_TOKEN) {
|
|
42
43
|
// GitLab CI: the job declares
|
|
43
44
|
// id_tokens: { FAABLE_ID_TOKEN: { aud: https://faable.com } }
|
|
44
45
|
// and the app must trust its project (`deploy apps set --gitlab-project`).
|
|
@@ -49,8 +50,8 @@ const context = async (targetAppId) => {
|
|
|
49
50
|
auth: {
|
|
50
51
|
idToken: process.env.FAABLE_ID_TOKEN,
|
|
51
52
|
appId: targetAppId,
|
|
52
|
-
provider:
|
|
53
|
-
}
|
|
53
|
+
provider: 'gitlab'
|
|
54
|
+
}
|
|
54
55
|
});
|
|
55
56
|
}
|
|
56
57
|
else {
|
|
@@ -59,15 +60,16 @@ const context = async (targetAppId) => {
|
|
|
59
60
|
const config = await loadLiveCredentials(store);
|
|
60
61
|
if (config) {
|
|
61
62
|
if (config.token) {
|
|
63
|
+
// Refreshed mid-command too, not only here (session.strategy.ts).
|
|
62
64
|
api = FaableApi.create({
|
|
63
|
-
authStrategy:
|
|
64
|
-
auth: { token: config.token }
|
|
65
|
+
authStrategy: session_strategy,
|
|
66
|
+
auth: { token: config.token }
|
|
65
67
|
});
|
|
66
68
|
}
|
|
67
69
|
else if (config.apikey) {
|
|
68
70
|
api = FaableApi.create({
|
|
69
71
|
authStrategy: apikey_strategy,
|
|
70
|
-
auth: { apikey: config.apikey }
|
|
72
|
+
auth: { apikey: config.apikey }
|
|
71
73
|
});
|
|
72
74
|
}
|
|
73
75
|
}
|
|
@@ -83,7 +85,7 @@ const context = async (targetAppId) => {
|
|
|
83
85
|
const appId = await api?.strategy?.app_id?.();
|
|
84
86
|
return {
|
|
85
87
|
api,
|
|
86
|
-
appId
|
|
88
|
+
appId
|
|
87
89
|
};
|
|
88
90
|
};
|
|
89
91
|
// Resolve the API client for a command that needs an authenticated session.
|
|
@@ -93,7 +95,7 @@ const context = async (targetAppId) => {
|
|
|
93
95
|
const requireApi = async (targetAppId) => {
|
|
94
96
|
const ctx = await context(targetAppId);
|
|
95
97
|
if (!ctx.api)
|
|
96
|
-
throw new CliError(
|
|
98
|
+
throw new CliError('not_logged_in', NOT_LOGGED_IN);
|
|
97
99
|
return ctx;
|
|
98
100
|
};
|
|
99
101
|
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { isTokenLive, loadLiveCredentials } from '../session.js';
|
|
2
|
+
|
|
3
|
+
// The `faable login` session, checked on every request instead of once per
|
|
4
|
+
// command: a command that waits longer than the access token lives — a
|
|
5
|
+
// volume restore or clone, `deploy run --timeout 3600` — used to die halfway
|
|
6
|
+
// with «session expired» (2026-11-03, a clone of a WordPress). While the
|
|
7
|
+
// token is live nothing is read; when it is not, ONE refresh is in flight and
|
|
8
|
+
// every request of that moment waits for it: two refreshes with the same
|
|
9
|
+
// refresh token could have the second one revoke the session.
|
|
10
|
+
const session_strategy = ({ token, load = () => loadLiveCredentials() }) => {
|
|
11
|
+
let current = token;
|
|
12
|
+
let inflight = null;
|
|
13
|
+
return {
|
|
14
|
+
headers: async () => {
|
|
15
|
+
if (!isTokenLive(current)) {
|
|
16
|
+
inflight ??= load()
|
|
17
|
+
.then(c => {
|
|
18
|
+
if (c?.token)
|
|
19
|
+
current = c.token;
|
|
20
|
+
})
|
|
21
|
+
.finally(() => {
|
|
22
|
+
inflight = null;
|
|
23
|
+
});
|
|
24
|
+
await inflight;
|
|
25
|
+
}
|
|
26
|
+
return { Authorization: `Bearer ${current}` };
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export { session_strategy };
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
import { CliError } from '../../../lib/errors.js';
|
|
3
|
+
import { confirm } from '../../../lib/interactive.js';
|
|
4
|
+
import { print_json, json_option } from '../../../lib/listing.js';
|
|
5
|
+
import { log } from '../../../log.js';
|
|
6
|
+
import { resolve_volume, volume_option } from './shared.js';
|
|
7
|
+
import { require_completed, wait_operation, state_label } from './operation.js';
|
|
8
|
+
import { app_option } from '../schedule/shared.js';
|
|
9
|
+
|
|
10
|
+
const POLL_MS = 3000;
|
|
11
|
+
const sleep = (ms) => new Promise(r => setTimeout(r, ms));
|
|
12
|
+
/** Polls `get` until `done` says so; the database side has no operation row. */
|
|
13
|
+
const poll = async (get, done) => {
|
|
14
|
+
for (;;) {
|
|
15
|
+
const v = await get();
|
|
16
|
+
if (done(v))
|
|
17
|
+
return v;
|
|
18
|
+
await sleep(POLL_MS);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
/** The one ready database of an app, or a clear refusal. */
|
|
22
|
+
const database_of = async (api, team, app_id, name) => {
|
|
23
|
+
const { results } = await api.listDatabases(team, app_id);
|
|
24
|
+
const ready = results.filter(d => d.state === 'ready');
|
|
25
|
+
if (ready.length !== 1) {
|
|
26
|
+
throw new CliError('not_found', ready.length === 0
|
|
27
|
+
? `${name} has no ready database to clone ${results.length ? `(it is ${results[0].state})` : ''}`.trim()
|
|
28
|
+
: `${name} has ${ready.length} databases: clone them by hand (database export/import)`);
|
|
29
|
+
}
|
|
30
|
+
return ready[0];
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* The source database's dump into the target's: export, stream the .sql.gz
|
|
34
|
+
* from its signed download straight to the import's signed upload (nothing
|
|
35
|
+
* on disk), apply — which backs the target database up first.
|
|
36
|
+
*/
|
|
37
|
+
const clone_database = async (api, team, from, to, quiet) => {
|
|
38
|
+
const src = await database_of(api, team, from.id, from.name);
|
|
39
|
+
const dst = await database_of(api, team, to.id, to.name);
|
|
40
|
+
if (!quiet)
|
|
41
|
+
log.info(`🗃 Exporting ${from.name}'s database`);
|
|
42
|
+
const { export_id } = await api.exportDatabase(team, src.id);
|
|
43
|
+
const exp = await poll(() => api.getDatabaseExport(team, src.id, export_id), e => e.state === 'completed' || e.state === 'failed');
|
|
44
|
+
if (exp.state !== 'completed' || !exp.url) {
|
|
45
|
+
throw new CliError('operation_failed', `The export of ${from.name}'s database failed: ${exp.error ?? 'no reason given'}`);
|
|
46
|
+
}
|
|
47
|
+
const imp = await api.importDatabase(team, dst.id);
|
|
48
|
+
if (!quiet)
|
|
49
|
+
log.info(`🗃 Loading it into ${to.name}'s database (a backup of it goes first)`);
|
|
50
|
+
const download = await axios.get(exp.url, {
|
|
51
|
+
responseType: 'stream',
|
|
52
|
+
timeout: 0
|
|
53
|
+
});
|
|
54
|
+
const length = download.headers['content-length'] ?? exp.size_bytes;
|
|
55
|
+
await axios.put(imp.upload_url, download.data, {
|
|
56
|
+
headers: length != null ? { 'content-length': String(length) } : {},
|
|
57
|
+
maxBodyLength: Infinity,
|
|
58
|
+
timeout: 0
|
|
59
|
+
});
|
|
60
|
+
await api.applyDatabaseImport(team, dst.id, imp.import_id);
|
|
61
|
+
const done = await poll(() => api.getDatabaseImport(team, dst.id, imp.import_id), i => i.state === 'completed' || i.state === 'failed');
|
|
62
|
+
if (done.state !== 'completed') {
|
|
63
|
+
throw new CliError('operation_failed', `The import into ${to.name}'s database failed: ${done.error ?? 'no reason given'}`);
|
|
64
|
+
}
|
|
65
|
+
return { export_id, import_id: imp.import_id };
|
|
66
|
+
};
|
|
67
|
+
const volumes_clone = {
|
|
68
|
+
command: 'clone',
|
|
69
|
+
describe: "Make an app a copy of another of the project: its volume (and with --database its database) replaced by the other's",
|
|
70
|
+
builder: yargs => volume_option(app_option(json_option(yargs)))
|
|
71
|
+
.option('from', {
|
|
72
|
+
type: 'string',
|
|
73
|
+
demandOption: true,
|
|
74
|
+
description: 'The app to copy (id, name or slug), in the same project'
|
|
75
|
+
})
|
|
76
|
+
.option('backup', {
|
|
77
|
+
type: 'string',
|
|
78
|
+
description: 'Copy one of its backups instead of what it holds now (see volumes backups -a <it>)'
|
|
79
|
+
})
|
|
80
|
+
.option('database', {
|
|
81
|
+
type: 'boolean',
|
|
82
|
+
default: false,
|
|
83
|
+
description: "Its database's data too (each app has its own)"
|
|
84
|
+
})
|
|
85
|
+
.option('yes', {
|
|
86
|
+
alias: 'y',
|
|
87
|
+
type: 'boolean',
|
|
88
|
+
default: false,
|
|
89
|
+
description: 'Skip the confirmation prompt'
|
|
90
|
+
})
|
|
91
|
+
.example('$0 deploy volumes clone --from my-blog --database -a my-blog-staging', 'A WordPress staging: the files and the database of my-blog')
|
|
92
|
+
.showHelpOnFail(false),
|
|
93
|
+
handler: async (args) => {
|
|
94
|
+
const to = await resolve_volume(args.app, args.volume);
|
|
95
|
+
const from = await resolve_volume(args.from, args.volume);
|
|
96
|
+
if (from.app_id === to.app_id) {
|
|
97
|
+
throw new CliError('usage', 'An app is not cloned onto itself: -a is the copy, --from the original');
|
|
98
|
+
}
|
|
99
|
+
if (from.team !== to.team) {
|
|
100
|
+
throw new CliError('usage', `${from.app.name} and ${to.app.name} are in different projects`);
|
|
101
|
+
}
|
|
102
|
+
const { api, team } = to;
|
|
103
|
+
const go = await confirm({
|
|
104
|
+
message: `Replace everything in ${to.app.name}/${to.volume.name}${args.database ? ' and its database' : ''} with a copy of ${from.app.name}${args.backup ? ` (backup ${args.backup})` : ''}? ${to.app.name} stops while it restores; what it holds now is backed up first.`,
|
|
105
|
+
yes: args.yes
|
|
106
|
+
});
|
|
107
|
+
if (!go) {
|
|
108
|
+
log.info('Cancelled.');
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
const quiet = !!args.json;
|
|
112
|
+
const onState = (s) => {
|
|
113
|
+
if (!quiet && s !== 'completed' && s !== 'failed')
|
|
114
|
+
log.info(state_label[s]);
|
|
115
|
+
};
|
|
116
|
+
let backup_id = args.backup;
|
|
117
|
+
if (!backup_id) {
|
|
118
|
+
if (!quiet)
|
|
119
|
+
log.info(`🗄 Backing up ${from.app.name}/${from.volume.name}, the copy`);
|
|
120
|
+
const b = await api.createVolumeOperation(team, from.volume.id, 'backup');
|
|
121
|
+
backup_id = require_completed(await wait_operation(api, team, b, { onState })).id;
|
|
122
|
+
}
|
|
123
|
+
if (!quiet)
|
|
124
|
+
log.info(`♻️ Restoring it into ${to.app.name}/${to.volume.name} (its current contents are backed up first)`);
|
|
125
|
+
const r = await api.restoreVolume(team, to.volume.id, backup_id);
|
|
126
|
+
const restored = require_completed(await wait_operation(api, team, r, { onState }));
|
|
127
|
+
const db = args.database
|
|
128
|
+
? await clone_database(api, team, { id: from.app_id, name: from.app.name }, { id: to.app_id, name: to.app.name }, quiet)
|
|
129
|
+
: null;
|
|
130
|
+
if (args.json)
|
|
131
|
+
return print_json({ backup_id, restore: restored, database: db });
|
|
132
|
+
log.info(`✅ ${to.app.name} is a copy of ${from.app.name}.`);
|
|
133
|
+
log.info(`To undo the files: faable deploy volumes restore ${restored.after_id ?? '<the backup before it>'} -a ${to.app.name}`);
|
|
134
|
+
// Files and data come as they are, and some of them name the original.
|
|
135
|
+
log.info(`⚠️ What names ${from.app.name}'s domain came along: in a WordPress, a WP_HOME in wp-config-local.php, and the links in the database. ` +
|
|
136
|
+
`faable deploy run -a ${to.app.name} -- wp search-replace https://${from.app.url} https://${to.app.url} --all-tables`);
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
export { volumes_clone };
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { volumes_backups, volumes_backup } from './backups.js';
|
|
2
|
+
import { volumes_clone } from './clone.js';
|
|
2
3
|
import { volumes_get, volumes_export } from './export.js';
|
|
3
4
|
import { volumes_import, volumes_put } from './import.js';
|
|
4
5
|
import { volumes_list } from './list.js';
|
|
@@ -22,10 +23,11 @@ const volumes = {
|
|
|
22
23
|
.command(volumes_backups)
|
|
23
24
|
.command(volumes_backup)
|
|
24
25
|
.command(volumes_restore)
|
|
26
|
+
.command(volumes_clone)
|
|
25
27
|
.command(volumes_export)
|
|
26
28
|
.command(volumes_import)
|
|
27
29
|
.command(volumes_put)
|
|
28
|
-
.demandCommand(1, 'Specify a volumes command: list, ls, get, rm, backups, backup, restore, export, import or put'),
|
|
30
|
+
.demandCommand(1, 'Specify a volumes command: list, ls, get, rm, backups, backup, restore, clone, export, import or put'),
|
|
29
31
|
handler: () => {
|
|
30
32
|
// Unreachable: demandCommand(1) either routes to a subcommand or fails
|
|
31
33
|
// through the global .fail() in src/index.ts.
|
|
@@ -12,7 +12,7 @@ const volumes_restore = {
|
|
|
12
12
|
.positional('backup_id', {
|
|
13
13
|
type: 'string',
|
|
14
14
|
demandOption: true,
|
|
15
|
-
description: 'The backup (see `faable deploy volumes backups`)'
|
|
15
|
+
description: 'The backup (see `faable deploy volumes backups`). It may be one of another app of the project: a look at it without touching that app'
|
|
16
16
|
})
|
|
17
17
|
.option('yes', {
|
|
18
18
|
alias: 'y',
|