Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (67.9 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
            return this.get_size() > 0 ? util.readablizeBytes(this.get_size() * 1024 * 1024) : "unknown";
246
        },
247

    
248
        get_os: function() {
249
            return this.get("OS");
250
        },
251

    
252
        get_created_user: function() {
253
            return synnefo.config.os_created_users[this.get_os()] || "root";
254
        },
255

    
256
        get_sort_order: function() {
257
            return parseInt(this.get('metadata') ? this.get('metadata').values.sortorder : -1)
258
        },
259

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

    
267
        is_public: function() {
268
            return this.get('is_public') || true;
269
        },
270
        
271
        ssh_keys_path: function() {
272
            prepend = '';
273
            if (this.get_created_user() != 'root') {
274
                prepend = '/home'
275
            }
276
            return '{1}/{0}/.ssh/authorized_keys'.format(this.get_created_user(), prepend);
277
        },
278

    
279
        _supports_ssh: function() {
280
            if (synnefo.config.support_ssh_os_list.indexOf(this.get_os()) > -1) {
281
                return true;
282
            }
283
            return false;
284
        },
285

    
286
        supports: function(feature) {
287
            if (feature == "ssh") {
288
                return this._supports_ssh()
289
            }
290
            return false;
291
        },
292

    
293
        personality_data_for_keys: function(keys) {
294
            contents = '';
295
            _.each(keys, function(key){
296
                contents = contents + key.get("content") + "\n"
297
            });
298
            contents = $.base64.encode(contents);
299

    
300
            return {
301
                path: this.ssh_keys_path(),
302
                contents: contents
303
            }
304
        }
305
    });
306

    
307
    // Flavor model
308
    models.Flavor = models.Model.extend({
309
        path: 'flavors',
310

    
311
        details_string: function() {
312
            return "{0} CPU, {1}MB, {2}GB".format(this.get('cpu'), this.get('ram'), this.get('disk'));
313
        },
314

    
315
        get_disk_size: function() {
316
            return parseInt(this.get("disk") * 1000)
317
        },
318

    
319
        get_disk_template_info: function() {
320
            var info = snf.config.flavors_disk_templates_info[this.get("disk_template")];
321
            if (!info) {
322
                info = { name: this.get("disk_template"), description:'' };
323
            }
324
            return info
325
        }
326

    
327
    });
328
    
329
    //network vms list helper
330
    var NetworkVMSList = function() {
331
        this.initialize = function() {
332
            this.vms = [];
333
            this.pending = [];
334
            this.pending_for_removal = [];
335
        }
336
        
337
        this.add_pending_for_remove = function(vm_id) {
338
            if (this.pending_for_removal.indexOf(vm_id) == -1) {
339
                this.pending_for_removal.push(vm_id);
340
            }
341

    
342
            if (this.pending_for_removal.length) {
343
                this.trigger("pending:remove:add");
344
            }
345
        },
346

    
347
        this.add_pending = function(vm_id) {
348
            if (this.pending.indexOf(vm_id) == -1) {
349
                this.pending[this.pending.length] = vm_id;
350
            }
351

    
352
            if (this.pending.length) {
353
                this.trigger("pending:add");
354
            }
355
        }
356

    
357
        this.check_pending = function() {
358
            var len = this.pending.length;
359
            var args = [this.pending];
360
            this.pending = _.difference(this.pending, this.vms);
361
            if (len != this.pending.length) {
362
                if (this.pending.length == 0) {
363
                    this.trigger("pending:clear");
364
                }
365
            }
366

    
367
            var len = this.pending_for_removal.length;
368
            this.pending_for_removal = _.intersection(this.pending_for_removal, this.vms);
369
            if (this.pending_for_removal.length == 0) {
370
                this.trigger("pending:remove:clear");
371
            }
372

    
373
        }
374

    
375

    
376
        this.add = function(vm_id) {
377
            if (this.vms.indexOf(vm_id) == -1) {
378
                this.vms[this.vms.length] = vm_id;
379
                this.trigger("network:connect", vm_id);
380
                this.check_pending();
381
                return true;
382
            }
383
        }
384

    
385
        this.remove = function(vm_id) {
386
            if (this.vms.indexOf(vm_id) > -1) {
387
                this.vms = _.without(this.vms, vm_id);
388
                this.trigger("network:disconnect", vm_id);
389
                this.check_pending();
390
                return true;
391
            }
392
        }
393

    
394
        this.get = function() {
395
            return this.vms;
396
        }
397

    
398
        this.list = function() {
399
            return storage.vms.filter(_.bind(function(vm){
400
                return this.vms.indexOf(vm.id) > -1;
401
            }, this))
402
        }
403

    
404
        this.initialize();
405
    };
406
    _.extend(NetworkVMSList.prototype, bb.Events);
407
    
408
    // vm networks list helper
409
    var VMNetworksList = function() {
410
        this.initialize = function() {
411
            this.networks = {};
412
            this.network_ids = [];
413
        }
414

    
415
        this.add = function(net_id, data) {
416
            if (!this.networks[net_id]) {
417
                this.networks[net_id] = data || {};
418
                this.network_ids[this.network_ids.length] = net_id;
419
                this.trigger("network:connect", net_id);
420
                return true;
421
            }
422
        }
423

    
424
        this.remove = function(net_id) {
425
            if (this.networks[net_id]) {
426
                delete this.networks[net_id];
427
                this.network_ids = _.without(this.network_ids, net_id);
428
                this.trigger("network:disconnect", net_id);
429
                return true;
430
            }
431
            return false;
432
        }
433

    
434
        this.get = function() {
435
            return this.networks;
436
        }
437

    
438
        this.list = function() {
439
            return storage.networks.filter(_.bind(function(net){
440
                return this.network_ids.indexOf(net.id) > -1;
441
            }, this))
442
        }
443

    
444
        this.initialize();
445
    };
446
    _.extend(VMNetworksList.prototype, bb.Events);
447
        
448
    models.ParamsList = function(){this.initialize.apply(this, arguments)};
