step2_controller.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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. console.log('TRACE: Entering controller:WizardStep2:evaluateStep function');
  264. if (this.get('isSubmitDisabled')) {
  265. return false;
  266. }
  267. this.set('hasSubmitted', true);
  268. this.checkHostError();
  269. this.updateHostNameArr();
  270. if (!this.get('hostNameArr.length')) {
  271. this.set('hostsError', Em.I18n.t('installer.step2.hostName.error.already_installed'));
  272. }
  273. if (this.get('hostsError') || this.get('sshUserError') || this.get('sshPortError') || this.get('agentUserError') || this.get('sshKeyError')) {
  274. return false;
  275. }
  276. if (this.get('isPattern')) {
  277. this.hostNamePatternPopup(this.get('hostNameArr'));
  278. return false;
  279. }
  280. if (this.get('inputtedAgainHostNames.length')) {
  281. this.installedHostsPopup();
  282. }
  283. else {
  284. this.proceedNext();
  285. }
  286. return true;
  287. },
  288. /**
  289. * check is there a pattern expression in host name textarea
  290. * push hosts that match pattern in hostNamesArr
  291. * @method parseHostNamesAsPatternExpression
  292. */
  293. parseHostNamesAsPatternExpression: function () {
  294. this.set('isPattern', false);
  295. var self = this;
  296. var hostNames = [];
  297. $.each(this.get('hostNameArr'), function (e, a) {
  298. var start, end, extra = {0: ""};
  299. if (/\[\d*\-\d*\]/.test(a)) {
  300. start = a.match(/\[\d*/);
  301. end = a.match(/\-\d*]/);
  302. start = start[0].substr(1);
  303. end = end[0].substr(1);
  304. if (parseInt(start) <= parseInt(end, 10) && parseInt(start, 10) >= 0) {
  305. self.set('isPattern', true);
  306. if (start[0] == "0" && start.length > 1) {
  307. extra = start.match(/0*/);
  308. }
  309. for (var i = parseInt(start, 10); i < parseInt(end, 10) + 1; i++) {
  310. hostNames.push(a.replace(/\[\d*\-\d*\]/, extra[0].substring(0, start.length - i.toString().length) + i))
  311. }
  312. } else {
  313. hostNames.push(a);
  314. }
  315. } else {
  316. hostNames.push(a);
  317. }
  318. });
  319. this.set('hostNameArr', hostNames.uniq());
  320. },
  321. /**
  322. * launch hosts to bootstrap
  323. * and save already registered hosts
  324. * @method proceedNext
  325. * @return {bool}
  326. */
  327. proceedNext: function (warningConfirmed) {
  328. if (this.isAllHostNamesValid() !== true && !warningConfirmed) {
  329. this.warningPopup();
  330. return false;
  331. }
  332. if (this.get('manualInstall') === true) {
  333. this.manualInstallPopup();
  334. return false;
  335. }
  336. this.saveHosts();
  337. App.router.send('next');
  338. return true;
  339. },
  340. /**
  341. * show warning for host names without dots or IP addresses
  342. * @return {App.ModalPopup}
  343. * @method warningPopup
  344. */
  345. warningPopup: function () {
  346. var self = this;
  347. return App.ModalPopup.show({
  348. header: Em.I18n.t('common.warning'),
  349. onPrimary: function () {
  350. this.hide();
  351. self.proceedNext(true);
  352. },
  353. bodyClass: Em.View.extend({
  354. template: Em.Handlebars.compile(Em.I18n.t('installer.step2.warning.popup.body').format(self.get('invalidHostNames').join(', ')))
  355. })
  356. });
  357. },
  358. /**
  359. * show popup with the list of hosts that are already part of the cluster
  360. * @return {App.ModalPopup}
  361. * @method installedHostsPopup
  362. */
  363. installedHostsPopup: function () {
  364. var self = this;
  365. return App.ModalPopup.show({
  366. header: Em.I18n.t('common.warning'),
  367. onPrimary: function () {
  368. self.proceedNext();
  369. this.hide();
  370. },
  371. bodyClass: Em.View.extend({
  372. inputtedAgainHostNames: function () {
  373. return self.get('inputtedAgainHostNames').join(', ');
  374. }.property(),
  375. templateName: require('templates/wizard/step2_installed_hosts_popup')
  376. })
  377. });
  378. },
  379. /**
  380. * Show popup with hosts generated by pattern
  381. * @method hostNamePatternPopup
  382. * @param {string[]} hostNames
  383. * @return {App.ModalPopup}
  384. */
  385. hostNamePatternPopup: function (hostNames) {
  386. var self = this;
  387. return App.ModalPopup.show({
  388. header: Em.I18n.t('installer.step2.hostName.pattern.header'),
  389. onPrimary: function () {
  390. self.proceedNext();
  391. this.hide();
  392. },
  393. bodyClass: Em.View.extend({
  394. templateName: require('templates/common/items_list_popup'),
  395. items: hostNames,
  396. insertedItems: [],
  397. didInsertElement: function () {
  398. lazyloading.run({
  399. destination: this.get('insertedItems'),
  400. source: this.get('items'),
  401. context: this,
  402. initSize: 100,
  403. chunkSize: 500,
  404. delay: 100
  405. });
  406. }
  407. })
  408. });
  409. },
  410. /**
  411. * Show notify that installation is manual
  412. * save hosts
  413. * @return {App.ModalPopup}
  414. * @method manualInstallPopup
  415. */
  416. manualInstallPopup: function () {
  417. var self = this;
  418. return App.ModalPopup.show({
  419. header: Em.I18n.t('installer.step2.manualInstall.popup.header'),
  420. onPrimary: function () {
  421. this.hide();
  422. self.saveHosts();
  423. App.router.send('next');
  424. },
  425. bodyClass: Em.View.extend({
  426. templateName: require('templates/wizard/step2ManualInstallPopup')
  427. })
  428. });
  429. },
  430. /**
  431. * Warn to manually install ambari-agent on each host
  432. * @method manualInstallWarningPopup
  433. */
  434. manualInstallWarningPopup: function () {
  435. if (!this.get('content.installOptions.useSsh')) {
  436. App.ModalPopup.show({
  437. header: Em.I18n.t('common.warning'),
  438. body: Em.I18n.t('installer.step2.manualInstall.info'),
  439. encodeBody: false,
  440. secondary: null
  441. });
  442. }
  443. this.set('content.installOptions.manualInstall', !this.get('content.installOptions.useSsh'));
  444. }.observes('content.installOptions.useSsh'),
  445. /**
  446. * Load java.home value frin server
  447. * @method setAmbariJavaHome
  448. */
  449. setAmbariJavaHome: function () {
  450. App.ajax.send({
  451. name: 'ambari.service',
  452. sender: this,
  453. success: 'onGetAmbariJavaHomeSuccess',
  454. error: 'onGetAmbariJavaHomeError'
  455. });
  456. },
  457. /**
  458. * Set received java.home value
  459. * @method onGetAmbariJavaHomeSuccess
  460. * @param {Object} data
  461. */
  462. onGetAmbariJavaHomeSuccess: function (data) {
  463. this.set('content.installOptions.javaHome', data.RootServiceComponents.properties['java.home']);
  464. },
  465. /**
  466. * Set default java.home value
  467. * @method onGetAmbariJavaHomeError
  468. */
  469. onGetAmbariJavaHomeError: function () {
  470. console.warn('can\'t get java.home value from server');
  471. this.set('content.installOptions.javaHome', App.get('defaultJavaHome'));
  472. },
  473. /**
  474. * Save hosts info and proceed to the next step
  475. * @method saveHosts
  476. */
  477. saveHosts: function () {
  478. var hosts = this.get('content.hosts');
  479. //add previously installed hosts
  480. for (var hostName in hosts) {
  481. if (!hosts[hostName].isInstalled) {
  482. delete hosts[hostName];
  483. }
  484. }
  485. this.set('content.hosts', $.extend(hosts, this.getHostInfo()));
  486. this.setAmbariJavaHome();
  487. }
  488. });