Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / ui / static / snf / js / models.js @ 43ec1206

History | View | Annotate | Download (68.4 kB)

1
// Copyright 2011 GRNET S.A. All rights reserved.
2
// 
3
// Redistribution and use in source and binary forms, with or
4
// without modification, are permitted provided that the following
5
// conditions are met:
6
// 
7
//   1. Redistributions of source code must retain the above
8
//      copyright notice, this list of conditions and the following
9
//      disclaimer.
10
// 
11
//   2. Redistributions in binary form must reproduce the above
12
//      copyright notice, this list of conditions and the following
13
//      disclaimer in the documentation and/or other materials
14
//      provided with the distribution.
15
// 
16
// THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
// AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
// POSSIBILITY OF SUCH DAMAGE.
28
// 
29
// The views and conclusions contained in the software and
30
// documentation are those of the authors and should not be
31
// interpreted as representing official policies, either expressed
32
// or implied, of GRNET S.A.
33
// 
34

    
35
;(function(root){
36
    
37
    // root
38
    var root = root;
39
    
40
    // setup namepsaces
41
    var snf = root.synnefo = root.synnefo || {};
42
    var models = snf.models = snf.models || {}
43
    var storage = snf.storage = snf.storage || {};
44
    var util = snf.util = snf.util || {};
45

    
46
    // shortcuts
47
    var bb = root.Backbone;
48
    var slice = Array.prototype.slice
49

    
50
    // logging
51
    var logger = new snf.logging.logger("SNF-MODELS");
52
    var debug = _.bind(logger.debug, logger);
53
    
54
    // get url helper
55
    var getUrl = function(baseurl) {
56
        var baseurl = baseurl || snf.config.api_urls[this.api_type];
57
        return baseurl + "/" + this.path;
58
    }
59
    
60
    // i18n
61
    BUILDING_MESSAGES = window.BUILDING_MESSAGES || {'INIT': 'init', 'COPY': '{0}, {1}, {2}', 'FINAL': 'final'};
62

    
63
    // Base object for all our models
64
    models.Model = bb.Model.extend({
65
        sync: snf.api.sync,
66
        api: snf.api,
67
        api_type: 'compute',
68
        has_status: false,
69

    
70
        initialize: function() {
71
            if (this.has_status) {
72
                this.bind("change:status", this.handle_remove);
73
                this.handle_remove();
74
            }
75
            
76
            this.api_call = _.bind(this.api.call, this);
77
            models.Model.__super__.initialize.apply(this, arguments);
78
        },
79

    
80
        handle_remove: function() {
81
            if (this.get("status") == 'DELETED') {
82
                if (this.collection) {
83
                    try { this.clear_pending_action();} catch (err) {};
84
                    try { this.reset_pending_actions();} catch (err) {};
85
                    try { this.stop_stats_update();} catch (err) {};
86
                    this.collection.remove(this.id);
87
                }
88
            }
89
        },
90
        
91
        // custom set method to allow submodels to use
92
        // set_<attr> methods for handling the value of each
93
        // attribute and overriding the default set method
94
        // for specific parameters
95
        set: function(params, options) {
96
            _.each(params, _.bind(function(value, key){
97
                if (this["set_" + key]) {
98
                    params[key] = this["set_" + key](value);
99
                }
100
            }, this))
101
            var ret = bb.Model.prototype.set.call(this, params, options);
102
            return ret;
103
        },
104

    
105
        url: function(options) {
106
            return getUrl.call(this, this.base_url) + "/" + this.id;
107
        },
108

    
109
        api_path: function(options) {
110
            return this.path + "/" + this.id;
111
        },
112

    
113
        parse: function(resp, xhr) {
114
            return resp.server;
115
        },
116

    
117
        remove: function() {
118
            this.api_call(this.api_path(), "delete");
119
        },
120

    
121
        changedKeys: function() {
122
            return _.keys(this.changedAttributes() || {});
123
        },
124

    
125
        hasOnlyChange: function(keys) {
126
            var ret = false;
127
            _.each(keys, _.bind(function(key) {
128
                if (this.changedKeys().length == 1 && this.changedKeys().indexOf(key) > -1) { ret = true};
129
            }, this));
130
            return ret;
131
        }
132

    
133
    })
134
    
135
    // Base object for all our model collections
136
    models.Collection = bb.Collection.extend({
137
        sync: snf.api.sync,
138
        api: snf.api,
139
        api_type: 'compute',
140
        supportIncUpdates: true,
141

    
142
        initialize: function() {
143
            models.Collection.__super__.initialize.apply(this, arguments);
144
            this.api_call = _.bind(this.api.call, this);
145
        },
146

    
147
        url: function(options, method) {
148
            return getUrl.call(this, this.base_url) + (
149
                    options.details || this.details && method != 'create' ? '/detail' : '');
150
        },
151

    
152
        fetch: function(options) {
153
            if (!options) { options = {} };
154
            // default to update
155
            if (!this.noUpdate) {
156
                if (options.update === undefined) { options.update = true };
157
                if (!options.removeMissing && options.refresh) { options.removeMissing = true };
158
            } else {
159
                if (options.refresh === undefined) {
160
                    options.refresh = true;
161
                }
162
            }
163
            // custom event foreach fetch
164
            return bb.Collection.prototype.fetch.call(this, options)
165
        },
166

    
167
        create: function(model, options) {
168
            var coll = this;
169
            options || (options = {});
170
            model = this._prepareModel(model, options);
171
            if (!model) return false;
172
            var success = options.success;
173
            options.success = function(nextModel, resp, xhr) {
174
                if (success) success(nextModel, resp, xhr);
175
            };
176
            model.save(null, options);
177
            return model;
178
        },
179

    
180
        get_fetcher: function(interval, increase, fast, increase_after_calls, max, initial_call, params) {
181
            var fetch_params = params || {};
182
            var handler_options = {};
183

    
184
            fetch_params.skips_timeouts = true;
185
            handler_options.interval = interval;
186
            handler_options.increase = increase;
187
            handler_options.fast = fast;
188
            handler_options.increase_after_calls = increase_after_calls;
189
            handler_options.max= max;
190
            handler_options.id = "collection id";
191

    
192
            var last_ajax = undefined;
193
            var callback = _.bind(function() {
194
                // clone to avoid referenced objects
195
                var params = _.clone(fetch_params);
196
                updater._ajax = last_ajax;
197
                
198
                // wait for previous request to finish
199
                if (last_ajax && last_ajax.readyState < 4 && last_ajax.statusText != "timeout") {
200
                    // opera readystate for 304 responses is 0
201
                    if (!($.browser.opera && last_ajax.readyState == 0 && last_ajax.status == 304)) {
202
                        return;
203
                    }
204
                }
205
                
206
                last_ajax = this.fetch(params);
207
            }, this);
208
            handler_options.callback = callback;
209

    
210
            var updater = new snf.api.updateHandler(_.clone(_.extend(handler_options, fetch_params)));
211
            snf.api.bind("call", _.throttle(_.bind(function(){ updater.faster(true)}, this)), 1000);
212
            return updater;
213
        }
214
    });
215
    
216
    // Image model
217
    models.Image = models.Model.extend({
218
        path: 'images',
219

    
220
        get_size: function() {
221
            return parseInt(this.get('metadata') ? this.get('metadata').values.size : -1)
222
        },
223

    
224
        get_meta: function(key) {
225
            if (this.get('metadata') && this.get('metadata').values && this.get('metadata').values[key]) {
226
                return _.escape(this.get('metadata').values[key]);
227
            }
228
            return undefined;
229
        },
230

    
231
        get_owner: function() {
232
            return this.get('owner') || _.keys(synnefo.config.system_images_owners)[0];
233
        },
234

    
235
        display_owner: function() {
236
            var owner = this.get_owner();
237
            if (_.include(_.keys(synnefo.config.system_images_owners), owner)) {
238
                return synnefo.config.system_images_owners[owner];
239
            } else {
240
                return owner;
241
            }
242
        },
243
    
244
        get_readable_size: function() {
245
            if (this.is_deleted()) {
246
                return synnefo.config.image_deleted_size_title || '(none)';
247
            }
248
            return this.get_size() > 0 ? util.readablizeBytes(this.get_size() * 1024 * 1024) : '(none)';
249
        },
250

    
251
        get_os: function() {
252
            return this.get("OS");
253
        },
254

    
255
        get_created_user: function() {
256
            return synnefo.config.os_created_users[this.get_os()] || "root";
257
        },
258

    
259
        get_sort_order: function() {
260
            return parseInt(this.get('metadata') ? this.get('metadata').values.sortorder : -1)
261
        },
262

    
263
        get_vm: function() {
264
            var vm_id = this.get("serverRef");
265
            var vm = undefined;
266
            vm = storage.vms.get(vm_id);
267
            return vm;
268
        },
269

    
270
        is_public: function() {
271
            return this.get('is_public') || true;
272
        },
273

    
274
        is_deleted: function() {
275
            return this.get('status') == "DELETED"
276
        },
277
        
278
        ssh_keys_path: function() {
279
            prepend = '';
280
            if (this.get_created_user() != 'root') {
281
                prepend = '/home'
282
            }
283
            return '{1}/{0}/.ssh/authorized_keys'.format(this.get_created_user(), prepend);
284
        },
285

    
286
        _supports_ssh: function() {
287
            if (synnefo.config.support_ssh_os_list.indexOf(this.get_os()) > -1) {
288
                return true;
289
            }
290
            return false;
291
        },
292

    
293
        supports: function(feature) {
294
            if (feature == "ssh") {
295
                return this._supports_ssh()
296
            }
297
            return false;
298
        },
299

    
300
        personality_data_for_keys: function(keys) {
301
            contents = '';
302
            _.each(keys, function(key){
303
                contents = contents + key.get("content") + "\n"
304
            });
305
            contents = $.base64.encode(contents);
306

    
307
            return {
308
                path: this.ssh_keys_path(),
309
                contents: contents
310
            }
311
        }
312
    });
313

    
314
    // Flavor model
315
    models.Flavor = models.Model.extend({
316
        path: 'flavors',
317

    
318
        details_string: function() {
319
            return "{0} CPU, {1}MB, {2}GB".format(this.get('cpu'), this.get('ram'), this.get('disk'));
320
        },
321

    
322
        get_disk_size: function() {
323
            return parseInt(this.get("disk") * 1000)
324
        },
325

    
326
        get_disk_template_info: function() {
327
            var info = snf.config.flavors_disk_templates_info[this.get("disk_template")];
328
            if (!info) {
329
                info = { name: this.get("disk_template"), description:'' };
330
            }
331
            return info
332
        }
333

    
334
    });
335
    
336
    //network vms list helper
337
    var NetworkVMSList = function() {
338
        this.initialize = function() {
339
            this.vms = [];
340
            this.pending = [];
341
            this.pending_for_removal = [];
342
        }
343
        
344
        this.add_pending_for_remove = function(vm_id) {
345
            if (this.pending_for_removal.indexOf(vm_id) == -1) {
346
                this.pending_for_removal.push(vm_id);
347
            }
348

    
349
            if (this.pending_for_removal.length) {
350
                this.trigger("pending:remove:add");
351
            }
352
        },
353

    
354
        this.add_pending = function(vm_id) {
355
            if (this.pending.indexOf(vm_id) == -1) {
356
                this.pending[this.pending.length] = vm_id;
357
            }
358

    
359
            if (this.pending.length) {
360
                this.trigger("pending:add");
361
            }
362
        }
363

    
364
        this.check_pending = function() {
365
            var len = this.pending.length;
366
            var args = [this.pending];
367
            this.pending = _.difference(this.pending, this.vms);
368
            if (len != this.pending.length) {
369
                if (this.pending.length == 0) {
370
                    this.trigger("pending:clear");
371
                }
372
            }
373

    
374
            var len = this.pending_for_removal.length;
375
            this.pending_for_removal = _.intersection(this.pending_for_removal, this.vms);
376
            if (this.pending_for_removal.length == 0) {
377
                this.trigger("pending:remove:clear");
378
            }
379

    
380
        }
381

    
382

    
383
        this.add = function(vm_id) {
384
            if (this.vms.indexOf(vm_id) == -1) {
385
                this.vms[this.vms.length] = vm_id;
386
                this.trigger("network:connect", vm_id);
387
                this.check_pending();
388
                return true;
389
            }
390
        }
391

    
392
        this.remove = function(vm_id) {
393
            if (this.vms.indexOf(vm_id) > -1) {
394
                this.vms = _.without(this.vms, vm_id);
395
                this.trigger("network:disconnect", vm_id);
396
                this.check_pending();
397
                return true;
398
            }
399
        }
400

    
401
        this.get = function() {
402
            return this.vms;
403
        }
404

    
405
        this.list = function() {
406
            return storage.vms.filter(_.bind(function(vm){
407
                return this.vms.indexOf(vm.id) > -1;
408
            }, this))
409
        }
410

    
411
        this.initialize();
412
    };
413
    _.extend(NetworkVMSList.prototype, bb.Events);
414
    
415
    // vm networks list helper
416
    var VMNetworksList = function() {
417
        this.initialize = function() {
418
            this.networks = {};
419
            this.network_ids = [];
420
        }
421

    
422
        this.add = function(net_id, data) {
423
            if (!this.networks[net_id]) {
424
                this.networks[net_id] = data || {};
425
                this.network_ids[this.network_ids.length] = net_id;
426
                this.trigger("network:connect", net_id);
427
                return true;
428
            }
429
        }
430

    
431
        this.remove = function(net_id) {
432
            if (this.networks[net_id]) {
433
                delete this.networks[net_id];
434
                this.network_ids = _.without(this.network_ids, net_id);
435
                this.trigger("network:disconnect", net_id);
436
                return true;
437
            }
438
            return false;
439
        }
440

    
441
        this.get = function() {
442
            return this.networks;
443
        }
444

    
445
        this.list = function() {
446
            return storage.networks.filter(_.bind(function(net){
447
                return this.network_ids.indexOf(net.id) > -1;
448
            }, this))
449
        }
450

    
451
        this.initialize();
452
    };
453
    _.extend(VMNetworksList.prototype, bb.Events);
454
        
455
    models.ParamsList = function(){this.initialize.apply(this, arguments)};
456
    _.extend(models.ParamsList.prototype, bb.Events, {
457

    
458
        initialize: function(parent, param_name) {
459
            this.parent = parent;
460
            this.actions = {};
461
            this.param_name = param_name;
462
            this.length = 0;
463
        },
464
        
465
        has_action: function(action) {
466
            return this.actions[action] ? true : false;
467
        },
468
            
469
        _parse_params: function(arguments) {
470
            if (arguments.length <= 1) {
471
                return [];
472
            }
473

    
474
            var args = _.toArray(arguments);
475
            return args.splice(1);
476
        },
477

    
478
        contains: function(action, params) {
479
            params = this._parse_params(arguments);
480
            var has_action = this.has_action(action);
481
            if (!has_action) { return false };
482

    
483
            var paramsEqual = false;
484
            _.each(this.actions[action], function(action_params) {
485
                if (_.isEqual(action_params, params)) {
486
                    paramsEqual = true;
487
                }
488
            });
489
                
490
            return paramsEqual;
491
        },
492
        
493
        is_empty: function() {
494
            return _.isEmpty(this.actions);
495
        },
496

    
497
        add: function(action, params) {
498
            params = this._parse_params(arguments);
499
            if (this.contains.apply(this, arguments)) { return this };
500
            var isnew = false
501
            if (!this.has_action(action)) {
502
                this.actions[action] = [];
503
                isnew = true;
504
            };
505

    
506
            this.actions[action].push(params);
507
            this.parent.trigger("change:" + this.param_name, this.parent, this);
508
            if (isnew) {
509
                this.trigger("add", action, params);
510
            } else {
511
                this.trigger("change", action, params);
512
            }
513
            return this;
514
        },
515
        
516
        remove_all: function(action) {
517
            if (this.has_action(action)) {
518
                delete this.actions[action];
519
                this.parent.trigger("change:" + this.param_name, this.parent, this);
520
                this.trigger("remove", action);
521
            }
522
            return this;
523
        },
524

    
525
        reset: function() {
526
            this.actions = {};
527
            this.parent.trigger("change:" + this.param_name, this.parent, this);
528
            this.trigger("reset");
529
            this.trigger("remove");
530
        },
531

    
532
        remove: function(action, params) {
533
            params = this._parse_params(arguments);
534
            if (!this.has_action(action)) { return this };
535
            var index = -1;
536
            _.each(this.actions[action], _.bind(function(action_params) {
537
                if (_.isEqual(action_params, params)) {
538
                    index = this.actions[action].indexOf(action_params);
539
                }
540
            }, this));
541
            
542
            if (index > -1) {
543
                this.actions[action].splice(index, 1);
544
                if (_.isEmpty(this.actions[action])) {
545
                    delete this.actions[action];
546
                }
547
                this.parent.trigger("change:" + this.param_name, this.parent, this);
548
                this.trigger("remove", action, params);
549
            }
550
        }
551

    
552
    });
553

    
554
    // Image model
555
    models.Network = models.Model.extend({
556
        path: 'networks',
557
        has_status: true,
558
        
559
        initialize: function() {
560
            this.vms = new NetworkVMSList();
561
            this.vms.bind("pending:add", _.bind(this.handle_pending_connections, this, "add"));
562
            this.vms.bind("pending:clear", _.bind(this.handle_pending_connections, this, "clear"));
563
            this.vms.bind("pending:remove:add", _.bind(this.handle_pending_connections, this, "add"));
564
            this.vms.bind("pending:remove:clear", _.bind(this.handle_pending_connections, this, "clear"));
565

    
566
            var ret = models.Network.__super__.initialize.apply(this, arguments);
567

    
568
            storage.vms.bind("change:linked_to_nets", _.bind(this.update_connections, this, "vm:change"));
569
            storage.vms.bind("add", _.bind(this.update_connections, this, "add"));
570
            storage.vms.bind("remove", _.bind(this.update_connections, this, "remove"));
571
            storage.vms.bind("reset", _.bind(this.update_connections, this, "reset"));
572

    
573
            this.bind("change:linked_to", _.bind(this.update_connections, this, "net:change"));
574
            this.update_connections();
575
            this.update_state();
576
            
577
            this.set({"actions": new models.ParamsList(this, "actions")});
578

    
579
            return ret;
580
        },
581

    
582
        toJSON: function() {
583
            var attrs = _.clone(this.attributes);
584
            attrs.actions = _.clone(this.get("actions").actions);
585
            return attrs;
586
        },
587

    
588
        update_state: function() {
589
            if (this.vms.pending.length) {
590
                this.set({state: "CONNECTING"});
591
                return
592
            }
593

    
594
            if (this.vms.pending_for_removal.length) {
595
                this.set({state: "DISCONNECTING"});
596
                return
597
            }   
598
            
599
            var firewalling = false;
600
            _.each(this.vms.get(), _.bind(function(vm_id){
601
                var vm = storage.vms.get(vm_id);
602
                if (!vm) { return };
603
                if (!_.isEmpty(vm.pending_firewalls)) {
604
                    this.set({state:"FIREWALLING"});
605
                    firewalling = true;
606
                    return false;
607
                }
608
            },this));
609
            if (firewalling) { return };
610

    
611
            this.set({state:"NORMAL"});
612
        },
613

    
614
        handle_pending_connections: function(action) {
615
            this.update_state();
616
        },
617

    
618
        // handle vm/network connections
619
        update_connections: function(action, model) {
620
            
621
            // vm removed disconnect vm from network
622
            if (action == "remove") {
623
                var removed_from_net = this.vms.remove(model.id);
624
                var removed_from_vm = model.networks.remove(this.id);
625
                if (removed_from_net) {this.trigger("vm:disconnect", model, this); this.change()};
626
                if (removed_from_vm) {model.trigger("network:disconnect", this, model); this.change()};
627
                return;
628
            }
629
            
630
            // update links for all vms
631
            var links = this.get("linked_to");
632
            storage.vms.each(_.bind(function(vm) {
633
                var vm_links = vm.get("linked_to") || [];
634
                if (vm_links.indexOf(this.id) > -1) {
635
                    // vm has connection to current network
636
                    if (links.indexOf(vm.id) > -1) {
637
                        // and network has connection to vm, so try
638
                        // to append it
639
                        var add_to_net = this.vms.add(vm.id);
640
                        var index = _.indexOf(vm_links, this.id);
641
                        var add_to_vm = vm.networks.add(this.id, vm.get("linked_to_nets")[index]);
642
                        
643
                        // call only if connection did not existed
644
                        if (add_to_net) {this.trigger("vm:connect", vm, this); this.change()};
645
                        if (add_to_vm) {vm.trigger("network:connect", this, vm); vm.change()};
646
                    } else {
647
                        // no connection, try to remove it
648
                        var removed_from_net = this.vms.remove(vm.id);
649
                        var removed_from_vm = vm.networks.remove(this.id);
650
                        if (removed_from_net) {this.trigger("vm:disconnect", vm, this); this.change()};
651
                        if (removed_from_vm) {vm.trigger("network:disconnect", this, vm); vm.change()};
652
                    }
653
                } else {
654
                    // vm has no connection to current network, try to remove it
655
                    var removed_from_net = this.vms.remove(vm.id);
656
                    var removed_from_vm = vm.networks.remove(this.id);
657
                    if (removed_from_net) {this.trigger("vm:disconnect", vm, this); this.change()};
658
                    if (removed_from_vm) {vm.trigger("network:disconnect", this, vm); vm.change()};
659
                }
660
            },this));
661
        },
662

    
663
        is_public: function() {
664
            return this.id == "public";
665
        },
666

    
667
        contains_vm: function(vm) {
668
            var net_vm_exists = this.vms.get().indexOf(vm.id) > -1;
669
            var vm_net_exists = vm.is_connected_to(this);
670
            return net_vm_exists && vm_net_exists;
671
        },
672
        
673
        call: function(action, params, success, error) {
674
            if (action == "destroy") {
675
                this.set({state:"DESTROY"});
676
                this.get("actions").remove("destroy");
677
                this.remove(_.bind(function(){
678
                    success();
679
                }, this), error);
680
            }
681
            
682
            if (action == "disconnect") {
683
                _.each(params, _.bind(function(vm_id) {
684
                    var vm = snf.storage.vms.get(vm_id);
685
                    this.get("actions").remove("disconnect", vm_id);
686
                    if (vm) {
687
                        this.remove_vm(vm, success, error);
688
                    }
689
                }, this));
690
            }
691
        },
692

    
693
        add_vm: function (vm, callback, error, options) {
694
            var payload = {add:{serverRef:"" + vm.id}};
695
            payload._options = options || {};
696
            return this.api_call(this.api_path() + "/action", "create", 
697
                                 payload,
698
                                 _.bind(function(){
699
                                     this.vms.add_pending(vm.id);
700
                                     if (callback) {callback()}
701
                                 },this), error);
702
        },
703

    
704
        remove_vm: function (vm, callback, error, options) {
705
            var payload = {remove:{serverRef:"" + vm.id}};
706
            payload._options = options || {};
707
            return this.api_call(this.api_path() + "/action", "create", 
708
                                 {remove:{serverRef:"" + vm.id}},
709
                                 _.bind(function(){
710
                                     this.vms.add_pending_for_remove(vm.id);
711
                                     if (callback) {callback()}
712
                                 },this), error);
713
        },
714

    
715
        rename: function(name, callback) {
716
            return this.api_call(this.api_path(), "update", {
717
                network:{name:name}, 
718
                _options:{
719
                    critical: false, 
720
                    error_params:{
721
                        title: "Network action failed",
722
                        ns: "Networks",
723
                        extra_details: {"Network id": this.id}
724
                    }
725
                }}, callback);
726
        },
727

    
728
        get_connectable_vms: function() {
729
            var servers = this.vms.list();
730
            return storage.vms.filter(function(vm){
731
                return servers.indexOf(vm) == -1 && !vm.in_error_state();
732
            })
733
        },
734

    
735
        state_message: function() {
736
            if (this.get("state") == "NORMAL" && this.is_public()) {
737
                return "Public network";
738
            }
739

    
740
            return models.Network.STATES[this.get("state")];
741
        },
742

    
743
        in_progress: function() {
744
            return models.Network.STATES_TRANSITIONS[this.get("state")] != undefined;
745
        },
746

    
747
        do_all_pending_actions: function(success, error) {
748
            var destroy = this.get("actions").has_action("destroy");
749
            _.each(this.get("actions").actions, _.bind(function(params, action) {
750
                _.each(params, _.bind(function(with_params) {
751
                    this.call(action, with_params, success, error);
752
                }, this));
753
            }, this));
754
        }
755
    });
756
    
757
    models.Network.STATES = {
758
        'NORMAL': 'Private network',
759
        'CONNECTING': 'Connecting...',
760
        'DISCONNECTING': 'Disconnecting...',
761
        'FIREWALLING': 'Firewall update...',
762
        'DESTROY': 'Destroying...'
763
    }
764

    
765
    models.Network.STATES_TRANSITIONS = {
766
        'CONNECTING': ['NORMAL'],
767
        'DISCONNECTING': ['NORMAL'],
768
        'FIREWALLING': ['NORMAL']
769
    }
770

    
771
    // Virtualmachine model
772
    models.VM = models.Model.extend({
773

    
774
        path: 'servers',
775
        has_status: true,
776
        initialize: function(params) {
777
            this.networks = new VMNetworksList();
778
            
779
            this.pending_firewalls = {};
780
            
781
            models.VM.__super__.initialize.apply(this, arguments);
782

    
783
            this.set({state: params.status || "ERROR"});
784
            this.log = new snf.logging.logger("VM " + this.id);
785
            this.pending_action = undefined;
786
            
787
            // init stats parameter
788
            this.set({'stats': undefined}, {silent: true});
789
            // defaults to not update the stats
790
            // each view should handle this vm attribute 
791
            // depending on if it displays stat images or not
792
            this.do_update_stats = false;
793
            
794
            // interval time
795
            // this will dynamicaly change if the server responds that
796
            // images get refreshed on different intervals
797
            this.stats_update_interval = synnefo.config.STATS_INTERVAL || 5000;
798
            this.stats_available = false;
799

    
800
            // initialize interval
801
            this.init_stats_intervals(this.stats_update_interval);
802
            
803
            this.bind("change:progress", _.bind(this.update_building_progress, this));
804
            this.update_building_progress();
805

    
806
            this.bind("change:firewalls", _.bind(this.handle_firewall_change, this));
807
            
808
            // default values
809
            this.set({linked_to_nets:this.get("linked_to_nets") || []});
810
            this.set({firewalls:this.get("firewalls") || []});
811

    
812
            this.bind("change:state", _.bind(function(){if (this.state() == "DESTROY") { this.handle_destroy() }}, this))
813
        },
814

    
815
        handle_firewall_change: function() {
816

    
817
        },
818
        
819
        set_linked_to_nets: function(data) {
820
            this.set({"linked_to":_.map(data, function(n){ return n.id})});
821
            return data;
822
        },
823

    
824
        is_connected_to: function(net) {
825
            return _.filter(this.networks.list(), function(n){return n.id == net.id}).length > 0;
826
        },
827
        
828
        status: function(st) {
829
            if (!st) { return this.get("status")}
830
            return this.set({status:st});
831
        },
832

    
833
        set_status: function(st) {
834
            var new_state = this.state_for_api_status(st);
835
            var transition = false;
836

    
837
            if (this.state() != new_state) {
838
                if (models.VM.STATES_TRANSITIONS[this.state()]) {
839
                    transition = this.state();
840
                }
841
            }
842
            
843
            // call it silently to avoid double change trigger
844
            this.set({'state': this.state_for_api_status(st)}, {silent: true});
845
            
846
            // trigger transition
847
            if (transition && models.VM.TRANSITION_STATES.indexOf(new_state) == -1) { 
848
                this.trigger("transition", {from:transition, to:new_state}) 
849
            };
850
            return st;
851
        },
852

    
853
        update_building_progress: function() {
854
            if (this.is_building()) {
855
                var progress = this.get("progress");
856
                if (progress == 0) {
857
                    this.state("BUILD_INIT");
858
                    this.set({progress_message: BUILDING_MESSAGES['INIT']});
859
                }
860
                if (progress > 0 && progress < 99) {
861
                    this.state("BUILD_COPY");
862
                    this.get_copy_details(true, undefined, _.bind(function(details){
863
                        this.set({
864
                            progress_message: BUILDING_MESSAGES['COPY'].format(details.copy, 
865
                                                                               details.size, 
866
                                                                               details.progress)
867
                        });
868
                    }, this));
869
                }
870
                if (progress == 100) {
871
                    this.state("BUILD_FINAL");
872
                    this.set({progress_message: BUILDING_MESSAGES['FINAL']});
873
                }
874
            } else {
875
            }
876
        },
877

    
878
        get_copy_details: function(human, image, callback) {
879
            var human = human || false;
880
            var image = image || this.get_image(_.bind(function(image){
881
                var progress = this.get('progress');
882
                var size = image.get_size();
883
                var size_copied = (size * progress / 100).toFixed(2);
884
                
885
                if (human) {
886
                    size = util.readablizeBytes(size*1024*1024);
887
                    size_copied = util.readablizeBytes(size_copied*1024*1024);
888
                }
889

    
890
                callback({'progress': progress, 'size': size, 'copy': size_copied})
891
            }, this));
892
        },
893

    
894
        start_stats_update: function(force_if_empty) {
895
            var prev_state = this.do_update_stats;
896

    
897
            this.do_update_stats = true;
898
            
899
            // fetcher initialized ??
900
            if (!this.stats_fetcher) {
901
                this.init_stats_intervals();
902
            }
903

    
904

    
905
            // fetcher running ???
906
            if (!this.stats_fetcher.running || !prev_state) {
907
                this.stats_fetcher.start();
908
            }
909

    
910
            if (force_if_empty && this.get("stats") == undefined) {
911
                this.update_stats(true);
912
            }
913
        },
914

    
915
        stop_stats_update: function(stop_calls) {
916
            this.do_update_stats = false;
917

    
918
            if (stop_calls) {
919
                this.stats_fetcher.stop();
920
            }
921
        },
922

    
923
        // clear and reinitialize update interval
924
        init_stats_intervals: function (interval) {
925
            this.stats_fetcher = this.get_stats_fetcher(this.stats_update_interval);
926
            this.stats_fetcher.start();
927
        },
928
        
929
        get_stats_fetcher: function(timeout) {
930
            var cb = _.bind(function(data){
931
                this.update_stats();
932
            }, this);
933
            var fetcher = new snf.api.updateHandler({'callback': cb, interval: timeout, id:'stats'});
934
            return fetcher;
935
        },
936

    
937
        // do the api call
938
        update_stats: function(force) {
939
            // do not update stats if flag not set
940
            if ((!this.do_update_stats && !force) || this.updating_stats) {
941
                return;
942
            }
943

    
944
            // make the api call, execute handle_stats_update on sucess
945
            // TODO: onError handler ???
946
            stats_url = this.url() + "/stats";
947
            this.updating_stats = true;
948
            this.sync("read", this, {
949
                handles_error:true, 
950
                url: stats_url, 
951
                refresh:true, 
952
                success: _.bind(this.handle_stats_update, this),
953
                error: _.bind(this.handle_stats_error, this),
954
                complete: _.bind(function(){this.updating_stats = false;}, this),
955
                critical: false,
956
                log_error: false,
957
                skips_timeouts: true
958
            });
959
        },
960

    
961
        get_stats_image: function(stat, type) {
962
        },
963
        
964
        _set_stats: function(stats) {
965
            var silent = silent === undefined ? false : silent;
966
            // unavailable stats while building
967
            if (this.get("status") == "BUILD") { 
968
                this.stats_available = false;
969
            } else { this.stats_available = true; }
970

    
971
            if (this.get("status") == "DESTROY") { this.stats_available = false; }
972
            
973
            this.set({stats: stats}, {silent:true});
974
            this.trigger("stats:update", stats);
975
        },
976

    
977
        unbind: function() {
978
            models.VM.__super__.unbind.apply(this, arguments);
979
        },
980

    
981
        handle_stats_error: function() {
982
            stats = {};
983
            _.each(['cpuBar', 'cpuTimeSeries', 'netBar', 'netTimeSeries'], function(k) {
984
                stats[k] = false;
985
            });
986

    
987
            this.set({'stats': stats});
988
        },
989

    
990
        // this method gets executed after a successful vm stats api call
991
        handle_stats_update: function(data) {
992
            var self = this;
993
            // avoid browser caching
994
            
995
            if (data.stats && _.size(data.stats) > 0) {
996
                var ts = $.now();
997
                var stats = data.stats;
998
                var images_loaded = 0;
999
                var images = {};
1000

    
1001
                function check_images_loaded() {
1002
                    images_loaded++;
1003

    
1004
                    if (images_loaded == 4) {
1005
                        self._set_stats(images);
1006
                    }
1007
                }
1008
                _.each(['cpuBar', 'cpuTimeSeries', 'netBar', 'netTimeSeries'], function(k) {
1009
                    
1010
                    stats[k] = stats[k] + "?_=" + ts;
1011
                    
1012
                    var stat = k.slice(0,3);
1013
                    var type = k.slice(3,6) == "Bar" ? "bar" : "time";
1014
                    var img = $("<img />");
1015
                    var val = stats[k];
1016
                    
1017
                    // load stat image to a temporary dom element
1018
                    // update model stats on image load/error events
1019
                    img.load(function() {
1020
                        images[k] = val;
1021
                        check_images_loaded();
1022
                    });
1023

    
1024
                    img.error(function() {
1025
                        images[stat + type] = false;
1026
                        check_images_loaded();
1027
                    });
1028

    
1029
                    img.attr({'src': stats[k]});
1030
                })
1031
                data.stats = stats;
1032
            }
1033

    
1034
            // do we need to change the interval ??
1035
            if (data.stats.refresh * 1000 != this.stats_update_interval) {
1036
                this.stats_update_interval = data.stats.refresh * 1000;
1037
                this.stats_fetcher.interval = this.stats_update_interval;
1038
                this.stats_fetcher.maximum_interval = this.stats_update_interval;
1039
                this.stats_fetcher.stop();
1040
                this.stats_fetcher.start(false);
1041
            }
1042
        },
1043

    
1044
        // helper method that sets the do_update_stats
1045
        // in the future this method could also make an api call
1046
        // immediaetly if needed
1047
        enable_stats_update: function() {
1048
            this.do_update_stats = true;
1049
        },
1050
        
1051
        handle_destroy: function() {
1052
            this.stats_fetcher.stop();
1053
        },
1054

    
1055
        require_reboot: function() {
1056
            if (this.is_active()) {
1057
                this.set({'reboot_required': true});
1058
            }
1059
        },
1060
        
1061
        set_pending_action: function(data) {
1062
            this.pending_action = data;
1063
            return data;
1064
        },
1065

    
1066
        // machine has pending action
1067
        update_pending_action: function(action, force) {
1068
            this.set({pending_action: action});
1069
        },
1070

    
1071
        clear_pending_action: function() {
1072
            this.set({pending_action: undefined});
1073
        },
1074

    
1075
        has_pending_action: function() {
1076
            return this.get("pending_action") ? this.get("pending_action") : false;
1077
        },
1078
        
1079
        // machine is active
1080
        is_active: function() {
1081
            return models.VM.ACTIVE_STATES.indexOf(this.state()) > -1;
1082
        },
1083
        
1084
        // machine is building 
1085
        is_building: function() {
1086
            return models.VM.BUILDING_STATES.indexOf(this.state()) > -1;
1087
        },
1088
        
1089
        in_error_state: function() {
1090
            return this.state() === "ERROR"
1091
        },
1092

    
1093
        // user can connect to machine
1094
        is_connectable: function() {
1095
            // check if ips exist
1096
            if (!this.get_addresses().ip4 && !this.get_addresses().ip6) {
1097
                return false;
1098
            }
1099
            return models.VM.CONNECT_STATES.indexOf(this.state()) > -1;
1100
        },
1101
        
1102
        set_firewalls: function(data) {
1103
            _.each(data, _.bind(function(val, key){
1104
                if (this.pending_firewalls && this.pending_firewalls[key] && this.pending_firewalls[key] == val) {
1105
                        this.require_reboot();
1106
                        this.remove_pending_firewall(key, val);
1107
                }
1108
            }, this));
1109
            return data;
1110
        },
1111

    
1112
        remove_pending_firewall: function(net_id, value) {
1113
            if (this.pending_firewalls[net_id] == value) {
1114
                delete this.pending_firewalls[net_id];
1115
                storage.networks.get(net_id).update_state();
1116
            }
1117
        },
1118
            
1119
        remove_meta: function(key, complete, error) {
1120
            var url = this.api_path() + "/meta/" + key;
1121
            this.api_call(url, "delete", undefined, complete, error);
1122
        },
1123

    
1124
        save_meta: function(meta, complete, error) {
1125
            var url = this.api_path() + "/meta/" + meta.key;
1126
            var payload = {meta:{}};
1127
            payload.meta[meta.key] = meta.value;
1128
            payload._options = {
1129
                critical:false, 
1130
                error_params: {
1131
                    title: "Machine metadata error",
1132
                    extra_details: {"Machine id": this.id}
1133
            }};
1134

    
1135
            this.api_call(url, "update", payload, complete, error);
1136
        },
1137

    
1138
        set_firewall: function(net_id, value, callback, error, options) {
1139
            if (this.get("firewalls") && this.get("firewalls")[net_id] == value) { return }
1140

    
1141
            this.pending_firewalls[net_id] = value;
1142
            this.trigger("change", this, this);
1143
            var payload = {"firewallProfile":{"profile":value}};
1144
            payload._options = _.extend({critical: false}, options);
1145
            
1146
            // reset firewall state on error
1147
            var error_cb = _.bind(function() {
1148
                thi
1149
            }, this);
1150

    
1151
            this.api_call(this.api_path() + "/action", "create", payload, callback, error);
1152
            storage.networks.get(net_id).update_state();
1153
        },
1154

    
1155
        firewall_pending: function(net_id) {
1156
            return this.pending_firewalls[net_id] != undefined;
1157
        },
1158
        
1159
        // update/get the state of the machine
1160
        state: function() {
1161
            var args = slice.call(arguments);
1162
                
1163
            // TODO: it might not be a good idea to set the state in set_state method
1164
            if (args.length > 0 && models.VM.STATES.indexOf(args[0]) > -1) {
1165
                this.set({'state': args[0]});
1166
            }
1167

    
1168
            return this.get('state');
1169
        },
1170
        
1171
        // get the state that the api status corresponds to
1172
        state_for_api_status: function(status) {
1173
            return this.state_transition(this.state(), status);
1174
        },
1175
        
1176
        // vm state equals vm api status
1177
        state_is_status: function(state) {
1178
            return models.VM.STATUSES.indexOf(state) != -1;
1179
        },
1180
        
1181
        // get transition state for the corresponging api status
1182
        state_transition: function(state, new_status) {
1183
            var statuses = models.VM.STATES_TRANSITIONS[state];
1184
            if (statuses) {
1185
                if (statuses.indexOf(new_status) > -1) {
1186
                    return new_status;
1187
                } else {
1188
                    return state;
1189
                }
1190
            } else {
1191
                return new_status;
1192
            }
1193
        },
1194
        
1195
        // the current vm state is a transition state
1196
        in_transition: function() {
1197
            return models.VM.TRANSITION_STATES.indexOf(this.state()) > -1 || 
1198
                models.VM.TRANSITION_STATES.indexOf(this.get('status')) > -1;
1199
        },
1200
        
1201
        // get image object
1202
        get_image: function(callback) {
1203
            var image = storage.images.get(this.get('imageRef'));
1204
            if (!image) {
1205
                storage.images.update_unknown_id(this.get('imageRef'), callback);
1206
                return;
1207
            }
1208
            callback(image);
1209
            return image;
1210
        },
1211
        
1212
        // get flavor object
1213
        get_flavor: function() {
1214
            var flv = storage.flavors.get(this.get('flavorRef'));
1215
            if (!flv) {
1216
                storage.flavors.update_unknown_id(this.get('flavorRef'));
1217
                flv = storage.flavors.get(this.get('flavorRef'));
1218
            }
1219
            return flv;
1220
        },
1221

    
1222
        // retrieve the metadata object
1223
        get_meta: function(key) {
1224
            try {
1225
                return _.escape(this.get('metadata').values[key]);
1226
            } catch (err) {
1227
                return {};
1228
            }
1229
        },
1230
        
1231
        // get metadata OS value
1232
        get_os: function() {
1233
            return this.get_meta('OS') || (this.get_image(function(){}) ? 
1234
                                          this.get_image(function(){}).get_os() || "okeanos" : "okeanos");
1235
        },
1236

    
1237
        get_gui: function() {
1238
            return this.get_meta('GUI');
1239
        },
1240

    
1241
        // get public ip addresses
1242
        // TODO: public network is always the 0 index ???
1243
        get_addresses: function(net_id) {
1244
            var net_id = net_id || "public";
1245
            
1246
            var info = this.get_network_info(net_id);
1247
            if (!info) { return {} };
1248
            addrs = {};
1249
            _.each(info.values, function(addr) {
1250
                addrs["ip" + addr.version] = addr.addr;
1251
            });
1252
            return addrs
1253
        },
1254

    
1255
        get_network_info: function(net_id) {
1256
            var net_id = net_id || "public";
1257
            
1258
            if (!this.networks.network_ids.length) { return {} };
1259

    
1260
            var addresses = this.networks.get();
1261
            try {
1262
                return _.select(addresses, function(net, key){return key == net_id })[0];
1263
            } catch (err) {
1264
                //this.log.debug("Cannot find network {0}".format(net_id))
1265
            }
1266
        },
1267

    
1268
        firewall_profile: function(net_id) {
1269
            var net_id = net_id || "public";
1270
            var firewalls = this.get("firewalls");
1271
            return firewalls[net_id];
1272
        },
1273

    
1274
        has_firewall: function(net_id) {
1275
            var net_id = net_id || "public";
1276
            return ["ENABLED","PROTECTED"].indexOf(this.firewall_profile()) > -1;
1277
        },
1278
    
1279
        // get actions that the user can execute
1280
        // depending on the vm state/status
1281
        get_available_actions: function() {
1282
            return models.VM.AVAILABLE_ACTIONS[this.state()];
1283
        },
1284

    
1285
        set_profile: function(profile, net_id) {
1286
        },
1287
        
1288
        // call rename api
1289
        rename: function(new_name) {
1290
            //this.set({'name': new_name});
1291
            this.sync("update", this, {
1292
                critical: true,
1293
                data: {
1294
                    'server': {
1295
                        'name': new_name
1296
                    }
1297
                }, 
1298
                // do the rename after the method succeeds
1299
                success: _.bind(function(){
1300
                    //this.set({name: new_name});
1301
                    snf.api.trigger("call");
1302
                }, this)
1303
            });
1304
        },
1305
        
1306
        get_console_url: function(data) {
1307
            var url_params = {
1308
                machine: this.get("name"),
1309
                host_ip: this.get_addresses().ip4,
1310
                host_ip_v6: this.get_addresses().ip6,
1311
                host: data.host,
1312
                port: data.port,
1313
                password: data.password
1314
            }
1315
            return '/machines/console?' + $.param(url_params);
1316
        },
1317

    
1318
        // action helper
1319
        call: function(action_name, success, error) {
1320
            var id_param = [this.id];
1321

    
1322
            success = success || function() {};
1323
            error = error || function() {};
1324

    
1325
            var self = this;
1326

    
1327
            switch(action_name) {
1328
                case 'start':
1329
                    this.__make_api_call(this.get_action_url(), // vm actions url
1330
                                         "create", // create so that sync later uses POST to make the call
1331
                                         {start:{}}, // payload
1332
                                         function() {
1333
                                             // set state after successful call
1334
                                             self.state("START"); 
1335
                                             success.apply(this, arguments);
1336
                                             snf.api.trigger("call");
1337
                                         },  
1338
                                         error, 'start');
1339
                    break;
1340
                case 'reboot':
1341
                    this.__make_api_call(this.get_action_url(), // vm actions url
1342
                                         "create", // create so that sync later uses POST to make the call
1343
                                         {reboot:{type:"HARD"}}, // payload
1344
                                         function() {
1345
                                             // set state after successful call
1346
                                             self.state("REBOOT"); 
1347
                                             success.apply(this, arguments)
1348
                                             snf.api.trigger("call");
1349
                                             self.set({'reboot_required': false});
1350
                                         },
1351
                                         error, 'reboot');
1352
                    break;
1353
                case 'shutdown':
1354
                    this.__make_api_call(this.get_action_url(), // vm actions url
1355
                                         "create", // create so that sync later uses POST to make the call
1356
                                         {shutdown:{}}, // payload
1357
                                         function() {
1358
                                             // set state after successful call
1359
                                             self.state("SHUTDOWN"); 
1360
                                             success.apply(this, arguments)
1361
                                             snf.api.trigger("call");
1362
                                         },  
1363
                                         error, 'shutdown');
1364
                    break;
1365
                case 'console':
1366
                    this.__make_api_call(this.url() + "/action", "create", {'console': {'type':'vnc'}}, function(data) {
1367
                        var cons_data = data.console;
1368
                        success.apply(this, [cons_data]);
1369
                    }, undefined, 'console')
1370
                    break;
1371
                case 'destroy':
1372
                    this.__make_api_call(this.url(), // vm actions url
1373
                                         "delete", // create so that sync later uses POST to make the call
1374
                                         undefined, // payload
1375
                                         function() {
1376
                                             // set state after successful call
1377
                                             self.state('DESTROY');
1378
                                             success.apply(this, arguments)
1379
                                         },  
1380
                                         error, 'destroy');
1381
                    break;
1382
                default:
1383
                    throw "Invalid VM action ("+action_name+")";
1384
            }
1385
        },
1386
        
1387
        __make_api_call: function(url, method, data, success, error, action) {
1388
            var self = this;
1389
            error = error || function(){};
1390
            success = success || function(){};
1391

    
1392
            var params = {
1393
                url: url,
1394
                data: data,
1395
                success: function(){ self.handle_action_succeed.apply(self, arguments); success.apply(this, arguments)},
1396
                error: function(){ self.handle_action_fail.apply(self, arguments); error.apply(this, arguments)},
1397
                error_params: { ns: "Machines actions", 
1398
                                title: "'" + this.get("name") + "'" + " " + action + " failed", 
1399
                                extra_details: { 'Machine ID': this.id, 'URL': url, 'Action': action || "undefined" },
1400
                                allow_reload: false
1401
                              },
1402
                display: false,
1403
                critical: false
1404
            }
1405
            this.sync(method, this, params);
1406
        },
1407

    
1408
        handle_action_succeed: function() {
1409
            this.trigger("action:success", arguments);
1410
        },
1411
        
1412
        reset_action_error: function() {
1413
            this.action_error = false;
1414
            this.trigger("action:fail:reset", this.action_error);
1415
        },
1416

    
1417
        handle_action_fail: function() {
1418
            this.action_error = arguments;
1419
            this.trigger("action:fail", arguments);
1420
        },
1421

    
1422
        get_action_url: function(name) {
1423
            return this.url() + "/action";
1424
        },
1425

    
1426
        get_connection_info: function(host_os, success, error) {
1427
            var url = "/machines/connect";
1428
            params = {
1429
                ip_address: this.get_addresses().ip4,
1430
                os: this.get_os(),
1431
                host_os: host_os,
1432
                srv: this.id
1433
            }
1434

    
1435
            url = url + "?" + $.param(params);
1436

    
1437
            var ajax = snf.api.sync("read", undefined, { url: url, 
1438
                                                         error:error, 
1439
                                                         success:success, 
1440
                                                         handles_error:1});
1441
        }
1442
    })
1443
    
1444
    models.VM.ACTIONS = [
1445
        'start',
1446
        'shutdown',
1447
        'reboot',
1448
        'console',
1449
        'destroy'
1450
    ]
1451

    
1452
    models.VM.AVAILABLE_ACTIONS = {
1453
        'UNKNWON'       : ['destroy'],
1454
        'BUILD'         : ['destroy'],
1455
        'REBOOT'        : ['shutdown', 'destroy', 'console'],
1456
        'STOPPED'       : ['start', 'destroy'],
1457
        'ACTIVE'        : ['shutdown', 'destroy', 'reboot', 'console'],
1458
        'ERROR'         : ['destroy'],
1459
        'DELETED'        : [],
1460
        'DESTROY'       : [],
1461
        'BUILD_INIT'    : ['destroy'],
1462
        'BUILD_COPY'    : ['destroy'],
1463
        'BUILD_FINAL'   : ['destroy'],
1464
        'SHUTDOWN'      : ['destroy'],
1465
        'START'         : [],
1466
        'CONNECT'       : [],
1467
        'DISCONNECT'    : []
1468
    }
1469

    
1470
    // api status values
1471
    models.VM.STATUSES = [
1472
        'UNKNWON',
1473
        'BUILD',
1474
        'REBOOT',
1475
        'STOPPED',
1476
        'ACTIVE',
1477
        'ERROR',
1478
        'DELETED'
1479
    ]
1480

    
1481
    // api status values
1482
    models.VM.CONNECT_STATES = [
1483
        'ACTIVE',
1484
        'REBOOT',
1485
        'SHUTDOWN'
1486
    ]
1487

    
1488
    // vm states
1489
    models.VM.STATES = models.VM.STATUSES.concat([
1490
        'DESTROY',
1491
        'BUILD_INIT',
1492
        'BUILD_COPY',
1493
        'BUILD_FINAL',
1494
        'SHUTDOWN',
1495
        'START',
1496
        'CONNECT',
1497
        'DISCONNECT',
1498
        'FIREWALL'
1499
    ]);
1500
    
1501
    models.VM.STATES_TRANSITIONS = {
1502
        'DESTROY' : ['DELETED'],
1503
        'SHUTDOWN': ['ERROR', 'STOPPED', 'DESTROY'],
1504
        'STOPPED': ['ERROR', 'ACTIVE', 'DESTROY'],
1505
        'ACTIVE': ['ERROR', 'STOPPED', 'REBOOT', 'SHUTDOWN', 'DESTROY'],
1506
        'START': ['ERROR', 'ACTIVE', 'DESTROY'],
1507
        'REBOOT': ['ERROR', 'ACTIVE', 'STOPPED', 'DESTROY'],
1508
        'BUILD': ['ERROR', 'ACTIVE', 'DESTROY'],
1509
        'BUILD_COPY': ['ERROR', 'ACTIVE', 'BUILD_FINAL', 'DESTROY'],
1510
        'BUILD_FINAL': ['ERROR', 'ACTIVE', 'DESTROY'],
1511
        'BUILD_INIT': ['ERROR', 'ACTIVE', 'BUILD_COPY', 'BUILD_FINAL', 'DESTROY']
1512
    }
1513

    
1514
    models.VM.TRANSITION_STATES = [
1515
        'DESTROY',
1516
        'SHUTDOWN',
1517
        'START',
1518
        'REBOOT',
1519
        'BUILD'
1520
    ]
1521

    
1522
    models.VM.ACTIVE_STATES = [
1523
        'BUILD', 'REBOOT', 'ACTIVE',
1524
        'BUILD_INIT', 'BUILD_COPY', 'BUILD_FINAL',
1525
        'SHUTDOWN', 'CONNECT', 'DISCONNECT'
1526
    ]
1527

    
1528
    models.VM.BUILDING_STATES = [
1529
        'BUILD', 'BUILD_INIT', 'BUILD_COPY', 'BUILD_FINAL'
1530
    ]
1531

    
1532
    models.Networks = models.Collection.extend({
1533
        model: models.Network,
1534
        path: 'networks',
1535
        details: true,
1536
        //noUpdate: true,
1537
        defaults: {'linked_to':[]},
1538

    
1539
        parse: function (resp, xhr) {
1540
            // FIXME: depricated global var
1541
            if (!resp) { return []};
1542
               
1543
            var data = _.map(resp.networks.values, _.bind(this.parse_net_api_data, this));
1544
            return data;
1545
        },
1546

    
1547
        reset_pending_actions: function() {
1548
            this.each(function(net) {
1549
                net.get("actions").reset();
1550
            })
1551
        },
1552

    
1553
        do_all_pending_actions: function() {
1554
            this.each(function(net) {
1555
                net.do_all_pending_actions();
1556
            })
1557
        },
1558

    
1559
        parse_net_api_data: function(data) {
1560
            if (data.servers && data.servers.values) {
1561
                data['linked_to'] = data.servers.values;
1562
            }
1563
            return data;
1564
        },
1565

    
1566
        create: function (name, callback) {
1567
            return this.api_call(this.path, "create", {network:{name:name}}, callback);
1568
        }
1569
    })
1570

    
1571
    models.Images = models.Collection.extend({
1572
        model: models.Image,
1573
        path: 'images',
1574
        details: true,
1575
        noUpdate: true,
1576
        supportIncUpdates: false,
1577
        meta_keys_as_attrs: ["OS", "description", "kernel", "size", "GUI"],
1578
        read_method: 'read',
1579

    
1580
        // update collection model with id passed
1581
        // making a direct call to the image
1582
        // api url
1583
        update_unknown_id: function(id, callback) {
1584
            var url = getUrl.call(this) + "/" + id;
1585
            this.api_call(this.path + "/" + id, this.read_method, {_options:{async:true, skip_api_error:true}}, undefined, 
1586
            _.bind(function() {
1587
                if (!this.get(id)) {
1588
                            if (this.fallback_service) {
1589
                        // if current service has fallback_service attribute set
1590
                        // use this service to retrieve the missing image model
1591
                        var tmpservice = new this.fallback_service();
1592
                        tmpservice.update_unknown_id(id, _.bind(function(img){
1593
                            img.attributes.status = "DELETED";
1594
                            this.add(img.attributes);
1595
                            callback(this.get(id));
1596
                        }, this));
1597
                    } else {
1598
                        var title = synnefo.config.image_deleted_title || 'Deleted';
1599
                        // else add a dummy DELETED state image entry
1600
                        this.add({id:id, name:title, size:-1, 
1601
                                  progress:100, status:"DELETED"});
1602
                        callback(this.get(id));
1603
                    }   
1604
                } else {
1605
                    callback(this.get(id));
1606
                }
1607
            }, this), _.bind(function(image, msg, xhr) {
1608
                if (!image) {
1609
                    var title = synnefo.config.image_deleted_title || 'Deleted';
1610
                    this.add({id:id, name:title, size:-1, 
1611
                              progress:100, status:"DELETED"});
1612
                    callback(this.get(id));
1613
                    return;
1614
                }
1615
                var img_data = this._read_image_from_request(image, msg, xhr);
1616
                this.add(img_data);
1617
                callback(this.get(id));
1618
            }, this));
1619
        },
1620

    
1621
        _read_image_from_request: function(image, msg, xhr) {
1622
            return image.image;
1623
        },
1624

    
1625
        parse: function (resp, xhr) {
1626
            // FIXME: depricated global var
1627
            var data = _.map(resp.images.values, _.bind(this.parse_meta, this));
1628
            return resp.images.values;
1629
        },
1630

    
1631
        get_meta_key: function(img, key) {
1632
            if (img.metadata && img.metadata.values && img.metadata.values[key]) {
1633
                return _.escape(img.metadata.values[key]);
1634
            }
1635
            return undefined;
1636
        },
1637

    
1638
        comparator: function(img) {
1639
            return -img.get_sort_order("sortorder") || 1000 * img.id;
1640
        },
1641

    
1642
        parse_meta: function(img) {
1643
            _.each(this.meta_keys_as_attrs, _.bind(function(key){
1644
                if (img[key]) { return };
1645
                img[key] = this.get_meta_key(img, key) || "";
1646
            }, this));
1647
            return img;
1648
        },
1649

    
1650
        active: function() {
1651
            return this.filter(function(img){return img.get('status') != "DELETED"});
1652
        },
1653

    
1654
        predefined: function() {
1655
            return _.filter(this.active(), function(i) { return !i.get("serverRef")});
1656
        },
1657
        
1658
        fetch_for_type: function(type, complete, error) {
1659
            this.fetch({update:true, 
1660
                        success: complete, 
1661
                        error: error, 
1662
                        skip_api_error: true });
1663
        },
1664
        
1665
        get_images_for_type: function(type) {
1666
            if (this['get_{0}_images'.format(type)]) {
1667
                return this['get_{0}_images'.format(type)]();
1668
            }
1669

    
1670
            return this.active();
1671
        },
1672

    
1673
        update_images_for_type: function(type, onStart, onComplete, onError, force_load) {
1674
            var load = false;
1675
            error = onError || function() {};
1676
            function complete(collection) { 
1677
                onComplete(collection.get_images_for_type(type)); 
1678
            }
1679
            
1680
            // do we need to fetch/update current collection entries
1681
            if (load) {
1682
                onStart();
1683
                this.fetch_for_type(type, complete, error);
1684
            } else {
1685
                // fallback to complete
1686
                complete(this);
1687
            }
1688
        }
1689
    })
1690

    
1691
    models.Flavors = models.Collection.extend({
1692
        model: models.Flavor,
1693
        path: 'flavors',
1694
        details: true,
1695
        noUpdate: true,
1696
        supportIncUpdates: false,
1697
        // update collection model with id passed
1698
        // making a direct call to the flavor
1699
        // api url
1700
        update_unknown_id: function(id, callback) {
1701
            var url = getUrl.call(this) + "/" + id;
1702
            this.api_call(this.path + "/" + id, "read", {_options:{async:false, skip_api_error:true}}, undefined, 
1703
            _.bind(function() {
1704
                this.add({id:id, cpu:"", ram:"", disk:"", name: "", status:"DELETED"})
1705
            }, this), _.bind(function(flv) {
1706
                if (!flv.flavor.status) { flv.flavor.status = "DELETED" };
1707
                this.add(flv.flavor);
1708
            }, this));
1709
        },
1710

    
1711
        parse: function (resp, xhr) {
1712
            // FIXME: depricated global var
1713
            return _.map(resp.flavors.values, function(o) { o.disk_template = o['SNF:disk_template']; return o});
1714
        },
1715

    
1716
        comparator: function(flv) {
1717
            return flv.get("disk") * flv.get("cpu") * flv.get("ram");
1718
        },
1719

    
1720
        unavailable_values_for_image: function(img, flavors) {
1721
            var flavors = flavors || this.active();
1722
            var size = img.get_size();
1723
            
1724
            var index = {cpu:[], disk:[], ram:[]};
1725

    
1726
            _.each(this.active(), function(el) {
1727
                var img_size = size;
1728
                var flv_size = el.get_disk_size();
1729
                if (flv_size < img_size) {
1730
                    if (index.disk.indexOf(flv_size) == -1) {
1731
                        index.disk.push(flv_size);
1732
                    }
1733
                };
1734
            });
1735
            
1736
            return index;
1737
        },
1738

    
1739
        get_flavor: function(cpu, mem, disk, disk_template, filter_list) {
1740
            if (!filter_list) { filter_list = this.models };
1741
            
1742
            return this.select(function(flv){
1743
                if (flv.get("cpu") == cpu + "" &&
1744
                   flv.get("ram") == mem + "" &&
1745
                   flv.get("disk") == disk + "" &&
1746
                   flv.get("disk_template") == disk_template &&
1747
                   filter_list.indexOf(flv) > -1) { return true; }
1748
            })[0];
1749
        },
1750
        
1751
        get_data: function(lst) {
1752
            var data = {'cpu': [], 'mem':[], 'disk':[]};
1753

    
1754
            _.each(lst, function(flv) {
1755
                if (data.cpu.indexOf(flv.get("cpu")) == -1) {
1756
                    data.cpu.push(flv.get("cpu"));
1757
                }
1758
                if (data.mem.indexOf(flv.get("ram")) == -1) {
1759
                    data.mem.push(flv.get("ram"));
1760
                }
1761
                if (data.disk.indexOf(flv.get("disk")) == -1) {
1762
                    data.disk.push(flv.get("disk"));
1763
                }
1764
            })
1765
            
1766
            return data;
1767
        },
1768

    
1769
        active: function() {
1770
            return this.filter(function(flv){return flv.get('status') != "DELETED"});
1771
        }
1772
            
1773
    })
1774

    
1775
    models.VMS = models.Collection.extend({
1776
        model: models.VM,
1777
        path: 'servers',
1778
        details: true,
1779
        copy_image_meta: true,
1780
        
1781
        parse: function (resp, xhr) {
1782
            // FIXME: depricated after refactoring
1783
            var data = resp;
1784
            if (!resp) { return [] };
1785
            data = _.filter(_.map(resp.servers.values, _.bind(this.parse_vm_api_data, this)), function(v){return v});
1786
            return data;
1787
        },
1788
        
1789
        get_reboot_required: function() {
1790
            return this.filter(function(vm){return vm.get("reboot_required") == true})
1791
        },
1792

    
1793
        has_pending_actions: function() {
1794
            return this.filter(function(vm){return vm.pending_action}).length > 0;
1795
        },
1796

    
1797
        reset_pending_actions: function() {
1798
            this.each(function(vm) {
1799
                vm.clear_pending_action();
1800
            })
1801
        },
1802

    
1803
        do_all_pending_actions: function(success, error) {
1804
            this.each(function(vm) {
1805
                if (vm.has_pending_action()) {
1806
                    vm.call(vm.pending_action, success, error);
1807
                    vm.clear_pending_action();
1808
                }
1809
            })
1810
        },
1811
        
1812
        do_all_reboots: function(success, error) {
1813
            this.each(function(vm) {
1814
                if (vm.get("reboot_required")) {
1815
                    vm.call("reboot", success, error);
1816
                }
1817
            });
1818
        },
1819

    
1820
        reset_reboot_required: function() {
1821
            this.each(function(vm) {
1822
                vm.set({'reboot_required': undefined});
1823
            })
1824
        },
1825
        
1826
        stop_stats_update: function(exclude) {
1827
            var exclude = exclude || [];
1828
            this.each(function(vm) {
1829
                if (exclude.indexOf(vm) > -1) {
1830
                    return;
1831
                }
1832
                vm.stop_stats_update();
1833
            })
1834
        },
1835
        
1836
        has_meta: function(vm_data) {
1837
            return vm_data.metadata && vm_data.metadata.values
1838
        },
1839

    
1840
        has_addresses: function(vm_data) {
1841
            return vm_data.metadata && vm_data.metadata.values
1842
        },
1843

    
1844
        parse_vm_api_data: function(data) {
1845
            // do not add non existing DELETED entries
1846
            if (data.status && data.status == "DELETED") {
1847
                if (!this.get(data.id)) {
1848
                    return false;
1849
                }
1850
            }
1851

    
1852
            // OS attribute
1853
            if (this.has_meta(data)) {
1854
                data['OS'] = data.metadata.values.OS || "okeanos";
1855
            }
1856
            
1857
            data['firewalls'] = {};
1858
            if (data['addresses'] && data['addresses'].values) {
1859
                data['linked_to_nets'] = data['addresses'].values;
1860
                _.each(data['addresses'].values, function(f){
1861
                    if (f['firewallProfile']) {
1862
                        data['firewalls'][f['id']] = f['firewallProfile']
1863
                    }
1864
                });
1865
            }
1866
            
1867
            // if vm has no metadata, no metadata object
1868
            // is in json response, reset it to force
1869
            // value update
1870
            if (!data['metadata']) {
1871
                data['metadata'] = {values:{}};
1872
            }
1873

    
1874
            return data;
1875
        },
1876

    
1877
        create: function (name, image, flavor, meta, extra, callback) {
1878
            if (this.copy_image_meta) {
1879
                if (image.get("OS")) {
1880
                    meta['OS'] = image.get("OS");
1881
                }
1882
           }
1883
            
1884
            opts = {name: name, imageRef: image.id, flavorRef: flavor.id, metadata:meta}
1885
            opts = _.extend(opts, extra);
1886

    
1887
            this.api_call(this.path, "create", {'server': opts}, undefined, undefined, callback, {critical: true});
1888
        }
1889

    
1890
    })
1891

    
1892
    models.PublicKey = models.Model.extend({
1893
        path: 'keys',
1894
        base_url: '/ui/userdata',
1895
        details: false,
1896
        noUpdate: true,
1897

    
1898

    
1899
        get_public_key: function() {
1900
            return cryptico.publicKeyFromString(this.get("content"));
1901
        },
1902

    
1903
        get_filename: function() {
1904
            return "{0}.pub".format(this.get("name"));
1905
        },
1906

    
1907
        identify_type: function() {
1908
            try {
1909
                var cont = snf.util.validatePublicKey(this.get("content"));
1910
                var type = cont.split(" ")[0];
1911
                return synnefo.util.publicKeyTypesMap[type];
1912
            } catch (err) { return false };
1913
        }
1914

    
1915
    })
1916
    
1917
    models.PublicKeys = models.Collection.extend({
1918
        model: models.PublicKey,
1919
        details: false,
1920
        path: 'keys',
1921
        base_url: '/ui/userdata',
1922
        noUpdate: true,
1923

    
1924
        generate_new: function(success, error) {
1925
            snf.api.sync('create', undefined, {
1926
                url: getUrl.call(this, this.base_url) + "/generate", 
1927
                success: success, 
1928
                error: error,
1929
                skip_api_error: true
1930
            });
1931
        },
1932

    
1933
        add_crypto_key: function(key, success, error, options) {
1934
            var options = options || {};
1935
            var m = new models.PublicKey();
1936

    
1937
            // guess a name
1938
            var name_tpl = "public key";
1939
            var name = name_tpl;
1940
            var name_count = 1;
1941
            
1942
            while(this.filter(function(m){ return m.get("name") == name }).length > 0) {
1943
                name = name_tpl + " " + name_count;
1944
                name_count++;
1945
            }
1946
            
1947
            m.set({name: name});
1948
            m.set({content: key});
1949
            
1950
            options.success = function () { return success(m) };
1951
            options.errror = error;
1952
            options.skip_api_error = true;
1953
            
1954
            this.create(m.attributes, options);
1955
        }
1956
    })
1957
    
1958
    // storage initialization
1959
    snf.storage.images = new models.Images();
1960
    snf.storage.flavors = new models.Flavors();
1961
    snf.storage.networks = new models.Networks();
1962
    snf.storage.vms = new models.VMS();
1963
    snf.storage.keys = new models.PublicKeys();
1964

    
1965
    //snf.storage.vms.fetch({update:true});
1966
    //snf.storage.images.fetch({update:true});
1967
    //snf.storage.flavors.fetch({update:true});
1968

    
1969
})(this);