82 lines
2.1 KiB
JavaScript
Executable File
82 lines
2.1 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
|
|
process.env.DEBUG = '*'
|
|
|
|
const defer = require('golike-defer').default
|
|
const getopts = require('getopts')
|
|
const { CancelToken } = require('promise-toolbox')
|
|
const { createVhdStreamWithLength } = require('vhd-lib')
|
|
|
|
const { createClient } = require('../')
|
|
|
|
const { createInputStream, resolveRef } = require('./utils')
|
|
|
|
defer(async ($defer, argv) => {
|
|
const opts = getopts(argv, { boolean: ['events', 'raw', 'remove-length'], string: ['sr', 'vdi'] })
|
|
|
|
const url = opts._[0]
|
|
|
|
if (url === undefined) {
|
|
return console.log(
|
|
'Usage: import-vdi [--events] [--raw] [--sr <SR identifier>] [--vdi <VDI identifier>] <XS URL> [<VHD file>]'
|
|
)
|
|
}
|
|
|
|
const { raw, sr, vdi } = opts
|
|
|
|
const createVdi = vdi === ''
|
|
if (createVdi && !(raw && sr !== undefined)) {
|
|
throw new Error('--vdi requires --raw and --sr flags')
|
|
}
|
|
|
|
const xapi = createClient({
|
|
allowUnauthorized: true,
|
|
url,
|
|
watchEvents: opts.events && ['event'],
|
|
})
|
|
|
|
await xapi.connect()
|
|
$defer(() => xapi.disconnect())
|
|
|
|
const { cancel, token } = CancelToken.source()
|
|
process.on('SIGINT', cancel)
|
|
|
|
let input = createInputStream(opts._[1])
|
|
$defer.onFailure(() => input.destroy())
|
|
|
|
let vdiRef
|
|
if (createVdi) {
|
|
vdiRef = await xapi.call('VDI.create', {
|
|
name_label: 'xen-api/import-vdi',
|
|
other_config: {},
|
|
read_only: false,
|
|
sharable: false,
|
|
SR: await resolveRef(xapi, 'SR', sr),
|
|
type: 'user',
|
|
virtual_size: input.length,
|
|
})
|
|
$defer.onFailure(() => xapi.call('VDI.destroy', vdiRef))
|
|
} else {
|
|
vdiRef = await resolveRef(xapi, 'VDI', args[1])
|
|
}
|
|
|
|
if (opts['remove-length']) {
|
|
delete input.length
|
|
console.log('length removed')
|
|
} else if (!raw && input.length === undefined) {
|
|
input = await createVhdStreamWithLength(input)
|
|
}
|
|
|
|
// https://xapi-project.github.io/xen-api/snapshots.html#uploading-a-disk-or-snapshot
|
|
const result = await xapi.putResource(token, input, '/import_raw_vdi/', {
|
|
query: {
|
|
format: raw ? 'raw' : 'vhd',
|
|
vdi: vdiRef,
|
|
},
|
|
})
|
|
|
|
if (result !== undefined) {
|
|
console.log(result)
|
|
}
|
|
})(process.argv.slice(2)).catch(console.error.bind(console, 'error'))
|