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