hdfs.ko.js 19 KB

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