step2_controller.js 8.9 KB

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