Avoid direct dependency on Blubird AMAP.
This commit is contained in:
parent
1b5f6bd3eb
commit
55b3def630
@ -2,11 +2,12 @@ $debug = (require 'debug') 'xo:api:vm'
|
|||||||
$find = require 'lodash.find'
|
$find = require 'lodash.find'
|
||||||
$findIndex = require 'lodash.findindex'
|
$findIndex = require 'lodash.findindex'
|
||||||
$forEach = require 'lodash.foreach'
|
$forEach = require 'lodash.foreach'
|
||||||
$request = require('bluebird').promisify(require('request'))
|
|
||||||
endsWith = require 'lodash.endswith'
|
endsWith = require 'lodash.endswith'
|
||||||
startsWith = require 'lodash.startswith'
|
startsWith = require 'lodash.startswith'
|
||||||
{coroutine: $coroutine} = require 'bluebird'
|
{coroutine: $coroutine} = require 'bluebird'
|
||||||
{parseXml} = require '../utils'
|
{parseXml, promisify} = require '../utils'
|
||||||
|
|
||||||
|
$request = promisify(require('request'))
|
||||||
|
|
||||||
#=====================================================================
|
#=====================================================================
|
||||||
|
|
||||||
|
@ -1,7 +1,3 @@
|
|||||||
import {coroutine} from 'bluebird'
|
|
||||||
|
|
||||||
// ===================================================================
|
|
||||||
|
|
||||||
export async function add ({
|
export async function add ({
|
||||||
host,
|
host,
|
||||||
username,
|
username,
|
||||||
@ -58,15 +54,15 @@ remove.params = {
|
|||||||
|
|
||||||
// TODO: remove this function when users are integrated to the main
|
// TODO: remove this function when users are integrated to the main
|
||||||
// collection.
|
// collection.
|
||||||
export const getAll = coroutine(function * () {
|
export async function getAll () {
|
||||||
const servers = yield this._servers.get()
|
const servers = await this._servers.get()
|
||||||
|
|
||||||
for (let i = 0, n = servers.length; i < n; ++i) {
|
for (let i = 0, n = servers.length; i < n; ++i) {
|
||||||
servers[i] = this.getServerPublicProperties(servers[i])
|
servers[i] = this.getServerPublicProperties(servers[i])
|
||||||
}
|
}
|
||||||
|
|
||||||
return servers
|
return servers
|
||||||
})
|
}
|
||||||
|
|
||||||
getAll.description = 'returns all the registered Xen server'
|
getAll.description = 'returns all the registered Xen server'
|
||||||
|
|
||||||
|
@ -1,7 +1,3 @@
|
|||||||
import {delay} from 'bluebird'
|
|
||||||
|
|
||||||
// ===================================================================
|
|
||||||
|
|
||||||
export function hasPermission ({userId, objectId, permission}) {
|
export function hasPermission ({userId, objectId, permission}) {
|
||||||
return this.hasPermission(userId, objectId, permission)
|
return this.hasPermission(userId, objectId, permission)
|
||||||
}
|
}
|
||||||
@ -23,7 +19,11 @@ hasPermission.params = {
|
|||||||
// -------------------------------------------------------------------
|
// -------------------------------------------------------------------
|
||||||
|
|
||||||
export function wait ({duration, returnValue}) {
|
export function wait ({duration, returnValue}) {
|
||||||
return delay(returnValue, +duration)
|
return new Promise(resolve => {
|
||||||
|
setTimeout(+duration, () => {
|
||||||
|
resolve(returnValue)
|
||||||
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
wait.params = {
|
wait.params = {
|
||||||
|
@ -4,13 +4,10 @@ $findIndex = require 'lodash.findindex'
|
|||||||
$findWhere = require 'lodash.find'
|
$findWhere = require 'lodash.find'
|
||||||
$forEach = require 'lodash.foreach'
|
$forEach = require 'lodash.foreach'
|
||||||
$isArray = require 'lodash.isarray'
|
$isArray = require 'lodash.isarray'
|
||||||
$request = require('bluebird').promisify(require('request'))
|
|
||||||
$result = require 'lodash.result'
|
$result = require 'lodash.result'
|
||||||
Bluebird = require 'bluebird'
|
|
||||||
endsWith = require 'lodash.endswith'
|
endsWith = require 'lodash.endswith'
|
||||||
escapeStringRegexp = require 'escape-string-regexp'
|
escapeStringRegexp = require 'escape-string-regexp'
|
||||||
eventToPromise = require 'event-to-promise'
|
eventToPromise = require 'event-to-promise'
|
||||||
fs = require('fs-extra')
|
|
||||||
map = require 'lodash.map'
|
map = require 'lodash.map'
|
||||||
sortBy = require 'lodash.sortby'
|
sortBy = require 'lodash.sortby'
|
||||||
startsWith = require 'lodash.startswith'
|
startsWith = require 'lodash.startswith'
|
||||||
@ -19,18 +16,12 @@ startsWith = require 'lodash.startswith'
|
|||||||
{
|
{
|
||||||
formatXml: $js2xml,
|
formatXml: $js2xml,
|
||||||
parseXml,
|
parseXml,
|
||||||
pFinally
|
pFinally,
|
||||||
|
promisify
|
||||||
} = require '../utils'
|
} = require '../utils'
|
||||||
|
{isVmRunning: $isVMRunning} = require('../xapi')
|
||||||
|
|
||||||
Bluebird.promisifyAll(fs)
|
$request = promisify(require('request'))
|
||||||
|
|
||||||
$isVMRunning = do ->
|
|
||||||
runningStates = {
|
|
||||||
'Paused': true
|
|
||||||
'Running': true
|
|
||||||
}
|
|
||||||
|
|
||||||
(VM) -> !!runningStates[VM.power_state]
|
|
||||||
|
|
||||||
#=====================================================================
|
#=====================================================================
|
||||||
|
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
import Bluebird from 'bluebird'
|
|
||||||
import filter from 'lodash.filter'
|
import filter from 'lodash.filter'
|
||||||
import forEach from 'lodash.foreach'
|
import forEach from 'lodash.foreach'
|
||||||
import fs from 'fs-extra'
|
import fs from 'fs-promise'
|
||||||
import {exec} from 'child_process'
|
import {exec} from 'child_process'
|
||||||
|
|
||||||
const execAsync = Bluebird.promisify(exec)
|
import {promisify} from './utils'
|
||||||
Bluebird.promisifyAll(fs)
|
|
||||||
|
const execAsync = promisify(exec)
|
||||||
|
|
||||||
const noop = () => {}
|
const noop = () => {}
|
||||||
|
|
||||||
@ -44,7 +44,7 @@ class NfsMounter {
|
|||||||
|
|
||||||
async _mount (mount) {
|
async _mount (mount) {
|
||||||
const path = this._fullPath(mount.path)
|
const path = this._fullPath(mount.path)
|
||||||
await fs.ensureDirAsync(path)
|
await fs.ensureDir(path)
|
||||||
return await execAsync(`mount -t nfs ${mount.host}:${mount.share} ${path}`)
|
return await execAsync(`mount -t nfs ${mount.host}:${mount.share} ${path}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,8 +104,8 @@ class LocalHandler {
|
|||||||
async sync (local) {
|
async sync (local) {
|
||||||
if (local.enabled) {
|
if (local.enabled) {
|
||||||
try {
|
try {
|
||||||
await fs.ensureDirAsync(local.path)
|
await fs.ensureDir(local.path)
|
||||||
await fs.accessAsync(local.path, fs.R_OK | fs.W_OK)
|
await fs.access(local.path, fs.R_OK | fs.W_OK)
|
||||||
} catch (exc) {
|
} catch (exc) {
|
||||||
local.enabled = false
|
local.enabled = false
|
||||||
local.error = exc.message
|
local.error = exc.message
|
||||||
|
12
src/utils.js
12
src/utils.js
@ -6,7 +6,7 @@ import isArray from 'lodash.isarray'
|
|||||||
import isString from 'lodash.isstring'
|
import isString from 'lodash.isstring'
|
||||||
import multiKeyHashInt from 'multikey-hash'
|
import multiKeyHashInt from 'multikey-hash'
|
||||||
import xml2js from 'xml2js'
|
import xml2js from 'xml2js'
|
||||||
import {promisify, method} from 'bluebird'
|
import {promisify} from 'bluebird'
|
||||||
import {randomBytes} from 'crypto'
|
import {randomBytes} from 'crypto'
|
||||||
|
|
||||||
// ===================================================================
|
// ===================================================================
|
||||||
@ -21,7 +21,7 @@ export function camelToSnakeCase (string) {
|
|||||||
// -------------------------------------------------------------------
|
// -------------------------------------------------------------------
|
||||||
|
|
||||||
// Ensure the value is an array, wrap it if necessary.
|
// Ensure the value is an array, wrap it if necessary.
|
||||||
export const ensureArray = (value) => {
|
export function ensureArray (value) {
|
||||||
if (value === undefined) {
|
if (value === undefined) {
|
||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
@ -98,6 +98,10 @@ export const pFinally = (promise, cb) => {
|
|||||||
|
|
||||||
// -------------------------------------------------------------------
|
// -------------------------------------------------------------------
|
||||||
|
|
||||||
|
export {promisify}
|
||||||
|
|
||||||
|
// -------------------------------------------------------------------
|
||||||
|
|
||||||
export function parseSize (size) {
|
export function parseSize (size) {
|
||||||
if (!isString(size)) {
|
if (!isString(size)) {
|
||||||
return size
|
return size
|
||||||
@ -139,13 +143,13 @@ export function map (col, iterator, thisArg = this) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create a hash from multiple values.
|
// Create a hash from multiple values.
|
||||||
export const multiKeyHash = method((...args) => {
|
export const multiKeyHash = (...args) => new Promise((resolve, reject) => {
|
||||||
const hash = multiKeyHashInt(...args)
|
const hash = multiKeyHashInt(...args)
|
||||||
|
|
||||||
const buf = new Buffer(4)
|
const buf = new Buffer(4)
|
||||||
buf.writeUInt32LE(hash, 0)
|
buf.writeUInt32LE(hash, 0)
|
||||||
|
|
||||||
return base64url(buf)
|
resolve(base64url(buf))
|
||||||
})
|
})
|
||||||
|
|
||||||
// Similar to `map()` but change the current collection.
|
// Similar to `map()` but change the current collection.
|
||||||
|
@ -4,14 +4,12 @@ import eventToPromise from 'event-to-promise'
|
|||||||
import filter from 'lodash.filter'
|
import filter from 'lodash.filter'
|
||||||
import find from 'lodash.find'
|
import find from 'lodash.find'
|
||||||
import forEach from 'lodash.foreach'
|
import forEach from 'lodash.foreach'
|
||||||
import fs from 'fs-extra'
|
|
||||||
import got from 'got'
|
import got from 'got'
|
||||||
import includes from 'lodash.includes'
|
import includes from 'lodash.includes'
|
||||||
import map from 'lodash.map'
|
import map from 'lodash.map'
|
||||||
import sortBy from 'lodash.sortby'
|
import sortBy from 'lodash.sortby'
|
||||||
import unzip from 'julien-f-unzip'
|
import unzip from 'julien-f-unzip'
|
||||||
import {PassThrough} from 'stream'
|
import {PassThrough} from 'stream'
|
||||||
import Bluebird, {promisify} from 'bluebird'
|
|
||||||
import {
|
import {
|
||||||
wrapError as wrapXapiError,
|
wrapError as wrapXapiError,
|
||||||
Xapi as XapiBase
|
Xapi as XapiBase
|
||||||
@ -22,12 +20,11 @@ import {
|
|||||||
camelToSnakeCase,
|
camelToSnakeCase,
|
||||||
ensureArray,
|
ensureArray,
|
||||||
noop, parseXml,
|
noop, parseXml,
|
||||||
pFinally
|
pFinally,
|
||||||
|
promisify
|
||||||
} from './utils'
|
} from './utils'
|
||||||
import {JsonRpcError} from './api-errors'
|
import {JsonRpcError} from './api-errors'
|
||||||
|
|
||||||
Bluebird.promisifyAll(fs)
|
|
||||||
|
|
||||||
const debug = createDebug('xo:xapi')
|
const debug = createDebug('xo:xapi')
|
||||||
|
|
||||||
// ===================================================================
|
// ===================================================================
|
||||||
|
@ -4,7 +4,7 @@ import Bluebird from 'bluebird'
|
|||||||
import eventToPromise from 'event-to-promise'
|
import eventToPromise from 'event-to-promise'
|
||||||
import filter from 'lodash.filter'
|
import filter from 'lodash.filter'
|
||||||
import forEach from 'lodash.foreach'
|
import forEach from 'lodash.foreach'
|
||||||
import fs from 'fs-extra'
|
import fs from 'fs-promise'
|
||||||
import includes from 'lodash.includes'
|
import includes from 'lodash.includes'
|
||||||
import isEmpty from 'lodash.isempty'
|
import isEmpty from 'lodash.isempty'
|
||||||
import isString from 'lodash.isstring'
|
import isString from 'lodash.isstring'
|
||||||
@ -682,8 +682,8 @@ export default class Xo extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async rollingBackupVm ({vm, path, tag, depth, compress, onlyMetadata}) {
|
async rollingBackupVm ({vm, path, tag, depth, compress, onlyMetadata}) {
|
||||||
await fs.ensureDirAsync(path)
|
await fs.ensureDir(path)
|
||||||
const files = await fs.readdirAsync(path)
|
const files = await fs.readdir(path)
|
||||||
|
|
||||||
const reg = new RegExp('^[^_]+_' + escapeStringRegexp(tag))
|
const reg = new RegExp('^[^_]+_' + escapeStringRegexp(tag))
|
||||||
const backups = sortBy(filter(files, (fileName) => reg.test(fileName)))
|
const backups = sortBy(filter(files, (fileName) => reg.test(fileName)))
|
||||||
@ -696,7 +696,7 @@ export default class Xo extends EventEmitter {
|
|||||||
const promises = []
|
const promises = []
|
||||||
for (let surplus = backups.length - (depth - 1); surplus > 0; surplus--) {
|
for (let surplus = backups.length - (depth - 1); surplus > 0; surplus--) {
|
||||||
const oldBackup = backups.shift()
|
const oldBackup = backups.shift()
|
||||||
promises.push(fs.unlinkAsync(`${path}/${oldBackup}`))
|
promises.push(fs.unlink(`${path}/${oldBackup}`))
|
||||||
}
|
}
|
||||||
await Bluebird.all(promises)
|
await Bluebird.all(promises)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user