hdfs.ko.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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.openPath = function (obj) {
  139. window.open("/filebrowser/view" + obj.path(), '_blank');
  140. }
  141. self.loadParents = function(breadcrumbs) {
  142. if (typeof breadcrumbs != "undefined" && breadcrumbs != null) {
  143. breadcrumbs.forEach(function (crumb, idx) {
  144. if (idx < breadcrumbs.length - 1 && crumb.url != "") {
  145. var _item = {
  146. path: crumb.url,
  147. name: crumb.label,
  148. rwx: ""
  149. }
  150. self.convertItemToObject(_item);
  151. }
  152. });
  153. }
  154. }
  155. self.fetchPath = function () {
  156. $.getJSON('/filebrowser/view' + self.path(), {
  157. 'pagesize': 15,
  158. 'format': 'json',
  159. 'doas': vm.doAs(),
  160. }, function (data) {
  161. self.loadParents(data.breadcrumbs);
  162. if (data['files'] && data['files'][0]['type'] == 'dir') { // Hack for now
  163. self.files.removeAll();
  164. $.each(data.files, function (index, item) {
  165. self.convertItemToObject(item);
  166. self.files.push(ko.mapping.fromJS({
  167. 'path': item.path,
  168. 'aclBit': item.rwx.indexOf('+') != -1
  169. })
  170. );
  171. });
  172. self.loadData(self.growingTree());
  173. }
  174. self.getAcls();
  175. }).fail(function (xhr, textStatus, errorThrown) {
  176. $(document).trigger("error", xhr.responseText);
  177. });
  178. };
  179. self.getAcls = function () {
  180. $(".jHueNotify").hide();
  181. var _isLoading = window.setTimeout(function(){
  182. self.isLoadingAcls(true);
  183. }, 1000);
  184. logGA('get_acls');
  185. $.getJSON('/security/api/hdfs/get_acls', {
  186. 'path': self.path()
  187. }, function (data) {
  188. window.clearTimeout(_isLoading);
  189. if (data != null) {
  190. self.acls.removeAll();
  191. self.originalAcls.removeAll();
  192. $.each(data.entries, function (index, item) {
  193. self.acls.push(parseAcl(item));
  194. self.originalAcls.push(parseAcl(item));
  195. });
  196. self.owner(data.owner);
  197. self.group(data.group);
  198. self.isLoadingAcls(false);
  199. $(document).trigger("loaded.acls");
  200. }
  201. }).fail(function (xhr, textStatus, errorThrown) {
  202. if (xhr.responseText.search('FileNotFoundException') == -1) { // TODO only fetch on existing path
  203. $(document).trigger("error", xhr.responseText);
  204. self.isLoadingAcls(false);
  205. }
  206. });
  207. };
  208. self.updateAcls = function () {
  209. $(".jHueNotify").hide();
  210. logGA('updateAcls');
  211. $.post("/security/api/hdfs/update_acls", {
  212. 'path': self.path(),
  213. 'acls': ko.mapping.toJSON(self.acls()),
  214. 'originalAcls': ko.mapping.toJSON(self.originalAcls())
  215. }, function (data) {
  216. var toDelete = []
  217. $.each(self.acls(), function (index, item) {
  218. if (item.status() == 'deleted') {
  219. toDelete.push(item);
  220. } else {
  221. item.status('');
  222. }
  223. });
  224. $.each(toDelete, function (index, item) {
  225. self.acls.remove(item);
  226. });
  227. $(document).trigger("info", 'Done!');
  228. }
  229. ).fail(function (xhr, textStatus, errorThrown) {
  230. $(document).trigger("error", JSON.parse(xhr.responseText).message);
  231. });
  232. }
  233. }
  234. var NodeModel = function(data) {
  235. var self = this;
  236. self.isExpanded = ko.observable(true);
  237. self.description = ko.observable();
  238. self.name = ko.observable();
  239. self.nodes = ko.observableArray([]);
  240. self.toggleVisibility = function() {
  241. self.isExpanded(! self.isExpanded());
  242. };
  243. ko.mapping.fromJS(data, self.mapOptions, self);
  244. };
  245. NodeModel.prototype.mapOptions = {
  246. nodes: {
  247. create: function(args) {
  248. return new NodeModel(args.data);
  249. }
  250. }
  251. };
  252. var HdfsViewModel = function (initial) {
  253. var self = this;
  254. self.assist = new Assist(self, initial);
  255. self.doAs = ko.observable('');
  256. self.doAs.subscribe(function() {
  257. self.assist.fetchPath();
  258. });
  259. self.availableHadoopUsers = ko.observableArray();
  260. self.availableHadoopGroups = ko.observableArray();
  261. self.init = function () {
  262. self.fetchUsers();
  263. self.assist.path('/');
  264. }
  265. self.fetchUsers = function () {
  266. $.getJSON('/desktop/api/users/autocomplete', function (data) {
  267. $.each(data.users, function (i, user) {
  268. self.availableHadoopUsers.push(user.username);
  269. });
  270. $.each(data.groups, function (i, group) {
  271. self.availableHadoopGroups.push(group.name);
  272. });
  273. });
  274. }
  275. };
  276. function logGA(page) {
  277. if (typeof trackOnGA == 'function') {
  278. trackOnGA('security/hdfs' + page);
  279. }
  280. }