Skip to content

Commit

Permalink
feat(xo-server): VM_destroy instead of deleteVm (#5693)
Browse files Browse the repository at this point in the history
Continuation of 5f1c127
  • Loading branch information
MathieuRA committed Mar 26, 2021
1 parent 22ba130 commit cb52a8b
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 39 deletions.
2 changes: 1 addition & 1 deletion packages/xo-server/src/api/sr.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export async function destroy({ sr }) {
const config = xapi.xo.getData(sr, 'xosan_config')
// we simply forget because the hosted disks are being destroyed with the VMs
await xapi.forgetSr(sr._xapiId)
await asyncMapSettled(config.nodes, node => xapi.deleteVm(node.vm.id))
await asyncMapSettled(config.nodes, node => this.getXapiObject(node.vm.id).$destroy())
await xapi.deleteNetwork(config.network)
if (sr.SR_type === 'xosan') {
await this.unbindXosanLicense({ srId: sr.id })
Expand Down
4 changes: 2 additions & 2 deletions packages/xo-server/src/api/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export async function copyVm({ vm, sr }) {
console.log('export full VM...')
const input = await srcXapi.exportVm(vm)
console.log('import full VM...')
await tgtXapi.deleteVm(await tgtXapi.importVm(input, { srId: sr }))
await tgtXapi.VM_destroy((await tgtXapi.importVm(input, { srId: sr })).$ref)
}

// delta
Expand All @@ -72,7 +72,7 @@ export async function copyVm({ vm, sr }) {
srId: sr,
})
console.log('transfered size:', transferSize)
await tgtXapi.deleteVm(copyVm)
await tgtXapi.VM_destroy(copyVm.$ref)
}
}

Expand Down
12 changes: 6 additions & 6 deletions packages/xo-server/src/api/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export const create = defer(async function ($defer, params) {
}

const xapiVm = await xapi.createVm(template._xapiId, params, checkLimits)
$defer.onFailure(() => xapi.deleteVm(xapiVm.$id, true, true))
$defer.onFailure(() => xapi.VM_destroy(xapiVm.$ref, true, true))

const vm = xapi.xo.addObject(xapiVm)

Expand Down Expand Up @@ -385,7 +385,7 @@ const delete_ = defer(async function (
}
})

return xapi.deleteVm(vm._xapiId, deleteDisks, force, forceDeleteDefaultTemplate)
return xapi.VM_destroy(vm._xapiRef, deleteDisks, force, forceDeleteDefaultTemplate)
})

