Moving rollingSnapshot form xapi to xo

This commit is contained in:
Fabrice Marsaud 2015-11-09 16:24:30 +01:00
parent a5590b090c
commit 76a44459cf
3 changed files with 23 additions and 28 deletions

View File

@ -472,9 +472,7 @@ exports.snapshot = snapshot
rollingSnapshot = $coroutine ({vm, tag, depth}) ->
yield checkPermissionsForSnapshot.call(this, vm)
snapshot = yield @getXAPI(vm).rollingSnapshotVm(vm.ref, tag, depth)
return snapshot.$id
yield @rollingSnapshotVm(vm, tag, depth)
rollingSnapshot.params = {
id: { type: 'string' }

View File

@ -1,8 +1,6 @@
import createDebug from 'debug'
import d3TimeFormat from 'd3-time-format'
import escapeStringRegexp from 'escape-string-regexp'
import eventToPromise from 'event-to-promise'
import filter from 'lodash.filter'
import find from 'lodash.find'
import got from 'got'
import includes from 'lodash.includes'
@ -25,8 +23,7 @@ import {
mapToArray,
noop,
parseXml,
pFinally,
safeDateFormat
pFinally
} from './utils'
import {JsonRpcError} from './api-errors'
@ -979,35 +976,17 @@ export default class Xapi extends XapiBase {
}
}
async snapshotVm (vmId) {
async snapshotVm (vmId, nameLabel = undefined) {
return await this._getOrWaitObject(
await this._snapshotVm(
this.getObject(vmId)
this.getObject(vmId),
nameLabel
)
)
}
// =================================================================
async rollingSnapshotVm (vmId, tag, depth) {
const vm = this.getObject(vmId)
const reg = new RegExp('^rollingSnapshot_[^_]+_' + escapeStringRegexp(tag))
const snapshots = sortBy(filter(vm.$snapshots, snapshot => reg.test(snapshot.name_label)), 'name_label')
const date = safeDateFormat(new Date())
const ref = await this._snapshotVm(vm, `rollingSnapshot_${date}_${tag}_${vm.name_label}`)
const promises = []
for (let surplus = snapshots.length - (depth - 1); surplus > 0; surplus--) {
const oldSnap = snapshots.shift()
promises.push(this.deleteVm(oldSnap.uuid, true))
}
await Promise.all(promises)
return await this._getOrWaitObject(ref)
}
async _createVbd (vm, vdi, {
bootable = false,
position = undefined,

View File

@ -794,6 +794,24 @@ export default class Xo extends EventEmitter {
return backupFullPath
}
async rollingSnapshotVm (vm, tag, depth) {
const xapi = this.getXAPI(vm)
vm = xapi.getObject(vm.id)
const reg = new RegExp('^rollingSnapshot_[^_]+_' + escapeStringRegexp(tag))
const snapshots = sortBy(filter(vm.$snapshots, snapshot => reg.test(snapshot.name_label)), 'name_label')
const date = safeDateFormat(new Date())
await xapi.snapshotVm(vm, `rollingSnapshot_${date}_${tag}_${vm.name_label}`)
const promises = []
for (let surplus = snapshots.length - (depth - 1); surplus > 0; surplus--) {
const oldSnap = snapshots.shift()
promises.push(xapi.deleteVm(oldSnap.uuid, true))
}
await Promise.all(promises)
}
// -----------------------------------------------------------------
async createAuthenticationToken ({userId}) {