hdfs.ko.js 14 KB

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