step2_controller.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 (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. var self = this;
  158. App.ModalPopup.show({
  159. header: Em.I18n.t('common.warning'),
  160. onPrimary: function () {
  161. self.proceedNext();
  162. this.hide();
  163. },
  164. bodyClass: Ember.View.extend({
  165. template: Ember.Handlebars.compile('<p>{{t installer.step2.evaluateStep.installedHosts}}</p><p>' + self.get('inputtedAgainHostNames').join(', ') + '</p><p>{{t installer.step2.evaluateStep.continueConfirm}}</p>')
  166. })
  167. });
  168. } else {
  169. this.proceedNext();
  170. }
  171. },
  172. /**
  173. * check is there a pattern expression in host name textarea
  174. * push hosts that match pattern in hostNamesArr
  175. */
  176. patternExpression: function(){
  177. this.isPattern = false;
  178. var self = this;
  179. var hostNames = [];
  180. $.each(this.hostNameArr, function(e,a){
  181. var start, end, extra = {0:""};
  182. if(/\[\d*\-\d*\]/.test(a)){
  183. start=a.match(/\[\d*/);
  184. end=a.match(/\-\d*]/);
  185. start=start[0].substr(1);
  186. end=end[0].substr(1);
  187. if(parseInt(start) <= parseInt(end) && parseInt(start) >= 0){
  188. self.isPattern = true;
  189. if(start[0] == "0" && start.length > 1) {
  190. extra = start.match(/0*/);
  191. }
  192. for (var i = parseInt(start); i < parseInt(end) + 1; i++) {
  193. hostNames.push(a.replace(/\[\d*\-\d*\]/,extra[0].substring(1,1+extra[0].length-i.toString().length)+i))
  194. }
  195. }else{
  196. hostNames.push(a);
  197. }
  198. }else{
  199. hostNames.push(a);
  200. }
  201. });
  202. this.hostNameArr = hostNames;
  203. },
  204. /**
  205. * launch hosts to bootstrap
  206. * and save already registered hosts
  207. * @return {Boolean}
  208. */
  209. proceedNext: function(){
  210. if (this.get('manualInstall') === true) {
  211. this.manualInstallPopup();
  212. return false;
  213. }
  214. var bootStrapData = JSON.stringify({'verbose': true, 'sshKey': this.get('sshKey'), hosts: this.get('hostNameArr')});
  215. if (App.skipBootstrap) {
  216. this.saveHosts();
  217. return true;
  218. }
  219. var requestId = App.router.get(this.get('content.controllerName')).launchBootstrap(bootStrapData);
  220. if (requestId == '0') {
  221. var controller = App.router.get(App.clusterStatus.wizardControllerName);
  222. controller.registerErrPopup(Em.I18n.t('common.information'), Em.I18n.t('installer.step2.evaluateStep.hostRegInProgress'));
  223. } else if (requestId) {
  224. this.set('content.installOptions.bootRequestId', requestId);
  225. this.saveHosts();
  226. }
  227. },
  228. /**
  229. * show popup with hosts generated by pattern
  230. * @param hostNames
  231. */
  232. hostNamePatternPopup: function (hostNames) {
  233. var self = this;
  234. App.ModalPopup.show({
  235. header: Em.I18n.t('installer.step2.hostName.pattern.header'),
  236. onPrimary: function () {
  237. self.proceedNext();
  238. this.hide();
  239. },
  240. bodyClass: Ember.View.extend({
  241. template: Ember.Handlebars.compile(['{{#each host in view.hostNames}}<p>{{host}}</p>{{/each}}'].join('\n')),
  242. hostNames: hostNames
  243. })
  244. });
  245. },
  246. /**
  247. * show notify that installation is manual
  248. * save hosts
  249. */
  250. manualInstallPopup: function () {
  251. var self = this;
  252. App.ModalPopup.show({
  253. header: Em.I18n.t('installer.step2.manualInstall.popup.header'),
  254. onPrimary: function () {
  255. this.hide();
  256. self.saveHosts();
  257. },
  258. bodyClass: Ember.View.extend({
  259. templateName: require('templates/wizard/step2ManualInstallPopup')
  260. })
  261. });
  262. },
  263. isSubmitDisabled: function () {
  264. return (this.get('hostsError') || this.get('sshKeyError'));
  265. }.property('hostsError', 'sshKeyError'),
  266. saveHosts: function(){
  267. this.set('content.hosts', this.getHostInfo());
  268. App.router.send('next');
  269. }
  270. });