step2_controller.js 6.3 KB

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