hdfs.ko.js 19 KB

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