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