From 067a6d01bca05ef5127f99aeed38640fe5de4d2b Mon Sep 17 00:00:00 2001 From: Fabrice Marsaud Date: Thu, 5 Nov 2015 11:21:51 +0100 Subject: [PATCH] List backup Files in remotes ans import them for restore --- src/api/remote.js | 26 ++++++++++++++++++++++++++ src/xo.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/src/api/remote.js b/src/api/remote.js index 05c7ef4d3..575c60d26 100644 --- a/src/api/remote.js +++ b/src/api/remote.js @@ -15,6 +15,16 @@ get.params = { id: {type: 'string'} } +export async function list (id) { + return await this.listRemote(id) +} + +list.permission = 'admin' +list.description = 'Lists the files found in a remote point' +list.params = { + id: {type: 'string'} +} + export async function create ({name, url}) { return await this.createRemote({name, url}) } @@ -39,6 +49,22 @@ set.params = { enabled: {type: 'boolean', optional: true} } +export async function importVm ({id, file, host}) { + await this.importVmFromRemote(id, file, host) +} + +importVm.permission = 'admin' +importVm.description = 'Imports a VM into host, from a file found in the chosen remote' +importVm.params = { + id: {type: 'string'}, + file: {type: 'string'}, + host: {type: 'string'} +} + +importVm.resolve = { + host: ['host', 'host', 'administrate'] +} + async function delete_ ({id}) { await this.removeRemote(id) } diff --git a/src/xo.js b/src/xo.js index 5604246b8..2ab25c1e2 100644 --- a/src/xo.js +++ b/src/xo.js @@ -2,6 +2,7 @@ import assign from 'lodash.assign' import Bluebird from 'bluebird' import createJsonSchemaValidator from 'is-my-json-valid' +import endsWith from 'lodash.endswith' import escapeStringRegexp from 'escape-string-regexp' import eventToPromise from 'event-to-promise' import filter from 'lodash.filter' @@ -677,6 +678,23 @@ export default class Xo extends EventEmitter { return this._developRemote((await this._getRemote(id)).properties) } + async listRemote (id) { + const remote = await this.getRemote(id) + return this._listRemote(remote) + } + + async _listRemote (remote) { + const fsRemotes = { + nfs: true, + local: true + } + if (remote.type in fsRemotes) { + const files = await fs.readdir(remote.path) + return filter(files, file => endsWith(file, '.xva')) + } + throw new Error('Unhandled remote type') + } + async createRemote ({name, url}) { let remote = await this._remotes.create(name, url) return await this.updateRemote(remote.get('id'), {enabled: true}) @@ -726,6 +744,21 @@ export default class Xo extends EventEmitter { } } + async importVmFromRemote (id, file, host) { + const remote = await this.getRemote(id) + const stream = fs.createReadStream(remote.path + '/' + file) + try { + await eventToPromise(stream, 'readable') + } catch (error) { + if (error.code === 'ENOENT') { + throw new Error('VM to import not found in this remote') + } + throw error + } + const xapi = this.getXAPI(host) + await xapi.importVm(stream) + } + // ----------------------------------------------------------------- async backupVm ({vm, pathToFile, compress, onlyMetadata}) {