hdfs.ko.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. // Licensed to Cloudera, Inc. under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. Cloudera, Inc. licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. function parseAcl(acl) {
  17. // (default:)?(user|group|mask|other):[[A-Za-z_][A-Za-z0-9._-]]*:([rwx-]{3})?
  18. m = acl.match(/(default:)?(user|group|mask|other):(.*?):(.)(.)(.)/);
  19. var acl = ko.mapping.fromJS({
  20. 'isDefault': m[1] != null,
  21. 'type': m[2],
  22. 'name': m[3],
  23. 'r': m[4] != '-',
  24. 'w': m[5] != '-',
  25. 'x': m[6] != '-',
  26. 'status': ''
  27. });
  28. acl.type.subscribe(function () {
  29. acl.status('modified');
  30. });
  31. acl.name.subscribe(function () {
  32. acl.status('modified');
  33. });
  34. acl.r.subscribe(function () {
  35. acl.status('modified');
  36. });
  37. acl.w.subscribe(function () {
  38. acl.status('modified');
  39. });
  40. acl.x.subscribe(function () {
  41. acl.status('modified');
  42. });
  43. return acl;
  44. }
  45. function printAcl(acl) {
  46. return (acl.isDefault() ? 'default:' : '') + acl.type() + ':' + acl.name() + ':' + (acl.r() ? 'r' : '-') + (acl.w() ? 'w' : '-') + (acl.x() ? 'x' : '-');
  47. }
  48. var Assist = function (vm, assist) {
  49. var self = this;
  50. self.compareNames = function (a, b) {
  51. if (a.name.toLowerCase() < b.name.toLowerCase())
  52. return -1;
  53. if (a.name.toLowerCase() > b.name.toLowerCase())
  54. return 1;
  55. return 0;
  56. }
  57. self.isLoadingAcls = ko.observable(false);
  58. self.isLoadingTree = ko.observable(false);
  59. self.showAclsAsText = ko.observable(false);
  60. self.isDiffMode = ko.observable(false);
  61. self.isDiffMode.subscribe(function () {
  62. self.refreshTree();
  63. });
  64. self.treeAdditionalData = {};
  65. self.treeData = ko.observable({nodes: []});
  66. self.loadData = function (data) {
  67. self.treeData(new TreeNodeModel(data));
  68. };
  69. self.initialGrowingTree = {
  70. name: "__HUEROOT__",
  71. path: "__HUEROOT__",
  72. aclBit: false,
  73. striked: false,
  74. selected: false,
  75. rwx: "",
  76. page: {
  77. number: -1,
  78. num_pages: -1
  79. },
  80. nodes: [
  81. {
  82. name: "/",
  83. path: "/",
  84. isDir: true,
  85. isExpanded: false,
  86. isChecked: false,
  87. aclBit: false,
  88. striked: false,
  89. selected: false,
  90. rwx: "",
  91. page: {
  92. number: -1,
  93. num_pages: -1
  94. },
  95. nodes: []
  96. }
  97. ]
  98. };
  99. self.growingTree = ko.observable(jQuery.extend(true, {}, self.initialGrowingTree));
  100. self.path = ko.observable('');
  101. self.path.subscribe(function (path) {
  102. self.pagenum(1);
  103. self.fetchPath();
  104. window.location.hash = path;
  105. });
  106. self.recursive = ko.observable(false);
  107. self.pagenum = ko.observable(1);
  108. self.fromLoadMore = false;
  109. self.fromRebuildTree = false;
  110. self.acls = ko.observableArray();
  111. self.originalAcls = ko.observableArray();
  112. self.regularAcls = ko.computed(function () {
  113. return $.grep(self.acls(), function (acl) {
  114. return !acl.isDefault();
  115. });
  116. });
  117. self.defaultAcls = ko.computed(function () {
  118. return $.grep(self.acls(), function (acl) {
  119. return acl.isDefault();
  120. });
  121. });
  122. self.changedRegularAcls = ko.computed(function () {
  123. return $.grep(self.regularAcls(), function (acl) {
  124. return ['new', 'deleted', 'modified'].indexOf(acl.status()) != -1;
  125. });
  126. });
  127. self.changedDefaultAcls = ko.computed(function () {
  128. return $.grep(self.defaultAcls(), function (acl) {
  129. return ['new', 'deleted', 'modified'].indexOf(acl.status()) != -1;
  130. });
  131. });
  132. self.owner = ko.observable('');
  133. self.group = ko.observable('');
  134. self.checkedItems = ko.observableArray([]);
  135. self.afterRender = function () {
  136. if (!self.fromLoadMore && !self.fromRebuildTree) {
  137. $(document).trigger("rendered.tree");
  138. }
  139. self.fromLoadMore = false;
  140. self.fromRebuildTree = false;
  141. }
  142. self.addAcl = function () {
  143. var newAcl = parseAcl('group::---');
  144. newAcl.status('new');
  145. self.acls.push(newAcl);
  146. };
  147. self.addDefaultAcl = function () {
  148. var newAcl = parseAcl('default:group::---');
  149. newAcl.status('new');
  150. self.acls.push(newAcl);
  151. };
  152. self.removeAcl = function (acl) {
  153. if (acl.status() == 'new') {
  154. self.acls.remove(acl);
  155. } else {
  156. acl.status('deleted');
  157. }
  158. };
  159. self.convertItemToObject = function (item) {
  160. if (item.path != null && item.name != "." && item.name != "..") {
  161. var _path = item.path;
  162. var _parent = _path.substr(0, _path.lastIndexOf("/"));
  163. if (_parent == "") {
  164. _parent = "/";
  165. }
  166. if (_path != "/") {
  167. self.growingTree(self.traversePath(self.growingTree(), _parent, item));
  168. }
  169. }
  170. }
  171. self.traversePath = function (leaf, parent, item) {
  172. var _mainFound = false;
  173. leaf.nodes.forEach(function (node) {
  174. if (node.path == item.path) {
  175. _mainFound = true;
  176. }
  177. if (parent.indexOf(node.path) > -1) {
  178. self.traversePath(node, parent, item);
  179. }
  180. });
  181. if (!_mainFound && leaf.path == parent) {
  182. var _chunks = item.path.split("/");
  183. leaf.nodes.push({
  184. name: _chunks[_chunks.length - 1],
  185. path: item.path,
  186. aclBit: item.rwx.indexOf('+') != -1,
  187. striked: item.striked != null,
  188. isExpanded: false,
  189. isChecked: false,
  190. rwx: item.rwx,
  191. isDir: item.type == "dir" || item.isDir == true,
  192. page: {
  193. number: -1,
  194. num_pages: -1
  195. },
  196. nodes: []
  197. });
  198. leaf.nodes.sort(self.compareNames);
  199. }
  200. return leaf;
  201. }
  202. self.getTreeAdditionalDataForPath = function (path) {
  203. if (typeof self.treeAdditionalData[path] == "undefined") {
  204. var _add = {
  205. loaded: false,
  206. expanded: true
  207. }
  208. self.treeAdditionalData[path] = _add;
  209. }
  210. return self.treeAdditionalData[path];
  211. }
  212. self.updatePathProperty = function (leaf, path, property, value) {
  213. if (leaf.path == path) {
  214. leaf[property] = value;
  215. }
  216. if (leaf.nodes.length > 0) {
  217. leaf.nodes.forEach(function (node) {
  218. self.updatePathProperty(node, path, property, value);
  219. });
  220. }
  221. return leaf;
  222. }
  223. self.updateTreeProperty = function (leaf, property, value) {
  224. leaf[property] = value;
  225. if (leaf.nodes.length > 0) {
  226. leaf.nodes.forEach(function (node) {
  227. self.updateTreeProperty(node, property, value);
  228. });
  229. }
  230. return leaf;
  231. }
  232. self.collapseTree = function () {
  233. self.updateTreeProperty(self.growingTree(), "isExpanded", false);
  234. self.updatePathProperty(self.growingTree(), "/", "isExpanded", true);
  235. self.loadData(self.growingTree());
  236. }
  237. self.collapseOthers = function () {
  238. self.updateTreeProperty(self.growingTree(), "isExpanded", false);
  239. self.updatePathProperty(self.growingTree(), "/", "isExpanded", true);
  240. var _path = self.path();
  241. var _crumb = "";
  242. for (var i = 0; i < _path.length; i++) {
  243. if ((_path[i] === "/" && _crumb != "")) {
  244. self.updatePathProperty(self.growingTree(), _crumb, "isExpanded", true);
  245. }
  246. _crumb += _path[i];
  247. }
  248. self.updatePathProperty(self.growingTree(), _path, "isExpanded", true);
  249. self.loadData(self.growingTree());
  250. }
  251. self.expandTree = function () {
  252. self.updateTreeProperty(self.growingTree(), "isExpanded", true);
  253. self.loadData(self.growingTree());
  254. }
  255. self.refreshTree = function (force) {
  256. self.growingTree(jQuery.extend(true, {}, self.initialGrowingTree));
  257. Object.keys(self.treeAdditionalData).forEach(function (path) {
  258. if (typeof force == "boolean" && force) {
  259. self.fetchPath(path, function () {
  260. self.updatePathProperty(self.growingTree(), path, "isExpanded", self.treeAdditionalData[path].expanded);
  261. self.loadData(self.growingTree());
  262. });
  263. } else {
  264. if (self.treeAdditionalData[path].loaded) {
  265. self.fetchPath(path, function () {
  266. self.updatePathProperty(self.growingTree(), path, "isExpanded", self.treeAdditionalData[path].expanded);
  267. self.loadData(self.growingTree());
  268. });
  269. }
  270. }
  271. });
  272. self.getAcls();
  273. }
  274. self.rebuildTree = function (leaf, paths) {
  275. paths.push(leaf.path);
  276. if (leaf.nodes.length > 0) {
  277. leaf.nodes.forEach(function (node) {
  278. if (node.isDir) {
  279. self.rebuildTree(node, paths);
  280. }
  281. });
  282. }
  283. return paths;
  284. }
  285. self.setPath = function (obj, toggle) {
  286. if (self.getTreeAdditionalDataForPath(obj.path()).loaded || (!obj.isExpanded() && !self.getTreeAdditionalDataForPath(obj.path()).loaded)) {
  287. if (typeof toggle == "boolean" && toggle) {
  288. obj.isExpanded(!obj.isExpanded());
  289. self.getTreeAdditionalDataForPath(obj.path()).expanded = obj.isExpanded();
  290. }
  291. self.updatePathProperty(self.growingTree(), obj.path(), "isExpanded", obj.isExpanded());
  292. }
  293. else {
  294. if (typeof toggle == "boolean" && toggle) {
  295. obj.isExpanded(!obj.isExpanded());
  296. } else {
  297. obj.isExpanded(false);
  298. }
  299. self.getTreeAdditionalDataForPath(obj.path()).expanded = obj.isExpanded();
  300. self.updatePathProperty(self.growingTree(), obj.path(), "isExpanded", obj.isExpanded());
  301. }
  302. self.path(obj.path());
  303. }
  304. self.togglePath = function (obj) {
  305. self.setPath(obj, true);
  306. }
  307. self.getCheckedItems = function (leaf, checked) {
  308. if (leaf == null){
  309. leaf = self.growingTree();
  310. }
  311. if (checked == null){
  312. checked = []
  313. }
  314. if (leaf.isChecked){
  315. checked.push(leaf);
  316. }
  317. if (leaf.nodes.length > 0) {
  318. leaf.nodes.forEach(function (node) {
  319. self.getCheckedItems(node, checked);
  320. });
  321. }
  322. return checked;
  323. }
  324. self.checkPath = function (obj) {
  325. obj.isChecked(!obj.isChecked());
  326. self.updatePathProperty(self.growingTree(), obj.path(), "isChecked", obj.isChecked());
  327. self.checkedItems(self.getCheckedItems());
  328. }
  329. self.openPath = function (obj) {
  330. window.open("/filebrowser/view" + obj.path(), '_blank');
  331. }
  332. self.loadParents = function (breadcrumbs) {
  333. if (typeof breadcrumbs != "undefined" && breadcrumbs != null) {
  334. breadcrumbs.forEach(function (crumb, idx) {
  335. if (idx < breadcrumbs.length - 1 && crumb.url != "") {
  336. var _item = {
  337. path: crumb.url,
  338. name: crumb.label,
  339. rwx: "",
  340. isDir: true,
  341. page: {
  342. number: -1,
  343. num_pages: -1
  344. }
  345. }
  346. self.convertItemToObject(_item);
  347. }
  348. });
  349. $(document).trigger("loaded.parents");
  350. }
  351. }
  352. self.fetchPath = function (optionalPath, loadCallback) {
  353. var _path = typeof optionalPath != "undefined" ? optionalPath : self.path();
  354. $.getJSON('/security/api/hdfs/list' + _path, {
  355. 'pagesize': 15,
  356. 'pagenum': self.pagenum(),
  357. 'format': 'json',
  358. 'doas': vm.doAs(),
  359. 'isDiffMode': self.isDiffMode()
  360. },
  361. function (data) {
  362. if (data.error != null && data.error == "FILE_NOT_FOUND") {
  363. self.path("/");
  364. }
  365. else {
  366. self.loadParents(data.breadcrumbs);
  367. if (data['files'] && data['files'][0] && data['files'][0]['type'] == 'dir') { // Hack for now
  368. $.each(data.files, function (index, item) {
  369. self.convertItemToObject(item);
  370. });
  371. }
  372. else {
  373. self.convertItemToObject(data);
  374. }
  375. self.getTreeAdditionalDataForPath(_path).loaded = true;
  376. if (data.page != null && data.page.number != null) {
  377. self.updatePathProperty(self.growingTree(), _path, "page", data.page);
  378. }
  379. if (typeof loadCallback != "undefined") {
  380. loadCallback(data);
  381. }
  382. else {
  383. self.loadData(self.growingTree());
  384. }
  385. if (typeof optionalPath == "undefined") {
  386. self.getAcls();
  387. }
  388. }
  389. }).fail(function (xhr, textStatus, errorThrown) {
  390. $(document).trigger("error", xhr.responseText);
  391. });
  392. };
  393. self.loadMore = function (what) {
  394. self.pagenum(what.page().next_page_number());
  395. self.fetchPath(what.path());
  396. self.fromLoadMore = true;
  397. }
  398. self.getAcls = function () {
  399. $(".jHueNotify").hide();
  400. var _isLoading = window.setTimeout(function () {
  401. self.isLoadingAcls(true);
  402. }, 1000);
  403. logGA('get_acls');
  404. $.getJSON('/security/api/hdfs/get_acls', {
  405. 'path': self.path()
  406. }, function (data) {
  407. window.clearTimeout(_isLoading);
  408. if (data != null) {
  409. self.acls.removeAll();
  410. self.originalAcls.removeAll();
  411. $.each(data.entries, function (index, item) {
  412. self.acls.push(parseAcl(item));
  413. self.originalAcls.push(parseAcl(item));
  414. });
  415. self.owner(data.owner);
  416. self.group(data.group);
  417. self.isLoadingAcls(false);
  418. $(document).trigger("loaded.acls");
  419. }
  420. }).fail(function (xhr, textStatus, errorThrown) {
  421. if (xhr.responseText.search('FileNotFoundException') == -1) { // TODO only fetch on existing path
  422. $(document).trigger("error", xhr.responseText);
  423. self.isLoadingAcls(false);
  424. }
  425. });
  426. };
  427. self.updateAcls = function () {
  428. $(".jHueNotify").hide();
  429. logGA('updateAcls');
  430. $.post("/security/api/hdfs/update_acls", {
  431. 'path': self.path(),
  432. 'acls': ko.mapping.toJSON(self.acls()),
  433. 'originalAcls': ko.mapping.toJSON(self.originalAcls())
  434. }, function (data) {
  435. var toDelete = []
  436. $.each(self.acls(), function (index, item) {
  437. if (item.status() == 'deleted') {
  438. toDelete.push(item);
  439. } else {
  440. item.status('');
  441. }
  442. });
  443. $.each(toDelete, function (index, item) {
  444. self.acls.remove(item);
  445. });
  446. self.refreshTree();
  447. $(document).trigger("updated.acls");
  448. }
  449. ).fail(function (xhr, textStatus, errorThrown) {
  450. $(document).trigger("error", JSON.parse(xhr.responseText).message);
  451. });
  452. }
  453. self.bulkAction = ko.observable("");
  454. self.bulkPerfomAction = function () {
  455. switch (self.bulkAction()) {
  456. case "add":
  457. self.bulkAddAcls();
  458. break;
  459. case "sync":
  460. self.bulkSyncAcls();
  461. break;
  462. case "delete":
  463. self.bulkDeleteAcls();
  464. break;
  465. }
  466. self.bulkAction("");
  467. }
  468. self.bulkDeleteAcls = function () {
  469. $(".jHueNotify").hide();
  470. logGA('bulkDeleteAcls');
  471. var checkedPaths = self.checkedItems();
  472. $.post("/security/api/hdfs/bulk_delete_acls", {
  473. 'path': self.path(),
  474. 'checkedPaths': ko.mapping.toJSON(checkedPaths),
  475. 'recursive': ko.mapping.toJSON(self.recursive())
  476. }, function (data) {
  477. if (checkedPaths.indexOf(self.path()) != -1) {
  478. self.acls.removeAll();
  479. }
  480. self.refreshTree();
  481. $(document).trigger("deleted.bulk.acls");
  482. }
  483. ).fail(function (xhr, textStatus, errorThrown) {
  484. $(document).trigger("error", JSON.parse(xhr.responseText).message);
  485. });
  486. }
  487. self.bulkAddAcls = function () {
  488. $(".jHueNotify").hide();
  489. logGA('bulkAddAcls');
  490. var checkedPaths = self.checkedItems();
  491. $.post("/security/api/hdfs/bulk_add_acls", {
  492. 'path': self.path(),
  493. 'acls': ko.mapping.toJSON(self.acls()),
  494. 'checkedPaths': ko.mapping.toJSON(checkedPaths),
  495. 'recursive': ko.mapping.toJSON(self.recursive())
  496. }, function (data) {
  497. self.refreshTree();
  498. $(document).trigger("added.bulk.acls");
  499. }
  500. ).fail(function (xhr, textStatus, errorThrown) {
  501. $(document).trigger("error", JSON.parse(xhr.responseText).message);
  502. });
  503. }
  504. self.bulkSyncAcls = function () {
  505. $(".jHueNotify").hide();
  506. logGA('bulkSyncAcls');
  507. var checkedPaths = self.checkedItems();
  508. $.post("/security/api/hdfs/bulk_sync_acls", {
  509. 'path': self.path(),
  510. 'acls': ko.mapping.toJSON(self.acls()),
  511. 'checkedPaths': ko.mapping.toJSON(checkedPaths),
  512. 'recursive': ko.mapping.toJSON(self.recursive())
  513. }, function (data) {
  514. self.refreshTree();
  515. $(document).trigger("syncd.bulk.acls");
  516. }
  517. ).fail(function (xhr, textStatus, errorThrown) {
  518. $(document).trigger("error", JSON.parse(xhr.responseText).message);
  519. });
  520. }
  521. }
  522. var HdfsViewModel = function (initial) {
  523. var self = this;
  524. self.assist = new Assist(self, initial);
  525. self.doAs = ko.observable(initial.user);
  526. self.doAs.subscribe(function () {
  527. self.assist.refreshTree();
  528. });
  529. self.availableHadoopUsers = ko.observableArray();
  530. self.availableHadoopGroups = ko.observableArray();
  531. self.selectableHadoopUsers = ko.computed(function () {
  532. var _users = ko.utils.arrayMap(self.availableHadoopUsers(), function (user) {
  533. return user.username;
  534. });
  535. return _users.sort();
  536. }, self);
  537. self.selectableHadoopGroups = ko.computed(function () {
  538. var _users = ko.utils.arrayMap(self.availableHadoopGroups(), function (group) {
  539. return group.name;
  540. });
  541. return _users.sort();
  542. }, self);
  543. self.init = function (path) {
  544. self.fetchUsers();
  545. self.assist.path(path);
  546. $(document).one("loaded.parents", function () {
  547. self.assist.isLoadingTree(true);
  548. var _paths = self.assist.rebuildTree(self.assist.growingTree().nodes[0], []);
  549. _paths.forEach(function (ipath, cnt) {
  550. self.assist.updatePathProperty(self.assist.growingTree(), ipath, "isExpanded", true);
  551. self.assist.fetchPath(ipath, function () {
  552. if (cnt == _paths.length - 1) {
  553. self.assist.fetchPath(path, function () {
  554. self.assist.updatePathProperty(self.assist.growingTree(), path, "isExpanded", true);
  555. self.assist.fromRebuildTree = true;
  556. self.assist.loadData(self.assist.growingTree());
  557. self.assist.isLoadingTree(false);
  558. });
  559. }
  560. });
  561. });
  562. });
  563. }
  564. self.fetchUsers = function () {
  565. $.getJSON('/desktop/api/users/autocomplete', {
  566. 'include_myself': true,
  567. 'extend_user': true
  568. }, function (data) {
  569. self.availableHadoopUsers(data.users);
  570. self.availableHadoopGroups(data.groups);
  571. $(document).trigger("loaded.users");
  572. });
  573. }
  574. };
  575. function logGA(page) {
  576. if (typeof trackOnGA == 'function') {
  577. trackOnGA('security/hdfs' + page);
  578. }
  579. }