hdfs.ko.js 19 KB

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