Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (68 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
                skips_timeouts: true
951
            });
952
        },
953

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1314
            var self = this;
1315

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1861
            return data;
1862
        },
1863

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

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

    
1877
    })
1878

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

    
1885

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

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

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

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

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

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

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

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

    
1956
})(this);