Statistics
| Branch: | Tag: | Revision:

root / ui / static / synnefo.js @ 935bb83f

History | View | Annotate | Download (12.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
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
            servers = data.servers;
127
                        update_machines_view(data);
128
        }
129
    });
130
    return false;
131
}
132

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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