step2_controller.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. App.WizardStep2Controller = Em.Controller.extend({
  20. name: 'wizardStep2Controller',
  21. hostNameArr: [],
  22. bootRequestId: null,
  23. hasSubmitted: false,
  24. hostNames: function () {
  25. return this.get('content.hostNames');
  26. }.property('content.hostNames'),
  27. manualInstall: function () {
  28. return this.get('content.manualInstall');
  29. }.property('content.manualInstall'),
  30. localRepo: function () {
  31. return this.get('content.localRepo');
  32. }.property('content.localRepo'),
  33. localRepoPath: function () {
  34. return this.get('content.localRepoPath');
  35. }.property('content.localRepoPath'),
  36. sshKey: function () {
  37. return this.get('content.sshKey');
  38. }.property('content.sshKey'),
  39. passphrase: function () {
  40. return this.get('content.passphrase');
  41. }.property('content.passphrase'),
  42. confirmPassphrase: function () {
  43. return this.get('content.confirmPassphrase');
  44. }.property('content.confirmPassphrase'),
  45. installType: function () {
  46. if (this.get('manualInstall') === true) {
  47. return 'manualDriven';
  48. } else {
  49. return 'ambariDriven';
  50. }
  51. }.property('manualInstall'),
  52. isHostNameValid: function (hostname) {
  53. // For now hostnames that start or end with '-' are not allowed
  54. return !(/^\-/.test(hostname) || /\-$/.test(hostname));
  55. },
  56. isAllHostNamesValid: function () {
  57. this.hostNameArr = this.get('hostNames').trim().split(new RegExp("\\s+", "g"));
  58. for (var index in this.hostNameArr) {
  59. if (!this.isHostNameValid(this.hostNameArr[index])) {
  60. return false;
  61. }
  62. }
  63. return true;
  64. },
  65. hostsError: function () {
  66. if (this.get('hasSubmitted') && this.get('hostNames').trim() === '') {
  67. return Em.I18n.t('installer.step2.hostName.error.required');
  68. } else if (this.isAllHostNamesValid() === false) {
  69. return Em.I18n.t('installer.step2.hostName.error.invalid');
  70. }
  71. return null;
  72. }.property('hostNames', 'manualInstall', 'hasSubmitted'),
  73. sshKeyError: function () {
  74. if (this.get('hasSubmitted') && this.get('manualInstall') === false && this.get('sshKey').trim() === '') {
  75. return Em.I18n.t('installer.step2.sshKey.error.required');
  76. }
  77. return null;
  78. }.property('sshKey', 'manualInstall', 'hasSubmitted'),
  79. localRepoError: function () {
  80. if (
  81. // (typeof this.get('localRepoPath') === 'undefined') ||
  82. !(/^([-a-z0-9._\/]|%[0-9a-f]{2})*$/i.test(this.get('localRepoPath')) ) ||
  83. (this.get('hasSubmitted') && this.get('localRepo') && this.get('localRepoPath').trim() === '' )
  84. )
  85. {
  86. return Em.I18n.t('installer.step2.localRepo.error.required');
  87. }
  88. return null;
  89. }.property('localRepo', 'localRepoPath', 'hasSubmitted'),
  90. /**
  91. * Get host info, which will be saved in parent controller
  92. */
  93. getHostInfo: function () {
  94. var hostNameArr = this.get('hostNameArr');
  95. var hostInfo = {};
  96. for (var i = 0; i < hostNameArr.length; i++) {
  97. hostInfo[hostNameArr[i]] = {
  98. name: hostNameArr[i],
  99. installType: this.get('installType'),
  100. bootStatus: 'pending'
  101. };
  102. }
  103. return hostInfo;
  104. },
  105. /**
  106. * Onclick handler for <code>next button</code>. Do all UI work except data saving.
  107. * This work is doing by router.
  108. * @return {Boolean}
  109. */
  110. evaluateStep: function () {
  111. console.log('TRACE: Entering controller:WizardStep2:evaluateStep function');
  112. if (this.get('isSubmitDisabled')) {
  113. return false;
  114. }
  115. if (this.get('manualInstall') === true) {
  116. this.manualInstallPopup();
  117. return false;
  118. }
  119. var bootStrapData = JSON.stringify({'verbose': true, 'sshKey': this.get('sshKey'), hosts: this.get('hostNameArr')});
  120. if (App.skipBootstrap) {
  121. App.router.send('next');
  122. return true;
  123. }
  124. var self = this;
  125. var method = App.testMode ? 'GET' : 'POST';
  126. var url = App.testMode ? '/data/wizard/bootstrap/bootstrap.json' : App.apiPrefix + '/bootstrap';
  127. $.ajax({
  128. type: method,
  129. url: url,
  130. data: bootStrapData,
  131. timeout: App.timeout,
  132. contentType: 'application/json',
  133. success: function (data) {
  134. console.log("TRACE: POST bootstrap succeeded");
  135. var requestId = data.requestId;
  136. self.set('bootRequestId',requestId);
  137. App.router.send('next');
  138. },
  139. error: function () {
  140. console.log("ERROR: POST bootstrap failed");
  141. alert('Bootstrap call failed. Please try again.');
  142. }
  143. });
  144. },
  145. manualInstallPopup: function (event) {
  146. App.ModalPopup.show({
  147. header: Em.I18n.t('installer.step2.manualInstall.popup.header'),
  148. onPrimary: function () {
  149. this.hide();
  150. App.router.send('next');
  151. },
  152. bodyClass: Ember.View.extend({
  153. templateName: require('templates/wizard/step2ManualInstallPopup')
  154. })
  155. });
  156. },
  157. isSubmitDisabled: function () {
  158. return (this.get('hostsError') || this.get('sshKeyError') || this.get('localRepoError'));
  159. }.property('hostsError', 'sshKeyError', 'localRepoError')
  160. });