This file is indexed.

/usr/share/maas/web/static/js/angular/directives/boot_images.js is in maas-region-api 2.4.0~beta2-6865-gec43e47e6-0ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
/* Copyright 2016 Canonical Ltd.  This software is licensed under the
 * GNU Affero General Public License version 3 (see the file LICENSE).
 *
 * Boot images directive.
*/

angular.module('MAAS').directive('maasBootImagesStatus', [
    'BootResourcesManager',
    function(BootResourcesManager) {
        return {
            restrict: "E",
            scope: {},
            template: [
                '<p class="page-header__status" ',
                    'data-ng-if="data.region_import_running">',
                    '<span class="u-text--loading">',
                        '<i class="p-icon--loading u-animation--spin"></i>',
                        'Step 1/2: Region controller importing',
                    '</span>',
                '</p>',
                '<p class="page-header__status" ',
                    'data-ng-if="!data.region_import_running && ',
                    'data.rack_import_running">',
                    '<span class="u-text--loading">',
                        '<i class="p-icon--loading u-animation--spin"></i>',
                        'Step 2/2: Rack controller(s) importing',
                    '</span>',
                '</p>'
            ].join(''),
            controller: function($scope, $rootScope, $element, $document) {
                // This controller doesn't start the polling. The
                // `maasBootImages` controller should be used on the page to
                // start the polling. This just presents a nice status spinner
                // when the polling is enabled.
                $scope.data = BootResourcesManager.getData();
            }
        };
    }]);

