77 lines
1.7 KiB
JavaScript
Executable File
77 lines
1.7 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
|
|
process.env.DEBUG = 'xen-api'
|
|
|
|
const createProgress = require('progress-stream')
|
|
const createTop = require('process-top')
|
|
const defer = require('golike-defer').default
|
|
const getopts = require('getopts')
|
|
const { CancelToken } = require('promise-toolbox')
|
|
|
|
const { createClient } = require('../')
|
|
|
|
const {
|
|
createOutputStream,
|
|
formatProgress,
|
|
pipeline,
|
|
resolveRecord,
|
|
throttle,
|
|
} = require('./utils')
|
|
|
|
defer(async ($defer, rawArgs) => {
|
|
const { raw, throttle: bps, _: args } = getopts(rawArgs, {
|
|
boolean: 'raw',
|
|
alias: {
|
|
raw: 'r',
|
|
throttle: 't',
|
|
},
|
|
})
|
|
|
|
if (args.length < 2) {
|
|
return console.log(
|
|
'Usage: export-vdi [--raw] <XS URL> <VDI identifier> [<VHD file>]'
|
|
)
|
|
}
|
|
|
|
const xapi = createClient({
|
|
allowUnauthorized: true,
|
|
url: args[0],
|
|
watchEvents: false,
|
|
})
|
|
|
|
await xapi.connect()
|
|
$defer(() => xapi.disconnect())
|
|
|
|
const { cancel, token } = CancelToken.source()
|
|
process.on('SIGINT', cancel)
|
|
|
|
const vdi = await resolveRecord(xapi, 'VDI', args[1])
|
|
|
|
// https://xapi-project.github.io/xen-api/snapshots.html#downloading-a-disk-or-snapshot
|
|
const exportStream = await xapi.getResource(token, '/export_raw_vdi/', {
|
|
query: {
|
|
format: raw ? 'raw' : 'vhd',
|
|
vdi: vdi.$ref,
|
|
},
|
|
})
|
|
|
|
console.warn('Export task:', exportStream.headers['task-id'])
|
|
|
|
const top = createTop()
|
|
const progressStream = createProgress()
|
|
|
|
$defer(
|
|
clearInterval,
|
|
setInterval(() => {
|
|
console.warn('\r %s | %s', top.toString(), formatProgress(progressStream.progress()))
|
|
}, 1e3)
|
|
)
|
|
|
|
await pipeline(
|
|
exportStream,
|
|
progressStream,
|
|
throttle(bps),
|
|
createOutputStream(args[2])
|
|
)
|
|
})(process.argv.slice(2)).catch(console.error.bind(console, 'error'))
|