step2_controller.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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.isEmpty(this.get('sshKey').trim())) {
  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.isEmpty(this.get('sshUser').trim())) {
  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.isEmpty(this.get('sshPort').trim() )) {
  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.isEmpty(this.get('agentUser').trim())) {
  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 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.uniq());
  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('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. this.set('content.installOptions.javaHome', App.get('defaultJavaHome'));
  454. },
  455. /**
  456. * Save hosts info and proceed to the next step
  457. * @method saveHosts
  458. */
  459. saveHosts: function () {
  460. var hosts = this.get('content.hosts');
  461. //add previously installed hosts
  462. for (var hostName in hosts) {
  463. if (!hosts[hostName].isInstalled) {
  464. delete hosts[hostName];
  465. }
  466. }
  467. this.set('content.hosts', $.extend(hosts, this.getHostInfo()));
  468. this.setAmbariJavaHome();
  469. }
  470. });