cluster_states.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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.clusterStatus = Ember.Object.create({
  20. clusterName: '',
  21. validStates: [
  22. 'DEFAULT',
  23. 'CLUSTER_NOT_CREATED_1',
  24. 'CLUSTER_DEPLOY_PREP_2',
  25. 'CLUSTER_INSTALLING_3',
  26. 'SERVICE_STARTING_3',
  27. 'CLUSTER_INSTALLED_4',
  28. 'ADD_HOSTS_DEPLOY_PREP_2',
  29. 'ADD_HOSTS_INSTALLING_3',
  30. 'ADD_HOSTS_INSTALLED_4',
  31. 'ADD_SERVICES_DEPLOY_PREP_2',
  32. 'ADD_SERVICES_INSTALLING_3',
  33. 'ADD_SERVICES_INSTALLED_4',
  34. 'STOPPING_SERVICES',
  35. 'STACK_UPGRADING',
  36. 'STACK_UPGRADE_FAILED',
  37. 'STACK_UPGRADED',
  38. 'ADD_SECURITY_STEP_1',
  39. 'ADD_SECURITY_STEP_2',
  40. 'ADD_SECURITY_STEP_3',
  41. 'ADD_SECURITY_STEP_4',
  42. 'DISABLE_SECURITY',
  43. 'HIGH_AVAILABILITY_DEPLOY',
  44. 'ROLLBACK_HIGH_AVAILABILITY'],
  45. clusterState: 'CLUSTER_NOT_CREATED_1',
  46. wizardControllerName: null,
  47. localdb: null,
  48. key: 'CLUSTER_CURRENT_STATUS',
  49. isInstalled: function(){
  50. var notInstalledStates = ['CLUSTER_NOT_CREATED_1', 'CLUSTER_DEPLOY_PREP_2', 'CLUSTER_INSTALLING_3', 'SERVICE_STARTING_3'];
  51. return !notInstalledStates.contains(this.get('clusterState'));
  52. }.property('clusterState'),
  53. /**
  54. * get cluster data from server and update cluster status
  55. * @param {Boolean} isAsync set this to true if the call is to be made asynchronously. if unspecified, false is assumed
  56. * @param {Boolean} overrideLocaldb
  57. * @return promise object for the get call
  58. */
  59. updateFromServer: function(isAsync, overrideLocaldb) {
  60. // if isAsync is undefined, set it to false
  61. isAsync = isAsync || false;
  62. // if overrideLocaldb is undefined, set it to true
  63. if(typeof overrideLocaldb == "undefined"){
  64. overrideLocaldb = true;
  65. }
  66. var user = App.db.getUser();
  67. var login = App.db.getLoginName();
  68. var url = App.apiPrefix + '/persist/' + this.get('key');
  69. return jQuery.ajax(
  70. {
  71. url: url,
  72. context: this,
  73. async: isAsync,
  74. success: function (response) {
  75. if (response) {
  76. var newValue = jQuery.parseJSON(response);
  77. if (newValue.clusterState) {
  78. this.set('clusterState', newValue.clusterState);
  79. }
  80. if (newValue.clusterName) {
  81. this.set('clusterName', newValue.clusterName);
  82. }
  83. if (newValue.wizardControllerName) {
  84. this.set('wizardControllerName', newValue.wizardControllerName);
  85. }
  86. if (newValue.localdb) {
  87. this.set('localdb', newValue.localdb);
  88. // restore HAWizard data if process was started
  89. var isHAWizardStarted = App.get('isAdmin') && !App.isEmptyObject(newValue.localdb.HighAvailabilityWizard);
  90. if (overrideLocaldb || isHAWizardStarted) {
  91. App.db.data = newValue.localdb;
  92. App.db.setLocalStorage();
  93. App.db.setUser(user);
  94. App.db.setLoginName(login);
  95. }
  96. }
  97. } else {
  98. // default status already set
  99. }
  100. // this is to ensure that the local storage namespaces are initialized with all expected namespaces.
  101. // after upgrading ambari, loading local storage data from the "persist" data saved via an older version of
  102. // Ambari can result in missing namespaces that are defined in the new version of Ambari.
  103. App.db.mergeStorage();
  104. },
  105. error: function (xhr) {
  106. if (xhr.status == 404) {
  107. // default status already set
  108. console.log('Persist API did NOT find the key CLUSTER_CURRENT_STATUS');
  109. return;
  110. }
  111. App.ModalPopup.show({
  112. header: Em.I18n.t('common.error'),
  113. secondary: false,
  114. bodyClass: Ember.View.extend({
  115. template: Ember.Handlebars.compile('<p>{{t common.update.error}}</p>')
  116. })
  117. });
  118. },
  119. statusCode: require('data/statusCodes')
  120. }
  121. );
  122. },
  123. /**
  124. * update cluster status and post it on server
  125. * @param newValue
  126. * @return {*}
  127. */
  128. setClusterStatus: function(newValue){
  129. if(App.testMode) return false;
  130. var user = App.db.getUser();
  131. var login = App.db.getLoginName();
  132. var val = {clusterName: this.get('clusterName')};
  133. if (newValue) {
  134. //setter
  135. if (newValue.clusterName) {
  136. this.set('clusterName', newValue.clusterName);
  137. val.clusterName = newValue.clusterName;
  138. }
  139. if (newValue.clusterState) {
  140. this.set('clusterState', newValue.clusterState);
  141. val.clusterState = newValue.clusterState;
  142. }
  143. if (newValue.wizardControllerName) {
  144. this.set('wizardControllerName', newValue.wizardControllerName);
  145. val.wizardControllerName = newValue.wizardControllerName;
  146. }
  147. if (newValue.localdb) {
  148. if (newValue.localdb.app && newValue.localdb.app.user)
  149. delete newValue.localdb.app.user;
  150. if (newValue.localdb.app && newValue.localdb.app.loginName)
  151. delete newValue.localdb.app.loginName;
  152. this.set('localdb', newValue.localdb);
  153. val.localdb = newValue.localdb;
  154. } else {
  155. delete App.db.data.app.user;
  156. delete App.db.data.app.loginName;
  157. val.localdb = App.db.data;
  158. App.db.setUser(user);
  159. App.db.setLoginName(login);
  160. }
  161. var keyValuePair = {};
  162. keyValuePair[this.get('key')] = JSON.stringify(val);
  163. App.ajax.send({
  164. name: 'cluster.state',
  165. sender: this,
  166. data: {
  167. keyValuePair: keyValuePair
  168. },
  169. beforeSend: 'clusterStatusBeforeSend',
  170. error: 'clusterStatusErrorCallBack'
  171. });
  172. return newValue;
  173. }
  174. },
  175. clusterStatusBeforeSend: function (keyValuePair) {
  176. console.log('BeforeSend: persistKeyValues', keyValuePair);
  177. },
  178. clusterStatusErrorCallBack: function(request, ajaxOptions, error, opt) {
  179. console.log("ERROR");
  180. var msg, doc;
  181. try {
  182. msg = 'Error ' + (request.status) + ' ';
  183. doc = $.parseXML(request.responseText);
  184. msg += $(doc).find("body p").text();
  185. } catch (e) {
  186. msg += JSON.parse(request.responseText).message;
  187. }
  188. App.ModalPopup.show({
  189. header: Em.I18n.t('common.error'),
  190. secondary: false,
  191. response: msg,
  192. bodyClass: Ember.View.extend({
  193. template: Ember.Handlebars.compile('<p>{{t common.persist.error}} {{response}}</p>')
  194. })
  195. });
  196. },
  197. /**
  198. * general info about cluster
  199. */
  200. value: function () {
  201. return {
  202. clusterName: this.get('clusterName'),
  203. clusterState: this.get('clusterState'),
  204. wizardControllerName: this.get('wizardControllerName'),
  205. localdb: this.get('localdb')
  206. };
  207. }.property('clusterName', 'clusterState', 'localdb', 'wizardControllerName')
  208. });