hdfs.ko.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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.isLoadingAcls = ko.observable(false);
  51. self.showAclsAsText = ko.observable(false);
  52. self.isDiffMode = ko.observable(false);
  53. self.treeCollapseStatus = {};
  54. self.treeLoadingStatus = {};
  55. self.treeData = ko.observable({nodes: []});
  56. self.loadData = function (data) {
  57. self.treeData(new TreeNodeModel(data));
  58. };
  59. self.growingTree = ko.observable({
  60. name: "__HUEROOT__",
  61. path: "__HUEROOT__",
  62. aclBit: false,
  63. striked: false,
  64. selected: false,
  65. nodes: [
  66. {
  67. name: "/",
  68. path: "/",
  69. isDir: true,
  70. isExpanded: true,
  71. aclBit: false,
  72. striked: false,
  73. selected: false,
  74. nodes: []
  75. }
  76. ]
  77. });
  78. self.path = ko.observable('');
  79. self.path.subscribe(function (path) {
  80. self.fetchPath();
  81. window.location.hash = path;
  82. });
  83. self.files = ko.observableArray();
  84. self.acls = ko.observableArray();
  85. self.originalAcls = ko.observableArray();
  86. self.regularAcls = ko.computed(function () {
  87. return $.grep(self.acls(), function (acl) {
  88. return !acl.isDefault();
  89. });
  90. });
  91. self.defaultAcls = ko.computed(function () {
  92. return $.grep(self.acls(), function (acl) {
  93. return acl.isDefault();
  94. });
  95. });
  96. self.changedAcls = ko.computed(function () {
  97. return $.grep(self.acls(), function (acl) {
  98. return ['new', 'deleted', 'modified'].indexOf(acl.status()) != -1;
  99. });
  100. });
  101. self.owner = ko.observable('');
  102. self.group = ko.observable('');
  103. self.afterRender = function() {
  104. $(document).trigger("rendered.tree");
  105. }
  106. self.addAcl = function () {
  107. var newAcl = parseAcl('group::---');
  108. newAcl.status('new');
  109. self.acls.push(newAcl);
  110. };
  111. self.addDefaultAcl = function () {
  112. var newAcl = parseAcl('default:group::---');
  113. newAcl.status('new');
  114. self.acls.push(newAcl);
  115. };
  116. self.removeAcl = function (acl) {
  117. if (acl.status() == 'new') {
  118. self.acls.remove(acl);
  119. } else {
  120. acl.status('deleted');
  121. }
  122. };
  123. self.convertItemToObject = function (item) {
  124. if (item.path != null) {
  125. var _path = item.path;
  126. var _parent = _path.substr(0, _path.lastIndexOf("/"));
  127. if (_parent == "") {
  128. _parent = "/";
  129. }
  130. if (_path != "/") {
  131. self.growingTree(self.traversePath(self.growingTree(), _parent, item));
  132. }
  133. }
  134. }
  135. self.traversePath = function (leaf, parent, item) {
  136. var _mainFound = false;
  137. leaf.nodes.forEach(function (node) {
  138. if (node.path == item.path) {
  139. _mainFound = true;
  140. }
  141. if (parent.indexOf(node.path) > -1) {
  142. self.traversePath(node, parent, item);
  143. }
  144. });
  145. if (!_mainFound && leaf.path == parent) {
  146. var _chunks = item.path.split("/");
  147. leaf.nodes.push({
  148. name: _chunks[_chunks.length - 1],
  149. path: item.path,
  150. aclBit: item.rwx.indexOf('+') != -1,
  151. striked: item.striked != null,
  152. isExpanded: true,
  153. isDir: item.type == "dir",
  154. nodes: []
  155. });
  156. }
  157. return leaf;
  158. }
  159. self.updatePathExpanded = function (leaf, path, expanded) {
  160. if (leaf.path == path) {
  161. leaf.isExpanded = expanded;
  162. }
  163. if (leaf.nodes.length > 0) {
  164. leaf.nodes.forEach(function (node) {
  165. self.updatePathExpanded(node, path, expanded);
  166. });
  167. }
  168. return leaf;
  169. }
  170. self.setPath = function (obj) {
  171. if (self.treeLoadingStatus[obj.path()] != null && self.treeLoadingStatus[obj.path()]) {
  172. obj.isExpanded(!obj.isExpanded());
  173. self.updatePathExpanded(self.growingTree(), obj.path(), obj.isExpanded());
  174. self.treeCollapseStatus[obj.path()] = obj.isExpanded();
  175. }
  176. self.path(obj.path());
  177. }
  178. self.openPath = function (obj) {
  179. window.open("/filebrowser/view" + obj.path(), '_blank');
  180. }
  181. self.loadParents = function (breadcrumbs) {
  182. if (typeof breadcrumbs != "undefined" && breadcrumbs != null) {
  183. breadcrumbs.forEach(function (crumb, idx) {
  184. if (idx < breadcrumbs.length - 1 && crumb.url != "") {
  185. var _item = {
  186. path: crumb.url,
  187. name: crumb.label,
  188. rwx: "",
  189. isDir: true
  190. }
  191. self.convertItemToObject(_item);
  192. }
  193. });
  194. }
  195. }
  196. self.fetchPath = function () {
  197. $.getJSON('/security/api/hdfs/list' + self.path(), {
  198. 'pagesize': 15,
  199. 'format': 'json',
  200. 'doas': vm.doAs(),
  201. 'isDiffMode': self.isDiffMode(),
  202. }, function (data) {
  203. self.treeLoadingStatus[self.path()] = true;
  204. self.loadParents(data.breadcrumbs);
  205. if (data['files'] && data['files'][0]['type'] == 'dir') { // Hack for now
  206. self.files.removeAll();
  207. $.each(data.files, function (index, item) {
  208. self.convertItemToObject(item);
  209. self.files.push(ko.mapping.fromJS({
  210. 'path': item.path,
  211. 'aclBit': item.rwx.indexOf('+') != -1,
  212. 'striked': item.striked != null,
  213. })
  214. );
  215. });
  216. }
  217. else {
  218. self.convertItemToObject(data);
  219. }
  220. self.loadData(self.growingTree());
  221. self.getAcls();
  222. }).fail(function (xhr, textStatus, errorThrown) {
  223. $(document).trigger("error", xhr.responseText);
  224. });
  225. };
  226. self.getAcls = function () {
  227. $(".jHueNotify").hide();
  228. var _isLoading = window.setTimeout(function () {
  229. self.isLoadingAcls(true);
  230. }, 1000);
  231. logGA('get_acls');
  232. $.getJSON('/security/api/hdfs/get_acls', {
  233. 'path': self.path()
  234. }, function (data) {
  235. window.clearTimeout(_isLoading);
  236. if (data != null) {
  237. self.acls.removeAll();
  238. self.originalAcls.removeAll();
  239. $.each(data.entries, function (index, item) {
  240. self.acls.push(parseAcl(item));
  241. self.originalAcls.push(parseAcl(item));
  242. });
  243. self.owner(data.owner);
  244. self.group(data.group);
  245. self.isLoadingAcls(false);
  246. $(document).trigger("loaded.acls");
  247. }
  248. }).fail(function (xhr, textStatus, errorThrown) {
  249. if (xhr.responseText.search('FileNotFoundException') == -1) { // TODO only fetch on existing path
  250. $(document).trigger("error", xhr.responseText);
  251. self.isLoadingAcls(false);
  252. }
  253. });
  254. };
  255. self.updateAcls = function () {
  256. $(".jHueNotify").hide();
  257. logGA('updateAcls');
  258. $.post("/security/api/hdfs/update_acls", {
  259. 'path': self.path(),
  260. 'acls': ko.mapping.toJSON(self.acls()),
  261. 'originalAcls': ko.mapping.toJSON(self.originalAcls())
  262. }, function (data) {
  263. var toDelete = []
  264. $.each(self.acls(), function (index, item) {
  265. if (item.status() == 'deleted') {
  266. toDelete.push(item);
  267. } else {
  268. item.status('');
  269. }
  270. });
  271. $.each(toDelete, function (index, item) {
  272. self.acls.remove(item);
  273. });
  274. $(document).trigger("info", 'Done!');
  275. }
  276. ).fail(function (xhr, textStatus, errorThrown) {
  277. $(document).trigger("error", JSON.parse(xhr.responseText).message);
  278. });
  279. }
  280. }
  281. var HdfsViewModel = function (initial) {
  282. var self = this;
  283. self.assist = new Assist(self, initial);
  284. self.doAs = ko.observable('');
  285. self.doAs.subscribe(function () {
  286. self.assist.fetchPath();
  287. });
  288. self.availableHadoopUsers = ko.observableArray();
  289. self.availableHadoopGroups = ko.observableArray();
  290. self.init = function (path) {
  291. self.fetchUsers();
  292. self.assist.path(path);
  293. }
  294. self.fetchUsers = function () {
  295. $.getJSON('/desktop/api/users/autocomplete', function (data) {
  296. $.each(data.users, function (i, user) {
  297. self.availableHadoopUsers.push(user.username);
  298. });
  299. $.each(data.groups, function (i, group) {
  300. self.availableHadoopGroups.push(group.name);
  301. });
  302. });
  303. }
  304. };
  305. function logGA(page) {
  306. if (typeof trackOnGA == 'function') {
  307. trackOnGA('security/hdfs' + page);
  308. }
  309. }