cluster_states.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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} overrideLocaldb
  97. * @return promise object for the get call
  98. * @method updateFromServer
  99. */
  100. updateFromServer: function (overrideLocaldb) {
  101. this.set('additionalData', {
  102. user: App.db.getUser(),
  103. login: App.db.getLoginName(),
  104. overrideLocaldb: !overrideLocaldb
  105. });
  106. return this.getUserPref(this.get('key'));
  107. },
  108. /**
  109. * Success callback for get-persist request
  110. * @param {object} response
  111. * @param {object} opt
  112. * @param {object} params
  113. * @method getUserPrefSuccessCallback
  114. */
  115. getUserPrefSuccessCallback: function (response, opt, params) {
  116. if (response) {
  117. if (response.clusterState) {
  118. this.set('clusterState', response.clusterState);
  119. }
  120. if (response.clusterName) {
  121. this.set('clusterName', response.clusterName);
  122. }
  123. if (response.wizardControllerName) {
  124. this.set('wizardControllerName', response.wizardControllerName);
  125. }
  126. if (response.localdb) {
  127. this.set('localdb', response.localdb);
  128. // restore HAWizard data if process was started
  129. var isHAWizardStarted = App.isAccessible('ADMIN') && !App.isEmptyObject(response.localdb.HighAvailabilityWizard);
  130. if (params.data.overrideLocaldb || isHAWizardStarted) {
  131. var localdbTables = (App.db.data.app && App.db.data.app.tables) ? App.db.data.app.tables : {};
  132. App.db.data = response.localdb;
  133. App.db.setLocalStorage();
  134. App.db.setUser(params.data.user);
  135. App.db.setLoginName(params.data.login);
  136. App.db.data.app.tables = localdbTables;
  137. }
  138. }
  139. }
  140. // this is to ensure that the local storage namespaces are initialized with all expected namespaces.
  141. // after upgrading ambari, loading local storage data from the "persist" data saved via an older version of
  142. // Ambari can result in missing namespaces that are defined in the new version of Ambari.
  143. App.db.mergeStorage();
  144. },
  145. /**
  146. * Error callback for get-persist request
  147. * @param {object} request
  148. * @param {object} ajaxOptions
  149. * @param {string} error
  150. * @method getUserPrefErrorCallback
  151. */
  152. getUserPrefErrorCallback: function (request, ajaxOptions, error) {
  153. if (request.status == 404) {
  154. // default status already set
  155. console.log('Persist API did NOT find the key CLUSTER_CURRENT_STATUS');
  156. return;
  157. }
  158. App.ModalPopup.show({
  159. header: Em.I18n.t('common.error'),
  160. secondary: false,
  161. bodyClass: Em.View.extend({
  162. template: Em.Handlebars.compile('<p>{{t common.update.error}}</p>')
  163. })
  164. });
  165. },
  166. /**
  167. * update cluster status and post it on server.
  168. * This function should always be called by admin user
  169. * @param {object} newValue
  170. * @param {object} opt - Can have additional params for ajax callBacks and sender
  171. * opt.successCallback
  172. * opt.successCallbackData
  173. * opt.errorCallback
  174. * opt.errorCallbackData
  175. * opt.alwaysCallback
  176. * opt.alwaysCallbackData
  177. * opt.sender
  178. * @method setClusterStatus
  179. * @return {*}
  180. */
  181. setClusterStatus: function (newValue, opt) {
  182. if (App.get('testMode')) return false;
  183. if (!App.isAccessible('ADMIN')) {
  184. Em.assert('Non-Admin user should not execute setClusterStatus function', true);
  185. }
  186. var user = App.db.getUser();
  187. var login = App.db.getLoginName();
  188. var val = {clusterName: this.get('clusterName')};
  189. if (newValue) {
  190. App.db.cleanTmp();
  191. //setter
  192. if (newValue.clusterName) {
  193. this.set('clusterName', newValue.clusterName);
  194. val.clusterName = newValue.clusterName;
  195. }
  196. if (newValue.clusterState) {
  197. this.set('clusterState', newValue.clusterState);
  198. val.clusterState = newValue.clusterState;
  199. }
  200. if (newValue.wizardControllerName) {
  201. this.set('wizardControllerName', newValue.wizardControllerName);
  202. val.wizardControllerName = newValue.wizardControllerName;
  203. }
  204. if (newValue.localdb) {
  205. if (newValue.localdb.app && newValue.localdb.app.user)
  206. delete newValue.localdb.app.user;
  207. if (newValue.localdb.app && newValue.localdb.app.loginName)
  208. delete newValue.localdb.app.loginName;
  209. if (newValue.localdb.app && newValue.localdb.app.tables)
  210. delete newValue.localdb.app.tables;
  211. this.set('localdb', newValue.localdb);
  212. val.localdb = newValue.localdb;
  213. } else {
  214. delete App.db.data.app.user;
  215. delete App.db.data.app.loginName;
  216. delete App.db.data.app.tables;
  217. val.localdb = App.db.data;
  218. App.db.setUser(user);
  219. App.db.setLoginName(login);
  220. }
  221. if (!$.mocho) {
  222. this.postUserPref(this.get('key'), val)
  223. .done(function () {
  224. !!opt && Em.typeOf(opt.successCallback) === 'function' && opt.successCallback.call(opt.sender || this, opt.successCallbackData);
  225. })
  226. .fail(function () {
  227. !!opt && Em.typeOf(opt.errorCallback) === 'function' && opt.errorCallback.call(opt.sender || this, opt.errorCallbackData);
  228. })
  229. .always(function () {
  230. !!opt && Em.typeOf(opt.alwaysCallback) === 'function' && opt.alwaysCallback.call(opt.sender || this, opt.alwaysCallbackData);
  231. });
  232. }
  233. return newValue;
  234. }
  235. },
  236. /**
  237. * Error callback for post-persist request
  238. * @param {object} request
  239. * @param {object} ajaxOptions
  240. * @param {string} error
  241. * @method postUserPrefErrorCallback
  242. */
  243. postUserPrefErrorCallback: function (request, ajaxOptions, error) {
  244. console.log("ERROR");
  245. var msg = '', doc;
  246. try {
  247. msg = 'Error ' + (request.status) + ' ';
  248. doc = $.parseXML(request.responseText);
  249. msg += $(doc).find("body p").text();
  250. } catch (e) {
  251. msg += JSON.parse(request.responseText).message;
  252. }
  253. App.ModalPopup.show({
  254. header: Em.I18n.t('common.error'),
  255. secondary: false,
  256. response: msg,
  257. bodyClass: Em.View.extend({
  258. template: Em.Handlebars.compile('<p>{{t common.persist.error}} {{response}}</p>')
  259. })
  260. });
  261. }
  262. });