Merge pull request #184 from vatesfr/abhamonr-disable-vm-start-during-delta-import

Disable vm start during delta import and explicit notification.
This commit is contained in:
Julien Fontanet
2016-01-13 11:25:56 +01:00
3 changed files with 43 additions and 4 deletions

View File

@@ -50,3 +50,11 @@ export class AlreadyAuthenticated extends JsonRpcError {
super('already authenticated', 4)
}
}
// -------------------------------------------------------------------
export class ForbiddenOperation extends JsonRpcError {
constructor (operation, reason) {
super(`forbidden operation: ${operation}`, 5, reason)
}
}

View File

@@ -32,7 +32,10 @@ import {
promisifyAll,
pSettle
} from './utils'
import {JsonRpcError} from './api-errors'
import {
JsonRpcError,
ForbiddenOperation
} from './api-errors'
const debug = createDebug('xo:xapi')
@@ -1284,7 +1287,15 @@ export default class Xapi extends XapiBase {
}
async startVm (vmId) {
await this._startVm(this.getObject(vmId))
try {
await this._startVm(this.getObject(vmId))
} catch (e) {
if (e.code === 'OPERATION_BLOCKED') {
throw new ForbiddenOperation('Start', e.params[1])
}
throw e
}
}
async startVmOnCd (vmId) {
@@ -1355,6 +1366,15 @@ export default class Xapi extends XapiBase {
}
}
// vm_operations: http://xapi-project.github.io/xen-api/classes/vm.html
async addForbiddenOperationToVm (vmId, operation, reason) {
await this.call('VM.add_to_blocked_operations', this.getObject(vmId).$ref, operation, `[XO]${reason}`)
}
async removeForbiddenOperationFromVm (vmId, operation) {
await this.call('VM.remove_from_blocked_operations', this.getObject(vmId).$ref, operation)
}
// =================================================================
async _createVbd (vm, vdi, {

View File

@@ -1143,7 +1143,12 @@ export default class Xo extends EventEmitter {
// Import vm metadata.
const vm = await this._importVmMetadata(xapi, `${fullBackupPath}.xva`)
const vmName = vm.name_label
await xapi._setObjectProperties(vm, { name_label: `[Importing...] ${vmName}` })
// Disable start and change the VM name label during import.
await Promise.all([
xapi.addForbiddenOperationToVm(vm.$id, 'start', 'Delta backup import...'),
xapi._setObjectProperties(vm, { name_label: `[Importing...] ${vmName}` })
])
// Destroy vbds if necessary. Why ?
// Because XenServer creates Vbds linked to the vdis of the backup vm if it exists.
@@ -1173,7 +1178,13 @@ export default class Xo extends EventEmitter {
}
)
)
await xapi._setObjectProperties(vm, { name_label: vmName })
// Import done, reenable start and set real vm name.
await Promise.all([
xapi.removeForbiddenOperationFromVm(vm.$id, 'start'),
xapi._setObjectProperties(vm, { name_label: vmName })
])
return xapiObjectToXo(vm).id
}