cluster_states.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. require('mixins/common/userPref');
  20. App.clusterStatus = Em.Object.create(App.UserPref, {
  21. /**
  22. * Cluster name
  23. * @type {string}
  24. */
  25. clusterName: '',
  26. /**
  27. * List valid cluster states
  28. * @type {string[]}
  29. */
  30. validStates: [
  31. 'DEFAULT',
  32. 'CLUSTER_NOT_CREATED_1',
  33. 'CLUSTER_DEPLOY_PREP_2',
  34. 'CLUSTER_INSTALLING_3',
  35. 'SERVICE_STARTING_3',
  36. 'CLUSTER_INSTALLED_4',
  37. 'ADD_HOSTS_DEPLOY_PREP_2',
  38. 'ADD_HOSTS_INSTALLING_3',
  39. 'ADD_HOSTS_INSTALLED_4',
  40. 'ADD_SERVICES_DEPLOY_PREP_2',
  41. 'ADD_SERVICES_INSTALLING_3',
  42. 'ADD_SERVICES_INSTALLED_4',
  43. 'STOPPING_SERVICES',
  44. 'STACK_UPGRADING',
  45. 'STACK_UPGRADE_FAILED',
  46. 'STACK_UPGRADED',
  47. 'ADD_SECURITY_STEP_1',
  48. 'ADD_SECURITY_STEP_2',
  49. 'ADD_SECURITY_STEP_3',
  50. 'ADD_SECURITY_STEP_4',
  51. 'DISABLE_SECURITY',
  52. 'HIGH_AVAILABILITY_DEPLOY',
  53. 'ROLLBACK_HIGH_AVAILABILITY'],
  54. /**
  55. * Default cluster state
  56. * @type {string}
  57. */
  58. clusterState: 'CLUSTER_NOT_CREATED_1',
  59. /**
  60. * Current used wizard <code>controller.name</code>
  61. * @type {string|null}
  62. */
  63. wizardControllerName: null,
  64. /**
  65. * Local DB
  66. * @type {object|null}
  67. */
  68. localdb: null,
  69. /**
  70. * Persist key
  71. * @type {string}
  72. */
  73. key: 'CLUSTER_CURRENT_STATUS',
  74. /**
  75. * Is cluster installed
  76. * @type {bool}
  77. */
  78. isInstalled: function () {
  79. var notInstalledStates = ['CLUSTER_NOT_CREATED_1', 'CLUSTER_DEPLOY_PREP_2', 'CLUSTER_INSTALLING_3', 'SERVICE_STARTING_3'];
  80. return !notInstalledStates.contains(this.get('clusterState'));
  81. }.property('clusterState'),
  82. /**
  83. * General info about cluster
  84. * @type {{clusterName: string, clusterState: string, wizardControllerName: string, localdb: object}}
  85. */
  86. value: function () {
  87. return {
  88. clusterName: this.get('clusterName'),
  89. clusterState: this.get('clusterState'),
  90. wizardControllerName: this.get('wizardControllerName'),
  91. localdb: this.get('localdb')
  92. };
  93. }.property('clusterName', 'clusterState', 'localdb', 'wizardControllerName'),
  94. /**
  95. * get cluster data from server and update cluster status
  96. * @param {bool} isAsync set this to true if the call is to be made asynchronously. if unspecified, false is assumed
  97. * @param {bool} overrideLocaldb
  98. * @return promise object for the get call
  99. * @method updateFromServer
  100. */
  101. updateFromServer: function (isAsync, overrideLocaldb) {
  102. // if isAsync is undefined, set it to false
  103. this.set('makeRequestAsync', isAsync || false);
  104. // if overrideLocaldb is undefined, set it to true
  105. this.set('additionalData', {
  106. user: App.db.getUser(),
  107. login: App.db.getLoginName(),
  108. overrideLocaldb: Em.isNone(overrideLocaldb) ? true : overrideLocaldb
  109. });
  110. return this.getUserPref(this.get('key'));
  111. },
  112. /**
  113. * Success callback for get-persist request
  114. * @param {object} response
  115. * @param {object} opt
  116. * @param {object} params
  117. * @method getUserPrefSuccessCallback
  118. */
  119. getUserPrefSuccessCallback: function (response, opt, params) {
  120. if (response) {
  121. if (response.clusterState) {
  122. this.set('clusterState', response.clusterState);
  123. }
  124. if (response.clusterName) {
  125. this.set('clusterName', response.clusterName);
  126. }
  127. if (response.wizardControllerName) {
  128. this.set('wizardControllerName', response.wizardControllerName);
  129. }
  130. if (response.localdb) {
  131. this.set('localdb', response.localdb);
  132. // restore HAWizard data if process was started
  133. var isHAWizardStarted = App.get('isAdmin') && !App.isEmptyObject(response.localdb.HighAvailabilityWizard);
  134. if (params.data.overrideLocaldb || isHAWizardStarted) {
  135. App.db.data = response.localdb;
  136. App.db.setLocalStorage();
  137. App.db.setUser(params.data.user);
  138. App.db.setLoginName(params.data.login);
  139. }
  140. }
  141. }
  142. // this is to ensure that the local storage namespaces are initialized with all expected namespaces.
  143. // after upgrading ambari, loading local storage data from the "persist" data saved via an older version of
  144. // Ambari can result in missing namespaces that are defined in the new version of Ambari.
  145. App.db.mergeStorage();
  146. },
  147. /**
  148. * Error callback for get-persist request
  149. * @param {object} request
  150. * @param {object} ajaxOptions
  151. * @param {string} error
  152. * @method getUserPrefErrorCallback
  153. */
  154. getUserPrefErrorCallback: function (request, ajaxOptions, error) {
  155. if (request.status == 404) {
  156. // default status already set
  157. console.log('Persist API did NOT find the key CLUSTER_CURRENT_STATUS');
  158. return;
  159. }
  160. App.ModalPopup.show({
  161. header: Em.I18n.t('common.error'),
  162. secondary: false,
  163. bodyClass: Em.View.extend({
  164. template: Em.Handlebars.compile('<p>{{t common.update.error}}</p>')
  165. })
  166. });
  167. },
  168. /**
  169. * update cluster status and post it on server
  170. * @param {object} newValue
  171. * @param {object} opt - used for additional ajax request options, by default ajax used synchronous mode
  172. * @method setClusterStatus
  173. * @return {*}
  174. */
  175. setClusterStatus: function (newValue, opt) {
  176. if (App.get('testMode')) return false;
  177. var user = App.db.getUser();
  178. var login = App.db.getLoginName();
  179. var val = {clusterName: this.get('clusterName')};
  180. if (newValue) {
  181. App.db.cleanTmp();
  182. //setter
  183. if (newValue.clusterName) {
  184. this.set('clusterName', newValue.clusterName);
  185. val.clusterName = newValue.clusterName;
  186. }
  187. if (newValue.clusterState) {
  188. this.set('clusterState', newValue.clusterState);
  189. val.clusterState = newValue.clusterState;
  190. }
  191. if (newValue.wizardControllerName) {
  192. this.set('wizardControllerName', newValue.wizardControllerName);
  193. val.wizardControllerName = newValue.wizardControllerName;
  194. }
  195. if (newValue.localdb) {
  196. if (newValue.localdb.app && newValue.localdb.app.user)
  197. delete newValue.localdb.app.user;
  198. if (newValue.localdb.app && newValue.localdb.app.loginName)
  199. delete newValue.localdb.app.loginName;
  200. this.set('localdb', newValue.localdb);
  201. val.localdb = newValue.localdb;
  202. } else {
  203. delete App.db.data.app.user;
  204. delete App.db.data.app.loginName;
  205. val.localdb = App.db.data;
  206. App.db.setUser(user);
  207. App.db.setLoginName(login);
  208. }
  209. if (opt) {
  210. var keyValuePair = {};
  211. keyValuePair[this.get('key')] = JSON.stringify(val);
  212. App.ajax.send({
  213. name: 'settings.post.user_pref',
  214. sender: opt.sender || this,
  215. data: {
  216. async: !!opt.async,
  217. keyValuePair: keyValuePair
  218. },
  219. success: opt.success,
  220. beforeSend: opt.beforeSend,
  221. error: opt.error
  222. });
  223. }
  224. else {
  225. this.set('makeRequestAsync', false);
  226. this.postUserPref(this.get('key'), val);
  227. }
  228. return newValue;
  229. }
  230. },
  231. /**
  232. * Error callback for post-persist request
  233. * @param {object} request
  234. * @param {object} ajaxOptions
  235. * @param {string} error
  236. * @method postUserPrefErrorCallback
  237. */
  238. postUserPrefErrorCallback: function (request, ajaxOptions, error) {
  239. console.log("ERROR");
  240. var msg = '', doc;
  241. try {
  242. msg = 'Error ' + (request.status) + ' ';
  243. doc = $.parseXML(request.responseText);
  244. msg += $(doc).find("body p").text();
  245. } catch (e) {
  246. msg += JSON.parse(request.responseText).message;
  247. }
  248. App.ModalPopup.show({
  249. header: Em.I18n.t('common.error'),
  250. secondary: false,
  251. response: msg,
  252. bodyClass: Em.View.extend({
  253. template: Em.Handlebars.compile('<p>{{t common.persist.error}} {{response}}</p>')
  254. })
  255. });
  256. }
  257. });