db.js 14 KB

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