cluster_states.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. * Stores instance of <code>App.ModalPopup</code> created by <code>postUserPrefErrorCallback</code>
  82. * @property {App.ModalPopup|null}
  83. */
  84. persistErrorModal: null,
  85. /**
  86. * General info about cluster
  87. * @type {{clusterName: string, clusterState: string, wizardControllerName: string, localdb: object}}
  88. */
  89. value: function () {
  90. return {
  91. clusterName: this.get('clusterName'),
  92. clusterState: this.get('clusterState'),
  93. wizardControllerName: this.get('wizardControllerName'),
  94. localdb: this.get('localdb')
  95. };
  96. }.property('clusterName', 'clusterState', 'localdb', 'wizardControllerName'),
  97. /**
  98. * get cluster data from server and update cluster status
  99. * @param {bool} overrideLocaldb
  100. * @return promise object for the get call
  101. * @method updateFromServer
  102. */
  103. updateFromServer: function (overrideLocaldb) {
  104. this.set('additionalData', {
  105. user: App.db.getUser(),
  106. login: App.db.getLoginName(),
  107. auth: App.db.getAuth(),
  108. overrideLocaldb: !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. // decompress response
  122. if (typeof response != 'object') {
  123. response = JSON.parse(LZString.decompressFromBase64(response));
  124. }
  125. if (response.clusterState) {
  126. this.set('clusterState', response.clusterState);
  127. }
  128. if (response.clusterName) {
  129. this.set('clusterName', response.clusterName);
  130. }
  131. if (response.wizardControllerName) {
  132. this.set('wizardControllerName', response.wizardControllerName);
  133. }
  134. if (response.localdb && !$.isEmptyObject(response.localdb)) {
  135. this.set('localdb', response.localdb);
  136. // restore HAWizard data if process was started
  137. var isHAWizardStarted = App.isAuthorized('SERVICE.ENABLE_HA') && !App.isEmptyObject(response.localdb.HighAvailabilityWizard);
  138. if (params.data.overrideLocaldb || isHAWizardStarted) {
  139. var localdbTables = (App.db.data.app && App.db.data.app.tables) ? App.db.data.app.tables : {};
  140. var authenticated = Em.get(App, 'db.data.app.authenticated') || false;
  141. App.db.data = response.localdb;
  142. App.db.setLocalStorage();
  143. App.db.setUser(params.data.user);
  144. App.db.setLoginName(params.data.login);
  145. App.db.setAuth(params.data.auth);
  146. App.db.setAuthenticated(authenticated);
  147. App.db.data.app.tables = localdbTables;
  148. }
  149. }
  150. }
  151. // this is to ensure that the local storage namespaces are initialized with all expected namespaces.
  152. // after upgrading ambari, loading local storage data from the "persist" data saved via an older version of
  153. // Ambari can result in missing namespaces that are defined in the new version of Ambari.
  154. App.db.mergeStorage();
  155. },
  156. /**
  157. * Error callback for get-persist request
  158. * @param {object} request
  159. * @param {object} ajaxOptions
  160. * @param {string} error
  161. * @method getUserPrefErrorCallback
  162. */
  163. getUserPrefErrorCallback: function (request, ajaxOptions, error) {
  164. if (request.status == 404) {
  165. // default status already set
  166. return;
  167. }
  168. App.ModalPopup.show({
  169. header: Em.I18n.t('common.error'),
  170. secondary: false,
  171. bodyClass: Em.View.extend({
  172. template: Em.Handlebars.compile('<p>{{t common.update.error}}</p>')
  173. })
  174. });
  175. },
  176. /**
  177. * update cluster status and post it on server.
  178. * This function should always be called by admin user
  179. * @param {object} newValue
  180. * @param {object} opt - Can have additional params for ajax callBacks and sender
  181. * opt.successCallback
  182. * opt.successCallbackData
  183. * opt.errorCallback
  184. * opt.errorCallbackData
  185. * opt.alwaysCallback
  186. * opt.alwaysCallbackData
  187. * opt.sender
  188. * @method setClusterStatus
  189. * @return {*}
  190. */
  191. setClusterStatus: function (newValue, opt) {
  192. if (App.get('testMode')) return false;
  193. var user = App.db.getUser();
  194. var auth = App.db.getAuth();
  195. var login = App.db.getLoginName();
  196. var val = {clusterName: this.get('clusterName')};
  197. if (newValue) {
  198. App.db.cleanTmp();
  199. //setter
  200. if (newValue.clusterName) {
  201. this.set('clusterName', newValue.clusterName);
  202. val.clusterName = newValue.clusterName;
  203. }
  204. if (newValue.clusterState) {
  205. this.set('clusterState', newValue.clusterState);
  206. val.clusterState = newValue.clusterState;
  207. }
  208. if (newValue.wizardControllerName) {
  209. this.set('wizardControllerName', newValue.wizardControllerName);
  210. val.wizardControllerName = newValue.wizardControllerName;
  211. }
  212. if (newValue.localdb) {
  213. if (newValue.localdb.app && newValue.localdb.app.user)
  214. delete newValue.localdb.app.user;
  215. if (newValue.localdb.app && newValue.localdb.app.auth)
  216. delete newValue.localdb.app.auth;
  217. if (newValue.localdb.app && newValue.localdb.app.loginName)
  218. delete newValue.localdb.app.loginName;
  219. if (newValue.localdb.app && newValue.localdb.app.tables)
  220. delete newValue.localdb.app.tables;
  221. if (newValue.localdb.app && newValue.localdb.app.authenticated)
  222. delete newValue.localdb.app.authenticated;
  223. this.set('localdb', newValue.localdb);
  224. val.localdb = newValue.localdb;
  225. } else {
  226. delete App.db.data.app.user;
  227. delete App.db.data.app.auth;
  228. delete App.db.data.app.loginName;
  229. delete App.db.data.app.tables;
  230. delete App.db.data.app.authenticated;
  231. val.localdb = App.db.data;
  232. App.db.setUser(user);
  233. App.db.setAuth(auth);
  234. App.db.setLoginName(login);
  235. }
  236. if (!$.mocho) {
  237. // compress val
  238. val = LZString.compressToBase64(JSON.stringify(val));
  239. this.postUserPref(this.get('key'), val)
  240. .done(function () {
  241. !!opt && Em.typeOf(opt.successCallback) === 'function' && opt.successCallback.call(opt.sender || this, opt.successCallbackData);
  242. })
  243. .fail(function () {
  244. !!opt && Em.typeOf(opt.errorCallback) === 'function' && opt.errorCallback.call(opt.sender || this, opt.errorCallbackData);
  245. })
  246. .always(function () {
  247. !!opt && Em.typeOf(opt.alwaysCallback) === 'function' && opt.alwaysCallback.call(opt.sender || this, opt.alwaysCallbackData);
  248. });
  249. }
  250. return newValue;
  251. }
  252. },
  253. /**
  254. * Error callback for post-persist request
  255. * @param {object} request
  256. * @param {object} ajaxOptions
  257. * @param {string} error
  258. * @method postUserPrefErrorCallback
  259. */
  260. postUserPrefErrorCallback: function (request, ajaxOptions, error) {
  261. var msg = '', doc;
  262. try {
  263. msg = 'Error ' + (request.status) + ' ';
  264. doc = $.parseXML(request.responseText);
  265. msg += $(doc).find("body p").text();
  266. } catch (e) {
  267. msg += JSON.parse(request.responseText).message;
  268. }
  269. if (this.get('persistErrorModal')) {
  270. if (this.get('persistErrorModal').get('state') === 'destroyed') {
  271. this.set('persistErrorModal', null);
  272. } else {
  273. this.get('persistErrorModal').onPrimary();
  274. this.set('persistErrorModal', null);
  275. }
  276. }
  277. var modal = App.ModalPopup.show({
  278. header: Em.I18n.t('common.error'),
  279. secondary: false,
  280. response: msg,
  281. bodyClass: Em.View.extend({
  282. template: Em.Handlebars.compile('<p>{{t common.persist.error}} {{response}}</p>')
  283. })
  284. });
  285. this.set('persistErrorModal', modal);
  286. }
  287. });