step2_controller.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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. /**
  24. * List of not installed hostnames
  25. * @type {string[]}
  26. */
  27. hostNameArr: [],
  28. /**
  29. * Does pattern-expression for hostnames contains some errors
  30. * @type {bool}
  31. */
  32. isPattern: false,
  33. /**
  34. * Don't know if it used any more
  35. */
  36. bootRequestId: null,
  37. /**
  38. * Is step submitted
  39. * @type {bool}
  40. */
  41. hasSubmitted: false,
  42. /**
  43. * @type {string[]}
  44. */
  45. inputtedAgainHostNames: [],
  46. /**
  47. * Is Installer Controller used
  48. * @type {bool}
  49. */
  50. isInstaller: function () {
  51. return this.get('content.controllerName') == 'installerController';
  52. }.property('content.controllerName'),
  53. /**
  54. * "Shortcut" to <code>content.installOptions.hostNames</code>
  55. * @type {string}
  56. */
  57. hostNames: function () {
  58. return this.get('content.installOptions.hostNames').toLowerCase();
  59. }.property('content.installOptions.hostNames'),
  60. /**
  61. * Is manual install selected
  62. * "Shortcut" to <code>content.installOptions.manualInstall</code>
  63. * @type {bool}
  64. */
  65. manualInstall: function () {
  66. return this.get('content.installOptions.manualInstall');
  67. }.property('content.installOptions.manualInstall'),
  68. /**
  69. * "Shortcut" to <code>content.installOptions.sshKey</code>
  70. * @type {string}
  71. */
  72. sshKey: function () {
  73. return this.get('content.installOptions.sshKey');
  74. }.property('content.installOptions.sshKey'),
  75. /**
  76. * "Shortcut" to <code>content.installOptions.sshUser</code>
  77. * @type {string}
  78. */
  79. sshUser: function () {
  80. return this.get('content.installOptions.sshUser');
  81. }.property('content.installOptions.sshUser'),
  82. /**
  83. * Installed type based on <code>manualInstall</code>
  84. * @type {string}
  85. */
  86. installType: function () {
  87. return this.get('manualInstall') ? 'manualDriven' : 'ambariDriven';
  88. }.property('manualInstall'),
  89. /**
  90. * List of invalid hostnames
  91. * @type {string[]}
  92. */
  93. invalidHostNames: [],
  94. /**
  95. * Error-message if <code>hostNames</code> is empty, null otherwise
  96. * @type {string|null}
  97. */
  98. hostsError: null,
  99. isSSHRegistrationEnabled: function () {
  100. return !App.get('isHadoopWindowsStack');
  101. }.property('App.isHadoopWindowsStack'),
  102. /**
  103. * Error-message if <code>sshKey</code> is empty, null otherwise
  104. * @type {string|null}
  105. */
  106. sshKeyError: function () {
  107. if (this.get('hasSubmitted') && this.get('manualInstall') === false && Em.isEmpty(this.get('sshKey').trim())) {
  108. return Em.I18n.t('installer.step2.sshKey.error.required');
  109. }
  110. return null;
  111. }.property('sshKey', 'manualInstall', 'hasSubmitted'),
  112. /**
  113. * Error-message if <code>sshUser</code> is empty, null otherwise
  114. * @type {string|null}
  115. */
  116. sshUserError: function () {
  117. if (this.get('manualInstall') === false && Em.isEmpty(this.get('sshUser').trim())) {
  118. return Em.I18n.t('installer.step2.sshUser.required');
  119. }
  120. return null;
  121. }.property('sshUser', 'hasSubmitted', 'manualInstall'),
  122. /**
  123. * is Submit button disabled
  124. * @type {bool}
  125. */
  126. isSubmitDisabled: function () {
  127. return (this.get('hostsError') || this.get('sshKeyError') || this.get('sshUserError'));
  128. }.property('hostsError', 'sshKeyError', 'sshUserError'),
  129. installedHostNames: function () {
  130. var installedHostsName = [];
  131. var hosts = this.get('content.hosts');
  132. for (var hostName in hosts) {
  133. if (hosts[hostName].isInstalled) {
  134. installedHostsName.push(hostName);
  135. }
  136. }
  137. return installedHostsName;
  138. }.property('content.hosts'),
  139. /**
  140. * Set not installed hosts to the hostNameArr
  141. * @method updateHostNameArr
  142. */
  143. updateHostNameArr: function () {
  144. this.set('hostNameArr', this.get('hostNames').trim().split(new RegExp("\\s+", "g")));
  145. this.parseHostNamesAsPatternExpression();
  146. this.get('inputtedAgainHostNames').clear();
  147. var tempArr = [],
  148. hostNameArr = this.get('hostNameArr');
  149. for (var i = 0; i < hostNameArr.length; i++) {
  150. if (!this.get('installedHostNames').contains(hostNameArr[i])) {
  151. tempArr.push(hostNameArr[i]);
  152. }
  153. else {
  154. this.get('inputtedAgainHostNames').push(hostNameArr[i]);
  155. }
  156. }
  157. this.set('hostNameArr', tempArr);
  158. },
  159. /**
  160. * Validate host names
  161. * @method isAllHostNamesValid
  162. * @return {bool}
  163. */
  164. isAllHostNamesValid: function () {
  165. var result = true;
  166. this.updateHostNameArr();
  167. this.get('invalidHostNames').clear();
  168. this.get('hostNameArr').forEach(function (hostName) {
  169. if (!validator.isHostname(hostName)) {
  170. this.get('invalidHostNames').push(hostName);
  171. result = false;
  172. }
  173. }, this);
  174. return result;
  175. },
  176. /**
  177. * Set hostsError if host names don't pass validation
  178. * @method checkHostError
  179. */
  180. checkHostError: function () {
  181. if (Em.isEmpty(this.get('hostNames').trim())) {
  182. this.set('hostsError', Em.I18n.t('installer.step2.hostName.error.required'));
  183. }
  184. else {
  185. this.set('hostsError', null);
  186. }
  187. },
  188. /**
  189. * Check hostnames after Submit was clicked or <code>hostNames</code> were changed
  190. * @method checkHostAfterSubmitHandler
  191. */
  192. checkHostAfterSubmitHandler: function () {
  193. if (this.get('hasSubmitted')) {
  194. this.checkHostError();
  195. }
  196. }.observes('hasSubmitted', 'hostNames'),
  197. /**
  198. * Get host info, which will be saved in parent controller
  199. * @method getHostInfo
  200. */
  201. getHostInfo: function () {
  202. var hostNameArr = this.get('hostNameArr');
  203. var hostInfo = {};
  204. for (var i = 0; i < hostNameArr.length; i++) {
  205. hostInfo[hostNameArr[i]] = {
  206. name: hostNameArr[i],
  207. installType: this.get('installType'),
  208. bootStatus: 'PENDING',
  209. isInstalled: false
  210. };
  211. }
  212. return hostInfo;
  213. },
  214. /**
  215. * Used to set sshKey from FileUploader
  216. * @method setSshKey
  217. * @param {string} sshKey
  218. */
  219. setSshKey: function (sshKey) {
  220. this.set("content.installOptions.sshKey", sshKey);
  221. },
  222. /**
  223. * Onclick handler for <code>next button</code>. Do all UI work except data saving.
  224. * This work is doing by router.
  225. * @method evaluateStep
  226. * @return {bool}
  227. */
  228. evaluateStep: function () {
  229. console.log('TRACE: Entering controller:WizardStep2:evaluateStep function');
  230. if (this.get('isSubmitDisabled')) {
  231. return false;
  232. }
  233. this.set('hasSubmitted', true);
  234. this.checkHostError();
  235. this.updateHostNameArr();
  236. if (!this.get('hostNameArr.length')) {
  237. this.set('hostsError', Em.I18n.t('installer.step2.hostName.error.already_installed'));
  238. }
  239. if (this.get('hostsError') || this.get('sshUserError') || this.get('sshKeyError')) {
  240. return false;
  241. }
  242. if (this.get('isPattern')) {
  243. this.hostNamePatternPopup(this.get('hostNameArr'));
  244. return false;
  245. }
  246. if (this.get('inputtedAgainHostNames.length')) {
  247. this.installedHostsPopup();
  248. }
  249. else {
  250. this.proceedNext();
  251. }
  252. return true;
  253. },
  254. /**
  255. * check is there a pattern expression in host name textarea
  256. * push hosts that match pattern in hostNamesArr
  257. * @method parseHostNamesAsPatternExpression
  258. */
  259. parseHostNamesAsPatternExpression: function () {
  260. this.set('isPattern', false);
  261. var self = this;
  262. var hostNames = [];
  263. $.each(this.get('hostNameArr'), function (e, a) {
  264. var start, end, extra = {0: ""};
  265. if (/\[\d*\-\d*\]/.test(a)) {
  266. start = a.match(/\[\d*/);
  267. end = a.match(/\-\d*]/);
  268. start = start[0].substr(1);
  269. end = end[0].substr(1);
  270. if (parseInt(start) <= parseInt(end, 10) && parseInt(start, 10) >= 0) {
  271. self.set('isPattern', true);
  272. if (start[0] == "0" && start.length > 1) {
  273. extra = start.match(/0*/);
  274. }
  275. for (var i = parseInt(start, 10); i < parseInt(end, 10) + 1; i++) {
  276. hostNames.push(a.replace(/\[\d*\-\d*\]/, extra[0].substring(0, start.length - i.toString().length) + i))
  277. }
  278. } else {
  279. hostNames.push(a);
  280. }
  281. } else {
  282. hostNames.push(a);
  283. }
  284. });
  285. this.set('hostNameArr', hostNames);
  286. },
  287. /**
  288. * launch hosts to bootstrap
  289. * and save already registered hosts
  290. * @method proceedNext
  291. * @return {bool}
  292. */
  293. proceedNext: function (warningConfirmed) {
  294. if (this.isAllHostNamesValid() !== true && !warningConfirmed) {
  295. this.warningPopup();
  296. return false;
  297. }
  298. if (this.get('manualInstall') === true) {
  299. this.manualInstallPopup();
  300. return false;
  301. }
  302. if (App.get('skipBootstrap')) {
  303. this.saveHosts();
  304. App.router.send('next');
  305. return true;
  306. }
  307. this.setupBootStrap();
  308. return true;
  309. },
  310. /**
  311. * setup bootstrap data and completion callback for bootstrap call
  312. * @method setupBootStrap
  313. */
  314. setupBootStrap: function () {
  315. var self = this;
  316. var bootStrapData = JSON.stringify({'verbose': true, 'sshKey': this.get('sshKey'), 'hosts': this.get('hostNameArr'), 'user': this.get('sshUser')});
  317. App.router.get(this.get('content.controllerName')).launchBootstrap(bootStrapData, function (requestId) {
  318. if (requestId == '0') {
  319. var controller = App.router.get(App.clusterStatus.wizardControllerName);
  320. controller.registerErrPopup(Em.I18n.t('common.information'), Em.I18n.t('installer.step2.evaluateStep.hostRegInProgress'));
  321. } else if (requestId) {
  322. self.set('content.installOptions.bootRequestId', requestId);
  323. self.saveHosts();
  324. App.router.send('next');
  325. }
  326. });
  327. },
  328. /**
  329. * show warning for host names without dots or IP addresses
  330. * @return {App.ModalPopup}
  331. * @method warningPopup
  332. */
  333. warningPopup: function () {
  334. var self = this;
  335. return App.ModalPopup.show({
  336. header: Em.I18n.t('common.warning'),
  337. onPrimary: function () {
  338. this.hide();
  339. self.proceedNext(true);
  340. },
  341. bodyClass: Em.View.extend({
  342. template: Em.Handlebars.compile(Em.I18n.t('installer.step2.warning.popup.body').format(self.get('invalidHostNames').join(', ')))
  343. })
  344. });
  345. },
  346. /**
  347. * show popup with the list of hosts that are already part of the cluster
  348. * @return {App.ModalPopup}
  349. * @method installedHostsPopup
  350. */
  351. installedHostsPopup: function () {
  352. var self = this;
  353. return App.ModalPopup.show({
  354. header: Em.I18n.t('common.warning'),
  355. onPrimary: function () {
  356. self.proceedNext();
  357. this.hide();
  358. },
  359. bodyClass: Em.View.extend({
  360. inputtedAgainHostNames: function () {
  361. return self.get('inputtedAgainHostNames').join(', ');
  362. }.property(),
  363. templateName: require('templates/wizard/step2_installed_hosts_popup')
  364. })
  365. });
  366. },
  367. /**
  368. * Show popup with hosts generated by pattern
  369. * @method hostNamePatternPopup
  370. * @param {string[]} hostNames
  371. * @return {App.ModalPopup}
  372. */
  373. hostNamePatternPopup: function (hostNames) {
  374. var self = this;
  375. return App.ModalPopup.show({
  376. header: Em.I18n.t('installer.step2.hostName.pattern.header'),
  377. onPrimary: function () {
  378. self.proceedNext();
  379. this.hide();
  380. },
  381. bodyClass: Em.View.extend({
  382. templateName: require('templates/common/items_list_popup'),
  383. items: hostNames,
  384. insertedItems: [],
  385. didInsertElement: function () {
  386. lazyloading.run({
  387. destination: this.get('insertedItems'),
  388. source: this.get('items'),
  389. context: this,
  390. initSize: 100,
  391. chunkSize: 500,
  392. delay: 100
  393. });
  394. }
  395. })
  396. });
  397. },
  398. /**
  399. * Show notify that installation is manual
  400. * save hosts
  401. * @return {App.ModalPopup}
  402. * @method manualInstallPopup
  403. */
  404. manualInstallPopup: function () {
  405. var self = this;
  406. return App.ModalPopup.show({
  407. header: Em.I18n.t('installer.step2.manualInstall.popup.header'),
  408. onPrimary: function () {
  409. this.hide();
  410. self.saveHosts();
  411. App.router.send('next');
  412. },
  413. bodyClass: Em.View.extend({
  414. templateName: require('templates/wizard/step2ManualInstallPopup')
  415. })
  416. });
  417. },
  418. /**
  419. * Warn to manually install ambari-agent on each host
  420. * @method manualInstallWarningPopup
  421. */
  422. manualInstallWarningPopup: function () {
  423. if (this.get('isSSHRegistrationEnabled') && !this.get('content.installOptions.useSsh')) {
  424. App.ModalPopup.show({
  425. header: Em.I18n.t('common.warning'),
  426. body: Em.I18n.t('installer.step2.manualInstall.info'),
  427. encodeBody: false,
  428. secondary: null
  429. });
  430. }
  431. this.set('content.installOptions.manualInstall', !this.get('content.installOptions.useSsh'));
  432. }.observes('content.installOptions.useSsh'),
  433. /**
  434. * Load java.home value frin server
  435. * @method setAmbariJavaHome
  436. */
  437. setAmbariJavaHome: function () {
  438. App.ajax.send({
  439. name: 'ambari.service',
  440. sender: this,
  441. success: 'onGetAmbariJavaHomeSuccess',
  442. error: 'onGetAmbariJavaHomeError'
  443. });
  444. },
  445. /**
  446. * Set received java.home value
  447. * @method onGetAmbariJavaHomeSuccess
  448. * @param {Object} data
  449. */
  450. onGetAmbariJavaHomeSuccess: function (data) {
  451. this.set('content.installOptions.javaHome', data.RootServiceComponents.properties['java.home']);
  452. },
  453. /**
  454. * Set default java.home value
  455. * @method onGetAmbariJavaHomeError
  456. */
  457. onGetAmbariJavaHomeError: function () {
  458. console.warn('can\'t get java.home value from server');
  459. this.set('content.installOptions.javaHome', App.get('defaultJavaHome'));
  460. },
  461. /**
  462. * Save hosts info and proceed to the next step
  463. * @method saveHosts
  464. */
  465. saveHosts: function () {
  466. var hosts = this.get('content.hosts');
  467. //add previously installed hosts
  468. for (var hostName in hosts) {
  469. if (!hosts[hostName].isInstalled) {
  470. delete hosts[hostName];
  471. }
  472. }
  473. this.set('content.hosts', $.extend(hosts, this.getHostInfo()));
  474. this.setAmbariJavaHome();
  475. }
  476. });