Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 114 additions & 1 deletion src/api/FaableApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,54 @@ export type FaableTask = {
finished_at: string | null
}

// A volume of an app (api `/volume`): today, the ones a blueprint defines
// (WordPress: `site` at /faable/app). The api answers them as internal until
// the blueprints are published.
export type FaableVolume = {
id: string
app_id: string
name: string
mount_path: string
quota_gb: number
storage_tier: string | null
backup: boolean
state: 'ready' | 'retained'
retain_until: string | null
used_bytes: number | null
used_inodes: number | null
usage_measured_at: string | null
}

// A backup, restore, export or import of a volume (api `/volume-operation`).
// The signed URLs are fresh on every read and valid one hour.
export type FaableVolumeOperation = {
id: string
volume_id: string
app_id: string
kind: 'backup' | 'restore' | 'export' | 'import' | 'put'
reason: 'manual' | 'scheduled' | 'pre_restore' | 'pre_import'
state: 'awaiting_upload' | 'queued' | 'running' | 'completed' | 'failed'
backup_id: string | null
after_id: string | null
size_bytes: number | null
error: string | null
started_at: string | null
finished_at: string | null
object_deleted_at: string | null
download_url?: string | null
download_expires_at?: string | null
upload_url?: string | null
upload_expires_at?: string | null
createdAt?: string
}

export type FaableVolumeBackups = {
results: FaableVolumeOperation[]
retention: number | null
scheduled: boolean
last_completed_at: string | null
}

