step2_controller.js 15 KB

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