components.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with this
  4. * work for additional information regarding copyright ownership. The ASF
  5. * licenses this file to you under the Apache License, Version 2.0 (the
  6. * "License"); you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. * License for the specific language governing permissions and limitations under
  15. * the License.
  16. */
  17. var App = require('app');
  18. module.exports = {
  19. installHostComponent: function(hostName, component) {
  20. var
  21. self = this,
  22. componentName = component.get('componentName'),
  23. displayName = component.get('displayName');
  24. App.ajax.send({
  25. name: 'host.host_component.add_new_component',
  26. sender: self,
  27. data: {
  28. hostName: hostName,
  29. component: component,
  30. data: JSON.stringify({
  31. RequestInfo: {
  32. "context": Em.I18n.t('requestInfo.installHostComponent') + " " + displayName
  33. },
  34. Body: {
  35. host_components: [
  36. {
  37. HostRoles: {
  38. component_name: componentName
  39. }
  40. }
  41. ]
  42. }
  43. })
  44. },
  45. success: 'addNewComponentSuccessCallback',
  46. error: 'ajaxErrorCallback'
  47. });
  48. },
  49. /**
  50. * Success callback for add host component request
  51. * @param {object} data
  52. * @param {object} opt
  53. * @param {object} params
  54. * @method addNewComponentSuccessCallback
  55. */
  56. addNewComponentSuccessCallback: function (data, opt, params) {
  57. console.log('Send request for ADDING NEW COMPONENT successfully');
  58. App.ajax.send({
  59. name: 'common.host.host_component.update',
  60. sender: App.router.get('mainHostDetailsController'),
  61. data: {
  62. hostName: params.hostName,
  63. componentName: params.component.get('componentName'),
  64. serviceName: params.component.get('serviceName'),
  65. component: params.component,
  66. "context": Em.I18n.t('requestInfo.installNewHostComponent') + " " + params.component.get('displayName'),
  67. HostRoles: {
  68. state: 'INSTALLED'
  69. },
  70. urlParams: "HostRoles/state=INIT"
  71. },
  72. success: 'installNewComponentSuccessCallback',
  73. error: 'ajaxErrorCallback'
  74. });
  75. },
  76. /**
  77. * Default error-callback for ajax-requests in current page
  78. * @param {object} request
  79. * @param {object} ajaxOptions
  80. * @param {string} error
  81. * @param {object} opt
  82. * @param {object} params
  83. * @method ajaxErrorCallback
  84. */
  85. ajaxErrorCallback: function (request, ajaxOptions, error, opt, params) {
  86. console.log('error on change component host status');
  87. App.ajax.defaultErrorHandler(request, opt.url, opt.method);
  88. },
  89. downloadClientConfigs: function (data) {
  90. var isForHost = typeof data.hostName !== 'undefined';
  91. var url = App.get('apiPrefix') + '/clusters/' + App.router.getClusterName() + '/' +
  92. (isForHost ? 'hosts/' + data.hostName + '/host_components/' : 'services/' + data.serviceName + '/components/') +
  93. data.componentName + '?format=client_config_tar';
  94. try {
  95. var self = this;
  96. $.fileDownload(url).fail(function (error) {
  97. var errorMessage = '';
  98. var isNoConfigs = false;
  99. if (error && $(error).text()) {
  100. var errorObj = JSON.parse($(error).text());
  101. if (errorObj && errorObj.message && errorObj.status) {
  102. isNoConfigs = errorObj.message.indexOf(Em.I18n.t('services.service.actions.downloadClientConfigs.fail.noConfigFile')) !== -1;
  103. errorMessage += isNoConfigs ? Em.I18n.t('services.service.actions.downloadClientConfigs.fail.noConfigFile') :
  104. Em.I18n.t('services.service.actions.downloadClientConfigs.fail.popup.body.errorMessage').format(data.displayName, errorObj.status, errorObj.message);
  105. } else {
  106. errorMessage += Em.I18n.t('services.service.actions.downloadClientConfigs.fail.popup.body.noErrorMessage').format(data.displayName);
  107. }
  108. errorMessage += isNoConfigs ? '' : Em.I18n.t('services.service.actions.downloadClientConfigs.fail.popup.body.question');
  109. } else {
  110. errorMessage += Em.I18n.t('services.service.actions.downloadClientConfigs.fail.popup.body.noErrorMessage').format(data.displayName) +
  111. Em.I18n.t('services.service.actions.downloadClientConfigs.fail.popup.body.question');
  112. }
  113. App.ModalPopup.show({
  114. header: Em.I18n.t('services.service.actions.downloadClientConfigs.fail.popup.header').format(data.displayName),
  115. bodyClass: Ember.View.extend({
  116. template: Em.Handlebars.compile(errorMessage)
  117. }),
  118. secondary: isNoConfigs ? false : Em.I18n.t('common.cancel'),
  119. onPrimary: function () {
  120. this.hide();
  121. if (!isNoConfigs) {
  122. self.downloadClientConfigs({
  123. context: Em.Object.create(data)
  124. })
  125. }
  126. }
  127. });
  128. });
  129. } catch (err) {
  130. var newWindow = window.open(url);
  131. newWindow.focus();
  132. }
  133. },
  134. /**
  135. * Check if all required components are installed on host.
  136. * Available options:
  137. * scope: 'host' - dependency level `host`,`cluster` or `*`.
  138. * hostName: 'example.com' - host name to search installed components
  139. * installedComponents: ['A', 'B'] - names of installed components
  140. *
  141. * By default scope level is `*`
  142. * For host level dependency you should specify at least `hostName` or `installedComponents` attribute.
  143. *
  144. * @param {String} componentName
  145. * @param {Object} opt - options. Allowed options are `hostName`, `installedComponents`, `scope`.
  146. * @return {Array} - names of missed components
  147. */
  148. checkComponentDependencies: function(componentName, opt) {
  149. opt = opt || {};
  150. opt.scope = opt.scope || '*';
  151. var installedComponents;
  152. var dependencies = App.StackServiceComponent.find(componentName).get('dependencies');
  153. dependencies = opt.scope === '*' ? dependencies : dependencies.filterProperty('scope', opt.scope);
  154. if (dependencies.length == 0) return [];
  155. switch (opt.scope) {
  156. case 'host':
  157. Em.assert("You should pass at least `hostName` or `installedComponents` to options.", opt.hostName || opt.installedComponents);
  158. installedComponents = opt.installedComponents || App.HostComponent.find().filterProperty('hostName', opt.hostName).mapProperty('componentName').uniq();
  159. break;
  160. default:
  161. // @todo: use more appropriate value regarding installed components
  162. installedComponents = opt.installedComponents || App.HostComponent.find().mapProperty('componentName').uniq();
  163. break;
  164. }
  165. return dependencies.filter(function(dependency) {
  166. return !installedComponents.contains(dependency.componentName);
  167. }).mapProperty('componentName');
  168. }
  169. };