Merge pull request #114 from vatesfr/marsaudf-import-backup

Import a VM from a remote backup
This commit is contained in:
Olivier Lambert 2015-11-05 15:46:26 +01:00
commit 712319974b
2 changed files with 59 additions and 0 deletions

View File

@ -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)
}

View File

@ -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}) {