step2_controller.js 5.4 KB

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