step2_controller.js 6.9 KB

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