step2_controller.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. var validator = require('utils/validator');
  20. App.WizardStep2Controller = Em.Controller.extend({
  21. name: 'wizardStep2Controller',
  22. hostNameArr: [],
  23. isPattern: false,
  24. bootRequestId: null,
  25. hasSubmitted: false,
  26. hostNames: function () {
  27. return this.get('content.installOptions.hostNames');
  28. }.property('content.installOptions.hostNames'),
  29. manualInstall: function () {
  30. return this.get('content.installOptions.manualInstall');
  31. }.property('content.installOptions.manualInstall'),
  32. sshKey: function () {
  33. return this.get('content.installOptions.sshKey');
  34. }.property('content.installOptions.sshKey'),
  35. installType: function () {
  36. return this.get('manualInstall') ? 'manualDriven' : 'ambariDriven';
  37. }.property('manualInstall'),
  38. isHostNameValid: function (hostname) {
  39. // For now hostnames that start or end with '-' are not allowed and hostname should be valid
  40. return validator.isHostname(hostname) && (!(/^\-/.test(hostname) || /\-$/.test(hostname)));
  41. },
  42. updateHostNameArr: function(){
  43. this.hostNameArr = this.get('hostNames').trim().split(new RegExp("\\s+", "g"));
  44. this.patternExpression();
  45. var installedHostNames = App.Host.find().mapProperty('hostName');
  46. var tempArr = [];
  47. for (i = 0; i < this.hostNameArr.length; i++) {
  48. if (!installedHostNames.contains(this.hostNameArr[i])) {
  49. tempArr.push(this.hostNameArr[i]);
  50. }
  51. }
  52. this.set('hostNameArr', tempArr);
  53. },
  54. isAllHostNamesValid: function () {
  55. var self = this;
  56. var result = true;
  57. this.updateHostNameArr();
  58. this.hostNameArr.forEach(function(hostName){
  59. if (!self.isHostNameValid(hostName)) {
  60. result = false;
  61. }
  62. });
  63. return result;
  64. },
  65. hostsError: null,
  66. checkHostError: function () {
  67. if (this.get('hostNames').trim() === '') {
  68. this.set('hostsError', Em.I18n.t('installer.step2.hostName.error.required'));
  69. }
  70. else {
  71. if (this.isAllHostNamesValid() === false) {
  72. this.set('hostsError', Em.I18n.t('installer.step2.hostName.error.invalid'));
  73. }
  74. else {
  75. this.set('hostsError', null);
  76. }
  77. }
  78. },
  79. checkHostAfterSubmitHandler: function() {
  80. if (this.get('hasSubmitted')) {
  81. this.checkHostError();
  82. }
  83. }.observes('hasSubmitted', 'hostNames'),
  84. sshKeyError: function () {
  85. if (this.get('hasSubmitted') && this.get('manualInstall') === false && this.get('sshKey').trim() === '') {
  86. return Em.I18n.t('installer.step2.sshKey.error.required');
  87. }
  88. return null;
  89. }.property('sshKey', 'manualInstall', 'hasSubmitted'),
  90. /**
  91. * Get host info, which will be saved in parent controller
  92. */
  93. getHostInfo: function () {
  94. var hostNameArr = this.get('hostNameArr');
  95. var hostInfo = {};
  96. for (var i = 0; i < hostNameArr.length; i++) {
  97. hostInfo[hostNameArr[i]] = {
  98. name: hostNameArr[i],
  99. installType: this.get('installType'),
  100. bootStatus: 'PENDING'
  101. };
  102. }
  103. return hostInfo;
  104. },
  105. /**
  106. * Used to set sshKey from FileUploader
  107. * @param sshKey
  108. */
  109. setSshKey: function(sshKey){
  110. this.set("content.installOptions.sshKey", sshKey);
  111. },
  112. /**
  113. * Onclick handler for <code>next button</code>. Do all UI work except data saving.
  114. * This work is doing by router.
  115. * @return {Boolean}
  116. */
  117. evaluateStep: function () {
  118. console.log('TRACE: Entering controller:WizardStep2:evaluateStep function');
  119. if (this.get('isSubmitDisabled')) {
  120. return false;
  121. }
  122. this.set('hasSubmitted', true);
  123. this.checkHostError();
  124. if (this.get('hostsError')) {
  125. return false;
  126. }
  127. if (this.get('sshKeyError')) {
  128. return false;
  129. }
  130. this.updateHostNameArr();
  131. if (!this.hostNameArr.length) {
  132. this.set('hostsError', Em.I18n.t('installer.step2.hostName.error.required'));
  133. return false;
  134. }
  135. if(this.isPattern)
  136. {
  137. this.hostNamePatternPopup(this.hostNameArr);
  138. return false;
  139. }
  140. this.proceedNext();
  141. },
  142. patternExpression: function(){
  143. this.isPattern = false;
  144. var self = this;
  145. var hostNames = [];
  146. $.each(this.hostNameArr, function(e,a){
  147. var start, end, extra = {0:""};
  148. if(/\[\d*\-\d*\]/.test(a)){
  149. start=a.match(/\[\d*/);
  150. end=a.match(/\-\d*/);
  151. start=start[0].substr(1);
  152. end=end[0].substr(1);
  153. if(parseInt(start) <= parseInt(end) && parseInt(start) >= 0){
  154. self.isPattern = true;
  155. if(start[0] == "0" && start.length > 1) {
  156. extra = start.match(/0*/);
  157. }
  158. for (var i = parseInt(start); i < parseInt(end) + 1; i++) {
  159. hostNames.push(a.replace(/\[\d*\-\d*\]/,extra[0].substring(1,1+extra[0].length-i.toString().length)+i))
  160. }
  161. }else{
  162. hostNames.push(a);
  163. }
  164. }else{
  165. hostNames.push(a);
  166. }
  167. });
  168. this.hostNameArr = hostNames;
  169. },
  170. proceedNext: function(){
  171. if (this.get('manualInstall') === true) {
  172. this.manualInstallPopup();
  173. return false;
  174. }
  175. var bootStrapData = JSON.stringify({'verbose': true, 'sshKey': this.get('sshKey'), hosts: this.get('hostNameArr')});
  176. if (App.skipBootstrap) {
  177. this.saveHosts();
  178. return true;
  179. }
  180. var requestId = App.router.get(this.get('content.controllerName')).launchBootstrap(bootStrapData);
  181. if (requestId == '0') {
  182. var controller = App.router.get(App.clusterStatus.wizardControllerName);
  183. controller.registerErrPopup('Information', 'Host Registration is currently in progress. Please try again later.');
  184. } else if (requestId) {
  185. this.set('content.installOptions.bootRequestId', requestId);
  186. this.saveHosts();
  187. }
  188. },
  189. hostNamePatternPopup: function (hostNames) {
  190. var self = this;
  191. App.ModalPopup.show({
  192. header: Em.I18n.t('installer.step2.hostName.pattern.header'),
  193. onPrimary: function () {
  194. self.proceedNext();
  195. this.hide();
  196. },
  197. bodyClass: Ember.View.extend({
  198. template: Ember.Handlebars.compile(['{{#each host in view.hostNames}}<p>{{host}}</p>{{/each}}'].join('\n')),
  199. hostNames: hostNames
  200. })
  201. });
  202. },
  203. manualInstallPopup: function () {
  204. var self = this;
  205. App.ModalPopup.show({
  206. header: Em.I18n.t('installer.step2.manualInstall.popup.header'),
  207. onPrimary: function () {
  208. this.hide();
  209. self.saveHosts();
  210. },
  211. bodyClass: Ember.View.extend({
  212. templateName: require('templates/wizard/step2ManualInstallPopup')
  213. })
  214. });
  215. },
  216. isSubmitDisabled: function () {
  217. return (this.get('hostsError') || this.get('sshKeyError'));
  218. }.property('hostsError', 'sshKeyError'),
  219. saveHosts: function(){
  220. this.set('content.hosts', this.getHostInfo());
  221. App.router.send('next');
  222. }
  223. });