449
    _.extend(models.ParamsList.prototype, bb.Events, {
450

    
451
        initialize: function(parent, param_name) {
452
            this.parent = parent;
453
            this.actions = {};
454
            this.param_name = param_name;
455
            this.length = 0;
456
        },
457
        
458
        has_action: function(action) {
459
            return this.actions[action] ? true : false;
460
        },
461
            
462
        _parse_params: function(arguments) {
463
            if (arguments.length <= 1) {
464
                return [];
465
            }
466

    
467
            var args = _.toArray(arguments);
468
            return args.splice(1);
469
        },
470

    
471
        contains: function(action, params) {
472
            params = this._parse_params(arguments);
473
            var has_action = this.has_action(action);
474
            if (!has_action) { return false };
475

    
476
            var paramsEqual = false;
477
            _.each(this.actions[action], function(action_params) {
478
                if (_.isEqual(action_params, params)) {
479
                    paramsEqual = true;
480
                }
481
            });
482
                
483
            return paramsEqual;
484
        },
485
        
486
        is_empty: function() {
487
            return _.isEmpty(this.actions);
488
        },
489

    
490
        add: function(action, params) {
491
            params = this._parse_params(arguments);
492
            if (this.contains.apply(this, arguments)) { return this };
493
            var isnew = false
494
            if (!this.has_action(action)) {
495
                this.actions[action] = [];
496
                isnew = true;
497
            };
498

    
499
            this.actions[action].push(params);
500
            this.parent.trigger("change:" + this.param_name, this.parent, this);
501
            if (isnew) {
502
                this.trigger("add", action, params);
503
            } else {
504
                this.trigger("change", action, params);
505
            }
506
            return this;
507
        },
508
        
509
        remove_all: function(action) {
510
            if (this.has_action(action)) {
511
                delete this.actions[action];
512
                this.parent.trigger("change:" + this.param_name, this.parent, this);
513
                this.trigger("remove", action);
514
            }
515
            return this;
516
        },
517

    
518
        reset: function() {
519
            this.actions = {};
520
            this.parent.trigger("change:" + this.param_name, this.parent, this);
521
            this.trigger("reset");
522
            this.trigger("remove");
523
        },
524

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

    
545
    });
546

    
547
    // Image model
548
    models.Network = models.Model.extend({
549
        path: 'networks',
550
        has_status: true,
551
        
552
        initialize: function() {
553
            this.vms = new NetworkVMSList();
554
            this.vms.bind("pending:add", _.bind(this.handle_pending_connections, this, "add"));
555
            this.vms.bind("pending:clear", _.bind(this.handle_pending_connections, this, "clear"));
556
            this.vms.bind("pending:remove:add", _.bind(this.handle_pending_connections, this, "add"));
557
            this.vms.bind("pending:remove:clear", _.bind(this.handle_pending_connections, this, "clear"));
558

    
559
            var ret = models.Network.__super__.initialize.apply(this, arguments);
560

    
561
            storage.vms.bind("change:linked_to_nets", _.bind(this.update_connections, this, "vm:change"));
562
            storage.vms.bind("add", _.bind(this.update_connections, this, "add"));
563
            storage.vms.bind("remove", _.bind(this.update_connections, this, "remove"));
564
            storage.vms.bind("reset", _.bind(this.update_connections, this, "reset"));
565

    
566
            this.bind("change:linked_to", _.bind(this.update_connections, this, "net:change"));
567
            this.update_connections();
568
            this.update_state();
569
            
570
            this.set({"actions": new models.ParamsList(this, "actions")});
571

    
572
            return ret;
573
        },
574

    
575
        toJSON: function() {
576
            var attrs = _.clone(this.attributes);
577
            attrs.actions = _.clone(this.get("actions").actions);
578
            return attrs;
579
        },
580

    
581
        update_state: function() {
582
            if (this.vms.pending.length) {
583
                this.set({state: "CONNECTING"});
584
                return
585
            }
586

    
587
            if (this.vms.pending_for_removal.length) {
588
                this.set({state: "DISCONNECTING"});
589
                return
590
            }   
591
            
592
            var firewalling = false;
593
            _.each(this.vms.get(), _.bind(function(vm_id){
594
                var vm = storage.vms.get(vm_id);
595
                if (!vm) { return };
596
                if (!_.isEmpty(vm.pending_firewalls)) {
597
                    this.set({state:"FIREWALLING"});
598
                    firewalling = true;
599
                    return false;
600
                }
601
            },this));
602
            if (firewalling) { return };
603

    
604
            this.set({state:"NORMAL"});
605
        },
606

    
607
        handle_pending_connections: function(action) {
608
            this.update_state();
609
        },
610

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

    
656
        is_public: function() {
657
            return this.id == "public";
658
        },
659

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

    
686
        add_vm: function (vm, callback, error, options) {
687
            var payload = {add:{serverRef:"" + vm.id}};
688
            payload._options = options || {};
689
            return this.api_call(this.api_path() + "/action", "create", 
690
                                 payload,
691
                                 _.bind(function(){
692
                                     this.vms.add_pending(vm.id);
693
                                     if (callback) {callback()}
694
                                 },this), error);
695
        },
696

    
697
        remove_vm: function (vm, callback, error, options) {
698
            var payload = {remove:{serverRef:"" + vm.id}};
699
            payload._options = options || {};
700
            return this.api_call(this.api_path() + "/action", "create", 
701
                                 {remove:{serverRef:"" + vm.id}},
702
                                 _.bind(function(){
703
                                     this.vms.add_pending_for_remove(vm.id);
704
                                     if (callback) {callback()}
705
                                 },this), error);
706
        },
707

    
708
        rename: function(name, callback) {
709
            return this.api_call(this.api_path(), "update", {
710
                network:{name:name}, 
711
                _options:{
712
                    critical: false, 
713
                    error_params:{
714
                        title: "Network action failed",
715
                        ns: "Networks",
716
                        extra_details: {"Network id": this.id}
717
                    }
718
                }}, callback);
719
        },
720

    
721
        get_connectable_vms: function() {
722
            var servers = this.vms.list();
723
            return storage.vms.filter(function(vm){
724
                return servers.indexOf(vm) == -1 && !vm.in_error_state();
725
            })
726
        },
727

    
728
        state_message: function() {
729
            if (this.get("state") == "NORMAL" && this.is_public()) {
730
                return "Public network";
731
            }
732

    
733
            return models.Network.STATES[this.get("state")];
734
        },
735

    
736
        in_progress: function() {
737
            return models.Network.STATES_TRANSITIONS[this.get("state")] != undefined;
738
        },
739

    
740
        do_all_pending_actions: function(success, error) {
741
            var destroy = this.get("actions").has_action("destroy");
742
            _.each(this.get("actions").actions, _.bind(function(params, action) {
743
                _.each(params, _.bind(function(with_params) {
744
                    this.call(action, with_params, success, error);
745
                }, this));
746
            }, this));
747
        }
748
    });
