chore(xo-server): more cancelable Xapi methods (#2701)

This commit is contained in:
Julien Fontanet 2018-02-28 15:25:22 +01:00 committed by GitHub
parent 60085798f2
commit 68e06303a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,7 +6,7 @@ import synchronized from 'decorator-synchronized'
import tarStream from 'tar-stream' import tarStream from 'tar-stream'
import vmdkToVhd from 'xo-vmdk-to-vhd' import vmdkToVhd from 'xo-vmdk-to-vhd'
import { import {
cancellable, cancelable,
catchPlus as pCatch, catchPlus as pCatch,
defer, defer,
fromEvent, fromEvent,
@ -710,18 +710,22 @@ export default class Xapi extends XapiBase {
// Returns a stream to the exported VM. // Returns a stream to the exported VM.
@concurrency(2, stream => stream.then(stream => fromEvent(stream, 'end'))) @concurrency(2, stream => stream.then(stream => fromEvent(stream, 'end')))
async exportVm (vmId, { compress = true } = {}) { @cancelable
async exportVm ($cancelToken, vmId, { compress = true } = {}) {
const vm = this.getObject(vmId) const vm = this.getObject(vmId)
let host let host
let snapshotRef let snapshotRef
if (isVmRunning(vm)) { if (isVmRunning(vm)) {
host = vm.$resident_on host = vm.$resident_on
snapshotRef = (await this._snapshotVm(vm, `[XO Export] ${vm.name_label}`)) snapshotRef = (await this._snapshotVm(
.$ref $cancelToken,
vm,
`[XO Export] ${vm.name_label}`
)).$ref
} }
const promise = this.getResource('/export/', { const promise = this.getResource($cancelToken, '/export/', {
host, host,
query: { query: {
ref: snapshotRef || vm.$ref, ref: snapshotRef || vm.$ref,
@ -779,7 +783,7 @@ export default class Xapi extends XapiBase {
} }
// Create a snapshot of the VM and returns a delta export object. // Create a snapshot of the VM and returns a delta export object.
@cancellable @cancelable
@deferrable @deferrable
async exportDeltaVm ( async exportDeltaVm (
$defer, $defer,
@ -1232,7 +1236,8 @@ export default class Xapi extends XapiBase {
) )
} }
async _importVm (stream, sr, onVmCreation = undefined) { @cancelable
async _importVm ($cancelToken, stream, sr, onVmCreation = undefined) {
const taskRef = await this.createTask('VM import') const taskRef = await this.createTask('VM import')
const query = {} const query = {}
@ -1242,16 +1247,18 @@ export default class Xapi extends XapiBase {
query.sr_id = sr.$ref query.sr_id = sr.$ref
} }
if (onVmCreation) { if (onVmCreation != null) {
;this._waitObject( ;this._waitObject(
obj => obj =>
obj && obj.current_operations && taskRef in obj.current_operations obj != null &&
obj.current_operations != null &&
taskRef in obj.current_operations
) )
.then(onVmCreation) .then(onVmCreation)
::ignoreErrors() ::ignoreErrors()
} }
const vmRef = await this.putResource(stream, '/import/', { const vmRef = await this.putResource($cancelToken, stream, '/import/', {
host, host,
query, query,
task: taskRef, task: taskRef,
@ -1412,7 +1419,8 @@ export default class Xapi extends XapiBase {
} }
@synchronized() // like @concurrency(1) but more efficient @synchronized() // like @concurrency(1) but more efficient
async _snapshotVm (vm, nameLabel = vm.name_label) { @cancelable
async _snapshotVm ($cancelToken, vm, nameLabel = vm.name_label) {
debug( debug(
`Snapshotting VM ${vm.name_label}${ `Snapshotting VM ${vm.name_label}${
nameLabel !== vm.name_label ? ` as ${nameLabel}` : '' nameLabel !== vm.name_label ? ` as ${nameLabel}` : ''
@ -1421,7 +1429,12 @@ export default class Xapi extends XapiBase {
let ref let ref
try { try {
ref = await this.call('VM.snapshot_with_quiesce', vm.$ref, nameLabel) ref = await this.callAsync(
$cancelToken,
'VM.snapshot_with_quiesce',
vm.$ref,
nameLabel
)
this.addTag(ref, 'quiesce')::ignoreErrors() this.addTag(ref, 'quiesce')::ignoreErrors()
await this._waitObjectState(ref, vm => includes(vm.tags, 'quiesce')) await this._waitObjectState(ref, vm => includes(vm.tags, 'quiesce'))
@ -1437,7 +1450,12 @@ export default class Xapi extends XapiBase {
) { ) {
throw error throw error
} }
ref = await this.call('VM.snapshot', vm.$ref, nameLabel) ref = await this.callAsync(
$cancelToken,
'VM.snapshot',
vm.$ref,
nameLabel
)
} }
// Convert the template to a VM and wait to have receive the up- // Convert the template to a VM and wait to have receive the up-
// to-date object. // to-date object.
@ -1854,7 +1872,7 @@ export default class Xapi extends XapiBase {
} }
@concurrency(12, stream => stream.then(stream => fromEvent(stream, 'end'))) @concurrency(12, stream => stream.then(stream => fromEvent(stream, 'end')))
@cancellable @cancelable
_exportVdi ($cancelToken, vdi, base, format = VDI_FORMAT_VHD) { _exportVdi ($cancelToken, vdi, base, format = VDI_FORMAT_VHD) {
const host = vdi.$SR.$PBDs[0].$host const host = vdi.$SR.$PBDs[0].$host