hdfs.ko.js 18 KB

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