root / ui / static / synnefo.js @ ab5282e4
History | View | Annotate | Download (14.2 kB)
1 |
var flavors = [], images = [], servers = [], disks = [], cpus = [], ram = [];
|
---|---|
2 |
var changes_since = 0, deferred = 0; |
3 |
|
4 |
function list_view() { |
5 |
changes_since = 0; // to reload full list |
6 |
clearTimeout(deferred); // clear old deferred calls
|
7 |
$.cookie("list", '1'); // set list cookie |
8 |
$("div#machinesview").load($("#list").attr("href"), function(){ |
9 |
$("a#standard")[0].className += ' activelink'; |
10 |
$("a#list")[0].className = ''; |
11 |
}); |
12 |
return false; |
13 |
} |
14 |
|
15 |
function standard_view() { |
16 |
changes_since = 0; // to reload full list |
17 |
clearTimeout(deferred); // clear old deferred calls
|
18 |
$.cookie("list", '0'); |
19 |
href=$("a#standard").attr("href"); |
20 |
$("div#machinesview").load(href, function(){ |
21 |
$("a#list")[0].className += ' activelink'; |
22 |
$("a#standard")[0].className = ''; |
23 |
}); |
24 |
return false; |
25 |
} |
26 |
|
27 |
function choose_view() { |
28 |
if ($.cookie("list")=='1') { |
29 |
list_view(); |
30 |
} else {
|
31 |
standard_view(); |
32 |
} |
33 |
} |
34 |
|
35 |
function toggleMenu() { |
36 |
var primary = $("ul.css-tabs li a.primary"); |
37 |
var secondary = $("ul.css-tabs li a.secondary"); |
38 |
var all = $("ul.css-tabs li a"); |
39 |
var toggled = $('ul.css-tabs li a.current').hasClass('secondary'); |
40 |
|
41 |
// if anything is still moving, do nothing
|
42 |
if ($(":animated").length) { |
43 |
return;
|
44 |
} |
45 |
|
46 |
// nothing is current to begin with
|
47 |
$('ul.css-tabs li a.current').removeClass('current'); |
48 |
|
49 |
// move stuff around
|
50 |
all.animate({top:'30px'}, {complete: function() { |
51 |
$(this).hide(); |
52 |
if (toggled) {
|
53 |
primary.show(); |
54 |
primary.animate({top:'9px'}, {complete: function() { |
55 |
$('ul.css-tabs li a.primary#machines').addClass('current'); |
56 |
$('a#machines').click(); |
57 |
}}); |
58 |
} else {
|
59 |
secondary.show(); |
60 |
secondary.animate({top:'9px'}, {complete: function() { |
61 |
$('ul.css-tabs li a.secondary#files').addClass('current'); |
62 |
$('a#files').click(); |
63 |
}}); |
64 |
} |
65 |
}}); |
66 |
|
67 |
// rotate arrow icon
|
68 |
if (toggled) {
|
69 |
$("#arrow").rotate({animateAngle: (0), bind:[{"click":function(){toggleMenu()}}]}); |
70 |
$("#arrow").rotateAnimation(0); |
71 |
} else {
|
72 |
$("#arrow").rotate({animateAngle: (-180), bind:[{"click":function(){toggleMenu()}}]}); |
73 |
$("#arrow").rotateAnimation(-180); |
74 |
} |
75 |
} |
76 |
|
77 |
// confirmation overlay generation
|
78 |
function confirm_action(action_string, action_function, serverIDs, serverNames) { |
79 |
if (serverIDs.length == 1){ |
80 |
$("#yes-no h3").text('You are about to ' + action_string + ' vm ' + serverNames[0]); |
81 |
} else if (serverIDs.length > 1){ |
82 |
$("#yes-no h3").text('You are about to ' + action_string + ' ' + serverIDs.length + ' machines'); |
83 |
} else {
|
84 |
return false; |
85 |
} |
86 |
// action confirmation overlay
|
87 |
var triggers = $("a#confirmation").overlay({ |
88 |
// some mask tweaks suitable for modal dialogs
|
89 |
mask: {
|
90 |
color: '#ebecff', |
91 |
opacity: '0.9' |
92 |
}, |
93 |
top: 'center', |
94 |
load: false |
95 |
}); |
96 |
// yes or no?
|
97 |
var buttons = $("#yes-no button").click(function(e) { |
98 |
// get user input
|
99 |
var yes = buttons.index(this) === 0; |
100 |
//close the confirmation window
|
101 |
$("a#confirmation").overlay().close(); |
102 |
// return true=yes or false=no
|
103 |
if (yes) {
|
104 |
action_function(serverIDs); |
105 |
} |
106 |
}); |
107 |
$("a#confirmation").data('overlay').load(); |
108 |
return false; |
109 |
} |
110 |
|
111 |
// get and show a list of running and terminated machines
|
112 |
function update_vms(interval) { |
113 |
try{ console.info('updating machines'); } catch(err){} |
114 |
var uri='/api/v1.0/servers/detail'; |
115 |
|
116 |
if (changes_since > 0) |
117 |
uri+='?changes-since='+changes_since
|
118 |
|
119 |
$.ajax({
|
120 |
url: uri,
|
121 |
type: "GET", |
122 |
timeout: TIMEOUT,
|
123 |
dataType: "json", |
124 |
error: function(jqXHR, textStatus, errorThrown) { |
125 |
// don't forget to try again later
|
126 |
if (interval) {
|
127 |
deferred = setTimeout(update_vms,interval,interval); |
128 |
} |
129 |
// as for now, just show an error message
|
130 |
if (jqXHR.status != undefined) { |
131 |
ajax_error(jqXHR.status); |
132 |
} else {
|
133 |
ajax_error(); |
134 |
} |
135 |
return false; |
136 |
}, |
137 |
success: function(data, textStatus, jqXHR) { |
138 |
changes_since = Date.parse(jqXHR.getResponseHeader('Date'))/1000; |
139 |
if (jqXHR.status != 304) { |
140 |
try {
|
141 |
servers = data.servers; |
142 |
} catch(err) { ajax_error('400');} |
143 |
update_machines_view(data); |
144 |
} |
145 |
if (interval) {
|
146 |
deferred = setTimeout(update_vms,interval,interval); |
147 |
} |
148 |
return false; |
149 |
} |
150 |
}); |
151 |
return false; |
152 |
} |
153 |
|
154 |
// get and show a list of available standard and custom images
|
155 |
function update_images() { |
156 |
$.ajax({
|
157 |
url: '/api/v1.0/images/detail', |
158 |
type: "GET", |
159 |
//async: false,
|
160 |
dataType: "json", |
161 |
timeout: TIMEOUT,
|
162 |
error: function(jqXHR, textStatus, errorThrown) { |
163 |
ajax_error(jqXHR.status); |
164 |
}, |
165 |
success: function(data, textStatus, jqXHR) { |
166 |
try {
|
167 |
images = data.images; |
168 |
} catch(err){
|
169 |
ajax_error("NO_IMAGES");
|
170 |
} |
171 |
if ($("ul#standard-images li").toArray().length + $("ul#custom-images li").toArray().length == 0) { |
172 |
$.each(data.images, function(i,image){ |
173 |
var img = $('#image-template').clone().attr("id","img-"+image.id).fadeIn("slow"); |
174 |
img.find("label").attr('for',"img-radio-" + image.id); |
175 |
img.find(".image-title").text(image.name);
|
176 |
img.find(".description").text(image.description);
|
177 |
img.find(".size").text(image.size);
|
178 |
img.find("input.radio").attr('id',"img-radio-" + image.id); |
179 |
if (i==0) img.find("input.radio").attr("checked","checked"); |
180 |
img.find("img.image-logo").attr('src','static/os_logos/'+image_tags[image.id]+'.png'); |
181 |
if (image.serverId) {
|
182 |
img.appendTo("ul#custom-images");
|
183 |
} else {
|
184 |
img.appendTo("ul#standard-images");
|
185 |
} |
186 |
}); |
187 |
} |
188 |
} |
189 |
}); |
190 |
return false; |
191 |
} |
192 |
|
193 |
Array.prototype.unique = function () { |
194 |
var r = new Array(); |
195 |
o:for(var i = 0, n = this.length; i < n; i++) |
196 |
{ |
197 |
for(var x = 0, y = r.length; x < y; x++) |
198 |
{ |
199 |
if(r[x]==this[i]) |
200 |
{ |
201 |
continue o;
|
202 |
} |
203 |
} |
204 |
r[r.length] = this[i];
|
205 |
} |
206 |
return r;
|
207 |
} |
208 |
|
209 |
// get and configure flavor selection
|
210 |
function update_flavors() { |
211 |
$.ajax({
|
212 |
url: '/api/v1.0/flavors/detail', |
213 |
type: "GET", |
214 |
//async: false,
|
215 |
dataType: "json", |
216 |
timeout: TIMEOUT,
|
217 |
error: function(jqXHR, textStatus, errorThrown) { |
218 |
try {
|
219 |
ajax_error(jqXHR.status); |
220 |
} catch (err) {
|
221 |
ajax_error(err); |
222 |
} |
223 |
}, |
224 |
success: function(data, textStatus, jqXHR) { |
225 |
flavors = data.flavors; |
226 |
$.each(flavors, function(i, flavor) { |
227 |
cpus[i] = flavor['cpu'];
|
228 |
disks[i] = flavor['disk'];
|
229 |
ram[i] = flavor['ram'];
|
230 |
}); |
231 |
cpus = cpus.unique(); |
232 |
disks = disks.unique(); |
233 |
ram = ram.unique(); |
234 |
// sliders for selecting VM flavor
|
235 |
$("#cpu:range").rangeinput({min:0, |
236 |
value:0, |
237 |
step:1, |
238 |
progress: true, |
239 |
max:cpus.length-1}); |
240 |
|
241 |
$("#storage:range").rangeinput({min:0, |
242 |
value:0, |
243 |
step:1, |
244 |
progress: true, |
245 |
max:disks.length-1}); |
246 |
|
247 |
$("#ram:range").rangeinput({min:0, |
248 |
value:0, |
249 |
step:1, |
250 |
progress: true, |
251 |
max:ram.length-1}); |
252 |
$("#small").click(); |
253 |
|
254 |
// update the indicators when sliding
|
255 |
$("#cpu:range").data().rangeinput.onSlide(function(event,value){ |
256 |
$("#cpu-indicator")[0].value = cpus[Number(value)]; |
257 |
}); |
258 |
$("#cpu:range").data().rangeinput.change(function(event,value){ |
259 |
$("#cpu-indicator")[0].value = cpus[Number(value)]; |
260 |
$("#custom").click(); |
261 |
}); |
262 |
$("#ram:range").data().rangeinput.onSlide(function(event,value){ |
263 |
$("#ram-indicator")[0].value = ram[Number(value)]; |
264 |
}); |
265 |
$("#ram:range").data().rangeinput.change(function(event,value){ |
266 |
$("#ram-indicator")[0].value = ram[Number(value)]; |
267 |
$("#custom").click(); |
268 |
}); |
269 |
$("#storage:range").data().rangeinput.onSlide(function(event,value){ |
270 |
$("#storage-indicator")[0].value = disks[Number(value)]; |
271 |
}); |
272 |
$("#storage:range").data().rangeinput.change(function(event,value){ |
273 |
$("#storage-indicator")[0].value = disks[Number(value)]; |
274 |
$("#custom").click(); |
275 |
}); |
276 |
} |
277 |
}); |
278 |
return false; |
279 |
} |
280 |
|
281 |
// return flavorRef from cpu, disk, ram values
|
282 |
function identify_flavor(cpu, disk, ram){ |
283 |
for (i=0;i<flavors.length;i++){ |
284 |
if (flavors[i]['cpu'] == cpu && flavors[i]['disk']==disk && flavors[i]['ram']==ram) { |
285 |
return flavors[i]['id'] |
286 |
} |
287 |
} |
288 |
return 0; |
289 |
} |
290 |
|
291 |
// update the actions in the
|
292 |
function updateActions() { |
293 |
var states = [];
|
294 |
var on = [];
|
295 |
var checked = $("table.list-machines tbody input[type='checkbox']:checked"); |
296 |
// disable all actions to begin with
|
297 |
for (action in actions) { |
298 |
$("#action-" + action).removeClass('enabled'); |
299 |
} |
300 |
|
301 |
// are there multiple machines selected?
|
302 |
if (checked.length>1) |
303 |
states[0] = 'multiple'; |
304 |
|
305 |
// check the states of selected machines
|
306 |
checked.each(function(i,checkbox) {
|
307 |
states[states.length] = checkbox.className; |
308 |
var ip = $("#" + checkbox.id.replace('input-','') + ".ip span.public").text(); |
309 |
if (ip.replace('undefined','').length) |
310 |
states[states.length] = 'network';
|
311 |
}); |
312 |
|
313 |
// decide which actions should be enabled
|
314 |
for (a in actions) { |
315 |
var enabled = false; |
316 |
for (var s =0; s<states.length; s++) { |
317 |
if (actions[a].indexOf(states[s]) != -1 ) { |
318 |
enabled = true;
|
319 |
} else {
|
320 |
enabled = false;
|
321 |
break;
|
322 |
} |
323 |
} |
324 |
if (enabled)
|
325 |
on[on.length]=a; |
326 |
} |
327 |
// enable those actions
|
328 |
for (action in on) { |
329 |
$("#action-" + on[action]).addClass('enabled'); |
330 |
} |
331 |
} |
332 |
|
333 |
// reboot action
|
334 |
function reboot(serverIDs){ |
335 |
if (!serverIDs.length){
|
336 |
ajax_success('DEFAULT');
|
337 |
return false; |
338 |
} |
339 |
// ajax post reboot call
|
340 |
var payload = {
|
341 |
"reboot": {"type" : "HARD"} |
342 |
}; |
343 |
serverID = serverIDs.pop(); |
344 |
|
345 |
$.ajax({
|
346 |
url: '/api/v1.0/servers/' + serverID + '/action', |
347 |
type: "POST", |
348 |
dataType: "json", |
349 |
data: JSON.stringify(payload),
|
350 |
timeout: TIMEOUT,
|
351 |
error: function(jqXHR, textStatus, errorThrown) { |
352 |
ajax_error(jqXHR.status); |
353 |
}, |
354 |
success: function(data, textStatus, jqXHR) { |
355 |
if ( jqXHR.status == '202') { |
356 |
try {
|
357 |
console.info('rebooted ' + serverID);
|
358 |
} catch(err) {}
|
359 |
reboot(serverIDs); |
360 |
} else {
|
361 |
ajax_error(jqXHR.status); |
362 |
} |
363 |
} |
364 |
}); |
365 |
|
366 |
return false; |
367 |
} |
368 |
|
369 |
// shutdown action
|
370 |
function shutdown(serverIDs) { |
371 |
if (!serverIDs.length){
|
372 |
ajax_success('DEFAULT');
|
373 |
return false; |
374 |
} |
375 |
// ajax post shutdown call
|
376 |
var payload = {
|
377 |
"shutdown": {"timeout" : "5"} |
378 |
}; |
379 |
|
380 |
serverID = serverIDs.pop() |
381 |
$.ajax({
|
382 |
url: '/api/v1.0/servers/' + serverID + '/action', |
383 |
type: "POST", |
384 |
dataType: "json", |
385 |
data: JSON.stringify(payload),
|
386 |
timeout: TIMEOUT,
|
387 |
error: function(jqXHR, textStatus, errorThrown) { |
388 |
ajax_error(jqXHR.status); |
389 |
}, |
390 |
success: function(data, textStatus, jqXHR) { |
391 |
if ( jqXHR.status == '202') { |
392 |
try {
|
393 |
console.info('suspended ' + serverID);
|
394 |
} catch(err) {}
|
395 |
shutdown(serverIDs); |
396 |
} else {
|
397 |
ajax_error(jqXHR.status); |
398 |
} |
399 |
} |
400 |
}); |
401 |
|
402 |
return false; |
403 |
} |
404 |
|
405 |
// destroy action
|
406 |
function destroy(serverIDs) { |
407 |
if (!serverIDs.length){
|
408 |
ajax_success('DEFAULT');
|
409 |
return false; |
410 |
} |
411 |
// ajax post destroy call can have an empty request body
|
412 |
var payload = {};
|
413 |
|
414 |
serverID = serverIDs.pop() |
415 |
$.ajax({
|
416 |
url: '/api/v1.0/servers/' + serverID, |
417 |
type: "DELETE", |
418 |
dataType: "json", |
419 |
data: JSON.stringify(payload),
|
420 |
timeout: TIMEOUT,
|
421 |
error: function(jqXHR, textStatus, errorThrown) { |
422 |
ajax_error(jqXHR.status); |
423 |
}, |
424 |
success: function(data, textStatus, jqXHR) { |
425 |
if ( jqXHR.status == '202') { |
426 |
try {
|
427 |
console.info('destroyed ' + serverID);
|
428 |
} catch (err) {}
|
429 |
destroy(serverIDs); |
430 |
} else {
|
431 |
ajax_error(jqXHR.status); |
432 |
} |
433 |
} |
434 |
}); |
435 |
|
436 |
return false; |
437 |
} |
438 |
|
439 |
// start action
|
440 |
function start(serverIDs){ |
441 |
if (!serverIDs.length){
|
442 |
ajax_success('DEFAULT');
|
443 |
return false; |
444 |
} |
445 |
// ajax post start call
|
446 |
var payload = {
|
447 |
"start": {"type" : "NORMAL"} |
448 |
}; |
449 |
|
450 |
serverID = serverIDs.pop() |
451 |
$.ajax({
|
452 |
url: '/api/v1.0/servers/' + serverID + '/action', |
453 |
type: "POST", |
454 |
dataType: "json", |
455 |
data: JSON.stringify(payload),
|
456 |
timeout: TIMEOUT,
|
457 |
error: function(jqXHR, textStatus, errorThrown) { |
458 |
ajax_error(jqXHR.status); |
459 |
}, |
460 |
success: function(data, textStatus, jqXHR) { |
461 |
if ( jqXHR.status == '202') { |
462 |
try {
|
463 |
console.info('started ' + serverID);
|
464 |
} catch(err) {}
|
465 |
start(serverIDs); |
466 |
} else {
|
467 |
ajax_error(jqXHR.status); |
468 |
} |
469 |
} |
470 |
}); |
471 |
|
472 |
return false; |
473 |
} |