Statistics
| Branch: | Tag: | Revision:

root / ui / static / synnefo.js @ 42f67a2a

History | View | Annotate | Download (15.7 kB)

1
function list_view() {
2
    $.cookie("list", '1'); // set list cookie
3
    $("div#machinesview").load($("#list").attr("href"), function(){
4
        $("a#standard")[0].className += ' activelink';
5
        $("a#list")[0].className = '';
6
    });
7
    return false;
8
}
9

    
10
function standard_view() {
11
    $.cookie("list", '0');
12
    href=$("a#standard").attr("href");
13
    $("div#machinesview").load(href, function(){
14
        $("a#list")[0].className += ' activelink';
15
        $("a#standard")[0].className = '';
16
    });
17
    return false;
18
}
19

    
20
function choose_view() {
21
    if ($.cookie("list")=='1') {
22
        list_view();
23
    } else {
24
        standard_view();
25
    }
26
}
27

    
28
function toggleMenu() {
29
    var primary = $("ul.css-tabs li a.primary");
30
    var secondary = $("ul.css-tabs li a.secondary");
31
    var all = $("ul.css-tabs li a");                        
32
    var toggled = $('ul.css-tabs li a.current').hasClass('secondary');
33
    
34
    // if anything is still moving, do nothing
35
    if ($(":animated").length) {
36
        return;
37
    } 
38
    
39
    // nothing is current to begin with
40
    $('ul.css-tabs li a.current').removeClass('current');
41
    
42
    // move stuff around
43
    all.animate({top:'30px'}, {complete: function() { 
44
        $(this).hide();
45
        if (toggled) {
46
            primary.show();
47
            primary.animate({top:'9px'}, {complete: function() {
48
                $('ul.css-tabs li a.primary#machines').addClass('current');
49
                $('a#machines').click();                                   
50
            }});
51
        } else {
52
            secondary.show();
53
            secondary.animate({top:'9px'}, {complete: function() {
54
                $('ul.css-tabs li a.secondary#files').addClass('current');
55
                $('a#files').click();                                                                           
56
            }});
57
        }                                              
58
    }});
59
    
60
    // rotate arrow icon
61
    if (toggled) {
62
        $("#arrow").rotate({animateAngle: (0), bind:[{"click":function(){toggleMenu()}}]});
63
        $("#arrow").rotateAnimation(0);                    
64
    } else {
65
        $("#arrow").rotate({animateAngle: (-180), bind:[{"click":function(){toggleMenu()}}]});
66
        $("#arrow").rotateAnimation(-180);
67
    }            
68
}
69

    
70
// confirmation overlay generation
71
function confirm_action(action_string, action_function, serverIDs, serverNames) {
72
    if (serverIDs.length == 1){
73
        $("#yes-no h3").text('You are about to ' + action_string + ' vm ' + serverNames[0]);
74
    } else if (serverIDs.length > 1){
75
        $("#yes-no h3").text('You are about to ' + action_string + ' ' + serverIDs.length + 'machines');
76
    } else {
77
        return false;
78
    }
79
    // action confirmation overlay
80
    var triggers = $("a#confirmation").overlay({
81
            // some mask tweaks suitable for modal dialogs
82
            mask: {
83
                    color: '#ebecff',
84
                    opacity: '0.9'
85
            },
86
        top: 'center',
87
        load: false
88
    });
89
    // yes or no?
90
    var buttons = $("#yes-no button").click(function(e) {
91
            // get user input
92
            var yes = buttons.index(this) === 0;
93
        //close the confirmation window
94
        $("a#confirmation").overlay().close(); 
95
        // return true=yes or false=no
96
        if (yes) {
97
            action_function(serverIDs);
98
        } else {
99
            // reload page
100
            choose_view();
101
        }
102
    });
103
    $("a#confirmation").data('overlay').load();
104
    return false;
105
}
106

    
107
// get and show a list of running and terminated machines
108
function update_vms() {
109
    console.info('updating machines');
110
    $(".running").text('');
111
    $(".terminated").text('');
112

    
113
    $.ajax({
114
        url: '/api/v1.0/servers/detail',
115
        type: "GET",
116
        timeout: TIMEOUT,
117
        dataType: "json",
118
        error: function(jqXHR, textStatus, errorThrown) { 
119
                    ajax_error(jqXHR);
120
                    return false;
121
                    },
122
        success: function(data, textStatus, jqXHR) {
123
            if ($(".running a.name").length + $(".terminated a.name").length == 0) {
124
            
125
                $.each(data.servers, function(i,server){
126
                    // if the machine is deleted it should not be included in any list
127
                    if (server.status == 'DELETED') {
128
                        return;
129
                    }
130
                    var machine = $("#machine-template").clone().attr("id", server.id).fadeIn("slow");
131
                    machine.find("input[type='checkbox']").attr("id", "input-" + server.id);
132
                    machine.find("input[type='checkbox']").attr("class", server.status);
133
                    machine.find("a.name span.name").text(server.name);
134
                    machine.find("img.logo").attr("src","static/machines/"+image_tags[server.imageId]+'.png');
135
                    machine.find("img.list-logo").attr("src","static/os_logos/"+image_tags[server.imageId]+'.png');
136
                    machine.find("img.list-logo").attr("title",image_tags[server.imageId]);
137
                    machine.find("span.imagetag").text(image_tags[server.imageId]);
138
    
139
                    machine.find("a.ip span.public").text(String(server.addresses.public.ip.addr).replace(',',' '));            
140
    
141
                    // TODO: handle SHARE_IP, SHARE_IP_NO_CONFIG, DELETE_IP, REBUILD, QUEUE_RESIZE, PREP_RESIZE, RESIZE, VERIFY_RESIZE, PASSWORD, RESCUE
142
                    if (server.status == 'BUILD'){
143
                        machine.find(".status").text('Building');
144
                        machine.appendTo(".running");
145
                    } else if (server.status == 'ACTIVE') {
146
                        machine.find(".status").text('Running');
147
                        machine.appendTo(".running"); 
148
                    } else if (server.status == 'REBOOT' || server.status == 'HARD_REBOOT') {
149
                        machine.find(".status").text('Rebooting');
150
                        machine.appendTo(".running");
151
                    } else if (server.status == 'STOPPED') {
152
                        machine.find(".status").text('Stopped');
153
                        machine.find("img.logo").attr("src","static/machines/"+image_tags[server.imageId]+'-off.png');
154
                        machine.find("img.list-logo").attr("src","static/os_logos/"+image_tags[server.imageId]+'-off.png');
155
                        machine.appendTo(".terminated");
156
                    } else if (server.status == 'ERROR') {
157
                        machine.find(".status").text('Error');
158
                        machine.find("img.logo").attr("src","static/machines/"+image_tags[server.imageId]+'-off.png');
159
                        machine.find("img.list-logo").attr("src","static/os_logos/"+image_tags[server.imageId]+'-off.png');
160
                        machine.appendTo(".terminated");
161
                    } 
162
                    else {
163
                        machine.find(".status").text('Unknown');
164
                        machine.find("img.logo").attr("src","static/machines/"+image_tags[server.imageId]+'-off.png');
165
                        machine.find("img.list-logo").attr("src","static/os_logos/"+image_tags[server.imageId]+'-off.png');
166
                        machine.appendTo(".terminated");
167
                    }
168
                });
169
            }
170
            $("#spinner").hide();
171
            $("div.machine:last-child").find("div.seperator").hide();
172
            // if the terminated list is populated then the seperator must be shown
173
            if ($(".terminated a.name").length > 0) {
174
                $("#mini.seperator").fadeIn("slow");
175
            }
176
            // creating the table in list view, if there are machines to show
177
            if ($("div.list table.list-machines tbody").length > 0) {
178
                $("div.list table.list-machines").dataTable({
179
                    "bInfo": false,
180
                    "bPaginate": false,
181
                            "bAutoWidth": false,
182
                            "bSort": true,    
183
                    "bStateSave": true,
184
                    //"sScrollY": "250px",
185
                    //"sScrollX": "500px",
186
                    //"sScrollXInner": "480px",
187
                    "aoColumnDefs": [
188
                        { "bSortable": false, "aTargets": [ 0 ] }
189
                    ]
190
                });
191
                $("div.list table.list-machines").show();
192
                $("div.list div.actions").show();
193
            }
194
        }
195
    });
196
    return false;
197
}
198

    
199
// get and show a list of anvailable standard and custom images
200
function update_images() { 
201
    $.ajax({
202
        url: '/api/v1.0/images/detail',
203
        type: "GET",
204
        //async: false,
205
        dataType: "json",
206
        timeout: TIMEOUT,
207
        error: function(jqXHR, textStatus, errorThrown) { 
208
                    ajax_error(jqXHR);
209
                    },
210
        success: function(data, textStatus, jqXHR) {
211
            if ($("ul#standard-images li").toArray().length + $("ul#custom-images li").toArray().length == 0) {
212
                $.each(data.images, function(i,image){
213
                    var img = $('#image-template').clone().attr("id","img-"+image.id).fadeIn("slow");
214
                    img.find("label").attr('for',"img-radio-" + image.id);
215
                    img.find(".image-title").text(image.name);
216
                    img.find(".description").text(image.description);
217
                    img.find(".size").text(image.size);
218
                    img.find("input.radio").attr('id',"img-radio-" + image.id);
219
                    if (i==0) img.find("input.radio").attr("checked","checked"); 
220
                    img.find("img.image-logo").attr('src','static/os_logos/'+image_tags[image.id]+'.png');
221
                    if (image.serverId) {
222
                        img.appendTo("ul#custom-images");
223
                    } else {
224
                        img.appendTo("ul#standard-images");
225
                    }
226
                });
227
            }
228
        }
229
    });
230
    return false;
231
}
232

    
233
var flavors = {}, disks = [], cpus = [], ram = [];
234

    
235
Array.prototype.unique = function () {
236
        var r = new Array();
237
        o:for(var i = 0, n = this.length; i < n; i++)
238
        {
239
                for(var x = 0, y = r.length; x < y; x++)
240
                {
241
                        if(r[x]==this[i])
242
                        {
243
                                continue o;
244
                        }
245
                }
246
                r[r.length] = this[i];
247
        }
248
        return r;
249
}
250

    
251
// get and configure flavor selection
252
function update_flavors() { 
253
    $.ajax({
254
        url: '/api/v1.0/flavors/detail',
255
        type: "GET",
256
        //async: false,
257
        dataType: "json",
258
        timeout: TIMEOUT,
259
        error: function(jqXHR, textStatus, errorThrown) { 
260
            ajax_error(jqXHR);
261
        },
262
        success: function(data, textStatus, jqXHR) {
263
            flavors = data.flavors;
264
            $.each(flavors, function(i, flavor) {
265
                cpus[i] = flavor['cpu'];
266
                disks[i] = flavor['disk'];
267
                ram[i] = flavor['ram'];
268
            });
269
            cpus = cpus.unique();
270
            disks = disks.unique();
271
            ram = ram.unique();
272
            // sliders for selecting VM flavor
273
            $("#cpu:range").rangeinput({min:0,
274
                                       value:0,
275
                                       step:1,
276
                                       progress: true,
277
                                       max:cpus.length-1});
278
            
279
            $("#storage:range").rangeinput({min:0,
280
                                       value:0,
281
                                       step:1,
282
                                       progress: true,
283
                                       max:disks.length-1});
284

    
285
            $("#ram:range").rangeinput({min:0,
286
                                       value:0,
287
                                       step:1,
288
                                       progress: true,
289
                                       max:ram.length-1});
290
            $("#small").click();
291
            
292
            // update the indicators when sliding
293
            $("#cpu:range").data().rangeinput.onSlide(function(event,value){
294
                $("#cpu-indicator")[0].value = cpus[Number(value)];
295
                $("#custom").click();
296
            });
297
            $("#ram:range").data().rangeinput.onSlide(function(event,value){
298
                $("#ram-indicator")[0].value = ram[Number(value)];
299
                $("#custom").click();
300
            });
301
            $("#storage:range").data().rangeinput.onSlide(function(event,value){
302
                $("#storage-indicator")[0].value = disks[Number(value)];
303
                $("#custom").click();
304
            });
305
        }
306
    });
307
    return false;
308
}
309
// return flavorId from cpu, disk, ram values
310
function identify_flavor(cpu, disk, ram){
311
    for (i=0;i<flavors.length;i++){
312
        if (flavors[i]['cpu'] == cpu && flavors[i]['disk']==disk && flavors[i]['ram']==ram) {
313
            return flavors[i]['id']
314
        }
315
    }
316
    return 0;
317
}
318

    
319
// reboot action
320
function reboot(serverIDs){
321
        if (!serverIDs.length){
322
                ajax_success();
323
                return false;
324
        }        
325
    // ajax post reboot call
326
    var payload = {
327
        "reboot": {"type" : "HARD"}
328
    };
329
    serverID = serverIDs.pop();
330
        
331
        $.ajax({
332
                url: '/api/v1.0/servers/' + serverID + '/action',
333
                type: "POST",        
334
                dataType: "json",
335
                data: JSON.stringify(payload),
336
                timeout: TIMEOUT,
337
                error: function(jqXHR, textStatus, errorThrown) {
338
                                        ajax_error(jqXHR, serverID);
339
                                },
340
                success: function(data, textStatus, jqXHR) {
341
                                        if ( jqXHR.status != '202') {
342
                                                ajax_error(jqXHR, serverID);
343
                                        } else {
344
                                                console.info('rebooted ' + serverID);                        
345
                                                reboot(serverIDs);
346
                                        }
347
                                }
348

    
349
    });
350
        
351
    return false;
352
}
353

    
354
// shutdown action
355
function shutdown(serverIDs) {
356
        if (!serverIDs.length){
357
                ajax_success();
358
                return false;
359
        }
360
    // ajax post shutdown call
361
    var payload = {
362
        "shutdown": {"timeout" : "5"}
363
    };   
364

    
365
        serverID = serverIDs.pop()
366
    $.ajax({
367
            url: '/api/v1.0/servers/' + serverID + '/action',
368
            type: "POST",
369
            dataType: "json",
370
        data: JSON.stringify(payload),
371
        timeout: TIMEOUT,
372
        error: function(jqXHR, textStatus, errorThrown) { 
373
                    ajax_error(jqXHR);
374
                    },
375
        success: function(data, textStatus, jqXHR) {
376
                    if ( jqXHR.status == '202') {
377
                                                console.info('suspended ' + serverID);                                        
378
                        shutdown(serverIDs);
379
                    } else {
380
                        ajax_error(jqXHR);
381
                    }}             
382
    });
383
    return false;    
384
}
385

    
386
// destroy action
387
function destroy(serverIDs) {
388
        if (!serverIDs.length){
389
                ajax_success();
390
                return false;
391
        }
392
    // ajax post shutdown call
393
    var payload = {
394
        "shutdown": {"timeout" : "5"}
395
    };   
396

    
397
        serverID = serverIDs.pop()
398
    $.ajax({
399
            url: '/api/v1.0/servers/' + serverID + '/action',
400
            type: "DELETE",
401
            dataType: "json",
402
        data: JSON.stringify(payload),
403
        timeout: TIMEOUT,
404
        error: function(jqXHR, textStatus, errorThrown) { 
405
                    ajax_error(jqXHR);
406
                    },
407
        success: function(data, textStatus, jqXHR) {
408
                    if ( jqXHR.status == '202') {
409
                                                console.info('suspended ' + serverID);                                        
410
                        shutdown(serverIDs);
411
                    } else {
412
                        ajax_error(jqXHR);
413
                    }}             
414
    });
415
    return false;    
416
}
417

    
418
// start action
419
function start(serverIDs){
420
        if (!serverIDs.length){
421
                ajax_success();
422
                return false;
423
        }        
424
    // ajax post start call
425
    var payload = {
426
        "start": {"type" : "NORMAL"}
427
    };   
428

    
429
        serverID = serverIDs.pop()
430
    $.ajax({
431
        url: '/api/v1.0/servers/' + serverID + '/action',
432
        type: "POST",
433
        dataType: "json",
434
        data: JSON.stringify(payload),
435
        timeout: TIMEOUT,
436
        error: function(jqXHR, textStatus, errorThrown) { 
437
                    ajax_error(jqXHR);
438
                    },
439
        success: function(data, textStatus, jqXHR) {
440
                    if ( jqXHR.status == '202') {
441
                                            console.info('started ' + serverID);                        
442
                        start(serverIDs);
443
                    } else {
444
                        ajax_error(jqXHR);
445
                    }}
446
    });
447
    return false;
448
}