749
    
750
    models.Network.STATES = {
751
        'NORMAL': 'Private network',
752
        'CONNECTING': 'Connecting...',
753
        'DISCONNECTING': 'Disconnecting...',
754
        'FIREWALLING': 'Firewall update...',
755
        'DESTROY': 'Destroying...'
756
    }
757

    
758
    models.Network.STATES_TRANSITIONS = {
759
        'CONNECTING': ['NORMAL'],
760
        'DISCONNECTING': ['NORMAL'],
761
        'FIREWALLING': ['NORMAL']
762
    }
763

    
764
    // Virtualmachine model
765
    models.VM = models.Model.extend({
766

    
767
        path: 'servers',
768
        has_status: true,
769
        initialize: function(params) {
770
            this.networks = new VMNetworksList();
771
            
772
            this.pending_firewalls = {};
773
            
774
            models.VM.__super__.initialize.apply(this, arguments);
775

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

    
793
            // initialize interval
794
            this.init_stats_intervals(this.stats_update_interval);
795
            
796
            this.bind("change:progress", _.bind(this.update_building_progress, this));
797
            this.update_building_progress();
798

    
799
            this.bind("change:firewalls", _.bind(this.handle_firewall_change, this));
800
            
801
            // default values
802
            this.set({linked_to_nets:this.get("linked_to_nets") || []});
803
            this.set({firewalls:this.get("firewalls") || []});
804

    
805
            this.bind("change:state", _.bind(function(){if (this.state() == "DESTROY") { this.handle_destroy() }}, this))
806
        },
807

    
808
        handle_firewall_change: function() {
809

    
810
        },
811
        
812
        set_linked_to_nets: function(data) {
813
            this.set({"linked_to":_.map(data, function(n){ return n.id})});
814
            return data;
815
        },
816

    
817
        is_connected_to: function(net) {
818
            return _.filter(this.networks.list(), function(n){return n.id == net.id}).length > 0;
819
        },
820
        
821
        status: function(st) {
822
            if (!st) { return this.get("status")}
823
            return this.set({status:st});
824
        },
825

    
826
        set_status: function(st) {
827
            var new_state = this.state_for_api_status(st);
828
            var transition = false;
829

    
830
            if (this.state() != new_state) {
831
                if (models.VM.STATES_TRANSITIONS[this.state()]) {
832
                    transition = this.state();
833
                }
834
            }
835
            
836
            // call it silently to avoid double change trigger
837
            this.set({'state': this.state_for_api_status(st)}, {silent: true});
838
            
839
            // trigger transition
840
            if (transition && models.VM.TRANSITION_STATES.indexOf(new_state) == -1) { 
841
                this.trigger("transition", {from:transition, to:new_state}) 
842
            };
843
            return st;
844
        },
845

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

    
871
        get_copy_details: function(human, image, callback) {
872
            var human = human || false;
873
            var image = image || this.get_image(_.bind(function(image){
874
                var progress = this.get('progress');
875
                var size = image.get_size();
876
                var size_copied = (size * progress / 100).toFixed(2);
877
                
878
                if (human) {
879
                    size = util.readablizeBytes(size*1024*1024);
880
                    size_copied = util.readablizeBytes(size_copied*1024*1024);
881
                }
882

    
883
                callback({'progress': progress, 'size': size, 'copy': size_copied})
884
            }, this));
885
        },
886

    
887
        start_stats_update: function(force_if_empty) {
888
            var prev_state = this.do_update_stats;
889

    
890
            this.do_update_stats = true;
891
            
892
            // fetcher initialized ??
893
            if (!this.stats_fetcher) {
894
                this.init_stats_intervals();
895
            }
896

    
897

    
898
            // fetcher running ???
899
            if (!this.stats_fetcher.running || !prev_state) {
900
                this.stats_fetcher.start();
901
            }
902

    
903
            if (force_if_empty && this.get("stats") == undefined) {
904
                this.update_stats(true);
905
            }
906
        },
907

    
908
        stop_stats_update: function(stop_calls) {
909
            this.do_update_stats = false;
910

    
911
            if (stop_calls) {
912
                this.stats_fetcher.stop();
913
            }
914
        },
915

    
916
        // clear and reinitialize update interval
917
        init_stats_intervals: function (interval) {
918
            this.stats_fetcher = this.get_stats_fetcher(this.stats_update_interval);
919
            this.stats_fetcher.start();
920
        },
921
        
922
        get_stats_fetcher: function(timeout) {
923
            var cb = _.bind(function(data){
924
                this.update_stats();
925
            }, this);
926
            var fetcher = new snf.api.updateHandler({'callback': cb, interval: timeout, id:'stats'});
927
            return fetcher;
928
        },
929

    
930
        // do the api call
931
        update_stats: function(force) {
932
            // do not update stats if flag not set
933
            if ((!this.do_update_stats && !force) || this.updating_stats) {
934
                return;
935
            }
936

    
937
            // make the api call, execute handle_stats_update on sucess
938
            // TODO: onError handler ???
939
            stats_url = this.url() + "/stats";
940
            this.updating_stats = true;
941
            this.sync("read", this, {
942
                handles_error:true, 
943
                url: stats_url, 
944
                refresh:true, 
945
                success: _.bind(this.handle_stats_update, this),
946
                error: _.bind(this.handle_stats_error, this),
947
                complete: _.bind(function(){this.updating_stats = false;}, this),
948
                critical: false,
949
                log_error: false
950
            });
951
        },
952

    
953
        get_stats_image: function(stat, type) {
954
        },
955
        
956
        _set_stats: function(stats) {
957
            var silent = silent === undefined ? false : silent;
958
            // unavailable stats while building
959
            if (this.get("status") == "BUILD") { 
960
                this.stats_available = false;
961
            } else { this.stats_available = true; }
962

    
963
            if (this.get("status") == "DESTROY") { this.stats_available = false; }
964
            
965
            this.set({stats: stats}, {silent:true});
966
            this.trigger("stats:update", stats);
967
        },
968

    
969
        unbind: function() {
970
            models.VM.__super__.unbind.apply(this, arguments);
971
        },
972

    
973
        handle_stats_error: function() {
974
            stats = {};
975
            _.each(['cpuBar', 'cpuTimeSeries', 'netBar', 'netTimeSeries'], function(k) {
976
                stats[k] = false;
977
            });
978

    
979
            this.set({'stats': stats});
980
        },
981

    
982
        // this method gets executed after a successful vm stats api call
983
        handle_stats_update: function(data) {
984
            var self = this;
985
            // avoid browser caching
986
            
987
            if (data.stats && _.size(data.stats) > 0) {
988
                var ts = $.now();
989
                var stats = data.stats;
990
                var images_loaded = 0;
991
                var images = {};
992

    
993
                function check_images_loaded() {
994
                    images_loaded++;
995

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

    
1016
                    img.error(function() {
1017
                        images[stat + type] = false;
1018
                        check_images_loaded();
1019
                    });
1020

    
1021
                    img.attr({'src': stats[k]});
1022
                })
1023
                data.stats = stats;
1024
            }
1025

    
1026
            // do we need to change the interval ??
1027
            if (data.stats.refresh * 1000 != this.stats_update_interval) {
1028
                this.stats_update_interval = data.stats.refresh * 1000;
1029
                this.stats_fetcher.interval = this.stats_update_interval;
1030
                this.stats_fetcher.maximum_interval = this.stats_update_interval;
1031
                this.stats_fetcher.stop();
1032
                this.stats_fetcher.start(false);
1033
            }
1034
        },
1035

    
1036
        // helper method that sets the do_update_stats
1037
        // in the future this method could also make an api call
1038
        // immediaetly if needed
1039
        enable_stats_update: function() {
1040
            this.do_update_stats = true;
1041
        },
1042
        
1043
        handle_destroy: function() {
1044
            this.stats_fetcher.stop();
1045
        },
1046

    
1047
        require_reboot: function() {
1048
            if (this.is_active()) {
1049
                this.set({'reboot_required': true});
1050
            }
1051
        },
1052
        
1053
        set_pending_action: function(data) {
1054
            this.pending_action = data;
1055
            return data;
1056
        },
1057

    
1058
        // machine has pending action
1059
        update_pending_action: function(action, force) {
1060
            this.set({pending_action: action});
1061
        },
1062

    
1063
        clear_pending_action: function() {
1064
            this.set({pending_action: undefined});
1065
        },
1066

    
1067
        has_pending_action: function() {
1068
            return this.get("pending_action") ? this.get("pending_action") : false;
1069
        },
1070
        
1071
        // machine is active
1072
        is_active: function() {
1073
            return models.VM.ACTIVE_STATES.indexOf(this.state()) > -1;
1074
        },
1075
        
1076
        // machine is building 
1077
        is_building: function() {
1078
            return models.VM.BUILDING_STATES.indexOf(this.state()) > -1;
1079
        },
1080
        
1081
        in_error_state: function() {
1082
            return this.state() === "ERROR"
1083
        },
1084

    
1085
        // user can connect to machine
1086
        is_connectable: function() {
1087
            // check if ips exist
1088
            if (!this.get_addresses().ip4 && !this.get_addresses().ip6) {
1089
                return false;
1090
            }
1091
            return models.VM.CONNECT_STATES.indexOf(this.state()) > -1;
1092
        },
1093
        
1094
        set_firewalls: function(data) {
1095
            _.each(data, _.bind(function(val, key){
1096
                if (this.pending_firewalls && this.pending_firewalls[key] && this.pending_firewalls[key] == val) {
1097
                        this.require_reboot();
1098
                        this.remove_pending_firewall(key, val);
1099
                }
1100
            }, this));
1101
            return data;
1102
        },
1103

    
1104
        remove_pending_firewall: function(net_id, value) {
1105
            if (this.pending_firewalls[net_id] == value) {
1106
                delete this.pending_firewalls[net_id];
1107
                storage.networks.get(net_id).update_state();
1108
            }
1109
        },
1110
            
1111
        remove_meta: function(key, complete, error) {
1112
            var url = this.api_path() + "/meta/" + key;
1113
            this.api_call(url, "delete", undefined, complete, error);
1114
        },
1115

    
1116
        save_meta: function(meta, complete, error) {
1117
            var url = this.api_path() + "/meta/" + meta.key;
1118
            var payload = {meta:{}};
1119
            payload.meta[meta.key] = meta.value;
1120
            payload._options = {
1121
                critical:false, 
1122
                error_params: {
1123
                    title: "Machine metadata error",
1124
                    extra_details: {"Machine id": this.id}
1125
            }};
1126

    
1127
            this.api_call(url, "update", payload, complete, error);
1128
        },
1129

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

    
1133
            this.pending_firewalls[net_id] = value;
1134
            this.trigger("change", this, this);
1135
            var payload = {"firewallProfile":{"profile":value}};
1136
            payload._options = _.extend({critical: false}, options);
1137
            
1138
            // reset firewall state on error
1139
            var error_cb = _.bind(function() {
1140
                thi
1141
            }, this);
1142

    
1143
            this.api_call(this.api_path() + "/action", "create", payload, callback, error);
1144
            storage.networks.get(net_id).update_state();
1145
        },
