step2_controller.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. App.WizardStep2Controller = Em.Controller.extend({
  20. name: 'wizardStep2Controller',
  21. hostNameArr: [],
  22. isPattern: false,
  23. bootRequestId: null,
  24. hasSubmitted: false,
  25. hostNames: function () {
  26. return this.get('content.hosts.hostNames');
  27. }.property('content.hosts.hostNames'),
  28. manualInstall: function () {
  29. return this.get('content.hosts.manualInstall');
  30. }.property('content.hosts.manualInstall'),
  31. localRepo: function () {
  32. return this.get('content.hosts.localRepo');
  33. }.property('content.hosts.localRepo'),
  34. sshKey: function () {
  35. return this.get('content.hosts.sshKey');
  36. }.property('content.hosts.sshKey'),
  37. passphrase: function () {
  38. return this.get('content.hosts.passphrase');
  39. }.property('content.hosts.passphrase'),
  40. confirmPassphrase: function () {
  41. return this.get('content.hosts.confirmPassphrase');
  42. }.property('content.hosts.confirmPassphrase'),
  43. installType: function () {
  44. if (this.get('manualInstall') === true) {
  45. return 'manualDriven';
  46. } else {
  47. return 'ambariDriven';
  48. }
  49. }.property('manualInstall'),
  50. isHostNameValid: function (hostname) {
  51. // For now hostnames that start or end with '-' are not allowed
  52. return !(/^\-/.test(hostname) || /\-$/.test(hostname));
  53. },
  54. isAllHostNamesValid: function () {
  55. this.hostNameArr = this.get('hostNames').trim().split(new RegExp("\\s+", "g"));
  56. for (var index in this.hostNameArr) {
  57. if (!this.isHostNameValid(this.hostNameArr[index])) {
  58. return false;
  59. }
  60. }
  61. return true;
  62. },
  63. hostsError: function () {
  64. if (this.get('hasSubmitted') && this.get('hostNames').trim() === '') {
  65. return Em.I18n.t('installer.step2.hostName.error.required');
  66. } else if (this.isAllHostNamesValid() === false) {
  67. return Em.I18n.t('installer.step2.hostName.error.invalid');
  68. }
  69. return null;
  70. }.property('hostNames', 'manualInstall', 'hasSubmitted'),
  71. sshKeyError: function () {
  72. if (this.get('hasSubmitted') && this.get('manualInstall') === false && this.get('sshKey').trim() === '') {
  73. return Em.I18n.t('installer.step2.sshKey.error.required');
  74. }
  75. return null;
  76. }.property('sshKey', 'manualInstall', 'hasSubmitted'),
  77. /**
  78. * Get host info, which will be saved in parent controller
  79. */
  80. getHostInfo: function () {
  81. var hostNameArr = this.get('hostNameArr');
  82. var hostInfo = {};
  83. for (var i = 0; i < hostNameArr.length; i++) {
  84. hostInfo[hostNameArr[i]] = {
  85. name: hostNameArr[i],
  86. installType: this.get('installType'),
  87. bootStatus: 'PENDING'
  88. };
  89. }
  90. return hostInfo;
  91. },
  92. /**
  93. * Onclick handler for <code>next button</code>. Do all UI work except data saving.
  94. * This work is doing by router.
  95. * @return {Boolean}
  96. */
  97. evaluateStep: function () {
  98. console.log('TRACE: Entering controller:WizardStep2:evaluateStep function');
  99. if (this.get('isSubmitDisabled')) {
  100. return false;
  101. }
  102. if(this.isPattern)
  103. {
  104. this.hostNamePatternPopup(this.hostNameArr);
  105. return false;
  106. }
  107. this.proceedNext();
  108. },
  109. patternExpression: function(){
  110. var self = this;
  111. var hostNames = [];
  112. $.each(this.hostNameArr, function(e,a){
  113. var start, end, extra = "";
  114. if(/\[\d*\-\d*\]/.test(a)){
  115. start=a.match(/\[\d*/);
  116. end=a.match(/\-\d*/);
  117. start=start[0].substr(1);
  118. end=end[0].substr(1);
  119. if(parseInt(start) <= parseInt(end) && parseInt(start) >= 0){
  120. self.isPattern = true;
  121. if(start[0] == "0" && start.length > 1)
  122. {
  123. extra = start[0];
  124. }
  125. for (var i = parseInt(start); i < parseInt(end) + 1; i++) {
  126. hostNames.push(a.replace(/\[\d*\-\d*\]/, extra+i ))
  127. }
  128. }
  129. }else{
  130. hostNames.push(a);
  131. }
  132. });
  133. this.hostNameArr = hostNames;
  134. },
  135. proceedNext: function(){
  136. if (this.get('manualInstall') === true) {
  137. this.manualInstallPopup();
  138. return false;
  139. }
  140. var bootStrapData = JSON.stringify({'verbose': true, 'sshKey': this.get('sshKey'), hosts: this.get('hostNameArr')});
  141. if (App.skipBootstrap) {
  142. App.router.send('next');
  143. return true;
  144. }
  145. var requestId = App.router.get(this.get('content.controllerName')).launchBootstrap(bootStrapData);
  146. if(requestId) {
  147. this.set('bootRequestId',requestId);
  148. App.router.send('next');
  149. }
  150. },
  151. hostNamePatternPopup: function (hostNames) {
  152. var self = this;
  153. App.ModalPopup.show({
  154. header: Em.I18n.t('installer.step2.hostName.pattern.header'),
  155. onPrimary: function () {
  156. self.proceedNext();
  157. this.hide();
  158. },
  159. bodyClass: Ember.View.extend({
  160. template: Ember.Handlebars.compile(['{{#each host in view.hostNames}}<p>{{host}}</p>{{/each}}'].join('\n')),
  161. hostNames: hostNames
  162. })
  163. });
  164. },
  165. manualInstallPopup: function (event) {
  166. App.ModalPopup.show({
  167. header: Em.I18n.t('installer.step2.manualInstall.popup.header'),
  168. onPrimary: function () {
  169. this.hide();
  170. App.router.send('next');
  171. },
  172. bodyClass: Ember.View.extend({
  173. templateName: require('templates/wizard/step2ManualInstallPopup')
  174. })
  175. });
  176. },
  177. isSubmitDisabled: function () {
  178. return (this.get('hostsError') || this.get('sshKeyError'));
  179. }.property('hostsError', 'sshKeyError')
  180. });