step2_controller.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. inputtedAgainHostNames: [],
  27. isInstaller: function () {
  28. return this.get('content.controllerName') == 'installerController';
  29. }.property('content.controllerName'),
  30. hostNames: function () {
  31. return this.get('content.installOptions.hostNames');
  32. }.property('content.installOptions.hostNames'),
  33. manualInstall: function () {
  34. return this.get('content.installOptions.manualInstall');
  35. }.property('content.installOptions.manualInstall'),
  36. sshKey: function () {
  37. return this.get('content.installOptions.sshKey');
  38. }.property('content.installOptions.sshKey'),
  39. sshUser: function () {
  40. return this.get('content.installOptions.sshUser');
  41. }.property('content.installOptions.sshUser'),
  42. installType: function () {
  43. return this.get('manualInstall') ? 'manualDriven' : 'ambariDriven';
  44. }.property('manualInstall'),
  45. isHostNameValid: function (hostname) {
  46. return validator.isHostname(hostname);
  47. },
  48. /**
  49. * set not installed hosts to the hostNameArr
  50. */
  51. updateHostNameArr: function(){
  52. this.hostNameArr = this.get('hostNames').trim().split(new RegExp("\\s+", "g"));
  53. this.patternExpression();
  54. this.get('inputtedAgainHostNames').clear();
  55. var installedHostNames = App.Host.find().mapProperty('hostName');
  56. var tempArr = [];
  57. for (var i = 0; i < this.hostNameArr.length; i++) {
  58. if (!installedHostNames.contains(this.hostNameArr[i])) {
  59. tempArr.push(this.hostNameArr[i]);
  60. } else {
  61. this.get('inputtedAgainHostNames').push(this.hostNameArr[i]);
  62. }
  63. }
  64. this.set('hostNameArr', tempArr);
  65. },
  66. invalidHostNames: [],
  67. /**
  68. * validate host names
  69. * @return {Boolean}
  70. */
  71. isAllHostNamesValid: function () {
  72. var result = true;
  73. this.updateHostNameArr();
  74. this.get('invalidHostNames').clear();
  75. this.hostNameArr.forEach(function(hostName){
  76. if (!this.isHostNameValid(hostName)) {
  77. this.get('invalidHostNames').push(hostName);
  78. result = false;
  79. }
  80. }, this);
  81. return result;
  82. },
  83. hostsError: null,
  84. /**
  85. * set hostsError if host names don't pass validation
  86. */
  87. checkHostError: function () {
  88. if (this.get('hostNames').trim() === '') {
  89. this.set('hostsError', Em.I18n.t('installer.step2.hostName.error.required'));
  90. }
  91. else {
  92. this.set('hostsError', null);
  93. }
  94. },
  95. checkHostAfterSubmitHandler: function() {
  96. if (this.get('hasSubmitted')) {
  97. this.checkHostError();
  98. }
  99. }.observes('hasSubmitted', 'hostNames'),
  100. sshKeyError: function () {
  101. if (this.get('hasSubmitted') && this.get('manualInstall') === false && this.get('sshKey').trim() === '') {
  102. return Em.I18n.t('installer.step2.sshKey.error.required');
  103. }
  104. return null;
  105. }.property('sshKey', 'manualInstall', 'hasSubmitted'),
  106. sshUserError: function(){
  107. if (this.get('manualInstall') === false && this.get('sshUser').trim() === '') {
  108. return Em.I18n.t('installer.step2.sshUser.required');
  109. }
  110. return null;
  111. }.property('sshUser', 'hasSubmitted', 'manualInstall'),
  112. /**
  113. * Get host info, which will be saved in parent controller
  114. */
  115. getHostInfo: function () {
  116. var hostNameArr = this.get('hostNameArr');
  117. var hostInfo = {};
  118. for (var i = 0; i < hostNameArr.length; i++) {
  119. hostInfo[hostNameArr[i]] = {
  120. name: hostNameArr[i],
  121. installType: this.get('installType'),
  122. bootStatus: 'PENDING'
  123. };
  124. }
  125. return hostInfo;
  126. },
  127. /**
  128. * Used to set sshKey from FileUploader
  129. * @param sshKey
  130. */
  131. setSshKey: function(sshKey){
  132. this.set("content.installOptions.sshKey", sshKey);
  133. },
  134. /**
  135. * Onclick handler for <code>next button</code>. Do all UI work except data saving.
  136. * This work is doing by router.
  137. * @return {Boolean}
  138. */
  139. evaluateStep: function () {
  140. console.log('TRACE: Entering controller:WizardStep2:evaluateStep function');
  141. if (this.get('isSubmitDisabled')) {
  142. return false;
  143. }
  144. this.set('hasSubmitted', true);
  145. this.checkHostError();
  146. if (this.get('hostsError') || this.get('sshUserError') || this.get('sshKeyError')) {
  147. return false;
  148. }
  149. this.updateHostNameArr();
  150. if (!this.hostNameArr.length) {
  151. this.set('hostsError', Em.I18n.t('installer.step2.hostName.error.already_installed'));
  152. return false;
  153. }
  154. if(this.isPattern)
  155. {
  156. this.hostNamePatternPopup(this.hostNameArr);
  157. return false;
  158. }
  159. if (this.get('inputtedAgainHostNames').length) {
  160. this.installedHostsPopup();
  161. } else {
  162. this.proceedNext();
  163. }
  164. },
  165. /**
  166. * check is there a pattern expression in host name textarea
  167. * push hosts that match pattern in hostNamesArr
  168. */
  169. patternExpression: function(){
  170. this.isPattern = false;
  171. var self = this;
  172. var hostNames = [];
  173. $.each(this.hostNameArr, function(e,a){
  174. var start, end, extra = {0:""};
  175. if(/\[\d*\-\d*\]/.test(a)){
  176. start=a.match(/\[\d*/);
  177. end=a.match(/\-\d*]/);
  178. start=start[0].substr(1);
  179. end=end[0].substr(1);
  180. if(parseInt(start) <= parseInt(end, 10) && parseInt(start, 10) >= 0){
  181. self.isPattern = true;
  182. if(start[0] == "0" && start.length > 1) {
  183. extra = start.match(/0*/);
  184. }
  185. for (var i = parseInt(start, 10); i < parseInt(end, 10) + 1; i++) {
  186. hostNames.push(a.replace(/\[\d*\-\d*\]/,extra[0].substring(0,start.length-i.toString().length)+i))
  187. }
  188. }else{
  189. hostNames.push(a);
  190. }
  191. }else{
  192. hostNames.push(a);
  193. }
  194. });
  195. this.hostNameArr = hostNames;
  196. },
  197. /**
  198. * launch hosts to bootstrap
  199. * and save already registered hosts
  200. * @return {Boolean}
  201. */
  202. proceedNext: function(warningConfirmed){
  203. if (this.isAllHostNamesValid() !== true && !warningConfirmed) {
  204. this.warningPopup();
  205. return false;
  206. }
  207. if (this.get('manualInstall') === true) {
  208. this.manualInstallPopup();
  209. return false;
  210. }
  211. var bootStrapData = JSON.stringify({'verbose': true, 'sshKey': this.get('sshKey'), 'hosts': this.get('hostNameArr'), 'user': this.get('sshUser')});
  212. if (App.skipBootstrap) {
  213. this.saveHosts();
  214. return true;
  215. }
  216. var requestId = App.router.get(this.get('content.controllerName')).launchBootstrap(bootStrapData);
  217. if (requestId == '0') {
  218. var controller = App.router.get(App.clusterStatus.wizardControllerName);
  219. controller.registerErrPopup(Em.I18n.t('common.information'), Em.I18n.t('installer.step2.evaluateStep.hostRegInProgress'));
  220. } else if (requestId) {
  221. this.set('content.installOptions.bootRequestId', requestId);
  222. this.saveHosts();
  223. }
  224. },
  225. /**
  226. * show warning for host names without dots or IP addresses
  227. */
  228. warningPopup: function () {
  229. var self = this;
  230. App.ModalPopup.show({
  231. header: Em.I18n.t('common.warning'),
  232. onPrimary: function () {
  233. this.hide();
  234. self.proceedNext(true);
  235. },
  236. bodyClass: Ember.View.extend({
  237. template: Ember.Handlebars.compile(Em.I18n.t('installer.step2.warning.popup.body').format(self.get('invalidHostNames').join(', ')))
  238. })
  239. });
  240. },
  241. /**
  242. * show popup with the list of hosts that are already part of the cluster
  243. */
  244. installedHostsPopup: function () {
  245. var self = this;
  246. App.ModalPopup.show({
  247. header: Em.I18n.t('common.warning'),
  248. onPrimary: function () {
  249. self.proceedNext();
  250. this.hide();
  251. },
  252. bodyClass: Ember.View.extend({
  253. template: Ember.Handlebars.compile('<p>{{t installer.step2.evaluateStep.installedHosts}}</p><p>' + self.get('inputtedAgainHostNames').join(', ') + '</p><p>{{t installer.step2.evaluateStep.continueConfirm}}</p>')
  254. })
  255. });
  256. },
  257. /**
  258. * show popup with hosts generated by pattern
  259. * @param hostNames
  260. */
  261. hostNamePatternPopup: function (hostNames) {
  262. var self = this;
  263. App.ModalPopup.show({
  264. header: Em.I18n.t('installer.step2.hostName.pattern.header'),
  265. onPrimary: function () {
  266. self.proceedNext();
  267. this.hide();
  268. },
  269. bodyClass: Ember.View.extend({
  270. template: Ember.Handlebars.compile(['{{#each host in view.hostNames}}<p>{{host}}</p>{{/each}}'].join('\n')),
  271. hostNames: hostNames
  272. })
  273. });
  274. },
  275. /**
  276. * show notify that installation is manual
  277. * save hosts
  278. */
  279. manualInstallPopup: function () {
  280. var self = this;
  281. App.ModalPopup.show({
  282. header: Em.I18n.t('installer.step2.manualInstall.popup.header'),
  283. onPrimary: function () {
  284. this.hide();
  285. self.saveHosts();
  286. },
  287. bodyClass: Ember.View.extend({
  288. templateName: require('templates/wizard/step2ManualInstallPopup')
  289. })
  290. });
  291. },
  292. /**
  293. * warn to manually install ambari-agent on each host
  294. */
  295. manualInstallWarningPopup: function(){
  296. if(!this.get('content.installOptions.useSsh')){
  297. App.ModalPopup.show({
  298. header: Em.I18n.t('common.warning'),
  299. body: Em.I18n.t('installer.step2.manualInstall.info'),
  300. encodeBody: false,
  301. onPrimary: function () {
  302. this.hide();
  303. },
  304. secondary: null
  305. });
  306. }
  307. this.set('content.installOptions.manualInstall', !this.get('content.installOptions.useSsh'));
  308. }.observes('content.installOptions.useSsh'),
  309. isSubmitDisabled: function () {
  310. return (this.get('hostsError') || this.get('sshKeyError') || this.get('sshUserError')) ;
  311. }.property('hostsError', 'sshKeyError', 'sshUserError'),
  312. setAmbariJavaHome: function(){
  313. App.ajax.send({
  314. name: 'ambari.service',
  315. sender: this,
  316. success: 'onGetAmbariJavaHomeSuccess',
  317. error: 'onGetAmbariJavaHomeError'
  318. });
  319. },
  320. onGetAmbariJavaHomeSuccess: function(data) {
  321. this.set('content.installOptions.javaHome',data.RootServiceComponents.properties['java.home']);
  322. },
  323. onGetAmbariJavaHomeError: function() {
  324. console.warn('can\'t get java.home value from server');
  325. this.set('content.installOptions.javaHome',App.defaultJavaHome);
  326. },
  327. saveHosts: function(){
  328. this.set('content.hosts', this.getHostInfo());
  329. this.setAmbariJavaHome();
  330. App.router.send('next');
  331. }
  332. });