1146

    
1147
        firewall_pending: function(net_id) {
1148
            return this.pending_firewalls[net_id] != undefined;
1149
        },
1150
        
1151
        // update/get the state of the machine
1152
        state: function() {
1153
            var args = slice.call(arguments);
1154
                
1155
            // TODO: it might not be a good idea to set the state in set_state method
1156
            if (args.length > 0 && models.VM.STATES.indexOf(args[0]) > -1) {
1157
                this.set({'state': args[0]});
1158
            }
1159

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

    
1214
        // retrieve the metadata object
1215
        get_meta: function(key) {
1216
            try {
1217
                return _.escape(this.get('metadata').values[key]);
1218
            } catch (err) {
1219
                return {};
1220
            }
1221
        },
1222
        
1223
        // get metadata OS value
1224
        get_os: function() {
1225
            return this.get_meta('OS') || (this.get_image(function(){}) ? 
1226
                                          this.get_image(function(){}).get_os() || "okeanos" : "okeanos");
1227
        },
1228

    
1229
        // get public ip addresses
1230
        // TODO: public network is always the 0 index ???
1231
        get_addresses: function(net_id) {
1232
            var net_id = net_id || "public";
1233
            
1234
            var info = this.get_network_info(net_id);
1235
            if (!info) { return {} };
1236
            addrs = {};
1237
            _.each(info.values, function(addr) {
1238
                addrs["ip" + addr.version] = addr.addr;
1239
            });
1240
            return addrs
1241
        },
1242

    
1243
        get_network_info: function(net_id) {
1244
            var net_id = net_id || "public";
1245
            
1246
            if (!this.networks.network_ids.length) { return {} };
1247

    
1248
            var addresses = this.networks.get();
1249
            try {
1250
                return _.select(addresses, function(net, key){return key == net_id })[0];
1251
            } catch (err) {
1252
                //this.log.debug("Cannot find network {0}".format(net_id))
1253
            }
1254
        },
1255

    
1256
        firewall_profile: function(net_id) {
1257
            var net_id = net_id || "public";
1258
            var firewalls = this.get("firewalls");
1259
            return firewalls[net_id];
1260
        },
1261

    
1262
        has_firewall: function(net_id) {
1263
            var net_id = net_id || "public";
1264
            return ["ENABLED","PROTECTED"].indexOf(this.firewall_profile()) > -1;
1265
        },
1266
    
1267
        // get actions that the user can execute
1268
        // depending on the vm state/status
1269
        get_available_actions: function() {
1270
            return models.VM.AVAILABLE_ACTIONS[this.state()];
1271
        },
1272

    
1273
        set_profile: function(profile, net_id) {
1274
        },
1275
        
1276
        // call rename api
1277
        rename: function(new_name) {
1278
            //this.set({'name': new_name});
1279
            this.sync("update", this, {
1280
                critical: true,
1281
                data: {
1282
                    'server': {
1283
                        'name': new_name
1284
                    }
1285
                }, 
1286
                // do the rename after the method succeeds
1287
                success: _.bind(function(){
1288
                    //this.set({name: new_name});
1289
                    snf.api.trigger("call");
1290
                }, this)
1291
            });
1292
        },
1293
        
1294
        get_console_url: function(data) {
1295
            var url_params = {
1296
                machine: this.get("name"),
1297
                host_ip: this.get_addresses().ip4,
1298
                host_ip_v6: this.get_addresses().ip6,
1299
                host: data.host,
1300
                port: data.port,
1301
                password: data.password
1302
            }
1303
            return '/machines/console?' + $.param(url_params);
1304
        },
1305

    
1306
        // action helper
1307
        call: function(action_name, success, error) {
1308
            var id_param = [this.id];
1309

    
1310
            success = success || function() {};
1311
            error = error || function() {};
1312

    
1313
            var self = this;
1314

    
1315
            switch(action_name) {
1316
                case 'start':
1317
                    this.__make_api_call(this.get_action_url(), // vm actions url
1318
                                         "create", // create so that sync later uses POST to make the call
1319
                                         {start:{}}, // payload
1320
                                         function() {
1321
                                             // set state after successful call
1322
                                             self.state("START"); 
1323
                                             success.apply(this, arguments);
1324
                                             snf.api.trigger("call");
1325
                                         },  
1326
                                         error, 'start');
1327
                    break;
1328
                case 'reboot':
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
                                         {reboot:{type:"HARD"}}, // payload
1332
                                         function() {
1333
                                             // set state after successful call
1334
                                             self.state("REBOOT"); 
1335
                                             success.apply(this, arguments)
1336
                                             snf.api.trigger("call");
1337
                                             self.set({'reboot_required': false});
1338
                                         },
1339
                                         error, 'reboot');
1340
                    break;
1341
                case 'shutdown':
1342
                    this.__make_api_call(this.get_action_url(), // vm actions url
1343
                                         "create", // create so that sync later uses POST to make the call
1344
                                         {shutdown:{}}, // payload
1345
                                         function() {
1346
                                             // set state after successful call
1347
                                             self.state("SHUTDOWN"); 
1348
                                             success.apply(this, arguments)
1349
                                             snf.api.trigger("call");
1350
                                         },  
1351
                                         error, 'shutdown');
1352
                    break;
1353
                case 'console':
1354
                    this.__make_api_call(this.url() + "/action", "create", {'console': {'type':'vnc'}}, function(data) {
1355
                        var cons_data = data.console;
1356
                        success.apply(this, [cons_data]);
1357
                    }, undefined, 'console')
1358
                    break;
1359
                case 'destroy':
1360
                    this.__make_api_call(this.url(), // vm actions url
1361
                                         "delete", // create so that sync later uses POST to make the call
1362
                                         undefined, // payload
1363
                                         function() {
1364
                                             // set state after successful call
1365
                                             self.state('DESTROY');
1366
                                             success.apply(this, arguments)
1367
                                         },  
1368
                                         error, 'destroy');
1369
                    break;
1370
                default:
1371
                    throw "Invalid VM action ("+action_name+")";
1372
            }
1373
        },
