cluster_states.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 isAsync: set this to true if the call is to be made asynchronously. if unspecified, false is assumed
  56. * @return promise object for the get call
  57. */
  58. updateFromServer: function(isAsync, overrideLocaldb) {
  59. // if isAsync is undefined, set it to false
  60. isAsync = isAsync || false;
  61. // if overrideLocaldb is undefined, set it to true
  62. if(typeof overrideLocaldb == "undefined"){
  63. overrideLocaldb = true;
  64. }
  65. var user = App.db.getUser();
  66. var login = App.db.getLoginName();
  67. var url = App.apiPrefix + '/persist/' + this.get('key');
  68. return jQuery.ajax(
  69. {
  70. url: url,
  71. context: this,
  72. async: isAsync,
  73. success: function (response) {
  74. if (response) {
  75. var newValue = jQuery.parseJSON(response);
  76. if (newValue.clusterState) {
  77. this.set('clusterState', newValue.clusterState);
  78. }
  79. if (newValue.clusterName) {
  80. this.set('clusterName', newValue.clusterName);
  81. }
  82. if (newValue.wizardControllerName) {
  83. this.set('wizardControllerName', newValue.wizardControllerName);
  84. }
  85. if (newValue.localdb) {
  86. this.set('localdb', newValue.localdb);
  87. if (overrideLocaldb) {
  88. App.db.data = newValue.localdb;
  89. App.db.setLocalStorage();
  90. App.db.setUser(user);
  91. App.db.setLoginName(login);
  92. }
  93. }
  94. } else {
  95. // default status already set
  96. }
  97. // this is to ensure that the local storage namespaces are initialized with all expected namespaces.
  98. // after upgrading ambari, loading local storage data from the "persist" data saved via an older version of
  99. // Ambari can result in missing namespaces that are defined in the new version of Ambari.
  100. App.db.mergeStorage();
  101. },
  102. error: function (xhr) {
  103. if (xhr.status == 404) {
  104. // default status already set
  105. console.log('Persist API did NOT find the key CLUSTER_CURRENT_STATUS');
  106. return;
  107. }
  108. App.ModalPopup.show({
  109. header: Em.I18n.t('common.error'),
  110. secondary: false,
  111. bodyClass: Ember.View.extend({
  112. template: Ember.Handlebars.compile('<p>{{t common.update.error}}</p>')
  113. })
  114. });
  115. },
  116. statusCode: require('data/statusCodes')
  117. }
  118. );
  119. },
  120. /**
  121. * update cluster status and post it on server
  122. * @param newValue
  123. * @return {*}
  124. */
  125. setClusterStatus: function(newValue){
  126. if(App.testMode) return false;
  127. var user = App.db.getUser();
  128. var login = App.db.getLoginName();
  129. var val = {clusterName: this.get('clusterName')};
  130. if (newValue) {
  131. //setter
  132. if (newValue.clusterName) {
  133. this.set('clusterName', newValue.clusterName);
  134. val.clusterName = newValue.clusterName;
  135. }
  136. if (newValue.clusterState) {
  137. this.set('clusterState', newValue.clusterState);
  138. val.clusterState = newValue.clusterState;
  139. }
  140. if (newValue.wizardControllerName) {
  141. this.set('wizardControllerName', newValue.wizardControllerName);
  142. val.wizardControllerName = newValue.wizardControllerName;
  143. }
  144. if (newValue.localdb) {
  145. if (newValue.localdb.app && newValue.localdb.app.user)
  146. delete newValue.localdb.app.user;
  147. if (newValue.localdb.app && newValue.localdb.app.loginName)
  148. delete newValue.localdb.app.loginName;
  149. this.set('localdb', newValue.localdb);
  150. val.localdb = newValue.localdb;
  151. } else {
  152. delete App.db.data.app.user;
  153. delete App.db.data.app.loginName;
  154. val.localdb = App.db.data;
  155. App.db.setUser(user);
  156. App.db.setLoginName(login);
  157. }
  158. var keyValuePair = {};
  159. keyValuePair[this.get('key')] = JSON.stringify(val);
  160. App.ajax.send({
  161. name: 'cluster.state',
  162. sender: this,
  163. data: {
  164. keyValuePair: keyValuePair
  165. },
  166. beforeSend: 'clusterStatusBeforeSend',
  167. error: 'clusterStatusErrorCallBack'
  168. });
  169. return newValue;
  170. }
  171. },
  172. clusterStatusBeforeSend: function (keyValuePair) {
  173. console.log('BeforeSend: persistKeyValues', keyValuePair);
  174. },
  175. clusterStatusErrorCallBack: function(request, ajaxOptions, error, opt) {
  176. console.log("ERROR");
  177. if(opt.newValue.errorCallBack) {
  178. opt.newValue.errorCallBack();
  179. } else {
  180. var doc = $.parseXML(request.responseText);
  181. var msg = 'Error ' + (request.status) + ' ';
  182. msg += $(doc).find("body p").text();
  183. }
  184. App.ModalPopup.show({
  185. header: Em.I18n.t('common.error'),
  186. secondary: false,
  187. response: msg,
  188. bodyClass: Ember.View.extend({
  189. template: Ember.Handlebars.compile('<p>{{t common.persist.error}} {{response}}</p>')
  190. })
  191. });
  192. },
  193. /**
  194. * general info about cluster
  195. */
  196. value: function () {
  197. return {
  198. clusterName: this.get('clusterName'),
  199. clusterState: this.get('clusterState'),
  200. wizardControllerName: this.get('wizardControllerName'),
  201. localdb: this.get('localdb')
  202. };
  203. }.property('clusterName', 'clusterState', 'localdb', 'wizardControllerName')
  204. });