angular.module('MAAS').directive('maasBootImages', [
    '$filter', 'BootResourcesManager', 'UsersManager', 'ManagerHelperService',
    function($filter, BootResourcesManager,
        UsersManager, ManagerHelperService) {
        return {
            restrict: "E",
            scope: {
                design: "=?"
            },
            templateUrl: (
                'static/partials/boot-images.html?v=' + (
                    MAAS_config.files_version)),
            controller: function($scope, $rootScope, $element, $document) {
                $scope.loading = true;
                $scope.saving = false;
                $scope.stopping = false;
                $scope.design = $scope.design || 'page';
                $scope.bootResources = BootResourcesManager.getData();
                $scope.ubuntuImages = [];
                $scope.source = {
                    isNew: false,
                    tooMany: false,
                    showingAdvanced: false,
                    connecting: false,
                    errorMessage: "",
                    source_type: 'maas.io',
                    url: '',
                    keyring_filename: '',
                    keyring_data: '',
                    releases: [],
                    arches: [],
                    selections: {
                        changed: false,
                        releases: [],
                        arches: []
                    }
                };
                $scope.ubuntuCoreImages = [];
                $scope.ubuntu_core = {
                    changed: false,
                    images: []
                };
                $scope.otherImages = [];
                $scope.other = {
                    changed: false,
                    images: []
                };
                $scope.generatedImages = [];
                $scope.customImages = [];
                $scope.ubuntuDeleteId = null;
                $scope.removingImage = null;

                // Return true if the authenticated user is super user.
                $scope.isSuperUser = function() {
                    return UsersManager.isSuperUser();
                };

                // Return the overall title icon.
                $scope.getTitleIcon = function() {
                    if($scope.bootResources.resources.length === 0) {
                        return 'p-icon--success-muted';
                    } else {
                        return 'p-icon--success';
                    }
                };

                // Return true if the mirror path section should be shown.
                $scope.showMirrorPath = function() {
                    if($scope.source.source_type === 'custom') {
                        return true;
                    } else {
                        return false;
                    }
                };

                // Return true if the advanced options are shown.
                $scope.isShowingAdvancedOptions = function() {
                    return $scope.source.showingAdvanced;
                };

                // Toggle showing the advanced options.
                $scope.toggleAdvancedOptions = function() {
                    $scope.source.showingAdvanced = (
                        !$scope.source.showingAdvanced);
                };

                // Return true if both keyring options are set.
                $scope.bothKeyringOptionsSet = function() {
                    return (
                        $scope.source.keyring_filename.length > 0 &&
                        $scope.source.keyring_data.length > 0);
                };

                // Return true when the connect button for the mirror path
                // should be shown.
                $scope.showConnectButton = function() {
                    return $scope.source.isNew;
                };

                // Called when the source radio chanaged.
                $scope.sourceChanged = function() {
                    var currentSources = $scope.bootResources.ubuntu.sources;
                    if(currentSources.length === 0) {
                        $scope.source.isNew = true;
                    } else {
                        var prevNew = $scope.source.isNew;
                        $scope.source.isNew = (
                            $scope.source.source_type !==
                            currentSources[0].source_type);
                        if($scope.source.source_type === 'custom') {
                            $scope.source.isNew = $scope.source.isNew || (
                                $scope.source.url !==
                                currentSources[0].url
                            );
                        }
                        if(prevNew && !$scope.source.isNew) {
                            // No longer a new source set url and keyring to
                            // original.
                            $scope.source.url = currentSources[0].url;
                            $scope.source.keyring_filename = (
                                currentSources[0].keyring_filename);
                            $scope.source.keyring_data = (
                                currentSources[0].keyring_data);
                        }
                        $scope.source.releases = [];
                        $scope.source.arches = [];
                        $scope.source.selections = {
                            changed: false,
                            releases: [],
                            arches: []
                        };
                    }
                    $scope.updateSource();
                    $scope.regenerateUbuntuImages();

                    // When the source is new and its maas.io automatically
                    // fetch the source information.
                    if($scope.source.isNew &&
                        $scope.source.source_type === 'maas.io') {
                        $scope.connect();
                    }
                };

                // Return true when the connect button should be disabled.
                $scope.isConnectButtonDisabled = function() {
                    if($scope.source.source_type === 'maas.io') {
                        return false;
                    } else {
                        return ($scope.bothKeyringOptionsSet() ||
                            $scope.source.url.length === 0 ||
                            $scope.source.connecting);
                    }
                };

                // Return the source parameters.
                $scope.getSourceParams = function() {
                    if($scope.source.source_type === 'maas.io') {
                        return {
                            source_type: 'maas.io'
                        };
                    } else {
                        return {
                            source_type: $scope.source.source_type,
                            url: $scope.source.url,
                            keyring_filename: $scope.source.keyring_filename,
                            keyring_data: $scope.source.keyring_data
                        };
                    }
                };

                // Select the default images that should be selected. Current
                // defaults are '16.04 LTS' and 'amd64'.
                $scope.selectDefaults = function() {
                    angular.forEach($scope.source.releases, function(release) {
                        if(release.name === "xenial") {
                            $scope.source.selections.releases.push(release);
                        }
                    });
                    angular.forEach($scope.source.arches, function(arch) {
                        if(arch.name === "amd64") {
                            $scope.source.selections.arches.push(arch);
                        }
                    });
                };

                // Connected to the simplestreams endpoint. This only gets the
                // information from the endpoint it does not save it into the
                // database.
                $scope.connect = function() {
                    if($scope.isConnectButtonDisabled()) {
                        return;
                    }

                    var source = $scope.getSourceParams();
                    $scope.source.connecting = true;
                    $scope.source.releases = [];
                    $scope.source.arches = [];
                    $scope.source.selections.changed = true;
                    $scope.source.selections.releases = [];
                    $scope.source.selections.arches = [];
                    $scope.regenerateUbuntuImages();
                    BootResourcesManager.fetch(source).then(function(data) {
                        $scope.source.connecting = false;
                        data = angular.fromJson(data);
                        $scope.source.releases = data.releases;
                        $scope.source.arches = data.arches;
                        $scope.selectDefaults();
                        $scope.regenerateUbuntuImages();
                    }, function(error) {
                        $scope.source.connecting = false;
                        $scope.source.errorMessage = error;
                    });
                };

                // Return true if the connect block should be shown.
                $scope.showConnectBlock = function() {
                    return $scope.source.tooMany || (
                        $scope.source.isNew && !$scope.showSelections());
                };

                // Return true if the release and architecture selection
                // should be shown.
                $scope.showSelections = function() {
                    return (
                        $scope.source.releases.length > 0 &&
                        $scope.source.arches.length > 0);
                };

                // Return the Ubuntu LTS releases.
                $scope.getUbuntuLTSReleases = function() {
                    var releases = $scope.bootResources.ubuntu.releases;
                    if($scope.source.isNew) {
                        releases = $scope.source.releases;
                    }
                    var filtered = [];
                    angular.forEach(releases, function(release) {
                        if(!release.deleted &&
                            release.title.indexOf('LTS') !== -1) {
                            filtered.push(release);
                        }
                    });
                    return filtered;
                };

                // Return the Ubuntu non-LTS releases.
                $scope.getUbuntuNonLTSReleases = function() {
                    var releases = $scope.bootResources.ubuntu.releases;
                    if($scope.source.isNew) {
                        releases = $scope.source.releases;
                    }
                    var filtered = [];
                    angular.forEach(releases, function(release) {
                        if(!release.deleted &&
                            release.title.indexOf('LTS') === -1) {
                            filtered.push(release);
                        }
                    });
                    return filtered;
                };

                // Return the available architectures.
                $scope.getArchitectures = function() {
                    var arches = $scope.bootResources.ubuntu.arches;
                    if($scope.source.isNew) {
                        arches = $scope.source.arches;
                    }
                    var filtered = [];
                    angular.forEach(arches, function(arch) {
                        if(!arch.deleted) {
                            filtered.push(arch);
                        }
                    });
                    return filtered;
                };

                // Return true if the source has this selected.
                $scope.isSelected = function(type, obj) {
                    return $scope.source.selections[type].indexOf(obj) >= 0;
                };

                // Toggle the selection of the release or the architecture.
                $scope.toggleSelection = function(type, obj) {
                    var idx = $scope.source.selections[type].indexOf(obj);
                    if(idx === -1) {
                        $scope.source.selections[type].push(obj);
                    } else {
                        $scope.source.selections[type].splice(idx, 1);
                    }
                    $scope.source.selections.changed = true;
                    $scope.regenerateUbuntuImages();
                };

                // Return true if the images table should be shown.
                $scope.showImagesTable = function() {
                    if($scope.ubuntuImages.length > 0) {
                        return true;
                    } else {
                        // Show the images table source has
                        // releases and architectures.
                        return (
                            $scope.source.arches.length > 0 &&
                            $scope.source.releases.length > 0);
                    }
                };

                // Regenerates the Ubuntu images list for the directive.
                $scope.regenerateUbuntuImages = function() {
                    var getResource = function() { return null; };
                    var resources = $scope.bootResources.resources.filter(
                        function(resource) {
                            var name_split = resource.name.split('/');
                            var resource_os = name_split[0];
                            return (
                                resource.rtype === 0 &&
                                resource_os === 'ubuntu');
                        });
                    if(!$scope.source.isNew) {
                        getResource = function(release, arch) {
                            var i;
                            for(i = 0; i < resources.length; i++) {
                                // Only care about Ubuntu images.
                                var resource = (resources[i]);
                                var name_split = resource.name.split('/');
                                var resource_release = name_split[1];
                                if(resource_release === release &&
                                    resource.arch === arch) {
                                    resources.splice(i, 1);
                                    return resource;
                                }
                            }
                            return null;
                        };
                    }

                    // Create the images based on the selections.
                    $scope.ubuntuImages.length = 0;
                    angular.forEach($scope.source.selections.releases,
                        function(release) {
                            angular.forEach($scope.source.selections.arches,
                                function(arch) {
                                    var image = {
                                        icon: 'p-icon--status-queued',
                                        title: release.title,
                                        arch: arch.title,
                                        size: '-',
                                        status: 'Selected for download',
                                        beingDeleted: false,
                                        name: release.name
                                    };
                                    var resource = getResource(
                                        release.name, arch.name);
                                    if(angular.isObject(resource)) {
                                        image.resourceId = resource.id;
                                        image.icon = (
                                            'p-icon--status-' + resource.icon);
                                        image.size = resource.size;
                                        image.status = resource.status;
                                        if(resource.downloading) {
                                            image.icon += ' u-animation--pulse';
                                        }
                                    }
                                    $scope.ubuntuImages.push(image);
                                });
                        });

                    // If not a new source and images remain in resources, then
                    // those are set to be deleted.
                    if(!$scope.source.isNew) {
                        angular.forEach(resources, function(resource) {
                            var name_split = resource.name.split('/');
                            var image = {
                                icon: 'p-icon--status-failed',
                                title: resource.title,
                                arch: resource.arch,
                                size: resource.size,
                                status: 'Will be deleted',
                                beingDeleted: true,
                                name: name_split[1]
                            };
                            $scope.ubuntuImages.push(image);
                        });
                    }
                };

                // Regenerates the Ubuntu Core images list for the directive.
                $scope.regenerateUbuntuCoreImages = function() {
                    var isUbuntuCore = function(resource) {
                        var name_split = resource.name.split('/');
                        var resource_os = name_split[0];
                        return (
                            resource.rtype === 0 &&
                            resource_os === 'ubuntu-core');
                    };
                    var resources = (
                        $scope.bootResources.resources.filter(isUbuntuCore));
                    var getResource = function(release, arch) {
                        var i;
                        for(i = 0; i < resources.length; i++) {
                            // Only care about other images. Removing custom,
                            // bootloaders, and Ubuntu images.
                            var resource = (resources[i]);
                            var name_split = resource.name.split('/');
                            var resource_release = name_split[1];
                            if(resource_release === release &&
                                resource.arch === arch) {
                                resources.splice(i, 1);
                                return resource;
                            }
                        }
                        return null;
                    };

                    // Create the images based on the selections.
                    $scope.ubuntuCoreImages.length = 0;
                    angular.forEach($scope.ubuntu_core.images,
                        function(ubuntuCoreImage) {
                            if(ubuntuCoreImage.checked) {
                                var name_split = ubuntuCoreImage.name.split(
                                    '/');
                                var image = {
                                    icon: 'p-icon--status-queued',
                                    title: ubuntuCoreImage.title,
                                    arch: name_split[1],
                                    size: '-',
                                    status: 'Selected for download',
                                    beingDeleted: false
                                };
                                var resource = getResource(
                                    name_split[3], name_split[1]);
                                if(angular.isObject(resource)) {
                                    image.icon = (
                                        'p-icon--status-' + resource.icon);
                                    image.size = resource.size;
                                    image.status = resource.status;
                                    if(resource.downloading) {
                                        image.icon += ' u-animation--pulse';
                                    }
                                }
                                $scope.ubuntuCoreImages.push(image);
                            }
                        });

                    // If not a new source and images remain in resources, then
                    // those are set to be deleted.
                    angular.forEach(resources, function(resource) {
                        var image = {
                            icon: 'p-icon--status-failed',
                            title: resource.title,
                            arch: resource.arch,
                            size: resource.size,
                            status: 'Will be deleted',
                            beingDeleted: true
                        };
                        $scope.ubuntuCoreImages.push(image);
                    });
                };

                // Regenerates the other images list for the directive.
                $scope.regenerateOtherImages = function() {
                    var isOther = function(resource) {
                        var name_split = resource.name.split('/');
                        var resource_os = name_split[0];
                        return (
                            resource.rtype === 0 &&
                            resource_os !== 'ubuntu' &&
                            resource_os !== 'ubuntu-core' &&
                            resource_os !== 'custom');
                    };
                    var resources = (
                        $scope.bootResources.resources.filter(isOther));
                    var getResource = function(release, arch) {
                        var i;
                        for(i = 0; i < resources.length; i++) {
                            // Only care about other images. Removing custom,
                            // bootloaders, and Ubuntu images.
                            var resource = (resources[i]);
                            var name_split = resource.name.split('/');
                            var resource_release = name_split[1];
                            if(resource_release === release &&
                                resource.arch === arch) {
                                resources.splice(i, 1);
                                return resource;
                            }
                        }
                        return null;
                    };

                    // Create the images based on the selections.
                    $scope.otherImages.length = 0;
                    angular.forEach($scope.other.images,
                        function(otherImage) {
                            if(otherImage.checked) {
                                var name_split = otherImage.name.split('/');
                                var image = {
                                    icon: 'p-icon--status-queued',
                                    title: otherImage.title,
                                    arch: name_split[1],
                                    size: '-',
                                    status: 'Selected for download',
                                    beingDeleted: false
                                };
                                var resource = getResource(
                                    name_split[3], name_split[1]);
                                if(angular.isObject(resource)) {
                                    image.icon = (
                                        'p-icon--status-' + resource.icon);
                                    image.size = resource.size;
                                    image.status = resource.status;
                                    if(resource.downloading) {
                                        image.icon += ' u-animation--pulse';
                                    }
                                }
                                $scope.otherImages.push(image);
                            }
                        });

                    // If not a new source and images remain in resources, then
                    // those are set to be deleted.
                    angular.forEach(resources, function(resource) {
                        var image = {
                            icon: 'p-icon--status-failed',
                            title: resource.title,
                            arch: resource.arch,
                            size: resource.size,
                            status: 'Will be deleted',
                            beingDeleted: true
                        };
                        $scope.otherImages.push(image);
                    });
                };

                // Helper for basic image generation.
                $scope._regenerateImages = function(rtype, images) {
                    // Create the generated images list.
                    images.length = 0;
                    angular.forEach($scope.bootResources.resources,
                        function(resource) {
                            if(resource.rtype === rtype) {
                                var image = {
                                    icon: 'p-icon--status-' + resource.icon,
                                    title: resource.title,
                                    arch: resource.arch,
                                    image_id: resource.id,
                                    size: resource.size,
                                    status: resource.status
                                };
                                if(resource.downloading) {
                                    image.icon += ' u-animation--pulse';
                                }
                                images.push(image);
                            }
                        });
                };

                // Regenerates the generated images list for the directive.
                $scope.regenerateGeneratedImages = function() {
                    $scope._regenerateImages(1, $scope.generatedImages);
                };

                // Regenerates the custom images list for the directive.
                $scope.regenerateCustomImages = function() {
                    $scope._regenerateImages(2, $scope.customImages);
                };

                // Returns true if at least one LTS is selected.
                $scope.ltsIsSelected = function() {
                    var i;
                    for(i = 0; i < $scope.ubuntuImages.length; i++) {
                        // Must have LTS in the title and not being deleted.
                        if(!$scope.ubuntuImages[i].beingDeleted &&
                            $scope.ubuntuImages[i].title.indexOf('LTS') >= 0) {
                            // Must be greater than Ubuntu series 14.
                            var series = parseInt(
                                $scope.ubuntuImages[i].title.split('.')[0], 10);
                            if(series >= 14) {
                                return true;
                            }
                        }
                    }
                    return false;
                };

                // Returns true if the commission series is selected
                $scope.commissioningSeriesSelected = function() {
                    var i;
                    for(i = 0; i < $scope.ubuntuImages.length; i++) {
                        if(!$scope.ubuntuImages[i].beingDeleted &&
                           $scope.ubuntuImages[i].name ===
                           $scope.bootResources.ubuntu.commissioning_series) {
                            return true;
                        }
                    }
                    return false;
                };

                // Return if we are asking about deleting this image.
                $scope.isShowingDeleteConfirm = function(image) {
                    return image === $scope.removingImage;
                };

                // Mark the image for deletion.
                $scope.quickRemove = function(image) {
                    $scope.removingImage = image;
                };

                // Cancel image deletion.
                $scope.cancelRemove = function() {
                    $scope.removingImage = null;
                };

                // Mark the image for deletion.
                $scope.confirmRemove = function(image) {
                    if(image === $scope.removingImage) {
                        BootResourcesManager.deleteImage(
                            {id: image.image_id});
                    }
                    $scope.cancelRemove();
                };

                // Return true if the stop import button should be shown.
                $scope.showStopImportButton = function() {
                    return $scope.bootResources.region_import_running;
                };

                // Return true if should show save selection button, this
                // doesn't mean it can actually be clicked.
                $scope.showSaveSelection = function() {
                    return $scope.showImagesTable();
                };

                // Return true if can save the current selection.
                $scope.canSaveSelection = function() {
                    var commissioning_series_being_deleted = false;
                    var commissioning_series_arches = 0;
                    var i;
                    for(i = 0; i < $scope.ubuntuImages.length; i++) {
                        if($scope.ubuntuImages[i].name ===
                           $scope.bootResources.ubuntu.commissioning_series) {
                            commissioning_series_arches++;
                        }
                    }
                    // Only prevent the current commissioning series from
                    // being deleted if it isn't the commissioning series isn't
                    // available on another architecture.. If the current
                    // commissioning series isn't currently selected another
                    // LTS may be choosen, downloaded, and configured as the
                    // commissioning series.
                    for(i = 0; i < $scope.ubuntuImages.length; i++) {
                        if($scope.ubuntuImages[i].beingDeleted &&
                           $scope.ubuntuImages[i].name ===
                           $scope.bootResources.ubuntu.commissioning_series &&
                           commissioning_series_arches === 1) {
                            commissioning_series_being_deleted = true;
                            break;
                        }
                    }
                    return (
                        !commissioning_series_being_deleted &&
                        !$scope.saving &&
                        !$scope.stopping &&
                        $scope.ltsIsSelected());
                };

                // Return the text for the save selection button.
                $scope.getSaveSelectionText = function() {
                    if($scope.saving) {
                        return "Saving...";
                    } else {
                        return "Update selection";
                    }
                };

                // Return true if can stop current import.
                $scope.canStopImport = function() {
                    return !$scope.saving && !$scope.stopping;
                };

                // Return the text for the stop import button.
                $scope.getStopImportText = function() {
                    if($scope.stopping) {
                        return "Stopping...";
                    } else {
                        return "Stop import";
                    }
                };

                // Called to stop the import.
                $scope.stopImport = function() {
                    if(!$scope.canStopImport()) {
                        return;
                    }

                    $scope.stopping = true;
                    BootResourcesManager.stopImport().then(function() {
                        $scope.stopping = false;
                    });
                };

                // Save the selections into boot selections.
                $scope.saveSelection = function() {
                    if(!$scope.canSaveSelection()) {
                        return;
                    }

                    var params = $scope.getSourceParams();
                    params.releases = (
                        $scope.source.selections.releases.map(function(obj) {
                            return obj.name;
                        }));
                    params.arches = (
                        $scope.source.selections.arches.map(function(obj) {
                            return obj.name;
                        }));
                    $scope.saving = true;
                    BootResourcesManager.saveUbuntu(params).then(function() {
                        $scope.saving = false;
                        $scope.source.isNew = false;
                        $scope.source.selections.changed = false;
                        $scope.updateSource();
                    });
                };

                // Re-create an array with the new objects using the old
                // selected array.
                $scope.getNewSelections = function(newObjs, oldSelections) {
                    var newSelections = [];
                    angular.forEach(newObjs, function(obj) {
                        angular.forEach(oldSelections, function(selection) {
                            if(obj.name === selection.name) {
                                newSelections.push(obj);
                            }
                        });
                    });
                    return newSelections;
                };

                // Update the source information.
                $scope.updateSource = function() {
                    // Do not update if the source is new.
                    if($scope.source.isNew) {
                        return;
                    }

                    var source_len = $scope.bootResources.ubuntu.sources.length;
                    if(source_len === 0) {
                        $scope.source.isNew = true;
                        $scope.source.source_type = 'custom';
                        $scope.source.errorMessage = (
                            "Currently no source exists.");
                    } else if(source_len === 1) {
                        var source = $scope.bootResources.ubuntu.sources[0];
                        $scope.source.source_type = source.source_type;
                        if(source.source_type === "maas.io") {
                            $scope.source.url = "";
                            $scope.source.keyring_filename = "";
                            $scope.source.keyring_data = "";
                        } else {
                            $scope.source.url = source.url;
                            $scope.source.keyring_filename = (
                                source.keyring_filename);
                            $scope.source.keyring_data = source.keyring_data;
                        }
                        $scope.source.releases = (
                            $scope.bootResources.ubuntu.releases);
                        $scope.source.arches = (
                            $scope.bootResources.ubuntu.arches);
                        if(!$scope.source.selections.changed) {
                            // User didn't make a change update to the
                            // current selections server side.
                            $scope.source.selections.releases = (
                                $scope.source.releases.filter(function(obj) {
                                    return obj.checked;
                                }));
                            $scope.source.selections.arches = (
                                $scope.source.arches.filter(function(obj) {
                                return obj.checked;
                            }));
                        } else {
                            // Update the objects to be the new objects, with
                            // the same selections.
                            $scope.source.selections.releases = (
                                $scope.getNewSelections(
                                    $scope.source.releases,
                                    $scope.source.selections.releases));
                            $scope.source.selections.arches = (
                                $scope.getNewSelections(
                                    $scope.source.arches,
                                    $scope.source.selections.arches));
                        }
                        $scope.regenerateUbuntuImages();
                    } else {
                        // Having more than one source prevents modification
                        // of the sources.
                        $scope.source.tooMany = true;
                        $scope.source.releases = (
                            $scope.bootResources.ubuntu.releases);
                        $scope.source.arches = (
                            $scope.bootResources.ubuntu.arches);
                        $scope.source.selections.releases = (
                            $scope.source.releases.filter(function(obj) {
                                return obj.checked;
                            }));
                        $scope.source.selections.arches = (
                            $scope.source.arches.filter(function(obj) {
                            return obj.checked;
                        }));
                        $scope.source.errorMessage = (
                            "More than one image source exists. UI does not " +
                            "support modification of sources when more than " +
                            "one has been defined. Used the API to adjust " +
                            "your sources.");
                        $scope.regenerateUbuntuImages();
                    }
                };

                // Toggle the selection of Ubuntu Core images.
                $scope.toggleUbuntuCoreSelection = function(image) {
                    $scope.ubuntu_core.changed = true;
                    image.checked = !image.checked;
                    $scope.regenerateUbuntuCoreImages();
                };

                // Save the Ubuntu Core image selections into boot selections.
                $scope.saveUbuntuCoreSelection = function() {
                    var params = {
                        images: $scope.ubuntu_core.images.filter(function(
                                image) {
                            return image.checked;
                        }).map(function(image) {
                            return image.name;
                        })
                    };
                    $scope.saving = true;
                    BootResourcesManager.saveUbuntuCore(params).then(
                            function() {
                        $scope.saving = false;
                    });
                };

                // Toggle the selection of other images.
                $scope.toggleOtherSelection = function(image) {
                    $scope.other.changed = true;
                    image.checked = !image.checked;
                    $scope.regenerateOtherImages();
                };

                // Save the other image selections into boot selections.
                $scope.saveOtherSelection = function() {
                    var params = {
                        images: $scope.other.images.filter(function(image) {
                            return image.checked;
                        }).map(function(image) {
                            return image.name;
                        })
                    };
                    $scope.saving = true;
                    BootResourcesManager.saveOther(params).then(function() {
                        $scope.saving = false;
                    });
                };

                // Return True if the Ubuntu image can be removed.
                $scope.canBeRemoved = function(image) {
                    // Image must have a resourceId to be able to be removed.
                    if(!angular.isNumber(image.resourceId)) {
                        return false;
                    }

                    // If the release or architecture is set to deleted
                    // then this image can be deleted.
                    var i;
                    var releases = $scope.bootResources.ubuntu.releases;
                    var arches = $scope.bootResources.ubuntu.arches;
                    for(i = 0; i < releases.length; i++) {
                        var release = releases[i];
                        if(release.deleted && image.title === release.title) {
                            return true;
                        }
                    }
                    for(i = 0; i < arches.length; i++) {
                        var arch = arches[i];
                        if(arch.deleted && image.arch === arch.name) {
                            return true;
                        }
                    }
                    return false;
                };

                // Deletes the give image.
                $scope.deleteImage = function(image) {
                    if(angular.isObject(image)) {
                        $scope.ubuntuDeleteId = image.resourceId;
                    } else {
                        $scope.ubuntuDeleteId = null;
                    }
                };

                // Deletes the give image.
                $scope.confirmDeleteImage = function() {
                    // Delete the image by its resourceId.
                    BootResourcesManager.deleteImage(
                        {id: $scope.ubuntuDeleteId});
                    $scope.ubuntuDeleteId = null;
                };

                // Start polling now that the directive is viewable and ensure
                // the UserManager is loaded.
                var ready = 2;
                BootResourcesManager.startPolling().then(function() {
                    ready -= 1;
                    if(ready === 0) {
                        $scope.loading = false;
                    }
                });
                ManagerHelperService.loadManager(
                    $scope, UsersManager).then(function() {
                    ready -= 1;
                    if(ready === 0) {
                        $scope.loading = false;
                    }
                });

                // Update the source information with the ubuntu source
                // information changes.
                $scope.$watch("bootResources.ubuntu", function() {
                    if(!angular.isObject($scope.bootResources.ubuntu)) {
                        return;
                    }
                    $scope.updateSource();
                });

                // Regenerate the images array when the resources change.
                $scope.$watch("bootResources.resources", function() {
                    if(!angular.isArray($scope.bootResources.resources)) {
                        return;
                    }
                    $scope.regenerateUbuntuImages();
                    $scope.regenerateUbuntuCoreImages();
                    $scope.regenerateOtherImages();
                    $scope.regenerateGeneratedImages();
                    $scope.regenerateCustomImages();
                });

                $scope.$watch("bootResources.ubuntu_core_images", function() {
                    var images = $scope.bootResources.ubuntu_core_images;
                    if(!angular.isArray(images)) {
                        return;
                    }
                    if(!$scope.ubuntu_core.changed) {
                        $scope.ubuntu_core.images = images;
                    }
                    $scope.regenerateUbuntuCoreImages();
                });

                $scope.$watch("bootResources.other_images", function() {
                    if(!angular.isArray($scope.bootResources.other_images)) {
                        return;
                    }
                    if(!$scope.other.changed) {
                        $scope.other.images = $scope.bootResources.other_images;
                    }
                    $scope.regenerateOtherImages();
                });

                // Stop polling when the directive is destroyed.
                $scope.$on('$destroy', function() {
                    BootResourcesManager.stopPolling();
                });
            }
        };
    }]);