1374
        
1375
        __make_api_call: function(url, method, data, success, error, action) {
1376
            var self = this;
1377
            error = error || function(){};
1378
            success = success || function(){};
1379

    
1380
            var params = {
1381
                url: url,
1382
                data: data,
1383
                success: function(){ self.handle_action_succeed.apply(self, arguments); success.apply(this, arguments)},
1384
                error: function(){ self.handle_action_fail.apply(self, arguments); error.apply(this, arguments)},
1385
                error_params: { ns: "Machines actions", 
1386
                                title: "'" + this.get("name") + "'" + " " + action + " failed", 
1387
                                extra_details: { 'Machine ID': this.id, 'URL': url, 'Action': action || "undefined" },
1388
                                allow_reload: false
1389
                              },
1390
                display: false,
1391
                critical: false
1392
            }
1393
            this.sync(method, this, params);
1394
        },
1395

    
1396
        handle_action_succeed: function() {
1397
            this.trigger("action:success", arguments);
1398
        },
1399
        
1400
        reset_action_error: function() {
1401
            this.action_error = false;
1402
            this.trigger("action:fail:reset", this.action_error);
1403
        },
1404

    
1405
        handle_action_fail: function() {
1406
            this.action_error = arguments;
1407
            this.trigger("action:fail", arguments);
1408
        },
1409

    
1410
        get_action_url: function(name) {
1411
            return this.url() + "/action";
1412
        },
1413

    
1414
        get_connection_info: function(host_os, success, error) {
1415
            var url = "/machines/connect";
1416
            params = {
1417
                ip_address: this.get_addresses().ip4,
1418
                os: this.get_os(),
1419
                host_os: host_os,
1420
                srv: this.id
1421
            }
1422

    
1423
            url = url + "?" + $.param(params);
1424

    
1425
            var ajax = snf.api.sync("read", undefined, { url: url, 
1426
                                                         error:error, 
1427
                                                         success:success, 
1428
                                                         handles_error:1});
1429
        }
