hdfs.ko.js 16 KB

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