Compare commits
217 Commits
xen-api-v0
...
xen-api-v0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f94fe0b325 | ||
|
|
cc543ae33a | ||
|
|
5df1e62d53 | ||
|
|
1dff6f81fc | ||
|
|
ecf2d95318 | ||
|
|
a1af272180 | ||
|
|
72f0793aeb | ||
|
|
e150be6a3e | ||
|
|
d71b13f2a2 | ||
|
|
66c88b37a4 | ||
|
|
a7198d2e83 | ||
|
|
d3584b5820 | ||
|
|
e2e6267dd7 | ||
|
|
aa5baecdc3 | ||
|
|
601b2aa6bb | ||
|
|
d09ba38dbd | ||
|
|
b847c2b7b5 | ||
|
|
30e44cb533 | ||
|
|
d43b83081b | ||
|
|
699363c548 | ||
|
|
6ea3eb4ba6 | ||
|
|
4aa20b6f6a | ||
|
|
9c8ea27238 | ||
|
|
22c515b0e7 | ||
|
|
7e2bd52f25 | ||
|
|
e65dd15edc | ||
|
|
781ffa5574 | ||
|
|
8fe3b1a368 | ||
|
|
69c48e2770 | ||
|
|
aa934ad725 | ||
|
|
8596ca607d | ||
|
|
643ea9e523 | ||
|
|
26f630d9d6 | ||
|
|
7481874ba2 | ||
|
|
a348585e76 | ||
|
|
8d135bc1fc | ||
|
|
2c00f0ffab | ||
|
|
5e1d627834 | ||
|
|
37b23bdc47 | ||
|
|
47f3788f55 | ||
|
|
63be8afd35 | ||
|
|
1166db7834 | ||
|
|
3d96765b1f | ||
|
|
4c52fe11d3 | ||
|
|
6c4178e107 | ||
|
|
02d1fb436d | ||
|
|
1c97bf9019 | ||
|
|
bfc4bb1f4c | ||
|
|
ccdce32562 | ||
|
|
87fdcf14e5 | ||
|
|
c0c63f49b1 | ||
|
|
4e1bef9537 | ||
|
|
8b78727d20 | ||
|
|
4cd7025be4 | ||
|
|
015ce2690d | ||
|
|
3e7f552a63 | ||
|
|
22882b1ff2 | ||
|
|
c7e6e72ce8 | ||
|
|
27e0621ad8 | ||
|
|
1987a8c68a | ||
|
|
1642798aa6 | ||
|
|
36b589c2db | ||
|
|
1f710b9b78 | ||
|
|
9b9f0e5607 | ||
|
|
590f6cb7b3 | ||
|
|
2c35ee11b3 | ||
|
|
bd0c747d98 | ||
|
|
b12fd45df1 | ||
|
|
c21fef7c72 | ||
|
|
b7f34b9da6 | ||
|
|
f82a6efda1 | ||
|
|
556ca71c3a | ||
|
|
42be2d5031 | ||
|
|
876e22b092 | ||
|
|
f9f9c16cb0 | ||
|
|
a881090e65 | ||
|
|
3d9fce02a4 | ||
|
|
ab6b1e2c32 | ||
|
|
59d9b3c6b4 | ||
|
|
8851a661c0 | ||
|
|
2d327961da | ||
|
|
3a41efbea8 | ||
|
|
45b8fd0100 | ||
|
|
155f2fc36c | ||
|
|
c3dc136de4 | ||
|
|
4cbc5c4e2f | ||
|
|
68820aaf59 | ||
|
|
cd65bc7683 | ||
|
|
6e0956f09f | ||
|
|
1191f0ba93 | ||
|
|
6534ffea26 | ||
|
|
f9173c41d1 | ||
|
|
a2faedcacb | ||
|
|
cb56b3b9d0 | ||
|
|
a61b50548c | ||
|
|
75ad588e0b | ||
|
|
6564edcc32 | ||
|
|
c46c0018ea | ||
|
|
35e8dcc3be | ||
|
|
d1600fd058 | ||
|
|
1416fb0c71 | ||
|
|
2975db247d | ||
|
|
03eaa652ce | ||
|
|
eac29993d3 | ||
|
|
af2a9225b8 | ||
|
|
a24de7fe3f | ||
|
|
16a9f44d4d | ||
|
|
6fcc148105 | ||
|
|
3485cb4ec4 | ||
|
|
b2a51bd658 | ||
|
|
e5ab1dc154 | ||
|
|
6274969635 | ||
|
|
069c430346 | ||
|
|
cbcc4dd21d | ||
|
|
b4cdf4d277 | ||
|
|
716d7bfcf6 | ||
|
|
b45a169a2f | ||
|
|
720b9ef999 | ||
|
|
9b9e4dddfc | ||
|
|
7434e0352f | ||
|
|
26d61af902 | ||
|
|
5bd12c5f9e | ||
|
|
e07fae4290 | ||
|
|
e304395179 | ||
|
|
6b83130853 | ||
|
|
9565718699 | ||
|
|
ac11885379 | ||
|
|
277669a13c | ||
|
|
fcbc476462 | ||
|
|
4944b415c7 | ||
|
|
5da7312d2d | ||
|
|
954d19fe50 | ||
|
|
addd86f5d2 | ||
|
|
1b90223210 | ||
|
|
95989ff63b | ||
|
|
799f758dce | ||
|
|
e075f1c08b | ||
|
|
7e0aa719b4 | ||
|
|
4ee352fdb2 | ||
|
|
96ea3ded4a | ||
|
|
8bbc6e9ff5 | ||
|
|
af7029812c | ||
|
|
c517b59138 | ||
|
|
5485e8a322 | ||
|
|
2540ac34b3 | ||
|
|
76e5d41a34 | ||
|
|
2c32a4e912 | ||
|
|
c66f7235b6 | ||
|
|
5444381f7d | ||
|
|
dc44679031 | ||
|
|
2cbd17b745 | ||
|
|
9ef13696d8 | ||
|
|
c3f635fd12 | ||
|
|
e3d1380435 | ||
|
|
f83737b538 | ||
|
|
bb1ea4e4d0 | ||
|
|
9cb4de2ea8 | ||
|
|
048cbf60ec | ||
|
|
36f40b4188 | ||
|
|
a3bba92063 | ||
|
|
ebcc6c9341 | ||
|
|
95f765055e | ||
|
|
49aa5ffccc | ||
|
|
d09d3fa80b | ||
|
|
4c8cd50643 | ||
|
|
eee72f4f27 | ||
|
|
45f6a7cb4d | ||
|
|
8866bd8663 | ||
|
|
3f9c515f1d | ||
|
|
c92567d4fa | ||
|
|
df3c76fa72 | ||
|
|
cea4157402 | ||
|
|
29ce3bd05e | ||
|
|
b3d58f4f0c | ||
|
|
d93d234c71 | ||
|
|
7fe9ae8a04 | ||
|
|
87cf1ed7cb | ||
|
|
a0ba5c8a57 | ||
|
|
d7208a15d9 | ||
|
|
debde0c67a | ||
|
|
97db55156a | ||
|
|
9d3477d465 | ||
|
|
031af000e6 | ||
|
|
0512fac3aa | ||
|
|
4272e8196a | ||
|
|
140f9d05df | ||
|
|
9222733243 | ||
|
|
5838c56c4e | ||
|
|
1814e0a260 | ||
|
|
711c5781e6 | ||
|
|
7e8c2211d8 | ||
|
|
f0858b7d93 | ||
|
|
3af6c28ab0 | ||
|
|
5c31c7f14c | ||
|
|
2610a9c777 | ||
|
|
58cf611497 | ||
|
|
61631e405b | ||
|
|
185e0849b1 | ||
|
|
f48b9d364b | ||
|
|
e4f1a7d4c1 | ||
|
|
e02f19ff67 | ||
|
|
72a2110845 | ||
|
|
9baa415249 | ||
|
|
22b840af14 | ||
|
|
61f32d89ca | ||
|
|
3c7da93dfc | ||
|
|
5831616fac | ||
|
|
d7b6d9f124 | ||
|
|
245978e2b3 | ||
|
|
3aae60bde9 | ||
|
|
91d36122eb | ||
|
|
36c1e2cc73 | ||
|
|
4a0a09ba3e | ||
|
|
04b44cff2b | ||
|
|
8309755ee3 | ||
|
|
41a75d404c | ||
|
|
8eb63de201 |
11
packages/xen-api/.gitignore
vendored
11
packages/xen-api/.gitignore
vendored
@@ -1,7 +1,12 @@
|
||||
/bower_components/
|
||||
/coverage/
|
||||
/dist/
|
||||
/examples/node_modules/
|
||||
/node_modules/
|
||||
|
||||
npm-debug.log
|
||||
npm-debug.log.*
|
||||
pnpm-debug.log
|
||||
pnpm-debug.log.*
|
||||
yarn-error.log*
|
||||
|
||||
!node_modules/*
|
||||
node_modules/*/
|
||||
/plot.dat
|
||||
|
||||
@@ -1,90 +0,0 @@
|
||||
{
|
||||
// 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
|
||||
}
|
||||
@@ -1,2 +1,24 @@
|
||||
/benchmark/
|
||||
/benchmarks/
|
||||
*.bench.js
|
||||
*.bench.js.map
|
||||
|
||||
/examples/
|
||||
example.js
|
||||
example.js.map
|
||||
*.example.js
|
||||
*.example.js.map
|
||||
|
||||
/fixture/
|
||||
/fixtures/
|
||||
*.fixture.js
|
||||
*.fixture.js.map
|
||||
*.fixtures.js
|
||||
*.fixtures.js.map
|
||||
|
||||
/test/
|
||||
/tests/
|
||||
*.spec.js
|
||||
*.spec.js.map
|
||||
|
||||
__snapshots__/
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- 'iojs'
|
||||
- '0.12'
|
||||
- '0.10'
|
||||
- stable
|
||||
- 6
|
||||
- 4
|
||||
|
||||
# Use containers.
|
||||
# http://docs.travis-ci.com/user/workers/container-based-infrastructure/
|
||||
sudo: false
|
||||
|
||||
@@ -1,6 +1,14 @@
|
||||
# xen-api [](https://travis-ci.org/js-xen-api)
|
||||
# xen-api [](https://travis-ci.org/vatesfr/xen-api)
|
||||
|
||||
> ${pkg.description}
|
||||
> Connector to the Xen API
|
||||
|
||||
Tested with:
|
||||
|
||||
- XenServer 7.1
|
||||
- XenServer 7
|
||||
- XenServer 6.5
|
||||
- XenServer 6.2
|
||||
- XenServer 5.6
|
||||
|
||||
## Install
|
||||
|
||||
@@ -12,6 +20,8 @@ Installation of the [npm package](https://npmjs.org/package/xen-api):
|
||||
|
||||
## Usage
|
||||
|
||||
### Library
|
||||
|
||||
```javascript
|
||||
var createClient = require('xen-api').createClient
|
||||
|
||||
@@ -20,9 +30,18 @@ var xapi = createClient({
|
||||
auth: {
|
||||
user: 'root',
|
||||
password: 'important secret password'
|
||||
}
|
||||
},
|
||||
readOnly: false
|
||||
})
|
||||
```
|
||||
|
||||
Options:
|
||||
|
||||
- `url`: address of a host in the pool we are trying to connect to
|
||||
- `auth`: credentials used to sign in
|
||||
- `readOnly = false`: if true, no methods with side-effects can be called
|
||||
|
||||
```js
|
||||
// Force connection.
|
||||
xapi.connect().catch(error => {
|
||||
console.error(error)
|
||||
@@ -34,26 +53,61 @@ xapi.objects.on('add', function (objects) {
|
||||
})
|
||||
```
|
||||
|
||||
> Note: all objects are frozen and cannot be altered!
|
||||
|
||||
Custom fields on objects (hidden − ie. non enumerable):
|
||||
- `$type`: the type of the object (`VM`, `task`, …);
|
||||
- `$ref`: the (opaque) reference of the object;
|
||||
- `$id`: the identifier of this object (its UUID if any, otherwise its reference);
|
||||
- `$pool`: the pool object this object belongs to.
|
||||
|
||||
Furthermore, any field containing a reference (or references if an
|
||||
array) can be resolved by prepending the field name with a `$`:
|
||||
|
||||
```javascript
|
||||
console.log(xapi.pool.$master.$resident_VMs[0].name_label)
|
||||
// vm1
|
||||
```
|
||||
|
||||
### CLI
|
||||
|
||||
A CLI is provided to help exploration and discovery of the XAPI.
|
||||
|
||||
```
|
||||
> xen-api https://xen1.company.net root
|
||||
Password: ******
|
||||
root@xen1.company.net> xapi.status
|
||||
'connected'
|
||||
root@xen1.company.net> xapi.pool.master
|
||||
'OpaqueRef:ec7c5147-8aee-990f-c70b-0de916a8e993'
|
||||
root@xen1.company.net> xapi.pool.$master.name_label
|
||||
'xen1'
|
||||
```
|
||||
|
||||
To ease searches, `find()` and `findAll()` functions are available:
|
||||
|
||||
```
|
||||
root@xen1.company.net> findAll({ $type: 'vm' }).length
|
||||
183
|
||||
```
|
||||
|
||||
## Development
|
||||
|
||||
### Installing dependencies
|
||||
|
||||
```
|
||||
# Install dependencies
|
||||
> npm install
|
||||
```
|
||||
|
||||
### Compilation
|
||||
# Run the tests
|
||||
> npm test
|
||||
|
||||
The sources files are watched and automatically recompiled on changes.
|
||||
|
||||
```
|
||||
# Continuously compile
|
||||
> npm run dev
|
||||
```
|
||||
|
||||
### Tests
|
||||
# Continuously run the tests
|
||||
> npm run dev-test
|
||||
|
||||
```
|
||||
> npm run test-dev
|
||||
# Build for production (automatically called by npm install)
|
||||
> npm run build
|
||||
```
|
||||
|
||||
## Contributions
|
||||
@@ -63,7 +117,7 @@ the code.
|
||||
|
||||
You may:
|
||||
|
||||
- report any [issue](https://github.com/julien-f/js-xen-api/issues)
|
||||
- report any [issue](https://github.com/pbdxen-api/issues)
|
||||
you've encountered;
|
||||
- fork and create a pull request.
|
||||
|
||||
|
||||
50
packages/xen-api/examples/export-vdi
Executable file
50
packages/xen-api/examples/export-vdi
Executable file
@@ -0,0 +1,50 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
process.env.DEBUG = '*'
|
||||
|
||||
const defer = require('golike-defer').default
|
||||
const pump = require('pump')
|
||||
const { fromCallback } = require('promise-toolbox')
|
||||
|
||||
const { createClient } = require('../')
|
||||
|
||||
const { createOutputStream, resolveRef } = require('./utils')
|
||||
|
||||
defer(async ($defer, args) => {
|
||||
let raw = false
|
||||
if (args[0] === '--raw') {
|
||||
raw = true
|
||||
args.shift()
|
||||
}
|
||||
|
||||
if (args.length < 2) {
|
||||
return console.log('Usage: export-vdi [--raw] <XS URL> <VDI identifier> [<VHD file>]')
|
||||
}
|
||||
|
||||
const xapi = createClient({
|
||||
allowUnauthorized: true,
|
||||
url: args[0],
|
||||
watchEvents: false
|
||||
})
|
||||
|
||||
await xapi.connect()
|
||||
$defer(() => xapi.disconnect())
|
||||
|
||||
// https://xapi-project.github.io/xen-api/snapshots.html#downloading-a-disk-or-snapshot
|
||||
const exportStream = await xapi.getResource('/export_raw_vdi/', {
|
||||
query: {
|
||||
format: raw ? 'raw' : 'vhd',
|
||||
vdi: await resolveRef(xapi, 'VDI', args[1])
|
||||
}
|
||||
})
|
||||
|
||||
console.warn('Export task:', exportStream.headers['task-id'])
|
||||
|
||||
await fromCallback(cb => pump(
|
||||
exportStream,
|
||||
createOutputStream(args[2]),
|
||||
cb
|
||||
))
|
||||
})(process.argv.slice(2)).catch(
|
||||
console.error.bind(console, 'error')
|
||||
)
|
||||
44
packages/xen-api/examples/export-vm
Executable file
44
packages/xen-api/examples/export-vm
Executable file
@@ -0,0 +1,44 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
process.env.DEBUG = '*'
|
||||
|
||||
const defer = require('golike-defer').default
|
||||
const pump = require('pump')
|
||||
const { fromCallback } = require('promise-toolbox')
|
||||
|
||||
const { createClient } = require('../')
|
||||
|
||||
const { createOutputStream, resolveRef } = require('./utils')
|
||||
|
||||
defer(async ($defer, args) => {
|
||||
if (args.length < 2) {
|
||||
return console.log('Usage: export-vm <XS URL> <VM identifier> [<XVA file>]')
|
||||
}
|
||||
|
||||
const xapi = createClient({
|
||||
allowUnauthorized: true,
|
||||
url: args[0],
|
||||
watchEvents: false
|
||||
})
|
||||
|
||||
await xapi.connect()
|
||||
$defer(() => xapi.disconnect())
|
||||
|
||||
// https://xapi-project.github.io/xen-api/importexport.html
|
||||
const exportStream = await xapi.getResource('/export/', {
|
||||
query: {
|
||||
ref: await resolveRef(xapi, 'VM', args[1]),
|
||||
use_compression: 'true'
|
||||
}
|
||||
})
|
||||
|
||||
console.warn('Export task:', exportStream.headers['task-id'])
|
||||
|
||||
await fromCallback(cb => pump(
|
||||
exportStream,
|
||||
createOutputStream(args[2]),
|
||||
cb
|
||||
))
|
||||
})(process.argv.slice(2)).catch(
|
||||
console.error.bind(console, 'error')
|
||||
)
|
||||
40
packages/xen-api/examples/import-vdi
Executable file
40
packages/xen-api/examples/import-vdi
Executable file
@@ -0,0 +1,40 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
process.env.DEBUG = '*'
|
||||
|
||||
const defer = require('golike-defer').default
|
||||
|
||||
const { createClient } = require('../')
|
||||
|
||||
const { createInputStream, resolveRef } = require('./utils')
|
||||
|
||||
defer(async ($defer, args) => {
|
||||
let raw = false
|
||||
if (args[0] === '--raw') {
|
||||
raw = true
|
||||
args.shift()
|
||||
}
|
||||
|
||||
if (args.length < 2) {
|
||||
return console.log('Usage: import-vdi [--raw] <XS URL> <VDI identifier> [<VHD file>]')
|
||||
}
|
||||
|
||||
const xapi = createClient({
|
||||
allowUnauthorized: true,
|
||||
url: args[0],
|
||||
watchEvents: false
|
||||
})
|
||||
|
||||
await xapi.connect()
|
||||
$defer(() => xapi.disconnect())
|
||||
|
||||
// https://xapi-project.github.io/xen-api/snapshots.html#uploading-a-disk-or-snapshot
|
||||
await xapi.putResource(createInputStream(args[2]), '/import_raw_vdi/', {
|
||||
query: {
|
||||
format: raw ? 'raw' : 'vhd',
|
||||
vdi: await resolveRef(xapi, 'VDI', args[1])
|
||||
}
|
||||
})
|
||||
})(process.argv.slice(2)).catch(
|
||||
console.error.bind(console, 'error')
|
||||
)
|
||||
31
packages/xen-api/examples/import-vm
Executable file
31
packages/xen-api/examples/import-vm
Executable file
@@ -0,0 +1,31 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
process.env.DEBUG = '*'
|
||||
|
||||
const defer = require('golike-defer').default
|
||||
|
||||
const { createClient } = require('../')
|
||||
|
||||
const { createInputStream, resolveRef } = require('./utils')
|
||||
|
||||
defer(async ($defer, args) => {
|
||||
if (args.length < 1) {
|
||||
return console.log('Usage: import-vm <XS URL> [<XVA file>] [<SR identifier>]')
|
||||
}
|
||||
|
||||
const xapi = createClient({
|
||||
allowUnauthorized: true,
|
||||
url: args[0],
|
||||
watchEvents: false
|
||||
})
|
||||
|
||||
await xapi.connect()
|
||||
$defer(() => xapi.disconnect())
|
||||
|
||||
// https://xapi-project.github.io/xen-api/importexport.html
|
||||
await xapi.putResource(createInputStream(args[1]), '/import/', {
|
||||
query: args[2] && { sr_id: await resolveRef(xapi, 'SR', args[2]) }
|
||||
})
|
||||
})(process.argv.slice(2)).catch(
|
||||
console.error.bind(console, 'error')
|
||||
)
|
||||
30
packages/xen-api/src/example.js → packages/xen-api/examples/log-events
Normal file → Executable file
30
packages/xen-api/src/example.js → packages/xen-api/examples/log-events
Normal file → Executable file
@@ -1,33 +1,37 @@
|
||||
import forEach from 'lodash.foreach'
|
||||
import size from 'lodash.size'
|
||||
import {createClient} from './'
|
||||
#!/usr/bin/env node
|
||||
|
||||
import sourceMapSupport from 'source-map-support'
|
||||
sourceMapSupport.install()
|
||||
require('source-map-support').install()
|
||||
|
||||
const { forEach, size } = require('lodash')
|
||||
|
||||
const { createClient } = require('../')
|
||||
|
||||
// ===================================================================
|
||||
|
||||
if (process.argv.length < 3) {
|
||||
return console.log('Usage: log-events <XS URL>')
|
||||
}
|
||||
|
||||
// ===================================================================
|
||||
// Creation
|
||||
|
||||
const xapi = createClient({
|
||||
url: 'https://192.168.1.1',
|
||||
auth: {
|
||||
user: 'root',
|
||||
password: 'qwerty'
|
||||
}
|
||||
allowUnauthorized: true,
|
||||
url: process.argv[2]
|
||||
})
|
||||
|
||||
// ===================================================================
|
||||
// Method call
|
||||
|
||||
const getAllVms = xapi.call('VM.get_all_records')
|
||||
|
||||
getAllVms()
|
||||
xapi.connect().then(() => {
|
||||
xapi.call('VM.get_all_records')
|
||||
.then(function (vms) {
|
||||
console.log('%s VMs fetched', size(vms))
|
||||
})
|
||||
.catch(function (error) {
|
||||
console.error(error)
|
||||
})
|
||||
})
|
||||
|
||||
// ===================================================================
|
||||
// Objects
|
||||
6
packages/xen-api/examples/package.json
Normal file
6
packages/xen-api/examples/package.json
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"dependencies": {
|
||||
"golike-defer": "^0.1.0",
|
||||
"pump": "^1.0.2"
|
||||
}
|
||||
}
|
||||
41
packages/xen-api/examples/utils.js
Normal file
41
packages/xen-api/examples/utils.js
Normal file
@@ -0,0 +1,41 @@
|
||||
const { createReadStream, createWriteStream, statSync } = require('fs')
|
||||
const { PassThrough } = require('stream')
|
||||
|
||||
const { isOpaqueRef } = require('../')
|
||||
|
||||
exports.createInputStream = path => {
|
||||
if (path === undefined || path === '-') {
|
||||
return process.stdin
|
||||
}
|
||||
|
||||
const { size } = statSync(path)
|
||||
|
||||
const stream = createReadStream(path)
|
||||
stream.length = size
|
||||
return stream
|
||||
}
|
||||
|
||||
exports.createOutputStream = path => {
|
||||
if (path !== undefined && path !== '-') {
|
||||
return createWriteStream(path)
|
||||
}
|
||||
|
||||
// introduce a through stream because stdout is not a normal stream!
|
||||
const stream = new PassThrough()
|
||||
stream.pipe(process.stdout)
|
||||
return stream
|
||||
}
|
||||
|
||||
exports.resolveRef = (xapi, type, refOrUuidOrNameLabel) =>
|
||||
isOpaqueRef(refOrUuidOrNameLabel)
|
||||
? refOrUuidOrNameLabel
|
||||
: xapi.call(`${type}.get_by_uuid`, refOrUuidOrNameLabel).catch(
|
||||
() => xapi.call(`${type}.get_by_name_label`, refOrUuidOrNameLabel).then(
|
||||
refs => {
|
||||
if (refs.length === 1) {
|
||||
return refs[0]
|
||||
}
|
||||
throw new Error(`no single match for ${type} with name label ${refOrUuidOrNameLabel}`)
|
||||
}
|
||||
)
|
||||
)
|
||||
30
packages/xen-api/examples/yarn.lock
Normal file
30
packages/xen-api/examples/yarn.lock
Normal file
@@ -0,0 +1,30 @@
|
||||
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
|
||||
# yarn lockfile v1
|
||||
|
||||
|
||||
end-of-stream@^1.1.0:
|
||||
version "1.4.0"
|
||||
resolved "https://registry.yarnpkg.com/end-of-stream/-/end-of-stream-1.4.0.tgz#7a90d833efda6cfa6eac0f4949dbb0fad3a63206"
|
||||
dependencies:
|
||||
once "^1.4.0"
|
||||
|
||||
golike-defer@^0.1.0:
|
||||
version "0.1.0"
|
||||
resolved "https://registry.yarnpkg.com/golike-defer/-/golike-defer-0.1.0.tgz#70a3d8991cdfe41845956bfb578f69bc3e49f525"
|
||||
|
||||
once@^1.3.1, once@^1.4.0:
|
||||
version "1.4.0"
|
||||
resolved "https://registry.yarnpkg.com/once/-/once-1.4.0.tgz#583b1aa775961d4b113ac17d9c50baef9dd76bd1"
|
||||
dependencies:
|
||||
wrappy "1"
|
||||
|
||||
pump@^1.0.2:
|
||||
version "1.0.2"
|
||||
resolved "https://registry.yarnpkg.com/pump/-/pump-1.0.2.tgz#3b3ee6512f94f0e575538c17995f9f16990a5d51"
|
||||
dependencies:
|
||||
end-of-stream "^1.1.0"
|
||||
once "^1.3.1"
|
||||
|
||||
wrappy@1:
|
||||
version "1.0.2"
|
||||
resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f"
|
||||
4
packages/xen-api/memory-test.gnu
Normal file
4
packages/xen-api/memory-test.gnu
Normal file
@@ -0,0 +1,4 @@
|
||||
set yrange [ 0 : ]
|
||||
set grid
|
||||
|
||||
plot for [i=2:4] "plot.dat" using 1:i with lines
|
||||
@@ -1,8 +1,8 @@
|
||||
{
|
||||
"name": "xen-api",
|
||||
"version": "0.2.0",
|
||||
"version": "0.13.1",
|
||||
"license": "ISC",
|
||||
"description": "",
|
||||
"description": "Connector to the Xen API",
|
||||
"keywords": [
|
||||
"xen",
|
||||
"api",
|
||||
@@ -10,11 +10,11 @@
|
||||
"xenapi",
|
||||
"xapi"
|
||||
],
|
||||
"homepage": "https://github.com/julien-f/js-xen-api",
|
||||
"bugs": "https://github.com/julien-f/js-xen-api/issues",
|
||||
"homepage": "https://github.com/vatesfr/xen-api",
|
||||
"bugs": "https://github.com/vatesfr/xen-api/issues",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/julien-f/js-xen-api.git"
|
||||
"url": "https://github.com/vatesfr/xen-api.git"
|
||||
},
|
||||
"author": {
|
||||
"name": "Julien Fontanet",
|
||||
@@ -22,38 +22,86 @@
|
||||
},
|
||||
"preferGlobal": false,
|
||||
"main": "dist/",
|
||||
"bin": {
|
||||
"xen-api": "dist/cli.js"
|
||||
},
|
||||
"files": [
|
||||
"dist/"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=4"
|
||||
},
|
||||
"dependencies": {
|
||||
"babel-runtime": "^5",
|
||||
"bluebird": "^2.9.21",
|
||||
"debug": "^2.1.3",
|
||||
"lodash.find": "^3.2.0",
|
||||
"lodash.findkey": "^3.0.1",
|
||||
"lodash.foreach": "^3.0.2",
|
||||
"lodash.size": "^3.0.0",
|
||||
"lodash.startswith": "^3.0.1",
|
||||
"make-error": "^1.0.1",
|
||||
"xmlrpc": "^1.3.0",
|
||||
"xo-collection": "0.0.1"
|
||||
"babel-polyfill": "^6.23.0",
|
||||
"blocked": "^1.2.1",
|
||||
"debug": "^2.6.8",
|
||||
"event-to-promise": "^0.8.0",
|
||||
"exec-promise": "^0.7.0",
|
||||
"http-request-plus": "^0.3.0",
|
||||
"json-rpc-protocol": "^0.11.2",
|
||||
"kindof": "^2.0.0",
|
||||
"lodash": "^4.17.4",
|
||||
"make-error": "^1.3.0",
|
||||
"minimist": "^1.2.0",
|
||||
"ms": "^2.0.0",
|
||||
"promise-toolbox": "^0.9.4",
|
||||
"pw": "0.0.4",
|
||||
"xmlrpc": "^1.3.2",
|
||||
"xo-collection": "^0.4.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"babel": "^5",
|
||||
"mocha": "*",
|
||||
"must": "*",
|
||||
"standard": "*"
|
||||
"babel-cli": "^6.24.1",
|
||||
"babel-eslint": "^7.2.3",
|
||||
"babel-plugin-lodash": "^3.2.11",
|
||||
"babel-plugin-transform-decorators-legacy": "^1.3.4",
|
||||
"babel-preset-env": "^1.5.1",
|
||||
"babel-preset-stage-0": "^6.24.1",
|
||||
"cross-env": "^5.0.0",
|
||||
"dependency-check": "^2.8.0",
|
||||
"husky": "^0.13.3",
|
||||
"jest": "^20.0.3",
|
||||
"rimraf": "^2.6.1",
|
||||
"standard": "^10.0.2"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "mkdir --parents dist && babel --optional=runtime --compact=true --source-maps --out-dir=dist/ src/",
|
||||
"dev": "mkdir --parents dist && babel --watch --optional=runtime --source-maps --out-dir=dist/ src/",
|
||||
"build": "cross-env NODE_ENV=production babel --source-maps --out-dir=dist/ src/",
|
||||
"commitmsg": "npm test",
|
||||
"dev": "cross-env NODE_ENV=development babel --watch --source-maps --out-dir=dist/ src/",
|
||||
"dev-test": "jest --bail --watch",
|
||||
"plot": "gnuplot -p memory-test.gnu",
|
||||
"posttest": "standard && dependency-check ./package.json",
|
||||
"prebuild": "rimraf dist/",
|
||||
"predev": "npm run prebuild",
|
||||
"prepublish": "npm run build",
|
||||
"test": "standard && mocha 'dist/**/*.spec.js'",
|
||||
"test-dev": "standard && mocha --watch --reporter=min 'dist/**/*.spec.js'"
|
||||
"test": "jest"
|
||||
},
|
||||
"babel": {
|
||||
"plugins": [
|
||||
"lodash",
|
||||
"transform-decorators-legacy"
|
||||
],
|
||||
"presets": [
|
||||
[
|
||||
"env",
|
||||
{
|
||||
"targets": {
|
||||
"node": 4
|
||||
}
|
||||
}
|
||||
],
|
||||
"stage-0"
|
||||
]
|
||||
},
|
||||
"jest": {
|
||||
"roots": [
|
||||
"<rootDir>/src"
|
||||
],
|
||||
"testRegex": "\\.spec\\.js$"
|
||||
},
|
||||
"standard": {
|
||||
"ignore": [
|
||||
"dist/**"
|
||||
]
|
||||
"dist"
|
||||
],
|
||||
"parser": "babel-eslint"
|
||||
}
|
||||
}
|
||||
|
||||
110
packages/xen-api/src/cli.js
Executable file
110
packages/xen-api/src/cli.js
Executable file
@@ -0,0 +1,110 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import 'babel-polyfill'
|
||||
|
||||
import blocked from 'blocked'
|
||||
import createDebug from 'debug'
|
||||
import eventToPromise from 'event-to-promise'
|
||||
import execPromise from 'exec-promise'
|
||||
import minimist from 'minimist'
|
||||
import pw from 'pw'
|
||||
import { asCallback, fromCallback } from 'promise-toolbox'
|
||||
import { filter, find, isArray } from 'lodash'
|
||||
import { start as createRepl } from 'repl'
|
||||
|
||||
import { createClient } from './'
|
||||
|
||||
// ===================================================================
|
||||
|
||||
function askPassword (prompt = 'Password: ') {
|
||||
if (prompt) {
|
||||
process.stdout.write(prompt)
|
||||
}
|
||||
|
||||
return new Promise(resolve => {
|
||||
pw(resolve)
|
||||
})
|
||||
}
|
||||
|
||||
function required (name) {
|
||||
throw new Error(`missing required argument ${name}`)
|
||||
}
|
||||
|
||||
// ===================================================================
|
||||
|
||||
const usage = 'Usage: xen-api <url> <user> [<password>]'
|
||||
|
||||
const main = async args => {
|
||||
const opts = minimist(args, {
|
||||
boolean: ['allow-unauthorized', 'help', 'read-only', 'verbose'],
|
||||
|
||||
alias: {
|
||||
'allow-unauthorized': 'au',
|
||||
debounce: 'd',
|
||||
help: 'h',
|
||||
'read-only': 'ro',
|
||||
verbose: 'v'
|
||||
}
|
||||
})
|
||||
|
||||
if (opts.help) {
|
||||
return usage
|
||||
}
|
||||
|
||||
if (opts.verbose) {
|
||||
// Does not work perfectly.
|
||||
//
|
||||
// https://github.com/visionmedia/debug/pull/156
|
||||
createDebug.enable('xen-api,xen-api:*')
|
||||
}
|
||||
|
||||
const [
|
||||
url = required('url'),
|
||||
user = required('user'),
|
||||
password = await askPassword()
|
||||
] = opts._
|
||||
|
||||
{
|
||||
const debug = createDebug('xen-api:perf')
|
||||
blocked(ms => {
|
||||
debug('blocked for %sms', ms | 0)
|
||||
})
|
||||
}
|
||||
|
||||
const xapi = createClient({
|
||||
url,
|
||||
allowUnauthorized: opts.au,
|
||||
auth: { user, password },
|
||||
debounce: opts.debounce != null ? +opts.debounce : null,
|
||||
readOnly: opts.ro
|
||||
})
|
||||
await xapi.connect()
|
||||
|
||||
const repl = createRepl({
|
||||
prompt: `${xapi._humanId}> `
|
||||
})
|
||||
repl.context.xapi = xapi
|
||||
|
||||
repl.context.find = predicate => find(xapi.objects.all, predicate)
|
||||
repl.context.findAll = predicate => filter(xapi.objects.all, predicate)
|
||||
|
||||
// Make the REPL waits for promise completion.
|
||||
repl.eval = (evaluate => (cmd, context, filename, cb) => {
|
||||
fromCallback(cb => {
|
||||
evaluate.call(repl, cmd, context, filename, cb)
|
||||
}).then(value =>
|
||||
isArray(value) ? Promise.all(value) : value
|
||||
)::asCallback(cb)
|
||||
})(repl.eval)
|
||||
|
||||
await eventToPromise(repl, 'exit')
|
||||
|
||||
try {
|
||||
await xapi.disconnect()
|
||||
} catch (error) {}
|
||||
}
|
||||
export default main
|
||||
|
||||
if (!module.parent) {
|
||||
execPromise(main)
|
||||
}
|
||||
@@ -1,15 +1,22 @@
|
||||
import Bluebird, {promisify} from 'bluebird'
|
||||
import Collection from 'xo-collection'
|
||||
import createDebug from 'debug'
|
||||
import findKey from 'lodash.findkey'
|
||||
import forEach from 'lodash.foreach'
|
||||
import startsWith from 'lodash.startswith'
|
||||
import {BaseError} from 'make-error'
|
||||
import kindOf from 'kindof'
|
||||
import ms from 'ms'
|
||||
import httpRequest from 'http-request-plus'
|
||||
import { BaseError } from 'make-error'
|
||||
import { EventEmitter } from 'events'
|
||||
import { filter, forEach, isArray, isObject, map, noop, omit, reduce, startsWith } from 'lodash'
|
||||
import {
|
||||
createClient as createXmlRpcClient,
|
||||
createSecureClient as createSecureXmlRpcClient
|
||||
} from 'xmlrpc'
|
||||
import {EventEmitter} from 'events'
|
||||
Cancel,
|
||||
cancelable,
|
||||
catchPlus as pCatch,
|
||||
defer,
|
||||
delay as pDelay,
|
||||
fromEvent,
|
||||
lastly
|
||||
} from 'promise-toolbox'
|
||||
|
||||
import autoTransport from './transports/auto'
|
||||
|
||||
const debug = createDebug('xen-api')
|
||||
|
||||
@@ -36,7 +43,7 @@ const NETWORK_ERRORS = {
|
||||
ETIMEDOUT: true
|
||||
}
|
||||
|
||||
const isNetworkError = (error) => NETWORK_ERRORS[error.code]
|
||||
const isNetworkError = ({code}) => NETWORK_ERRORS[code]
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
@@ -45,152 +52,451 @@ const XAPI_NETWORK_ERRORS = {
|
||||
HOST_HAS_NO_MANAGEMENT_IP: true
|
||||
}
|
||||
|
||||
const isXapiNetworkError = (error) => XAPI_NETWORK_ERRORS[error.code]
|
||||
const isXapiNetworkError = ({code}) => XAPI_NETWORK_ERRORS[code]
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
const areEventsLost = (error) => error.code === 'EVENTS_LOST'
|
||||
const areEventsLost = ({code}) => code === 'EVENTS_LOST'
|
||||
|
||||
const isHostSlave = (error) => error.code === 'HOST_IS_SLAVE'
|
||||
const isHostSlave = ({code}) => code === 'HOST_IS_SLAVE'
|
||||
|
||||
const isSessionInvalid = (error) => error.code === 'SESSION_INVALID'
|
||||
const isMethodUnknown = ({code}) => code === 'MESSAGE_METHOD_UNKNOWN'
|
||||
|
||||
const isSessionInvalid = ({code}) => code === 'SESSION_INVALID'
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
class XapiError extends BaseError {
|
||||
constructor (error) {
|
||||
super(error[0])
|
||||
constructor ([code, ...params]) {
|
||||
super(`${code}(${params.join(', ')})`)
|
||||
|
||||
this.code = error[0]
|
||||
this.params = error.slice(1)
|
||||
this.code = code
|
||||
this.params = params
|
||||
|
||||
// slot than can be assigned later
|
||||
this.method = null
|
||||
}
|
||||
}
|
||||
|
||||
export const wrapError = error => new XapiError(error)
|
||||
|
||||
// ===================================================================
|
||||
|
||||
const URL_RE = /^(http(s)?:\/\/)?([^/]+?)(?::([0-9]+))?(?:\/.*)?$/
|
||||
function parseUrl (url) {
|
||||
const URL_RE = /^(?:(https?:)\/*)?(?:([^:]+):([^@]+)@)?([^/]+?)(?::([0-9]+))?\/?$/
|
||||
const parseUrl = url => {
|
||||
const matches = URL_RE.exec(url)
|
||||
if (!matches) {
|
||||
throw new Error('invalid URL: ' + url)
|
||||
}
|
||||
|
||||
const [, protocol, , host, port] = matches
|
||||
let [, , isSecure] = matches
|
||||
const [ , protocol = 'https:', username, password, hostname, port ] = matches
|
||||
return { protocol, username, password, hostname, port }
|
||||
}
|
||||
|
||||
if (!protocol) {
|
||||
isSecure = true
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
const {
|
||||
create: createObject,
|
||||
defineProperties,
|
||||
defineProperty,
|
||||
freeze: freezeObject
|
||||
} = Object
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
const OPAQUE_REF_PREFIX = 'OpaqueRef:'
|
||||
export const isOpaqueRef = value =>
|
||||
typeof value === 'string' &&
|
||||
startsWith(value, OPAQUE_REF_PREFIX)
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
const RE_READ_ONLY_METHOD = /^[^.]+\.get_/
|
||||
const isReadOnlyCall = (method, args) => (
|
||||
args.length === 1 &&
|
||||
isOpaqueRef(args[0]) &&
|
||||
RE_READ_ONLY_METHOD.test(method)
|
||||
)
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
const getKey = o => o.$id
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
const EMPTY_ARRAY = freezeObject([])
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
const TASK_RESULT_PREFIX_LEN = '<value>'.length
|
||||
const TASK_RESULT_SUFFIX_LEN = '</value>'.length
|
||||
|
||||
const getTaskResult = (task, onSuccess, onFailure) => {
|
||||
const { status } = task
|
||||
if (status === 'cancelled') {
|
||||
return [ onFailure(new Cancel('task canceled')) ]
|
||||
}
|
||||
|
||||
return {
|
||||
isSecure: Boolean(isSecure),
|
||||
host,
|
||||
port: port !== undefined ?
|
||||
+port :
|
||||
isSecure ? 443 : 80
|
||||
if (status === 'failure') {
|
||||
return [ onFailure(wrapError(task.error_info)) ]
|
||||
}
|
||||
if (status === 'success') {
|
||||
// a task result is either void or a ref, it should be fine to
|
||||
// remove the XML prefix/suffix
|
||||
return [ onSuccess(task.result.slice(
|
||||
TASK_RESULT_PREFIX_LEN,
|
||||
-TASK_RESULT_SUFFIX_LEN
|
||||
)) ]
|
||||
}
|
||||
}
|
||||
|
||||
const noop = () => {}
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
const notConnectedPromise = Bluebird.reject(new Error('not connected'))
|
||||
const MAX_TRIES = 5
|
||||
|
||||
// Does nothing but avoid a Bluebird message error.
|
||||
notConnectedPromise.catch(noop)
|
||||
const CONNECTED = 'connected'
|
||||
const CONNECTING = 'connecting'
|
||||
const DISCONNECTED = 'disconnected'
|
||||
|
||||
// ===================================================================
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
export class Xapi extends EventEmitter {
|
||||
constructor (opts) {
|
||||
super()
|
||||
|
||||
this._url = parseUrl(opts.url)
|
||||
this._allowUnauthorized = opts.allowUnauthorized
|
||||
this._auth = opts.auth
|
||||
this._pool = null
|
||||
this._readOnly = Boolean(opts.readOnly)
|
||||
this._sessionId = null
|
||||
const url = this._url = parseUrl(opts.url)
|
||||
|
||||
this._sessionId = notConnectedPromise
|
||||
if (this._auth === undefined) {
|
||||
const user = url.username
|
||||
if (user !== undefined) {
|
||||
this._auth = {
|
||||
user,
|
||||
password: url.password
|
||||
}
|
||||
delete url.username
|
||||
delete url.password
|
||||
}
|
||||
}
|
||||
|
||||
this._init()
|
||||
|
||||
this._poolId = null
|
||||
this._objects = new Collection()
|
||||
this._objects.getId = (object) => object.$id
|
||||
|
||||
this._fromToken = ''
|
||||
this.on('connected', this._watchEvents)
|
||||
this.on('disconnected', () => {
|
||||
if (opts.watchEvents !== false) {
|
||||
this._debounce = opts.debounce == null
|
||||
? 200
|
||||
: opts.debounce
|
||||
this._fromToken = ''
|
||||
this._objects.clear()
|
||||
|
||||
// Memoize this function _addObject().
|
||||
this._getPool = () => this._pool
|
||||
|
||||
const objects = this._objects = new Collection()
|
||||
objects.getKey = getKey
|
||||
|
||||
this._objectsByRefs = createObject(null)
|
||||
this._objectsByRefs['OpaqueRef:NULL'] = null
|
||||
|
||||
this._taskWatchers = Object.create(null)
|
||||
|
||||
this.on('connected', this._watchEvents)
|
||||
this.on('disconnected', () => {
|
||||
this._fromToken = ''
|
||||
objects.clear()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
get _url () {
|
||||
return this.__url
|
||||
}
|
||||
|
||||
set _url (url) {
|
||||
this.__url = url
|
||||
this._call = autoTransport({
|
||||
allowUnauthorized: this._allowUnauthorized,
|
||||
url
|
||||
})
|
||||
}
|
||||
|
||||
get readOnly () {
|
||||
return this._readOnly
|
||||
}
|
||||
|
||||
set readOnly (ro) {
|
||||
this._readOnly = Boolean(ro)
|
||||
}
|
||||
|
||||
get sessionId () {
|
||||
const id = this._sessionId
|
||||
|
||||
if (!id || id === CONNECTING) {
|
||||
throw new Error('sessionId is only available when connected')
|
||||
}
|
||||
|
||||
return id
|
||||
}
|
||||
|
||||
get status () {
|
||||
const {_sessionId: sessionId} = this
|
||||
const id = this._sessionId
|
||||
|
||||
if (sessionId.isFulfilled()) {
|
||||
return 'connected'
|
||||
}
|
||||
|
||||
if (sessionId.isPending()) {
|
||||
return 'connecting'
|
||||
}
|
||||
|
||||
return 'disconnected'
|
||||
return id
|
||||
? (
|
||||
id === CONNECTING
|
||||
? CONNECTING
|
||||
: CONNECTED
|
||||
)
|
||||
: DISCONNECTED
|
||||
}
|
||||
|
||||
get _humanId () {
|
||||
return `${this._auth.user}@${this._url.host}`
|
||||
return `${this._auth.user}@${this._url.hostname}`
|
||||
}
|
||||
|
||||
connect () {
|
||||
const {status} = this
|
||||
|
||||
if (status === 'connected') {
|
||||
return Bluebird.reject(new Error('already connected'))
|
||||
if (status === CONNECTED) {
|
||||
return Promise.reject(new Error('already connected'))
|
||||
}
|
||||
|
||||
if (status === 'connecting') {
|
||||
return Bluebird.reject(new Error('already connecting'))
|
||||
if (status === CONNECTING) {
|
||||
return Promise.reject(new Error('already connecting'))
|
||||
}
|
||||
|
||||
this._sessionId = this._transportCall('session.login_with_password', [
|
||||
this._sessionId = CONNECTING
|
||||
|
||||
return this._transportCall('session.login_with_password', [
|
||||
this._auth.user,
|
||||
this._auth.password
|
||||
])
|
||||
]).then(
|
||||
sessionId => {
|
||||
this._sessionId = sessionId
|
||||
|
||||
return this._sessionId.then(() => {
|
||||
debug('%s: connected', this._humanId)
|
||||
debug('%s: connected', this._humanId)
|
||||
|
||||
this.emit('connected')
|
||||
})
|
||||
this.emit(CONNECTED)
|
||||
},
|
||||
error => {
|
||||
this._sessionId = null
|
||||
|
||||
throw error
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
disconnect () {
|
||||
const {status} = this
|
||||
return Promise.resolve().then(() => {
|
||||
const { status } = this
|
||||
|
||||
if (status === 'disconnected') {
|
||||
return Bluebird.reject('already disconnected')
|
||||
}
|
||||
if (status === DISCONNECTED) {
|
||||
return Promise.reject(new Error('already disconnected'))
|
||||
}
|
||||
|
||||
if (status === 'connecting') {
|
||||
return this._sessionId.cancel().catch(Bluebird.CancellationError, () => {
|
||||
debug('%s: disconnected', this._humanId)
|
||||
this._sessionId = null
|
||||
|
||||
this.emit('disconnected')
|
||||
})
|
||||
}
|
||||
|
||||
this._sessionId = notConnectedPromise
|
||||
|
||||
return Bluebird.resolve().then(() => {
|
||||
debug('%s: disconnected', this._humanId)
|
||||
|
||||
this.emit('disconnected')
|
||||
this.emit(DISCONNECTED)
|
||||
})
|
||||
}
|
||||
|
||||
// High level calls.
|
||||
call (method, ...args) {
|
||||
return this._sessionCall(method, args)
|
||||
return this._readOnly && !isReadOnlyCall(method, args)
|
||||
? Promise.reject(new Error(`cannot call ${method}() in read only mode`))
|
||||
: this._sessionCall(method, args)
|
||||
}
|
||||
|
||||
@cancelable
|
||||
callAsync ($cancelToken, method, ...args) {
|
||||
return this.call(`Async.${method}`, ...args).then(taskRef => {
|
||||
$cancelToken.promise.then(() => {
|
||||
this._sessionCall('task.cancel', taskRef).catch(noop)
|
||||
})
|
||||
|
||||
return this.watchTask(taskRef)::lastly(() => {
|
||||
this._sessionCall('task.destroy', taskRef).catch(noop)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// Nice getter which returns the object for a given $id (internal to
|
||||
// this lib), UUID (unique identifier that some objects have) or
|
||||
// opaque reference (internal to XAPI).
|
||||
getObject (idOrUuidOrRef, defaultValue) {
|
||||
const object = typeof idOrUuidOrRef === 'string'
|
||||
? (
|
||||
// if there is an UUID, it is also the $id.
|
||||
this._objects.all[idOrUuidOrRef] ||
|
||||
this._objectsByRefs[idOrUuidOrRef]
|
||||
)
|
||||
: this._objects.all[idOrUuidOrRef.$id]
|
||||
|
||||
if (object) return object
|
||||
|
||||
if (arguments.length > 1) return defaultValue
|
||||
|
||||
throw new Error('there is not object can be matched to ' + idOrUuidOrRef)
|
||||
}
|
||||
|
||||
// Returns the object for a given opaque reference (internal to
|
||||
// XAPI).
|
||||
getObjectByRef (ref, defaultValue) {
|
||||
const object = this._objectsByRefs[ref]
|
||||
|
||||
if (object) return object
|
||||
|
||||
if (arguments.length > 1) return defaultValue
|
||||
|
||||
throw new Error('there is no object with the ref ' + ref)
|
||||
}
|
||||
|
||||
// Returns the object for a given UUID (unique identifier that some
|
||||
// objects have).
|
||||
getObjectByUuid (uuid, defaultValue) {
|
||||
// Objects ids are already UUIDs if they have one.
|
||||
const object = this._objects.all[uuid]
|
||||
|
||||
if (object) return object
|
||||
|
||||
if (arguments.length > 1) return defaultValue
|
||||
|
||||
throw new Error('there is no object with the UUID ' + uuid)
|
||||
}
|
||||
|
||||
@cancelable
|
||||
getResource ($cancelToken, pathname, { host, query }) {
|
||||
// TODO: should we create a task to properly cancel the request?
|
||||
return httpRequest(
|
||||
$cancelToken,
|
||||
this._url,
|
||||
host && {
|
||||
hostname: this.getObject(host).address
|
||||
},
|
||||
{
|
||||
pathname,
|
||||
query: {
|
||||
...query,
|
||||
session_id: this.sessionId
|
||||
},
|
||||
rejectUnauthorized: !this._allowUnauthorized
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@cancelable
|
||||
putResource ($cancelToken, body, pathname, {
|
||||
host,
|
||||
query
|
||||
} = {}) {
|
||||
// TODO: should we create a task to properly cancel the request?
|
||||
const headers = {}
|
||||
|
||||
// Xen API does not support chunk encoding.
|
||||
const isStream = typeof body.pipe === 'function'
|
||||
const { length } = body
|
||||
if (isStream && length === undefined) {
|
||||
// add a fake huge content length (1 PiB)
|
||||
headers['content-length'] = '1125899906842624'
|
||||
}
|
||||
|
||||
const doRequest = override => httpRequest.put(
|
||||
$cancelToken,
|
||||
this._url,
|
||||
host && {
|
||||
hostname: this.getObject(host).address
|
||||
},
|
||||
{
|
||||
body,
|
||||
headers,
|
||||
pathname,
|
||||
query: {
|
||||
...query,
|
||||
session_id: this.sessionId
|
||||
},
|
||||
rejectUnauthorized: !this._allowUnauthorized
|
||||
},
|
||||
override
|
||||
)
|
||||
|
||||
const promise = isStream
|
||||
|
||||
// dummy request to probe for a redirection before consuming body
|
||||
? doRequest({
|
||||
body: '',
|
||||
query: {
|
||||
// omit task_id because this request will fail on purpose
|
||||
...(
|
||||
query != null && 'task_id' in query
|
||||
? omit(query, 'task_id')
|
||||
: query
|
||||
),
|
||||
session_id: this.sessionId
|
||||
},
|
||||
maxRedirects: 0
|
||||
}).then(
|
||||
response => {
|
||||
response.req.abort()
|
||||
return doRequest()
|
||||
},
|
||||
error => {
|
||||
let response
|
||||
if (error != null && (response = error.response) != null) {
|
||||
response.req.abort()
|
||||
|
||||
const { headers: { location }, statusCode } = response
|
||||
if (statusCode === 302 && location !== undefined) {
|
||||
return doRequest(location)
|
||||
}
|
||||
}
|
||||
|
||||
throw error
|
||||
}
|
||||
)
|
||||
|
||||
// http-request-plus correctly handle redirects if body is not a stream
|
||||
: doRequest()
|
||||
|
||||
return promise.then(response => {
|
||||
// TODO: response.header['task-id']
|
||||
|
||||
const { req } = response
|
||||
|
||||
if (req.finished) {
|
||||
req.abort()
|
||||
return
|
||||
}
|
||||
|
||||
return fromEvent(req, 'finish').then(() => {
|
||||
req.abort()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
watchTask (ref) {
|
||||
const watchers = this._taskWatchers
|
||||
if (watchers === undefined) {
|
||||
throw new Error('Xapi#watchTask() requires events watching')
|
||||
}
|
||||
|
||||
// allow task object to be passed
|
||||
if (ref.$ref !== undefined) ref = ref.$ref
|
||||
|
||||
let watcher = watchers[ref]
|
||||
if (watcher === undefined) {
|
||||
// sync check if the task is already settled
|
||||
const task = this.objects.all[ref]
|
||||
if (task !== undefined) {
|
||||
const result = getTaskResult(task, Promise.resolve, Promise.reject)
|
||||
if (result) {
|
||||
return result[0]
|
||||
}
|
||||
}
|
||||
|
||||
watcher = watchers[ref] = defer()
|
||||
}
|
||||
return watcher.promise
|
||||
}
|
||||
|
||||
get pool () {
|
||||
@@ -203,135 +509,306 @@ export class Xapi extends EventEmitter {
|
||||
|
||||
// Medium level call: handle session errors.
|
||||
_sessionCall (method, args) {
|
||||
if (startsWith(method, 'session.')) {
|
||||
return Bluebird.reject(
|
||||
new Error('session.*() methods are disabled from this interface')
|
||||
)
|
||||
try {
|
||||
if (startsWith(method, 'session.')) {
|
||||
throw new Error('session.*() methods are disabled from this interface')
|
||||
}
|
||||
|
||||
return this._transportCall(method, [this.sessionId].concat(args))
|
||||
::pCatch(isSessionInvalid, () => {
|
||||
// XAPI is sometimes reinitialized and sessions are lost.
|
||||
// Try to login again.
|
||||
debug('%s: the session has been reinitialized', this._humanId)
|
||||
|
||||
this._sessionId = null
|
||||
return this.connect().then(() => this._sessionCall(method, args))
|
||||
})
|
||||
} catch (error) {
|
||||
return Promise.reject(error)
|
||||
}
|
||||
}
|
||||
|
||||
_addObject (type, ref, object) {
|
||||
const {_objectsByRefs: objectsByRefs} = this
|
||||
|
||||
const reservedKeys = {
|
||||
id: true,
|
||||
pool: true,
|
||||
ref: true,
|
||||
type: true
|
||||
}
|
||||
const getKey = (key, obj) => reservedKeys[key] && obj === object
|
||||
? `$$${key}`
|
||||
: `$${key}`
|
||||
|
||||
// Creates resolved properties.
|
||||
forEach(object, function resolveObject (value, key, object) {
|
||||
if (isArray(value)) {
|
||||
if (!value.length) {
|
||||
// If the array is empty, it isn't possible to be sure that
|
||||
// it is not supposed to contain links, therefore, in
|
||||
// benefice of the doubt, a resolved property is defined.
|
||||
defineProperty(object, getKey(key, object), {
|
||||
value: EMPTY_ARRAY
|
||||
})
|
||||
|
||||
// Minor memory optimization, use the same empty array for
|
||||
// everyone.
|
||||
object[key] = EMPTY_ARRAY
|
||||
} else if (isOpaqueRef(value[0])) {
|
||||
// This is an array of refs.
|
||||
defineProperty(object, getKey(key, object), {
|
||||
get: () => freezeObject(map(value, (ref) => objectsByRefs[ref]))
|
||||
})
|
||||
|
||||
freezeObject(value)
|
||||
}
|
||||
} else if (isObject(value)) {
|
||||
forEach(value, resolveObject)
|
||||
|
||||
freezeObject(value)
|
||||
} else if (isOpaqueRef(value)) {
|
||||
defineProperty(object, getKey(key, object), {
|
||||
get: () => objectsByRefs[value]
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
// All custom properties are read-only and non enumerable.
|
||||
defineProperties(object, {
|
||||
$id: { value: object.uuid || ref },
|
||||
$pool: { get: this._getPool },
|
||||
$ref: { value: ref },
|
||||
$type: { value: type }
|
||||
})
|
||||
|
||||
// Finally freezes the object.
|
||||
freezeObject(object)
|
||||
|
||||
const objects = this._objects
|
||||
|
||||
// An object's UUID can change during its life.
|
||||
const prev = objectsByRefs[ref]
|
||||
let prevUuid
|
||||
if (prev && (prevUuid = prev.uuid) && prevUuid !== object.uuid) {
|
||||
objects.remove(prevUuid)
|
||||
}
|
||||
|
||||
return this._sessionId.then((sessionId) => {
|
||||
return this._transportCall(method, [sessionId].concat(args))
|
||||
}).catch(isSessionInvalid, () => {
|
||||
// XAPI is sometimes reinitialized and sessions are lost.
|
||||
// Try to login again.
|
||||
debug('%s: the session has been reinitialized', this._humanId)
|
||||
this._objects.set(object)
|
||||
objectsByRefs[ref] = object
|
||||
|
||||
this._sessionId = null
|
||||
|
||||
return this._sessionCall(method, args)
|
||||
})
|
||||
if (type === 'pool') {
|
||||
this._pool = object
|
||||
} else if (type === 'task') {
|
||||
const taskWatchers = this._taskWatchers
|
||||
let taskWatcher = taskWatchers[ref]
|
||||
if (
|
||||
taskWatcher !== undefined &&
|
||||
getTaskResult(object, taskWatcher.resolve, taskWatcher.reject)
|
||||
) {
|
||||
delete taskWatchers[ref]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Low level call: handle transport errors.
|
||||
_transportCall (method, args) {
|
||||
debug('%s: %s(...)', this._humanId, method)
|
||||
_removeObject (ref) {
|
||||
const {_objectsByRefs: objectsByRefs} = this
|
||||
|
||||
return this._xmlRpcCall(method, args)
|
||||
.then(result => {
|
||||
const {Status: status} = result
|
||||
const object = objectsByRefs[ref]
|
||||
|
||||
// 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(isHostSlave, ({params: [master]}) => {
|
||||
debug('%s: host is slave, attempting to connect at %s', this._humanId, master)
|
||||
|
||||
this._url.host = master
|
||||
this._init()
|
||||
|
||||
return this._transportCall(method, args)
|
||||
})
|
||||
.catch(isNetworkError, isXapiNetworkError, () => {
|
||||
debug('%s: a network error happened', this._humanId)
|
||||
|
||||
// TODO: ability to cancel the connection
|
||||
// TODO: ability to force immediate reconnection
|
||||
// TODO: implement back-off
|
||||
|
||||
return Bluebird.delay(5e3).then(() => {
|
||||
// TODO: handling not responding host.
|
||||
|
||||
return this._transportCall(method, args)
|
||||
})
|
||||
})
|
||||
.cancellable()
|
||||
if (object) {
|
||||
this._objects.unset(object.$id)
|
||||
delete objectsByRefs[ref]
|
||||
}
|
||||
}
|
||||
|
||||
_init () {
|
||||
const {isSecure, host, port} = this._url
|
||||
_processEvents (events) {
|
||||
forEach(events, event => {
|
||||
const {operation: op} = event
|
||||
|
||||
const client = (isSecure ?
|
||||
createSecureXmlRpcClient :
|
||||
createXmlRpcClient
|
||||
)({
|
||||
host,
|
||||
port,
|
||||
rejectUnauthorized: false,
|
||||
timeout: 10
|
||||
})
|
||||
|
||||
this._xmlRpcCall = promisify(client.methodCall, client)
|
||||
}
|
||||
|
||||
_normalizeObject (type, ref, object) {
|
||||
object.$id = object.uuid || ref
|
||||
object.$ref = ref
|
||||
object.$type = type
|
||||
|
||||
Object.defineProperty(object, '$pool', {
|
||||
// enumerable: true,
|
||||
get: () => this._pool
|
||||
const {ref} = event
|
||||
if (op === 'del') {
|
||||
this._removeObject(ref)
|
||||
} else {
|
||||
this._addObject(event.class, ref, event.snapshot)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
_watchEvents () {
|
||||
this.call('event.from', [
|
||||
['*'], this._fromToken, 1e3 + 0.1
|
||||
]).then(({token, events}) => {
|
||||
const loop = () => this.status === CONNECTED && this._sessionCall('event.from', [
|
||||
['*'],
|
||||
this._fromToken,
|
||||
60 + 0.1 // Force float.
|
||||
]).then(onSuccess, onFailure)
|
||||
|
||||
const onSuccess = ({token, events}) => {
|
||||
this._fromToken = token
|
||||
this._processEvents(events)
|
||||
|
||||
const {_objects: objects} = this
|
||||
const debounce = this._debounce
|
||||
return debounce != null
|
||||
? pDelay(debounce).then(loop)
|
||||
: loop()
|
||||
}
|
||||
const onFailure = error => {
|
||||
if (areEventsLost(error)) {
|
||||
this._fromToken = ''
|
||||
this._objects.clear()
|
||||
|
||||
forEach(events, event => {
|
||||
const {operation: op} = event
|
||||
return loop()
|
||||
}
|
||||
|
||||
const {ref} = event
|
||||
if (op === 'del') {
|
||||
// TODO: This should probably be speed up with an index.
|
||||
const key = findKey(objects.all, {$ref: ref})
|
||||
throw error
|
||||
}
|
||||
|
||||
if (key !== undefined) {
|
||||
objects.remove(key)
|
||||
}
|
||||
} else {
|
||||
const {class: type, snapshot: object} = event
|
||||
return loop()::pCatch(
|
||||
isMethodUnknown,
|
||||
|
||||
this._normalizeObject(type, ref, object)
|
||||
objects.set(object)
|
||||
// If the server failed, it is probably due to an excessively
|
||||
// large response.
|
||||
// Falling back to legacy events watch should be enough.
|
||||
error => error && error.res && error.res.statusCode === 500,
|
||||
|
||||
if (object.$type === 'pool') {
|
||||
this._pool = object
|
||||
}
|
||||
}
|
||||
() => this._watchEventsLegacy()
|
||||
)
|
||||
}
|
||||
|
||||
// This method watches events using the legacy `event.next` XAPI
|
||||
// methods.
|
||||
//
|
||||
// It also has to manually get all objects first.
|
||||
_watchEventsLegacy () {
|
||||
const getAllObjects = () => {
|
||||
return this._sessionCall('system.listMethods', []).then(methods => {
|
||||
// Uses introspection to determine the methods to use to get
|
||||
// all objects.
|
||||
const getAllRecordsMethods = filter(
|
||||
methods,
|
||||
::/\.get_all_records$/.test
|
||||
)
|
||||
|
||||
return Promise.all(map(
|
||||
getAllRecordsMethods,
|
||||
method => this._sessionCall(method, []).then(
|
||||
objects => {
|
||||
const type = method.slice(0, method.indexOf('.')).toLowerCase()
|
||||
forEach(objects, (object, ref) => {
|
||||
this._addObject(type, ref, object)
|
||||
})
|
||||
},
|
||||
error => {
|
||||
if (error.code !== 'MESSAGE_REMOVED') {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
)
|
||||
))
|
||||
})
|
||||
}).catch(areEventsLost, () => {
|
||||
this._objects.clear()
|
||||
}).then(() => {
|
||||
this._watchEvents()
|
||||
}).catch(noop)
|
||||
}
|
||||
|
||||
const watchEvents = () => this._sessionCall('event.register', [ ['*'] ]).then(loop)
|
||||
|
||||
const loop = () => this.status === CONNECTED && this._sessionCall('event.next', []).then(onSuccess, onFailure)
|
||||
|
||||
const onSuccess = events => {
|
||||
this._processEvents(events)
|
||||
|
||||
const debounce = this._debounce
|
||||
return debounce == null
|
||||
? loop()
|
||||
: pDelay(debounce).then(loop)
|
||||
}
|
||||
|
||||
const onFailure = error => {
|
||||
if (areEventsLost(error)) {
|
||||
return this._sessionCall('event.unregister', [ ['*'] ]).then(watchEvents)
|
||||
}
|
||||
|
||||
throw error
|
||||
}
|
||||
|
||||
return getAllObjects().then(watchEvents)
|
||||
}
|
||||
}
|
||||
|
||||
Xapi.prototype._transportCall = reduce([
|
||||
function (method, args) {
|
||||
return this._call(method, args).catch(error => {
|
||||
if (isArray(error)) {
|
||||
error = wrapError(error)
|
||||
}
|
||||
|
||||
error.method = method
|
||||
throw error
|
||||
})
|
||||
},
|
||||
call => function () {
|
||||
let tries = 1
|
||||
const loop = () => call.apply(this, arguments)
|
||||
::pCatch(isNetworkError, isXapiNetworkError, error => {
|
||||
debug('%s: network error %s', this._humanId, error.code)
|
||||
|
||||
if (++tries < MAX_TRIES) {
|
||||
// TODO: ability to cancel the connection
|
||||
// TODO: ability to force immediate reconnection
|
||||
// TODO: implement back-off
|
||||
|
||||
return pDelay(5e3).then(loop)
|
||||
}
|
||||
|
||||
debug('%s too many network errors (%s), give up', this._humanId, tries)
|
||||
|
||||
// mark as disconnected
|
||||
this.disconnect()::pCatch(noop)
|
||||
|
||||
throw error
|
||||
})
|
||||
return loop()
|
||||
},
|
||||
call => function loop () {
|
||||
return call.apply(this, arguments)
|
||||
::pCatch(isHostSlave, ({params: [master]}) => {
|
||||
debug('%s: host is slave, attempting to connect at %s', this._humanId, master)
|
||||
|
||||
const newUrl = {
|
||||
...this._url,
|
||||
hostname: master
|
||||
}
|
||||
this.emit('redirect', newUrl)
|
||||
this._url = newUrl
|
||||
|
||||
return loop.apply(this, arguments)
|
||||
})
|
||||
},
|
||||
call => function (method) {
|
||||
const startTime = Date.now()
|
||||
return call.apply(this, arguments).then(
|
||||
result => {
|
||||
debug(
|
||||
'%s: %s(...) [%s] ==> %s',
|
||||
this._humanId,
|
||||
method,
|
||||
ms(Date.now() - startTime),
|
||||
kindOf(result)
|
||||
)
|
||||
return result
|
||||
},
|
||||
error => {
|
||||
debug(
|
||||
'%s: %s(...) [%s] =!> %s',
|
||||
this._humanId,
|
||||
method,
|
||||
ms(Date.now() - startTime),
|
||||
error
|
||||
)
|
||||
throw error
|
||||
}
|
||||
)
|
||||
}
|
||||
], (call, decorator) => decorator(call))
|
||||
|
||||
// ===================================================================
|
||||
|
||||
// The default value is a factory function.
|
||||
export const createClient = (opts) => new Xapi(opts)
|
||||
export const createClient = opts => new Xapi(opts)
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
/* 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.skip('returns an instance of Xapi', function () {
|
||||
expect(createClient('example.org')).to.be.a(Xapi)
|
||||
})
|
||||
})
|
||||
29
packages/xen-api/src/inject-events.js
Executable file
29
packages/xen-api/src/inject-events.js
Executable file
@@ -0,0 +1,29 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { delay as pDelay } from 'promise-toolbox'
|
||||
|
||||
import { createClient } from './'
|
||||
|
||||
const xapi = (() => {
|
||||
const [ , , url, user, password ] = process.argv
|
||||
|
||||
return createClient({
|
||||
auth: { user, password },
|
||||
url,
|
||||
watchEvents: false
|
||||
})
|
||||
})()
|
||||
|
||||
xapi.connect()
|
||||
|
||||
// Get the pool record's ref.
|
||||
.then(() => xapi.call('pool.get_all'))
|
||||
|
||||
// Injects lots of events.
|
||||
.then(([ poolRef ]) => {
|
||||
const loop = () => xapi.call('event.inject', 'pool', poolRef)
|
||||
::pDelay(10) // A small delay is required to avoid overloading the Xen API.
|
||||
.then(loop)
|
||||
|
||||
return loop()
|
||||
})
|
||||
22
packages/xen-api/src/memory-test.js
Executable file
22
packages/xen-api/src/memory-test.js
Executable file
@@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import { createClient } from './'
|
||||
|
||||
let i = 0
|
||||
setInterval(() => {
|
||||
const usage = process.memoryUsage()
|
||||
console.log(
|
||||
'%s %s %s %s',
|
||||
i++,
|
||||
Math.round(usage.rss / 1e6),
|
||||
Math.round(usage.heapTotal / 1e6),
|
||||
Math.round(usage.heapUsed / 1e6)
|
||||
)
|
||||
}, 1e2)
|
||||
|
||||
const [ , , url, user, password ] = process.argv
|
||||
createClient({
|
||||
auth: { user, password },
|
||||
readOnly: true,
|
||||
url
|
||||
}).connect()
|
||||
3
packages/xen-api/src/transports/_utils.js
Normal file
3
packages/xen-api/src/transports/_utils.js
Normal file
@@ -0,0 +1,3 @@
|
||||
import makeError from 'make-error'
|
||||
|
||||
export const UnsupportedTransport = makeError('UnsupportedTransport')
|
||||
36
packages/xen-api/src/transports/auto.js
Normal file
36
packages/xen-api/src/transports/auto.js
Normal file
@@ -0,0 +1,36 @@
|
||||
import jsonRpc from './json-rpc'
|
||||
import xmlRpc from './xml-rpc'
|
||||
import xmlRpcJson from './xml-rpc-json'
|
||||
import { UnsupportedTransport } from './_utils'
|
||||
|
||||
const factories = [ jsonRpc, xmlRpcJson, xmlRpc ]
|
||||
const { length } = factories
|
||||
|
||||
export default opts => {
|
||||
let i = 0
|
||||
|
||||
let call
|
||||
function create () {
|
||||
const current = factories[i++](opts)
|
||||
if (i < length) {
|
||||
const currentI = i
|
||||
call = (method, args) => current(method, args).catch(
|
||||
error => {
|
||||
if (error instanceof UnsupportedTransport) {
|
||||
if (currentI === i) { // not changed yet
|
||||
create()
|
||||
}
|
||||
return call(method, args)
|
||||
}
|
||||
|
||||
throw error
|
||||
}
|
||||
)
|
||||
} else {
|
||||
call = current
|
||||
}
|
||||
}
|
||||
create()
|
||||
|
||||
return (method, args) => call(method, args)
|
||||
}
|
||||
38
packages/xen-api/src/transports/json-rpc.js
Normal file
38
packages/xen-api/src/transports/json-rpc.js
Normal file
@@ -0,0 +1,38 @@
|
||||
import httpRequestPlus from 'http-request-plus'
|
||||
import { format, parse } from 'json-rpc-protocol'
|
||||
|
||||
import { UnsupportedTransport } from './_utils'
|
||||
|
||||
export default ({ allowUnauthorized, url }) => {
|
||||
return (method, args) => httpRequestPlus.post(url, {
|
||||
rejectUnauthorized: !allowUnauthorized,
|
||||
body: format.request(0, method, args),
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
path: '/jsonrpc'
|
||||
}).readAll('utf8').then(
|
||||
text => {
|
||||
let response
|
||||
try {
|
||||
response = parse(text)
|
||||
} catch (error) {
|
||||
throw new UnsupportedTransport()
|
||||
}
|
||||
|
||||
if (response.type === 'response') {
|
||||
return response.result
|
||||
}
|
||||
|
||||
throw response.error
|
||||
},
|
||||
error => {
|
||||
if (error.response !== undefined) { // HTTP error
|
||||
throw new UnsupportedTransport()
|
||||
}
|
||||
|
||||
throw error
|
||||
}
|
||||
)
|
||||
}
|
||||
97
packages/xen-api/src/transports/xml-rpc-json.js
Normal file
97
packages/xen-api/src/transports/xml-rpc-json.js
Normal file
@@ -0,0 +1,97 @@
|
||||
import { createClient, createSecureClient } from 'xmlrpc'
|
||||
import { promisify } from 'promise-toolbox'
|
||||
|
||||
import { UnsupportedTransport } from './_utils'
|
||||
|
||||
const logError = error => {
|
||||
if (error.res) {
|
||||
console.error(
|
||||
'XML-RPC Error: %s (response status %s)',
|
||||
error.message,
|
||||
error.res.statusCode
|
||||
)
|
||||
console.error('%s', error.body)
|
||||
}
|
||||
|
||||
throw error
|
||||
}
|
||||
|
||||
const SPECIAL_CHARS = {
|
||||
'\r': '\\r',
|
||||
'\t': '\\t'
|
||||
}
|
||||
const SPECIAL_CHARS_RE = new RegExp(
|
||||
Object.keys(SPECIAL_CHARS).join('|'),
|
||||
'g'
|
||||
)
|
||||
|
||||
const parseResult = result => {
|
||||
const status = result.Status
|
||||
|
||||
// Return the plain result if it does not have a valid XAPI
|
||||
// format.
|
||||
if (status === undefined) {
|
||||
return result
|
||||
}
|
||||
|
||||
if (status !== 'Success') {
|
||||
throw result.ErrorDescription
|
||||
}
|
||||
|
||||
const value = result.Value
|
||||
|
||||
// XAPI returns an empty string (invalid JSON) for an empty
|
||||
// result.
|
||||
if (value === '') {
|
||||
return ''
|
||||
}
|
||||
|
||||
try {
|
||||
return JSON.parse(value)
|
||||
} catch (error) {
|
||||
// XAPI JSON sometimes contains invalid characters.
|
||||
if (!(error instanceof SyntaxError)) {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
let replaced = false
|
||||
const fixedValue = value.replace(SPECIAL_CHARS_RE, match => {
|
||||
replaced = true
|
||||
return SPECIAL_CHARS[match]
|
||||
})
|
||||
|
||||
if (replaced) {
|
||||
try {
|
||||
return JSON.parse(fixedValue)
|
||||
} catch (error) {
|
||||
if (!(error instanceof SyntaxError)) {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw new UnsupportedTransport()
|
||||
}
|
||||
|
||||
export default ({
|
||||
allowUnauthorized,
|
||||
url: { hostname, path, port, protocol }
|
||||
}) => {
|
||||
const client = (
|
||||
protocol === 'https:'
|
||||
? createSecureClient
|
||||
: createClient
|
||||
)({
|
||||
host: hostname,
|
||||
path: '/json',
|
||||
port,
|
||||
rejectUnauthorized: !allowUnauthorized
|
||||
})
|
||||
const call = promisify(client.methodCall, client)
|
||||
|
||||
return (method, args) => call(method, args).then(
|
||||
parseResult,
|
||||
logError
|
||||
)
|
||||
}
|
||||
52
packages/xen-api/src/transports/xml-rpc.js
Normal file
52
packages/xen-api/src/transports/xml-rpc.js
Normal file
@@ -0,0 +1,52 @@
|
||||
import { createClient, createSecureClient } from 'xmlrpc'
|
||||
import { promisify } from 'promise-toolbox'
|
||||
|
||||
const logError = error => {
|
||||
if (error.res) {
|
||||
console.error(
|
||||
'XML-RPC Error: %s (response status %s)',
|
||||
error.message,
|
||||
error.res.statusCode
|
||||
)
|
||||
console.error('%s', error.body)
|
||||
}
|
||||
|
||||
throw error
|
||||
}
|
||||
|
||||
const parseResult = result => {
|
||||
const status = result.Status
|
||||
|
||||
// Return the plain result if it does not have a valid XAPI
|
||||
// format.
|
||||
if (status === undefined) {
|
||||
return result
|
||||
}
|
||||
|
||||
if (status !== 'Success') {
|
||||
throw result.ErrorDescription
|
||||
}
|
||||
|
||||
return result.Value
|
||||
}
|
||||
|
||||
export default ({
|
||||
allowUnauthorized,
|
||||
url: { hostname, path, port, protocol }
|
||||
}) => {
|
||||
const client = (
|
||||
protocol === 'https:'
|
||||
? createSecureClient
|
||||
: createClient
|
||||
)({
|
||||
host: hostname,
|
||||
port,
|
||||
rejectUnauthorized: !allowUnauthorized
|
||||
})
|
||||
const call = promisify(client.methodCall, client)
|
||||
|
||||
return (method, args) => call(method, args).then(
|
||||
parseResult,
|
||||
logError
|
||||
)
|
||||
}
|
||||
3790
packages/xen-api/yarn.lock
Normal file
3790
packages/xen-api/yarn.lock
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user