1430
    })
1431
    
1432
    models.VM.ACTIONS = [
1433
        'start',
1434
        'shutdown',
1435
        'reboot',
1436
        'console',
1437
        'destroy'
1438
    ]
1439

    
1440
    models.VM.AVAILABLE_ACTIONS = {
1441
        'UNKNWON'       : ['destroy'],
1442
        'BUILD'         : ['destroy'],
1443
        'REBOOT'        : ['shutdown', 'destroy', 'console'],
1444
        'STOPPED'       : ['start', 'destroy'],
1445
        'ACTIVE'        : ['shutdown', 'destroy', 'reboot', 'console'],
1446
        'ERROR'         : ['destroy'],
1447
        'DELETED'        : [],
1448
        'DESTROY'       : [],
1449
        'BUILD_INIT'    : ['destroy'],
1450
        'BUILD_COPY'    : ['destroy'],
1451
        'BUILD_FINAL'   : ['destroy'],
1452
        'SHUTDOWN'      : ['destroy'],
1453
        'START'         : [],
1454
        'CONNECT'       : [],
1455
        'DISCONNECT'    : []
1456
    }
1457

    
1458
    // api status values
1459
    models.VM.STATUSES = [
1460
        'UNKNWON',
1461
        'BUILD',
1462
        'REBOOT',
1463
        'STOPPED',
1464
        'ACTIVE',
1465
        'ERROR',
1466
        'DELETED'
1467
    ]
1468

    
1469
    // api status values
1470
    models.VM.CONNECT_STATES = [
1471
        'ACTIVE',
1472
        'REBOOT',
1473
        'SHUTDOWN'
1474
    ]
1475

    
1476
    // vm states
1477
    models.VM.STATES = models.VM.STATUSES.concat([
1478
        'DESTROY',
1479
        'BUILD_INIT',
1480
        'BUILD_COPY',
1481
        'BUILD_FINAL',
1482
        'SHUTDOWN',
1483
        'START',
1484
        'CONNECT',
1485
        'DISCONNECT',
1486
        'FIREWALL'
1487
    ]);
1488
    
1489
    models.VM.STATES_TRANSITIONS = {
1490
        'DESTROY' : ['DELETED'],
1491
        'SHUTDOWN': ['ERROR', 'STOPPED', 'DESTROY'],
1492
        'STOPPED': ['ERROR', 'ACTIVE', 'DESTROY'],
1493
        'ACTIVE': ['ERROR', 'STOPPED', 'REBOOT', 'SHUTDOWN', 'DESTROY'],
1494
        'START': ['ERROR', 'ACTIVE', 'DESTROY'],
1495
        'REBOOT': ['ERROR', 'ACTIVE', 'STOPPED', 'DESTROY'],
1496
        'BUILD': ['ERROR', 'ACTIVE', 'DESTROY'],
1497
        'BUILD_COPY': ['ERROR', 'ACTIVE', 'BUILD_FINAL', 'DESTROY'],
1498
        'BUILD_FINAL': ['ERROR', 'ACTIVE', 'DESTROY'],
1499
        'BUILD_INIT': ['ERROR', 'ACTIVE', 'BUILD_COPY', 'BUILD_FINAL', 'DESTROY']
1500
    }
1501

    
1502
    models.VM.TRANSITION_STATES = [
1503
        'DESTROY',
1504
        'SHUTDOWN',
1505
        'START',
1506
        'REBOOT',
1507
        'BUILD'
1508
    ]
1509

    
1510
    models.VM.ACTIVE_STATES = [
1511
        'BUILD', 'REBOOT', 'ACTIVE',
1512
        'BUILD_INIT', 'BUILD_COPY', 'BUILD_FINAL',
1513
        'SHUTDOWN', 'CONNECT', 'DISCONNECT'
1514
    ]
1515

    
1516
    models.VM.BUILDING_STATES = [
1517
        'BUILD', 'BUILD_INIT', 'BUILD_COPY', 'BUILD_FINAL'
1518
    ]
1519

    
1520
    models.Networks = models.Collection.extend({
1521
        model: models.Network,
1522
        path: 'networks',
1523
        details: true,
1524
        //noUpdate: true,
1525
        defaults: {'linked_to':[]},
1526

    
1527
        parse: function (resp, xhr) {
1528
            // FIXME: depricated global var
1529
            if (!resp) { return []};
1530
               
1531
            var data = _.map(resp.networks.values, _.bind(this.parse_net_api_data, this));
1532
            return data;
1533
        },
1534

    
1535
        reset_pending_actions: function() {
1536
            this.each(function(net) {
1537
                net.get("actions").reset();
1538
            })
1539
        },
1540

    
1541
        do_all_pending_actions: function() {
1542
            this.each(function(net) {
1543
                net.do_all_pending_actions();
1544
            })
1545
        },
1546

    
1547
        parse_net_api_data: function(data) {
1548
            if (data.servers && data.servers.values) {
1549
                data['linked_to'] = data.servers.values;
1550
            }
1551
            return data;
1552
        },
1553

    
1554
        create: function (name, callback) {
1555
            return this.api_call(this.path, "create", {network:{name:name}}, callback);
1556
        }
1557
    })
