hdfs.ko.js 9.2 KB

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