root / ui / static / synnefo.js @ 8e5122dd
History | View | Annotate | Download (66.1 kB)
1 |
//
|
---|---|
2 |
// Copyright 2011 GRNET S.A. All rights reserved.
|
3 |
//
|
4 |
// Redistribution and use in source and binary forms, with or
|
5 |
// without modification, are permitted provided that the following
|
6 |
// conditions are met:
|
7 |
//
|
8 |
// 1. Redistributions of source code must retain the above
|
9 |
// copyright notice, this list of conditions and the following
|
10 |
// disclaimer.
|
11 |
//
|
12 |
// 2. Redistributions in binary form must reproduce the above
|
13 |
// copyright notice, this list of conditions and the following
|
14 |
// disclaimer in the documentation and/or other materials
|
15 |
// provided with the distribution.
|
16 |
//
|
17 |
// THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
18 |
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
19 |
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
20 |
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
21 |
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
22 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
23 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
24 |
// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
25 |
// AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
26 |
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
27 |
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
28 |
// POSSIBILITY OF SUCH DAMAGE.
|
29 |
//
|
30 |
// The views and conclusions contained in the software and
|
31 |
// documentation are those of the authors and should not be
|
32 |
// interpreted as representing official policies, either expressed
|
33 |
// or implied, of GRNET S.A.
|
34 |
//
|
35 |
var API_URL = "/api/v1.1"; |
36 |
var changes_since = 0, deferred = 0, update_request = false, load_request = false, pending_actions = []; |
37 |
var flavors = [], images = [], servers = [], disks = [], cpus = [], ram = [];
|
38 |
var networks = [], networks_changes_since = 0; |
39 |
|
40 |
|
41 |
//FIXME: authentication
|
42 |
//if cookie with value X-Auth-Token exists, set the value on the headers.
|
43 |
$.ajaxSetup({
|
44 |
'beforeSend': function(xhr) { |
45 |
//if ($.cookie("X-Auth-Token") != null) {
|
46 |
xhr.setRequestHeader("X-Auth-Token", $.cookie("X-Auth-Token")); |
47 |
//} else {
|
48 |
// $.cookie("X-Auth-Token", "46e427d657b20defe352804f0eb6f8a2"); // set X-Auth-Token cookie
|
49 |
//}
|
50 |
} |
51 |
}); |
52 |
|
53 |
|
54 |
// jquery show/hide events
|
55 |
var _oldshow = $.fn.show; |
56 |
$.fn.show = function(speed, callback) { |
57 |
$(this).trigger('show'); |
58 |
return _oldshow.apply(this,arguments); |
59 |
} |
60 |
var _oldhide = $.fn.hide; |
61 |
$.fn.hide = function(speed, callback) { |
62 |
$(this).trigger('hide'); |
63 |
return _oldhide.apply(this,arguments); |
64 |
} |
65 |
|
66 |
function ISODateString(d){ |
67 |
//return a date in an ISO 8601 format using UTC.
|
68 |
//do not include time zone info (Z) at the end
|
69 |
//taken from the Mozilla Developer Center
|
70 |
function pad(n){ return n<10 ? '0'+n : n } |
71 |
return d.getUTCFullYear()+ '-' + |
72 |
pad(d.getUTCMonth()+1) + '-' + |
73 |
pad(d.getUTCDate()) + 'T' +
|
74 |
pad(d.getUTCHours()) + ':' +
|
75 |
pad(d.getUTCMinutes()) + ':' +
|
76 |
pad(d.getUTCSeconds()) +'Z'
|
77 |
} |
78 |
|
79 |
function parse_error(responseText, errorCode){ |
80 |
var errors = [];
|
81 |
try {
|
82 |
responseObj = JSON.parse(responseText); |
83 |
} |
84 |
catch(err) {
|
85 |
errors[0] = {'code': errorCode}; |
86 |
return errors;
|
87 |
} |
88 |
for (var err in responseObj){ |
89 |
errors[errors.length] = responseObj[err]; |
90 |
} |
91 |
return errors;
|
92 |
} |
93 |
|
94 |
// indexOf prototype for IE
|
95 |
if (!Array.prototype.indexOf) {
|
96 |
Array.prototype.indexOf = function(elt /*, from*/) { |
97 |
var len = this.length; |
98 |
var from = Number(arguments[1]) || 0; |
99 |
from = (from < 0)
|
100 |
? Math.ceil(from) |
101 |
: Math.floor(from); |
102 |
if (from < 0) |
103 |
from += len; |
104 |
|
105 |
for (; from < len; from++) {
|
106 |
if (from in this && |
107 |
this[from] === elt)
|
108 |
return from;
|
109 |
} |
110 |
return -1; |
111 |
}; |
112 |
} |
113 |
|
114 |
// trim prototype for IE
|
115 |
if(typeof String.prototype.trim !== 'function') { |
116 |
String.prototype.trim = function() { |
117 |
return this.replace(/^\s+|\s+$/g, ''); |
118 |
} |
119 |
} |
120 |
|
121 |
function update_confirmations() { |
122 |
// hide all confirm boxes to begin with
|
123 |
$('#machines-pane div.confirm_single').hide(); |
124 |
$('#machines-pane div.confirm_multiple').hide(); |
125 |
var action_type = [];
|
126 |
// standard view or single view
|
127 |
if ($.cookie("view") == '0' || $.cookie("view") == '2') { |
128 |
for (var i=0; i<pending_actions.length; i++) { |
129 |
// show single confirms
|
130 |
if (pending_actions[i][0] == reboot) { |
131 |
action_type = "reboot";
|
132 |
} else if (pending_actions[i][0] == shutdown) { |
133 |
action_type = "shutdown";
|
134 |
} else if (pending_actions[i][0] == start) { |
135 |
action_type = "start";
|
136 |
} else if (pending_actions[i][0] == open_console) { |
137 |
action_type = "console";
|
138 |
} else {
|
139 |
action_type = "destroy";
|
140 |
} |
141 |
$("#machines-pane #" + pending_actions[i][1] + |
142 |
" div.action-container." + action_type + " div.confirm_single").show(); |
143 |
} |
144 |
} |
145 |
// if more than one pending action show multiple confirm box
|
146 |
if (pending_actions.length>1 || $.cookie("view") == '1' && pending_actions.length == 1){ |
147 |
$('#machines-pane div.confirm_multiple span.actionLen').text(pending_actions.length); |
148 |
$('#machines-pane div.confirm_multiple').show(); |
149 |
} |
150 |
} |
151 |
|
152 |
function update_network_confirmations(){ |
153 |
// hide all confirm boxes to begin with
|
154 |
$('#networks-pane div.confirm_multiple').hide(); |
155 |
|
156 |
for (var i=0;i<pending_actions.length;i++){ |
157 |
// show single confirms depending on the action
|
158 |
if (pending_actions[i][0] == delete_network) { |
159 |
$("#networks-pane div.network#net-"+pending_actions[i][1]).children('.confirm_single').show(); |
160 |
} else if (pending_actions[i][0] == remove_server_from_network) { |
161 |
$("#networks-pane div.network #net-"+pending_actions[i][1]+"-server-"+pending_actions[i][2]).children('.confirm_single').show(); |
162 |
} // else {}
|
163 |
} |
164 |
|
165 |
// if more than one pending action show multiple confirm box
|
166 |
if (pending_actions.length > 1){ |
167 |
$('#networks-pane div.confirm_multiple span.actionLen').text(pending_actions.length); |
168 |
$('#networks-pane div.confirm_multiple').show(); |
169 |
} |
170 |
} |
171 |
|
172 |
function list_view() { |
173 |
changes_since = 0; // to reload full list |
174 |
pending_actions = []; // clear pending actions
|
175 |
update_confirmations(); |
176 |
clearTimeout(deferred); // clear old deferred calls
|
177 |
try {
|
178 |
update_request.abort(); // cancel pending ajax updates
|
179 |
load_request.abort(); |
180 |
}catch(err){}
|
181 |
$.cookie("view", '1'); // set list cookie |
182 |
uri = $("a#list").attr("href"); |
183 |
load_request = $.ajax({
|
184 |
url: uri,
|
185 |
type: "GET", |
186 |
timeout: TIMEOUT,
|
187 |
dataType: "html", |
188 |
error: function(jqXHR, textStatus, errorThrown) { |
189 |
return false; |
190 |
}, |
191 |
success: function(data, textStatus, jqXHR) { |
192 |
$("a#list")[0].className += ' activelink'; |
193 |
$("a#standard")[0].className = ''; |
194 |
$("a#single")[0].className = ''; |
195 |
$("div#machinesview").html(data); |
196 |
} |
197 |
}); |
198 |
return false; |
199 |
} |
200 |
|
201 |
function single_view() { |
202 |
changes_since = 0; // to reload full list |
203 |
pending_actions = []; // clear pending actions
|
204 |
update_confirmations(); |
205 |
clearTimeout(deferred); // clear old deferred calls
|
206 |
try {
|
207 |
update_request.abort(); // cancel pending ajax updates
|
208 |
load_request.abort(); |
209 |
}catch(err){}
|
210 |
$.cookie("view", '2'); // set list cookie |
211 |
uri = $("a#single").attr("href"); |
212 |
load_request = $.ajax({
|
213 |
url: uri,
|
214 |
type: "GET", |
215 |
timeout: TIMEOUT,
|
216 |
dataType: "html", |
217 |
error: function(jqXHR, textStatus, errorThrown) { |
218 |
return false; |
219 |
}, |
220 |
success: function(data, textStatus, jqXHR) { |
221 |
$("a#single")[0].className += ' activelink'; |
222 |
$("a#standard")[0].className = ''; |
223 |
$("a#list")[0].className = ''; |
224 |
$("div#machinesview").html(data); |
225 |
} |
226 |
}); |
227 |
return false; |
228 |
} |
229 |
|
230 |
function standard_view() { |
231 |
changes_since = 0; // to reload full list |
232 |
pending_actions = []; // clear pending actions
|
233 |
update_confirmations(); |
234 |
clearTimeout(deferred); // clear old deferred calls
|
235 |
try {
|
236 |
update_request.abort() // cancel pending ajax updates
|
237 |
load_request.abort(); |
238 |
}catch(err){}
|
239 |
$.cookie("view", '0'); |
240 |
uri = $("a#standard").attr("href"); |
241 |
load_request = $.ajax({
|
242 |
url: uri,
|
243 |
type: "GET", |
244 |
timeout: TIMEOUT,
|
245 |
dataType: "html", |
246 |
error: function(jqXHR, textStatus, errorThrown) { |
247 |
return false; |
248 |
}, |
249 |
success: function(data, textStatus, jqXHR) { |
250 |
$("a#standard")[0].className += ' activelink'; |
251 |
$("a#list")[0].className = ''; |
252 |
$("a#single")[0].className = ''; |
253 |
$("div#machinesview").html(data); |
254 |
} |
255 |
}); |
256 |
return false; |
257 |
} |
258 |
|
259 |
function choose_view() { |
260 |
if ($.cookie("view")=='1') { |
261 |
list_view(); |
262 |
} else if ($.cookie("view")=='2'){ |
263 |
single_view(); |
264 |
} else {
|
265 |
standard_view(); |
266 |
} |
267 |
} |
268 |
|
269 |
// return value from metadata key "OS", if it exists
|
270 |
function os_icon(metadata) { |
271 |
if (!metadata) {
|
272 |
return 'unknown'; |
273 |
} |
274 |
if (metadata.values.OS == undefined || metadata.values.OS == '') { |
275 |
return 'unknown'; |
276 |
} else {
|
277 |
if (os_icons.indexOf(metadata.values.OS) == -1) { |
278 |
return 'unknown'; |
279 |
} else {
|
280 |
return metadata.values.OS;
|
281 |
} |
282 |
} |
283 |
} |
284 |
|
285 |
function os_icon_from_value(metadata) { |
286 |
if (!metadata) {
|
287 |
return 'unknown'; |
288 |
} |
289 |
if (metadata == undefined || metadata == '') { |
290 |
return 'unknown'; |
291 |
} else {
|
292 |
if (os_icons.indexOf(metadata) == -1) { |
293 |
return 'unknown'; |
294 |
} else {
|
295 |
return metadata;
|
296 |
} |
297 |
} |
298 |
} |
299 |
|
300 |
// get and show a list of running and terminated machines
|
301 |
function update_vms(interval) { |
302 |
try{ console.info('updating machines'); } catch(err){} |
303 |
var uri= API_URL + '/servers/detail'; |
304 |
|
305 |
if (changes_since != 0) |
306 |
uri+='?changes-since='+changes_since
|
307 |
|
308 |
update_request = $.ajax({
|
309 |
cache: false, |
310 |
url: uri,
|
311 |
type: "GET", |
312 |
timeout: TIMEOUT,
|
313 |
dataType: "json", |
314 |
error: function(jqXHR, textStatus, errorThrown) { |
315 |
// don't forget to try again later
|
316 |
if (interval) {
|
317 |
clearTimeout(deferred); // clear old deferred calls
|
318 |
deferred = setTimeout(function() {update_vms(interval);},interval,interval);
|
319 |
} |
320 |
// as for now, just show an error message
|
321 |
try { console.info('update_vms errback:' + jqXHR.status ) } catch(err) {} |
322 |
try {
|
323 |
ajax_error(jqXHR.status, undefined, 'Update VMs', jqXHR.responseText); |
324 |
} catch(err) {
|
325 |
ajax_error(0, undefined, 'Update VMs', jqXHR.responseText); |
326 |
} |
327 |
return false; |
328 |
}, |
329 |
success: function(data, textStatus, jqXHR) { |
330 |
// create changes_since string if necessary
|
331 |
if (jqXHR.getResponseHeader('Date') != null){ |
332 |
changes_since_date = new Date(jqXHR.getResponseHeader('Date')); |
333 |
changes_since = ISODateString(changes_since_date); |
334 |
} |
335 |
|
336 |
if (interval) {
|
337 |
clearTimeout(deferred); // clear old deferred calls
|
338 |
deferred = setTimeout(function() {update_vms(interval);},interval,interval);
|
339 |
} |
340 |
|
341 |
if (jqXHR.status == 200 || jqXHR.status == 203) { |
342 |
try {
|
343 |
servers = data.servers.values; |
344 |
jQuery.parseJSON(data); |
345 |
update_machines_view(data); |
346 |
} catch(err) { ajax_error('400', undefined, 'Update VMs', jqXHR.responseText);} |
347 |
} else if (jqXHR.status != 304){ |
348 |
try { console.info('update_vms callback:' + jqXHR.status ) } catch(err) {} |
349 |
/*
|
350 |
FIXME: Here it should return the error, however Opera does not support 304.
|
351 |
Instead 304 it returns 0. To dealt with this we treat 0 as an
|
352 |
304, which should be corrected (Bug #317).
|
353 |
*/
|
354 |
//ajax_error(jqXHR.status, undefined, 'Update VMs', jqXHR.responseText);
|
355 |
} |
356 |
return false; |
357 |
} |
358 |
}); |
359 |
return false; |
360 |
} |
361 |
|
362 |
// get a list of running and terminated machines, used in network view
|
363 |
function update_networks(interval) { |
364 |
try{ console.info('updating networks'); } catch(err){} |
365 |
var uri= API_URL + '/servers/detail'; |
366 |
|
367 |
if (changes_since != 0) |
368 |
uri+='?changes-since='+changes_since
|
369 |
|
370 |
update_request = $.ajax({
|
371 |
cache: false, |
372 |
url: uri,
|
373 |
type: "GET", |
374 |
timeout: TIMEOUT,
|
375 |
dataType: "json", |
376 |
error: function(jqXHR, textStatus, errorThrown) { |
377 |
// don't forget to try again later
|
378 |
if (interval) {
|
379 |
clearTimeout(deferred); // clear old deferred calls
|
380 |
deferred = setTimeout(function() {update_networks(interval);},interval,interval);
|
381 |
} |
382 |
// as for now, just show an error message
|
383 |
try { console.info('update_networks errback:' + jqXHR.status ) } catch(err) {} |
384 |
try {
|
385 |
ajax_error(jqXHR.status, undefined, 'Update networks', jqXHR.responseText); |
386 |
} catch(err) {
|
387 |
ajax_error(0, undefined, 'Update networks', jqXHR.responseText); |
388 |
} |
389 |
return false; |
390 |
}, |
391 |
success: function(data, textStatus, jqXHR) { |
392 |
// create changes_since string if necessary
|
393 |
if (jqXHR.getResponseHeader('Date') != null){ |
394 |
changes_since_date = new Date(jqXHR.getResponseHeader('Date')); |
395 |
changes_since = ISODateString(changes_since_date); |
396 |
} |
397 |
|
398 |
if (interval) {
|
399 |
clearTimeout(deferred); // clear old deferred calls
|
400 |
deferred = setTimeout(function() {update_networks(interval);},interval,interval);
|
401 |
} |
402 |
|
403 |
if (jqXHR.status == 200 || jqXHR.status == 203) { |
404 |
try {
|
405 |
servers = data.servers.values; |
406 |
jQuery.parseJSON(data); |
407 |
update_network_names(data); |
408 |
} catch(err) { ajax_error('400', undefined, 'Update networks', jqXHR.responseText);} |
409 |
} else if (jqXHR.status == 304) { |
410 |
update_network_names(); |
411 |
} |
412 |
else {
|
413 |
try { console.info('update_networks callback:' + jqXHR.status ) } catch(err) {} |
414 |
/*
|
415 |
FIXME: Here it should return the error, however Opera does not support 304.
|
416 |
Instead 304 it returns 0. To dealt with this we treat 0 as an
|
417 |
304, which should be corrected (Bug #317).
|
418 |
*/
|
419 |
//ajax_error(jqXHR.status, undefined, 'Update networks', jqXHR.responseText);
|
420 |
update_network_names(); |
421 |
} |
422 |
return false; |
423 |
} |
424 |
}); |
425 |
return false; |
426 |
} |
427 |
|
428 |
// get and show a list of public and private networks
|
429 |
function update_network_names(servers_data) { |
430 |
try{ console.info('updating network names'); } catch(err){} |
431 |
var uri= API_URL + '/networks/detail'; |
432 |
|
433 |
if (networks_changes_since != 0) |
434 |
//FIXME: Comment out the following, until metadata do not 304 when changed
|
435 |
uri+='?changes-since=' + networks_changes_since
|
436 |
|
437 |
update_request = $.ajax({
|
438 |
cache: false, |
439 |
url: uri,
|
440 |
type: "GET", |
441 |
timeout: TIMEOUT,
|
442 |
dataType: "json", |
443 |
error: function(jqXHR, textStatus, errorThrown) { |
444 |
// as for now, just show an error message
|
445 |
try {
|
446 |
console.info('update_network names errback:' + jqXHR.status )
|
447 |
} catch(err) {}
|
448 |
try {
|
449 |
ajax_error(jqXHR.status, undefined, 'Update network names', jqXHR.responseText); |
450 |
} catch(err) {
|
451 |
ajax_error(0, undefined, 'Update network names', jqXHR.responseText); |
452 |
} |
453 |
return false; |
454 |
}, |
455 |
success: function(data, textStatus, jqXHR) { |
456 |
// create changes_since string if necessary
|
457 |
if (jqXHR.getResponseHeader('Date') != null){ |
458 |
changes_since_date = new Date(jqXHR.getResponseHeader('Date')); |
459 |
networks_changes_since = ISODateString(changes_since_date); |
460 |
} |
461 |
|
462 |
if (jqXHR.status == 200 || jqXHR.status == 203) { |
463 |
try {
|
464 |
networks = data.networks.values; |
465 |
jQuery.parseJSON(data); |
466 |
update_networks_view(servers_data, data); |
467 |
} catch(err) {
|
468 |
ajax_error('400', undefined, 'Update network names', jqXHR.responseText); |
469 |
} |
470 |
} else if (jqXHR.status == 304) { |
471 |
update_networks_view(servers_data); |
472 |
} else if (jqXHR.status != 304){ |
473 |
try { console.info('update_network_names callback:' + jqXHR.status ) } catch(err) {} |
474 |
/*
|
475 |
FIXME: Here it should return the error, however Opera does not support 304.
|
476 |
Instead 304 it returns 0. To dealt with this we treat 0 as an
|
477 |
304, which should be corrected (Bug #317).
|
478 |
*/
|
479 |
//ajax_error(jqXHR.status, undefined, 'Update network names', jqXHR.responseText);
|
480 |
update_networks_view(servers_data); |
481 |
} |
482 |
return false; |
483 |
} |
484 |
}); |
485 |
return false; |
486 |
} |
487 |
|
488 |
// get and show a list of available standard and custom images
|
489 |
function update_images() { |
490 |
$.ajax({
|
491 |
url: API_URL + '/images/detail', |
492 |
type: "GET", |
493 |
//async: false,
|
494 |
dataType: "json", |
495 |
timeout: TIMEOUT,
|
496 |
error: function(jqXHR, textStatus, errorThrown) { |
497 |
try {
|
498 |
ajax_error(jqXHR.status, undefined, 'Update Images', jqXHR.responseText); |
499 |
} catch(err) {
|
500 |
ajax_error(0, undefined, 'Update Images', jqXHR.responseText); |
501 |
} |
502 |
}, |
503 |
success: function(data, textStatus, jqXHR) { |
504 |
try {
|
505 |
images = data.images.values; |
506 |
jQuery.parseJSON(data); |
507 |
update_wizard_images(); |
508 |
} catch(err){
|
509 |
ajax_error("NO_IMAGES");
|
510 |
} |
511 |
} |
512 |
}); |
513 |
return false; |
514 |
} |
515 |
|
516 |
function update_wizard_images() { |
517 |
if ($("ul#standard-images li").toArray().length + $("ul#custom-images li").toArray().length == 0) { |
518 |
$.each(images, function(i,image){ |
519 |
var img = $('#image-template').clone().attr("id","img-"+image.id).fadeIn("slow"); |
520 |
img.find("label").attr('for',"img-radio-" + image.id); |
521 |
img.find(".image-title").text(image.name);
|
522 |
if (image.metadata) {
|
523 |
if (image.metadata.values.description != undefined) { |
524 |
img.find(".description").text(image.metadata.values.description);
|
525 |
} |
526 |
if (image.metadata.values.size != undefined) { |
527 |
img.find("#size").text(image.metadata.values.size);
|
528 |
} |
529 |
} |
530 |
img.find("input.radio").attr('id',"img-radio-" + image.id); |
531 |
if (i==0) img.find("input.radio").attr("checked","checked"); |
532 |
var image_logo = os_icon(image.metadata);
|
533 |
img.find("img.image-logo").attr('src','static/icons/os/'+image_logo+'.png'); |
534 |
if (image.metadata) {
|
535 |
if (image.metadata.values.serverId != undefined) { |
536 |
img.appendTo("ul#custom-images");
|
537 |
} else {
|
538 |
img.appendTo("ul#standard-images");
|
539 |
} |
540 |
} else {
|
541 |
img.appendTo("ul#standard-images");
|
542 |
} |
543 |
}); |
544 |
} |
545 |
} |
546 |
|
547 |
function update_wizard_flavors(){ |
548 |
// sliders for selecting VM flavor
|
549 |
$("#cpu:range").rangeinput({min:0, |
550 |
value:0, |
551 |
step:1, |
552 |
progress: true, |
553 |
max:cpus.length-1}); |
554 |
|
555 |
$("#storage:range").rangeinput({min:0, |
556 |
value:0, |
557 |
step:1, |
558 |
progress: true, |
559 |
max:disks.length-1}); |
560 |
|
561 |
$("#ram:range").rangeinput({min:0, |
562 |
value:0, |
563 |
step:1, |
564 |
progress: true, |
565 |
max:ram.length-1}); |
566 |
$("#small").click(); |
567 |
|
568 |
// update the indicators when sliding
|
569 |
$("#cpu:range").data().rangeinput.onSlide(function(event,value){ |
570 |
$("#cpu-indicator")[0].value = cpus[Number(value)]; |
571 |
$("#cpu-indicator").addClass('selectedrange'); |
572 |
}); |
573 |
$("#cpu:range").data().rangeinput.change(function(event,value){ |
574 |
$("#cpu-indicator")[0].value = cpus[Number(value)]; |
575 |
$("#custom").click(); |
576 |
$("#cpu-indicator").removeClass('selectedrange'); |
577 |
}); |
578 |
$("#ram:range").data().rangeinput.onSlide(function(event,value){ |
579 |
$("#ram-indicator")[0].value = ram[Number(value)]; |
580 |
$("#ram-indicator").addClass('selectedrange'); |
581 |
}); |
582 |
$("#ram:range").data().rangeinput.change(function(event,value){ |
583 |
$("#ram-indicator")[0].value = ram[Number(value)]; |
584 |
$("#custom").click(); |
585 |
$("#ram-indicator").removeClass('selectedrange'); |
586 |
}); |
587 |
$("#storage:range").data().rangeinput.onSlide(function(event,value){ |
588 |
$("#storage-indicator")[0].value = disks[Number(value)]; |
589 |
$("#storage-indicator").addClass('selectedrange'); |
590 |
}); |
591 |
$("#storage:range").data().rangeinput.change(function(event,value){ |
592 |
$("#storage-indicator")[0].value = disks[Number(value)]; |
593 |
$("#custom").click(); |
594 |
$("#storage-indicator").removeClass('selectedrange'); |
595 |
}); |
596 |
} |
597 |
|
598 |
Array.prototype.unique = function () { |
599 |
var r = new Array(); |
600 |
o:for(var i = 0, n = this.length; i < n; i++) |
601 |
{ |
602 |
for(var x = 0, y = r.length; x < y; x++) |
603 |
{ |
604 |
if(r[x]==this[i]) |
605 |
{ |
606 |
continue o;
|
607 |
} |
608 |
} |
609 |
r[r.length] = this[i];
|
610 |
} |
611 |
return r;
|
612 |
} |
613 |
|
614 |
// get and configure flavor selection
|
615 |
function update_flavors() { |
616 |
$.ajax({
|
617 |
url: API_URL + '/flavors/detail', |
618 |
type: "GET", |
619 |
//async: false,
|
620 |
dataType: "json", |
621 |
timeout: TIMEOUT,
|
622 |
error: function(jqXHR, textStatus, errorThrown) { |
623 |
try {
|
624 |
ajax_error(jqXHR.status, undefined, 'Update Flavors', jqXHR.responseText); |
625 |
} catch (err) {
|
626 |
ajax_error(err); |
627 |
} |
628 |
// start updating vm list
|
629 |
update_vms(UPDATE_INTERVAL); |
630 |
}, |
631 |
success: function(data, textStatus, jqXHR) { |
632 |
|
633 |
try {
|
634 |
flavors = data.flavors.values; |
635 |
jQuery.parseJSON(data); |
636 |
$.each(flavors, function(i, flavor) { |
637 |
cpus[i] = flavor['cpu'];
|
638 |
disks[i] = flavor['disk'];
|
639 |
ram[i] = flavor['ram'];
|
640 |
}); |
641 |
cpus = cpus.unique(); |
642 |
disks = disks.unique(); |
643 |
ram = ram.unique(); |
644 |
update_wizard_flavors(); |
645 |
} catch(err){
|
646 |
ajax_error("NO_FLAVORS");
|
647 |
} |
648 |
// start updating vm list
|
649 |
update_vms(UPDATE_INTERVAL); |
650 |
} |
651 |
}); |
652 |
return false; |
653 |
} |
654 |
|
655 |
// return flavorRef from cpu, disk, ram values
|
656 |
function identify_flavor(cpu, disk, ram){ |
657 |
for (i=0;i<flavors.length;i++){ |
658 |
if (flavors[i]['cpu'] == cpu && flavors[i]['disk']==disk && flavors[i]['ram']==ram) { |
659 |
return flavors[i]['id'] |
660 |
} |
661 |
} |
662 |
return 0; |
663 |
} |
664 |
|
665 |
// return image entry from imageRef
|
666 |
function get_image(imageRef) { |
667 |
for (i=0;i<images.length;i++){ |
668 |
if (images[i]['id'] == imageRef) { |
669 |
return images[i];
|
670 |
} |
671 |
} |
672 |
return 0; |
673 |
} |
674 |
|
675 |
// return machine entry from serverID
|
676 |
function get_machine(serverID) { |
677 |
for (i=0;i<servers.length;i++){ |
678 |
if (servers[i]['id'] == serverID) { |
679 |
return servers[i];
|
680 |
} |
681 |
} |
682 |
return 0; |
683 |
} |
684 |
|
685 |
// update the actions in icon view, per server
|
686 |
function update_iconview_actions(serverID, server_status) { |
687 |
if ($.cookie("view")=='2') { |
688 |
// remove .disable from all actions to begin with
|
689 |
$('#machinesview-single #' + serverID + ' div.single-action').show(); |
690 |
// decide which actions should be disabled
|
691 |
for (current_action in actions) { |
692 |
if (actions[current_action].indexOf(server_status) == -1 ) { |
693 |
$('#machinesview-single #' + serverID + ' div.action-' + current_action).hide(); |
694 |
} |
695 |
} |
696 |
} else {
|
697 |
// remove .disable from all actions to begin with
|
698 |
$('#machinesview-icon.standard #' + serverID + ' div.actions').find('a').removeClass('disabled'); |
699 |
// decide which actions should be disabled
|
700 |
for (current_action in actions) { |
701 |
if (actions[current_action].indexOf(server_status) == -1 ) { |
702 |
$('#machinesview-icon.standard #' + serverID + ' a.action-' + current_action).addClass('disabled'); |
703 |
} |
704 |
} |
705 |
} |
706 |
} |
707 |
|
708 |
// update the actions in list view
|
709 |
function update_listview_actions() { |
710 |
var states = [];
|
711 |
var on = [];
|
712 |
var checked = $("table.list-machines tbody input[type='checkbox']:checked"); |
713 |
// disable all actions to begin with
|
714 |
$('#machinesview .list div.actions').children().removeClass('enabled'); |
715 |
|
716 |
// are there multiple machines selected?
|
717 |
if (checked.length>1) |
718 |
states[0] = 'multiple'; |
719 |
|
720 |
// check the states of selected machines
|
721 |
checked.each(function(i,checkbox) {
|
722 |
states[states.length] = checkbox.className; |
723 |
var ip = $("#" + checkbox.id.replace('input-','') + ".ip span.public").text(); |
724 |
if (ip.replace('undefined','').length) |
725 |
states[states.length] = 'network';
|
726 |
}); |
727 |
|
728 |
// decide which actions should be enabled
|
729 |
for (a in actions) { |
730 |
var enabled = false; |
731 |
for (var s =0; s<states.length; s++) { |
732 |
if (actions[a].indexOf(states[s]) != -1 ) { |
733 |
enabled = true;
|
734 |
} else {
|
735 |
enabled = false;
|
736 |
break;
|
737 |
} |
738 |
} |
739 |
if (enabled)
|
740 |
on[on.length]=a; |
741 |
} |
742 |
// enable those actions
|
743 |
for (action in on) { |
744 |
$("#action-" + on[action]).addClass('enabled'); |
745 |
} |
746 |
} |
747 |
|
748 |
//create server action
|
749 |
function create_vm(machineName, imageRef, flavorRef){ |
750 |
var image_logo = os_icon(get_image(imageRef).metadata);
|
751 |
var uri = API_URL + '/servers'; |
752 |
var payload = {
|
753 |
"server": {
|
754 |
"name": machineName,
|
755 |
"imageRef": imageRef,
|
756 |
"flavorRef" : flavorRef,
|
757 |
"metadata" : {
|
758 |
"OS" : image_logo
|
759 |
} |
760 |
} |
761 |
}; |
762 |
|
763 |
$.ajax({
|
764 |
url: uri,
|
765 |
type: "POST", |
766 |
contentType: "application/json", |
767 |
dataType: "json", |
768 |
data: JSON.stringify(payload),
|
769 |
timeout: TIMEOUT,
|
770 |
error: function(jqXHR, textStatus, errorThrown) { |
771 |
// close wizard and show error box
|
772 |
$('#machines-pane a#create').data('overlay').close(); |
773 |
try {
|
774 |
ajax_error(jqXHR.status, undefined, 'Create VM', jqXHR.responseText); |
775 |
} catch(err) {
|
776 |
ajax_error(0, undefined, 'Create VM', jqXHR.responseText); |
777 |
} |
778 |
}, |
779 |
success: function(data, textStatus, jqXHR) { |
780 |
if ( jqXHR.status == '202') { |
781 |
ajax_success("CREATE_VM_SUCCESS", data.server.adminPass);
|
782 |
} else {
|
783 |
// close wizard and show error box
|
784 |
$('#machines-pane a#create').data('overlay').close(); |
785 |
ajax_error(jqXHR.status, undefined, 'Create VM', jqXHR.responseText); |
786 |
} |
787 |
} |
788 |
}); |
789 |
} |
790 |
|
791 |
// reboot action
|
792 |
function reboot(serverIDs){ |
793 |
if (!serverIDs.length){
|
794 |
//ajax_success('DEFAULT');
|
795 |
return false; |
796 |
} |
797 |
// ajax post reboot call
|
798 |
var payload = {
|
799 |
"reboot": {"type" : "HARD"} |
800 |
}; |
801 |
|
802 |
var serverID = serverIDs.pop();
|
803 |
|
804 |
$.ajax({
|
805 |
url: API_URL + '/servers/' + serverID + '/action', |
806 |
type: "POST", |
807 |
contentType: "application/json", |
808 |
dataType: "json", |
809 |
data: JSON.stringify(payload),
|
810 |
timeout: TIMEOUT,
|
811 |
error: function(jqXHR, textStatus, errorThrown) { |
812 |
// in machine views
|
813 |
if ( $.cookie("pane") == 0) { |
814 |
try {
|
815 |
display_failure(jqXHR.status, serverID, 'Reboot', jqXHR.responseText);
|
816 |
} catch (err) {
|
817 |
display_failure(0, serverID, 'Reboot', jqXHR.responseText); |
818 |
} |
819 |
} |
820 |
// in network view
|
821 |
else {
|
822 |
try {
|
823 |
display_reboot_failure(jqXHR.status, serverID, jqXHR.responseText); |
824 |
} catch (err) {
|
825 |
display_reboot_failure(0, serverID, jqXHR.responseText);
|
826 |
} |
827 |
} |
828 |
}, |
829 |
success: function(data, textStatus, jqXHR) { |
830 |
if ( jqXHR.status == '202') { |
831 |
try {
|
832 |
console.info('rebooted ' + serverID);
|
833 |
} catch(err) {}
|
834 |
// indicate that the action succeeded
|
835 |
// in machine views
|
836 |
if ( $.cookie("pane") == 0) { |
837 |
display_success(serverID); |
838 |
} |
839 |
// in network view
|
840 |
else {
|
841 |
display_reboot_success(serverID); |
842 |
} |
843 |
// continue with the rest of the servers
|
844 |
reboot(serverIDs); |
845 |
} else {
|
846 |
ajax_error(jqXHR.status, serverID, 'Reboot', jqXHR.responseText);
|
847 |
} |
848 |
} |
849 |
}); |
850 |
return false; |
851 |
} |
852 |
|
853 |
// shutdown action
|
854 |
function shutdown(serverIDs) { |
855 |
if (!serverIDs.length){
|
856 |
//ajax_success('DEFAULT');
|
857 |
return false; |
858 |
} |
859 |
// ajax post shutdown call
|
860 |
var payload = {
|
861 |
"shutdown": {}
|
862 |
}; |
863 |
|
864 |
var serverID = serverIDs.pop();
|
865 |
|
866 |
$.ajax({
|
867 |
url: API_URL + '/servers/' + serverID + '/action', |
868 |
type: "POST", |
869 |
contentType: "application/json", |
870 |
dataType: "json", |
871 |
data: JSON.stringify(payload),
|
872 |
timeout: TIMEOUT,
|
873 |
error: function(jqXHR, textStatus, errorThrown) { |
874 |
try {
|
875 |
display_failure(jqXHR.status, serverID, 'Shutdown', jqXHR.responseText);
|
876 |
} catch(err) {
|
877 |
display_failure(0, serverID, 'Shutdown', jqXHR.responseText); |
878 |
} |
879 |
}, |
880 |
success: function(data, textStatus, jqXHR) { |
881 |
if ( jqXHR.status == '202') { |
882 |
try {
|
883 |
console.info('suspended ' + serverID);
|
884 |
} catch(err) {}
|
885 |
// indicate that the action succeeded
|
886 |
display_success(serverID); |
887 |
// continue with the rest of the servers
|
888 |
shutdown(serverIDs); |
889 |
} else {
|
890 |
ajax_error(jqXHR.status, serverID, 'Shutdown', jqXHR.responseText);
|
891 |
} |
892 |
} |
893 |
}); |
894 |
return false; |
895 |
} |
896 |
|
897 |
// destroy action
|
898 |
function destroy(serverIDs) { |
899 |
if (!serverIDs.length){
|
900 |
//ajax_success('DEFAULT');
|
901 |
return false; |
902 |
} |
903 |
// ajax post destroy call can have an empty request body
|
904 |
var payload = {};
|
905 |
|
906 |
var serverID = serverIDs.pop();
|
907 |
|
908 |
$.ajax({
|
909 |
url: API_URL + '/servers/' + serverID, |
910 |
type: "DELETE", |
911 |
contentType: "application/json", |
912 |
dataType: "json", |
913 |
data: JSON.stringify(payload),
|
914 |
timeout: TIMEOUT,
|
915 |
error: function(jqXHR, textStatus, errorThrown) { |
916 |
try {
|
917 |
display_failure(jqXHR.status, serverID, 'Destroy', jqXHR.responseText);
|
918 |
} catch(err) {
|
919 |
display_failure(0, serverID, 'Destroy', jqXHR.responseText); |
920 |
} |
921 |
}, |
922 |
success: function(data, textStatus, jqXHR) { |
923 |
if ( jqXHR.status == '204') { |
924 |
try {
|
925 |
console.info('destroyed ' + serverID);
|
926 |
} catch (err) {}
|
927 |
// indicate that the action succeeded
|
928 |
display_success(serverID); |
929 |
// continue with the rest of the servers
|
930 |
destroy(serverIDs); |
931 |
} else {
|
932 |
ajax_error(jqXHR.status, serverID, 'Destroy', jqXHR.responseText);
|
933 |
} |
934 |
} |
935 |
}); |
936 |
return false; |
937 |
} |
938 |
|
939 |
// start action
|
940 |
function start(serverIDs){ |
941 |
if (!serverIDs.length){
|
942 |
//ajax_success('DEFAULT');
|
943 |
return false; |
944 |
} |
945 |
// ajax post start call
|
946 |
var payload = {
|
947 |
"start": {}
|
948 |
}; |
949 |
|
950 |
var serverID = serverIDs.pop();
|
951 |
|
952 |
$.ajax({
|
953 |
url: API_URL + '/servers/' + serverID + '/action', |
954 |
type: "POST", |
955 |
contentType: "application/json", |
956 |
dataType: "json", |
957 |
data: JSON.stringify(payload),
|
958 |
timeout: TIMEOUT,
|
959 |
error: function(jqXHR, textStatus, errorThrown) { |
960 |
try {
|
961 |
display_failure(jqXHR.status, serverID, 'Start', jqXHR.responseText);
|
962 |
} catch(err) {
|
963 |
display_failure(0, serverID, 'Start', jqXHR.responseText); |
964 |
} |
965 |
}, |
966 |
success: function(data, textStatus, jqXHR) { |
967 |
if ( jqXHR.status == '202') { |
968 |
try {
|
969 |
console.info('started ' + serverID);
|
970 |
} catch(err) {}
|
971 |
// indicate that the action succeeded
|
972 |
display_success(serverID); |
973 |
// continue with the rest of the servers
|
974 |
start(serverIDs); |
975 |
} else {
|
976 |
ajax_error(jqXHR.status, serverID, 'Start', jqXHR.responseText);
|
977 |
} |
978 |
} |
979 |
}); |
980 |
return false; |
981 |
} |
982 |
|
983 |
// Show VNC console
|
984 |
function vnc_attachment(host, port, password) { |
985 |
// FIXME: Must be made into parameters, in settings.py
|
986 |
//vnc = open("", "displayWindow",
|
987 |
// "status=yes,toolbar=yes,menubar=yes");
|
988 |
vd = document.open("application/x-vnc");
|
989 |
|
990 |
vd.writeln("[connection]");
|
991 |
vd.writeln("host=" + host);
|
992 |
vd.writeln("port=" + port);
|
993 |
vd.writeln("password=" + password);
|
994 |
|
995 |
vd.close(); |
996 |
} |
997 |
|
998 |
// Show VNC console
|
999 |
function show_vnc_console(serverID, serverName, serverIP, host, port, password) { |
1000 |
var params_url = '?machine=' + serverName + '&host_ip=' + serverIP + '&host=' + host + '&port=' + port + '&password=' + password; |
1001 |
var params_window = 'scrollbars=no,' + |
1002 |
'menubar=no,' +
|
1003 |
'toolbar=no,' +
|
1004 |
'status=no,' +
|
1005 |
'top=0,' +
|
1006 |
'left=0,' +
|
1007 |
'height=' + screen.height + ',' + |
1008 |
'width=' + screen.width + ',' + |
1009 |
'fullscreen=yes';
|
1010 |
|
1011 |
window.open('machines/console' + params_url, 'formresult' + serverID, params_window); |
1012 |
|
1013 |
// Restore os icon in list view
|
1014 |
osIcon = $('#'+serverID).parent().parent().find('.list-logo'); |
1015 |
osIcon.attr('src',osIcon.attr('os')); |
1016 |
return false; |
1017 |
} |
1018 |
|
1019 |
// console action
|
1020 |
function open_console(serverIDs){ |
1021 |
if (!serverIDs.length){
|
1022 |
//ajax_success('DEFAULT');
|
1023 |
return false; |
1024 |
} |
1025 |
// ajax post start call
|
1026 |
var payload = {
|
1027 |
"console": {"type": "vnc"} |
1028 |
}; |
1029 |
|
1030 |
var serverID = serverIDs.pop();
|
1031 |
|
1032 |
var machine = get_machine(serverID);
|
1033 |
var serverName = machine.name;
|
1034 |
try {
|
1035 |
var serverIP = machine.addresses.values[0].values[0].addr; |
1036 |
} catch(err) { var serverIP = 'undefined'; } |
1037 |
|
1038 |
$.ajax({
|
1039 |
url: API_URL + '/servers/' + serverID + '/action', |
1040 |
type: "POST", |
1041 |
contentType: "application/json", |
1042 |
dataType: "json", |
1043 |
data: JSON.stringify(payload),
|
1044 |
timeout: TIMEOUT,
|
1045 |
error: function(jqXHR, textStatus, errorThrown) { |
1046 |
try {
|
1047 |
display_failure(jqXHR.status, serverID, 'Console', jqXHR.responseText);
|
1048 |
} catch(err) {
|
1049 |
display_failure(0, serverID, 'Console', jqXHR.responseText); |
1050 |
} |
1051 |
}, |
1052 |
success: function(data, textStatus, jqXHR) { |
1053 |
if ( jqXHR.status == '200') { |
1054 |
try {
|
1055 |
console.info('got_console ' + serverID);
|
1056 |
} catch(err) {}
|
1057 |
// indicate that the action succeeded
|
1058 |
// show_vnc_console(serverID, serverName, serverIP,
|
1059 |
// data.console.host,data.console.port,data.console.password);
|
1060 |
show_vnc_console(serverID, serverName, serverIP, |
1061 |
data.console.host,data.console.port,data.console.password); |
1062 |
display_success(serverID); |
1063 |
// hide spinner
|
1064 |
$('#' + serverID + ' .spinner').hide(); |
1065 |
// continue with the rest of the servers
|
1066 |
open_console(serverIDs); |
1067 |
} else {
|
1068 |
ajax_error(jqXHR.status, serverID, 'Console', jqXHR.responseText);
|
1069 |
} |
1070 |
} |
1071 |
}); |
1072 |
return false; |
1073 |
} |
1074 |
|
1075 |
// connect to machine action
|
1076 |
function machine_connect(serverIDs){ |
1077 |
if (!serverIDs.length){
|
1078 |
//ajax_success('DEFAULT');
|
1079 |
return false; |
1080 |
} |
1081 |
var serverID = serverIDs.pop();
|
1082 |
|
1083 |
var machine = get_machine(serverID);
|
1084 |
var serverName = machine.name;
|
1085 |
try {
|
1086 |
var serverIP = machine.addresses.values[0].values[0].addr; |
1087 |
} catch(err) { var serverIP = 'undefined'; } |
1088 |
|
1089 |
try {
|
1090 |
var os = machine.metadata.values.OS;
|
1091 |
} catch(err) { var os = 'undefined'; } |
1092 |
|
1093 |
var params_url = '?ip_address=' + serverIP + '&os=' + os; |
1094 |
|
1095 |
window.open('machines/connect' + params_url);
|
1096 |
|
1097 |
// Restore os icon in list view
|
1098 |
osIcon = $('#'+serverID).parent().parent().find('.list-logo'); |
1099 |
osIcon.attr('src',osIcon.attr('os')); |
1100 |
|
1101 |
return false; |
1102 |
} |
1103 |
|
1104 |
|
1105 |
// rename server
|
1106 |
function rename(serverID, serverName){ |
1107 |
if (!serverID.length){
|
1108 |
//ajax_success('DEFAULT');
|
1109 |
return false; |
1110 |
} |
1111 |
// ajax post rename call
|
1112 |
var payload = {
|
1113 |
"server": {"name": serverName} |
1114 |
}; |
1115 |
|
1116 |
$.ajax({
|
1117 |
url: API_URL + '/servers/' + serverID, |
1118 |
type: "PUT", |
1119 |
contentType: "application/json", |
1120 |
dataType: "json", |
1121 |
data: JSON.stringify(payload),
|
1122 |
timeout: TIMEOUT,
|
1123 |
error: function(jqXHR, textStatus, errorThrown) { |
1124 |
try {
|
1125 |
display_failure(jqXHR.status, serverID, 'Rename', jqXHR.responseText);
|
1126 |
} catch(err) {
|
1127 |
display_failure(0, serverID, 'Rename', jqXHR.responseText); |
1128 |
} |
1129 |
}, |
1130 |
success: function(data, textStatus, jqXHR) { |
1131 |
if ( jqXHR.status == '204' || jqXHR.status == '1223') { |
1132 |
try {
|
1133 |
console.info('renamed ' + serverID);
|
1134 |
} catch(err) {}
|
1135 |
// indicate that the action succeeded
|
1136 |
display_success(serverID); |
1137 |
} else {
|
1138 |
ajax_error(jqXHR.status, serverID, 'Rename', jqXHR.responseText);
|
1139 |
} |
1140 |
} |
1141 |
}); |
1142 |
return false; |
1143 |
} |
1144 |
|
1145 |
// get server metadata
|
1146 |
function get_metadata(serverID, keys_only) { |
1147 |
$.ajax({
|
1148 |
url: API_URL + '/servers/' + serverID + '/meta', |
1149 |
cache: false, |
1150 |
type: "GET", |
1151 |
//async: false,
|
1152 |
dataType: "json", |
1153 |
timeout: TIMEOUT,
|
1154 |
error: function(jqXHR, textStatus, errorThrown) { |
1155 |
try {
|
1156 |
// close wizard and show error box
|
1157 |
$("a#metadata-scrollable").data('overlay').close(); |
1158 |
ajax_error(jqXHR.status, undefined, 'Get metadata', jqXHR.responseText); |
1159 |
} catch (err) {
|
1160 |
ajax_error(err); |
1161 |
} |
1162 |
}, |
1163 |
success: function(data, textStatus, jqXHR) { |
1164 |
// to list the new results in the edit dialog
|
1165 |
if (keys_only) {
|
1166 |
list_metadata_keys(serverID, data.metadata.values); |
1167 |
} else {
|
1168 |
list_metadata(data); |
1169 |
list_metadata_keys(serverID, data.metadata.values); |
1170 |
} |
1171 |
//hide spinner
|
1172 |
$('#metadata-wizard .large-spinner').hide(); |
1173 |
} |
1174 |
}); |
1175 |
return false; |
1176 |
} |
1177 |
|
1178 |
// delete metadata key-value pair
|
1179 |
function delete_metadata(serverID, meta_key) { |
1180 |
$.ajax({
|
1181 |
url: API_URL + '/servers/' + serverID + '/meta/' + meta_key, |
1182 |
type: "DELETE", |
1183 |
//async: false,
|
1184 |
dataType: "json", |
1185 |
timeout: TIMEOUT,
|
1186 |
error: function(jqXHR, textStatus, errorThrown) { |
1187 |
try {
|
1188 |
// close wizard and show error box
|
1189 |
$("a#metadata-scrollable").data('overlay').close(); |
1190 |
ajax_error(jqXHR.status, undefined, 'Delete metadata', jqXHR.responseText); |
1191 |
} catch (err) {
|
1192 |
ajax_error(err); |
1193 |
} |
1194 |
}, |
1195 |
success: function(data, textStatus, jqXHR) { |
1196 |
// success: Do nothing, the UI is already updated
|
1197 |
} |
1198 |
}); |
1199 |
return false; |
1200 |
} |
1201 |
|
1202 |
// add metadata key-value pair
|
1203 |
function update_metadata(serverID, meta_key, meta_value) { |
1204 |
var payload = {
|
1205 |
"meta": {
|
1206 |
} |
1207 |
}; |
1208 |
payload["meta"][meta_key] = meta_value;
|
1209 |
|
1210 |
$.ajax({
|
1211 |
url: API_URL + '/servers/' + serverID + '/meta/' + meta_key, |
1212 |
type: "PUT", |
1213 |
contentType: "application/json", |
1214 |
dataType: "json", |
1215 |
data: JSON.stringify(payload),
|
1216 |
timeout: TIMEOUT,
|
1217 |
error: function(jqXHR, textStatus, errorThrown) { |
1218 |
try {
|
1219 |
// close wizard and show error box
|
1220 |
$("a#metadata-scrollable").data('overlay').close(); |
1221 |
ajax_error(jqXHR.status, undefined, 'add metadata', jqXHR.responseText); |
1222 |
} catch (err) {
|
1223 |
ajax_error(err); |
1224 |
} |
1225 |
}, |
1226 |
success: function(data, textStatus, jqXHR) { |
1227 |
// success: Update icons if meta key is OS
|
1228 |
if (meta_key == "OS") { |
1229 |
$("#metadata-wizard .machine-icon").attr("src","static/icons/machines/small/" + os_icon_from_value(meta_value) + '-' + $("#metadata-wizard div#on-off").text() + '.png'); |
1230 |
var machine = $("#machinesview-icon").find("div#" + serverID); |
1231 |
var os = os_icon_from_value(meta_value);
|
1232 |
var state = $("#metadata-wizard div#on-off").text() |
1233 |
set_machine_os_image(machine, "icon", state, os);
|
1234 |
} |
1235 |
} |
1236 |
}); |
1237 |
return false; |
1238 |
} |
1239 |
|
1240 |
// get stats
|
1241 |
function get_server_stats(serverID) { |
1242 |
$.ajax({
|
1243 |
url: API_URL + '/servers/' + serverID + '/stats', |
1244 |
cache: false, |
1245 |
type: "GET", |
1246 |
//async: false,
|
1247 |
dataType: "json", |
1248 |
timeout: TIMEOUT,
|
1249 |
error: function(jqXHR, textStatus, errorThrown) { |
1250 |
// report error as text inline
|
1251 |
$('#' + serverID + ' img.busy').hide(); |
1252 |
$('#' + serverID + ' div.stat-error').show(); |
1253 |
}, |
1254 |
success: function(data, textStatus, jqXHR) { |
1255 |
// in icon view
|
1256 |
if ( $.cookie('view') == 0 ) { |
1257 |
$('#' + serverID + ' img.busy').removeClass('busy'); |
1258 |
$('#' + serverID + ' img.cpu').attr("src", data.stats.cpuBar); |
1259 |
$('#' + serverID + ' img.net').attr("src", data.stats.netBar); |
1260 |
} |
1261 |
// in single view
|
1262 |
else if ( $.cookie('view') == 2 ) { |
1263 |
$('#' + serverID + ' div.cpu-graph img.stats').attr("src", data.stats.cpuTimeSeries); |
1264 |
$('#' + serverID + ' div.net-graph img.stats').attr("src", data.stats.netTimeSeries); |
1265 |
} |
1266 |
} |
1267 |
}); |
1268 |
return false; |
1269 |
} |
1270 |
|
1271 |
// create network
|
1272 |
function create_network(networkName){ |
1273 |
// ajax post start call
|
1274 |
var payload = {
|
1275 |
"network": { "name": networkName } |
1276 |
}; |
1277 |
|
1278 |
$.ajax({
|
1279 |
url: API_URL + '/networks', |
1280 |
type: "POST", |
1281 |
contentType: "application/json", |
1282 |
dataType: "json", |
1283 |
data: JSON.stringify(payload),
|
1284 |
timeout: TIMEOUT,
|
1285 |
error: function(jqXHR, textStatus, errorThrown) { |
1286 |
try {
|
1287 |
// close wizard and show error box
|
1288 |
$("a#networkscreate").overlay().close(); |
1289 |
ajax_error(jqXHR.status, undefined, 'Create network', jqXHR.responseText); |
1290 |
} catch (err) {
|
1291 |
ajax_error(err); |
1292 |
} |
1293 |
}, |
1294 |
success: function(data, textStatus, jqXHR) { |
1295 |
if ( jqXHR.status == '202') { |
1296 |
try {
|
1297 |
console.info('created network ' + networkName);
|
1298 |
} catch(err) {}
|
1299 |
/*
|
1300 |
On success of this call nothing happens.
|
1301 |
When the UI gets the first update containing the created server,
|
1302 |
the creation wizard is closed and the new network is inserted
|
1303 |
to the DOM. This is done in update_networks_view()
|
1304 |
*/
|
1305 |
} else {
|
1306 |
// close wizard and show error box
|
1307 |
$("a#networkscreate").overlay().close(); |
1308 |
ajax_error(jqXHR.status, undefined, 'Create network', jqXHR.responseText); |
1309 |
} |
1310 |
} |
1311 |
}); |
1312 |
return false; |
1313 |
} |
1314 |
|
1315 |
// rename network
|
1316 |
function rename_network(networkID, networkName){ |
1317 |
if (!networkID.length){
|
1318 |
//ajax_success('DEFAULT');
|
1319 |
return false; |
1320 |
} |
1321 |
// prepare payload
|
1322 |
var payload = {
|
1323 |
"network": {"name": networkName} |
1324 |
}; |
1325 |
// ajax call
|
1326 |
$.ajax({
|
1327 |
url: API_URL + '/networks/' + networkID, |
1328 |
type: "PUT", |
1329 |
contentType: "application/json", |
1330 |
dataType: "json", |
1331 |
data: JSON.stringify(payload),
|
1332 |
timeout: TIMEOUT,
|
1333 |
error: function(jqXHR, textStatus, errorThrown) { |
1334 |
try {
|
1335 |
ajax_error(jqXHR.status, undefined, 'Rename network', jqXHR.responseText); |
1336 |
} catch (err) {
|
1337 |
ajax_error(0, undefined, 'Rename network', jqXHR.responseText); |
1338 |
} |
1339 |
}, |
1340 |
success: function(data, textStatus, jqXHR) { |
1341 |
if ( jqXHR.status == '204') { |
1342 |
try {
|
1343 |
console.info('renamed network' + networkID);
|
1344 |
} catch(err) {}
|
1345 |
} else {
|
1346 |
ajax_error(jqXHR.status, undefined, 'Rename network', jqXHR.responseText); |
1347 |
} |
1348 |
} |
1349 |
}); |
1350 |
return false; |
1351 |
} |
1352 |
|
1353 |
function delete_network(networkIDs){ |
1354 |
if (!networkIDs.length){
|
1355 |
//ajax_success('DEFAULT');
|
1356 |
return false; |
1357 |
} |
1358 |
// get a network
|
1359 |
var networkID = networkIDs.pop();
|
1360 |
// ajax post destroy call can have an empty request body
|
1361 |
var payload = {};
|
1362 |
// ajax call
|
1363 |
$.ajax({
|
1364 |
url: API_URL + '/networks/' + networkID, |
1365 |
type: "DELETE", |
1366 |
contentType: "application/json", |
1367 |
dataType: "json", |
1368 |
data: JSON.stringify(payload),
|
1369 |
timeout: TIMEOUT,
|
1370 |
error: function(jqXHR, textStatus, errorThrown) { |
1371 |
try {
|
1372 |
display_net_failure(jqXHR.status, networkID, 'Delete', jqXHR.responseText);
|
1373 |
} catch (err) {
|
1374 |
display_net_failure(0, networkID, 'Delete', jqXHR.responseText); |
1375 |
} |
1376 |
}, |
1377 |
success: function(data, textStatus, jqXHR) { |
1378 |
if ( jqXHR.status == '204') { |
1379 |
try {
|
1380 |
console.info('deleted network ' + networkID);
|
1381 |
} catch(err) {}
|
1382 |
// continue with the rest of the servers
|
1383 |
delete_network(networkIDs); |
1384 |
} else {
|
1385 |
try {
|
1386 |
display_net_failure(jqXHR.status, networkID, 'Delete', jqXHR.responseText);
|
1387 |
} catch (err) {
|
1388 |
display_net_failure(0, networkID, 'Delete', jqXHR.responseText); |
1389 |
} |
1390 |
} |
1391 |
} |
1392 |
}); |
1393 |
return false; |
1394 |
} |
1395 |
|
1396 |
function add_server_to_network(networkID, serverIDs, serverNames, serverStates) { |
1397 |
if (!serverIDs.length){
|
1398 |
// close the overlay when all the calls are made
|
1399 |
$("a#add-machines-overlay").overlay().close(); |
1400 |
return false; |
1401 |
} |
1402 |
// get a server
|
1403 |
var serverID = serverIDs.pop();
|
1404 |
var serverName = serverNames.pop();
|
1405 |
var serverState = serverStates.pop();
|
1406 |
// prepare payload
|
1407 |
var payload = {
|
1408 |
"add": { "serverRef": serverID } |
1409 |
}; |
1410 |
// prepare ajax call
|
1411 |
$.ajax({
|
1412 |
url: API_URL + '/networks/' + networkID + '/action', |
1413 |
type: "POST", |
1414 |
contentType: "application/json", |
1415 |
dataType: "json", |
1416 |
data: JSON.stringify(payload),
|
1417 |
timeout: TIMEOUT,
|
1418 |
error: function(jqXHR, textStatus, errorThrown) { |
1419 |
try {
|
1420 |
// close wizard and show error box
|
1421 |
$("a#add-machines-overlay").data('overlay').close(); |
1422 |
ajax_error(jqXHR.status, undefined, 'Add server to network', jqXHR.responseText); |
1423 |
} catch (err) {
|
1424 |
ajax_error(0, undefined, 'Add server to network', jqXHR.responseText); |
1425 |
} |
1426 |
}, |
1427 |
success: function(data, textStatus, jqXHR) { |
1428 |
if ( jqXHR.status == '202') { |
1429 |
try {
|
1430 |
console.info('added server ' + serverID + ' to network ' + networkID); |
1431 |
} catch(err) {}
|
1432 |
// toggle the reboot dialog
|
1433 |
display_reboot_dialog(networkID, serverID, serverName, serverState); |
1434 |
// continue with the rest of the servers
|
1435 |
add_server_to_network(networkID, serverIDs, serverNames, serverStates); |
1436 |
} else {
|
1437 |
// close wizard and show error box
|
1438 |
$("a#add-machines-overlay").data('overlay').close(); |
1439 |
ajax_error(jqXHR.status, undefined, 'Add server to network', jqXHR.responseText); |
1440 |
} |
1441 |
} |
1442 |
}); |
1443 |
return false; |
1444 |
} |
1445 |
|
1446 |
function remove_server_from_network(networkIDs, serverIDs, serverNames, serverStates) { |
1447 |
if (!networkIDs.length){
|
1448 |
//ajax_success('DEFAULT');
|
1449 |
return false; |
1450 |
} |
1451 |
// get a network and a server
|
1452 |
var networkID = networkIDs.pop();
|
1453 |
var serverID = serverIDs.pop();
|
1454 |
var serverName = serverNames.pop();
|
1455 |
var serverState = serverStates.pop();
|
1456 |
// prepare payload
|
1457 |
var payload = {
|
1458 |
"remove": { "serverRef": serverID } |
1459 |
}; |
1460 |
// prepare ajax call
|
1461 |
$.ajax({
|
1462 |
url: API_URL + '/networks/' + networkID + '/action', |
1463 |
type: "POST", |
1464 |
contentType: "application/json", |
1465 |
dataType: "json", |
1466 |
data: JSON.stringify(payload),
|
1467 |
timeout: TIMEOUT,
|
1468 |
error: function(jqXHR, textStatus, errorThrown) { |
1469 |
try {
|
1470 |
ajax_error(jqXHR.status, undefined, 'Remove server form network', jqXHR.responseText); |
1471 |
} catch (err) {
|
1472 |
ajax_error(0, undefined, 'Remove server form network', jqXHR.responseText); |
1473 |
} |
1474 |
}, |
1475 |
success: function(data, textStatus, jqXHR) { |
1476 |
if ( jqXHR.status == '202') { |
1477 |
try {
|
1478 |
console.info('deleted server ' + serverID + ' from network ' + networkID); |
1479 |
} catch(err) {}
|
1480 |
// toggle the reboot dialog
|
1481 |
display_reboot_dialog(networkID, serverID, serverName, serverState); |
1482 |
// continue with the rest of the servers
|
1483 |
remove_server_form_network(networkIDs, serverIDs, serverNames, serverStates); |
1484 |
} else {
|
1485 |
ajax_error(jqXHR.status, undefined, 'Remove server form network', jqXHR.responseText); |
1486 |
} |
1487 |
} |
1488 |
}); |
1489 |
return false; |
1490 |
} |
1491 |
|
1492 |
function set_firewall(networkID, serverID, profile) { |
1493 |
if (!networkID.length || !serverID.length || !profile.length){
|
1494 |
return false; |
1495 |
} |
1496 |
// prepare payload
|
1497 |
var payload = {
|
1498 |
"firewallProfile": { "profile": profile } |
1499 |
}; |
1500 |
// prepare ajax call
|
1501 |
$.ajax({
|
1502 |
url: API_URL + '/servers/' + serverID + '/action', |
1503 |
type: "POST", |
1504 |
contentType: "application/json", |
1505 |
dataType: "json", |
1506 |
data: JSON.stringify(payload),
|
1507 |
timeout: TIMEOUT,
|
1508 |
error: function(jqXHR, textStatus, errorThrown) { |
1509 |
try {
|
1510 |
ajax_error(jqXHR.status, undefined, 'Set firewall profile', jqXHR.responseText); |
1511 |
} catch (err) {
|
1512 |
ajax_error(0, undefined, 'Set firewall profile', jqXHR.responseText); |
1513 |
} |
1514 |
}, |
1515 |
success: function(data, textStatus, jqXHR) { |
1516 |
if ( jqXHR.status == '202') { |
1517 |
try {
|
1518 |
console.info('for server ' + serverID + ' set firewall profile to ' + profile); |
1519 |
} catch(err) {}
|
1520 |
//remove progress gif and toggle the content
|
1521 |
$('div#net-' + networkID + '-server-' + serverID + ' button.firewall-apply').html(VARIOUS["APPLY"]); |
1522 |
$('div#net-' + networkID + '-server-' + serverID + ' button.firewall-apply').attr("disabled", false); |
1523 |
$('div#net-' + networkID + '-server-' + serverID + ' div.firewall-header').click(); |
1524 |
// toggle the reboot dialog
|
1525 |
var serverName = $('div#net-' + networkID + '-server-' + serverID + ' div.machine-name-div span.name').text(); |
1526 |
var serverState = $('div#net-' + networkID + '-server-' + serverID + ' img.logo').attr('src').split('-')[1]; |
1527 |
serverState = serverState.split('.')[0]; |
1528 |
display_reboot_dialog(networkID, serverID, serverName, serverState); |
1529 |
} else {
|
1530 |
ajax_error(jqXHR.status, undefined, 'Set firewall profile', jqXHR.responseText); |
1531 |
} |
1532 |
} |
1533 |
}); |
1534 |
return false; |
1535 |
} |
1536 |
|
1537 |
// show the welcome screen
|
1538 |
function showWelcome() { |
1539 |
$("#view-select").fadeOut("fast"); |
1540 |
$("#emptymachineslist").fadeIn("fast"); |
1541 |
$("#machinesview").hide(); |
1542 |
} |
1543 |
|
1544 |
// hide the welcome screen
|
1545 |
function hideWelcome() { |
1546 |
$("#emptymachineslist").fadeOut("fast"); |
1547 |
$("#view-select").fadeIn("fast"); |
1548 |
$("div#view-select").show(); |
1549 |
$("#machinesview").show(); |
1550 |
} |
1551 |
|
1552 |
function log_server_status_change(server_entry, new_status) { |
1553 |
// firebug console logging
|
1554 |
try {
|
1555 |
if ($("#machinesview-single").length > 0) { |
1556 |
console.info(server_entry.find("div.machine-details div.name").text() +
|
1557 |
' from ' + server_entry.find(".state-label").text() + |
1558 |
' to ' + STATUSES[new_status]);
|
1559 |
} else {
|
1560 |
console.info(server_entry.find("div.name span.name").text() +
|
1561 |
' from ' + server_entry.find(".status").text() + |
1562 |
' to ' + STATUSES[new_status]);
|
1563 |
} |
1564 |
} catch(err) {}
|
1565 |
} |
1566 |
|
1567 |
function get_flavor_params(flavorRef) { |
1568 |
var cpus, ram, disk;
|
1569 |
if ( flavors.length > 0 ) { |
1570 |
var current_flavor = ''; |
1571 |
for (i=0; i<flavors.length; i++) { |
1572 |
if (flavors[i]['id'] == flavorRef) { |
1573 |
current_flavor = flavors[i]; |
1574 |
} |
1575 |
} |
1576 |
cpus = current_flavor['cpu'];
|
1577 |
ram = current_flavor['ram'];
|
1578 |
disk = current_flavor['disk'];
|
1579 |
} else {
|
1580 |
cpus = 'undefined';
|
1581 |
ram = 'undefined';
|
1582 |
disk = 'undefined';
|
1583 |
} |
1584 |
return {'cpus': cpus, 'ram': ram, 'disk': disk}; |
1585 |
} |
1586 |
|
1587 |
function get_image_params(imageRef) { |
1588 |
var image_name, image_size;
|
1589 |
if ( images.length > 0 ) { |
1590 |
var current_image = ''; |
1591 |
for (i=0; i<images.length; i++) { |
1592 |
if (images[i]['id'] == imageRef) { |
1593 |
current_image = images[i]; |
1594 |
} |
1595 |
} |
1596 |
try {
|
1597 |
image_name = current_image['name'];
|
1598 |
} catch(err) { image_name = 'undefined'; } |
1599 |
try{
|
1600 |
image_size = current_image['metadata']['values']['size']; |
1601 |
} catch(err) { image_size = 'undefined'; } |
1602 |
} else {
|
1603 |
image_name = 'undefined';
|
1604 |
image_size = 'undefined';
|
1605 |
} |
1606 |
return {'name': image_name,'size': image_size}; |
1607 |
} |
1608 |
|
1609 |
function get_public_ips(server) { |
1610 |
var ip4, ip6;
|
1611 |
try {
|
1612 |
if (server.addresses.values) {
|
1613 |
$.each (server.addresses.values, function(i, value) { |
1614 |
if (value.id == 'public') { |
1615 |
try {
|
1616 |
$.each (value.values, function(i, ip) { |
1617 |
if (ip.version == '4') { |
1618 |
ip4 = ip.addr; |
1619 |
} else if (ip.version == '6') { |
1620 |
ip6 = ip.addr; |
1621 |
} else {
|
1622 |
ip4 = 'undefined';
|
1623 |
ip6 = 'undefined';
|
1624 |
} |
1625 |
}); |
1626 |
} catch (err){
|
1627 |
try{console.info('Server ' + server.id + ' has invalid ips')}catch(err){}; |
1628 |
ip4 = 'undefined';
|
1629 |
ip6 = 'undefined';
|
1630 |
} |
1631 |
} |
1632 |
}); |
1633 |
} |
1634 |
} catch (err) {
|
1635 |
try{console.info('Server ' + server.id + ' has no network addresses')}catch(err){}; |
1636 |
ip4 = 'undefined';
|
1637 |
ip6 = 'undefined';
|
1638 |
} |
1639 |
return {'ip4': ip4, 'ip6': ip6}; |
1640 |
} |
1641 |
|
1642 |
function get_private_ips(server) { |
1643 |
|
1644 |
} |
1645 |
|
1646 |
function close_all_overlays() { |
1647 |
try {
|
1648 |
$("a#networkscreate").overlay().close(); |
1649 |
} catch(err) {}
|
1650 |
try {
|
1651 |
$('a#create').overlay().close(); |
1652 |
} catch(err) {}
|
1653 |
try {
|
1654 |
$("a#add-machines-overlay").overlay().close(); |
1655 |
} catch(err) {}
|
1656 |
try {
|
1657 |
$("a#metadata-scrollable").overlay().close(); |
1658 |
} catch(err) {}
|
1659 |
} |
1660 |
|
1661 |
// logout
|
1662 |
function user_session_logout() { |
1663 |
$.cookie("X-Auth-Token", null); |
1664 |
if (window.LOGOUT_REDIRECT !== undefined) |
1665 |
{ |
1666 |
window.location = window.LOGOUT_REDIRECT; |
1667 |
} else {
|
1668 |
window.location.reload(); |
1669 |
} |
1670 |
} |
1671 |
|
1672 |
// action indicators
|
1673 |
function init_action_indicator_handlers(machines_view) |
1674 |
{ |
1675 |
// init once for each view
|
1676 |
if (window.ACTION_ICON_HANDLERS == undefined) |
1677 |
{ |
1678 |
window.ACTION_ICON_HANDLERS = {}; |
1679 |
} |
1680 |
|
1681 |
if (machines_view in window.ACTION_ICON_HANDLERS) |
1682 |
{ |
1683 |
return;
|
1684 |
} |
1685 |
window.ACTION_ICON_HANDLERS[machines_view] = 1;
|
1686 |
|
1687 |
if (machines_view == "list") |
1688 |
{ |
1689 |
// totally different logic for list view
|
1690 |
init_action_indicator_list_handlers(); |
1691 |
return;
|
1692 |
} |
1693 |
|
1694 |
function update_action_icon_indicators(force) |
1695 |
{ |
1696 |
function show(el, action) { |
1697 |
$(".action-indicator", $(el)).attr("class", "action-indicator " + action); |
1698 |
$(".action-indicator", $(el)).show(); |
1699 |
} |
1700 |
|
1701 |
function hide(el) { |
1702 |
$(".action-indicator", $(el)).hide(); |
1703 |
} |
1704 |
|
1705 |
function get_pending_actions(el) { |
1706 |
return $(".confirm_single:visible", $(el)); |
1707 |
} |
1708 |
|
1709 |
function other_indicators(el) { |
1710 |
return $("img.wave:visible, img.spinner:visible", $(el)) |
1711 |
} |
1712 |
|
1713 |
$("div.machine:visible, div.single-container").each(function(index, el){ |
1714 |
var el = $(el); |
1715 |
var pending = get_pending_actions(el);
|
1716 |
var other = other_indicators(el);
|
1717 |
var action = undefined; |
1718 |
var force_action = force;
|
1719 |
var visible = $(el).css("display") == "block"; |
1720 |
|
1721 |
if (force_action !==undefined && force_action.el !== el[0]) { |
1722 |
// force action for other vm
|
1723 |
// skipping force action
|
1724 |
force_action = undefined;
|
1725 |
} |
1726 |
|
1727 |
if (force_action !==undefined && force_action.el === el[0]) { |
1728 |
action = force_action.action; |
1729 |
} |
1730 |
|
1731 |
if (other.length >= 1) { |
1732 |
return;
|
1733 |
} |
1734 |
|
1735 |
if (pending.length >= 1 && force_action === undefined) { |
1736 |
action = $(pending.parent()).attr("class").replace("action-container",""); |
1737 |
} |
1738 |
|
1739 |
if (action in {'console':''}) { |
1740 |
return;
|
1741 |
} |
1742 |
|
1743 |
if (action !== undefined) { |
1744 |
show(el, action); |
1745 |
} else {
|
1746 |
try {
|
1747 |
if (el.attr('id') == pending_actions[0][1]) |
1748 |
{ |
1749 |
return;
|
1750 |
} |
1751 |
} catch (err) {
|
1752 |
} |
1753 |
hide(el); |
1754 |
} |
1755 |
|
1756 |
}); |
1757 |
} |
1758 |
|
1759 |
// action indicators
|
1760 |
$(".action-container").live('mouseover', function(evn) { |
1761 |
force_action = {'el': $(evn.currentTarget).parent().parent()[0], 'action':$(evn.currentTarget).attr("class").replace("action-container","")}; |
1762 |
// single view case
|
1763 |
if ($(force_action.el).attr("class") == "upper") |
1764 |
{ |
1765 |
force_action.el = $(evn.currentTarget).parent().parent().parent()[0] |
1766 |
}; |
1767 |
update_action_icon_indicators(force_action); |
1768 |
}); |
1769 |
|
1770 |
$("img.spinner, img.wave").live('hide', function(){ |
1771 |
update_action_icon_indicators(); |
1772 |
}); |
1773 |
// register events where icons should get updated
|
1774 |
|
1775 |
// hide action indicator image on mouse out, spinner appear, wave appear
|
1776 |
$(".action-container").live("mouseout", function(evn){ |
1777 |
update_action_icon_indicators(); |
1778 |
}); |
1779 |
|
1780 |
$(".confirm_single").live("click", function(evn){ |
1781 |
update_action_icon_indicators(); |
1782 |
}); |
1783 |
|
1784 |
$("img.spinner, img.wave").live('show', function(){ |
1785 |
$("div.action-indicator").hide(); |
1786 |
}); |
1787 |
|
1788 |
$(".confirm_single button.no").live('click', function(evn){ |
1789 |
$("div.action-indicator", $(evn.currentTarget).parent().parent()).hide(); |
1790 |
}); |
1791 |
|
1792 |
$(".confirm_multiple button.no").click(function(){ |
1793 |
$("div.action-indicator").hide(); |
1794 |
}); |
1795 |
|
1796 |
$(".confirm_multiple button.yes").click(function(){ |
1797 |
$("div.action-indicator").hide(); |
1798 |
}); |
1799 |
} |
1800 |
|
1801 |
function init_action_indicator_list_handlers() |
1802 |
{ |
1803 |
var skip_actions = { 'console':'','connect':'','details':'' }; |
1804 |
|
1805 |
var has_pending_confirmation = function() |
1806 |
{ |
1807 |
return $(".confirm_multiple:visible").length >= 1 |
1808 |
} |
1809 |
|
1810 |
function update_action_indicator_icons(force_action, skip_pending) |
1811 |
{ |
1812 |
// pending action based on the element class
|
1813 |
var pending_action = $(".selected", $(".actions"))[0]; |
1814 |
var selected = get_list_view_selected_machine_rows();
|
1815 |
|
1816 |
// reset previous state
|
1817 |
list_view_hide_action_indicators(); |
1818 |
|
1819 |
if (pending_action == undefined && !force_action) |
1820 |
{ |
1821 |
// no action selected
|
1822 |
return;
|
1823 |
} |
1824 |
|
1825 |
if (force_action != undefined) |
1826 |
{ |
1827 |
// user forced action choice
|
1828 |
var action_class = force_action;
|
1829 |
} else {
|
1830 |
// retrieve action name (reboot, stop, etc..)
|
1831 |
var action_class = $(pending_action).attr("id").replace("action-",""); |
1832 |
} |
1833 |
|
1834 |
selected.each(function(index, el) {
|
1835 |
if (has_pending_confirmation() && skip_pending)
|
1836 |
{ |
1837 |
return;
|
1838 |
} |
1839 |
var el = $(el); |
1840 |
var logo = $("img.list-logo", el); |
1841 |
$(".action-indicator", el).remove(); |
1842 |
var cls = "action-indicator " + action_class; |
1843 |
// add icon div
|
1844 |
logo.after('<div class="' + cls + '"></div>'); |
1845 |
// hide os logo
|
1846 |
$("img.list-logo", el).hide(); |
1847 |
}); |
1848 |
} |
1849 |
|
1850 |
// on mouseover we force the images to the hovered action
|
1851 |
$(".actions a").live("mouseover", function(evn) { |
1852 |
var el = $(evn.currentTarget); |
1853 |
if (!el.hasClass("enabled")) |
1854 |
{ |
1855 |
return;
|
1856 |
} |
1857 |
var action_class = el.attr("id").replace("action-",""); |
1858 |
if (action_class in skip_actions) |
1859 |
{ |
1860 |
return;
|
1861 |
} |
1862 |
update_action_indicator_icons(action_class, false);
|
1863 |
}); |
1864 |
|
1865 |
|
1866 |
// register events where icons should get updated
|
1867 |
$(".actions a.enabled").live("click", function(evn) { |
1868 |
// clear previous selections
|
1869 |
$("a.selected").removeClass("selected"); |
1870 |
|
1871 |
var el = $(evn.currentTarget); |
1872 |
el.addClass("selected");
|
1873 |
update_action_indicator_icons(undefined, false); |
1874 |
}); |
1875 |
|
1876 |
$(".actions a").live("mouseout", function(evn) { |
1877 |
update_action_indicator_icons(undefined, false); |
1878 |
}); |
1879 |
|
1880 |
$(".confirm_multiple button.no").click(function(){ |
1881 |
list_view_hide_action_indicators(); |
1882 |
}); |
1883 |
|
1884 |
$(".confirm_multiple button.yes").click(function(){ |
1885 |
list_view_hide_action_indicators(); |
1886 |
}); |
1887 |
|
1888 |
$("input[type=checkbox]").live('change', function(){ |
1889 |
// pending_actions will become empty on every checkbox click/change
|
1890 |
// line 154 machines_list.html
|
1891 |
pending_actions = []; |
1892 |
if (pending_actions.length == 0) |
1893 |
{ |
1894 |
$(".confirm_multiple").hide(); |
1895 |
$("a.selected").each(function(index, el){$(el).removeClass("selected")}); |
1896 |
} |
1897 |
update_action_indicator_icons(undefined, false); |
1898 |
}); |
1899 |
|
1900 |
} |
1901 |
|
1902 |
function list_view_hide_action_indicators() |
1903 |
{ |
1904 |
$("tr td .action-indicator").remove(); |
1905 |
$("tr td img.list-logo").show(); |
1906 |
} |
1907 |
|
1908 |
function get_list_view_selected_machine_rows() |
1909 |
{ |
1910 |
var table = $("table.list-machines"); |
1911 |
var rows = $("tr:has(input[type=checkbox]:checked)",table); |
1912 |
return rows;
|
1913 |
} |
1914 |
|
1915 |
// machines images utils
|
1916 |
function set_machine_os_image(machine, machines_view, state, os, skip_reset_states, remove_state) { |
1917 |
var views_map = {'single': '.single-image', 'icon': '.logo'}; |
1918 |
var states_map = {'on': 'state1', 'off': 'state3', 'hover': 'state4', 'click': 'state2'} |
1919 |
var sizes_map = {'single': 'large', 'icon': 'medium'} |
1920 |
|
1921 |
var size = sizes_map[machines_view];
|
1922 |
var img_selector = views_map[machines_view];
|
1923 |
var cls = states_map[state];
|
1924 |
|
1925 |
console.log(state, os, skip_reset_states, remove_state, cls); |
1926 |
if (os === "unknown") { os = "okeanos" } ; |
1927 |
var new_img = 'url("./static/icons/machines/' + size + '/' + os + '-sprite.png")'; |
1928 |
|
1929 |
var el = $(img_selector, machine); |
1930 |
var current_img = el.css("backgroundImage"); |
1931 |
if (os == undefined){ |
1932 |
new_img = current_img; |
1933 |
} |
1934 |
|
1935 |
// os changed
|
1936 |
el.css("backgroundImage", new_img);
|
1937 |
|
1938 |
// reset current state
|
1939 |
if (skip_reset_states === undefined) |
1940 |
{ |
1941 |
el.removeClass("single-image-state1");
|
1942 |
el.removeClass("single-image-state2");
|
1943 |
el.removeClass("single-image-state3");
|
1944 |
el.removeClass("single-image-state4");
|
1945 |
} |
1946 |
|
1947 |
if (remove_state !== undefined) |
1948 |
{ |
1949 |
remove_state = "single-image-" + states_map[remove_state];
|
1950 |
el.removeClass(remove_state); |
1951 |
return;
|
1952 |
} |
1953 |
|
1954 |
// set proper state
|
1955 |
el.addClass("single-image-" + cls);
|
1956 |
} |
1957 |
|