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