Various updates.
This commit is contained in:
parent
426dbf4fff
commit
10edf58bf9
|
@ -20,7 +20,7 @@
|
|||
"q": ">=0.9.6",
|
||||
"sync": ">=0.2.2",
|
||||
"then-redis": ">=0.3.8",
|
||||
"underscore": ">=1.4.4",
|
||||
"underscore": ">=1.5.2",
|
||||
"validator": ">=1.2.1",
|
||||
"ws": ">=0.4.27",
|
||||
"xmlrpc": ">=1.1.0"
|
||||
|
|
35
src/api.js
35
src/api.js
|
@ -535,14 +535,21 @@ Api.fn.xo = {
|
|||
// (and possibly heavy) computing.
|
||||
|
||||
var xo = this.xo;
|
||||
var xobjs = xo.xobjs;
|
||||
return Q.all([
|
||||
xo.hosts.get(),
|
||||
xo.vms.get({
|
||||
xobjs.host.get(),
|
||||
xobjs.host_metrics.get().then(function (metrics) {
|
||||
return _.indexBy(metrics, 'id');
|
||||
}),
|
||||
xobjs.VM.get({
|
||||
'is_a_template': false,
|
||||
'is_control_domain': false,
|
||||
}),
|
||||
xo.srs.count(),
|
||||
]).spread(function (hosts, vms, n_srs) {
|
||||
xobjs.VM_metrics.get().then(function (metrics) {
|
||||
return _.indexBy(metrics, 'id');
|
||||
}),
|
||||
xobjs.SR.count(),
|
||||
]).spread(function (hosts, host_metrics, vms, vms_metrics, n_srs) {
|
||||
var running_vms = _.where(vms, {
|
||||
'power_state': 'Running',
|
||||
});
|
||||
|
@ -551,16 +558,18 @@ Api.fn.xo = {
|
|||
var total_memory = 0;
|
||||
_.each(hosts, function (host) {
|
||||
n_cpus += host.host_CPUs.length;
|
||||
total_memory += +host.metrics.memory_total;
|
||||
total_memory += +host_metrics[host.metrics].memory_total;
|
||||
});
|
||||
|
||||
var n_vifs = 0;
|
||||
var n_vcpus = 0;
|
||||
var used_memory = 0;
|
||||
_.each(vms, function (vm) {
|
||||
var metrics = vms_metrics[vm.metrics];
|
||||
|
||||
n_vifs += vm.VIFs.length;
|
||||
n_vcpus += +vm.metrics.VCPUs_number;
|
||||
used_memory += +vm.metrics.memory_actual;
|
||||
n_vcpus += +metrics.VCPUs_number;
|
||||
used_memory += +metrics.memory_actual;
|
||||
});
|
||||
|
||||
return {
|
||||
|
@ -584,7 +593,7 @@ Api.fn.xo = {
|
|||
throw Api.err.INVALID_PARAMS;
|
||||
}
|
||||
|
||||
return this.xo.pools.first(p_pool_id).then(function (pool) {
|
||||
return this.xobjs.pool.first(p_pool_id).then(function (pool) {
|
||||
return pool.get('sessionId');
|
||||
});
|
||||
},
|
||||
|
@ -599,7 +608,10 @@ Api.fn.xapi = {
|
|||
throw Api.err.INVALID_METHOD;
|
||||
}
|
||||
|
||||
return this.xo[match[1] +'s'].get();
|
||||
var xobjs = this.xo.xobjs;
|
||||
var collection = xobjs[match[1]] || xobjs[match[1].toUpperCase()];
|
||||
|
||||
return collection.get();
|
||||
},
|
||||
|
||||
'vm': {
|
||||
|
@ -611,9 +623,10 @@ Api.fn.xapi = {
|
|||
}
|
||||
|
||||
var xo = this.xo;
|
||||
var xobjs = xo.xobjs;
|
||||
var vm;
|
||||
return this.checkPermission(session, 'write').then(function () {
|
||||
return xo.vms.first(p_id);
|
||||
return xobjs.VM.first(p_id);
|
||||
}).then(function (tmp) {
|
||||
vm = tmp;
|
||||
|
||||
|
@ -622,7 +635,7 @@ Api.fn.xapi = {
|
|||
throw Api.err.NO_SUCH_OBJECT;
|
||||
}
|
||||
|
||||
return xo.pools.first(vm.get('pool_uuid'));
|
||||
return xobjs.pool.first(vm.get('pool_uuid'));
|
||||
}).then(function (pool) {
|
||||
var xapi = xo.connections[pool.get('uuid')];
|
||||
|
||||
|
|
58
src/xapi.js
58
src/xapi.js
|
@ -5,29 +5,31 @@ Q.longStackSupport = true;
|
|||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
|
||||
function Xapi(host)
|
||||
function Xapi(host, username, password)
|
||||
{
|
||||
// Parent constructor.
|
||||
Xapi.super_.call(this);
|
||||
|
||||
this.xmlrpc = xmlrpc.createSecureClient({
|
||||
hostname: host,
|
||||
port: '443',
|
||||
rejectUnauthorized: false,
|
||||
}); // @todo Handle connection success/error.
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
|
||||
this.changeHost(host);
|
||||
}
|
||||
require('util').inherits(Xapi, require('events').EventEmitter);
|
||||
|
||||
Xapi.prototype.call = function (method) {
|
||||
var args = arguments;
|
||||
var params = Array.prototype.slice.call(arguments, 1);
|
||||
|
||||
if (this.sessionId)
|
||||
var self = this;
|
||||
return Q(self.sessionId).then(function (session_id) {
|
||||
if (session_id)
|
||||
{
|
||||
params.unshift(this.sessionId);
|
||||
params.unshift(session_id);
|
||||
}
|
||||
|
||||
return Q.ninvoke(this.xmlrpc, 'methodCall', method, params)
|
||||
.then(function (value) {
|
||||
return Q.ninvoke(self.xmlrpc, 'methodCall', method, params);
|
||||
}).then(function (value) {
|
||||
if ('Success' !== value.Status)
|
||||
{
|
||||
if ('Failure' === value.Status)
|
||||
|
@ -39,17 +41,43 @@ Xapi.prototype.call = function (method) {
|
|||
}
|
||||
|
||||
return value.Value;
|
||||
}).fail(function (error) {
|
||||
if ('HOST_IS_SLAVE' !== error[0])
|
||||
{
|
||||
throw error;
|
||||
}
|
||||
|
||||
self.changeHost(error[1]);
|
||||
return self.call.apply(self, args);
|
||||
});
|
||||
};
|
||||
|
||||
Xapi.prototype.connect = function (username, password) {
|
||||
var self = this;
|
||||
Xapi.prototype.changeHost = function (host) {
|
||||
if (this.host === host)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
return this.call('session.login_with_password', username, password)
|
||||
.then(function (session_id) {
|
||||
console.log(self.xmlrpc.options, session_id);
|
||||
this.host = host;
|
||||
this.xmlrpc = xmlrpc.createSecureClient({
|
||||
hostname: host,
|
||||
port: '443',
|
||||
rejectUnauthorized: false,
|
||||
}); // @todo Handle connection success/error.
|
||||
|
||||
var self = this;
|
||||
self.sessionId = undefined;
|
||||
self.sessionId = self.call(
|
||||
'session.login_with_password',
|
||||
self.username,
|
||||
self.password
|
||||
).then(function (session_id) {
|
||||
self.sessionId = session_id;
|
||||
|
||||
return session_id;
|
||||
});
|
||||
|
||||
return self.sessionId;
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
|
|
28
src/xo.js
28
src/xo.js
|
@ -230,35 +230,27 @@ Xo.prototype.start = function (cfg) {
|
|||
// When a server is added we should connect to it and fetch data.
|
||||
var connect = function (server) {
|
||||
var pool_id = server.id;
|
||||
var xapi = new Xapi(server.host);
|
||||
var xapi = new Xapi(server.host, server.username, server.password);
|
||||
|
||||
xapi.connect(server.username, server.password).then(function () {
|
||||
var xclasses = xo.xclasses;
|
||||
var xobjs = xo.xobjs;
|
||||
|
||||
return _.map(xclasses, function (xclass) {
|
||||
var collection = xo.xobjs[xclass];
|
||||
var helper = function () {
|
||||
return Q.all(_.map(xclasses, function (xclass) {
|
||||
var collection = xobjs[xclass];
|
||||
|
||||
return xapi.call(xclass +'.get_all_records').then(function (records) {
|
||||
_.each(records, function (record) {
|
||||
records = _.map(records, function (record, ref) {
|
||||
record.id = ref;
|
||||
record.pool = pool_id;
|
||||
return record;
|
||||
});
|
||||
|
||||
return collection.add(records, {
|
||||
'update': true,
|
||||
'replace': true,
|
||||
});
|
||||
}).fail(function (error) {
|
||||
if ('HOST_IS_SLAVE' === error[0])
|
||||
{
|
||||
server.host = error[1];
|
||||
}
|
||||
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
});
|
||||
}).fail(function (error) {
|
||||
console.log(error);
|
||||
})).fail(function (error) {
|
||||
console.error(error);
|
||||
});
|
||||
};
|
||||
// Connect existing servers.
|
||||
|
|
Loading…
Reference in New Issue