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