cluster_states.js 9.5 KB

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