From 76a44459cf1abd7fce0f035aa2e231effa0ffc65 Mon Sep 17 00:00:00 2001 From: Fabrice Marsaud Date: Mon, 9 Nov 2015 16:24:30 +0100 Subject: [PATCH] Moving rollingSnapshot form xapi to xo --- src/api/vm.coffee | 4 +--- src/xapi.js | 29 ++++------------------------- src/xo.js | 18 ++++++++++++++++++ 3 files changed, 23 insertions(+), 28 deletions(-) diff --git a/src/api/vm.coffee b/src/api/vm.coffee index c47cd9d23..a7e59acef 100644 --- a/src/api/vm.coffee +++ b/src/api/vm.coffee @@ -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' } diff --git a/src/xapi.js b/src/xapi.js index b87a3d1c8..834d992e8 100644 --- a/src/xapi.js +++ b/src/xapi.js @@ -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, diff --git a/src/xo.js b/src/xo.js index 601a8093b..dad560f0d 100644 --- a/src/xo.js +++ b/src/xo.js @@ -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}) {