root / ui / static / snf / js / models.js @ 16392650
History | View | Annotate | Download (51.9 kB)
1 |
;(function(root){
|
---|---|
2 |
|
3 |
// root
|
4 |
var root = root;
|
5 |
|
6 |
// setup namepsaces
|
7 |
var snf = root.synnefo = root.synnefo || {};
|
8 |
var models = snf.models = snf.models || {}
|
9 |
var storage = snf.storage = snf.storage || {};
|
10 |
var util = snf.util = snf.util || {};
|
11 |
|
12 |
// shortcuts
|
13 |
var bb = root.Backbone;
|
14 |
var slice = Array.prototype.slice
|
15 |
|
16 |
// logging
|
17 |
var logger = new snf.logging.logger("SNF-MODELS"); |
18 |
var debug = _.bind(logger.debug, logger);
|
19 |
|
20 |
// get url helper
|
21 |
var getUrl = function() { |
22 |
return snf.config.api_url + "/" + this.path; |
23 |
} |
24 |
|
25 |
// i18n
|
26 |
BUILDING_MESSAGES = window.BUILDING_MESSAGES || {'INIT': 'init', 'COPY': '{0}, {1}, {2}', 'FINAL': 'final'}; |
27 |
|
28 |
// Base object for all our models
|
29 |
models.Model = bb.Model.extend({ |
30 |
sync: snf.api.sync,
|
31 |
api: snf.api,
|
32 |
has_status: false, |
33 |
|
34 |
initialize: function() { |
35 |
if (this.has_status) { |
36 |
this.bind("change:status", this.handle_remove); |
37 |
this.handle_remove();
|
38 |
} |
39 |
models.Model.__super__.initialize.apply(this, arguments) |
40 |
}, |
41 |
|
42 |
handle_remove: function() { |
43 |
if (this.get("status") == 'DELETED') { |
44 |
if (this.collection) { |
45 |
try { this.clear_pending_action();} catch (err) {}; |
46 |
try { this.reset_pending_actions();} catch (err) {}; |
47 |
try { this.stop_stats_update();} catch (err) {}; |
48 |
this.collection.remove(this.id); |
49 |
} |
50 |
} |
51 |
}, |
52 |
|
53 |
// custom set method to allow submodels to use
|
54 |
// set_<attr> methods for handling the value of each
|
55 |
// attribute and overriding the default set method
|
56 |
// for specific parameters
|
57 |
set: function(params, options) { |
58 |
_.each(params, _.bind(function(value, key){
|
59 |
if (this["set_" + key]) { |
60 |
params[key] = this["set_" + key](value); |
61 |
} |
62 |
}, this))
|
63 |
var ret = bb.Model.prototype.set.call(this, params, options); |
64 |
return ret;
|
65 |
}, |
66 |
|
67 |
url: function(options) { |
68 |
return getUrl.call(this) + "/" + this.id; |
69 |
}, |
70 |
|
71 |
api_path: function(options) { |
72 |
return this.path + "/" + this.id; |
73 |
}, |
74 |
|
75 |
parse: function(resp, xhr) { |
76 |
return resp.server;
|
77 |
}, |
78 |
|
79 |
remove: function() { |
80 |
this.api.call(this.api_path(), "delete"); |
81 |
}, |
82 |
|
83 |
changedKeys: function() { |
84 |
return _.keys(this.changedAttributes() || {}); |
85 |
}, |
86 |
|
87 |
hasOnlyChange: function(keys) { |
88 |
var ret = false; |
89 |
_.each(keys, _.bind(function(key) {
|
90 |
if (this.changedKeys().length == 1 && this.changedKeys().indexOf(key) > -1) { ret = true}; |
91 |
}, this));
|
92 |
return ret;
|
93 |
} |
94 |
|
95 |
}) |
96 |
|
97 |
// Base object for all our model collections
|
98 |
models.Collection = bb.Collection.extend({ |
99 |
sync: snf.api.sync,
|
100 |
api: snf.api,
|
101 |
supportIncUpdates: true, |
102 |
url: function(options) { |
103 |
return getUrl.call(this) + (options.details || this.details ? '/detail' : ''); |
104 |
}, |
105 |
|
106 |
fetch: function(options) { |
107 |
// default to update
|
108 |
if (!this.noUpdate) { |
109 |
if (!options) { options = {} };
|
110 |
if (options.update === undefined) { options.update = true }; |
111 |
if (!options.removeMissing && options.refresh) { options.removeMissing = true }; |
112 |
} |
113 |
// custom event foreach fetch
|
114 |
return bb.Collection.prototype.fetch.call(this, options) |
115 |
}, |
116 |
|
117 |
get_fetcher: function(timeout, fast, limit, initial, params) { |
118 |
var fetch_params = params || {};
|
119 |
fetch_params.skips_timeouts = true;
|
120 |
|
121 |
var timeout = parseInt(timeout);
|
122 |
var fast = fast || 1000; |
123 |
var limit = limit;
|
124 |
var initial_call = initial || true; |
125 |
|
126 |
var last_ajax = undefined; |
127 |
var cb = _.bind(function() { |
128 |
// clone to avoid referenced objects
|
129 |
var params = _.clone(fetch_params);
|
130 |
updater._ajax = last_ajax; |
131 |
if (last_ajax) {
|
132 |
last_ajax.abort(); |
133 |
} |
134 |
last_ajax = this.fetch(params);
|
135 |
}, this);
|
136 |
var updater = new snf.api.updateHandler({'callback': cb, timeout:timeout, |
137 |
fast:fast, limit:limit, |
138 |
call_on_start:initial_call});
|
139 |
|
140 |
snf.api.bind("call", _.throttle(_.bind(function(){ updater.faster(true)}, this)), 1000); |
141 |
return updater;
|
142 |
} |
143 |
}); |
144 |
|
145 |
// Image model
|
146 |
models.Image = models.Model.extend({ |
147 |
path: 'images', |
148 |
|
149 |
get_size: function() { |
150 |
return parseInt(this.get('metadata') ? this.get('metadata').values.size : -1) |
151 |
}, |
152 |
|
153 |
get_readable_size: function() { |
154 |
return this.get_size() > 0 ? util.readablizeBytes(this.get_size() * 1024 * 1024) : "unknown"; |
155 |
}, |
156 |
|
157 |
get_os: function() { |
158 |
return this.get("OS"); |
159 |
}, |
160 |
|
161 |
get_sort_order: function() { |
162 |
return parseInt(this.get('metadata') ? this.get('metadata').values.sortorder : -1) |
163 |
} |
164 |
}); |
165 |
|
166 |
// Flavor model
|
167 |
models.Flavor = models.Model.extend({ |
168 |
path: 'flavors', |
169 |
|
170 |
details_string: function() { |
171 |
return "{0} CPU, {1}MB, {2}GB".format(this.get('cpu'), this.get('ram'), this.get('disk')); |
172 |
}, |
173 |
|
174 |
get_disk_size: function() { |
175 |
return parseInt(this.get("disk") * 1000) |
176 |
} |
177 |
|
178 |
}); |
179 |
|
180 |
//network vms list helper
|
181 |
var NetworkVMSList = function() { |
182 |
this.initialize = function() { |
183 |
this.vms = [];
|
184 |
this.pending = [];
|
185 |
this.pending_for_removal = [];
|
186 |
} |
187 |
|
188 |
this.add_pending_for_remove = function(vm_id) { |
189 |
if (this.pending_for_removal.indexOf(vm_id) == -1) { |
190 |
this.pending_for_removal.push(vm_id);
|
191 |
} |
192 |
|
193 |
if (this.pending_for_removal.length) { |
194 |
this.trigger("pending:remove:add"); |
195 |
} |
196 |
}, |
197 |
|
198 |
this.add_pending = function(vm_id) { |
199 |
if (this.pending.indexOf(vm_id) == -1) { |
200 |
this.pending[this.pending.length] = vm_id; |
201 |
} |
202 |
|
203 |
if (this.pending.length) { |
204 |
this.trigger("pending:add"); |
205 |
} |
206 |
} |
207 |
|
208 |
this.check_pending = function() { |
209 |
var len = this.pending.length; |
210 |
var args = [this.pending]; |
211 |
this.pending = _.difference(this.pending, this.vms); |
212 |
if (len != this.pending.length) { |
213 |
if (this.pending.length == 0) { |
214 |
this.trigger("pending:clear"); |
215 |
} |
216 |
} |
217 |
|
218 |
var len = this.pending_for_removal.length; |
219 |
this.pending_for_removal = _.intersection(this.pending_for_removal, this.vms); |
220 |
if (this.pending_for_removal.length == 0) { |
221 |
this.trigger("pending:remove:clear"); |
222 |
} |
223 |
|
224 |
} |
225 |
|
226 |
|
227 |
this.add = function(vm_id) { |
228 |
if (this.vms.indexOf(vm_id) == -1) { |
229 |
this.vms[this.vms.length] = vm_id; |
230 |
this.trigger("network:connect", vm_id); |
231 |
this.check_pending();
|
232 |
return true; |
233 |
} |
234 |
} |
235 |
|
236 |
this.remove = function(vm_id) { |
237 |
if (this.vms.indexOf(vm_id) > -1) { |
238 |
this.vms = _.without(this.vms, vm_id); |
239 |
this.trigger("network:disconnect", vm_id); |
240 |
this.check_pending();
|
241 |
return true; |
242 |
} |
243 |
} |
244 |
|
245 |
this.get = function() { |
246 |
return this.vms; |
247 |
} |
248 |
|
249 |
this.list = function() { |
250 |
return storage.vms.filter(_.bind(function(vm){ |
251 |
return this.vms.indexOf(vm.id) > -1; |
252 |
}, this))
|
253 |
} |
254 |
|
255 |
this.initialize();
|
256 |
}; |
257 |
_.extend(NetworkVMSList.prototype, bb.Events); |
258 |
|
259 |
// vm networks list helper
|
260 |
var VMNetworksList = function() { |
261 |
this.initialize = function() { |
262 |
this.networks = {};
|
263 |
this.network_ids = [];
|
264 |
} |
265 |
|
266 |
this.add = function(net_id, data) { |
267 |
if (!this.networks[net_id]) { |
268 |
this.networks[net_id] = data || {};
|
269 |
this.network_ids[this.network_ids.length] = net_id; |
270 |
this.trigger("network:connect", net_id); |
271 |
return true; |
272 |
} |
273 |
} |
274 |
|
275 |
this.remove = function(net_id) { |
276 |
if (this.networks[net_id]) { |
277 |
delete this.networks[net_id]; |
278 |
this.network_ids = _.without(this.network_ids, net_id); |
279 |
this.trigger("network:disconnect", net_id); |
280 |
return true; |
281 |
} |
282 |
return false; |
283 |
} |
284 |
|
285 |
this.get = function() { |
286 |
return this.networks; |
287 |
} |
288 |
|
289 |
this.list = function() { |
290 |
return storage.networks.filter(_.bind(function(net){ |
291 |
return this.network_ids.indexOf(net.id) > -1; |
292 |
}, this))
|
293 |
} |
294 |
|
295 |
this.initialize();
|
296 |
}; |
297 |
_.extend(VMNetworksList.prototype, bb.Events); |
298 |
|
299 |
// Image model
|
300 |
models.Network = models.Model.extend({ |
301 |
path: 'networks', |
302 |
has_status: true, |
303 |
|
304 |
initialize: function() { |
305 |
this.vms = new NetworkVMSList(); |
306 |
this.vms.bind("pending:add", _.bind(this.handle_pending_connections, this, "add")); |
307 |
this.vms.bind("pending:clear", _.bind(this.handle_pending_connections, this, "clear")); |
308 |
this.vms.bind("pending:remove:add", _.bind(this.handle_pending_connections, this, "add")); |
309 |
this.vms.bind("pending:remove:clear", _.bind(this.handle_pending_connections, this, "clear")); |
310 |
|
311 |
var ret = models.Network.__super__.initialize.apply(this, arguments); |
312 |
|
313 |
storage.vms.bind("change:linked_to_nets", _.bind(this.update_connections, this, "vm:change")); |
314 |
storage.vms.bind("add", _.bind(this.update_connections, this, "add")); |
315 |
storage.vms.bind("remove", _.bind(this.update_connections, this, "remove")); |
316 |
storage.vms.bind("reset", _.bind(this.update_connections, this, "reset")); |
317 |
|
318 |
this.bind("change:linked_to", _.bind(this.update_connections, this, "net:change")); |
319 |
this.update_connections();
|
320 |
this.update_state();
|
321 |
|
322 |
return ret;
|
323 |
}, |
324 |
|
325 |
update_state: function() { |
326 |
if (this.vms.pending.length) { |
327 |
this.set({state: "CONNECTING"}); |
328 |
return
|
329 |
} |
330 |
|
331 |
if (this.vms.pending_for_removal.length) { |
332 |
this.set({state: "DISCONNECTING"}); |
333 |
return
|
334 |
} |
335 |
|
336 |
var firewalling = false; |
337 |
_.each(this.vms.get(), _.bind(function(vm_id){ |
338 |
var vm = storage.vms.get(vm_id);
|
339 |
if (!vm) { return }; |
340 |
if (!_.isEmpty(vm.pending_firewalls)) {
|
341 |
this.set({state:"FIREWALLING"}); |
342 |
firewalling = true;
|
343 |
return false; |
344 |
} |
345 |
},this));
|
346 |
if (firewalling) { return }; |
347 |
|
348 |
this.set({state:"NORMAL"}); |
349 |
}, |
350 |
|
351 |
handle_pending_connections: function(action) { |
352 |
this.update_state();
|
353 |
}, |
354 |
|
355 |
// handle vm/network connections
|
356 |
update_connections: function(action, model) { |
357 |
|
358 |
// vm removed disconnect vm from network
|
359 |
if (action == "remove") { |
360 |
var removed_from_net = this.vms.remove(model.id); |
361 |
var removed_from_vm = model.networks.remove(this.id); |
362 |
if (removed_from_net) {this.trigger("vm:disconnect", model, this); this.change()}; |
363 |
if (removed_from_vm) {model.trigger("network:disconnect", this, model); this.change()}; |
364 |
return;
|
365 |
} |
366 |
|
367 |
// update links for all vms
|
368 |
var links = this.get("linked_to"); |
369 |
storage.vms.each(_.bind(function(vm) {
|
370 |
var vm_links = vm.get("linked_to") || []; |
371 |
if (vm_links.indexOf(this.id) > -1) { |
372 |
// vm has connection to current network
|
373 |
if (links.indexOf(vm.id) > -1) { |
374 |
// and network has connection to vm, so try
|
375 |
// to append it
|
376 |
var add_to_net = this.vms.add(vm.id); |
377 |
var index = _.indexOf(vm_links, this.id); |
378 |
var add_to_vm = vm.networks.add(this.id, vm.get("linked_to_nets")[index]); |
379 |
|
380 |
// call only if connection did not existed
|
381 |
if (add_to_net) {this.trigger("vm:connect", vm, this); this.change()}; |
382 |
if (add_to_vm) {vm.trigger("network:connect", this, vm); vm.change()}; |
383 |
} else {
|
384 |
// no connection, try to remove it
|
385 |
var removed_from_net = this.vms.remove(vm.id); |
386 |
var removed_from_vm = vm.networks.remove(this.id); |
387 |
if (removed_from_net) {this.trigger("vm:disconnect", vm, this); this.change()}; |
388 |
if (removed_from_vm) {vm.trigger("network:disconnect", this, vm); vm.change()}; |
389 |
} |
390 |
} else {
|
391 |
// vm has no connection to current network, try to remove it
|
392 |
var removed_from_net = this.vms.remove(vm.id); |
393 |
var removed_from_vm = vm.networks.remove(this.id); |
394 |
if (removed_from_net) {this.trigger("vm:disconnect", vm, this); this.change()}; |
395 |
if (removed_from_vm) {vm.trigger("network:disconnect", this, vm); vm.change()}; |
396 |
} |
397 |
},this));
|
398 |
}, |
399 |
|
400 |
is_public: function() { |
401 |
return this.id == "public"; |
402 |
}, |
403 |
|
404 |
contains_vm: function(vm) { |
405 |
var net_vm_exists = this.vms.get().indexOf(vm.id) > -1; |
406 |
var vm_net_exists = vm.is_connected_to(this); |
407 |
return net_vm_exists && vm_net_exists;
|
408 |
}, |
409 |
|
410 |
add_vm: function (vm, callback, error, options) { |
411 |
var payload = {add:{serverRef:"" + vm.id}}; |
412 |
payload._options = options || {}; |
413 |
return this.api.call(this.api_path() + "/action", "create", |
414 |
payload, |
415 |
_.bind(function(){
|
416 |
this.vms.add_pending(vm.id);
|
417 |
if (callback) {callback()}
|
418 |
},this), error);
|
419 |
}, |
420 |
|
421 |
remove_vm: function (vm, callback, error, options) { |
422 |
var payload = {remove:{serverRef:"" + vm.id}}; |
423 |
payload._options = options || {}; |
424 |
return this.api.call(this.api_path() + "/action", "create", |
425 |
{remove:{serverRef:"" + vm.id}}, |
426 |
_.bind(function(){
|
427 |
this.vms.add_pending_for_remove(vm.id);
|
428 |
if (callback) {callback()}
|
429 |
},this), error);
|
430 |
}, |
431 |
|
432 |
rename: function(name, callback) { |
433 |
return this.api.call(this.api_path(), "update", { |
434 |
network:{name:name}, |
435 |
_options:{
|
436 |
critical: false, |
437 |
error_params:{
|
438 |
title: "Network action failed", |
439 |
ns: "Networks", |
440 |
extra_details: {"Network id": this.id} |
441 |
} |
442 |
}}, callback); |
443 |
}, |
444 |
|
445 |
get_connectable_vms: function() { |
446 |
var servers = this.vms.list(); |
447 |
return storage.vms.filter(function(vm){ |
448 |
return servers.indexOf(vm) == -1 && !vm.in_error_state(); |
449 |
}) |
450 |
}, |
451 |
|
452 |
state_message: function() { |
453 |
if (this.get("state") == "NORMAL" && this.is_public()) { |
454 |
return "Public network"; |
455 |
} |
456 |
|
457 |
return models.Network.STATES[this.get("state")]; |
458 |
}, |
459 |
|
460 |
in_progress: function() { |
461 |
return models.Network.STATES_TRANSITIONS[this.get("state")] != undefined; |
462 |
} |
463 |
}); |
464 |
|
465 |
models.Network.STATES = { |
466 |
'NORMAL': 'Private network', |
467 |
'CONNECTING': 'Connecting...', |
468 |
'DISCONNECTING': 'Disconnecting...', |
469 |
'FIREWALLING': 'Firewall update...', |
470 |
'DESTROY': 'Destroying...' |
471 |
} |
472 |
|
473 |
models.Network.STATES_TRANSITIONS = { |
474 |
'CONNECTING': ['NORMAL'], |
475 |
'DISCONNECTING': ['NORMAL'], |
476 |
'FIREWALLING': ['NORMAL'] |
477 |
} |
478 |
|
479 |
// Virtualmachine model
|
480 |
models.VM = models.Model.extend({ |
481 |
|
482 |
path: 'servers', |
483 |
has_status: true, |
484 |
initialize: function(params) { |
485 |
this.networks = new VMNetworksList(); |
486 |
|
487 |
this.pending_firewalls = {};
|
488 |
|
489 |
models.VM.__super__.initialize.apply(this, arguments); |
490 |
|
491 |
this.set({state: params.status || "ERROR"}); |
492 |
this.log = new snf.logging.logger("VM " + this.id); |
493 |
this.pending_action = undefined; |
494 |
|
495 |
// init stats parameter
|
496 |
this.set({'stats': undefined}, {silent: true}); |
497 |
// defaults to not update the stats
|
498 |
// each view should handle this vm attribute
|
499 |
// depending on if it displays stat images or not
|
500 |
this.do_update_stats = false; |
501 |
|
502 |
// interval time
|
503 |
// this will dynamicaly change if the server responds that
|
504 |
// images get refreshed on different intervals
|
505 |
this.stats_update_interval = synnefo.config.STATS_INTERVAL || 5000; |
506 |
this.stats_available = false; |
507 |
|
508 |
// initialize interval
|
509 |
this.init_stats_intervals(this.stats_update_interval); |
510 |
|
511 |
this.bind("change:progress", _.bind(this.update_building_progress, this)); |
512 |
this.update_building_progress();
|
513 |
|
514 |
this.bind("change:firewalls", _.bind(this.handle_firewall_change, this)); |
515 |
|
516 |
// default values
|
517 |
this.set({linked_to_nets:this.get("linked_to_nets") || []}); |
518 |
this.set({firewalls:this.get("firewalls") || []}); |
519 |
|
520 |
this.bind("change:state", _.bind(function(){if (this.state() == "DESTROY") { this.handle_destroy() }}, this)) |
521 |
}, |
522 |
|
523 |
handle_firewall_change: function() { |
524 |
|
525 |
}, |
526 |
|
527 |
set_linked_to_nets: function(data) { |
528 |
this.set({"linked_to":_.map(data, function(n){ return n.id})}); |
529 |
return data;
|
530 |
}, |
531 |
|
532 |
is_connected_to: function(net) { |
533 |
return _.filter(this.networks.list(), function(n){return n.id == net.id}).length > 0; |
534 |
}, |
535 |
|
536 |
status: function(st) { |
537 |
if (!st) { return this.get("status")} |
538 |
return this.set({status:st}); |
539 |
}, |
540 |
|
541 |
set_status: function(st) { |
542 |
var new_state = this.state_for_api_status(st); |
543 |
var transition = false; |
544 |
|
545 |
if (this.state() != new_state) { |
546 |
if (models.VM.STATES_TRANSITIONS[this.state()]) { |
547 |
transition = this.state();
|
548 |
} |
549 |
} |
550 |
|
551 |
// call it silently to avoid double change trigger
|
552 |
this.set({'state': this.state_for_api_status(st)}, {silent: true}); |
553 |
|
554 |
// trigger transition
|
555 |
if (transition && models.VM.TRANSITION_STATES.indexOf(new_state) == -1) { |
556 |
this.trigger("transition", {from:transition, to:new_state}) |
557 |
}; |
558 |
return st;
|
559 |
}, |
560 |
|
561 |
update_building_progress: function() { |
562 |
if (this.is_building()) { |
563 |
var progress = this.get("progress"); |
564 |
if (progress == 0) { |
565 |
this.state("BUILD_INIT"); |
566 |
this.set({progress_message: BUILDING_MESSAGES['INIT']}); |
567 |
} |
568 |
if (progress > 0 && progress < 99) { |
569 |
this.state("BUILD_COPY"); |
570 |
var params = this.get_copy_details(true); |
571 |
this.set({progress_message: BUILDING_MESSAGES['COPY'].format(params.copy, |
572 |
params.size, |
573 |
params.progress)}); |
574 |
} |
575 |
if (progress == 100) { |
576 |
this.state("BUILD_FINAL"); |
577 |
this.set({progress_message: BUILDING_MESSAGES['FINAL']}); |
578 |
} |
579 |
} else {
|
580 |
} |
581 |
}, |
582 |
|
583 |
get_copy_details: function(human, image) { |
584 |
var human = human || false; |
585 |
var image = image || this.get_image(); |
586 |
|
587 |
var progress = this.get('progress'); |
588 |
var size = image.get_size();
|
589 |
var size_copied = (size * progress / 100).toFixed(2); |
590 |
|
591 |
if (human) {
|
592 |
size = util.readablizeBytes(size*1024*1024); |
593 |
size_copied = util.readablizeBytes(size_copied*1024*1024); |
594 |
} |
595 |
return {'progress': progress, 'size': size, 'copy': size_copied}; |
596 |
}, |
597 |
|
598 |
start_stats_update: function(force_if_empty) { |
599 |
var prev_state = this.do_update_stats; |
600 |
|
601 |
this.do_update_stats = true; |
602 |
|
603 |
// fetcher initialized ??
|
604 |
if (!this.stats_fetcher) { |
605 |
this.init_stats_intervals();
|
606 |
} |
607 |
|
608 |
|
609 |
// fetcher running ???
|
610 |
if (!this.stats_fetcher.running || !prev_state) { |
611 |
this.stats_fetcher.start();
|
612 |
} |
613 |
|
614 |
if (force_if_empty && this.get("stats") == undefined) { |
615 |
this.update_stats(true); |
616 |
} |
617 |
}, |
618 |
|
619 |
stop_stats_update: function(stop_calls) { |
620 |
this.do_update_stats = false; |
621 |
|
622 |
if (stop_calls) {
|
623 |
this.stats_fetcher.stop();
|
624 |
} |
625 |
}, |
626 |
|
627 |
// clear and reinitialize update interval
|
628 |
init_stats_intervals: function (interval) { |
629 |
this.stats_fetcher = this.get_stats_fetcher(this.stats_update_interval); |
630 |
this.stats_fetcher.start();
|
631 |
}, |
632 |
|
633 |
get_stats_fetcher: function(timeout) { |
634 |
var cb = _.bind(function(data){ |
635 |
this.update_stats();
|
636 |
}, this);
|
637 |
var fetcher = new snf.api.updateHandler({'callback': cb, timeout:timeout}); |
638 |
return fetcher;
|
639 |
}, |
640 |
|
641 |
// do the api call
|
642 |
update_stats: function(force) { |
643 |
// do not update stats if flag not set
|
644 |
if ((!this.do_update_stats && !force) || this.updating_stats) { |
645 |
return;
|
646 |
} |
647 |
|
648 |
// make the api call, execute handle_stats_update on sucess
|
649 |
// TODO: onError handler ???
|
650 |
stats_url = this.url() + "/stats"; |
651 |
this.updating_stats = true; |
652 |
this.sync("GET", this, { |
653 |
handles_error:true, |
654 |
url: stats_url,
|
655 |
refresh:true, |
656 |
success: _.bind(this.handle_stats_update, this), |
657 |
error: _.bind(this.handle_stats_error, this), |
658 |
complete: _.bind(function(){this.updating_stats = false;}, this), |
659 |
critical: false, |
660 |
display: false, |
661 |
log_error: false |
662 |
}); |
663 |
}, |
664 |
|
665 |
get_stats_image: function(stat, type) { |
666 |
}, |
667 |
|
668 |
_set_stats: function(stats) { |
669 |
var silent = silent === undefined ? false : silent; |
670 |
// unavailable stats while building
|
671 |
if (this.get("status") == "BUILD") { |
672 |
this.stats_available = false; |
673 |
} else { this.stats_available = true; } |
674 |
|
675 |
if (this.get("status") == "DESTROY") { this.stats_available = false; } |
676 |
|
677 |
this.set({stats: stats}, {silent:true}); |
678 |
this.trigger("stats:update", stats); |
679 |
}, |
680 |
|
681 |
unbind: function() { |
682 |
models.VM.__super__.unbind.apply(this, arguments); |
683 |
}, |
684 |
|
685 |
handle_stats_error: function() { |
686 |
stats = {}; |
687 |
_.each(['cpuBar', 'cpuTimeSeries', 'netBar', 'netTimeSeries'], function(k) { |
688 |
stats[k] = false;
|
689 |
}); |
690 |
|
691 |
this.set({'stats': stats}); |
692 |
}, |
693 |
|
694 |
// this method gets executed after a successful vm stats api call
|
695 |
handle_stats_update: function(data) { |
696 |
var self = this; |
697 |
// avoid browser caching
|
698 |
|
699 |
if (data.stats && _.size(data.stats) > 0) { |
700 |
var ts = $.now(); |
701 |
var stats = data.stats;
|
702 |
var images_loaded = 0; |
703 |
var images = {};
|
704 |
|
705 |
function check_images_loaded() { |
706 |
images_loaded++; |
707 |
|
708 |
if (images_loaded == 4) { |
709 |
self._set_stats(images); |
710 |
} |
711 |
} |
712 |
_.each(['cpuBar', 'cpuTimeSeries', 'netBar', 'netTimeSeries'], function(k) { |
713 |
|
714 |
stats[k] = stats[k] + "?_=" + ts;
|
715 |
|
716 |
var stat = k.slice(0,3); |
717 |
var type = k.slice(3,6) == "Bar" ? "bar" : "time"; |
718 |
var img = $("<img />"); |
719 |
var val = stats[k];
|
720 |
|
721 |
// load stat image to a temporary dom element
|
722 |
// update model stats on image load/error events
|
723 |
img.load(function() {
|
724 |
images[k] = val; |
725 |
check_images_loaded(); |
726 |
}); |
727 |
|
728 |
img.error(function() {
|
729 |
images[stat + type] = false;
|
730 |
check_images_loaded(); |
731 |
}); |
732 |
|
733 |
img.attr({'src': stats[k]});
|
734 |
}) |
735 |
data.stats = stats; |
736 |
} |
737 |
|
738 |
// do we need to change the interval ??
|
739 |
if (data.stats.refresh * 1000 != this.stats_update_interval) { |
740 |
this.stats_update_interval = data.stats.refresh * 1000; |
741 |
this.stats_fetcher.timeout = this.stats_update_interval; |
742 |
this.stats_fetcher.stop();
|
743 |
this.stats_fetcher.start(false); |
744 |
} |
745 |
}, |
746 |
|
747 |
// helper method that sets the do_update_stats
|
748 |
// in the future this method could also make an api call
|
749 |
// immediaetly if needed
|
750 |
enable_stats_update: function() { |
751 |
this.do_update_stats = true; |
752 |
}, |
753 |
|
754 |
handle_destroy: function() { |
755 |
this.stats_fetcher.stop();
|
756 |
}, |
757 |
|
758 |
require_reboot: function() { |
759 |
if (this.is_active()) { |
760 |
this.set({'reboot_required': true}); |
761 |
} |
762 |
}, |
763 |
|
764 |
set_pending_action: function(data) { |
765 |
this.pending_action = data;
|
766 |
return data;
|
767 |
}, |
768 |
|
769 |
// machine has pending action
|
770 |
update_pending_action: function(action, force) { |
771 |
this.set({pending_action: action}); |
772 |
}, |
773 |
|
774 |
clear_pending_action: function() { |
775 |
this.set({pending_action: undefined}); |
776 |
}, |
777 |
|
778 |
has_pending_action: function() { |
779 |
return this.get("pending_action") ? this.get("pending_action") : false; |
780 |
}, |
781 |
|
782 |
// machine is active
|
783 |
is_active: function() { |
784 |
return models.VM.ACTIVE_STATES.indexOf(this.state()) > -1; |
785 |
}, |
786 |
|
787 |
// machine is building
|
788 |
is_building: function() { |
789 |
return models.VM.BUILDING_STATES.indexOf(this.state()) > -1; |
790 |
}, |
791 |
|
792 |
in_error_state: function() { |
793 |
return this.state() === "ERROR" |
794 |
}, |
795 |
|
796 |
// user can connect to machine
|
797 |
is_connectable: function() { |
798 |
// check if ips exist
|
799 |
if (!this.get_addresses().ip4 && !this.get_addresses().ip6) { |
800 |
return false; |
801 |
} |
802 |
return models.VM.CONNECT_STATES.indexOf(this.state()) > -1; |
803 |
}, |
804 |
|
805 |
set_firewalls: function(data) { |
806 |
_.each(data, _.bind(function(val, key){
|
807 |
if (this.pending_firewalls && this.pending_firewalls[key] && this.pending_firewalls[key] == val) { |
808 |
this.require_reboot();
|
809 |
this.remove_pending_firewall(key, val);
|
810 |
} |
811 |
}, this));
|
812 |
return data;
|
813 |
}, |
814 |
|
815 |
remove_pending_firewall: function(net_id, value) { |
816 |
if (this.pending_firewalls[net_id] == value) { |
817 |
delete this.pending_firewalls[net_id]; |
818 |
storage.networks.get(net_id).update_state(); |
819 |
} |
820 |
}, |
821 |
|
822 |
remove_meta: function(key, complete, error) { |
823 |
var url = this.api_path() + "/meta/" + key; |
824 |
this.api.call(url, "delete", undefined, complete, error); |
825 |
}, |
826 |
|
827 |
save_meta: function(meta, complete, error) { |
828 |
var url = this.api_path() + "/meta/" + meta.key; |
829 |
var payload = {meta:{}}; |
830 |
payload.meta[meta.key] = meta.value; |
831 |
payload._options = { |
832 |
critical:false, |
833 |
error_params: {
|
834 |
title: "Machine metadata error", |
835 |
extra_details: {"Machine id": this.id} |
836 |
}}; |
837 |
|
838 |
this.api.call(url, "update", payload, complete, error); |
839 |
}, |
840 |
|
841 |
set_firewall: function(net_id, value, callback, error, options) { |
842 |
if (this.get("firewalls") && this.get("firewalls")[net_id] == value) { return } |
843 |
|
844 |
this.pending_firewalls[net_id] = value;
|
845 |
this.trigger("change", this, this); |
846 |
var payload = {"firewallProfile":{"profile":value}}; |
847 |
payload._options = _.extend({critical: false}, options); |
848 |
|
849 |
// reset firewall state on error
|
850 |
var error_cb = _.bind(function() { |
851 |
thi |
852 |
}, this);
|
853 |
|
854 |
this.api.call(this.api_path() + "/action", "create", payload, callback, error); |
855 |
storage.networks.get(net_id).update_state(); |
856 |
}, |
857 |
|
858 |
firewall_pending: function(net_id) { |
859 |
return this.pending_firewalls[net_id] != undefined; |
860 |
}, |
861 |
|
862 |
// update/get the state of the machine
|
863 |
state: function() { |
864 |
var args = slice.call(arguments); |
865 |
|
866 |
// TODO: it might not be a good idea to set the state in set_state method
|
867 |
if (args.length > 0 && models.VM.STATES.indexOf(args[0]) > -1) { |
868 |
this.set({'state': args[0]}); |
869 |
} |
870 |
|
871 |
return this.get('state'); |
872 |
}, |
873 |
|
874 |
// get the state that the api status corresponds to
|
875 |
state_for_api_status: function(status) { |
876 |
return this.state_transition(this.state(), status); |
877 |
}, |
878 |
|
879 |
// vm state equals vm api status
|
880 |
state_is_status: function(state) { |
881 |
return models.VM.STATUSES.indexOf(state) != -1; |
882 |
}, |
883 |
|
884 |
// get transition state for the corresponging api status
|
885 |
state_transition: function(state, new_status) { |
886 |
var statuses = models.VM.STATES_TRANSITIONS[state];
|
887 |
if (statuses) {
|
888 |
if (statuses.indexOf(new_status) > -1) { |
889 |
return new_status;
|
890 |
} else {
|
891 |
return state;
|
892 |
} |
893 |
} else {
|
894 |
return new_status;
|
895 |
} |
896 |
}, |
897 |
|
898 |
// the current vm state is a transition state
|
899 |
in_transition: function() { |
900 |
return models.VM.TRANSITION_STATES.indexOf(this.state()) > -1 || |
901 |
models.VM.TRANSITION_STATES.indexOf(this.get('status')) > -1; |
902 |
}, |
903 |
|
904 |
// get image object
|
905 |
// TODO: update images synchronously if image not found
|
906 |
get_image: function() { |
907 |
var image = storage.images.get(this.get('imageRef')); |
908 |
if (!image) {
|
909 |
storage.images.update_unknown_id(this.get('imageRef')); |
910 |
image = storage.images.get(this.get('imageRef')); |
911 |
} |
912 |
return image;
|
913 |
}, |
914 |
|
915 |
// get flavor object
|
916 |
get_flavor: function() { |
917 |
var flv = storage.flavors.get(this.get('flavorRef')); |
918 |
if (!flv) {
|
919 |
storage.flavors.update_unknown_id(this.get('flavorRef')); |
920 |
flv = storage.flavors.get(this.get('flavorRef')); |
921 |
} |
922 |
return flv;
|
923 |
}, |
924 |
|
925 |
// retrieve the metadata object
|
926 |
get_meta: function() { |
927 |
try {
|
928 |
return this.get('metadata').values |
929 |
} catch (err) {
|
930 |
return {};
|
931 |
} |
932 |
}, |
933 |
|
934 |
// get metadata OS value
|
935 |
get_os: function() { |
936 |
return this.get_meta().OS || (this.get_image() ? this.get_image().get_os() || "okeanos" : "okeanos"); |
937 |
}, |
938 |
|
939 |
// get public ip addresses
|
940 |
// TODO: public network is always the 0 index ???
|
941 |
get_addresses: function(net_id) { |
942 |
var net_id = net_id || "public"; |
943 |
|
944 |
var info = this.get_network_info(net_id); |
945 |
if (!info) { return {} }; |
946 |
addrs = {}; |
947 |
_.each(info.values, function(addr) {
|
948 |
addrs["ip" + addr.version] = addr.addr;
|
949 |
}); |
950 |
return addrs
|
951 |
}, |
952 |
|
953 |
get_network_info: function(net_id) { |
954 |
var net_id = net_id || "public"; |
955 |
|
956 |
if (!this.networks.network_ids.length) { return {} }; |
957 |
|
958 |
var addresses = this.networks.get(); |
959 |
try {
|
960 |
return _.select(addresses, function(net, key){return key == net_id })[0]; |
961 |
} catch (err) {
|
962 |
//this.log.debug("Cannot find network {0}".format(net_id))
|
963 |
} |
964 |
}, |
965 |
|
966 |
firewall_profile: function(net_id) { |
967 |
var net_id = net_id || "public"; |
968 |
var firewalls = this.get("firewalls"); |
969 |
return firewalls[net_id];
|
970 |
}, |
971 |
|
972 |
has_firewall: function(net_id) { |
973 |
var net_id = net_id || "public"; |
974 |
return ["ENABLED","PROTECTED"].indexOf(this.firewall_profile()) > -1; |
975 |
}, |
976 |
|
977 |
// get actions that the user can execute
|
978 |
// depending on the vm state/status
|
979 |
get_available_actions: function() { |
980 |
return models.VM.AVAILABLE_ACTIONS[this.state()]; |
981 |
}, |
982 |
|
983 |
set_profile: function(profile, net_id) { |
984 |
}, |
985 |
|
986 |
// call rename api
|
987 |
rename: function(new_name) { |
988 |
//this.set({'name': new_name});
|
989 |
this.sync("update", this, { |
990 |
critical: true, |
991 |
data: {
|
992 |
'server': {
|
993 |
'name': new_name
|
994 |
} |
995 |
}, |
996 |
// do the rename after the method succeeds
|
997 |
success: _.bind(function(){ |
998 |
//this.set({name: new_name});
|
999 |
snf.api.trigger("call");
|
1000 |
}, this)
|
1001 |
}); |
1002 |
}, |
1003 |
|
1004 |
get_console_url: function(data) { |
1005 |
var url_params = {
|
1006 |
machine: this.get("name"), |
1007 |
host_ip: this.get_addresses().ip4, |
1008 |
host_ip_v6: this.get_addresses().ip6, |
1009 |
host: data.host,
|
1010 |
port: data.port,
|
1011 |
password: data.password
|
1012 |
} |
1013 |
return '/machines/console?' + $.param(url_params); |
1014 |
}, |
1015 |
|
1016 |
// action helper
|
1017 |
call: function(action_name, success, error) { |
1018 |
var id_param = [this.id]; |
1019 |
|
1020 |
success = success || function() {};
|
1021 |
error = error || function() {};
|
1022 |
|
1023 |
var self = this; |
1024 |
|
1025 |
switch(action_name) {
|
1026 |
case 'start': |
1027 |
this.__make_api_call(this.get_action_url(), // vm actions url |
1028 |
"create", // create so that sync later uses POST to make the call |
1029 |
{start:{}}, // payload |
1030 |
function() {
|
1031 |
// set state after successful call
|
1032 |
self.state("START");
|
1033 |
success.apply(this, arguments); |
1034 |
snf.api.trigger("call");
|
1035 |
}, |
1036 |
error, 'start');
|
1037 |
break;
|
1038 |
case 'reboot': |
1039 |
this.__make_api_call(this.get_action_url(), // vm actions url |
1040 |
"create", // create so that sync later uses POST to make the call |
1041 |
{reboot:{type:"HARD"}}, // payload |
1042 |
function() {
|
1043 |
// set state after successful call
|
1044 |
self.state("REBOOT");
|
1045 |
success.apply(this, arguments) |
1046 |
snf.api.trigger("call");
|
1047 |
self.set({'reboot_required': false}); |
1048 |
}, |
1049 |
error, 'reboot');
|
1050 |
break;
|
1051 |
case 'shutdown': |
1052 |
this.__make_api_call(this.get_action_url(), // vm actions url |
1053 |
"create", // create so that sync later uses POST to make the call |
1054 |
{shutdown:{}}, // payload |
1055 |
function() {
|
1056 |
// set state after successful call
|
1057 |
self.state("SHUTDOWN");
|
1058 |
success.apply(this, arguments) |
1059 |
snf.api.trigger("call");
|
1060 |
}, |
1061 |
error, 'shutdown');
|
1062 |
break;
|
1063 |
case 'console': |
1064 |
this.__make_api_call(this.url() + "/action", "create", {'console': {'type':'vnc'}}, function(data) { |
1065 |
var cons_data = data.console;
|
1066 |
success.apply(this, [cons_data]);
|
1067 |
}, undefined, 'console') |
1068 |
break;
|
1069 |
case 'destroy': |
1070 |
this.__make_api_call(this.url(), // vm actions url |
1071 |
"delete", // create so that sync later uses POST to make the call |
1072 |
undefined, // payload |
1073 |
function() {
|
1074 |
// set state after successful call
|
1075 |
self.state('DESTROY');
|
1076 |
success.apply(this, arguments) |
1077 |
}, |
1078 |
error, 'destroy');
|
1079 |
break;
|
1080 |
default:
|
1081 |
throw "Invalid VM action ("+action_name+")"; |
1082 |
} |
1083 |
}, |
1084 |
|
1085 |
__make_api_call: function(url, method, data, success, error, action) { |
1086 |
var self = this; |
1087 |
error = error || function(){};
|
1088 |
success = success || function(){};
|
1089 |
|
1090 |
var params = {
|
1091 |
url: url,
|
1092 |
data: data,
|
1093 |
success: function(){ self.handle_action_succeed.apply(self, arguments); success.apply(this, arguments)}, |
1094 |
error: function(){ self.handle_action_fail.apply(self, arguments); error.apply(this, arguments)}, |
1095 |
error_params: { ns: "Machines actions", |
1096 |
title: "'" + this.get("name") + "'" + " " + action + " failed", |
1097 |
extra_details: { 'Machine ID': this.id, 'URL': url, 'Action': action || "undefined" }, |
1098 |
allow_reload: false |
1099 |
}, |
1100 |
display: false, |
1101 |
critical: false |
1102 |
} |
1103 |
this.sync(method, this, params); |
1104 |
}, |
1105 |
|
1106 |
handle_action_succeed: function() { |
1107 |
this.trigger("action:success", arguments); |
1108 |
}, |
1109 |
|
1110 |
reset_action_error: function() { |
1111 |
this.action_error = false; |
1112 |
this.trigger("action:fail:reset", this.action_error); |
1113 |
}, |
1114 |
|
1115 |
handle_action_fail: function() { |
1116 |
this.action_error = arguments; |
1117 |
this.trigger("action:fail", arguments); |
1118 |
}, |
1119 |
|
1120 |
get_action_url: function(name) { |
1121 |
return this.url() + "/action"; |
1122 |
}, |
1123 |
|
1124 |
get_connection_info: function(host_os, success, error) { |
1125 |
var url = "/machines/connect"; |
1126 |
params = { |
1127 |
ip_address: this.get_addresses().ip4, |
1128 |
os: this.get_os(), |
1129 |
host_os: host_os,
|
1130 |
srv: this.id |
1131 |
} |
1132 |
|
1133 |
url = url + "?" + $.param(params); |
1134 |
|
1135 |
var ajax = snf.api.sync("read", undefined, { url: url, |
1136 |
error:error,
|
1137 |
success:success,
|
1138 |
handles_error:1}); |
1139 |
} |
1140 |
}) |
1141 |
|
1142 |
models.VM.ACTIONS = [ |
1143 |
'start',
|
1144 |
'shutdown',
|
1145 |
'reboot',
|
1146 |
'console',
|
1147 |
'destroy'
|
1148 |
] |
1149 |
|
1150 |
models.VM.AVAILABLE_ACTIONS = { |
1151 |
'UNKNWON' : ['destroy'], |
1152 |
'BUILD' : ['destroy'], |
1153 |
'REBOOT' : ['shutdown', 'destroy', 'console'], |
1154 |
'STOPPED' : ['start', 'destroy'], |
1155 |
'ACTIVE' : ['shutdown', 'destroy', 'reboot', 'console'], |
1156 |
'ERROR' : ['destroy'], |
1157 |
'DELETED' : [],
|
1158 |
'DESTROY' : [],
|
1159 |
'BUILD_INIT' : ['destroy'], |
1160 |
'BUILD_COPY' : ['destroy'], |
1161 |
'BUILD_FINAL' : ['destroy'], |
1162 |
'SHUTDOWN' : ['destroy'], |
1163 |
'START' : [],
|
1164 |
'CONNECT' : [],
|
1165 |
'DISCONNECT' : []
|
1166 |
} |
1167 |
|
1168 |
// api status values
|
1169 |
models.VM.STATUSES = [ |
1170 |
'UNKNWON',
|
1171 |
'BUILD',
|
1172 |
'REBOOT',
|
1173 |
'STOPPED',
|
1174 |
'ACTIVE',
|
1175 |
'ERROR',
|
1176 |
'DELETED'
|
1177 |
] |
1178 |
|
1179 |
// api status values
|
1180 |
models.VM.CONNECT_STATES = [ |
1181 |
'ACTIVE',
|
1182 |
'REBOOT',
|
1183 |
'SHUTDOWN'
|
1184 |
] |
1185 |
|
1186 |
// vm states
|
1187 |
models.VM.STATES = models.VM.STATUSES.concat([ |
1188 |
'DESTROY',
|
1189 |
'BUILD_INIT',
|
1190 |
'BUILD_COPY',
|
1191 |
'BUILD_FINAL',
|
1192 |
'SHUTDOWN',
|
1193 |
'START',
|
1194 |
'CONNECT',
|
1195 |
'DISCONNECT',
|
1196 |
'FIREWALL'
|
1197 |
]); |
1198 |
|
1199 |
models.VM.STATES_TRANSITIONS = { |
1200 |
'DESTROY' : ['DELETED'], |
1201 |
'SHUTDOWN': ['ERROR', 'STOPPED', 'DESTROY'], |
1202 |
'STOPPED': ['ERROR', 'ACTIVE', 'DESTROY'], |
1203 |
'ACTIVE': ['ERROR', 'STOPPED', 'REBOOT', 'SHUTDOWN', 'DESTROY'], |
1204 |
'START': ['ERROR', 'ACTIVE', 'DESTROY'], |
1205 |
'REBOOT': ['ERROR', 'ACTIVE', 'STOPPED', 'DESTROY'], |
1206 |
'BUILD': ['ERROR', 'ACTIVE', 'DESTROY'], |
1207 |
'BUILD_COPY': ['ERROR', 'ACTIVE', 'BUILD_FINAL', 'DESTROY'], |
1208 |
'BUILD_FINAL': ['ERROR', 'ACTIVE', 'DESTROY'], |
1209 |
'BUILD_INIT': ['ERROR', 'ACTIVE', 'BUILD_COPY', 'BUILD_FINAL', 'DESTROY'] |
1210 |
} |
1211 |
|
1212 |
models.VM.TRANSITION_STATES = [ |
1213 |
'DESTROY',
|
1214 |
'SHUTDOWN',
|
1215 |
'START',
|
1216 |
'REBOOT',
|
1217 |
'BUILD'
|
1218 |
] |
1219 |
|
1220 |
models.VM.ACTIVE_STATES = [ |
1221 |
'BUILD', 'REBOOT', 'ACTIVE', |
1222 |
'BUILD_INIT', 'BUILD_COPY', 'BUILD_FINAL', |
1223 |
'SHUTDOWN', 'CONNECT', 'DISCONNECT' |
1224 |
] |
1225 |
|
1226 |
models.VM.BUILDING_STATES = [ |
1227 |
'BUILD', 'BUILD_INIT', 'BUILD_COPY', 'BUILD_FINAL' |
1228 |
] |
1229 |
|
1230 |
models.Networks = models.Collection.extend({ |
1231 |
model: models.Network,
|
1232 |
path: 'networks', |
1233 |
details: true, |
1234 |
//noUpdate: true,
|
1235 |
defaults: {'linked_to':[]}, |
1236 |
|
1237 |
parse: function (resp, xhr) { |
1238 |
// FIXME: depricated global var
|
1239 |
if (!resp) { return []}; |
1240 |
|
1241 |
var data = _.map(resp.networks.values, _.bind(this.parse_net_api_data, this)); |
1242 |
return data;
|
1243 |
}, |
1244 |
|
1245 |
parse_net_api_data: function(data) { |
1246 |
if (data.servers && data.servers.values) {
|
1247 |
data['linked_to'] = data.servers.values;
|
1248 |
} |
1249 |
return data;
|
1250 |
}, |
1251 |
|
1252 |
create: function (name, callback) { |
1253 |
return this.api.call(this.path, "create", {network:{name:name}}, callback); |
1254 |
} |
1255 |
}) |
1256 |
|
1257 |
models.Images = models.Collection.extend({ |
1258 |
model: models.Image,
|
1259 |
path: 'images', |
1260 |
details: true, |
1261 |
noUpdate: true, |
1262 |
supportIncUpdates: false, |
1263 |
meta_keys_as_attrs: ["OS", "description", "kernel", "size", "GUI"], |
1264 |
|
1265 |
// update collection model with id passed
|
1266 |
// making a direct call to the image
|
1267 |
// api url
|
1268 |
update_unknown_id: function(id) { |
1269 |
var url = getUrl.call(this) + "/" + id; |
1270 |
this.api.call(this.path + "/" + id, "read", {_options:{async:false}}, undefined, |
1271 |
_.bind(function() {
|
1272 |
this.add({id:id, name:"Unknown image", size:-1, progress:100, status:"DELETED"}) |
1273 |
}, this), _.bind(function(image) { |
1274 |
this.add(image.image);
|
1275 |
}, this));
|
1276 |
}, |
1277 |
|
1278 |
parse: function (resp, xhr) { |
1279 |
// FIXME: depricated global var
|
1280 |
var data = _.map(resp.images.values, _.bind(this.parse_meta, this)); |
1281 |
return resp.images.values;
|
1282 |
}, |
1283 |
|
1284 |
get_meta_key: function(img, key) { |
1285 |
if (img.metadata && img.metadata.values && img.metadata.values[key]) {
|
1286 |
return img.metadata.values[key];
|
1287 |
} |
1288 |
return undefined; |
1289 |
}, |
1290 |
|
1291 |
comparator: function(img) { |
1292 |
return -img.get_sort_order("sortorder") || 1000 * img.id; |
1293 |
}, |
1294 |
|
1295 |
parse_meta: function(img) { |
1296 |
_.each(this.meta_keys_as_attrs, _.bind(function(key){ |
1297 |
img[key] = this.get_meta_key(img, key) || ""; |
1298 |
}, this));
|
1299 |
return img;
|
1300 |
}, |
1301 |
|
1302 |
active: function() { |
1303 |
return this.filter(function(img){return img.get('status') != "DELETED"}); |
1304 |
} |
1305 |
}) |
1306 |
|
1307 |
models.Flavors = models.Collection.extend({ |
1308 |
model: models.Flavor,
|
1309 |
path: 'flavors', |
1310 |
details: true, |
1311 |
noUpdate: true, |
1312 |
supportIncUpdates: false, |
1313 |
// update collection model with id passed
|
1314 |
// making a direct call to the flavor
|
1315 |
// api url
|
1316 |
update_unknown_id: function(id) { |
1317 |
var url = getUrl.call(this) + "/" + id; |
1318 |
this.api.call(this.path + "/" + id, "read", {_options:{async:false}}, undefined, |
1319 |
_.bind(function() {
|
1320 |
this.add({id:id, cpu:"", ram:"", disk:"", name: "", status:"DELETED"}) |
1321 |
}, this), _.bind(function(flv) { |
1322 |
if (!flv.flavor.status) { flv.flavor.status = "DELETED" }; |
1323 |
this.add(flv.flavor);
|
1324 |
}, this));
|
1325 |
}, |
1326 |
|
1327 |
parse: function (resp, xhr) { |
1328 |
// FIXME: depricated global var
|
1329 |
return resp.flavors.values;
|
1330 |
}, |
1331 |
|
1332 |
comparator: function(flv) { |
1333 |
return flv.get("disk") * flv.get("cpu") * flv.get("ram"); |
1334 |
}, |
1335 |
|
1336 |
unavailable_values_for_image: function(img, flavors) { |
1337 |
var flavors = flavors || this.active(); |
1338 |
var size = img.get_size();
|
1339 |
|
1340 |
var index = {cpu:[], disk:[], ram:[]}; |
1341 |
|
1342 |
_.each(this.active(), function(el) { |
1343 |
var img_size = size;
|
1344 |
var flv_size = el.get_disk_size();
|
1345 |
if (flv_size < img_size) {
|
1346 |
if (index.disk.indexOf(flv_size) == -1) { |
1347 |
index.disk.push(flv_size); |
1348 |
} |
1349 |
}; |
1350 |
}); |
1351 |
|
1352 |
return index;
|
1353 |
}, |
1354 |
|
1355 |
get_flavor: function(cpu, mem, disk, filter_list) { |
1356 |
if (!filter_list) { filter_list = this.models }; |
1357 |
|
1358 |
return this.select(function(flv){ |
1359 |
if (flv.get("cpu") == cpu + "" && |
1360 |
flv.get("ram") == mem + "" && |
1361 |
flv.get("disk") == disk + "" && |
1362 |
filter_list.indexOf(flv) > -1) { return true; } |
1363 |
})[0];
|
1364 |
}, |
1365 |
|
1366 |
get_data: function(lst) { |
1367 |
var data = {'cpu': [], 'mem':[], 'disk':[]}; |
1368 |
|
1369 |
_.each(lst, function(flv) {
|
1370 |
if (data.cpu.indexOf(flv.get("cpu")) == -1) { |
1371 |
data.cpu.push(flv.get("cpu"));
|
1372 |
} |
1373 |
if (data.mem.indexOf(flv.get("ram")) == -1) { |
1374 |
data.mem.push(flv.get("ram"));
|
1375 |
} |
1376 |
if (data.disk.indexOf(flv.get("disk")) == -1) { |
1377 |
data.disk.push(flv.get("disk"));
|
1378 |
} |
1379 |
}) |
1380 |
|
1381 |
return data;
|
1382 |
}, |
1383 |
|
1384 |
active: function() { |
1385 |
return this.filter(function(flv){return flv.get('status') != "DELETED"}); |
1386 |
} |
1387 |
|
1388 |
}) |
1389 |
|
1390 |
models.VMS = models.Collection.extend({ |
1391 |
model: models.VM,
|
1392 |
path: 'servers', |
1393 |
details: true, |
1394 |
copy_image_meta: true, |
1395 |
|
1396 |
parse: function (resp, xhr) { |
1397 |
// FIXME: depricated after refactoring
|
1398 |
var data = resp;
|
1399 |
if (!resp) { return [] }; |
1400 |
data = _.filter(_.map(resp.servers.values, _.bind(this.parse_vm_api_data, this)), function(v){return v}); |
1401 |
return data;
|
1402 |
}, |
1403 |
|
1404 |
get_reboot_required: function() { |
1405 |
return this.filter(function(vm){return vm.get("reboot_required") == true}) |
1406 |
}, |
1407 |
|
1408 |
has_pending_actions: function() { |
1409 |
return this.filter(function(vm){return vm.pending_action}).length > 0; |
1410 |
}, |
1411 |
|
1412 |
reset_pending_actions: function() { |
1413 |
this.each(function(vm) { |
1414 |
vm.clear_pending_action(); |
1415 |
}) |
1416 |
}, |
1417 |
|
1418 |
stop_stats_update: function(exclude) { |
1419 |
var exclude = exclude || [];
|
1420 |
this.each(function(vm) { |
1421 |
if (exclude.indexOf(vm) > -1) { |
1422 |
return;
|
1423 |
} |
1424 |
vm.stop_stats_update(); |
1425 |
}) |
1426 |
}, |
1427 |
|
1428 |
has_meta: function(vm_data) { |
1429 |
return vm_data.metadata && vm_data.metadata.values
|
1430 |
}, |
1431 |
|
1432 |
has_addresses: function(vm_data) { |
1433 |
return vm_data.metadata && vm_data.metadata.values
|
1434 |
}, |
1435 |
|
1436 |
parse_vm_api_data: function(data) { |
1437 |
// do not add non existing DELETED entries
|
1438 |
if (data.status && data.status == "DELETED") { |
1439 |
if (!this.get(data.id)) { |
1440 |
console.error("non exising deleted vm", data)
|
1441 |
return false; |
1442 |
} |
1443 |
} |
1444 |
|
1445 |
// OS attribute
|
1446 |
if (this.has_meta(data)) { |
1447 |
data['OS'] = data.metadata.values.OS || "okeanos"; |
1448 |
} |
1449 |
|
1450 |
data['firewalls'] = {};
|
1451 |
if (data['addresses'] && data['addresses'].values) { |
1452 |
data['linked_to_nets'] = data['addresses'].values; |
1453 |
_.each(data['addresses'].values, function(f){ |
1454 |
if (f['firewallProfile']) { |
1455 |
data['firewalls'][f['id']] = f['firewallProfile'] |
1456 |
} |
1457 |
}); |
1458 |
} |
1459 |
|
1460 |
// if vm has no metadata, no metadata object
|
1461 |
// is in json response, reset it to force
|
1462 |
// value update
|
1463 |
if (!data['metadata']) { |
1464 |
data['metadata'] = {values:{}}; |
1465 |
} |
1466 |
|
1467 |
return data;
|
1468 |
}, |
1469 |
|
1470 |
create: function (name, image, flavor, meta, extra, callback) { |
1471 |
if (this.copy_image_meta) { |
1472 |
meta['OS'] = image.get("OS"); |
1473 |
} |
1474 |
|
1475 |
opts = {name: name, imageRef: image.id, flavorRef: flavor.id, metadata:meta} |
1476 |
opts = _.extend(opts, extra); |
1477 |
|
1478 |
this.api.call(this.path, "create", {'server': opts}, undefined, undefined, callback, {critical: false}); |
1479 |
} |
1480 |
|
1481 |
}) |
1482 |
|
1483 |
|
1484 |
// storage initialization
|
1485 |
snf.storage.images = new models.Images();
|
1486 |
snf.storage.flavors = new models.Flavors();
|
1487 |
snf.storage.networks = new models.Networks();
|
1488 |
snf.storage.vms = new models.VMS();
|
1489 |
|
1490 |
//snf.storage.vms.fetch({update:true});
|
1491 |
//snf.storage.images.fetch({update:true});
|
1492 |
//snf.storage.flavors.fetch({update:true});
|
1493 |
|
1494 |
})(this);
|