hdfs.ko.js 14 KB

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