step2_controller.js 7.0 KB

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