hdfs.ko.js 8.3 KB

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