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} overrideLocaldb
  97. * @return promise object for the get call
  98. * @method updateFromServer
  99. */
  100. updateFromServer: function (overrideLocaldb) {
  101. // if overrideLocaldb is undefined, set it to true
  102. this.set('additionalData', {
  103. user: App.db.getUser(),
  104. login: App.db.getLoginName(),
  105. overrideLocaldb: !overrideLocaldb
  106. });
  107. return this.getUserPref(this.get('key'));
  108. },
  109. /**
  110. * Success callback for get-persist request
  111. * @param {object} response
  112. * @param {object} opt
  113. * @param {object} params
  114. * @method getUserPrefSuccessCallback
  115. */
  116. getUserPrefSuccessCallback: function (response, opt, params) {
  117. if (response) {
  118. if (response.clusterState) {
  119. this.set('clusterState', response.clusterState);
  120. }
  121. if (response.clusterName) {
  122. this.set('clusterName', response.clusterName);
  123. }
  124. if (response.wizardControllerName) {
  125. this.set('wizardControllerName', response.wizardControllerName);
  126. }
  127. if (response.localdb) {
  128. this.set('localdb', response.localdb);
  129. // restore HAWizard data if process was started
  130. var isHAWizardStarted = App.get('isAdmin') && !App.isEmptyObject(response.localdb.HighAvailabilityWizard);
  131. if (params.data.overrideLocaldb || isHAWizardStarted) {
  132. var localdbTables = (App.db.data.app && App.db.data.app.tables) ? App.db.data.app.tables : {};
  133. App.db.data = response.localdb;
  134. App.db.setLocalStorage();
  135. App.db.setUser(params.data.user);
  136. App.db.setLoginName(params.data.login);
  137. App.db.data.app.tables = localdbTables;
  138. }
  139. }
  140. }
  141. // this is to ensure that the local storage namespaces are initialized with all expected namespaces.
  142. // after upgrading ambari, loading local storage data from the "persist" data saved via an older version of
  143. // Ambari can result in missing namespaces that are defined in the new version of Ambari.
  144. App.db.mergeStorage();
  145. },
  146. /**
  147. * Error callback for get-persist request
  148. * @param {object} request
  149. * @param {object} ajaxOptions
  150. * @param {string} error
  151. * @method getUserPrefErrorCallback
  152. */
  153. getUserPrefErrorCallback: function (request, ajaxOptions, error) {
  154. if (request.status == 404) {
  155. // default status already set
  156. console.log('Persist API did NOT find the key CLUSTER_CURRENT_STATUS');
  157. return;
  158. }
  159. App.ModalPopup.show({
  160. header: Em.I18n.t('common.error'),
  161. secondary: false,
  162. bodyClass: Em.View.extend({
  163. template: Em.Handlebars.compile('<p>{{t common.update.error}}</p>')
  164. })
  165. });
  166. },
  167. /**
  168. * update cluster status and post it on server
  169. * @param {object} newValue
  170. * @param {object} opt - used for additional ajax request options, by default ajax used synchronous mode
  171. * @method setClusterStatus
  172. * @return {*}
  173. */
  174. setClusterStatus: function (newValue, opt) {
  175. if (App.get('testMode')) return false;
  176. var user = App.db.getUser();
  177. var login = App.db.getLoginName();
  178. var val = {clusterName: this.get('clusterName')};
  179. if (newValue) {
  180. App.db.cleanTmp();
  181. //setter
  182. if (newValue.clusterName) {
  183. this.set('clusterName', newValue.clusterName);
  184. val.clusterName = newValue.clusterName;
  185. }
  186. if (newValue.clusterState) {
  187. this.set('clusterState', newValue.clusterState);
  188. val.clusterState = newValue.clusterState;
  189. }
  190. if (newValue.wizardControllerName) {
  191. this.set('wizardControllerName', newValue.wizardControllerName);
  192. val.wizardControllerName = newValue.wizardControllerName;
  193. }
  194. if (newValue.localdb) {
  195. if (newValue.localdb.app && newValue.localdb.app.user)
  196. delete newValue.localdb.app.user;
  197. if (newValue.localdb.app && newValue.localdb.app.loginName)
  198. delete newValue.localdb.app.loginName;
  199. if (newValue.localdb.app && newValue.localdb.app.tables)
  200. delete newValue.localdb.app.tables;
  201. this.set('localdb', newValue.localdb);
  202. val.localdb = newValue.localdb;
  203. } else {
  204. delete App.db.data.app.user;
  205. delete App.db.data.app.loginName;
  206. delete App.db.data.app.tables;
  207. val.localdb = App.db.data;
  208. App.db.setUser(user);
  209. App.db.setLoginName(login);
  210. }
  211. if (opt) {
  212. var keyValuePair = {};
  213. keyValuePair[this.get('key')] = JSON.stringify(val);
  214. App.ajax.send({
  215. name: 'settings.post.user_pref',
  216. sender: opt.sender || this,
  217. data: {
  218. keyValuePair: keyValuePair
  219. },
  220. success: opt.success,
  221. beforeSend: opt.beforeSend,
  222. error: opt.error
  223. });
  224. }
  225. else {
  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. });