step2_controller.js 8.0 KB

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