hdfs.ko.js 14 KB

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