hdfs.ko.js 14 KB

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