1558

    
1559
    models.Images = models.Collection.extend({
1560
        model: models.Image,
1561
        path: 'images',
1562
        details: true,
1563
        noUpdate: true,
1564
        supportIncUpdates: false,
1565
        meta_keys_as_attrs: ["OS", "description", "kernel", "size", "GUI"],
1566
        read_method: 'read',
1567

    
1568
        // update collection model with id passed
1569
        // making a direct call to the image
1570
        // api url
1571
        update_unknown_id: function(id, callback) {
1572
            var url = getUrl.call(this) + "/" + id;
1573
            this.api_call(this.path + "/" + id, this.read_method, {_options:{async:true, skip_api_error:true}}, undefined, 
1574
            _.bind(function() {
1575
                if (!this.get(id)) {
1576
                            if (this.fallback_service) {
1577
                        // if current service has fallback_service attribute set
1578
                        // use this service to retrieve the missing image model
1579
                        var tmpservice = new this.fallback_service();
1580
                        tmpservice.update_unknown_id(id, _.bind(function(img){
1581
                            img.attributes.status = "DELETED";
1582
                            this.add(img.attributes);
1583
                            callback(this.get(id));
1584
                        }, this));
1585
                    } else {
1586
                        // else add a dummy DELETED state image entry
1587
                        this.add({id:id, name:"Unknown image", size:-1, 
1588
                                  progress:100, status:"DELETED"});
1589
                        callback(this.get(id));
1590
                    }   
1591
                } else {
1592
                    callback(this.get(id));
1593
                }
1594
            }, this), _.bind(function(image, msg, xhr) {
1595
                if (!image) {
1596
                    this.add({id:id, name:"Unknown image", size:-1, 
1597
                              progress:100, status:"DELETED"});
1598
                    callback(this.get(id));
1599
                    return;
1600
                }
1601
                var img_data = this._read_image_from_request(image, msg, xhr);
1602
                this.add(img_data);
1603
                callback(this.get(id));
1604
            }, this));
1605
        },
1606

    
1607
        _read_image_from_request: function(image, msg, xhr) {
1608
            return image.image;
1609
        },
1610

    
1611
        parse: function (resp, xhr) {
1612
            // FIXME: depricated global var
1613
            var data = _.map(resp.images.values, _.bind(this.parse_meta, this));
1614
            return resp.images.values;
1615
        },
1616

    
1617
        get_meta_key: function(img, key) {
1618
            if (img.metadata && img.metadata.values && img.metadata.values[key]) {
1619
                return _.escape(img.metadata.values[key]);
1620
            }
1621
            return undefined;
1622
        },
1623

    
1624
        comparator: function(img) {
1625
            return -img.get_sort_order("sortorder") || 1000 * img.id;
1626
        },
1627

    
1628
        parse_meta: function(img) {
1629
            _.each(this.meta_keys_as_attrs, _.bind(function(key){
1630
                if (img[key]) { return };
1631
                img[key] = this.get_meta_key(img, key) || "";
1632
            }, this));
1633
            return img;
1634
        },
1635

    
1636
        active: function() {
1637
            return this.filter(function(img){return img.get('status') != "DELETED"});
1638
        },
1639

    
1640
        predefined: function() {
1641
            return _.filter(this.active(), function(i) { return !i.get("serverRef")});
1642
        },
1643
        
1644
        fetch_for_type: function(type, complete, error) {
1645
            this.fetch({update:true, 
1646
                        success: complete, 
1647
                        error: error, 
1648
                        skip_api_error: true });
1649
        },
1650
        
1651
        get_images_for_type: function(type) {
1652
            if (this['get_{0}_images'.format(type)]) {
1653
                return this['get_{0}_images'.format(type)]();
1654
            }
1655

    
1656
            return this.active();
1657
        },
1658

    
1659
        update_images_for_type: function(type, onStart, onComplete, onError, force_load) {
1660
            var load = false;
1661
            error = onError || function() {};
1662
            function complete(collection) { 
1663
                onComplete(collection.get_images_for_type(type)); 
1664
            }
1665
            
1666
            // do we need to fetch/update current collection entries
1667
            if (load) {
1668
                onStart();
1669
                this.fetch_for_type(type, complete, error);
1670
            } else {
1671
                // fallback to complete
1672
                complete(this);
1673
            }
1674
        }
1675
    })
1676

    
1677
    models.Flavors = models.Collection.extend({
1678
        model: models.Flavor,
1679
        path: 'flavors',
1680
        details: true,
1681
        noUpdate: true,
1682
        supportIncUpdates: false,
1683
        // update collection model with id passed
1684
        // making a direct call to the flavor
1685
        // api url
1686
        update_unknown_id: function(id, callback) {
1687
            var url = getUrl.call(this) + "/" + id;
1688
            this.api_call(this.path + "/" + id, "read", {_options:{async:false, skip_api_error:true}}, undefined, 
1689
            _.bind(function() {
1690
                this.add({id:id, cpu:"", ram:"", disk:"", name: "", status:"DELETED"})
1691
            }, this), _.bind(function(flv) {
1692
                if (!flv.flavor.status) { flv.flavor.status = "DELETED" };
1693
                this.add(flv.flavor);
1694
            }, this));
1695
        },
1696

    
1697
        parse: function (resp, xhr) {
1698
            // FIXME: depricated global var
1699
            return _.map(resp.flavors.values, function(o) { o.disk_template = o['SNF:disk_template']; return o});
1700
        },
1701

    
1702
        comparator: function(flv) {
1703
            return flv.get("disk") * flv.get("cpu") * flv.get("ram");
1704
        },
1705

    
1706
        unavailable_values_for_image: function(img, flavors) {
1707
            var flavors = flavors || this.active();
1708
            var size = img.get_size();
1709
            
1710
            var index = {cpu:[], disk:[], ram:[]};
1711

    
1712
            _.each(this.active(), function(el) {
1713
                var img_size = size;
1714
                var flv_size = el.get_disk_size();
1715
                if (flv_size < img_size) {
1716
                    if (index.disk.indexOf(flv_size) == -1) {
1717
                        index.disk.push(flv_size);
1718
                    }
1719
                };
1720
            });
1721
            
1722
            return index;
1723
        },
1724

    
1725
        get_flavor: function(cpu, mem, disk, disk_template, filter_list) {
1726
            if (!filter_list) { filter_list = this.models };
1727
            
1728
            return this.select(function(flv){
1729
                if (flv.get("cpu") == cpu + "" &&
1730
                   flv.get("ram") == mem + "" &&
1731
                   flv.get("disk") == disk + "" &&
1732
                   flv.get("disk_template") == disk_template &&
1733
                   filter_list.indexOf(flv) > -1) { return true; }
1734
            })[0];
1735
        },
1736
        
1737
        get_data: function(lst) {
1738
            var data = {'cpu': [], 'mem':[], 'disk':[]};
1739

    
1740
            _.each(lst, function(flv) {
1741
                if (data.cpu.indexOf(flv.get("cpu")) == -1) {
1742
                    data.cpu.push(flv.get("cpu"));
1743
                }
1744
                if (data.mem.indexOf(flv.get("ram")) == -1) {
1745
                    data.mem.push(flv.get("ram"));
1746
                }
1747
                if (data.disk.indexOf(flv.get("disk")) == -1) {
1748
                    data.disk.push(flv.get("disk"));
1749
                }
1750
            })
1751
            
1752
            return data;
1753
        },
1754

    
1755
        active: function() {
1756
            return this.filter(function(flv){return flv.get('status') != "DELETED"});
1757
        }
1758
            
1759
    })
