db.js 13 KB

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