step2_controller.js 15 KB

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