1760

    
1761
    models.VMS = models.Collection.extend({
1762
        model: models.VM,
1763
        path: 'servers',
1764
        details: true,
1765
        copy_image_meta: true,
1766
        
1767
        parse: function (resp, xhr) {
1768
            // FIXME: depricated after refactoring
1769
            var data = resp;
1770
            if (!resp) { return [] };
1771
            data = _.filter(_.map(resp.servers.values, _.bind(this.parse_vm_api_data, this)), function(v){return v});
1772
            return data;
1773
        },
1774
        
1775
        get_reboot_required: function() {
1776
            return this.filter(function(vm){return vm.get("reboot_required") == true})
1777
        },
1778

    
1779
        has_pending_actions: function() {
1780
            return this.filter(function(vm){return vm.pending_action}).length > 0;
1781
        },
1782

    
1783
        reset_pending_actions: function() {
1784
            this.each(function(vm) {
1785
                vm.clear_pending_action();
1786
            })
1787
        },
1788

    
1789
        do_all_pending_actions: function(success, error) {
1790
            this.each(function(vm) {
1791
                if (vm.has_pending_action()) {
1792
                    vm.call(vm.pending_action, success, error);
1793
                    vm.clear_pending_action();
1794
                }
1795
            })
1796
        },
1797
        
1798
        do_all_reboots: function(success, error) {
1799
            this.each(function(vm) {
1800
                if (vm.get("reboot_required")) {
1801
                    vm.call("reboot", success, error);
1802
                }
1803
            });
1804
        },
1805

    
1806
        reset_reboot_required: function() {
1807
            this.each(function(vm) {
1808
                vm.set({'reboot_required': undefined});
1809
            })
1810
        },
1811
        
1812
        stop_stats_update: function(exclude) {
1813
            var exclude = exclude || [];
1814
            this.each(function(vm) {
1815
                if (exclude.indexOf(vm) > -1) {
1816
                    return;
1817
                }
1818
                vm.stop_stats_update();
1819
            })
1820
        },
1821
        
1822
        has_meta: function(vm_data) {
1823
            return vm_data.metadata && vm_data.metadata.values
1824
        },
1825

    
1826
        has_addresses: function(vm_data) {
1827
            return vm_data.metadata && vm_data.metadata.values
1828
        },
1829

    
1830
        parse_vm_api_data: function(data) {
1831
            // do not add non existing DELETED entries
1832
            if (data.status && data.status == "DELETED") {
1833
                if (!this.get(data.id)) {
1834
                    return false;
1835
                }
1836
            }
1837

    
1838
            // OS attribute
1839
            if (this.has_meta(data)) {
1840
                data['OS'] = data.metadata.values.OS || "okeanos";
1841
            }
1842
            
1843
            data['firewalls'] = {};
1844
            if (data['addresses'] && data['addresses'].values) {
1845
                data['linked_to_nets'] = data['addresses'].values;
1846
                _.each(data['addresses'].values, function(f){
1847
                    if (f['firewallProfile']) {
1848
                        data['firewalls'][f['id']] = f['firewallProfile']
1849
                    }
1850
                });
1851
            }
1852
            
1853
            // if vm has no metadata, no metadata object
1854
            // is in json response, reset it to force
1855
            // value update
1856
            if (!data['metadata']) {
1857
                data['metadata'] = {values:{}};
1858
            }
1859

    
1860
            return data;
1861
        },
1862

    
1863
        create: function (name, image, flavor, meta, extra, callback) {
1864
            if (this.copy_image_meta) {
1865
                if (image.get("OS")) {
1866
                    meta['OS'] = image.get("OS");
1867
                }
1868
           }
1869
            
1870
            opts = {name: name, imageRef: image.id, flavorRef: flavor.id, metadata:meta}
1871
            opts = _.extend(opts, extra);
1872

    
1873
            this.api_call(this.path, "create", {'server': opts}, undefined, undefined, callback, {critical: true});
1874
        }
1875

    
1876
    })
1877

    
1878
    models.PublicKey = models.Model.extend({
1879
        path: 'keys',
1880
        base_url: '/ui/userdata',
1881
        details: false,
1882
        noUpdate: true,
1883

    
1884

    
1885
        get_public_key: function() {
1886
            return cryptico.publicKeyFromString(this.get("content"));
1887
        },
1888

    
1889
        get_filename: function() {
1890
            return "{0}.pub".format(this.get("name"));
1891
        },
1892

    
1893
        identify_type: function() {
1894
            try {
1895
                var cont = snf.util.validatePublicKey(this.get("content"));
1896
                var type = cont.split(" ")[0];
1897
                return synnefo.util.publicKeyTypesMap[type];
1898
            } catch (err) { return false };
1899
        }
1900

    
1901
    })
1902
    
1903
    models.PublicKeys = models.Collection.extend({
1904
        model: models.PublicKey,
1905
        details: false,
1906
        path: 'keys',
1907
        base_url: '/ui/userdata',
1908
        noUpdate: true,
1909

    
1910
        generate_new: function(success, error) {
1911
            snf.api.sync('create', undefined, {
1912
                url: getUrl.call(this, this.base_url) + "/generate", 
1913
                success: success, 
1914
                error: error,
1915
                skip_api_error: true
1916
            });
1917
        },
1918

    
1919
        add_crypto_key: function(key, success, error, options) {
1920
            var options = options || {};
1921
            var m = new models.PublicKey();
1922

    
1923
            // guess a name
1924
            var name_tpl = "public key";
1925
            var name = name_tpl;
1926
            var name_count = 1;
1927
            
1928
            while(this.filter(function(m){ return m.get("name") == name }).length > 0) {
1929
                name = name_tpl + " " + name_count;
1930
                name_count++;
1931
            }
1932
            
1933
            m.set({name: name});
1934
            m.set({content: key});
1935
            
1936
            options.success = function () { return success(m) };
1937
            options.errror = error;
1938
            options.skip_api_error = true;
1939
            
1940
            this.create(m.attributes, options);
1941
        }
1942
    })
1943
    
1944
    // storage initialization
1945
    snf.storage.images = new models.Images();
1946
    snf.storage.flavors = new models.Flavors();
1947
    snf.storage.networks = new models.Networks();
1948
    snf.storage.vms = new models.VMS();
1949
    snf.storage.keys = new models.PublicKeys();
1950

    
1951
    //snf.storage.vms.fetch({update:true});
1952
    //snf.storage.images.fetch({update:true});
1953
    //snf.storage.flavors.fetch({update:true});
1954

    
1955
})(this);