step2_controller.js 11 KB

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