db.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. App.db = {};
  20. var InitialData = {
  21. 'app': {
  22. 'loginName': '',
  23. 'authenticated': false,
  24. 'configs': [],
  25. 'tables': {
  26. 'filterConditions': {},
  27. 'displayLength': {},
  28. 'startIndex': {},
  29. 'sortingConditions': {},
  30. 'selectedItems': {}
  31. }
  32. },
  33. 'Installer': {},
  34. 'AddHost': {},
  35. 'AddService': {},
  36. 'WidgetWizard': {},
  37. 'KerberosWizard': {},
  38. 'ReassignMaster': {},
  39. 'AddSecurity': {},
  40. 'AddAlertDefinition': {
  41. content: {}
  42. },
  43. 'HighAvailabilityWizard': {},
  44. 'RMHighAvailabilityWizard': {},
  45. 'AddHawqStandbyWizard': {},
  46. 'ActivateHawqStandbyWizard': {},
  47. 'RAHighAvailabilityWizard': {},
  48. 'RollbackHighAvailabilityWizard': {},
  49. 'MainAdminStackAndUpgrade': {},
  50. 'KerberosDisable': {},
  51. 'tmp': {}
  52. };
  53. function checkNamespace(namespace) {
  54. if (!namespace) {
  55. return false;
  56. }
  57. if (Em.isNone(Em.get(App.db.data, namespace))) {
  58. Em.setFullPath(App.db.data, namespace, {});
  59. }
  60. return true;
  61. }
  62. if (typeof Storage === 'undefined') {
  63. // stub for unit testing purposes
  64. window.localStorage = {};
  65. localStorage.setItem = function (key, val) {
  66. this[key] = val;
  67. };
  68. localStorage.getItem = function (key) {
  69. return this[key];
  70. };
  71. window.localStorage.setObject = function (key, value) {
  72. this[key] = value;
  73. };
  74. window.localStorage.getObject = function (key) {
  75. return this[key];
  76. };
  77. }
  78. else {
  79. Storage.prototype.setObject = function (key, value) {
  80. this.setItem(key, JSON.stringify(value));
  81. };
  82. Storage.prototype.getObject = function (key) {
  83. var value = this.getItem(key);
  84. return value && JSON.parse(value);
  85. };
  86. }
  87. App.db.cleanUp = function () {
  88. App.db.data = InitialData;
  89. localStorage.setObject('ambari', App.db.data);
  90. };
  91. App.db.cleanTmp = function () {
  92. App.db.data.tmp = {};
  93. localStorage.setObject('ambari', App.db.data);
  94. };
  95. App.db.updateStorage = function () {
  96. App.db.data = localStorage.getObject('ambari');
  97. if (Em.get(App, 'db.data.app.tables') && Em.get(App, 'db.data.app.configs')) {
  98. return true;
  99. }
  100. App.db.cleanUp();
  101. return false;
  102. };
  103. /*
  104. Initialize wizard namespaces if they are not initialized on login.
  105. This will be required during upgrade.
  106. */
  107. App.db.mergeStorage = function () {
  108. if (localStorage.getObject('ambari') == null) {
  109. App.db.cleanUp();
  110. } else {
  111. localStorage.setObject('ambari', $.extend(true, {}, InitialData, App.db.data));
  112. }
  113. };
  114. // called whenever user logs in
  115. if (localStorage.getObject('ambari') == null) {
  116. App.db.cleanUp();
  117. }
  118. /**
  119. *
  120. * @param {string} namespace
  121. * @param {string} key
  122. * @returns {*}
  123. */
  124. App.db.get = function (namespace, key) {
  125. App.db.data = localStorage.getObject('ambari');
  126. Em.assert('`namespace` should be defined', !!namespace);
  127. checkNamespace(namespace);
  128. return Em.get(Em.get(App.db.data, namespace), key);
  129. };
  130. /**
  131. *
  132. * @param {string} namespace
  133. * @param {string[]} listOfProperties
  134. * @returns {object}
  135. */
  136. App.db.getProperties = function (namespace, listOfProperties) {
  137. App.db.data = localStorage.getObject('ambari');
  138. Em.assert('`namespace` should be defined', !!namespace);
  139. checkNamespace(namespace);
  140. return Em.getProperties(Em.get(App.db.data, namespace), listOfProperties);
  141. };
  142. /**
  143. *
  144. * @param {string} namespace
  145. * @param {string} key
  146. * @param {*} value
  147. */
  148. App.db.set = function (namespace, key, value) {
  149. App.db.data = localStorage.getObject('ambari');
  150. Em.assert('`namespace` should be defined', !!namespace);
  151. checkNamespace(namespace);
  152. Em.set(Em.get(App.db.data, namespace), key, value);
  153. localStorage.setObject('ambari', App.db.data);
  154. };
  155. /**
  156. *
  157. * @param {string} namespace
  158. * @param {{key: value}} hash
  159. */
  160. App.db.setProperties = function (namespace, hash) {
  161. App.db.data = localStorage.getObject('ambari');
  162. Em.assert('`namespace` should be defined', !!namespace);
  163. checkNamespace(namespace);
  164. Em.setProperties(Em.get(App.db.data, namespace), hash);
  165. localStorage.setObject('ambari', App.db.data);
  166. };
  167. App.db.setLoginName = function (name) {
  168. App.db.set('app', 'loginName', name);
  169. };
  170. /**
  171. * Set user model to db
  172. * @param user
  173. */
  174. App.db.setUser = function (user) {
  175. App.db.set('app', 'user', user);
  176. };
  177. App.db.setAuth = function (auth) {
  178. App.db.set('app', 'auth', auth);
  179. };
  180. App.db.setAuthenticated = function (authenticated) {
  181. App.db.set('app', 'authenticated', authenticated);
  182. App.db.data = localStorage.getObject('ambari');
  183. };
  184. App.db.setFilterConditions = function (name, filterConditions) {
  185. App.db.set('app.tables.filterConditions', name, filterConditions);
  186. };
  187. App.db.setComboSearchQuery = function (name, query) {
  188. App.db.set('app.tables.comboSearchQuery', name, query);
  189. };
  190. App.db.setDisplayLength = function (name, displayLength) {
  191. App.db.set('app.tables.displayLength', name, displayLength);
  192. };
  193. App.db.setStartIndex = function (name, startIndex) {
  194. App.db.set('app.tables.startIndex', name, startIndex);
  195. };
  196. App.db.setSortingStatuses = function (name, sortingConditions) {
  197. App.db.set('app.tables.sortingConditions', name, sortingConditions);
  198. };
  199. App.db.setSelectedHosts = function (name, selectedHosts) {
  200. App.db.set('app.tables.selectedItems', name, selectedHosts);
  201. };
  202. App.db.setHosts = function (hostInfo) {
  203. App.db.set('Installer', 'hostInfo', hostInfo);
  204. };
  205. App.db.setMasterComponentHosts = function (masterComponentHosts) {
  206. App.db.set('Installer', 'masterComponentHosts', masterComponentHosts);
  207. };
  208. App.db.setMasterToReassign = function (masterComponent) {
  209. App.db.set('ReassignMaster', 'masterComponent', masterComponent);
  210. };
  211. App.db.setReassignTasksStatuses = function (tasksStatuses) {
  212. App.db.set('ReassignMaster', 'tasksStatuses', tasksStatuses);
  213. };
  214. App.db.setReassignTasksRequestIds = function (requestIds) {
  215. App.db.set('ReassignMaster', 'tasksRequestIds', requestIds);
  216. };
  217. App.db.setStacks = function (stacks) {
  218. App.db.set('Installer', 'stacksVersions', stacks);
  219. };
  220. App.db.setConfigs = function (configs) {
  221. App.db.set('app', 'configs', configs);
  222. };
  223. /**
  224. * Set current step value for specified Wizard Type
  225. * @param wizardType
  226. * @param currentStep
  227. */
  228. App.db.setWizardCurrentStep = function (wizardType, currentStep) {
  229. App.db.set(wizardType.capitalize(), 'currentStep', currentStep);
  230. };
  231. /**
  232. * Set localStorage with data from server
  233. */
  234. App.db.setLocalStorage = function () {
  235. localStorage.setObject('ambari', App.db.data);
  236. };
  237. App.db.setSecurityWizardStatus = function (status) {
  238. App.db.set('AddSecurity', 'status', status);
  239. };
  240. App.db.setDisableSecurityStatus = function (status) {
  241. App.db.set('AddSecurity', 'disableSecurityStatus', status);
  242. };
  243. App.db.setSecurityDeployCommands = function (commands) {
  244. App.db.set('AddSecurity', 'securityDeployCommands', commands);
  245. };
  246. App.db.setHighAvailabilityWizardConfigTag = function (tag) {
  247. App.db.set('HighAvailabilityWizard', tag.name, tag.value);
  248. };
  249. App.db.setHighAvailabilityWizardHdfsClientHosts = function (hostNames) {
  250. App.db.set('HighAvailabilityWizard', 'hdfsClientHostNames', hostNames);
  251. };
  252. App.db.setHighAvailabilityWizardTasksStatuses = function (tasksStatuses) {
  253. App.db.set('HighAvailabilityWizard', 'tasksStatuses', tasksStatuses);
  254. };
  255. App.db.setHighAvailabilityWizardTasksRequestIds = function (requestIds) {
  256. App.db.set('HighAvailabilityWizard', 'tasksRequestIds', requestIds);
  257. };
  258. App.db.setHighAvailabilityWizardHdfsUser = function (hdfsUser) {
  259. App.db.set('HighAvailabilityWizard', 'hdfsUser', hdfsUser);
  260. };
  261. App.db.setHighAvailabilityWizardRequestIds = function (requestIds) {
  262. App.db.set('HighAvailabilityWizard', 'requestIds', requestIds);
  263. };
  264. App.db.setHighAvailabilityWizardNameServiceId = function (nameServiceId) {
  265. App.db.set('HighAvailabilityWizard', 'nameServiceId', nameServiceId);
  266. };
  267. App.db.setRollBackHighAvailabilityWizardAddNNHost = function (host) {
  268. App.db.set('RollbackHighAvailabilityWizard', 'addNNHost', host);
  269. };
  270. App.db.setRollBackHighAvailabilityWizardSNNHost = function (host) {
  271. App.db.set('RollbackHighAvailabilityWizard', 'sNNHost', host);
  272. };
  273. App.db.setRollBackHighAvailabilityWizardSelectedAddNN = function (host) {
  274. App.db.set('RollbackHighAvailabilityWizard', 'selectedAddNN', host);
  275. };
  276. App.db.setRollBackHighAvailabilityWizardSelectedSNN = function (host) {
  277. App.db.set('RollbackHighAvailabilityWizard', 'selectedSNNH', host);
  278. };
  279. App.db.setRollbackHighAvailabilityWizardTasksStatuses = function (tasksStatuses) {
  280. App.db.set('RollbackHighAvailabilityWizard', 'tasksStatuses', tasksStatuses);
  281. };
  282. App.db.setRollbackHighAvailabilityWizardRequestIds = function (requestIds) {
  283. App.db.set('RollbackHighAvailabilityWizard', 'requestIds', requestIds);
  284. };
  285. App.db.setReassignMasterWizardRequestIds = function (requestIds) {
  286. App.db.set('ReassignMaster', 'requestIds', requestIds);
  287. };
  288. App.db.setReassignMasterWizardComponentDir = function (componentDir) {
  289. App.db.set('ReassignMaster', 'componentDir', componentDir);
  290. };
  291. App.db.setReassignMasterWizardReassignHosts = function (reassignHosts) {
  292. App.db.set('ReassignMaster', 'reassignHosts', reassignHosts);
  293. };
  294. App.db.setKerberosWizardConfigTag = function (tag) {
  295. App.db.set('KerberosWizard', tag.name, tag.value);
  296. };
  297. /**
  298. * Get user model from db
  299. * @return {*}
  300. */
  301. App.db.getUser = function () {
  302. return App.db.get('app', 'user');
  303. };
  304. App.db.getAuth = function () {
  305. return App.db.get('app', 'auth');
  306. };
  307. App.db.getLoginName = function () {
  308. return App.db.get('app', 'loginName');
  309. };
  310. App.db.getAuthenticated = function () {
  311. return Boolean(App.db.get('app', 'authenticated'));
  312. };
  313. App.db.getFilterConditions = function (name) {
  314. return name ? App.db.get('app.tables.filterConditions', name) : null;
  315. };
  316. App.db.getComboSearchQuery = function (name) {
  317. return name ? App.db.get('app.tables.comboSearchQuery', name) : null;
  318. };
  319. App.db.getDisplayLength = function (name) {
  320. return name ? App.db.get('app.tables.displayLength', name) : null;
  321. };
  322. App.db.getStartIndex = function (name) {
  323. return name ? App.db.get('app.tables.startIndex', name): null;
  324. };
  325. App.db.getSortingStatuses = function (name) {
  326. return name ? App.db.get('app.tables.sortingConditions', name): null;
  327. };
  328. App.db.getSelectedHosts = function (name) {
  329. return App.db.get('app.tables.selectedItems', name) || [];
  330. };
  331. /**
  332. * Return current step for specified Wizard Type
  333. * @param wizardType
  334. * @return {*}
  335. */
  336. App.db.getWizardCurrentStep = function (wizardType) {
  337. return App.db.get(wizardType.capitalize(), 'currentStep') || 0;
  338. };
  339. App.db.getAllHostNames = function () {
  340. return App.db.get('Installer', 'hostNames');
  341. };
  342. App.db.getHosts = function () {
  343. return App.db.get('Installer', 'hostInfo');
  344. };
  345. App.db.getMasterToReassign = function () {
  346. return App.db.get('ReassignMaster', 'masterComponent');
  347. };
  348. App.db.getReassignTasksStatuses = function () {
  349. return App.db.get('ReassignMaster', 'tasksStatuses');
  350. };
  351. App.db.getReassignTasksRequestIds = function () {
  352. return App.db.get('ReassignMaster', 'tasksRequestIds');
  353. };
  354. App.db.getSecurityWizardStatus = function () {
  355. return App.db.get('AddSecurity', 'status');
  356. };
  357. App.db.getDisableSecurityStatus = function () {
  358. return App.db.get('AddSecurity', 'disableSecurityStatus');
  359. };
  360. App.db.getStacks = function () {
  361. return App.db.get('Installer', 'stacksVersions');
  362. };
  363. App.db.getHighAvailabilityWizardHdfsUser = function () {
  364. return App.db.get('HighAvailabilityWizard', 'hdfsUser');
  365. };
  366. App.db.getHighAvailabilityWizardTasksStatuses = function () {
  367. return App.db.get('HighAvailabilityWizard', 'tasksStatuses');
  368. };
  369. App.db.getHighAvailabilityWizardTasksRequestIds = function () {
  370. return App.db.get('HighAvailabilityWizard', 'tasksRequestIds');
  371. };
  372. App.db.getHighAvailabilityWizardFailedTask = function () {
  373. return App.db.get('HighAvailabilityWizard', 'failedTask');
  374. };
  375. App.db.getHighAvailabilityWizardHdfsClientHosts = function () {
  376. return App.db.get('HighAvailabilityWizard', 'hdfsClientHostNames');
  377. };
  378. App.db.getHighAvailabilityWizardConfigTag = function (tag) {
  379. return App.db.get('HighAvailabilityWizard', tag);
  380. };
  381. App.db.getHighAvailabilityWizardRequestIds = function () {
  382. return App.db.get('HighAvailabilityWizard', 'requestIds');
  383. };
  384. App.db.getHighAvailabilityWizardNameServiceId = function () {
  385. return App.db.get('HighAvailabilityWizard', 'nameServiceId');
  386. };
  387. App.db.getRollbackHighAvailabilityWizardTasksStatuses = function () {
  388. return App.db.get('RollbackHighAvailabilityWizard', 'tasksStatuses');
  389. };
  390. App.db.getRollbackHighAvailabilityWizardRequestIds = function () {
  391. return App.db.get('RollbackHighAvailabilityWizard', 'requestIds');
  392. };
  393. App.db.getRollBackHighAvailabilityWizardAddNNHost = function () {
  394. return App.db.get('RollbackHighAvailabilityWizard', 'addNNHost');
  395. };
  396. App.db.getRollBackHighAvailabilityWizardSNNHost = function () {
  397. return App.db.get('RollbackHighAvailabilityWizard', 'sNNHost');
  398. };
  399. App.db.getReassignMasterWizardRequestIds = function () {
  400. return App.db.get('ReassignMaster', 'requestIds');
  401. };
  402. App.db.getReassignMasterWizardComponentDir = function () {
  403. return App.db.get('ReassignMaster', 'componentDir');
  404. };
  405. App.db.getConfigs = function () {
  406. return App.db.get('app', 'configs');
  407. };
  408. App.db.getReassignMasterWizardReassignHosts = function () {
  409. return App.db.get('ReassignMaster', 'reassignHosts');
  410. };
  411. module.exports = App.db;