Statistics
| Branch: | Tag: | Revision:

root / ui / static / synnefo.js @ a3e418b1

History | View | Annotate | Download (12.6 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
function auto_update_vms(interval) {
108
        update_vms();
109
        setTimeout(auto_update_vms,interval,interval);
110
}
111

    
112
// get and show a list of running and terminated machines
113
function update_vms() {
114
    try{ console.info('updating machines'); } catch(err){}
115

    
116
    $.ajax({
117
        url: '/api/v1.0/servers/detail',
118
        type: "GET",
119
        timeout: TIMEOUT,
120
        dataType: "json",
121
        error: function(jqXHR, textStatus, errorThrown) { 
122
                    ajax_error(jqXHR);
123
                    return false;
124
                    },
125
        success: function(data, textStatus, jqXHR) {
126
                        update_machines_view(data);
127
        }
128
    });
129
    return false;
130
}
131

    
132
// get and show a list of anvailable standard and custom images
133
function update_images() { 
134
    $.ajax({
135
        url: '/api/v1.0/images/detail',
136
        type: "GET",
137
        //async: false,
138
        dataType: "json",
139
        timeout: TIMEOUT,
140
        error: function(jqXHR, textStatus, errorThrown) { 
141
                    ajax_error(jqXHR);
142
                    },
143
        success: function(data, textStatus, jqXHR) {
144
            images = data.images;
145
            if ($("ul#standard-images li").toArray().length + $("ul#custom-images li").toArray().length == 0) {
146
                $.each(data.images, function(i,image){
147
                    var img = $('#image-template').clone().attr("id","img-"+image.id).fadeIn("slow");
148
                    img.find("label").attr('for',"img-radio-" + image.id);
149
                    img.find(".image-title").text(image.name);
150
                    img.find(".description").text(image.description);
151
                    img.find(".size").text(image.size);
152
                    img.find("input.radio").attr('id',"img-radio-" + image.id);
153
                    if (i==0) img.find("input.radio").attr("checked","checked"); 
154
                    img.find("img.image-logo").attr('src','static/os_logos/'+image_tags[image.id]+'.png');
155
                    if (image.serverId) {
156
                        img.appendTo("ul#custom-images");
157
                    } else {
158
                        img.appendTo("ul#standard-images");
159
                    }
160
                });
161
            }
162
        }
163
    });
164
    return false;
165
}
166

    
167
var flavors = {}, images = {}, disks = [], cpus = [], ram = [];
168

    
169
Array.prototype.unique = function () {
170
        var r = new Array();
171
        o:for(var i = 0, n = this.length; i < n; i++)
172
        {
173
                for(var x = 0, y = r.length; x < y; x++)
174
                {
175
                        if(r[x]==this[i])
176
                        {
177
                                continue o;
178
                        }
179
                }
180
                r[r.length] = this[i];
181
        }
182
        return r;
183
}
184

    
185
// get and configure flavor selection
186
function update_flavors() { 
187
    $.ajax({
188
        url: '/api/v1.0/flavors/detail',
189
        type: "GET",
190
        //async: false,
191
        dataType: "json",
192
        timeout: TIMEOUT,
193
        error: function(jqXHR, textStatus, errorThrown) { 
194
            ajax_error(jqXHR);
195
        },
196
        success: function(data, textStatus, jqXHR) {
197
            flavors = data.flavors;
198
            $.each(flavors, function(i, flavor) {
199
                cpus[i] = flavor['cpu'];
200
                disks[i] = flavor['disk'];
201
                ram[i] = flavor['ram'];
202
            });
203
            cpus = cpus.unique();
204
            disks = disks.unique();
205
            ram = ram.unique();
206
            // sliders for selecting VM flavor
207
            $("#cpu:range").rangeinput({min:0,
208
                                       value:0,
209
                                       step:1,
210
                                       progress: true,
211
                                       max:cpus.length-1});
212
            
213
            $("#storage:range").rangeinput({min:0,
214
                                       value:0,
215
                                       step:1,
216
                                       progress: true,
217
                                       max:disks.length-1});
218

    
219
            $("#ram:range").rangeinput({min:0,
220
                                       value:0,
221
                                       step:1,
222
                                       progress: true,
223
                                       max:ram.length-1});
224
            $("#small").click();
225
            
226
            // update the indicators when sliding
227
            $("#cpu:range").data().rangeinput.onSlide(function(event,value){
228
                $("#cpu-indicator")[0].value = cpus[Number(value)];
229
                $("#custom").click();
230
            });
231
            $("#ram:range").data().rangeinput.onSlide(function(event,value){
232
                $("#ram-indicator")[0].value = ram[Number(value)];
233
                $("#custom").click();
234
            });
235
            $("#storage:range").data().rangeinput.onSlide(function(event,value){
236
                $("#storage-indicator")[0].value = disks[Number(value)];
237
                $("#custom").click();
238
            });
239
        }
240
    });
241
    return false;
242
}
243
// return flavorId from cpu, disk, ram values
244
function identify_flavor(cpu, disk, ram){
245
    for (i=0;i<flavors.length;i++){
246
        if (flavors[i]['cpu'] == cpu && flavors[i]['disk']==disk && flavors[i]['ram']==ram) {
247
            return flavors[i]['id']
248
        }
249
    }
250
    return 0;
251
}
252

    
253
// update the actions in the 
254
function updateActions() {
255
        var states = [];
256
        var on = [];
257
        var checked = $("table.list-machines tbody input[type='checkbox']:checked");
258
        // disable all actions to begin with
259
        for (action in actions) {
260
                $("#action-" + action).removeClass('enabled');
261
        }
262

    
263
        // are there multiple machines selected?
264
        if (checked.length>1)
265
                states[0] = 'multiple';
266
        
267
        // check the states of selected machines
268
        checked.each(function(i,checkbox) {
269
                states[states.length] = checkbox.className;
270
                var ip = $("#" + checkbox.id.replace('input-','') + ".ip span.public").text();
271
                if (ip.replace('undefined','').length)
272
                        states[states.length] = 'network';
273
        });
274

    
275
        // decide which actions should be enabled
276
        for (a in actions) {
277
                var enabled = false;
278
                for (var s =0; s<states.length; s++) {
279
                        if (actions[a].indexOf(states[s]) != -1 ) {
280
                                enabled = true;
281
                        } else {
282
                                enabled = false;
283
                                break;
284
                        }
285
                }
286
                if (enabled)
287
                        on[on.length]=a;
288
        }
289
        // enable those actions
290
        for (action in on) {
291
                $("#action-" + on[action]).addClass('enabled');
292
        }
293
}
294

    
295
// reboot action
296
function reboot(serverIDs){
297
        if (!serverIDs.length){
298
                ajax_success();
299
                return false;
300
        }        
301
    // ajax post reboot call
302
    var payload = {
303
        "reboot": {"type" : "HARD"}
304
    };
305
    serverID = serverIDs.pop();
306
        
307
        $.ajax({
308
                url: '/api/v1.0/servers/' + serverID + '/action',
309
                type: "POST",        
310
                dataType: "json",
311
                data: JSON.stringify(payload),
312
                timeout: TIMEOUT,
313
                error: function(jqXHR, textStatus, errorThrown) {
314
                                        ajax_error(jqXHR, serverID);
315
                                },
316
                success: function(data, textStatus, jqXHR) {
317
                                        if ( jqXHR.status != '202') {
318
                                                ajax_error(jqXHR, serverID);
319
                                        } else {
320
                                                try{console.info('rebooted ' + serverID);} catch(err) {}                   
321
                                                reboot(serverIDs);
322
                                        }
323
                                }
324

    
325
    });
326
        
327
    return false;
328
}
329

    
330
// shutdown action
331
function shutdown(serverIDs) {
332
        if (!serverIDs.length){
333
                ajax_success();
334
                return false;
335
        }
336
    // ajax post shutdown call
337
    var payload = {
338
        "shutdown": {"timeout" : "5"}
339
    };   
340

    
341
        serverID = serverIDs.pop()
342
    $.ajax({
343
            url: '/api/v1.0/servers/' + serverID + '/action',
344
            type: "POST",
345
            dataType: "json",
346
        data: JSON.stringify(payload),
347
        timeout: TIMEOUT,
348
        error: function(jqXHR, textStatus, errorThrown) { 
349
                    ajax_error(jqXHR);
350
                    },
351
        success: function(data, textStatus, jqXHR) {
352
                    if ( jqXHR.status == '202') {
353
                                                try{ console.info('suspended ' + serverID); } catch(err) {}                                       
354
                        shutdown(serverIDs);
355
                    } else {
356
                        ajax_error(jqXHR);
357
                    }}             
358
    });
359
    return false;    
360
}
361

    
362
// destroy action
363
function destroy(serverIDs) {
364
        if (!serverIDs.length){
365
                ajax_success();
366
                return false;
367
        }
368
    // ajax post shutdown call
369
    var payload = {
370
        "shutdown": {"timeout" : "5"}
371
    };   
372

    
373
        serverID = serverIDs.pop()
374
    $.ajax({
375
            url: '/api/v1.0/servers/' + serverID + '/action',
376
            type: "DELETE",
377
            dataType: "json",
378
        data: JSON.stringify(payload),
379
        timeout: TIMEOUT,
380
        error: function(jqXHR, textStatus, errorThrown) { 
381
                    ajax_error(jqXHR);
382
                    },
383
        success: function(data, textStatus, jqXHR) {
384
                    if ( jqXHR.status == '202') {
385
                                                try { console.info('suspended ' + serverID); } catch (err) {}                                        
386
                        shutdown(serverIDs);
387
                    } else {
388
                        ajax_error(jqXHR);
389
                    }}             
390
    });
391
    return false;    
392
}
393

    
394
// start action
395
function start(serverIDs){
396
        if (!serverIDs.length){
397
                ajax_success();
398
                return false;
399
        }        
400
    // ajax post start call
401
    var payload = {
402
        "start": {"type" : "NORMAL"}
403
    };   
404

    
405
        serverID = serverIDs.pop()
406
    $.ajax({
407
        url: '/api/v1.0/servers/' + serverID + '/action',
408
        type: "POST",
409
        dataType: "json",
410
        data: JSON.stringify(payload),
411
        timeout: TIMEOUT,
412
        error: function(jqXHR, textStatus, errorThrown) { 
413
                    ajax_error(jqXHR);
414
                    },
415
        success: function(data, textStatus, jqXHR) {
416
                    if ( jqXHR.status == '202') {
417
                                            try { console.info('started ' + serverID); } catch(err) {}                      
418
                        start(serverIDs);
419
                    } else {
420
                        ajax_error(jqXHR);
421
                    }}
422
    });
423
    return false;
424
}