@faable/faable 4.28.0 โ 4.30.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/dist/api/FaableApi.js
CHANGED
|
@@ -392,6 +392,37 @@ class FaableApi {
|
|
|
392
392
|
async listVolumeFiles(team, volume_id, path) {
|
|
393
393
|
return data(this.client.post(`/volume/${volume_id}/list-files`, undefined, { params: path ? { path } : {}, headers: projectHeader(team) }));
|
|
394
394
|
}
|
|
395
|
+
// rm: the paths go to the volume's trash (7 days), never deleted at once.
|
|
396
|
+
async removeVolumePaths(team, volume_id, paths) {
|
|
397
|
+
return data(this.client.post(`/volume/${volume_id}/remove`, { paths }, { headers: projectHeader(team) }));
|
|
398
|
+
}
|
|
399
|
+
// rm --undo: what a remove sent to the trash comes back.
|
|
400
|
+
async undoVolumeOperation(team, operation_id) {
|
|
401
|
+
return data(this.client.post(`/volume-operation/${operation_id}/undo`, undefined, { headers: projectHeader(team) }));
|
|
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
|
+
}
|
|
395
426
|
async restoreVolume(team, volume_id, backup_id) {
|
|
396
427
|
return data(this.client.post(`/volume/${volume_id}/restore`, { backup_id }, { headers: projectHeader(team) }));
|
|
397
428
|
}
|
|
@@ -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,9 +1,11 @@
|
|
|
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';
|
|
5
6
|
import { volumes_ls } from './ls.js';
|
|
6
7
|
import { volumes_restore } from './restore.js';
|
|
8
|
+
import { volumes_rm } from './rm.js';
|
|
7
9
|
|
|
8
10
|
// The volumes of an app (api `/volume`, `/volume-operation`): today the ones
|
|
9
11
|
// a blueprint defines (WordPress: `site` at /faable/app). Hidden until the
|
|
@@ -11,19 +13,21 @@ import { volumes_restore } from './restore.js';
|
|
|
11
13
|
// describe below. Arch: deploy/volumenes-cli-comparativa.md (backlog ยง460).
|
|
12
14
|
const volumes = {
|
|
13
15
|
command: 'volumes <command>',
|
|
14
|
-
// 'Manage the volumes of an app: usage, files (ls, get, put), backups, restore, export and import'
|
|
16
|
+
// 'Manage the volumes of an app: usage, files (ls, get, put, rm), backups, restore, export and import'
|
|
15
17
|
describe: false,
|
|
16
18
|
builder: yargs => yargs
|
|
17
19
|
.command(volumes_list)
|
|
18
20
|
.command(volumes_ls)
|
|
19
21
|
.command(volumes_get)
|
|
22
|
+
.command(volumes_rm)
|
|
20
23
|
.command(volumes_backups)
|
|
21
24
|
.command(volumes_backup)
|
|
22
25
|
.command(volumes_restore)
|
|
26
|
+
.command(volumes_clone)
|
|
23
27
|
.command(volumes_export)
|
|
24
28
|
.command(volumes_import)
|
|
25
29
|
.command(volumes_put)
|
|
26
|
-
.demandCommand(1, 'Specify a volumes command: list, ls, get, 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'),
|
|
27
31
|
handler: () => {
|
|
28
32
|
// Unreachable: demandCommand(1) either routes to a subcommand or fails
|
|
29
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',
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { CliError } from '../../../lib/errors.js';
|
|
2
|
+
import { confirm } from '../../../lib/interactive.js';
|
|
3
|
+
import { print_json, json_option } from '../../../lib/listing.js';
|
|
4
|
+
import { log } from '../../../log.js';
|
|
5
|
+
import { resolve_app, app_option } from '../schedule/shared.js';
|
|
6
|
+
import { wait_operation, require_completed, volume_dest, state_label } from './operation.js';
|
|
7
|
+
import { resolve_volume, wait_option, volume_option } from './shared.js';
|
|
8
|
+
|
|
9
|
+
// rm never deletes on the spot (backlog ยง460): the paths go to the volume's
|
|
10
|
+
// trash, `--undo <rm id>` brings them back, and they are emptied 7 days later.
|
|
11
|
+
const volumes_rm = {
|
|
12
|
+
command: 'rm [paths..]',
|
|
13
|
+
describe: 'Move files or directories of a volume to its trash, with the app serving: --undo brings them back for 7 days, then they are emptied',
|
|
14
|
+
builder: yargs => wait_option(volume_option(app_option(json_option(yargs))))
|
|
15
|
+
.positional('paths', {
|
|
16
|
+
type: 'string',
|
|
17
|
+
array: true,
|
|
18
|
+
description: 'Files or directories of the volume, relative to its root'
|
|
19
|
+
})
|
|
20
|
+
.option('undo', {
|
|
21
|
+
type: 'string',
|
|
22
|
+
description: 'Bring back what an rm moved to the trash (its id)'
|
|
23
|
+
})
|
|
24
|
+
.option('yes', {
|
|
25
|
+
alias: 'y',
|
|
26
|
+
type: 'boolean',
|
|
27
|
+
default: false,
|
|
28
|
+
description: 'Skip the confirmation prompt'
|
|
29
|
+
})
|
|
30
|
+
.check(a => {
|
|
31
|
+
const n = (a.paths ?? []).length;
|
|
32
|
+
if (a.undo && n)
|
|
33
|
+
throw new Error('Either paths to rm, or --undo <rm id>');
|
|
34
|
+
if (!a.undo && !n)
|
|
35
|
+
throw new Error('Say what to rm, or --undo <rm id>');
|
|
36
|
+
return true;
|
|
37
|
+
})
|
|
38
|
+
.example('$0 deploy volumes rm wp-content/plugins/old-plugin -a my-blog', 'A plugin directory, to the trash')
|
|
39
|
+
.example('$0 deploy volumes rm --undo volumeop_โฆ -a my-blog', 'Back from the trash')
|
|
40
|
+
.showHelpOnFail(false),
|
|
41
|
+
handler: async (args) => {
|
|
42
|
+
const onState = (s) => {
|
|
43
|
+
if (!args.json && s !== 'completed' && s !== 'failed')
|
|
44
|
+
log.info(state_label[s]);
|
|
45
|
+
};
|
|
46
|
+
if (args.undo) {
|
|
47
|
+
const { api, team, app } = await resolve_app(args.app);
|
|
48
|
+
const op = await api.undoVolumeOperation(team, args.undo);
|
|
49
|
+
if (!args.wait) {
|
|
50
|
+
if (args.json)
|
|
51
|
+
return print_json(op);
|
|
52
|
+
log.info(`โป๏ธ Undo ${op.id} queued.`);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
if (!args.json)
|
|
56
|
+
log.info(`โป๏ธ Bringing back ${(op.paths ?? []).join(', ')} in ${app.name}`);
|
|
57
|
+
const done = await wait_operation(api, team, op, { onState });
|
|
58
|
+
if (args.json)
|
|
59
|
+
print_json(done);
|
|
60
|
+
require_completed(done);
|
|
61
|
+
if (!args.json)
|
|
62
|
+
log.info('โ
Back where they were.');
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const paths = (args.paths ?? []).map(p => {
|
|
66
|
+
const d = volume_dest(p);
|
|
67
|
+
if (d === '.') {
|
|
68
|
+
throw new CliError('usage', 'rm takes files or directories, not the whole volume');
|
|
69
|
+
}
|
|
70
|
+
return d;
|
|
71
|
+
});
|
|
72
|
+
const { api, team, app, volume } = await resolve_volume(args.app, args.volume);
|
|
73
|
+
const go = await confirm({
|
|
74
|
+
message: `Move ${paths.join(', ')} of ${app.name}/${volume.name} to the trash? They can be brought back for 7 days.`,
|
|
75
|
+
yes: args.yes
|
|
76
|
+
});
|
|
77
|
+
if (!go) {
|
|
78
|
+
log.info('Cancelled.');
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
const op = await api.removeVolumePaths(team, volume.id, paths);
|
|
82
|
+
const undo = `faable deploy volumes rm --undo ${op.id} -a ${app.name}`;
|
|
83
|
+
if (!args.wait) {
|
|
84
|
+
if (args.json)
|
|
85
|
+
return print_json(op);
|
|
86
|
+
log.info(`๐ rm ${op.id} queued. To undo it: ${undo}`);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
const done = await wait_operation(api, team, op, { onState });
|
|
90
|
+
if (args.json)
|
|
91
|
+
print_json(done);
|
|
92
|
+
require_completed(done);
|
|
93
|
+
if (!args.json) {
|
|
94
|
+
log.info(`๐ Moved to the trash: ${paths.join(', ')}.`);
|
|
95
|
+
log.info(`To bring them back (7 days): ${undo}`);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
export { volumes_rm };
|