step2_controller.js 6.6 KB

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