root / snf-cyclades-app / synnefo / ui / static / snf / js / models.js @ 5acb8b8a
History | View | Annotate | Download (86.9 kB)
1 |
// Copyright 2011 GRNET S.A. All rights reserved.
|
---|---|
2 |
//
|
3 |
// Redistribution and use in source and binary forms, with or
|
4 |
// without modification, are permitted provided that the following
|
5 |
// conditions are met:
|
6 |
//
|
7 |
// 1. Redistributions of source code must retain the above
|
8 |
// copyright notice, this list of conditions and the following
|
9 |
// disclaimer.
|
10 |
//
|
11 |
// 2. Redistributions in binary form must reproduce the above
|
12 |
// copyright notice, this list of conditions and the following
|
13 |
// disclaimer in the documentation and/or other materials
|
14 |
// provided with the distribution.
|
15 |
//
|
16 |
// THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
// AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
// POSSIBILITY OF SUCH DAMAGE.
|
28 |
//
|
29 |
// The views and conclusions contained in the software and
|
30 |
// documentation are those of the authors and should not be
|
31 |
// interpreted as representing official policies, either expressed
|
32 |
// or implied, of GRNET S.A.
|
33 |
//
|
34 |
|
35 |
;(function(root){
|
36 |
|
37 |
// root
|
38 |
var root = root;
|
39 |
|
40 |
// setup namepsaces
|
41 |
var snf = root.synnefo = root.synnefo || {};
|
42 |
var models = snf.models = snf.models || {}
|
43 |
var storage = snf.storage = snf.storage || {};
|
44 |
var util = snf.util = snf.util || {};
|
45 |
|
46 |
// shortcuts
|
47 |
var bb = root.Backbone;
|
48 |
var slice = Array.prototype.slice
|
49 |
|
50 |
// logging
|
51 |
var logger = new snf.logging.logger("SNF-MODELS"); |
52 |
var debug = _.bind(logger.debug, logger);
|
53 |
|
54 |
// get url helper
|
55 |
var getUrl = function(baseurl) { |
56 |
var baseurl = baseurl || snf.config.api_urls[this.api_type]; |
57 |
var append = "/"; |
58 |
if (baseurl.split("").reverse()[0] == "/") { |
59 |
append = "";
|
60 |
} |
61 |
return baseurl + append + this.path; |
62 |
} |
63 |
|
64 |
// i18n
|
65 |
BUILDING_MESSAGES = window.BUILDING_MESSAGES || {'INIT': 'init', 'COPY': '{0}, {1}, {2}', 'FINAL': 'final'}; |
66 |
|
67 |
// Base object for all our models
|
68 |
models.Model = bb.Model.extend({ |
69 |
sync: snf.api.sync,
|
70 |
api: snf.api,
|
71 |
api_type: 'compute', |
72 |
has_status: false, |
73 |
auto_bind: [],
|
74 |
|
75 |
|
76 |
initialize: function() { |
77 |
var self = this; |
78 |
|
79 |
this._proxy_model_cache = {};
|
80 |
_.each(this.auto_bind, function(fname) { |
81 |
self[fname] = _.bind(self[fname], self); |
82 |
}); |
83 |
|
84 |
if (this.has_status) { |
85 |
this.bind("change:status", this.handle_remove); |
86 |
this.handle_remove();
|
87 |
} |
88 |
|
89 |
this.api_call = _.bind(this.api.call, this); |
90 |
|
91 |
if (this.proxy_attrs) { |
92 |
this.init_proxy_attrs();
|
93 |
} |
94 |
|
95 |
if (this.storage_attrs) { |
96 |
this.init_storage_attrs();
|
97 |
} |
98 |
|
99 |
if (this.model_actions) { |
100 |
this.init_model_actions();
|
101 |
} |
102 |
|
103 |
models.Model.__super__.initialize.apply(this, arguments); |
104 |
|
105 |
}, |
106 |
|
107 |
// Initialize model actions object
|
108 |
// For each entry in model's model_action object register the relevant
|
109 |
// model proxy `can_<actionname>` attributes.
|
110 |
init_model_actions: function() { |
111 |
var actions = _.keys(this.model_actions); |
112 |
this.set({
|
113 |
"actions": new models._ActionsModel({}, { |
114 |
actions: actions,
|
115 |
model: this |
116 |
}) |
117 |
}); |
118 |
this.actions = this.get("actions"); |
119 |
this.actions.bind("set-pending", function(action) { |
120 |
this.trigger("action:set-pending", action, this.actions, this); |
121 |
}, this);
|
122 |
this.actions.bind("unset-pending", function(action) { |
123 |
this.trigger("action:unset-pending", action, this.actions, this); |
124 |
}, this);
|
125 |
this.actions.bind("reset-pending", function() { |
126 |
this.trigger("action:reset-pending", this.actions, this); |
127 |
}, this);
|
128 |
|
129 |
_.each(this.model_actions, function(params, key){ |
130 |
var attr = 'can_' + key; |
131 |
if (params.length == 0) { return } |
132 |
var deps = params[0]; |
133 |
var cb = _.bind(params[1], this); |
134 |
_.each(deps, function(dep) {
|
135 |
this._set_proxy_attr(attr, dep, cb);
|
136 |
}, this);
|
137 |
}, this);
|
138 |
}, |
139 |
|
140 |
// Initialize proxy storage model attributes. These attribues allows
|
141 |
// us to automatically access cross collection associated objects.
|
142 |
init_storage_attrs: function() { |
143 |
_.each(this.storage_attrs, function(params, attr) { |
144 |
var store, key, attr_name;
|
145 |
store = synnefo.storage[params[0]];
|
146 |
key = params[1];
|
147 |
attr_resolver = params[2];
|
148 |
if (!attr_resolver) {
|
149 |
attr_resolver = function(model, attr) { |
150 |
return model.get(attr);
|
151 |
} |
152 |
} |
153 |
attr_name = attr; |
154 |
|
155 |
var resolve_related_instance = function(storage, attr_name, val) { |
156 |
var data = {};
|
157 |
|
158 |
if (!val) {
|
159 |
// update with undefined and return
|
160 |
data[key] = undefined;
|
161 |
this.set(data);
|
162 |
return;
|
163 |
}; |
164 |
|
165 |
// retrieve related object (check if its a Model??)
|
166 |
var obj = store.get(val);
|
167 |
|
168 |
if (obj) {
|
169 |
// set related object
|
170 |
data[attr_name] = obj; |
171 |
this.set(data, {silent:true}) |
172 |
this.trigger("change:" + attr_name, obj); |
173 |
} else {
|
174 |
var self = this; |
175 |
var retry_to_resolve = function(store, val, key) { |
176 |
var retries = 0; |
177 |
var retry = window.setInterval(function(){ |
178 |
retries++; |
179 |
if (retries > 200) { |
180 |
clearInterval(retry); |
181 |
} |
182 |
var obj = store.get(val);
|
183 |
if (obj) {
|
184 |
data[key] = obj; |
185 |
self.set(data, {silent:false}); |
186 |
clearInterval(retry); |
187 |
} |
188 |
}, 500);
|
189 |
return retry
|
190 |
} |
191 |
retry_to_resolve(store, val, key); |
192 |
} |
193 |
} |
194 |
|
195 |
var self = this; |
196 |
function init_bindings(instance, store, key, attr, attr_resolver) { |
197 |
instance.bind('change:' + attr, function(model) { |
198 |
resolve_related_instance.call(model, store, key, attr_resolver(model, attr)); |
199 |
}, this);
|
200 |
|
201 |
instance.bind('add', function(model) { |
202 |
resolve_related_instance.call(model, store, key, attr_resolver(model, attr)); |
203 |
}, this);
|
204 |
} |
205 |
|
206 |
init_bindings(this, store, key, attr, attr_resolver);
|
207 |
resolve_related_instance.call(this, store, key, attr_resolver(this, attr)); |
208 |
}, this);
|
209 |
}, |
210 |
|
211 |
_proxy_model_cache: {},
|
212 |
|
213 |
_bind_model: function(model, attr, check_attr, cb) { |
214 |
var proxy_cache_key = attr + '_' + check_attr; |
215 |
if (this._proxy_model_cache[proxy_cache_key]) { |
216 |
var proxy = this._proxy_model_cache[proxy_cache_key]; |
217 |
proxy[0].unbind('change', proxy[1]); |
218 |
} |
219 |
var data = {};
|
220 |
var changebind = _.bind(function() { |
221 |
data[attr] = cb.call(this, this.get(check_attr)); |
222 |
this.set(data);
|
223 |
}, this);
|
224 |
model.bind('change', changebind);
|
225 |
this._proxy_model_cache[proxy_cache_key] = [model, changebind];
|
226 |
}, |
227 |
|
228 |
_bind_attr: function(attr, check_attr, cb) { |
229 |
this.bind('change:' + check_attr, function() { |
230 |
if (this.get(check_attr) instanceof models.Model) { |
231 |
var model = this.get(check_attr); |
232 |
this._bind_model(model, attr, check_attr, cb);
|
233 |
} |
234 |
var val = cb.call(this, this.get(check_attr)); |
235 |
var data = {};
|
236 |
if (this.get(attr) !== val) { |
237 |
data[attr] = val; |
238 |
this.set(data);
|
239 |
} |
240 |
}, this);
|
241 |
}, |
242 |
|
243 |
_set_proxy_attr: function(attr, check_attr, cb) { |
244 |
// initial set
|
245 |
var data = {};
|
246 |
data[attr] = cb.call(this, this.get(check_attr)); |
247 |
if (data[attr] !== undefined) { |
248 |
this.set(data, {silent:true}); |
249 |
} |
250 |
if(this.get(check_attr) instanceof models.Model) { |
251 |
this._bind_model(this.get(check_attr), attr, check_attr, cb); |
252 |
} |
253 |
this._bind_attr(attr, check_attr, cb);
|
254 |
}, |
255 |
|
256 |
init_proxy_attrs: function() { |
257 |
_.each(this.proxy_attrs, function(opts, attr){ |
258 |
var cb = opts[1]; |
259 |
_.each(opts[0], function(check_attr){ |
260 |
this._set_proxy_attr(attr, check_attr, cb)
|
261 |
}, this);
|
262 |
}, this);
|
263 |
}, |
264 |
|
265 |
handle_remove: function() { |
266 |
if (this.get("status") == 'DELETED') { |
267 |
if (this.collection) { |
268 |
try { this.clear_pending_action();} catch (err) {}; |
269 |
try { this.reset_pending_actions();} catch (err) {}; |
270 |
try { this.stop_stats_update();} catch (err) {}; |
271 |
this.collection.remove(this.id); |
272 |
} |
273 |
} |
274 |
}, |
275 |
|
276 |
// custom set method to allow submodels to use
|
277 |
// set_<attr> methods for handling the value of each
|
278 |
// attribute and overriding the default set method
|
279 |
// for specific parameters
|
280 |
set: function(params, options) { |
281 |
_.each(params, _.bind(function(value, key){
|
282 |
if (this["set_" + key]) { |
283 |
params[key] = this["set_" + key](value); |
284 |
} |
285 |
}, this))
|
286 |
var ret = bb.Model.prototype.set.call(this, params, options); |
287 |
return ret;
|
288 |
}, |
289 |
|
290 |
url: function(options) { |
291 |
return getUrl.call(this, this.base_url) + "/" + this.id; |
292 |
}, |
293 |
|
294 |
api_path: function(options) { |
295 |
return this.path + "/" + this.id; |
296 |
}, |
297 |
|
298 |
parse: function(resp, xhr) { |
299 |
}, |
300 |
|
301 |
remove: function(complete, error, success) { |
302 |
this.api_call(this.api_path(), "delete", undefined, complete, error, success); |
303 |
}, |
304 |
|
305 |
changedKeys: function() { |
306 |
return _.keys(this.changedAttributes() || {}); |
307 |
}, |
308 |
|
309 |
// return list of changed attributes that included in passed list
|
310 |
// argument
|
311 |
getKeysChanged: function(keys) { |
312 |
return _.intersection(keys, this.changedKeys()); |
313 |
}, |
314 |
|
315 |
// boolean check of keys changed
|
316 |
keysChanged: function(keys) { |
317 |
return this.getKeysChanged(keys).length > 0; |
318 |
}, |
319 |
|
320 |
// check if any of the passed attribues has changed
|
321 |
hasOnlyChange: function(keys) { |
322 |
var ret = false; |
323 |
_.each(keys, _.bind(function(key) {
|
324 |
if (this.changedKeys().length == 1 && this.changedKeys().indexOf(key) > -1) { ret = true}; |
325 |
}, this));
|
326 |
return ret;
|
327 |
} |
328 |
|
329 |
}) |
330 |
|
331 |
// Base object for all our model collections
|
332 |
models.Collection = bb.Collection.extend({ |
333 |
sync: snf.api.sync,
|
334 |
api: snf.api,
|
335 |
api_type: 'compute', |
336 |
supportIncUpdates: true, |
337 |
|
338 |
initialize: function() { |
339 |
models.Collection.__super__.initialize.apply(this, arguments); |
340 |
this.api_call = _.bind(this.api.call, this); |
341 |
if (this.sortFields) { |
342 |
_.each(this.sortFields, function(f) { |
343 |
this.bind("change:" + f, _.bind(this.resort, this)); |
344 |
}, this);
|
345 |
} |
346 |
}, |
347 |
|
348 |
resort: function() { |
349 |
this.sort();
|
350 |
}, |
351 |
|
352 |
url: function(options, method) { |
353 |
return getUrl.call(this, this.base_url) + ( |
354 |
options.details || this.details && method != 'create' ? '/detail' : ''); |
355 |
}, |
356 |
|
357 |
fetch: function(options) { |
358 |
if (!options) { options = {} };
|
359 |
// default to update
|
360 |
if (!this.noUpdate) { |
361 |
if (options.update === undefined) { options.update = true }; |
362 |
if (!options.removeMissing && options.refresh) {
|
363 |
options.removeMissing = true;
|
364 |
}; |
365 |
// for collections which associated models don't support
|
366 |
// deleted state identification through attributes, resolve
|
367 |
// deleted entries by checking for missing objects in fetch
|
368 |
// responses.
|
369 |
if (this.updateEntries && options.removeMissing === undefined) { |
370 |
options.removeMissing = true;
|
371 |
} |
372 |
} else {
|
373 |
if (options.refresh === undefined) { |
374 |
options.refresh = true;
|
375 |
if (this.updateEntries) { |
376 |
options.update = true;
|
377 |
options.removeMissing = true;
|
378 |
} |
379 |
} |
380 |
} |
381 |
// custom event foreach fetch
|
382 |
return bb.Collection.prototype.fetch.call(this, options) |
383 |
}, |
384 |
|
385 |
create: function(model, options) { |
386 |
var coll = this; |
387 |
options || (options = {}); |
388 |
model = this._prepareModel(model, options);
|
389 |
if (!model) return false; |
390 |
var success = options.success;
|
391 |
options.success = function(nextModel, resp, xhr) { |
392 |
if (coll.add_on_create) {
|
393 |
coll.add(nextModel, options); |
394 |
} |
395 |
if (success) success(nextModel, resp, xhr);
|
396 |
}; |
397 |
model.save(null, options);
|
398 |
return model;
|
399 |
}, |
400 |
|
401 |
get_fetcher: function(interval, increase, fast, increase_after_calls, max, initial_call, params) { |
402 |
var fetch_params = params || {};
|
403 |
var handler_options = {};
|
404 |
|
405 |
fetch_params.skips_timeouts = true;
|
406 |
handler_options.interval = interval; |
407 |
handler_options.increase = increase; |
408 |
handler_options.fast = fast; |
409 |
handler_options.increase_after_calls = increase_after_calls; |
410 |
handler_options.max= max; |
411 |
handler_options.id = "collection id";
|
412 |
|
413 |
var last_ajax = undefined; |
414 |
var callback = _.bind(function() { |
415 |
// clone to avoid referenced objects
|
416 |
var params = _.clone(fetch_params);
|
417 |
updater._ajax = last_ajax; |
418 |
|
419 |
// wait for previous request to finish
|
420 |
if (last_ajax && last_ajax.readyState < 4 && last_ajax.statusText != "timeout") { |
421 |
// opera readystate for 304 responses is 0
|
422 |
if (!($.browser.opera && last_ajax.readyState == 0 && last_ajax.status == 304)) { |
423 |
return;
|
424 |
} |
425 |
} |
426 |
last_ajax = this.fetch(params);
|
427 |
}, this);
|
428 |
handler_options.callback = callback; |
429 |
|
430 |
var updater = new snf.api.updateHandler(_.clone(_.extend(handler_options, fetch_params))); |
431 |
snf.api.bind("call", _.throttle(_.bind(function(){ updater.faster(true)}, this)), 1000); |
432 |
return updater;
|
433 |
} |
434 |
}); |
435 |
|
436 |
// Image model
|
437 |
models.Image = models.Model.extend({ |
438 |
path: 'images', |
439 |
|
440 |
get_size: function() { |
441 |
return parseInt(this.get('metadata') ? this.get('metadata').size : -1) |
442 |
}, |
443 |
|
444 |
get_description: function(escape) { |
445 |
if (escape == undefined) { escape = true }; |
446 |
if (escape) { return this.escape('description') || "No description available"} |
447 |
return this.get('description') || "No description available." |
448 |
}, |
449 |
|
450 |
get_meta: function(key) { |
451 |
if (this.get('metadata') && this.get('metadata')) { |
452 |
if (!this.get('metadata')[key]) { return null } |
453 |
return _.escape(this.get('metadata')[key]); |
454 |
} else {
|
455 |
return null; |
456 |
} |
457 |
}, |
458 |
|
459 |
get_meta_keys: function() { |
460 |
if (this.get('metadata') && this.get('metadata')) { |
461 |
return _.keys(this.get('metadata')); |
462 |
} else {
|
463 |
return [];
|
464 |
} |
465 |
}, |
466 |
|
467 |
get_owner: function() { |
468 |
return this.get('owner') || _.keys(synnefo.config.system_images_owners)[0]; |
469 |
}, |
470 |
|
471 |
get_owner_uuid: function() { |
472 |
return this.get('owner_uuid'); |
473 |
}, |
474 |
|
475 |
is_system_image: function() { |
476 |
var owner = this.get_owner(); |
477 |
return _.include(_.keys(synnefo.config.system_images_owners), owner)
|
478 |
}, |
479 |
|
480 |
owned_by: function(user) { |
481 |
if (!user) { user = synnefo.user }
|
482 |
return user.get_username() == this.get('owner_uuid'); |
483 |
}, |
484 |
|
485 |
display_owner: function() { |
486 |
var owner = this.get_owner(); |
487 |
if (_.include(_.keys(synnefo.config.system_images_owners), owner)) {
|
488 |
return synnefo.config.system_images_owners[owner];
|
489 |
} else {
|
490 |
return owner;
|
491 |
} |
492 |
}, |
493 |
|
494 |
get_readable_size: function() { |
495 |
if (this.is_deleted()) { |
496 |
return synnefo.config.image_deleted_size_title || '(none)'; |
497 |
} |
498 |
return this.get_size() > 0 ? util.readablizeBytes(this.get_size() * 1024 * 1024) : '(none)'; |
499 |
}, |
500 |
|
501 |
get_os: function() { |
502 |
return this.get_meta('OS'); |
503 |
}, |
504 |
|
505 |
get_gui: function() { |
506 |
return this.get_meta('GUI'); |
507 |
}, |
508 |
|
509 |
get_created_users: function() { |
510 |
try {
|
511 |
var users = this.get_meta('users').split(" "); |
512 |
} catch (err) { users = null } |
513 |
if (!users) {
|
514 |
var osfamily = this.get_meta('osfamily'); |
515 |
if (osfamily == 'windows') { |
516 |
users = ['Administrator'];
|
517 |
} else {
|
518 |
users = ['root'];
|
519 |
} |
520 |
} |
521 |
return users;
|
522 |
}, |
523 |
|
524 |
get_sort_order: function() { |
525 |
return parseInt(this.get('metadata') ? this.get('metadata').sortorder : -1) |
526 |
}, |
527 |
|
528 |
get_vm: function() { |
529 |
var vm_id = this.get("serverRef"); |
530 |
var vm = undefined; |
531 |
vm = storage.vms.get(vm_id); |
532 |
return vm;
|
533 |
}, |
534 |
|
535 |
is_public: function() { |
536 |
return this.get('is_public') == undefined ? true : this.get('is_public'); |
537 |
}, |
538 |
|
539 |
is_deleted: function() { |
540 |
return this.get('status') == "DELETED" |
541 |
}, |
542 |
|
543 |
ssh_keys_paths: function() { |
544 |
return _.map(this.get_created_users(), function(username) { |
545 |
prepend = '';
|
546 |
if (username != 'root') { |
547 |
prepend = '/home'
|
548 |
} |
549 |
return {'user': username, 'path': '{1}/{0}/.ssh/authorized_keys'.format(username, |
550 |
prepend)}; |
551 |
}); |
552 |
}, |
553 |
|
554 |
_supports_ssh: function() { |
555 |
var os_list = synnefo.config.support_ssh_os_list;
|
556 |
var osfamily_list = synnefo.config.support_ssh_os_family_list;
|
557 |
|
558 |
var os = this.get_os(); |
559 |
if (os_list.indexOf(os) > -1) { |
560 |
return true; |
561 |
} |
562 |
|
563 |
var osfamily = this.get_meta("osfamily"); |
564 |
if (osfamily_list.indexOf(osfamily) > -1) { |
565 |
return true |
566 |
} |
567 |
|
568 |
return false; |
569 |
}, |
570 |
|
571 |
supports: function(feature) { |
572 |
if (feature == "ssh") { |
573 |
return this._supports_ssh() |
574 |
} |
575 |
return false; |
576 |
}, |
577 |
|
578 |
personality_data_for_keys: function(keys) { |
579 |
return _.map(this.ssh_keys_paths(), function(pathinfo) { |
580 |
var contents = ''; |
581 |
_.each(keys, function(key){
|
582 |
contents = contents + key.get("content") + "\n" |
583 |
}); |
584 |
contents = $.base64.encode(contents);
|
585 |
|
586 |
return {
|
587 |
path: pathinfo.path,
|
588 |
contents: contents,
|
589 |
mode: 0600, |
590 |
owner: pathinfo.user
|
591 |
} |
592 |
}); |
593 |
} |
594 |
}); |
595 |
|
596 |
// Flavor model
|
597 |
models.Flavor = models.Model.extend({ |
598 |
path: 'flavors', |
599 |
|
600 |
details_string: function() { |
601 |
return "{0} CPU, {1}MB, {2}GB".format(this.get('cpu'), this.get('ram'), this.get('disk')); |
602 |
}, |
603 |
|
604 |
get_disk_size: function() { |
605 |
return parseInt(this.get("disk") * 1024) |
606 |
}, |
607 |
|
608 |
get_ram_size: function() { |
609 |
return parseInt(this.get("ram")) |
610 |
}, |
611 |
|
612 |
get_disk_template_info: function() { |
613 |
var info = snf.config.flavors_disk_templates_info[this.get("disk_template")]; |
614 |
if (!info) {
|
615 |
info = { name: this.get("disk_template"), description:'' }; |
616 |
} |
617 |
return info
|
618 |
}, |
619 |
|
620 |
disk_to_bytes: function() { |
621 |
return parseInt(this.get("disk")) * 1024 * 1024 * 1024; |
622 |
}, |
623 |
|
624 |
ram_to_bytes: function() { |
625 |
return parseInt(this.get("ram")) * 1024 * 1024; |
626 |
}, |
627 |
|
628 |
}); |
629 |
|
630 |
models.ParamsList = function(){this.initialize.apply(this, arguments)}; |
631 |
_.extend(models.ParamsList.prototype, bb.Events, { |
632 |
|
633 |
initialize: function(parent, param_name) { |
634 |
this.parent = parent;
|
635 |
this.actions = {};
|
636 |
this.param_name = param_name;
|
637 |
this.length = 0; |
638 |
}, |
639 |
|
640 |
has_action: function(action) { |
641 |
return this.actions[action] ? true : false; |
642 |
}, |
643 |
|
644 |
_parse_params: function(arguments) { |
645 |
if (arguments.length <= 1) { |
646 |
return [];
|
647 |
} |
648 |
|
649 |
var args = _.toArray(arguments); |
650 |
return args.splice(1); |
651 |
}, |
652 |
|
653 |
contains: function(action, params) { |
654 |
params = this._parse_params(arguments); |
655 |
var has_action = this.has_action(action); |
656 |
if (!has_action) { return false }; |
657 |
|
658 |
var paramsEqual = false; |
659 |
_.each(this.actions[action], function(action_params) { |
660 |
if (_.isEqual(action_params, params)) {
|
661 |
paramsEqual = true;
|
662 |
} |
663 |
}); |
664 |
|
665 |
return paramsEqual;
|
666 |
}, |
667 |
|
668 |
is_empty: function() { |
669 |
return _.isEmpty(this.actions); |
670 |
}, |
671 |
|
672 |
add: function(action, params) { |
673 |
params = this._parse_params(arguments); |
674 |
if (this.contains.apply(this, arguments)) { return this }; |
675 |
var isnew = false |
676 |
if (!this.has_action(action)) { |
677 |
this.actions[action] = [];
|
678 |
isnew = true;
|
679 |
}; |
680 |
|
681 |
this.actions[action].push(params);
|
682 |
this.parent.trigger("change:" + this.param_name, this.parent, this); |
683 |
if (isnew) {
|
684 |
this.trigger("add", action, params); |
685 |
} else {
|
686 |
this.trigger("change", action, params); |
687 |
} |
688 |
return this; |
689 |
}, |
690 |
|
691 |
remove_all: function(action) { |
692 |
if (this.has_action(action)) { |
693 |
delete this.actions[action]; |
694 |
this.parent.trigger("change:" + this.param_name, this.parent, this); |
695 |
this.trigger("remove", action); |
696 |
} |
697 |
return this; |
698 |
}, |
699 |
|
700 |
reset: function() { |
701 |
this.actions = {};
|
702 |
this.parent.trigger("change:" + this.param_name, this.parent, this); |
703 |
this.trigger("reset"); |
704 |
this.trigger("remove"); |
705 |
}, |
706 |
|
707 |
remove: function(action, params) { |
708 |
params = this._parse_params(arguments); |
709 |
if (!this.has_action(action)) { return this }; |
710 |
var index = -1; |
711 |
_.each(this.actions[action], _.bind(function(action_params) { |
712 |
if (_.isEqual(action_params, params)) {
|
713 |
index = this.actions[action].indexOf(action_params);
|
714 |
} |
715 |
}, this));
|
716 |
|
717 |
if (index > -1) { |
718 |
this.actions[action].splice(index, 1); |
719 |
if (_.isEmpty(this.actions[action])) { |
720 |
delete this.actions[action]; |
721 |
} |
722 |
this.parent.trigger("change:" + this.param_name, this.parent, this); |
723 |
this.trigger("remove", action, params); |
724 |
} |
725 |
} |
726 |
|
727 |
}); |
728 |
|
729 |
// Virtualmachine model
|
730 |
models.VM = models.Model.extend({ |
731 |
|
732 |
path: 'servers', |
733 |
has_status: true, |
734 |
proxy_attrs: {
|
735 |
'busy': [
|
736 |
['status', 'state'], function() { |
737 |
return !_.contains(['ACTIVE', 'STOPPED'], this.get('status')); |
738 |
} |
739 |
], |
740 |
'in_progress': [
|
741 |
['status', 'state'], function() { |
742 |
return this.in_transition(); |
743 |
} |
744 |
] |
745 |
}, |
746 |
|
747 |
initialize: function(params) { |
748 |
var self = this; |
749 |
this.ports = new Backbone.FilteredCollection(undefined, { |
750 |
collection: synnefo.storage.ports,
|
751 |
collectionFilter: function(m) { |
752 |
return self.id == m.get('device_id') |
753 |
}}); |
754 |
|
755 |
this.pending_firewalls = {};
|
756 |
|
757 |
models.VM.__super__.initialize.apply(this, arguments); |
758 |
|
759 |
|
760 |
this.set({state: params.status || "ERROR"}); |
761 |
this.log = new snf.logging.logger("VM " + this.id); |
762 |
this.pending_action = undefined; |
763 |
|
764 |
// init stats parameter
|
765 |
this.set({'stats': undefined}, {silent: true}); |
766 |
// defaults to not update the stats
|
767 |
// each view should handle this vm attribute
|
768 |
// depending on if it displays stat images or not
|
769 |
this.do_update_stats = false; |
770 |
|
771 |
// interval time
|
772 |
// this will dynamicaly change if the server responds that
|
773 |
// images get refreshed on different intervals
|
774 |
this.stats_update_interval = synnefo.config.STATS_INTERVAL || 5000; |
775 |
this.stats_available = false; |
776 |
|
777 |
// initialize interval
|
778 |
this.init_stats_intervals(this.stats_update_interval); |
779 |
|
780 |
// handle progress message on instance change
|
781 |
this.bind("change", _.bind(this.update_status_message, this)); |
782 |
this.bind("change:task_state", _.bind(this.update_status, this)); |
783 |
// force update of progress message
|
784 |
this.update_status_message(true); |
785 |
|
786 |
// default values
|
787 |
this.bind("change:state", _.bind(function(){ |
788 |
if (this.state() == "DESTROY") { |
789 |
this.handle_destroy()
|
790 |
} |
791 |
}, this));
|
792 |
|
793 |
}, |
794 |
|
795 |
get_public_ips: function() { |
796 |
var ips = [];
|
797 |
this.ports.filter(function(port) { |
798 |
if (port.get('network') && !port.get('network').get('is_public')) { return } |
799 |
if (!port.get("ips")) { return } |
800 |
port.get("ips").each(function(ip) { |
801 |
ips.push(ip); |
802 |
}); |
803 |
}); |
804 |
return ips;
|
805 |
}, |
806 |
|
807 |
has_public_ip: function() { |
808 |
return this.ports.filter(function(port) { |
809 |
return port.get("network") && |
810 |
port.get("network").get("is_public") && |
811 |
port.get("ips").length > 0; |
812 |
}).length > 0;
|
813 |
}, |
814 |
|
815 |
has_public_ipv6: function() { |
816 |
return this.has_ip_version("v6", true); |
817 |
}, |
818 |
|
819 |
has_public_ipv4: function() { |
820 |
return this.has_ip_version("v4", true); |
821 |
}, |
822 |
|
823 |
has_ip_version: function(ver, public) { |
824 |
var found = false; |
825 |
this.ports.each(function(port) { |
826 |
if (found) { return } |
827 |
if (public !== undefined) { |
828 |
if (port.get("network") && |
829 |
port.get("network").get("is_public") != public) { |
830 |
return
|
831 |
} |
832 |
} |
833 |
port.get('ips').each(function(ip) { |
834 |
if (found) { return } |
835 |
if (ip.get("type") == ver) { |
836 |
found = true
|
837 |
} |
838 |
}) |
839 |
}, this)
|
840 |
return found;
|
841 |
}, |
842 |
|
843 |
status: function(st) { |
844 |
if (!st) { return this.get("status")} |
845 |
return this.set({status:st}); |
846 |
}, |
847 |
|
848 |
update_status: function() { |
849 |
this.set_status(this.get('status')); |
850 |
}, |
851 |
|
852 |
set_status: function(st) { |
853 |
var new_state = this.state_for_api_status(st); |
854 |
var transition = false; |
855 |
|
856 |
if (this.state() != new_state) { |
857 |
if (models.VM.STATES_TRANSITIONS[this.state()]) { |
858 |
transition = this.state();
|
859 |
} |
860 |
} |
861 |
|
862 |
// call it silently to avoid double change trigger
|
863 |
var state = this.state_for_api_status(st); |
864 |
this.set({'state': state}, {silent: true}); |
865 |
|
866 |
// trigger transition
|
867 |
if (transition && models.VM.TRANSITION_STATES.indexOf(new_state) == -1) { |
868 |
this.trigger("transition", {from:transition, to:new_state}) |
869 |
}; |
870 |
return st;
|
871 |
}, |
872 |
|
873 |
get_diagnostics: function(success) { |
874 |
this.__make_api_call(this.get_diagnostics_url(), |
875 |
"read", // create so that sync later uses POST to make the call |
876 |
null, // payload |
877 |
function(data) {
|
878 |
success(data); |
879 |
}, |
880 |
null, 'diagnostics'); |
881 |
}, |
882 |
|
883 |
has_diagnostics: function() { |
884 |
return this.get("diagnostics") && this.get("diagnostics").length; |
885 |
}, |
886 |
|
887 |
get_progress_info: function() { |
888 |
// details about progress message
|
889 |
// contains a list of diagnostic messages
|
890 |
return this.get("status_messages"); |
891 |
}, |
892 |
|
893 |
get_status_message: function() { |
894 |
return this.get('status_message'); |
895 |
}, |
896 |
|
897 |
// extract status message from diagnostics
|
898 |
status_message_from_diagnostics: function(diagnostics) { |
899 |
var valid_sources_map = synnefo.config.diagnostics_status_messages_map;
|
900 |
var valid_sources = valid_sources_map[this.get('status')]; |
901 |
if (!valid_sources) { return null }; |
902 |
|
903 |
// filter messsages based on diagnostic source
|
904 |
var messages = _.filter(diagnostics, function(diag) { |
905 |
return valid_sources.indexOf(diag.source) > -1; |
906 |
}); |
907 |
|
908 |
var msg = messages[0]; |
909 |
if (msg) {
|
910 |
var message = msg.message;
|
911 |
var message_tpl = snf.config.diagnostic_messages_tpls[msg.source];
|
912 |
|
913 |
if (message_tpl) {
|
914 |
message = message_tpl.replace('MESSAGE', msg.message);
|
915 |
} |
916 |
return message;
|
917 |
} |
918 |
|
919 |
// no message to display, but vm in build state, display
|
920 |
// finalizing message.
|
921 |
if (this.is_building() == 'BUILD') { |
922 |
return synnefo.config.BUILDING_MESSAGES['FINAL']; |
923 |
} |
924 |
return null; |
925 |
}, |
926 |
|
927 |
update_status_message: function(force) { |
928 |
// update only if one of the specified attributes has changed
|
929 |
if (
|
930 |
!this.keysChanged(['diagnostics', 'progress', 'status', 'state']) |
931 |
&& !force |
932 |
) { return };
|
933 |
|
934 |
// if user requested to destroy the vm set the appropriate
|
935 |
// message.
|
936 |
if (this.get('state') == "DESTROY") { |
937 |
message = "Terminating..."
|
938 |
this.set({status_message: message}) |
939 |
return;
|
940 |
} |
941 |
|
942 |
// set error message, if vm has diagnostic message display it as
|
943 |
// progress message
|
944 |
if (this.in_error_state()) { |
945 |
var d = this.get('diagnostics'); |
946 |
if (d && d.length) {
|
947 |
var message = this.status_message_from_diagnostics(d); |
948 |
this.set({status_message: message}); |
949 |
} else {
|
950 |
this.set({status_message: null}); |
951 |
} |
952 |
return;
|
953 |
} |
954 |
|
955 |
// identify building status message
|
956 |
if (this.is_building()) { |
957 |
var self = this; |
958 |
var success = function(msg) { |
959 |
self.set({status_message: msg});
|
960 |
} |
961 |
this.get_building_status_message(success);
|
962 |
return;
|
963 |
} |
964 |
|
965 |
this.set({status_message:null}); |
966 |
}, |
967 |
|
968 |
// get building status message. Asynchronous function since it requires
|
969 |
// access to vm image.
|
970 |
get_building_status_message: function(callback) { |
971 |
// no progress is set, vm is in initial build status
|
972 |
var progress = this.get("progress"); |
973 |
if (progress == 0 || !progress) { |
974 |
return callback(BUILDING_MESSAGES['INIT']); |
975 |
} |
976 |
|
977 |
// vm has copy progress, display copy percentage
|
978 |
if (progress > 0 && progress <= 99) { |
979 |
this.get_copy_details(true, undefined, _.bind( |
980 |
function(details){
|
981 |
callback(BUILDING_MESSAGES['COPY'].format(details.copy,
|
982 |
details.size, |
983 |
details.progress)); |
984 |
}, this));
|
985 |
return;
|
986 |
} |
987 |
|
988 |
// copy finished display FINAL message or identify status message
|
989 |
// from diagnostics.
|
990 |
if (progress >= 100) { |
991 |
if (!this.has_diagnostics()) { |
992 |
callback(BUILDING_MESSAGES['FINAL']);
|
993 |
} else {
|
994 |
var d = this.get("diagnostics"); |
995 |
var msg = this.status_message_from_diagnostics(d); |
996 |
if (msg) {
|
997 |
callback(msg); |
998 |
} |
999 |
} |
1000 |
} |
1001 |
}, |
1002 |
|
1003 |
get_copy_details: function(human, image, callback) { |
1004 |
var human = human || false; |
1005 |
var image = image || this.get_image(_.bind(function(image){ |
1006 |
var progress = this.get('progress'); |
1007 |
var size = image.get_size();
|
1008 |
var size_copied = (size * progress / 100).toFixed(2); |
1009 |
|
1010 |
if (human) {
|
1011 |
size = util.readablizeBytes(size*1024*1024); |
1012 |
size_copied = util.readablizeBytes(size_copied*1024*1024); |
1013 |
} |
1014 |
|
1015 |
callback({'progress': progress, 'size': size, 'copy': size_copied}) |
1016 |
}, this));
|
1017 |
}, |
1018 |
|
1019 |
start_stats_update: function(force_if_empty) { |
1020 |
var prev_state = this.do_update_stats; |
1021 |
|
1022 |
this.do_update_stats = true; |
1023 |
|
1024 |
// fetcher initialized ??
|
1025 |
if (!this.stats_fetcher) { |
1026 |
this.init_stats_intervals();
|
1027 |
} |
1028 |
|
1029 |
|
1030 |
// fetcher running ???
|
1031 |
if (!this.stats_fetcher.running || !prev_state) { |
1032 |
this.stats_fetcher.start();
|
1033 |
} |
1034 |
|
1035 |
if (force_if_empty && this.get("stats") == undefined) { |
1036 |
this.update_stats(true); |
1037 |
} |
1038 |
}, |
1039 |
|
1040 |
stop_stats_update: function(stop_calls) { |
1041 |
this.do_update_stats = false; |
1042 |
|
1043 |
if (stop_calls) {
|
1044 |
this.stats_fetcher.stop();
|
1045 |
} |
1046 |
}, |
1047 |
|
1048 |
// clear and reinitialize update interval
|
1049 |
init_stats_intervals: function (interval) { |
1050 |
this.stats_fetcher = this.get_stats_fetcher(this.stats_update_interval); |
1051 |
this.stats_fetcher.start();
|
1052 |
}, |
1053 |
|
1054 |
get_stats_fetcher: function(timeout) { |
1055 |
var cb = _.bind(function(data){ |
1056 |
this.update_stats();
|
1057 |
}, this);
|
1058 |
var fetcher = new snf.api.updateHandler({'callback': cb, interval: timeout, id:'stats'}); |
1059 |
return fetcher;
|
1060 |
}, |
1061 |
|
1062 |
// do the api call
|
1063 |
update_stats: function(force) { |
1064 |
// do not update stats if flag not set
|
1065 |
if ((!this.do_update_stats && !force) || this.updating_stats) { |
1066 |
return;
|
1067 |
} |
1068 |
|
1069 |
// make the api call, execute handle_stats_update on sucess
|
1070 |
// TODO: onError handler ???
|
1071 |
stats_url = this.url() + "/stats"; |
1072 |
this.updating_stats = true; |
1073 |
this.sync("read", this, { |
1074 |
handles_error:true, |
1075 |
url: stats_url,
|
1076 |
refresh:true, |
1077 |
success: _.bind(this.handle_stats_update, this), |
1078 |
error: _.bind(this.handle_stats_error, this), |
1079 |
complete: _.bind(function(){this.updating_stats = false;}, this), |
1080 |
critical: false, |
1081 |
log_error: false, |
1082 |
skips_timeouts: true |
1083 |
}); |
1084 |
}, |
1085 |
|
1086 |
get_attachment: function(id) { |
1087 |
var attachment = undefined; |
1088 |
_.each(this.get("attachments"), function(a) { |
1089 |
if (a.id == id) {
|
1090 |
attachment = a; |
1091 |
} |
1092 |
}); |
1093 |
return attachment
|
1094 |
}, |
1095 |
|
1096 |
_set_stats: function(stats) { |
1097 |
var silent = silent === undefined ? false : silent; |
1098 |
// unavailable stats while building
|
1099 |
if (this.get("status") == "BUILD") { |
1100 |
this.stats_available = false; |
1101 |
} else { this.stats_available = true; } |
1102 |
|
1103 |
if (this.get("status") == "DESTROY") { this.stats_available = false; } |
1104 |
|
1105 |
this.set({stats: stats}, {silent:true}); |
1106 |
this.trigger("stats:update", stats); |
1107 |
}, |
1108 |
|
1109 |
unbind: function() { |
1110 |
models.VM.__super__.unbind.apply(this, arguments); |
1111 |
}, |
1112 |
|
1113 |
can_start: function(flv, count_current) { |
1114 |
var get_quota = function(key) { |
1115 |
return synnefo.storage.quotas.get(key).get('available'); |
1116 |
} |
1117 |
var flavor = flv || this.get_flavor(); |
1118 |
var vm_ram_current = 0, vm_cpu_current = 0; |
1119 |
if (flv && this.is_active() || flv && count_current) { |
1120 |
var current = this.get_flavor(); |
1121 |
vm_ram_current = current.ram_to_bytes(); |
1122 |
vm_cpu_current = parseInt(current.get('cpu'));
|
1123 |
} |
1124 |
var vm_ram = flavor.ram_to_bytes();
|
1125 |
var vm_cpu = parseInt(flavor.get('cpu')); |
1126 |
var available_cpu = get_quota('cyclades.cpu') + vm_cpu_current; |
1127 |
var available_ram = get_quota('cyclades.ram') + vm_ram_current; |
1128 |
if (vm_ram > available_ram || vm_cpu > available_cpu) { return false } |
1129 |
return true |
1130 |
}, |
1131 |
|
1132 |
can_connect: function() { |
1133 |
if (!synnefo.config.hotplug_enabled && this.is_active()) { return false } |
1134 |
return _.contains(["ACTIVE", "STOPPED"], this.get("status")) && |
1135 |
!this.get('suspended') |
1136 |
}, |
1137 |
|
1138 |
can_disconnect: function() { |
1139 |
return _.contains(["ACTIVE", "STOPPED"], this.get("status")) |
1140 |
}, |
1141 |
|
1142 |
can_resize: function() { |
1143 |
return this.get('status') == 'STOPPED'; |
1144 |
}, |
1145 |
|
1146 |
handle_stats_error: function() { |
1147 |
stats = {}; |
1148 |
_.each(['cpuBar', 'cpuTimeSeries', 'netBar', 'netTimeSeries'], function(k) { |
1149 |
stats[k] = false;
|
1150 |
}); |
1151 |
|
1152 |
this.set({'stats': stats}); |
1153 |
}, |
1154 |
|
1155 |
// this method gets executed after a successful vm stats api call
|
1156 |
handle_stats_update: function(data) { |
1157 |
var self = this; |
1158 |
// avoid browser caching
|
1159 |
|
1160 |
if (data.stats && _.size(data.stats) > 0) { |
1161 |
var ts = $.now(); |
1162 |
var stats = data.stats;
|
1163 |
var images_loaded = 0; |
1164 |
var images = {};
|
1165 |
|
1166 |
function check_images_loaded() { |
1167 |
images_loaded++; |
1168 |
|
1169 |
if (images_loaded == 4) { |
1170 |
self._set_stats(images); |
1171 |
} |
1172 |
} |
1173 |
_.each(['cpuBar', 'cpuTimeSeries', 'netBar', 'netTimeSeries'], function(k) { |
1174 |
|
1175 |
stats[k] = stats[k] + "?_=" + ts;
|
1176 |
|
1177 |
var stat = k.slice(0,3); |
1178 |
var type = k.slice(3,6) == "Bar" ? "bar" : "time"; |
1179 |
var img = $("<img />"); |
1180 |
var val = stats[k];
|
1181 |
|
1182 |
// load stat image to a temporary dom element
|
1183 |
// update model stats on image load/error events
|
1184 |
img.load(function() {
|
1185 |
images[k] = val; |
1186 |
check_images_loaded(); |
1187 |
}); |
1188 |
|
1189 |
img.error(function() {
|
1190 |
images[stat + type] = false;
|
1191 |
check_images_loaded(); |
1192 |
}); |
1193 |
|
1194 |
img.attr({'src': stats[k]});
|
1195 |
}) |
1196 |
data.stats = stats; |
1197 |
} |
1198 |
|
1199 |
// do we need to change the interval ??
|
1200 |
if (data.stats.refresh * 1000 != this.stats_update_interval) { |
1201 |
this.stats_update_interval = data.stats.refresh * 1000; |
1202 |
this.stats_fetcher.interval = this.stats_update_interval; |
1203 |
this.stats_fetcher.maximum_interval = this.stats_update_interval; |
1204 |
this.stats_fetcher.stop();
|
1205 |
this.stats_fetcher.start(false); |
1206 |
} |
1207 |
}, |
1208 |
|
1209 |
// helper method that sets the do_update_stats
|
1210 |
// in the future this method could also make an api call
|
1211 |
// immediaetly if needed
|
1212 |
enable_stats_update: function() { |
1213 |
this.do_update_stats = true; |
1214 |
}, |
1215 |
|
1216 |
handle_destroy: function() { |
1217 |
this.stats_fetcher.stop();
|
1218 |
}, |
1219 |
|
1220 |
require_reboot: function() { |
1221 |
if (this.is_active()) { |
1222 |
this.set({'reboot_required': true}); |
1223 |
} |
1224 |
}, |
1225 |
|
1226 |
set_pending_action: function(data) { |
1227 |
this.pending_action = data;
|
1228 |
return data;
|
1229 |
}, |
1230 |
|
1231 |
// machine has pending action
|
1232 |
update_pending_action: function(action, force) { |
1233 |
this.set({pending_action: action}); |
1234 |
}, |
1235 |
|
1236 |
clear_pending_action: function() { |
1237 |
this.set({pending_action: undefined}); |
1238 |
}, |
1239 |
|
1240 |
has_pending_action: function() { |
1241 |
return this.get("pending_action") ? this.get("pending_action") : false; |
1242 |
}, |
1243 |
|
1244 |
// machine is active
|
1245 |
is_active: function() { |
1246 |
return models.VM.ACTIVE_STATES.indexOf(this.state()) > -1; |
1247 |
}, |
1248 |
|
1249 |
// machine is building
|
1250 |
is_building: function() { |
1251 |
return models.VM.BUILDING_STATES.indexOf(this.state()) > -1; |
1252 |
}, |
1253 |
|
1254 |
is_rebooting: function() { |
1255 |
return this.state() == 'REBOOT'; |
1256 |
}, |
1257 |
|
1258 |
in_error_state: function() { |
1259 |
return this.state() === "ERROR" |
1260 |
}, |
1261 |
|
1262 |
// user can connect to machine
|
1263 |
is_connectable: function() { |
1264 |
return models.VM.CONNECT_STATES.indexOf(this.state()) > -1; |
1265 |
}, |
1266 |
|
1267 |
remove_meta: function(key, complete, error) { |
1268 |
var url = this.api_path() + "/metadata/" + key; |
1269 |
this.api_call(url, "delete", undefined, complete, error); |
1270 |
}, |
1271 |
|
1272 |
save_meta: function(meta, complete, error) { |
1273 |
var url = this.api_path() + "/metadata/" + meta.key; |
1274 |
var payload = {meta:{}}; |
1275 |
payload.meta[meta.key] = meta.value; |
1276 |
payload._options = { |
1277 |
critical:false, |
1278 |
error_params: {
|
1279 |
title: "Machine metadata error", |
1280 |
extra_details: {"Machine id": this.id} |
1281 |
}}; |
1282 |
|
1283 |
this.api_call(url, "update", payload, complete, error); |
1284 |
}, |
1285 |
|
1286 |
|
1287 |
// update/get the state of the machine
|
1288 |
state: function() { |
1289 |
var args = slice.call(arguments); |
1290 |
|
1291 |
if (args.length > 0 && models.VM.STATES.indexOf(args[0]) > -1) { |
1292 |
this.set({'state': args[0]}); |
1293 |
} |
1294 |
|
1295 |
return this.get('state'); |
1296 |
}, |
1297 |
|
1298 |
// get the state that the api status corresponds to
|
1299 |
state_for_api_status: function(status) { |
1300 |
return this.state_transition(this.state(), status); |
1301 |
}, |
1302 |
|
1303 |
// get transition state for the corresponging api status
|
1304 |
state_transition: function(state, new_status) { |
1305 |
var statuses = models.VM.STATES_TRANSITIONS[state];
|
1306 |
if (statuses) {
|
1307 |
if (statuses.indexOf(new_status) > -1) { |
1308 |
return new_status;
|
1309 |
} else {
|
1310 |
return state;
|
1311 |
} |
1312 |
} else {
|
1313 |
return new_status;
|
1314 |
} |
1315 |
}, |
1316 |
|
1317 |
// the current vm state is a transition state
|
1318 |
in_transition: function() { |
1319 |
return models.VM.TRANSITION_STATES.indexOf(this.state()) > -1 || |
1320 |
models.VM.TRANSITION_STATES.indexOf(this.get('status')) > -1; |
1321 |
}, |
1322 |
|
1323 |
// get image object
|
1324 |
get_image: function(callback) { |
1325 |
if (callback == undefined) { callback = function(){} } |
1326 |
var image = storage.images.get(this.get('image')); |
1327 |
if (!image) {
|
1328 |
storage.images.update_unknown_id(this.get('image'), callback); |
1329 |
return;
|
1330 |
} |
1331 |
callback(image); |
1332 |
return image;
|
1333 |
}, |
1334 |
|
1335 |
// get flavor object
|
1336 |
get_flavor: function() { |
1337 |
var flv = storage.flavors.get(this.get('flavor')); |
1338 |
if (!flv) {
|
1339 |
storage.flavors.update_unknown_id(this.get('flavor')); |
1340 |
flv = storage.flavors.get(this.get('flavor')); |
1341 |
} |
1342 |
return flv;
|
1343 |
}, |
1344 |
|
1345 |
get_resize_flavors: function() { |
1346 |
var vm_flavor = this.get_flavor(); |
1347 |
var flavors = synnefo.storage.flavors.filter(function(f){ |
1348 |
return f.get('disk_template') == |
1349 |
vm_flavor.get('disk_template') && f.get('disk') == |
1350 |
vm_flavor.get('disk');
|
1351 |
}); |
1352 |
return flavors;
|
1353 |
}, |
1354 |
|
1355 |
get_flavor_quotas: function() { |
1356 |
var flavor = this.get_flavor(); |
1357 |
return {
|
1358 |
cpu: flavor.get('cpu'), |
1359 |
ram: flavor.get_ram_size(),
|
1360 |
disk:flavor.get_disk_size()
|
1361 |
} |
1362 |
}, |
1363 |
|
1364 |
get_meta: function(key, deflt) { |
1365 |
if (this.get('metadata') && this.get('metadata')) { |
1366 |
if (!this.get('metadata')[key]) { return deflt } |
1367 |
return _.escape(this.get('metadata')[key]); |
1368 |
} else {
|
1369 |
return deflt;
|
1370 |
} |
1371 |
}, |
1372 |
|
1373 |
get_meta_keys: function() { |
1374 |
if (this.get('metadata') && this.get('metadata')) { |
1375 |
return _.keys(this.get('metadata')); |
1376 |
} else {
|
1377 |
return [];
|
1378 |
} |
1379 |
}, |
1380 |
|
1381 |
// get metadata OS value
|
1382 |
get_os: function() { |
1383 |
var image = this.get_image(); |
1384 |
return this.get_meta('OS') || (image ? |
1385 |
image.get_os() || "okeanos" : "okeanos"); |
1386 |
}, |
1387 |
|
1388 |
get_gui: function() { |
1389 |
return this.get_meta('GUI'); |
1390 |
}, |
1391 |
|
1392 |
get_hostname: function() { |
1393 |
return this.get_meta('hostname') || this.get('fqdn') || synnefo.config.no_fqdn_message; |
1394 |
}, |
1395 |
|
1396 |
// get actions that the user can execute
|
1397 |
// depending on the vm state/status
|
1398 |
get_available_actions: function() { |
1399 |
return models.VM.AVAILABLE_ACTIONS[this.state()]; |
1400 |
}, |
1401 |
|
1402 |
set_profile: function(profile, net_id) { |
1403 |
}, |
1404 |
|
1405 |
// call rename api
|
1406 |
rename: function(new_name) { |
1407 |
//this.set({'name': new_name});
|
1408 |
this.sync("update", this, { |
1409 |
critical: true, |
1410 |
data: {
|
1411 |
'server': {
|
1412 |
'name': new_name
|
1413 |
} |
1414 |
}, |
1415 |
success: _.bind(function(){ |
1416 |
snf.api.trigger("call");
|
1417 |
}, this)
|
1418 |
}); |
1419 |
}, |
1420 |
|
1421 |
get_console_url: function(data) { |
1422 |
var url_params = {
|
1423 |
machine: this.get("name"), |
1424 |
host_ip: this.get_hostname(), |
1425 |
host_ip_v6: this.get_hostname(), |
1426 |
host: data.host,
|
1427 |
port: data.port,
|
1428 |
password: data.password
|
1429 |
} |
1430 |
return synnefo.config.ui_console_url + '?' + $.param(url_params); |
1431 |
}, |
1432 |
|
1433 |
set_firewall: function(nic, value, success_cb, error_cb) { |
1434 |
var self = this; |
1435 |
var success = function() { self.require_reboot(); success_cb() } |
1436 |
var error = function() { error_cb() } |
1437 |
var data = {'nic': nic.id, 'profile': value, 'display': true}; |
1438 |
var url = this.url() + "/action"; |
1439 |
//var params = {skip_api_error: false, display: true};
|
1440 |
this.call('firewallProfile', success, error, data); |
1441 |
}, |
1442 |
|
1443 |
connect_floating_ip: function(ip, cb) { |
1444 |
this.set({'status': 'CONNECTING'}); |
1445 |
synnefo.storage.ports.create({ |
1446 |
port: {
|
1447 |
network_id: ip.get('floating_network_id'), |
1448 |
device_id: this.id, |
1449 |
fixed_ips: [{'ip_address': ip.get('floating_ip_address')}] |
1450 |
} |
1451 |
}, {complete: cb, skip_api_error: false}) |
1452 |
}, |
1453 |
|
1454 |
// action helper
|
1455 |
call: function(action_name, success, error, params) { |
1456 |
var id_param = [this.id]; |
1457 |
|
1458 |
params = params || {}; |
1459 |
success = success || function() {};
|
1460 |
error = error || function() {};
|
1461 |
|
1462 |
var self = this; |
1463 |
|
1464 |
switch(action_name) {
|
1465 |
case 'start': |
1466 |
this.__make_api_call(this.get_action_url(), // vm actions url |
1467 |
"create", // create so that sync later uses POST to make the call |
1468 |
{start:{}}, // payload |
1469 |
function() {
|
1470 |
// set state after successful call
|
1471 |
self.state("START");
|
1472 |
success.apply(this, arguments); |
1473 |
snf.api.trigger("call");
|
1474 |
}, |
1475 |
error, 'start', params);
|
1476 |
break;
|
1477 |
case 'reboot': |
1478 |
this.__make_api_call(this.get_action_url(), // vm actions url |
1479 |
"create", // create so that sync later uses POST to make the call |
1480 |
{reboot:{}}, // payload |
1481 |
function() {
|
1482 |
// set state after successful call
|
1483 |
self.state("REBOOT");
|
1484 |
success.apply(this, arguments) |
1485 |
snf.api.trigger("call");
|
1486 |
self.set({'reboot_required': false}); |
1487 |
}, |
1488 |
error, 'reboot', params);
|
1489 |
break;
|
1490 |
case 'shutdown': |
1491 |
this.__make_api_call(this.get_action_url(), // vm actions url |
1492 |
"create", // create so that sync later uses POST to make the call |
1493 |
{shutdown:{}}, // payload |
1494 |
function() {
|
1495 |
// set state after successful call
|
1496 |
self.state("SHUTDOWN");
|
1497 |
success.apply(this, arguments) |
1498 |
snf.api.trigger("call");
|
1499 |
}, |
1500 |
error, 'shutdown', params);
|
1501 |
break;
|
1502 |
case 'console': |
1503 |
this.__make_api_call(this.url() + "/action", "create", |
1504 |
{'console': {'type':'vnc'}}, |
1505 |
function(data) {
|
1506 |
var cons_data = data.console;
|
1507 |
success.apply(this, [cons_data]);
|
1508 |
}, undefined, 'console', params) |
1509 |
break;
|
1510 |
case 'destroy': |
1511 |
this.__make_api_call(this.url(), // vm actions url |
1512 |
"delete", // create so that sync later uses POST to make the call |
1513 |
undefined, // payload |
1514 |
function() {
|
1515 |
// set state after successful call
|
1516 |
self.state('DESTROY');
|
1517 |
success.apply(this, arguments); |
1518 |
synnefo.storage.quotas.get('cyclades.vm').decrease();
|
1519 |
|
1520 |
}, |
1521 |
error, 'destroy', params);
|
1522 |
break;
|
1523 |
case 'resize': |
1524 |
this.__make_api_call(this.get_action_url(), // vm actions url |
1525 |
"create", // create so that sync later uses POST to make the call |
1526 |
{resize: {flavorRef:params.flavor}}, // payload |
1527 |
function() {
|
1528 |
self.state('RESIZE');
|
1529 |
success.apply(this, arguments); |
1530 |
snf.api.trigger("call");
|
1531 |
}, |
1532 |
error, 'resize', params);
|
1533 |
break;
|
1534 |
case 'destroy': |
1535 |
this.__make_api_call(this.url(), // vm actions url |
1536 |
"delete", // create so that sync later uses POST to make the call |
1537 |
undefined, // payload |
1538 |
function() {
|
1539 |
// set state after successful call
|
1540 |
self.state('DESTROY');
|
1541 |
success.apply(this, arguments); |
1542 |
synnefo.storage.quotas.get('cyclades.vm').decrease();
|
1543 |
|
1544 |
}, |
1545 |
error, 'destroy', params);
|
1546 |
break;
|
1547 |
case 'firewallProfile': |
1548 |
this.__make_api_call(this.get_action_url(), // vm actions url |
1549 |
"create",
|
1550 |
{firewallProfile:{nic:params.nic, profile:params.profile}}, // payload |
1551 |
function() {
|
1552 |
success.apply(this, arguments); |
1553 |
snf.api.trigger("call");
|
1554 |
}, |
1555 |
error, 'start', params);
|
1556 |
break;
|
1557 |
default:
|
1558 |
throw "Invalid VM action ("+action_name+")"; |
1559 |
} |
1560 |
}, |
1561 |
|
1562 |
__make_api_call: function(url, method, data, success, error, action, |
1563 |
extra_params) { |
1564 |
var self = this; |
1565 |
error = error || function(){};
|
1566 |
success = success || function(){};
|
1567 |
|
1568 |
var params = {
|
1569 |
url: url,
|
1570 |
data: data,
|
1571 |
success: function() { |
1572 |
self.handle_action_succeed.apply(self, arguments);
|
1573 |
success.apply(this, arguments) |
1574 |
}, |
1575 |
error: function() { |
1576 |
self.handle_action_fail.apply(self, arguments);
|
1577 |
error.apply(this, arguments) |
1578 |
}, |
1579 |
error_params: { ns: "Machines actions", |
1580 |
title: "'" + this.get("name") + "'" + " " + action + " failed", |
1581 |
extra_details: {
|
1582 |
'Machine ID': this.id, |
1583 |
'URL': url,
|
1584 |
'Action': action || "undefined" }, |
1585 |
allow_reload: false |
1586 |
}, |
1587 |
display: false, |
1588 |
critical: false |
1589 |
} |
1590 |
_.extend(params, extra_params); |
1591 |
this.sync(method, this, params); |
1592 |
}, |
1593 |
|
1594 |
handle_action_succeed: function() { |
1595 |
this.trigger("action:success", arguments); |
1596 |
}, |
1597 |
|
1598 |
reset_action_error: function() { |
1599 |
this.action_error = false; |
1600 |
this.trigger("action:fail:reset", this.action_error); |
1601 |
}, |
1602 |
|
1603 |
handle_action_fail: function() { |
1604 |
this.action_error = arguments; |
1605 |
this.trigger("action:fail", arguments); |
1606 |
}, |
1607 |
|
1608 |
get_action_url: function(name) { |
1609 |
return this.url() + "/action"; |
1610 |
}, |
1611 |
|
1612 |
get_diagnostics_url: function() { |
1613 |
return this.url() + "/diagnostics"; |
1614 |
}, |
1615 |
|
1616 |
get_users: function() { |
1617 |
var image;
|
1618 |
var users = [];
|
1619 |
try {
|
1620 |
var users = this.get_meta('users').split(" "); |
1621 |
} catch (err) { users = null } |
1622 |
if (!users) {
|
1623 |
image = this.get_image();
|
1624 |
if (image) {
|
1625 |
users = image.get_created_users(); |
1626 |
} |
1627 |
} |
1628 |
return users;
|
1629 |
}, |
1630 |
|
1631 |
get_connection_info: function(host_os, success, error) { |
1632 |
var url = synnefo.config.ui_connect_url;
|
1633 |
var users = this.get_users(); |
1634 |
|
1635 |
params = { |
1636 |
ip_address: this.get_hostname(), |
1637 |
hostname: this.get_hostname(), |
1638 |
os: this.get_os(), |
1639 |
host_os: host_os,
|
1640 |
ports: JSON.stringify(this.get('SNF:port_forwarding') || {}), |
1641 |
srv: this.id |
1642 |
} |
1643 |
|
1644 |
if (users.length) {
|
1645 |
params['username'] = _.last(users)
|
1646 |
} |
1647 |
|
1648 |
url = url + "?" + $.param(params); |
1649 |
|
1650 |
var ajax = snf.api.sync("read", undefined, { url: url, |
1651 |
error:error,
|
1652 |
success:success,
|
1653 |
handles_error:1}); |
1654 |
} |
1655 |
}); |
1656 |
|
1657 |
models.VM.ACTIONS = [ |
1658 |
'start',
|
1659 |
'shutdown',
|
1660 |
'reboot',
|
1661 |
'console',
|
1662 |
'destroy',
|
1663 |
'resize',
|
1664 |
'snapshot'
|
1665 |
] |
1666 |
|
1667 |
models.VM.TASK_STATE_STATUS_MAP = { |
1668 |
'BULDING': 'BUILD', |
1669 |
'REBOOTING': 'REBOOT', |
1670 |
'STOPPING': 'SHUTDOWN', |
1671 |
'STARTING': 'START', |
1672 |
'RESIZING': 'RESIZE', |
1673 |
'CONNECTING': 'CONNECT', |
1674 |
'DISCONNECTING': 'DISCONNECT', |
1675 |
'DESTROYING': 'DESTROY' |
1676 |
} |
1677 |
|
1678 |
models.VM.AVAILABLE_ACTIONS = { |
1679 |
'UNKNWON' : ['destroy'], |
1680 |
'BUILD' : ['destroy'], |
1681 |
'REBOOT' : ['destroy'], |
1682 |
'STOPPED' : ['start', 'destroy', 'resize', 'snapshot'], |
1683 |
'ACTIVE' : ['shutdown', 'destroy', 'reboot', 'console', 'resize', 'snapshot'], |
1684 |
'ERROR' : ['destroy'], |
1685 |
'DELETED' : ['destroy'], |
1686 |
'DESTROY' : ['destroy'], |
1687 |
'SHUTDOWN' : ['destroy'], |
1688 |
'START' : ['destroy'], |
1689 |
'CONNECT' : ['destroy'], |
1690 |
'DISCONNECT' : ['destroy'], |
1691 |
'RESIZE' : ['destroy'] |
1692 |
} |
1693 |
|
1694 |
models.VM.AVAILABLE_ACTIONS_INACTIVE = {} |
1695 |
|
1696 |
// api status values
|
1697 |
models.VM.STATUSES = [ |
1698 |
'UNKNWON',
|
1699 |
'BUILD',
|
1700 |
'REBOOT',
|
1701 |
'STOPPED',
|
1702 |
'ACTIVE',
|
1703 |
'ERROR',
|
1704 |
'DELETED',
|
1705 |
'RESIZE'
|
1706 |
] |
1707 |
|
1708 |
// api status values
|
1709 |
models.VM.CONNECT_STATES = [ |
1710 |
'ACTIVE',
|
1711 |
'REBOOT',
|
1712 |
'SHUTDOWN'
|
1713 |
] |
1714 |
|
1715 |
// vm states
|
1716 |
models.VM.STATES = models.VM.STATUSES.concat([ |
1717 |
'DESTROY',
|
1718 |
'SHUTDOWN',
|
1719 |
'START',
|
1720 |
'CONNECT',
|
1721 |
'DISCONNECT',
|
1722 |
'FIREWALL',
|
1723 |
'RESIZE'
|
1724 |
]); |
1725 |
|
1726 |
models.VM.STATES_TRANSITIONS = { |
1727 |
'DESTROY' : ['DELETED'], |
1728 |
'SHUTDOWN': ['ERROR', 'STOPPED', 'DESTROY'], |
1729 |
'STOPPED': ['ERROR', 'ACTIVE', 'DESTROY'], |
1730 |
'ACTIVE': ['ERROR', 'STOPPED', 'REBOOT', 'SHUTDOWN', 'DESTROY'], |
1731 |
'START': ['ERROR', 'ACTIVE', 'DESTROY'], |
1732 |
'REBOOT': ['ERROR', 'ACTIVE', 'STOPPED', 'DESTROY'], |
1733 |
'BUILD': ['ERROR', 'ACTIVE', 'DESTROY'], |
1734 |
'RESIZE': ['ERROR', 'STOPPED'] |
1735 |
} |
1736 |
|
1737 |
models.VM.TRANSITION_STATES = [ |
1738 |
'DESTROY',
|
1739 |
'SHUTDOWN',
|
1740 |
'START',
|
1741 |
'REBOOT',
|
1742 |
'BUILD',
|
1743 |
'RESIZE',
|
1744 |
'DISCONNECT',
|
1745 |
'CONNECT'
|
1746 |
] |
1747 |
|
1748 |
models.VM.ACTIVE_STATES = [ |
1749 |
'BUILD', 'REBOOT', 'ACTIVE', |
1750 |
'SHUTDOWN', 'CONNECT', 'DISCONNECT' |
1751 |
] |
1752 |
|
1753 |
models.VM.BUILDING_STATES = [ |
1754 |
'BUILD'
|
1755 |
] |
1756 |
|
1757 |
models.Images = models.Collection.extend({ |
1758 |
model: models.Image,
|
1759 |
path: 'images', |
1760 |
details: true, |
1761 |
noUpdate: true, |
1762 |
supportIncUpdates: false, |
1763 |
meta_keys_as_attrs: ["OS", "description", "kernel", "size", "GUI"], |
1764 |
meta_labels: {},
|
1765 |
read_method: 'read', |
1766 |
|
1767 |
// update collection model with id passed
|
1768 |
// making a direct call to the image
|
1769 |
// api url
|
1770 |
update_unknown_id: function(id, callback) { |
1771 |
var url = getUrl.call(this) + "/" + id; |
1772 |
this.api_call(this.path + "/" + id, this.read_method, { |
1773 |
_options:{
|
1774 |
async:true, |
1775 |
skip_api_error:true} |
1776 |
}, undefined,
|
1777 |
_.bind(function() {
|
1778 |
if (!this.get(id)) { |
1779 |
if (this.fallback_service) { |
1780 |
// if current service has fallback_service attribute set
|
1781 |
// use this service to retrieve the missing image model
|
1782 |
var tmpservice = new this.fallback_service(); |
1783 |
tmpservice.update_unknown_id(id, _.bind(function(img){
|
1784 |
img.attributes.status = "DELETED";
|
1785 |
this.add(img.attributes);
|
1786 |
callback(this.get(id));
|
1787 |
}, this));
|
1788 |
} else {
|
1789 |
var title = synnefo.config.image_deleted_title || 'Deleted'; |
1790 |
// else add a dummy DELETED state image entry
|
1791 |
this.add({id:id, name:title, size:-1, |
1792 |
progress:100, status:"DELETED"}); |
1793 |
callback(this.get(id));
|
1794 |
} |
1795 |
} else {
|
1796 |
callback(this.get(id));
|
1797 |
} |
1798 |
}, this), _.bind(function(image, msg, xhr) { |
1799 |
if (!image) {
|
1800 |
var title = synnefo.config.image_deleted_title || 'Deleted'; |
1801 |
this.add({id:id, name:title, size:-1, |
1802 |
progress:100, status:"DELETED"}); |
1803 |
callback(this.get(id));
|
1804 |
return;
|
1805 |
} |
1806 |
var img_data = this._read_image_from_request(image, msg, xhr); |
1807 |
this.add(img_data);
|
1808 |
callback(this.get(id));
|
1809 |
}, this));
|
1810 |
}, |
1811 |
|
1812 |
_read_image_from_request: function(image, msg, xhr) { |
1813 |
return image.image;
|
1814 |
}, |
1815 |
|
1816 |
parse: function (resp, xhr) { |
1817 |
var parsed = _.map(resp.images, _.bind(this.parse_meta, this)); |
1818 |
parsed = this.fill_owners(parsed);
|
1819 |
return parsed;
|
1820 |
}, |
1821 |
|
1822 |
fill_owners: function(images) { |
1823 |
// do translate uuid->displayname if needed
|
1824 |
// store display name in owner attribute for compatibility
|
1825 |
var uuids = [];
|
1826 |
|
1827 |
var images = _.map(images, function(img, index) { |
1828 |
if (synnefo.config.translate_uuids) {
|
1829 |
uuids.push(img['owner']);
|
1830 |
} |
1831 |
img['owner_uuid'] = img['owner']; |
1832 |
return img;
|
1833 |
}); |
1834 |
|
1835 |
if (uuids.length > 0) { |
1836 |
var handle_results = function(data) { |
1837 |
_.each(images, function (img) {
|
1838 |
img['owner'] = data.uuid_catalog[img['owner_uuid']]; |
1839 |
}); |
1840 |
} |
1841 |
// notice the async false
|
1842 |
var uuid_map = this.translate_uuids(uuids, false, |
1843 |
handle_results) |
1844 |
} |
1845 |
return images;
|
1846 |
}, |
1847 |
|
1848 |
translate_uuids: function(uuids, async, cb) { |
1849 |
var url = synnefo.config.user_catalog_url;
|
1850 |
var data = JSON.stringify({'uuids': uuids}); |
1851 |
|
1852 |
// post to user_catalogs api
|
1853 |
snf.api.sync('create', undefined, { |
1854 |
url: url,
|
1855 |
data: data,
|
1856 |
async: async,
|
1857 |
success: cb
|
1858 |
}); |
1859 |
}, |
1860 |
|
1861 |
get_meta_key: function(img, key) { |
1862 |
if (img.metadata && img.metadata && img.metadata[key]) {
|
1863 |
return _.escape(img.metadata[key]);
|
1864 |
} |
1865 |
return undefined; |
1866 |
}, |
1867 |
|
1868 |
comparator: function(img) { |
1869 |
return -img.get_sort_order("sortorder") || 0; |
1870 |
}, |
1871 |
|
1872 |
parse_meta: function(img) { |
1873 |
_.each(this.meta_keys_as_attrs, _.bind(function(key){ |
1874 |
if (img[key]) { return }; |
1875 |
img[key] = this.get_meta_key(img, key) || ""; |
1876 |
}, this));
|
1877 |
return img;
|
1878 |
}, |
1879 |
|
1880 |
active: function() { |
1881 |
return this.filter(function(img){return img.get('status') != "DELETED"}); |
1882 |
}, |
1883 |
|
1884 |
predefined: function() { |
1885 |
return _.filter(this.active(), function(i) { return !i.get("serverRef")}); |
1886 |
}, |
1887 |
|
1888 |
fetch_for_type: function(type, complete, error) { |
1889 |
this.fetch({update:true, |
1890 |
success: complete,
|
1891 |
error: error,
|
1892 |
skip_api_error: true }); |
1893 |
}, |
1894 |
|
1895 |
get_images_for_type: function(type) { |
1896 |
if (this['get_{0}_images'.format(type)]) { |
1897 |
return this['get_{0}_images'.format(type)](); |
1898 |
} |
1899 |
|
1900 |
return this.active(); |
1901 |
}, |
1902 |
|
1903 |
update_images_for_type: function(type, onStart, onComplete, onError, force_load) { |
1904 |
var load = false; |
1905 |
error = onError || function() {};
|
1906 |
function complete(collection) { |
1907 |
onComplete(collection.get_images_for_type(type)); |
1908 |
} |
1909 |
|
1910 |
// do we need to fetch/update current collection entries
|
1911 |
if (load) {
|
1912 |
onStart(); |
1913 |
this.fetch_for_type(type, complete, error);
|
1914 |
} else {
|
1915 |
// fallback to complete
|
1916 |
complete(this);
|
1917 |
} |
1918 |
} |
1919 |
}) |
1920 |
|
1921 |
models.Flavors = models.Collection.extend({ |
1922 |
model: models.Flavor,
|
1923 |
path: 'flavors', |
1924 |
details: true, |
1925 |
noUpdate: true, |
1926 |
supportIncUpdates: false, |
1927 |
// update collection model with id passed
|
1928 |
// making a direct call to the flavor
|
1929 |
// api url
|
1930 |
update_unknown_id: function(id, callback) { |
1931 |
var url = getUrl.call(this) + "/" + id; |
1932 |
this.api_call(this.path + "/" + id, "read", {_options:{async:false, skip_api_error:true}}, undefined, |
1933 |
_.bind(function() {
|
1934 |
this.add({id:id, cpu:"Unknown", ram:"Unknown", disk:"Unknown", name: "Unknown", status:"DELETED"}) |
1935 |
}, this), _.bind(function(flv) { |
1936 |
if (!flv.flavor.status) { flv.flavor.status = "DELETED" }; |
1937 |
this.add(flv.flavor);
|
1938 |
}, this));
|
1939 |
}, |
1940 |
|
1941 |
parse: function (resp, xhr) { |
1942 |
return _.map(resp.flavors, function(o) { |
1943 |
o.cpu = o['vcpus'];
|
1944 |
o.disk_template = o['SNF:disk_template'];
|
1945 |
return o
|
1946 |
}); |
1947 |
}, |
1948 |
|
1949 |
comparator: function(flv) { |
1950 |
return flv.get("disk") * flv.get("cpu") * flv.get("ram"); |
1951 |
}, |
1952 |
|
1953 |
unavailable_values_for_quotas: function(quotas, flavors, extra) { |
1954 |
var flavors = flavors || this.active(); |
1955 |
var index = {cpu:[], disk:[], ram:[]}; |
1956 |
var extra = extra == undefined ? {cpu:0, disk:0, ram:0} : extra; |
1957 |
|
1958 |
_.each(flavors, function(el) {
|
1959 |
|
1960 |
var disk_available = quotas['disk'] + extra.disk; |
1961 |
var disk_size = el.get_disk_size();
|
1962 |
if (index.disk.indexOf(disk_size) == -1) { |
1963 |
var disk = el.disk_to_bytes();
|
1964 |
if (disk > disk_available) {
|
1965 |
index.disk.push(el.get('disk'));
|
1966 |
} |
1967 |
} |
1968 |
|
1969 |
var ram_available = quotas['ram'] + extra.ram * 1024 * 1024; |
1970 |
var ram_size = el.get_ram_size();
|
1971 |
if (index.ram.indexOf(ram_size) == -1) { |
1972 |
var ram = el.ram_to_bytes();
|
1973 |
if (ram > ram_available) {
|
1974 |
index.ram.push(el.get('ram'))
|
1975 |
} |
1976 |
} |
1977 |
|
1978 |
var cpu = el.get('cpu'); |
1979 |
var cpu_available = quotas['cpu'] + extra.cpu; |
1980 |
if (index.cpu.indexOf(cpu) == -1) { |
1981 |
if (cpu > cpu_available) {
|
1982 |
index.cpu.push(el.get('cpu'))
|
1983 |
} |
1984 |
} |
1985 |
}); |
1986 |
return index;
|
1987 |
}, |
1988 |
|
1989 |
unavailable_values_for_image: function(img, flavors) { |
1990 |
var flavors = flavors || this.active(); |
1991 |
var size = img.get_size();
|
1992 |
|
1993 |
var index = {cpu:[], disk:[], ram:[]}; |
1994 |
|
1995 |
_.each(this.active(), function(el) { |
1996 |
var img_size = size;
|
1997 |
var flv_size = el.get_disk_size();
|
1998 |
if (flv_size < img_size) {
|
1999 |
if (index.disk.indexOf(el.get("disk")) == -1) { |
2000 |
index.disk.push(el.get("disk"));
|
2001 |
} |
2002 |
}; |
2003 |
}); |
2004 |
|
2005 |
return index;
|
2006 |
}, |
2007 |
|
2008 |
get_flavor: function(cpu, mem, disk, disk_template, filter_list) { |
2009 |
if (!filter_list) { filter_list = this.models }; |
2010 |
|
2011 |
return this.select(function(flv){ |
2012 |
if (flv.get("cpu") == cpu + "" && |
2013 |
flv.get("ram") == mem + "" && |
2014 |
flv.get("disk") == disk + "" && |
2015 |
flv.get("disk_template") == disk_template &&
|
2016 |
filter_list.indexOf(flv) > -1) { return true; } |
2017 |
})[0];
|
2018 |
}, |
2019 |
|
2020 |
get_data: function(lst) { |
2021 |
var data = {'cpu': [], 'mem':[], 'disk':[], 'disk_template':[]}; |
2022 |
|
2023 |
_.each(lst, function(flv) {
|
2024 |
if (data.cpu.indexOf(flv.get("cpu")) == -1) { |
2025 |
data.cpu.push(flv.get("cpu"));
|
2026 |
} |
2027 |
if (data.mem.indexOf(flv.get("ram")) == -1) { |
2028 |
data.mem.push(flv.get("ram"));
|
2029 |
} |
2030 |
if (data.disk.indexOf(flv.get("disk")) == -1) { |
2031 |
data.disk.push(flv.get("disk"));
|
2032 |
} |
2033 |
if (data.disk_template.indexOf(flv.get("disk_template")) == -1) { |
2034 |
data.disk_template.push(flv.get("disk_template"));
|
2035 |
} |
2036 |
}) |
2037 |
|
2038 |
return data;
|
2039 |
}, |
2040 |
|
2041 |
active: function() { |
2042 |
return this.filter(function(flv){return flv.get('status') != "DELETED"}); |
2043 |
} |
2044 |
|
2045 |
}) |
2046 |
|
2047 |
models.VMS = models.Collection.extend({ |
2048 |
model: models.VM,
|
2049 |
path: 'servers', |
2050 |
details: true, |
2051 |
copy_image_meta: true, |
2052 |
|
2053 |
parse: function (resp, xhr) { |
2054 |
var data = resp;
|
2055 |
if (!resp) { return [] }; |
2056 |
data = _.filter(_.map(resp.servers, |
2057 |
_.bind(this.parse_vm_api_data, this)), |
2058 |
function(v){return v}); |
2059 |
return data;
|
2060 |
}, |
2061 |
|
2062 |
parse_vm_api_data: function(data) { |
2063 |
// do not add non existing DELETED entries
|
2064 |
if (data.status && data.status == "DELETED") { |
2065 |
if (!this.get(data.id)) { |
2066 |
return false; |
2067 |
} |
2068 |
} |
2069 |
|
2070 |
if ('SNF:task_state' in data) { |
2071 |
data['task_state'] = data['SNF:task_state']; |
2072 |
if (data['task_state']) { |
2073 |
var status = models.VM.TASK_STATE_STATUS_MAP[data['task_state']]; |
2074 |
if (status) { data['status'] = status } |
2075 |
} |
2076 |
} |
2077 |
|
2078 |
// OS attribute
|
2079 |
if (this.has_meta(data)) { |
2080 |
data['OS'] = data.metadata.OS || snf.config.unknown_os;
|
2081 |
} |
2082 |
|
2083 |
if (!data.diagnostics) {
|
2084 |
data.diagnostics = []; |
2085 |
} |
2086 |
|
2087 |
// network metadata
|
2088 |
data['firewalls'] = {};
|
2089 |
data['fqdn'] = data['SNF:fqdn']; |
2090 |
|
2091 |
// if vm has no metadata, no metadata object
|
2092 |
// is in json response, reset it to force
|
2093 |
// value update
|
2094 |
if (!data['metadata']) { |
2095 |
data['metadata'] = {};
|
2096 |
} |
2097 |
|
2098 |
// v2.0 API returns objects
|
2099 |
data.image_obj = data.image; |
2100 |
data.image = data.image_obj.id; |
2101 |
data.flavor_obj = data.flavor; |
2102 |
data.flavor = data.flavor_obj.id; |
2103 |
|
2104 |
return data;
|
2105 |
}, |
2106 |
|
2107 |
get_reboot_required: function() { |
2108 |
return this.filter(function(vm){return vm.get("reboot_required") == true}) |
2109 |
}, |
2110 |
|
2111 |
has_pending_actions: function() { |
2112 |
return this.filter(function(vm){return vm.pending_action}).length > 0; |
2113 |
}, |
2114 |
|
2115 |
reset_pending_actions: function() { |
2116 |
this.each(function(vm) { |
2117 |
vm.clear_pending_action(); |
2118 |
}) |
2119 |
}, |
2120 |
|
2121 |
do_all_pending_actions: function(success, error) { |
2122 |
this.each(function(vm) { |
2123 |
if (vm.has_pending_action()) {
|
2124 |
vm.call(vm.pending_action, success, error); |
2125 |
vm.clear_pending_action(); |
2126 |
} |
2127 |
}) |
2128 |
}, |
2129 |
|
2130 |
do_all_reboots: function(success, error) { |
2131 |
this.each(function(vm) { |
2132 |
if (vm.get("reboot_required")) { |
2133 |
vm.call("reboot", success, error);
|
2134 |
} |
2135 |
}); |
2136 |
}, |
2137 |
|
2138 |
reset_reboot_required: function() { |
2139 |
this.each(function(vm) { |
2140 |
vm.set({'reboot_required': undefined}); |
2141 |
}) |
2142 |
}, |
2143 |
|
2144 |
stop_stats_update: function(exclude) { |
2145 |
var exclude = exclude || [];
|
2146 |
this.each(function(vm) { |
2147 |
if (exclude.indexOf(vm) > -1) { |
2148 |
return;
|
2149 |
} |
2150 |
vm.stop_stats_update(); |
2151 |
}) |
2152 |
}, |
2153 |
|
2154 |
has_meta: function(vm_data) { |
2155 |
return vm_data.metadata && vm_data.metadata
|
2156 |
}, |
2157 |
|
2158 |
has_addresses: function(vm_data) { |
2159 |
return vm_data.metadata && vm_data.metadata
|
2160 |
}, |
2161 |
|
2162 |
create: function (name, image, flavor, meta, extra, callback) { |
2163 |
|
2164 |
if (this.copy_image_meta) { |
2165 |
if (synnefo.config.vm_image_common_metadata) {
|
2166 |
_.each(synnefo.config.vm_image_common_metadata, |
2167 |
function(key){
|
2168 |
if (image.get_meta(key)) {
|
2169 |
meta[key] = image.get_meta(key); |
2170 |
} |
2171 |
}); |
2172 |
} |
2173 |
|
2174 |
if (image.get("OS")) { |
2175 |
meta['OS'] = image.get("OS"); |
2176 |
} |
2177 |
} |
2178 |
|
2179 |
opts = {name: name, imageRef: image.id, flavorRef: flavor.id, |
2180 |
metadata:meta}
|
2181 |
opts = _.extend(opts, extra); |
2182 |
|
2183 |
var cb = function(data) { |
2184 |
synnefo.storage.quotas.get('cyclades.vm').increase();
|
2185 |
callback(data); |
2186 |
} |
2187 |
|
2188 |
this.api_call(this.path, "create", {'server': opts}, undefined, |
2189 |
undefined, cb, {critical: true}); |
2190 |
}, |
2191 |
|
2192 |
load_missing_images: function(callback) { |
2193 |
var missing_ids = [];
|
2194 |
var resolved = 0; |
2195 |
|
2196 |
// fill missing_ids
|
2197 |
this.each(function(el) { |
2198 |
var imgid = el.get("image"); |
2199 |
var existing = synnefo.storage.images.get(imgid);
|
2200 |
if (!existing && missing_ids.indexOf(imgid) == -1) { |
2201 |
missing_ids.push(imgid); |
2202 |
} |
2203 |
}); |
2204 |
var check = function() { |
2205 |
// once all missing ids where resolved continue calling the
|
2206 |
// callback
|
2207 |
resolved++; |
2208 |
if (resolved == missing_ids.length) {
|
2209 |
callback(missing_ids) |
2210 |
} |
2211 |
} |
2212 |
if (missing_ids.length == 0) { |
2213 |
callback(missing_ids); |
2214 |
return;
|
2215 |
} |
2216 |
// start resolving missing image ids
|
2217 |
_(missing_ids).each(function(imgid){
|
2218 |
synnefo.storage.images.update_unknown_id(imgid, check); |
2219 |
}); |
2220 |
}, |
2221 |
|
2222 |
get_connectable: function() { |
2223 |
return storage.vms.filter(function(vm){ |
2224 |
return !vm.in_error_state() && !vm.is_building();
|
2225 |
}); |
2226 |
} |
2227 |
}) |
2228 |
|
2229 |
models.PublicKey = models.Model.extend({ |
2230 |
path: 'keys', |
2231 |
api_type: 'userdata', |
2232 |
detail: false, |
2233 |
model_actions: {
|
2234 |
'remove': [['name'], function() { |
2235 |
return true; |
2236 |
}] |
2237 |
}, |
2238 |
|
2239 |
get_public_key: function() { |
2240 |
return cryptico.publicKeyFromString(this.get("content")); |
2241 |
}, |
2242 |
|
2243 |
get_filename: function() { |
2244 |
return "{0}.pub".format(this.get("name")); |
2245 |
}, |
2246 |
|
2247 |
identify_type: function() { |
2248 |
try {
|
2249 |
var cont = snf.util.validatePublicKey(this.get("content")); |
2250 |
var type = cont.split(" ")[0]; |
2251 |
return synnefo.util.publicKeyTypesMap[type];
|
2252 |
} catch (err) { return false }; |
2253 |
}, |
2254 |
|
2255 |
rename: function(new_name) { |
2256 |
//this.set({'name': new_name});
|
2257 |
this.sync("update", this, { |
2258 |
critical: true, |
2259 |
data: {'name': new_name}, |
2260 |
success: _.bind(function(){ |
2261 |
snf.api.trigger("call");
|
2262 |
}, this)
|
2263 |
}); |
2264 |
}, |
2265 |
|
2266 |
do_remove: function() { |
2267 |
this.actions.reset_pending();
|
2268 |
this.remove(function() { |
2269 |
synnefo.storage.keys.fetch(); |
2270 |
}); |
2271 |
} |
2272 |
}) |
2273 |
|
2274 |
models._ActionsModel = models.Model.extend({ |
2275 |
defaults: { pending: null }, |
2276 |
actions: [],
|
2277 |
status: {
|
2278 |
INACTIVE: 0, |
2279 |
PENDING: 1, |
2280 |
CALLED: 2 |
2281 |
}, |
2282 |
|
2283 |
initialize: function(attrs, opts) { |
2284 |
models._ActionsModel.__super__.initialize.call(this, attrs);
|
2285 |
this.actions = opts.actions;
|
2286 |
this.model = opts.model;
|
2287 |
this.bind("change", function() { |
2288 |
this.set({'pending': this.get_pending()}); |
2289 |
}, this);
|
2290 |
this.clear();
|
2291 |
}, |
2292 |
|
2293 |
_in_status: function(st) { |
2294 |
var actions = null; |
2295 |
_.each(this.attributes, function(status, action){ |
2296 |
if (status == st) {
|
2297 |
if (!actions) {
|
2298 |
actions = [] |
2299 |
} |
2300 |
actions.push(action); |
2301 |
} |
2302 |
}); |
2303 |
return actions;
|
2304 |
}, |
2305 |
|
2306 |
get_pending: function() { |
2307 |
return this._in_status(this.status.PENDING); |
2308 |
}, |
2309 |
|
2310 |
unset_pending_action: function(action) { |
2311 |
var data = {};
|
2312 |
data[action] = this.status.INACTIVE;
|
2313 |
this.set(data);
|
2314 |
this.trigger("unset-pending", action); |
2315 |
}, |
2316 |
|
2317 |
set_pending_action: function(action, reset_pending) { |
2318 |
reset_pending = reset_pending === undefined ? true : reset_pending; |
2319 |
var data = {};
|
2320 |
data[action] = this.status.PENDING;
|
2321 |
if (reset_pending) {
|
2322 |
this.reset_pending();
|
2323 |
} |
2324 |
this.set(data);
|
2325 |
this.trigger("set-pending", action); |
2326 |
}, |
2327 |
|
2328 |
reset_pending: function() { |
2329 |
var data = {};
|
2330 |
_.each(this.actions, function(action) { |
2331 |
data[action] = this.status.INACTIVE;
|
2332 |
}, this);
|
2333 |
this.set(data);
|
2334 |
this.trigger("reset-pending"); |
2335 |
} |
2336 |
}); |
2337 |
|
2338 |
models.PublicPool = models.Model.extend({}); |
2339 |
models.PublicPools = models.Collection.extend({ |
2340 |
model: models.PublicPool,
|
2341 |
path: 'os-floating-ip-pools', |
2342 |
api_type: 'compute', |
2343 |
noUpdate: true, |
2344 |
|
2345 |
parse: function(data) { |
2346 |
return _.map(data.floating_ip_pools, function(pool) { |
2347 |
pool.id = pool.name; |
2348 |
return pool;
|
2349 |
}); |
2350 |
} |
2351 |
}); |
2352 |
|
2353 |
models.PublicKeys = models.Collection.extend({ |
2354 |
model: models.PublicKey,
|
2355 |
details: false, |
2356 |
path: 'keys', |
2357 |
api_type: 'userdata', |
2358 |
noUpdate: true, |
2359 |
updateEntries: true, |
2360 |
|
2361 |
generate_new: function(success, error) { |
2362 |
snf.api.sync('create', undefined, { |
2363 |
url: getUrl.call(this, this.base_url) + "/generate", |
2364 |
success: success,
|
2365 |
error: error,
|
2366 |
skip_api_error: true |
2367 |
}); |
2368 |
}, |
2369 |
|
2370 |
add_crypto_key: function(key, success, error, options) { |
2371 |
var options = options || {};
|
2372 |
var m = new models.PublicKey(); |
2373 |
|
2374 |
// guess a name
|
2375 |
var name_tpl = "my generated public key"; |
2376 |
var name = name_tpl;
|
2377 |
var name_count = 1; |
2378 |
|
2379 |
while(this.filter(function(m){ return m.get("name") == name }).length > 0) { |
2380 |
name = name_tpl + " " + name_count;
|
2381 |
name_count++; |
2382 |
} |
2383 |
|
2384 |
m.set({name: name});
|
2385 |
m.set({content: key});
|
2386 |
|
2387 |
options.success = function () { return success(m) }; |
2388 |
options.errror = error; |
2389 |
options.skip_api_error = true;
|
2390 |
|
2391 |
this.create(m.attributes, options);
|
2392 |
} |
2393 |
}); |
2394 |
|
2395 |
|
2396 |
models.Quota = models.Model.extend({ |
2397 |
|
2398 |
initialize: function() { |
2399 |
models.Quota.__super__.initialize.apply(this, arguments); |
2400 |
this.bind("change", this.check, this); |
2401 |
this.check();
|
2402 |
}, |
2403 |
|
2404 |
check: function() { |
2405 |
var usage, limit;
|
2406 |
usage = this.get('usage'); |
2407 |
limit = this.get('limit'); |
2408 |
if (usage >= limit) {
|
2409 |
this.trigger("available"); |
2410 |
} else {
|
2411 |
this.trigger("unavailable"); |
2412 |
} |
2413 |
}, |
2414 |
|
2415 |
increase: function(val) { |
2416 |
if (val === undefined) { val = 1}; |
2417 |
this.set({'usage': this.get('usage') + val}) |
2418 |
}, |
2419 |
|
2420 |
decrease: function(val) { |
2421 |
if (val === undefined) { val = 1}; |
2422 |
this.set({'usage': this.get('usage') - val}) |
2423 |
}, |
2424 |
|
2425 |
can_consume: function() { |
2426 |
var usage, limit;
|
2427 |
usage = this.get('usage'); |
2428 |
limit = this.get('limit'); |
2429 |
if (usage >= limit) {
|
2430 |
return false |
2431 |
} else {
|
2432 |
return true |
2433 |
} |
2434 |
}, |
2435 |
|
2436 |
is_bytes: function() { |
2437 |
return this.get('resource').get('unit') == 'bytes'; |
2438 |
}, |
2439 |
|
2440 |
get_available: function(active) { |
2441 |
suffix = '';
|
2442 |
if (active) { suffix = '_active'} |
2443 |
var value = this.get('limit'+suffix) - this.get('usage'+suffix); |
2444 |
if (active) {
|
2445 |
if (this.get('available') <= value) { |
2446 |
value = this.get('available'); |
2447 |
} |
2448 |
} |
2449 |
if (value < 0) { return value } |
2450 |
return value
|
2451 |
}, |
2452 |
|
2453 |
get_readable: function(key, active) { |
2454 |
var value;
|
2455 |
if (key == 'available') { |
2456 |
value = this.get_available(active);
|
2457 |
} else {
|
2458 |
value = this.get(key)
|
2459 |
} |
2460 |
if (value <= 0) { value = 0 } |
2461 |
// greater than max js int (assume infinite quota)
|
2462 |
if (value > Math.pow(2, 53)) { |
2463 |
return "Infinite" |
2464 |
} |
2465 |
|
2466 |
if (!this.is_bytes()) { |
2467 |
return value + ""; |
2468 |
} |
2469 |
|
2470 |
return snf.util.readablizeBytes(value);
|
2471 |
} |
2472 |
}); |
2473 |
|
2474 |
models.Quotas = models.Collection.extend({ |
2475 |
model: models.Quota,
|
2476 |
api_type: 'accounts', |
2477 |
path: 'quotas', |
2478 |
parse: function(resp) { |
2479 |
filtered = _.map(resp.system, function(value, key) {
|
2480 |
var available = (value.limit - value.usage) || 0; |
2481 |
var available_active = available;
|
2482 |
var keysplit = key.split("."); |
2483 |
var limit_active = value.limit;
|
2484 |
var usage_active = value.usage;
|
2485 |
keysplit[keysplit.length-1] = "total_" + keysplit[keysplit.length-1]; |
2486 |
var activekey = keysplit.join("."); |
2487 |
var exists = resp.system[activekey];
|
2488 |
if (exists) {
|
2489 |
available_active = exists.limit - exists.usage; |
2490 |
limit_active = exists.limit; |
2491 |
usage_active = exists.usage; |
2492 |
} |
2493 |
return _.extend(value, {'name': key, 'id': key, |
2494 |
'available': available,
|
2495 |
'available_active': available_active,
|
2496 |
'limit_active': limit_active,
|
2497 |
'usage_active': usage_active,
|
2498 |
'resource': snf.storage.resources.get(key)});
|
2499 |
}); |
2500 |
return filtered;
|
2501 |
}, |
2502 |
|
2503 |
get_by_id: function(k) { |
2504 |
return this.filter(function(q) { return q.get('name') == k})[0] |
2505 |
}, |
2506 |
|
2507 |
get_available_for_vm: function(options) { |
2508 |
var quotas = synnefo.storage.quotas;
|
2509 |
var key = 'available'; |
2510 |
var available_quota = {};
|
2511 |
_.each(['cyclades.ram', 'cyclades.cpu', 'cyclades.disk'], |
2512 |
function (key) {
|
2513 |
var value = quotas.get(key).get_available(true); |
2514 |
available_quota[key.replace('cyclades.', '')] = value; |
2515 |
}); |
2516 |
return available_quota;
|
2517 |
} |
2518 |
}) |
2519 |
|
2520 |
models.Resource = models.Model.extend({ |
2521 |
api_type: 'accounts', |
2522 |
path: 'resources' |
2523 |
}); |
2524 |
|
2525 |
models.Resources = models.Collection.extend({ |
2526 |
api_type: 'accounts', |
2527 |
path: 'resources', |
2528 |
model: models.Network,
|
2529 |
|
2530 |
parse: function(resp) { |
2531 |
return _.map(resp, function(value, key) { |
2532 |
return _.extend(value, {'name': key, 'id': key}); |
2533 |
}) |
2534 |
} |
2535 |
}); |
2536 |
|
2537 |
// storage initialization
|
2538 |
snf.storage.images = new models.Images();
|
2539 |
snf.storage.flavors = new models.Flavors();
|
2540 |
snf.storage.vms = new models.VMS();
|
2541 |
snf.storage.keys = new models.PublicKeys();
|
2542 |
snf.storage.resources = new models.Resources();
|
2543 |
snf.storage.quotas = new models.Quotas();
|
2544 |
snf.storage.public_pools = new models.PublicPools();
|
2545 |
|
2546 |
})(this);
|