hdfs.ko.js 7.9 KB

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