From 55b3def6309a7275c83220d0b4095c7ab71271ec Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Mon, 5 Oct 2015 13:06:53 +0200 Subject: [PATCH] Avoid direct dependency on Blubird AMAP. --- src/api/host.coffee | 5 +++-- src/api/server.js | 10 +++------- src/api/test.js | 10 +++++----- src/api/vm.coffee | 17 ++++------------- src/remote-handler.js | 14 +++++++------- src/utils.js | 12 ++++++++---- src/xapi.js | 7 ++----- src/xo.js | 8 ++++---- 8 files changed, 36 insertions(+), 47 deletions(-) diff --git a/src/api/host.coffee b/src/api/host.coffee index b19a1dfd0..86366f17b 100644 --- a/src/api/host.coffee +++ b/src/api/host.coffee @@ -2,11 +2,12 @@ $debug = (require 'debug') 'xo:api:vm' $find = require 'lodash.find' $findIndex = require 'lodash.findindex' $forEach = require 'lodash.foreach' -$request = require('bluebird').promisify(require('request')) endsWith = require 'lodash.endswith' startsWith = require 'lodash.startswith' {coroutine: $coroutine} = require 'bluebird' -{parseXml} = require '../utils' +{parseXml, promisify} = require '../utils' + +$request = promisify(require('request')) #===================================================================== diff --git a/src/api/server.js b/src/api/server.js index 975d55071..abe3d25e7 100644 --- a/src/api/server.js +++ b/src/api/server.js @@ -1,7 +1,3 @@ -import {coroutine} from 'bluebird' - -// =================================================================== - export async function add ({ host, username, @@ -58,15 +54,15 @@ remove.params = { // TODO: remove this function when users are integrated to the main // collection. -export const getAll = coroutine(function * () { - const servers = yield this._servers.get() +export async function getAll () { + const servers = await this._servers.get() for (let i = 0, n = servers.length; i < n; ++i) { servers[i] = this.getServerPublicProperties(servers[i]) } return servers -}) +} getAll.description = 'returns all the registered Xen server' diff --git a/src/api/test.js b/src/api/test.js index 6ecd503b3..99698adb0 100644 --- a/src/api/test.js +++ b/src/api/test.js @@ -1,7 +1,3 @@ -import {delay} from 'bluebird' - -// =================================================================== - export function hasPermission ({userId, objectId, permission}) { return this.hasPermission(userId, objectId, permission) } @@ -23,7 +19,11 @@ hasPermission.params = { // ------------------------------------------------------------------- export function wait ({duration, returnValue}) { - return delay(returnValue, +duration) + return new Promise(resolve => { + setTimeout(+duration, () => { + resolve(returnValue) + }) + }) } wait.params = { diff --git a/src/api/vm.coffee b/src/api/vm.coffee index 2032a0c89..10d2f789c 100644 --- a/src/api/vm.coffee +++ b/src/api/vm.coffee @@ -4,13 +4,10 @@ $findIndex = require 'lodash.findindex' $findWhere = require 'lodash.find' $forEach = require 'lodash.foreach' $isArray = require 'lodash.isarray' -$request = require('bluebird').promisify(require('request')) $result = require 'lodash.result' -Bluebird = require 'bluebird' endsWith = require 'lodash.endswith' escapeStringRegexp = require 'escape-string-regexp' eventToPromise = require 'event-to-promise' -fs = require('fs-extra') map = require 'lodash.map' sortBy = require 'lodash.sortby' startsWith = require 'lodash.startswith' @@ -19,18 +16,12 @@ startsWith = require 'lodash.startswith' { formatXml: $js2xml, parseXml, - pFinally + pFinally, + promisify } = require '../utils' +{isVmRunning: $isVMRunning} = require('../xapi') -Bluebird.promisifyAll(fs) - -$isVMRunning = do -> - runningStates = { - 'Paused': true - 'Running': true - } - - (VM) -> !!runningStates[VM.power_state] +$request = promisify(require('request')) #===================================================================== diff --git a/src/remote-handler.js b/src/remote-handler.js index e23625d83..edce89b65 100644 --- a/src/remote-handler.js +++ b/src/remote-handler.js @@ -1,11 +1,11 @@ -import Bluebird from 'bluebird' import filter from 'lodash.filter' import forEach from 'lodash.foreach' -import fs from 'fs-extra' +import fs from 'fs-promise' import {exec} from 'child_process' -const execAsync = Bluebird.promisify(exec) -Bluebird.promisifyAll(fs) +import {promisify} from './utils' + +const execAsync = promisify(exec) const noop = () => {} @@ -44,7 +44,7 @@ class NfsMounter { async _mount (mount) { 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}`) } @@ -104,8 +104,8 @@ class LocalHandler { async sync (local) { if (local.enabled) { try { - await fs.ensureDirAsync(local.path) - await fs.accessAsync(local.path, fs.R_OK | fs.W_OK) + await fs.ensureDir(local.path) + await fs.access(local.path, fs.R_OK | fs.W_OK) } catch (exc) { local.enabled = false local.error = exc.message diff --git a/src/utils.js b/src/utils.js index 04006c322..951f28105 100644 --- a/src/utils.js +++ b/src/utils.js @@ -6,7 +6,7 @@ import isArray from 'lodash.isarray' import isString from 'lodash.isstring' import multiKeyHashInt from 'multikey-hash' import xml2js from 'xml2js' -import {promisify, method} from 'bluebird' +import {promisify} from 'bluebird' import {randomBytes} from 'crypto' // =================================================================== @@ -21,7 +21,7 @@ export function camelToSnakeCase (string) { // ------------------------------------------------------------------- // Ensure the value is an array, wrap it if necessary. -export const ensureArray = (value) => { +export function ensureArray (value) { if (value === undefined) { return [] } @@ -98,6 +98,10 @@ export const pFinally = (promise, cb) => { // ------------------------------------------------------------------- +export {promisify} + +// ------------------------------------------------------------------- + export function parseSize (size) { if (!isString(size)) { return size @@ -139,13 +143,13 @@ export function map (col, iterator, thisArg = this) { } // Create a hash from multiple values. -export const multiKeyHash = method((...args) => { +export const multiKeyHash = (...args) => new Promise((resolve, reject) => { const hash = multiKeyHashInt(...args) const buf = new Buffer(4) buf.writeUInt32LE(hash, 0) - return base64url(buf) + resolve(base64url(buf)) }) // Similar to `map()` but change the current collection. diff --git a/src/xapi.js b/src/xapi.js index 6b4bc8ac8..88e034e23 100644 --- a/src/xapi.js +++ b/src/xapi.js @@ -4,14 +4,12 @@ import eventToPromise from 'event-to-promise' import filter from 'lodash.filter' import find from 'lodash.find' import forEach from 'lodash.foreach' -import fs from 'fs-extra' import got from 'got' import includes from 'lodash.includes' import map from 'lodash.map' import sortBy from 'lodash.sortby' import unzip from 'julien-f-unzip' import {PassThrough} from 'stream' -import Bluebird, {promisify} from 'bluebird' import { wrapError as wrapXapiError, Xapi as XapiBase @@ -22,12 +20,11 @@ import { camelToSnakeCase, ensureArray, noop, parseXml, - pFinally + pFinally, + promisify } from './utils' import {JsonRpcError} from './api-errors' -Bluebird.promisifyAll(fs) - const debug = createDebug('xo:xapi') // =================================================================== diff --git a/src/xo.js b/src/xo.js index 37047443c..4f8f7358a 100644 --- a/src/xo.js +++ b/src/xo.js @@ -4,7 +4,7 @@ import Bluebird from 'bluebird' import eventToPromise from 'event-to-promise' import filter from 'lodash.filter' import forEach from 'lodash.foreach' -import fs from 'fs-extra' +import fs from 'fs-promise' import includes from 'lodash.includes' import isEmpty from 'lodash.isempty' import isString from 'lodash.isstring' @@ -682,8 +682,8 @@ export default class Xo extends EventEmitter { } async rollingBackupVm ({vm, path, tag, depth, compress, onlyMetadata}) { - await fs.ensureDirAsync(path) - const files = await fs.readdirAsync(path) + await fs.ensureDir(path) + const files = await fs.readdir(path) const reg = new RegExp('^[^_]+_' + escapeStringRegexp(tag)) const backups = sortBy(filter(files, (fileName) => reg.test(fileName))) @@ -696,7 +696,7 @@ export default class Xo extends EventEmitter { const promises = [] for (let surplus = backups.length - (depth - 1); surplus > 0; surplus--) { const oldBackup = backups.shift() - promises.push(fs.unlinkAsync(`${path}/${oldBackup}`)) + promises.push(fs.unlink(`${path}/${oldBackup}`)) } await Bluebird.all(promises)