step2_controller.js 15 KB

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