Code coverage report for app/controllers/main/host/add_controller.js

Statements: 78.57% (121 / 154)      Branches: 87.1% (54 / 62)      Functions: 78.57% (33 / 42)      Lines: 78.15% (118 / 151)      Ignored: none     

All files » app/controllers/main/host/ » add_controller.js
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    1                                     1   1                                                                                                                                                                                     1 1       1 1 1                     1               4 4 3 3   4               1                                                                             4 4 4 2   2 2   1 1 1 1           4             3 3 3 3 3                     6 6 6 11   6 2 3               6           3 3 2 1 1 1       1 1                               1 1             1             4 4 4 4 4             7 7 5 3 1                   8 8 5 6 4 2 2 2 2 2 2 2 2 1   1                       5   3               10 10 10 10 10 2 2 2 2 2 2 1   1 1 1 1 1                   2   8       3 3 2   3               3   3       1 1             2 2                   3 3 3 3 3 3 3 2 2     3   1                       1      
'use strict';
 
;require.register("controllers/main/host/add_controller", function (exports, require, module) {
  /**
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
 
  var App = require('app');
 
  App.AddHostController = App.WizardController.extend({
 
    name: 'addHostController',
 
    totalSteps: 7,
 
    /**
     * @type {string}
     */
    displayName: Em.I18n.t('hosts.add.header'),
 
    /**
     * Used for hiding back button in wizard
     */
    hideBackButton: true,
 
    /**
     * All wizards data will be stored in this variable
     *
     * cluster - cluster name
     * hosts - hosts, ssh key, repo info, etc.
     * services - services list
     * hostsInfo - list of selected hosts
     * slaveComponentHosts, hostSlaveComponents - info about slave hosts
     * masterComponentHosts - info about master hosts
     * serviceConfigGroups - info about selected config group for service
     * configGroups - all config groups
     * config??? - to be described later
     */
    content: Em.Object.create({
      cluster: null,
      hosts: null,
      installOptions: null,
      services: null,
      slaveComponentHosts: null,
      masterComponentHosts: null,
      serviceConfigProperties: null,
      advancedServiceConfig: null,
      controllerName: 'addHostController',
      serviceConfigGroups: null,
      configGroups: null
    }),
 
    /**
     * Load data for all steps until <code>current step</code>
     */
    loadMap: {
      '1': [{
        type: 'sync',
        callback: function callback() {
          this.load('hosts');
          this.load('installOptions');
          this.load('cluster');
        }
      }],
      '2': [{
        type: 'sync',
        callback: function callback() {
          this.loadServices();
        }
      }],
      '3': [{
        type: 'async',
        callback: function callback() {
          var self = this,
              dfd = $.Deferred();
          this.loadClients();
          this.loadServices();
          this.loadMasterComponentHosts().done(function () {
            self.loadSlaveComponentHosts();
            self.load('hosts');
            dfd.resolve();
          });
          return dfd.promise();
        }
      }],
      '5': [{
        type: 'sync',
        callback: function callback() {
          this.loadServiceConfigProperties();
          this.getServiceConfigGroups();
        }
      }]
    },
 
    /**
     * save info about wizard progress, particularly current step of wizard
     * @param currentStep
     * @param completed
     */
    setCurrentStep: function setCurrentStep(currentStep, completed) {
      this._super(currentStep, completed);
      App.clusterStatus.setClusterStatus({
        wizardControllerName: this.get('name'),
        localdb: App.db.data
      });
      var self = this;
      Em.run.next(function () {
        Iif (self.isConfigGroupsEmpty()) {
          self.disableStep(4);
        }
      });
    },
 
    /**
     * return new object extended from clusterStatusTemplate
     * @return Object
     */
    getCluster: function getCluster() {
      return jQuery.extend({}, this.get('clusterStatusTemplate'), { name: App.router.getClusterName() });
    },
 
    /**
     * Remove host from model. Used at <code>Confirm hosts</code> step
     * @param hosts Array of hosts, which we want to delete
     */
    removeHosts: function removeHosts(hosts) {
      var dbHosts = this.getDBProperty('hosts');
      hosts.forEach(function (_hostInfo) {
        var host = _hostInfo.name;
        delete dbHosts[host];
      });
      this.setDBProperty('hosts', dbHosts);
    },
 
    disableStep: function disableStep(step) {
      this.get('isStepDisabled').findProperty('step', step).set('value', true);
    },
 
    isConfigGroupsEmpty: function isConfigGroupsEmpty() {
      return !this.get('content.configGroups') || !this.get('content.configGroups').length;
    },
 
    /**
     * Load services data. Will be used at <code>Select services(step4)</code> step
     */
    loadServices: function loadServices() {
      var services = this.getDBProperty('services');
      if (!services) {
        services = {
          selectedServices: [],
          installedServices: []
        };
        App.StackService.find().forEach(function (item) {
          var isInstalled = App.Service.find().someProperty('serviceName', item.get('serviceName'));
          item.set('isSelected', isInstalled);
          item.set('isInstalled', isInstalled);
          if (isInstalled) {
            services.selectedServices.push(item.get('serviceName'));
            services.installedServices.push(item.get('serviceName'));
          }
        }, this);
        this.setDBProperty('services', services);
      } else {
        App.StackService.find().forEach(function (item) {
          var isSelected = services.selectedServices.contains(item.get('serviceName'));
          var isInstalled = services.installedServices.contains(item.get('serviceName'));
          item.set('isSelected', isSelected);
          item.set('isInstalled', isInstalled);
        }, this);
      }
      this.set('content.services', App.StackService.find());
    },
 
    /**
      * Load slave component hosts data for using in required step controllers
      * TODO move to mixin
      */
    loadSlaveComponentHosts: function loadSlaveComponentHosts() {
      var props = this.getDBProperties(['slaveComponentHosts', 'hosts']);
      var slaveComponentHosts = props.slaveComponentHosts || [];
      if (slaveComponentHosts.length) {
        var hosts = props.hosts || {},
            host_names = Em.keys(hosts);
        slaveComponentHosts.forEach(function (component) {
          component.hosts.forEach(function (host) {
            //Em.set(host, 'hostName', hosts[host.host_id].name);
            for (var i = 0; i < host_names.length; i++) {
              Eif (hosts[host_names[i]].id === host.host_id) {
                host.hostName = host_names[i];
                break;
              }
            }
          });
        });
      }
      this.set("content.slaveComponentHosts", slaveComponentHosts);
    },
 
    /**
     * Generate clients list for selected services and save it to model
     */
    saveClients: function saveClients() {
      var serviceComponents = App.StackServiceComponent.find();
      var services = this.get('content.services').filterProperty('isInstallable').filterProperty('isSelected');
      var clients = this.getClientsToInstall(services, serviceComponents);
      this.setDBProperty('clientInfo', clients);
      this.set('content.clients', clients);
    },
 
    /**
     * get list of clients which will be installed on host
     * @param services {Array} of service objects
     * @param components {Array} of component objects
     * @returns {Array} returns array of clients
     * @method getClientsToInstall;
     */
    getClientsToInstall: function getClientsToInstall(services, components) {
      var clients = [];
      services.forEach(function (_service) {
        var serviceClients = components.filter(function (component) {
          return component.get('serviceName') == _service.get('serviceName') && component.get('isClient');
        });
        if (serviceClients.length) {
          serviceClients.forEach(function (client) {
            clients.push({
              component_name: client.get('componentName'),
              display_name: client.get('displayName'),
              isInstalled: false
            });
          });
        }
      }, this);
      return clients;
    },
    /**
     *  Apply config groups from step4 Configurations
     */
    applyConfigGroup: function applyConfigGroup() {
      var serviceConfigGroups = this.get('content.configGroups');
      serviceConfigGroups.forEach(function (group) {
        if (group.configGroups.someProperty('ConfigGroup.group_name', group.selectedConfigGroup)) {
          var configGroup = group.configGroups.findProperty('ConfigGroup.group_name', group.selectedConfigGroup);
          group.hosts.forEach(function (host) {
            configGroup.ConfigGroup.hosts.push({
              host_name: host
            });
          }, this);
          delete configGroup.href;
          App.ajax.send({
            name: 'config_groups.update_config_group',
            sender: this,
            data: {
              id: configGroup.ConfigGroup.id,
              configGroup: configGroup
            }
          });
        }
      }, this);
    },
 
    /**
     * Load information about selected config groups
     */
    getServiceConfigGroups: function getServiceConfigGroups() {
      var serviceConfigGroups = this.getDBProperty('serviceConfigGroups');
      this.set('content.configGroups', serviceConfigGroups);
    },
 
    /**
     * Save information about selected config groups
     */
    saveServiceConfigGroups: function saveServiceConfigGroups() {
      this.setDBProperty('serviceConfigGroups', this.get('content.configGroups'));
    },
 
    /**
     * Set content.configGroups for step4
     */
    loadServiceConfigGroups: function loadServiceConfigGroups() {
      var selectedServices = [];
      this.loadServiceConfigGroupsBySlaves(selectedServices);
      this.loadServiceConfigGroupsByClients(selectedServices);
      this.sortServiceConfigGroups(selectedServices);
      this.set('content.configGroups', selectedServices);
    },
    /**
     * sort config groups by name
     * @param selectedServices
     */
    sortServiceConfigGroups: function sortServiceConfigGroups(selectedServices) {
      selectedServices.forEach(function (selectedService) {
        selectedService.configGroups.sort(function (cfgA, cfgB) {
          if (cfgA.ConfigGroup.group_name < cfgB.ConfigGroup.group_name) return -1;
          if (cfgA.ConfigGroup.group_name > cfgB.ConfigGroup.group_name) return 1;
          return 0;
        });
      });
    },
    /**
     * load service config groups by slave components,
     * push them into selectedServices
     * @param selectedServices
     */
    loadServiceConfigGroupsBySlaves: function loadServiceConfigGroupsBySlaves(selectedServices) {
      var slaveComponentHosts = this.get('content.slaveComponentHosts');
      if (slaveComponentHosts && slaveComponentHosts.length > 0) {
        slaveComponentHosts.forEach(function (slave) {
          if (slave.hosts.length > 0) {
            if (slave.componentName !== "CLIENT") {
              var service = App.StackServiceComponent.find(slave.componentName).get('stackService');
              var serviceName = service.get('serviceName');
              var configGroups = this.get('content.configGroups').filterProperty('ConfigGroup.tag', serviceName);
              var configGroupsNames = configGroups.mapProperty('ConfigGroup.group_name');
              var defaultGroupName = 'Default';
              var selectedService = selectedServices.findProperty('serviceId', serviceName);
              configGroupsNames.unshift(defaultGroupName);
              if (selectedService) {
                Em.set(selectedService, 'hosts', Em.getWithDefault(selectedService, 'hosts', []).concat(slave.hosts.mapProperty('hostName')).uniq());
              } else {
                selectedServices.push({
                  serviceId: serviceName,
                  displayName: service.get('displayName'),
                  hosts: slave.hosts.mapProperty('hostName'),
                  configGroupsNames: configGroupsNames,
                  configGroups: configGroups,
                  selectedConfigGroup: defaultGroupName
                });
              }
            }
          }
        }, this);
        return true;
      }
      return false;
    },
    /**
     * load service config groups by clients,
     * push them into selectedServices
     * @param selectedServices
     */
    loadServiceConfigGroupsByClients: function loadServiceConfigGroupsByClients(selectedServices) {
      var slaveComponentHosts = this.get('content.slaveComponentHosts');
      var clients = this.get('content.clients');
      var client = slaveComponentHosts && slaveComponentHosts.findProperty('componentName', 'CLIENT');
      var selectedClientHosts = client && client.hosts.mapProperty('hostName');
      if (clients && selectedClientHosts && clients.length > 0 && selectedClientHosts.length > 0) {
        this.loadClients();
        clients.forEach(function (client) {
          var service = App.StackServiceComponent.find(client.component_name).get('stackService');
          var serviceName = service.get('serviceName');
          var serviceMatch = selectedServices.findProperty('serviceId', serviceName);
          if (serviceMatch) {
            serviceMatch.hosts = serviceMatch.hosts.concat(selectedClientHosts).uniq();
          } else {
            var configGroups = this.get('content.configGroups').filterProperty('ConfigGroup.tag', serviceName);
            var configGroupsNames = configGroups.mapProperty('ConfigGroup.group_name').sort();
            var defaultGroupName = 'Default';
            configGroupsNames.unshift(defaultGroupName);
            selectedServices.push({
              serviceId: serviceName,
              displayName: service.get('displayName'),
              hosts: selectedClientHosts,
              configGroupsNames: configGroupsNames,
              configGroups: configGroups,
              selectedConfigGroup: defaultGroupName
            });
          }
        }, this);
        return true;
      }
      return false;
    },
 
    loadServiceConfigProperties: function loadServiceConfigProperties() {
      var serviceConfigProperties = App.db.get('AddService', 'serviceConfigProperties');
      if (!serviceConfigProperties || !serviceConfigProperties.length) {
        serviceConfigProperties = App.db.get('Installer', 'serviceConfigProperties');
      }
      this.set('content.serviceConfigProperties', serviceConfigProperties);
    },
 
    /**
     * Remove all loaded data.
     * Created as copy for App.router.clearAllSteps
     */
    clearAllSteps: function clearAllSteps() {
      this.clearInstallOptions();
      // clear temporary information stored during the install
      this.set('content.cluster', this.getCluster());
    },
 
    clearStorageData: function clearStorageData() {
      this._super();
      this.resetDbNamespace();
    },
 
    /**
     * Clear all temporary data
     */
    finish: function finish() {
      this.clearAllSteps();
      this.clearStorageData();
    },
 
    /**
     * send request to server in order to install services
     * @param isRetry
     * @param callback
     * @param errorCallback
     */
    installServices: function installServices(isRetry, callback, errorCallback) {
      callback = callback || Em.K;
      this.set('content.cluster.oldRequestsId', []);
      this.set('content.cluster.status', 'PENDING');
      var clusterName = this.get('content.cluster.name');
      var hostNames = [];
      var hosts = this.getDBProperty('hosts');
      for (var hostname in hosts) {
        Eif (!hosts[hostname].isInstalled) {
          hostNames.push(hostname);
        }
      }
      if (!clusterName || hostNames.length === 0) return false;
 
      App.ajax.send({
        name: "common.host_components.update",
        sender: this,
        data: {
          "context": Em.I18n.t('requestInfo.installComponents'),
          "query": "HostRoles/host_name.in(" + hostNames.join(',') + ")",
          "HostRoles": { "state": "INSTALLED" },
          "level": "HOST_COMPONENT"
        },
        success: 'installServicesSuccessCallback',
        error: 'installServicesErrorCallback'
      }).then(callback, errorCallback || callback);
      return true;
    }
  });
});