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