slave_component_groups_controller.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. /**
  20. * Used to manage slave component config. User could create different settings for separate group
  21. * @type {*}
  22. */
  23. App.SlaveComponentGroupsController = Em.ArrayController.extend({
  24. name: 'slaveComponentGroupsController',
  25. contentBinding: 'App.router.wizardStep7Controller.slaveComponentHosts',
  26. serviceBinding: 'App.router.wizardStep7Controller.selectedService',
  27. selectedComponentName: function () {
  28. switch (App.router.get('wizardStep7Controller.selectedService.serviceName')) {
  29. case 'HDFS':
  30. return 'DATANODE';
  31. case 'MAPREDUCE':
  32. return 'TASKTRACKER';
  33. case 'HBASE':
  34. return 'HBASE_REGIONSERVER';
  35. default:
  36. return null;
  37. }
  38. }.property('App.router.wizardStep7Controller.selectedService'),
  39. selectedSlaveComponent: function () {
  40. var selectedComponentName = this.get('selectedComponentName');
  41. if (selectedComponentName) {
  42. return this.findProperty('componentName', selectedComponentName);
  43. }
  44. }.property('selectedComponentName'),
  45. hosts: function () {
  46. var selectedComponentName = this.get('selectedComponentName');
  47. if (selectedComponentName) {
  48. var component = this.findProperty('componentName', selectedComponentName);
  49. if(component){
  50. return component.hosts;
  51. }
  52. }
  53. }.property('@each.hosts', 'selectedComponentName'),
  54. groups: function () {
  55. var hosts = this.get('hosts');
  56. if(hosts){
  57. return hosts.mapProperty('group').uniq();
  58. }
  59. }.property('hosts'),
  60. componentGroups: function () {
  61. var component = this.get('selectedSlaveComponent');
  62. if(component){
  63. if (!component.groups) {
  64. component.groups = [];
  65. var defaultGroup = {name: 'Default', index: 'default', type: 'default', active: true};
  66. component.groups.pushObject(defaultGroup);
  67. }
  68. return component.groups;
  69. }
  70. return [];
  71. }.property('selectedSlaveComponent'),
  72. getGroupsForDropDown: function () {
  73. return this.get('componentGroups').getEach('name');
  74. }.property('selectedComponentName', 'componentGroups.@each.name'),
  75. activeGroup: function () {
  76. var componentGroups = this.get('componentGroups');
  77. if (componentGroups) {
  78. var active = componentGroups.findProperty('active', true);
  79. if (active){
  80. return active;
  81. }
  82. }
  83. return null;
  84. }.property('componentGroups.@each.active', 'componentGroups.@each.name'),
  85. /**
  86. * Show slave hosts to groups popup
  87. * @param event
  88. */
  89. showAddSlaveComponentGroup: function (event) {
  90. var componentName = event.context;
  91. var component = this.get('selectedSlaveComponent');
  92. App.ModalPopup.show({
  93. header: componentName + ' Groups',
  94. bodyClass: Ember.View.extend({
  95. controllerBinding: 'App.router.slaveComponentGroupsController',
  96. templateName: require('templates/wizard/slave_component_hosts_popup')
  97. }),
  98. onPrimary: function (event) {
  99. if (component.tempSelectedGroups && component.tempSelectedGroups.length) {
  100. component.tempSelectedGroups.forEach(function (item) {
  101. var changed = component.hosts.filterProperty('hostname', item.hostName);
  102. changed.setEach('group', item.groupName);
  103. })
  104. }
  105. delete component.tempSelectedGroups;
  106. this.hide();
  107. },
  108. onSecondary: function (event) {
  109. delete component.tempSelectedGroups;
  110. this.hide();
  111. },
  112. onClose: function (event) {
  113. delete component.tempSelectedGroups;
  114. this.hide();
  115. }
  116. });
  117. },
  118. /**
  119. * Utility method. Save temporary info about changes in <code>slave hosts to groups</code> popup
  120. * @param host
  121. * @param groupName
  122. */
  123. changeHostGroup: function (host, groupName) {
  124. var component = this.get('selectedSlaveComponent');
  125. if (component.tempSelectedGroups === undefined) {
  126. component.tempSelectedGroups = [];
  127. }
  128. var values = component.tempSelectedGroups.filterProperty('hostName', host.hostname);
  129. if (values.length === 0)
  130. component.tempSelectedGroups.pushObject({hostName: host.hostname, groupName: groupName});
  131. else
  132. values.setEach('groupName', groupName);
  133. },
  134. /**
  135. * add new group to component(click on button)
  136. */
  137. addSlaveComponentGroup: function () {
  138. var component = this.get('selectedSlaveComponent');
  139. var newGroupName = 'New Group';
  140. component.groups.setEach('active', false);
  141. var newGroups = component.groups.filterProperty('name', newGroupName);
  142. if (newGroups.length === 0){
  143. component.newGroupIndex = 0;
  144. }
  145. else {
  146. component.newGroupIndex = component.newGroupIndex || 0;
  147. this.checkGroupName();
  148. newGroupName = 'New Group ' + component.newGroupIndex;
  149. }
  150. var newGroup = {name: newGroupName, index: component.newGroupIndex, type: 'new', active: true};
  151. component.groups.pushObject(newGroup);
  152. $('.remove-group-error').hide();
  153. },
  154. checkGroupName: function () {
  155. var component = this.get('selectedSlaveComponent');
  156. component.newGroupIndex++;
  157. var newGroupName = 'New Group ' + component.newGroupIndex;
  158. var groups = component.groups.filterProperty('name', newGroupName);
  159. if (groups.length !== 0) {
  160. this.checkGroupName();
  161. }
  162. },
  163. /**
  164. * Onclick handler for <code>choose hosts for slave group</code> link
  165. * @param event
  166. */
  167. showEditSlaveComponentGroups: function (event) {
  168. this.showAddSlaveComponentGroup(event);
  169. },
  170. getHostsByGroup: function (group) {
  171. var hosts = this.get('hosts');
  172. if(hosts){
  173. return hosts.filterProperty('group', group.name);
  174. }
  175. },
  176. /**
  177. * Change tab
  178. * @param event
  179. */
  180. showSlaveComponentGroup: function (event) {
  181. var component = this.get('selectedSlaveComponent');
  182. if(!component.groups){
  183. debugger;
  184. }
  185. component.groups.setEach('active', false);
  186. var group = component.groups.filterProperty('name', event.context.name);
  187. group.setEach('active', true);
  188. var assignedHosts = component.hosts.filterProperty('group', event.context.name);
  189. if (assignedHosts.length === 0) {
  190. $('.remove-group-error').hide();
  191. }
  192. },
  193. /**
  194. * Remove tab
  195. * @param event
  196. */
  197. removeSlaveComponentGroup: function (event) {
  198. var group = event.context;
  199. var component = this.get('selectedSlaveComponent');
  200. var assignedHosts = component.hosts.filterProperty('group', group.name);
  201. if (assignedHosts.length !== 0) {
  202. $('.remove-group-error').show();
  203. } else {
  204. $('.remove-group-error').hide();
  205. var key = component.groups.indexOf(group);
  206. component.groups.removeObject(component.groups[key]);
  207. var newGroups = component.groups.filterProperty('type', 'new');
  208. if (newGroups.length == 0)
  209. component.newGroupIndex = 0;
  210. else {
  211. var lastNewGroup = newGroups[newGroups.length - 1];
  212. component.newGroupIndex = lastNewGroup.index;
  213. }
  214. if (group.active) {
  215. var lastGroup;
  216. if (key === component.groups.length)
  217. lastGroup = component.groups.slice(key - 1, key);
  218. else lastGroup = component.groups.slice(key, key + 1);
  219. lastGroup.setEach('active', true);
  220. }
  221. }
  222. },
  223. /**
  224. * change group name of slave component
  225. * @param group
  226. * @param newGroupName
  227. * @return {Boolean}
  228. */
  229. changeSlaveGroupName: function (group, newGroupName) {
  230. var component = this.get('selectedSlaveComponent');
  231. var isExist = component.groups.filterProperty('name', newGroupName);
  232. if (isExist.length !== 0)
  233. return true;
  234. else {
  235. var assignedHosts = component.hosts.filterProperty('group', group.name);
  236. if (assignedHosts.length !== 0){
  237. assignedHosts.setEach('group', newGroupName);
  238. }
  239. var groupFilter = component.groups.filterProperty('name', group.name);
  240. groupFilter.setEach('name', newGroupName);
  241. }
  242. return false;
  243. }
  244. });