feat(xen-api/examples/export-vm): add --gzip and --zstd

This commit is contained in:
Julien Fontanet
2019-01-14 14:57:24 +01:00
parent 308d53dc6b
commit 270185d9dc
3 changed files with 43 additions and 34 deletions

View File

@@ -6,35 +6,18 @@ const createProgress = require('progress-stream')
const createTop = require('process-top')
const defer = require('golike-defer').default
const getopts = require('getopts')
const humanFormat = require('human-format')
const { CancelToken } = require('promise-toolbox')
const { createClient } = require('../')
const {
createOutputStream,
formatProgress,
pipeline,
resolveRecord,
throttle,
} = require('./utils')
const formatSize = bytes => humanFormat(bytes, { scale: 'binary', unit: 'B' })
function Progress$toString() {
return [
formatSize(this.transferred),
' / ',
formatSize(this.length),
' | ',
this.runtime,
's / ',
this.eta,
's | ',
formatSize(this.speed),
'/s',
].join('')
}
defer(async ($defer, rawArgs) => {
const { raw, throttle: bps, _: args } = getopts(rawArgs, {
boolean: 'raw',
@@ -80,7 +63,7 @@ defer(async ($defer, rawArgs) => {
$defer(
clearInterval,
setInterval(() => {
console.warn('\r %s | %s', top.toString(), Progress$toString.call(progressStream.progress()))
console.warn('\r %s | %s', top.toString(), formatProgress(progressStream.progress()))
}, 1e3)
)

View File

@@ -2,15 +2,25 @@
process.env.DEBUG = '*'
const createProgress = require('progress-stream')
const defer = require('golike-defer').default
const pump = require('pump')
const { CancelToken, fromCallback } = require('promise-toolbox')
const getopts = require('getopts')
const { CancelToken } = require('promise-toolbox')
const { createClient } = require('../')
const { createOutputStream, resolveRef } = require('./utils')
const {
createOutputStream,
formatProgress,
pipeline,
resolveRecord,
} = require('./utils')
defer(async ($defer, rawArgs) => {
const { gzip, zstd, _: args } = getopts(rawArgs, {
boolean: ['gzip', 'zstd'],
})
defer(async ($defer, args) => {
if (args.length < 2) {
return console.log('Usage: export-vm <XS URL> <VM identifier> [<XVA file>]')
}
@@ -18,7 +28,7 @@ defer(async ($defer, args) => {
const xapi = createClient({
allowUnauthorized: true,
url: args[0],
watchEvents: false
watchEvents: false,
})
await xapi.connect()
@@ -30,18 +40,16 @@ defer(async ($defer, args) => {
// https://xapi-project.github.io/xen-api/importexport.html
const exportStream = await xapi.getResource(token, '/export/', {
query: {
ref: await resolveRef(xapi, 'VM', args[1]),
use_compression: 'true'
}
ref: (await resolveRecord(xapi, 'VM', args[1])).$ref,
use_compression: zstd ? 'zstd' : gzip ? 'true' : 'false',
},
})
console.warn('Export task:', exportStream.headers['task-id'])
await fromCallback(cb => pump(
await pipeline(
exportStream,
createOutputStream(args[2]),
cb
))
})(process.argv.slice(2)).catch(
console.error.bind(console, 'error')
)
createProgress({ time: 1e3 }, p => console.warn(formatProgress(p))),
createOutputStream(args[2])
)
})(process.argv.slice(2)).catch(console.error.bind(console, 'error'))

View File

@@ -1,6 +1,7 @@
const { createReadStream, createWriteStream, statSync } = require('fs')
const { fromCallback } = require('promise-toolbox')
const { PassThrough, pipeline } = require('readable-stream')
const humanFormat = require('human-format')
const Throttle = require('throttle')
const { isOpaqueRef } = require('../')
@@ -28,6 +29,23 @@ exports.createOutputStream = path => {
return stream
}
const formatSizeOpts = { scale: 'binary', unit: 'B' }
const formatSize = bytes => humanFormat(bytes, formatSizeOpts)
exports.formatProgress = p =>
[
formatSize(p.transferred),
' / ',
formatSize(p.length),
' | ',
p.runtime,
's / ',
p.eta,
's | ',
formatSize(p.speed),
'/s',
].join('')
exports.pipeline = (...streams) => {
return fromCallback(cb => {
streams = streams.filter(_ => _ != null)