54 lines
1.3 KiB
JavaScript
Executable File
54 lines
1.3 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
|
|
process.env.DEBUG = '*'
|
|
|
|
const defer = require('golike-defer').default
|
|
const pump = require('pump')
|
|
const { CancelToken, fromCallback } = require('promise-toolbox')
|
|
|
|
const { createClient } = require('../')
|
|
|
|
const { createOutputStream, resolveRef } = require('./utils')
|
|
|
|
defer(async ($defer, args) => {
|
|
let raw = false
|
|
if (args[0] === '--raw') {
|
|
raw = true
|
|
args.shift()
|
|
}
|
|
|
|
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)
|
|
|
|
// 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: await resolveRef(xapi, 'VDI', args[1])
|
|
}
|
|
})
|
|
|
|
console.warn('Export task:', exportStream.headers['task-id'])
|
|
|
|
await fromCallback(cb => pump(
|
|
exportStream,
|
|
createOutputStream(args[2]),
|
|
cb
|
|
))
|
|
})(process.argv.slice(2)).catch(
|
|
console.error.bind(console, 'error')
|
|
)
|