feat(xo-server#_listVmBackupsOnRemote): use @xen-orchestra/backups lib (#5622)

This commit is contained in:
badrAZ
2021-02-26 11:17:45 +01:00
committed by GitHub
parent b9ce0bd99d
commit baa5847949
4 changed files with 24 additions and 41 deletions

View File

@@ -1,6 +1,7 @@
const mapValues = require('lodash/mapValues')
const { dirname } = require('path')
exports.formatVmBackup = function formatVmBackup(backup) {
function formatVmBackup(backup) {
return {
disks:
backup.vhds === undefined
@@ -26,3 +27,8 @@ exports.formatVmBackup = function formatVmBackup(backup) {
},
}
}
// format all backups as returned by RemoteAdapter#listAllVmBackups()
exports.formatVmBackups = function formatVmBackups(backupsByVM) {
return mapValues(backupsByVM, backups => backups.map(formatVmBackup))
}

View File

@@ -1,7 +1,6 @@
import defer from 'golike-defer'
import Disposable from 'promise-toolbox/Disposable'
import fromCallback from 'promise-toolbox/fromCallback'
import mapValues from 'lodash/mapValues'
import using from 'promise-toolbox/using'
import { asyncMap } from '@xen-orchestra/backups/asyncMap'
import { Backup } from '@xen-orchestra/backups/Backup'
@@ -11,7 +10,7 @@ import { decorateWith } from '@vates/decorate-with'
import { deduped } from '@vates/disposable/deduped'
import { DurablePartition } from '@xen-orchestra/backups/DurablePartition'
import { execFile } from 'child_process'
import { formatVmBackup } from '@xen-orchestra/backups/formatVmBackup'
import { formatVmBackups } from '@xen-orchestra/backups/formatVmBackups'
import { ImportVmBackup } from '@xen-orchestra/backups/ImportVmBackup'
import { Readable } from 'stream'
import { RemoteAdapter } from '@xen-orchestra/backups/RemoteAdapter'
@@ -247,9 +246,7 @@ export default class Backups {
await asyncMap(Object.keys(remotes), async remoteId => {
try {
await using(this.getAdapter(remotes[remoteId]), async adapter => {
backups[remoteId] = mapValues(await adapter.listAllVmBackups(), vmBackups =>
vmBackups.map(backup => formatVmBackup(backup))
)
backups[remoteId] = formatVmBackups(await adapter.listAllVmBackups())
})
} catch (error) {
warn('listVmBackups', { error, remote: remotes[remoteId] })

View File

@@ -31,4 +31,4 @@
> In case of conflict, the highest (lowest in previous list) `$version` wins.
- @xen-orchestra/backups minor
- xo-server patch
- xo-server minor

View File

@@ -12,7 +12,7 @@ import { PassThrough } from 'stream'
import { AssertionError } from 'assert'
import { basename, dirname } from 'path'
import { decorateWith } from '@vates/decorate-with'
import { formatVmBackup } from '@xen-orchestra/backups/formatVmBackup'
import { formatVmBackups } from '@xen-orchestra/backups/formatVmBackups'
import { invalidParameters } from 'xo-common/api-errors'
import { isValidXva } from '@xen-orchestra/backups/isValidXva'
import { parseDuration } from '@vates/parse-duration'
@@ -31,7 +31,7 @@ import {
sum,
values,
} from 'lodash'
import { CancelToken, ignoreErrors, timeout } from 'promise-toolbox'
import { CancelToken, ignoreErrors, timeout, using } from 'promise-toolbox'
import Vhd, { chainVhd, checkVhdChain, createSyntheticStream as createVhdReadStream } from 'vhd-lib'
import type Logger from '../logs/loggers/abstract'
@@ -930,55 +930,35 @@ export default class BackupNg {
)
async _listVmBackupsOnRemote(remoteId: string) {
const app = this._app
const backupsByVm = {}
try {
const { proxy, url, options } = await app.getRemoteWithCredentials(remoteId)
let backupsByVm
if (proxy !== undefined) {
const { [remoteId]: backupsByVm } = await app.callProxyMethod(proxy, 'backup.listVmBackups', {
;({ [remoteId]: backupsByVm } = await app.callProxyMethod(proxy, 'backup.listVmBackups', {
remotes: {
[remoteId]: {
url,
options,
},
},
})
// inject the remote id on the backup which is needed for importVmBackupNg()
forOwn(backupsByVm, backups =>
backups.forEach(backup => {
backup.id = `${remoteId}${backup.id}`
})
}))
} else {
backupsByVm = await using(app.getBackupsRemoteAdapter(remoteId), async adapter =>
formatVmBackups(await adapter.listAllVmBackups())
)
return backupsByVm
}
const handler = await app.getRemoteHandler(remoteId)
const entries = (
await handler.list(BACKUP_DIR).catch(error => {
if (error == null || error.code !== 'ENOENT') {
throw error
}
return []
})
).filter(name => name !== 'index.json')
await Promise.all(
entries.map(async vmUuid => {
// $FlowFixMe don't know what is the problem (JFT)
const backups = await this._listVmBackups(handler, remoteId, vmUuid)
if (backups.length === 0) {
return
}
backupsByVm[vmUuid] = backups.map(backup => formatVmBackup(backup))
// inject the remote id on the backup which is needed for importVmBackupNg()
forOwn(backupsByVm, backups =>
backups.forEach(backup => {
backup.id = `${remoteId}${backup.id}`
})
)
return backupsByVm
} catch (error) {
log.warn(`listVmBackups for remote ${remoteId}:`, { error })
}
return backupsByVm
}
async listVmBackupsNg(remotes: string[], _forceRefresh = false) {