step2_controller.js 14 KB

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