List backup Files in remotes ans import them for restore
This commit is contained in:
parent
27825f9e2e
commit
067a6d01bc
@ -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)
|
||||
}
|
||||
|
33
src/xo.js
33
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}) {
|
||||
|
Loading…
Reference in New Issue
Block a user