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