// A cron schedule that queues a task (api `/taskschedule`, Hobby and Pro).
export type FaableTaskSchedule = {
id: string
Expand Down Expand Up @@ -880,7 +928,10 @@ export class FaableApi<T = any> {
team: string,
id: string,
body: Partial<
Pick<FaableTaskSchedule, 'cron' | 'argv' | 'name' | 'timeout_s' | 'enabled'>
Pick<
FaableTaskSchedule,
'cron' | 'argv' | 'name' | 'timeout_s' | 'enabled'
>
>
) {
return data(
Expand All @@ -898,6 +949,68 @@ export class FaableApi<T = any> {
)
}

async listVolumes(team: string, app_id: string) {
return data(
this.client.get<{ results: FaableVolume[] }>(`/volume`, {
params: { app_id },
headers: projectHeader(team)
})
)
}

async listVolumeBackups(team: string, volume_id: string) {
return data(
this.client.get<FaableVolumeBackups>(`/volume/${volume_id}/backups`, {
headers: projectHeader(team)
})
)
}

// backup, export, import and put: each queues (or, an import or a put,
// opens) one operation of the volume.
async createVolumeOperation(
team: string,
volume_id: string,
kind: 'backup' | 'export' | 'import' | 'put'
) {
return data(
this.client.post<FaableVolumeOperation>(
`/volume/${volume_id}/${kind}`,
undefined,
{ headers: projectHeader(team) }
)
)
}

async restoreVolume(team: string, volume_id: string, backup_id: string) {
return data(
this.client.post<FaableVolumeOperation>(
`/volume/${volume_id}/restore`,
{ backup_id },
{ headers: projectHeader(team) }
)
)
}

async getVolumeOperation(team: string, operation_id: string) {
return data(
this.client.get<FaableVolumeOperation>(
`/volume-operation/${operation_id}`,
{ headers: projectHeader(team) }
)
)
}

async applyVolumeImport(team: string, operation_id: string) {
return data(
this.client.post<FaableVolumeOperation>(
`/volume-operation/${operation_id}/apply`,
undefined,
{ headers: projectHeader(team) }
)
)
}

async getTask(task_id: string, team: string) {
return data(
this.client.get<FaableTask>(`/task/${task_id}`, {
Expand Down
2 changes: 2 additions & 0 deletions src/commands/deploy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { launch } from './launch'
import { quota, usage } from './project_usage'
import { schedule } from './schedule'
import { secrets } from './secrets'
import { volumes } from './volumes'
import { waf } from './waf'

export type { DeployCommandArgs } from './launch'
Expand Down Expand Up @@ -53,6 +54,7 @@ export const deploy: CommandModule = {
.command(status)
.command(run)
.command(schedule)
.command(volumes)
.command(traffic)
.command(usage)
.command(quota)
Expand Down
96 changes: 96 additions & 0 deletions src/commands/deploy/volumes/backups.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { CommandModule } from 'yargs'
import { json_option, print, print_json } from '../../../lib/listing'
import { log } from '../../../log'
import {
app_option,
format_bytes,
require_completed,
resolve_volume,
state_label,
volume_option,
wait_operation,
wait_option
} from './shared'

interface BackupsArgs {
app?: string
volume?: string
json?: boolean
}

export const volumes_backups: CommandModule<unknown, BackupsArgs> = {
command: 'backups',
describe: 'List the backups of a volume (nightly, and the ones you take)',
builder: yargs =>
volume_option(app_option(json_option(yargs))).showHelpOnFail(false) as any,
handler: async args => {
const { api, team, app, volume } = await resolve_volume(
args.app,
args.volume
)
const list = await api.listVolumeBackups(team, volume.id)
if (args.json) return print_json(list)
const how = [
list.scheduled ? 'one every night' : 'no nightly backup',
list.retention != null ? `the last ${list.retention} kept` : null
].filter(Boolean)
log.info(`🗄 Backups of ${app.name}/${volume.name} (${how.join(', ')}):`)
if (list.results.length === 0) {
log.info(' None yet. Take one with: faable deploy volumes backup')
return
}
for (const b of list.results) {
const when = b.finished_at ?? b.started_at ?? b.createdAt ?? ''
const note =
b.state === 'completed'
? b.object_deleted_at
? 'deleted by retention'
: format_bytes(b.size_bytes)
: b.state === 'failed'
? `failed: ${b.error ?? '?'}`
: state_label[b.state]
print(` ${b.id} ${when} ${b.reason.padEnd(11)} ${note}`)
}
log.info('Restore one with: faable deploy volumes restore <backup id>')
}
}

interface BackupArgs extends BackupsArgs {
wait?: boolean
}

export const volumes_backup: CommandModule<unknown, BackupArgs> = {
command: 'backup',
describe: 'Back a volume up now (the app keeps running)',
builder: yargs =>
wait_option(volume_option(app_option(json_option(yargs)))).showHelpOnFail(
false
) as any,
handler: async args => {
const { api, team, app, volume } = await resolve_volume(
args.app,
args.volume
)
const op = await api.createVolumeOperation(team, volume.id, 'backup')
if (!args.wait) {
if (args.json) return print_json(op)
log.info(`🗄 Backup ${op.id} of ${app.name}/${volume.name} queued.`)
return
}
if (!args.json)
log.info(`🗄 Backing up ${app.name}/${volume.name} (${op.id})`)
const done = await wait_operation(api, team, op, {
onState: s => {
if (!args.json && s !== 'completed' && s !== 'failed')
log.info(state_label[s])
}
})
if (args.json) {
print_json(done)
require_completed(done)
return
}
require_completed(done)
log.info(`✅ Backed up: ${format_bytes(done.size_bytes)} (${done.id}).`)
}
}
77 changes: 77 additions & 0 deletions src/commands/deploy/volumes/export.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import axios from 'axios'
import { createWriteStream } from 'node:fs'
import { rename, rm } from 'node:fs/promises'
import { pipeline } from 'node:stream/promises'
import { CommandModule } from 'yargs'
import { CliError } from '../../../lib/errors'
import { json_option, print_json } from '../../../lib/listing'
import { log } from '../../../log'
import {
app_option,
format_bytes,
require_completed,
resolve_volume,
state_label,
volume_option,
wait_operation
} from './shared'

interface ExportArgs {
app?: string
volume?: string
output?: string
json?: boolean
}

export const volumes_export: CommandModule<unknown, ExportArgs> = {
command: 'export',
describe:
'Download the contents of a volume as a .tar.gz (the app keeps running)',
builder: yargs =>
volume_option(app_option(json_option(yargs)))
.option('output', {
alias: 'o',
type: 'string',
description:
'Where to save it (default <app>-<volume>-<date>.tar.gz in the current directory)'
})
.showHelpOnFail(false) as any,
handler: async args => {
const { api, team, app, volume } = await resolve_volume(
args.app,
args.volume
)
const op = await api.createVolumeOperation(team, volume.id, 'export')
if (!args.json)
log.info(`📦 Exporting ${app.name}/${volume.name} (${op.id})`)
const done = require_completed(
await wait_operation(api, team, op, {
onState: s => {
if (!args.json && s !== 'completed' && s !== 'failed')
log.info(state_label[s])
}
})
)
// The completed operation read by wait_operation carries a fresh URL.
const url = done.download_url
if (!url) {
throw new CliError(
'not_found',
`The export ${done.id} finished but has no download link; try faable deploy volumes export again`
)
}
const day = new Date().toISOString().slice(0, 10)
const file = args.output ?? `${app.name}-${volume.name}-${day}.tar.gz`
const part = `${file}.part`
const res = await axios.get(url, { responseType: 'stream', timeout: 0 })
try {
await pipeline(res.data, createWriteStream(part))
await rename(part, file)
} catch (error) {
await rm(part, { force: true })
throw error
}
if (args.json) return print_json({ ...done, file })
log.info(`✅ Saved ${file} (${format_bytes(done.size_bytes)}).`)
}
}
Loading
Loading