step2_controller.js 9.0 KB

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