step2_controller.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. inputtedAgainHostNames: [],
  27. isInstaller: function () {
  28. return this.get('content.controllerName') == 'installerController';
  29. }.property('content.controllerName'),
  30. hostNames: function () {
  31. return this.get('content.installOptions.hostNames');
  32. }.property('content.installOptions.hostNames'),
  33. manualInstall: function () {
  34. return this.get('content.installOptions.manualInstall');
  35. }.property('content.installOptions.manualInstall'),
  36. sshKey: function () {
  37. return this.get('content.installOptions.sshKey');
  38. }.property('content.installOptions.sshKey'),
  39. sshUser: function () {
  40. return this.get('content.installOptions.sshUser');
  41. }.property('content.installOptions.sshUser'),
  42. installType: function () {
  43. return this.get('manualInstall') ? 'manualDriven' : 'ambariDriven';
  44. }.property('manualInstall'),
  45. isHostNameValid: function (hostname) {
  46. // disabling hostname validation as we don't want to be too restrictive and disallow
  47. // user's hostnames
  48. // return validator.isHostname(hostname) && (!(/^\-/.test(hostname) || /\-$/.test(hostname)));
  49. return true;
  50. },
  51. /**
  52. * set not installed hosts to the hostNameArr
  53. */
  54. updateHostNameArr: function(){
  55. this.hostNameArr = this.get('hostNames').trim().split(new RegExp("\\s+", "g"));
  56. this.patternExpression();
  57. this.get('inputtedAgainHostNames').clear();
  58. var installedHostNames = App.Host.find().mapProperty('hostName');
  59. var tempArr = [];
  60. for (var i = 0; i < this.hostNameArr.length; i++) {
  61. if (!installedHostNames.contains(this.hostNameArr[i])) {
  62. tempArr.push(this.hostNameArr[i]);
  63. } else {
  64. this.get('inputtedAgainHostNames').push(this.hostNameArr[i]);
  65. }
  66. }
  67. this.set('hostNameArr', tempArr);
  68. },
  69. /**
  70. * validate host names
  71. * @return {Boolean}
  72. */
  73. isAllHostNamesValid: function () {
  74. var self = this;
  75. var result = true;
  76. this.updateHostNameArr();
  77. this.hostNameArr.forEach(function(hostName){
  78. if (!self.isHostNameValid(hostName)) {
  79. result = false;
  80. }
  81. });
  82. return result;
  83. },
  84. hostsError: null,
  85. /**
  86. * set hostsError if host names don't pass validation
  87. */
  88. checkHostError: function () {
  89. if (this.get('hostNames').trim() === '') {
  90. this.set('hostsError', Em.I18n.t('installer.step2.hostName.error.required'));
  91. }
  92. else {
  93. if (this.isAllHostNamesValid() === false) {
  94. this.set('hostsError', Em.I18n.t('installer.step2.hostName.error.invalid'));
  95. }
  96. else {
  97. this.set('hostsError', null);
  98. }
  99. }
  100. },
  101. checkHostAfterSubmitHandler: function() {
  102. if (this.get('hasSubmitted')) {
  103. this.checkHostError();
  104. }
  105. }.observes('hasSubmitted', 'hostNames'),
  106. sshKeyError: function () {
  107. if (this.get('hasSubmitted') && this.get('manualInstall') === false && this.get('sshKey').trim() === '') {
  108. return Em.I18n.t('installer.step2.sshKey.error.required');
  109. }
  110. return null;
  111. }.property('sshKey', 'manualInstall', 'hasSubmitted'),
  112. sshUserError: function(){
  113. if (this.get('manualInstall') === false && this.get('sshUser').trim() === '') {
  114. return Em.I18n.t('installer.step2.sshUser.required');
  115. }
  116. return null;
  117. }.property('sshUser', 'hasSubmitted', 'manualInstall'),
  118. /**
  119. * Get host info, which will be saved in parent controller
  120. */
  121. getHostInfo: function () {
  122. var hostNameArr = this.get('hostNameArr');
  123. var hostInfo = {};
  124. for (var i = 0; i < hostNameArr.length; i++) {
  125. hostInfo[hostNameArr[i]] = {
  126. name: hostNameArr[i],
  127. installType: this.get('installType'),
  128. bootStatus: 'PENDING'
  129. };
  130. }
  131. return hostInfo;
  132. },
  133. /**
  134. * Used to set sshKey from FileUploader
  135. * @param sshKey
  136. */
  137. setSshKey: function(sshKey){
  138. this.set("content.installOptions.sshKey", sshKey);
  139. },
  140. /**
  141. * Onclick handler for <code>next button</code>. Do all UI work except data saving.
  142. * This work is doing by router.
  143. * @return {Boolean}
  144. */
  145. evaluateStep: function () {
  146. console.log('TRACE: Entering controller:WizardStep2:evaluateStep function');
  147. if (this.get('isSubmitDisabled')) {
  148. return false;
  149. }
  150. this.set('hasSubmitted', true);
  151. this.checkHostError();
  152. if (this.get('hostsError') || this.get('sshUserError') || this.get('sshKeyError')) {
  153. return false;
  154. }
  155. this.updateHostNameArr();
  156. if (!this.hostNameArr.length) {
  157. this.set('hostsError', Em.I18n.t('installer.step2.hostName.error.already_installed'));
  158. return false;
  159. }
  160. if(this.isPattern)
  161. {
  162. this.hostNamePatternPopup(this.hostNameArr);
  163. return false;
  164. }
  165. if (this.get('inputtedAgainHostNames').length) {
  166. this.installedHostsPopup();
  167. } else {
  168. this.proceedNext();
  169. }
  170. },
  171. /**
  172. * check is there a pattern expression in host name textarea
  173. * push hosts that match pattern in hostNamesArr
  174. */
  175. patternExpression: function(){
  176. this.isPattern = false;
  177. var self = this;
  178. var hostNames = [];
  179. $.each(this.hostNameArr, function(e,a){
  180. var start, end, extra = {0:""};
  181. if(/\[\d*\-\d*\]/.test(a)){
  182. start=a.match(/\[\d*/);
  183. end=a.match(/\-\d*]/);
  184. start=start[0].substr(1);
  185. end=end[0].substr(1);
  186. if(parseInt(start) <= parseInt(end, 10) && parseInt(start, 10) >= 0){
  187. self.isPattern = true;
  188. if(start[0] == "0" && start.length > 1) {
  189. extra = start.match(/0*/);
  190. }
  191. for (var i = parseInt(start, 10); i < parseInt(end, 10) + 1; i++) {
  192. hostNames.push(a.replace(/\[\d*\-\d*\]/,extra[0].substring(0,start.length-i.toString().length)+i))
  193. }
  194. }else{
  195. hostNames.push(a);
  196. }
  197. }else{
  198. hostNames.push(a);
  199. }
  200. });
  201. this.hostNameArr = hostNames;
  202. },
  203. /**
  204. * launch hosts to bootstrap
  205. * and save already registered hosts
  206. * @return {Boolean}
  207. */
  208. proceedNext: function(){
  209. if (this.get('manualInstall') === true) {
  210. this.manualInstallPopup();
  211. return false;
  212. }
  213. var bootStrapData = JSON.stringify({'verbose': true, 'sshKey': this.get('sshKey'), 'hosts': this.get('hostNameArr'), 'user': this.get('sshUser')});
  214. if (App.skipBootstrap) {
  215. this.saveHosts();
  216. return true;
  217. }
  218. var requestId = App.router.get(this.get('content.controllerName')).launchBootstrap(bootStrapData);
  219. if (requestId == '0') {
  220. var controller = App.router.get(App.clusterStatus.wizardControllerName);
  221. controller.registerErrPopup(Em.I18n.t('common.information'), Em.I18n.t('installer.step2.evaluateStep.hostRegInProgress'));
  222. } else if (requestId) {
  223. this.set('content.installOptions.bootRequestId', requestId);
  224. this.saveHosts();
  225. }
  226. },
  227. /**
  228. * show popup with the list of hosts that are already part of the cluster
  229. */
  230. installedHostsPopup: function () {
  231. var self = this;
  232. App.ModalPopup.show({
  233. header: Em.I18n.t('common.warning'),
  234. onPrimary: function () {
  235. self.proceedNext();
  236. this.hide();
  237. },
  238. bodyClass: Ember.View.extend({
  239. template: Ember.Handlebars.compile('<p>{{t installer.step2.evaluateStep.installedHosts}}</p><p>' + self.get('inputtedAgainHostNames').join(', ') + '</p><p>{{t installer.step2.evaluateStep.continueConfirm}}</p>')
  240. })
  241. });
  242. },
  243. /**
  244. * show popup with hosts generated by pattern
  245. * @param hostNames
  246. */
  247. hostNamePatternPopup: function (hostNames) {
  248. var self = this;
  249. App.ModalPopup.show({
  250. header: Em.I18n.t('installer.step2.hostName.pattern.header'),
  251. onPrimary: function () {
  252. self.proceedNext();
  253. this.hide();
  254. },
  255. bodyClass: Ember.View.extend({
  256. template: Ember.Handlebars.compile(['{{#each host in view.hostNames}}<p>{{host}}</p>{{/each}}'].join('\n')),
  257. hostNames: hostNames
  258. })
  259. });
  260. },
  261. /**
  262. * show notify that installation is manual
  263. * save hosts
  264. */
  265. manualInstallPopup: function () {
  266. var self = this;
  267. App.ModalPopup.show({
  268. header: Em.I18n.t('installer.step2.manualInstall.popup.header'),
  269. onPrimary: function () {
  270. this.hide();
  271. self.saveHosts();
  272. },
  273. bodyClass: Ember.View.extend({
  274. templateName: require('templates/wizard/step2ManualInstallPopup')
  275. })
  276. });
  277. },
  278. isSubmitDisabled: function () {
  279. return (this.get('hostsError') || this.get('sshKeyError') || this.get('sshUserError')) ;
  280. }.property('hostsError', 'sshKeyError', 'sshUserError'),
  281. saveHosts: function(){
  282. this.set('content.hosts', this.getHostInfo());
  283. App.router.send('next');
  284. }
  285. });