delete_.params = {
Expand Down Expand Up @@ -664,11 +664,11 @@ export const clone = defer(async function ($defer, { vm, name, full_copy: fullCo
await checkPermissionOnSrs.call(this, vm)
const xapi = this.getXapi(vm)

const { $id: cloneId } = await xapi.cloneVm(vm._xapiRef, {
const { $id: cloneId, $ref: cloneRef } = await xapi.cloneVm(vm._xapiRef, {
nameLabel: name,
fast: !fullCopy,
})
$defer.onFailure(() => xapi.deleteVm(cloneId))
$defer.onFailure(() => xapi.VM_destroy(cloneRef))

const isAdmin = this.user.permission === 'admin'
if (!isAdmin) {
Expand Down Expand Up @@ -786,10 +786,10 @@ export const snapshot = defer(async function (
}

const xapi = this.getXapi(vm)
const { $id: snapshotId } = await (saveMemory
const { $id: snapshotId, $ref: snapshotRef } = await (saveMemory
? xapi.checkpointVm(vm._xapiRef, name)
: xapi.snapshotVm(vm._xapiRef, name))
$defer.onFailure(() => xapi.deleteVm(snapshotId))
$defer.onFailure(() => xapi.VM_destroy(snapshotRef))

if (description !== undefined) {
await xapi.editVm(snapshotId, { name_description: description })
Expand Down
16 changes: 8 additions & 8 deletions packages/xo-server/src/api/xosan.js
Original file line number Diff line number Diff line change
Expand Up @@ -585,10 +585,10 @@ export const createSR = defer(async function (
const firstSr = srsObjects[0]
CURRENT_POOL_OPERATIONS[poolId] = { ...OPERATION_OBJECT, state: 1 }
const firstVM = await this::_importGlusterVM(xapi, template, firstSr)
$defer.onFailure(() => xapi.deleteVm(firstVM, true))
$defer.onFailure(() => xapi.VM_destroy(firstVM.$ref, true))
CURRENT_POOL_OPERATIONS[poolId] = { ...OPERATION_OBJECT, state: 2 }
const copiedVms = await asyncMapSettled(srsObjects.slice(1), sr =>
copyVm(xapi, firstVM, sr)::tap(({ vm }) => $defer.onFailure(() => xapi.deleteVm(vm)))
copyVm(xapi, firstVM, sr)::tap(({ vm }) => $defer.onFailure(() => xapi.VM_destroy(vm.$ref)))
)
const vmsAndSrs = [
{
Expand All @@ -602,7 +602,7 @@ export const createSR = defer(async function (
const sr = firstSr
const arbiterIP = networkPrefix + vmIpLastNumber++
const arbiterVm = await xapi.copyVm(firstVM, sr)
$defer.onFailure(() => xapi.deleteVm(arbiterVm, true))
$defer.onFailure(() => xapi.VM_destroy(arbiterVm.$ref, true))
arbiter = await _prepareGlusterVm(xapi, sr, arbiterVm, xosanNetwork, arbiterIP, {
labelSuffix: '_arbiter',
increaseDataDisk: false,
Expand Down Expand Up @@ -870,7 +870,7 @@ export async function replaceBrick({ xosansr, previousBrick, newLvmSr, brickSize
await xapi.xo.setData(xosansr, 'xosan_config', data)
CURRENT_POOL_OPERATIONS[poolId] = { ...OPERATION_OBJECT, state: 2 }
if (previousVMEntry) {
await xapi.deleteVm(previousVMEntry.vm, true)
await xapi.VM_destroy(previousVMEntry.vm.$ref, true)
}
CURRENT_POOL_OPERATIONS[poolId] = { ...OPERATION_OBJECT, state: 3 }
await xapi.callAsync('SR.scan', xapi.getObject(xosansr).$ref)
Expand Down Expand Up @@ -1013,7 +1013,7 @@ const insertNewGlusterVm = defer(async function (
const srObject = xapi.getObject(lvmsrId)
// can't really copy an existing VM, because existing gluster VMs disks might too large to be copied.
const newVM = await this::_importGlusterVM(xapi, data.template, lvmsrId)
$defer.onFailure(() => xapi.deleteVm(newVM, true))
$defer.onFailure(() => xapi.VM_destroy(newVM.$ref, true))
const addressAndHost = await _prepareGlusterVm(xapi, srObject, newVM, xosanNetwork, ipAddress, {
labelSuffix,
increaseDataDisk,
Expand Down Expand Up @@ -1051,7 +1051,7 @@ export const addBricks = defer(async function ($defer, { xosansr, lvmsrs, brickS
newAddresses.push(ipAddress)
const { newVM, addressAndHost } = await this::insertNewGlusterVm(xapi, xosansr, newSr, { ipAddress, brickSize })
$defer.onFailure(() => glusterCmd(glusterEndpoint, 'peer detach ' + ipAddress, true))
$defer.onFailure(() => xapi.deleteVm(newVM, true))
$defer.onFailure(() => xapi.VM_destroy(newVM.$ref, true))
const brickName = addressAndHost.brickName
newNodes.push({
brickName,
Expand All @@ -1070,7 +1070,7 @@ export const addBricks = defer(async function ($defer, { xosansr, lvmsrs, brickS
data.type = 'replica'
await xapi.xo.setData(xosansr, 'xosan_config', data)
await glusterCmd(glusterEndpoint, 'peer detach ' + arbiterNode.vm.ip, true)
await xapi.deleteVm(arbiterNode.vm.id, true)
await xapi.VM_destroy(await xapi.call('VM.get_by_uuid', arbiterNode.vm.id), true)
}
CURRENT_POOL_OPERATIONS[poolId] = { ...OPERATION_OBJECT, state: 1 }
await glusterCmd(glusterEndpoint, `volume add-brick xosan ${newNodes.map(n => n.brickName).join(' ')}`)
Expand Down Expand Up @@ -1123,7 +1123,7 @@ export const removeBricks = defer(async function ($defer, { xosansr, bricks }) {
remove(data.nodes, node => ips.includes(node.vm.ip))
await xapi.xo.setData(xosansr.id, 'xosan_config', data)
await xapi.callAsync('SR.scan', xapi.getObject(xosansr._xapiId).$ref)
await asyncMapSettled(brickVMs, vm => xapi.deleteVm(vm.vm, true))
await asyncMapSettled(brickVMs, vm => xapi.VM_destroy(vm.vm.$ref, true))
} finally {
delete CURRENT_POOL_OPERATIONS[xapi.pool.$id]
}
Expand Down
9 changes: 0 additions & 9 deletions packages/xo-server/src/xapi/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -563,15 +563,6 @@ export default class Xapi extends XapiBase {
)
}

/**
* @deprecated Use VM_destroy instead
*/
async deleteVm(vmOrId, deleteDisks = true, force = false, forceDeleteDefaultTemplate = false) {
const $ref = typeof vmOrId === 'string' ? this.getObject(vmOrId).$ref : vmOrId.$ref

return this.VM_destroy($ref, { deleteDisks, force, forceDeleteDefaultTemplate })
}

getVmConsole(vmId) {
const vm = this.getObject(vmId)

Expand Down
1 change: 0 additions & 1 deletion packages/xo-server/src/xapi/index.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ declare export class Xapi {

barrier(): Promise<void>;
barrier(ref: string): Promise<XapiObject>;
deleteVm(vm: Id): Promise<void>;
editVm(vm: Id, $Dict<mixed>): Promise<void>;
exportDeltaVm(
cancelToken: mixed,
Expand Down
2 changes: 1 addition & 1 deletion packages/xo-server/src/xapi/mixins/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export default {

// Clones the template.
const vmRef = await this[clone ? '_cloneVm' : '_copyVm'](template, nameLabel)
$defer.onFailure(() => this.deleteVm(vmRef))
$defer.onFailure(() => this.VM_destroy(vmRef))

// Copy BIOS strings
// https://support.citrix.com/article/CTX230618
Expand Down
17 changes: 9 additions & 8 deletions packages/xo-server/src/xo-mixins/backups.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ export default class {
bypassVdiChainsCheck: force,
snapshotNameLabel: `XO_DELTA_EXPORT: ${targetSr.name_label} (${targetSr.uuid})`,
})
$defer.onFailure(() => srcXapi.deleteVm(delta.vm.uuid))
$defer.onFailure(() => this._xo.getXapiObject(delta.vm.uuid).$destroy())
$defer.onFailure(cancel)

const date = safeDateFormat(Date.now())
Expand All @@ -400,11 +400,11 @@ export default class {
// Once done, (asynchronously) remove the (now obsolete) local
// base.
if (localBaseUuid) {
promise.then(() => srcXapi.deleteVm(localBaseUuid))::ignoreErrors()
promise.then(() => this._xo.getXapiObject(localBaseUuid).$destroy())::ignoreErrors()
}

if (toRemove !== undefined) {
promise.then(() => asyncMapSettled(toRemove, _ => targetXapi.deleteVm(_.$id)))::ignoreErrors()
promise.then(() => asyncMapSettled(toRemove, _ => targetXapi.VM_destroy(_.$ref)))::ignoreErrors()
}

// (Asynchronously) Identify snapshot as future base.
Expand Down Expand Up @@ -591,7 +591,7 @@ export default class {
)
const baseVm = bases.pop()
forEach(bases, base => {
xapi.deleteVm(base.$id)::ignoreErrors()
xapi.VM_destroy(base.$ref)::ignoreErrors()
})

// Check backup dirs.
Expand Down Expand Up @@ -621,7 +621,8 @@ export default class {
fullVdisRequired,
disableBaseTags: true,
})
$defer.onFailure(() => xapi.deleteVm(delta.vm.uuid))
const exportedVmRef = await xapi.call('VM.get_by_uuid', delta.vm.uuid)
$defer.onFailure(() => xapi.VM_destroy(exportedVmRef))
$defer.onFailure(cancel)

// Save vdis.
Expand Down Expand Up @@ -711,7 +712,7 @@ export default class {
await this._removeOldDeltaVmBackups(xapi, { vm, handler, dir, retention })

if (baseVm) {
xapi.deleteVm(baseVm.$id)::ignoreErrors()
xapi.VM_destroy(baseVm.$ref)::ignoreErrors()
}

return {
Expand Down Expand Up @@ -837,7 +838,7 @@ export default class {
const promises = []
for (let surplus = snapshots.length - (retention - 1); surplus > 0; surplus--) {
const oldSnap = snapshots.shift()
promises.push(xapi.deleteVm(oldSnap.uuid))
promises.push(xapi.VM_destroy(oldSnap.$ref))
}
await Promise.all(promises)
}
Expand All @@ -846,7 +847,7 @@ export default class {
return Promise.all(
mapToArray(vms, vm =>
// Do not consider a failure to delete an old copy as a fatal error.
xapi.deleteVm(vm.$id)::ignoreErrors()
xapi.VM_destroy(vm.$ref)::ignoreErrors()
)
)
}
Expand Down
6 changes: 3 additions & 3 deletions packages/xo-server/src/xo-mixins/proxies.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export default class Proxy {
const { vmUuid } = await this._getProxy(id)
if (vmUuid !== undefined) {
try {
await this._app.getXapi(vmUuid).deleteVm(vmUuid)
await this._app.getXapiObject(vmUuid).$destroy()
} catch (error) {
if (!noSuchObject.is(error)) {
throw error
Expand Down Expand Up @@ -208,7 +208,7 @@ export default class Proxy {
}),
{ srId }
)
$defer.onFailure(() => xapi._deleteVm(vm))
$defer.onFailure(() => xapi.VM_destroy(vm.$ref))

const arg = { licenseId, boundObjectId: vm.uuid }
await app.bindLicense(arg)
Expand Down Expand Up @@ -268,7 +268,7 @@ export default class Proxy {
const { vmUuid } = await this._getProxy(proxyId)
if (vmUuid !== undefined) {
try {
await app.getXapi(vmUuid).deleteVm(vmUuid)
await app.getXapiObject(vmUuid).$destroy()
} catch (error) {
if (!noSuchObject.is(error)) {
throw error
Expand Down

0 comments on commit cb52a8b

Please sign in to comment.