Initial commit.

This commit is contained in:
Julien Fontanet 2015-03-31 18:44:33 +02:00
commit e7b406c127
10 changed files with 540 additions and 0 deletions

View File

@ -0,0 +1,65 @@
# http://EditorConfig.org
#
# Julien Fontanet's configuration
# https://gist.github.com/julien-f/8096213
# Top-most EditorConfig file.
root = true
# Common config.
[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespaces = true
# CoffeeScript
#
# https://github.com/polarmobile/coffeescript-style-guide/blob/master/README.md
[*.{,lit}coffee]
indent_size = 2
indent_style = space
# Markdown
[*.{md,mdwn,mdown,markdown}]
indent_size = 4
indent_style = space
# Package.json
#
# This indentation style is the one used by npm.
[/package.json]
indent_size = 2
indent_style = space
# Jade
[*.jade]
indent_size = 2
indent_style = space
# JavaScript
#
# Two spaces seems to be the standard most common style, at least in
# Node.js (http://nodeguide.com/style.html#tabs-vs-spaces).
[*.js]
indent_size = 2
indent_style = space
# Less
[*.less]
indent_size = 2
indent_style = space
# Sass
#
# Style used for http://libsass.com
[*.s[ac]ss]
indent_size = 2
indent_style = space
# YAML
#
# Only spaces are allowed.
[*.yaml]
indent_size = 2
indent_style = space

7
packages/xen-api/.gitignore vendored Normal file
View File

@ -0,0 +1,7 @@
/bower_components/
/dist/
npm-debug.log
!node_modules/*
node_modules/*/

View File

@ -0,0 +1,90 @@
{
// Julien Fontanet JSHint configuration
// https://gist.github.com/julien-f/8095615
//
// Changes from defaults:
// - all enforcing options enabled (except `++`, `--`, ES3 and strict mode which is enabled automatically by Babel)
// - single quotes
// - all relaxing options disabled (except ES5 and ES6)
// - environments are set to Browserify, mocha & Node.js
//
// See http://jshint.com/docs/ for more details
"maxerr" : 50, // {int} Maximum error before stopping
// Enforcing
"bitwise" : true, // true: Prohibit bitwise operators (&, |, ^, etc.)
"camelcase" : true, // true: Identifiers must be in camelCase
"curly" : true, // true: Require {} for every new block or scope
"eqeqeq" : true, // true: Require triple equals (===) for comparison
"es3" : false, // true: Require ES3 compatible code (for IE < 9)
"forin" : true, // true: Require filtering for..in loops with obj.hasOwnProperty()
"freeze" : true, // true: Prohibit overwriting prototypes of native objects (Array, Date, ...)
"futurehostile" : true, // true: Prohibit use of identifiers reserved for future JavaScript versions.
"latedef" : true, // true: Require variables/functions to be defined before being used
"noarg" : true, // true: Prohibit use of `arguments.caller` and `arguments.callee`
"nocomma" : true, // true: Prohibit use of the comma operator
"nonbsp" : true, // true: Prohibit use of non breakable spaces
"nonew" : true, // true: Prohibit use of constructors for side-effects (without assignment)
"plusplus" : false, // true: Prohibit use of `++` & `--`
"quotmark" : "single", // Quotation mark consistency:
// false : do nothing (default)
// true : ensure whatever is used is consistent
// "single" : require single quotes
// "double" : require double quotes
"singleGroups" : true, // Prohibit unnecessary use of the grouping operator `()`
"undef" : true, // true: Require all non-global variables to be declared (prevents global leaks)
"unused" : true, // true: Require all defined variables be used
"strict" : false, // true: Requires all functions run in ES5 Strict Mode
"maxcomplexity" : 7, // {int} Max cyclomatic complexity per function
"maxdepth" : 3, // {int} Max depth of nested blocks (within functions)
"maxlen" : 80, // {int} Max number of characters per line
"maxparams" : 4, // {int} Max number of formal params allowed per function
"maxstatements" : 20, // {int} Max number statements per function
// Relaxing
"asi" : false, // true: Tolerate Automatic Semicolon Insertion (no semicolons)
"boss" : false, // true: Tolerate assignments where comparisons would be expected
"debug" : false, // true: Allow debugger statements e.g. browser breakpoints.
"elision" : false, // true: Tolerate use of ES3 array elision element
"eqnull" : false, // true: Tolerate use of `== null`
"es5" : true, // true: Tolerate ES5 syntax
"esnext" : true, // true: Allow ES.next (ES6) syntax (ex: `const`)
"evil" : false, // true: Tolerate use of `eval` and `new Function()`
"expr" : false, // true: Tolerate `ExpressionStatement` as Programs
"globalstrict" : false, // true: Allow global "use strict" (also enables 'strict')
"iterator" : false, // true: Tolerate using the `__iterator__` property
"lastsemic" : false, // true: Tolerate omitting a semicolon for the last statement of a 1-line block
"loopfunc" : false, // true: Tolerate functions being defined in loops
"moz" : false, // true: Allow Mozilla specific syntax (extends and overrides esnext features)
// (ex: `for each`, multiple try/catch, function expression…)
"noyield" : false, // true: Tolerate use of generators without `yield`s.
"notypeof" : false, // true: Tolerate typeof comparison with unknown values.
"proto" : false, // true: Tolerate using the `__proto__` property
"scripturl" : false, // true: Tolerate script-targeted URLs
"shadow" : false, // true: Allows re-define variables later in code e.g. `var x=1; x=2;`
"supernew" : false, // true: Tolerate `new function () { ... };` and `new Object;`
"validthis" : false, // true: Tolerate using this in a non-constructor function
"noyield" : false, // true: Tolerate generators without yields
// Environments
"browser" : false, // Web Browser (window, document, etc)
"browserify" : true, // Browserify (node.js code in the browser)
"couch" : false, // CouchDB
"devel" : false, // Development/debugging (alert, confirm, etc)
"dojo" : false, // Dojo Toolkit
"jquery" : false, // jQuery
"mocha" : true, // mocha
"mootools" : false, // MooTools
"node" : true, // Node.js
"nonstandard" : false, // Widely adopted globals (escape, unescape, etc)
"phantom" : false, // PhantomJS
"prototypejs" : false, // Prototype and Scriptaculous
"rhino" : false, // Rhino
"worker" : false, // Web Workers
"wsh" : false, // Windows Scripting Host
"yui" : false, // Yahoo User Interface
// Custom Globals
"globals" : {} // additional predefined global variables
}

View File

@ -0,0 +1,2 @@
*.spec.js
*.spec.js.map

View File

@ -0,0 +1,5 @@
language: node_js
node_js:
- 'iojs'
- '0.12'
- '0.10'

View File

@ -0,0 +1,52 @@
# ${pkg.name} [![Build Status](https://travis-ci.org/${pkg.shortGitHubPath}.png?branch=master)](https://travis-ci.org/${pkg.shortGitHubPath})
> ${pkg.description}
## Install
Installation of the [npm package](https://npmjs.org/package/${pkg.name}):
```
> npm install --save ${pkg.name}
```
## Usage
**TODO**
## Development
### Installing dependencies
```
> npm install
```
### Compilation
The sources files are watched and automatically recompiled on changes.
```
> npm run dev
```
### Tests
```
> npm run test-dev
```
## Contributions
Contributions are *very* welcomed, either on the documentation or on
the code.
You may:
- report any [issue](${pkg.bugs})
you've encountered;
- fork and create a pull request.
## License
${pkg.license} © [${pkg.author.name}](${pkg.author.url})

View File

@ -0,0 +1,50 @@
{
"private": true,
"name": "",
"version": "0.0.0",
"license": "ISC",
"description": "",
"keywords": [],
"homepage": "",
"bugs": "",
"repository": {
"type": "git",
"url": ""
},
"author": {
"name": "Julien Fontanet",
"email": "julien.fontanet@isonoe.net"
},
"preferGlobal": false,
"main": "dist/",
"files": [
"dist/"
],
"dependencies": {
"babel-runtime": "^4",
"bluebird": "^2.9.21",
"debug": "^2.1.3",
"lodash.foreach": "^3.0.2",
"lodash.size": "^3.0.0",
"make-error": "^0.3.0",
"xmlrpc": "^1.3.0"
},
"devDependencies": {
"babel": "^4",
"mocha": "*",
"must": "*",
"standard": "*"
},
"scripts": {
"build": "mkdir --parents dist && babel --optional=runtime --compact=true --source-maps --out-dir=dist/ src/",
"dev": "mkdir --parents dist && babel --watch --optional=runtime --compact=true --source-maps --out-dir=dist/ src/",
"prepublish": "npm build",
"test": "standard && npm run build && mocha 'dist/**/*.spec.js'",
"test-dev": "standard && mocha --watch --reporter=min 'dist/**/*.spec.js'"
},
"standard": {
"ignore": [
"dist/**"
]
}
}

View File

@ -0,0 +1,43 @@
import forEach from 'lodash.foreach'
import size from 'lodash.size'
import {createClient} from './'
// ===================================================================
// Creation
const xapi = createClient({
url: 'https://192.168.1.1',
auth: {
user: 'root',
password: 'qwerty'
}
})
// ===================================================================
// Events
xapi.on('events', function (events) {
forEach(events, event => {
console.log(
'[event] %s %s: %s',
event.operation,
event.class,
event.ref
)
})
})
xapi.on('eventsLost', function () {
console.warn('[event] some events may have been lost')
})
// ===================================================================
// Method call
xapi.call('VM.get_all_records', [])
.then(function (vms) {
console.log('%s VMs fetched', size(vms))
})
.catch(function (error) {
console.error(error)
})

View File

@ -0,0 +1,209 @@
import Bluebird, {promisify} from 'bluebird'
import createDebug from 'debug'
import makeError from 'make-error'
import {
createClient as createXmlRpcClient,
createSecureClient as createSecureXmlRpcClient
} from 'xmlrpc'
import {EventEmitter} from 'events'
const debug = createDebug('xo:xapi')
// ===================================================================
// http://www.gnu.org/software/libc/manual/html_node/Error-Codes.html
const NETWORK_ERRORS = {
// Connection has been closed outside of our control.
ECONNRESET: true,
// Connection has been aborted locally.
ECONNABORTED: true,
// Host is up but refuses connection (typically: no such service).
ECONNREFUSED: true,
// TODO: ??
EINVAL: true,
// Host is not reachable (does not respond).
EHOSTUNREAD: true,
// Connection configured timed out has been reach.
ETIMEDOUT: true
}
const isNetworkError = (error) => NETWORK_ERRORS[error.code]
// -------------------------------------------------------------------
const XAPI_NETWORK_ERRORS = {
HOST_STILL_BOOTING: true,
HOST_HAS_NO_MANAGEMENT_IP: true
}
const isXapiNetworkError = (error) => XAPI_NETWORK_ERRORS[error.code]
// -------------------------------------------------------------------
const isSessionInvalid = (error) => error.code === 'SESSION_INVALID'
const areEventsLost = (error) => error.code === 'EVENTS_LOST'
// -------------------------------------------------------------------
class XapiError {
constructor (error) {
XapiError.super.call(this, error[0])
this.code = error[0]
this.params = error.slice(1)
}
}
makeError(XapiError)
// ===================================================================
const URL_RE = /^(?:http(s)?:\/\/)?([^/]+?)(?::([0-9]+))?(?:\/.*)?$/
function parseUrl (url) {
const matches = URL_RE.exec(url)
if (!matches) {
throw new Error('invalid URL: ' + url)
}
const [, isSecure, host, port] = matches
return {
isSecure: Boolean(isSecure),
host,
port: port !== undefined ?
+port :
isSecure ? 443 : 80
}
}
// ===================================================================
export class Xapi extends EventEmitter {
constructor (opts) {
super()
this._url = parseUrl(opts.url)
this._auth = opts.auth
this._sessionId = null
this._init()
this._fromToken = ''
this._watchEvents()
}
// High level calls.
call (method, args) {
// When no arguments are passed, return a curried version of the
// method.
//
// Example:
//
// var logIn = xapi.call('session.login_with_password')
// logIn('user', 'password')
if (!args) {
return (...args) => this.call(method, args)
}
return this._sessionCall(method, args)
}
_logIn () {
if (!this._sessionId) {
this._sessionId = this._transportCall('session.login_with_password', [
this._auth.user,
this._auth.password
])
}
return this._sessionId
}
// Medium level call: handle session errors.
_sessionCall (method, args) {
return this._logIn().then((sessionId) => {
return this._transportCall(method, [sessionId].concat(args))
}).catch(isSessionInvalid, () => {
// XAPI is sometimes reinitialized and sessions are lost.
// Try to login again.
return this._sessionCall(method, args)
})
}
// Low level call: handle transport errors.
_transportCall (method, args) {
debug('%s: %s(%j)', this._readableId, method, args)
return this._xmlRpcCall(method, args)
.then(result => {
const {Status: status} = result
// Return the plain result if it does not have a valid XAPI
// format.
if (!status) {
return result
}
if (status === 'Success') {
return result.Value
}
throw new XapiError(result.ErrorDescription)
})
.catch(isNetworkError, isXapiNetworkError, () => {
// TODO: ability to cancel the connection
// TODO: ability to force immediate reconnection
// TODO: implement back-off
return Bluebird.delay(5e3).then(() => {
// TODO: handling HOST_IS_SLAVE.
// TODO: handling not responding host.
return this._transportCall(method, args)
})
})
}
_init () {
const {isSecure, host, port} = this._url
const client = (isSecure ?
createSecureXmlRpcClient :
createXmlRpcClient
)({
host,
port,
rejectUnauthorized: false,
timeout: 10
})
this._xmlRpcCall = promisify(client.methodCall, client)
}
_watchEvents () {
this.call('event.from', [
['*'], this._fromToken, 1e3 + 0.1
]).then(({token, events}) => {
this._fromToken = token
if (events.length) {
this.emit('events', events)
}
}).catch(areEventsLost, () => {
this.emit('eventsLost')
}).then(() => {
this._watchEvents()
})
}
}
// ===================================================================
// The default value is a factory function.
export const createClient = (opts) => new Xapi(opts)

View File

@ -0,0 +1,17 @@
/* eslint-env mocha */
import {createClient, Xapi} from './'
import expect from 'must'
// ===================================================================
describe('createClient()', function () {
it('is a function', function () {
expect(createClient).to.be.a.function()
})
it('returns an instance of Xapi', function () {
expect(createClient('example.org')).to.be.a(Xapi)
})
})