step2_controller.js 14 KB

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