step2_controller.js 14 KB

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