hdfs.ko.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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() + "?pagesize=15&format=json", function (data) {
  150. self.loadParents(data.breadcrumbs);
  151. if (data['files'] && data['files'][0]['type'] == 'dir') { // Hack for now
  152. self.files.removeAll();
  153. $.each(data.files, function (index, item) {
  154. self.convertItemToObject(item);
  155. self.files.push(ko.mapping.fromJS({
  156. 'path': item.path,
  157. 'aclBit': item.rwx.indexOf('+') != -1
  158. })
  159. );
  160. });
  161. self.loadData(self.growingTree());
  162. }
  163. self.getAcls();
  164. }).fail(function (xhr, textStatus, errorThrown) {
  165. $(document).trigger("error", xhr.responseText);
  166. });
  167. };
  168. self.getAcls = function () {
  169. $(".jHueNotify").hide();
  170. logGA('get_acls');
  171. $.getJSON('/security/api/hdfs/get_acls', {
  172. 'path': self.path()
  173. }, function (data) {
  174. self.acls.removeAll();
  175. self.originalAcls.removeAll();
  176. $.each(data.entries, function (index, item) {
  177. self.acls.push(parseAcl(item));
  178. self.originalAcls.push(parseAcl(item));
  179. });
  180. self.owner(data.owner);
  181. self.group(data.group);
  182. }).fail(function (xhr, textStatus, errorThrown) {
  183. if (xhr.responseText.search('FileNotFoundException') == -1) { // TODO only fetch on existing path
  184. $(document).trigger("error", xhr.responseText);
  185. }
  186. });
  187. };
  188. self.updateAcls = function () {
  189. $(".jHueNotify").hide();
  190. logGA('updateAcls');
  191. $.post("/security/api/hdfs/update_acls", {
  192. 'path': self.path(),
  193. 'acls': ko.mapping.toJSON(self.acls()),
  194. 'originalAcls': ko.mapping.toJSON(self.originalAcls())
  195. }, function (data) {
  196. var toDelete = []
  197. $.each(self.acls(), function (index, item) {
  198. if (item.status() == 'deleted') {
  199. toDelete.push(item);
  200. } else {
  201. item.status('');
  202. }
  203. });
  204. $.each(toDelete, function (index, item) {
  205. self.acls.remove(item);
  206. });
  207. $(document).trigger("info", 'Done!');
  208. }
  209. ).fail(function (xhr, textStatus, errorThrown) {
  210. $(document).trigger("error", JSON.parse(xhr.responseText).message);
  211. });
  212. }
  213. }
  214. var NodeModel = function(data) {
  215. var self = this;
  216. self.isExpanded = ko.observable(true);
  217. self.description = ko.observable();
  218. self.name = ko.observable();
  219. self.nodes = ko.observableArray([]);
  220. self.toggleVisibility = function() {
  221. self.isExpanded(!self.isExpanded());
  222. };
  223. ko.mapping.fromJS(data, self.mapOptions, self);
  224. };
  225. NodeModel.prototype.mapOptions = {
  226. nodes: {
  227. create: function(args) {
  228. return new NodeModel(args.data);
  229. }
  230. }
  231. };
  232. // Might rename Assist to Acls and create Assist for the tree widget?
  233. var HdfsViewModel = function (initial) {
  234. var self = this;
  235. self.assist = new Assist(self, initial);
  236. //self.assist.path('/user/hue/nano');
  237. self.assist.path('/user/hue/oozie-oozi/0000001-140325184036225-oozie-oozi-W/pig--pig');
  238. self.availableHadoopUsers = ko.observableArray();
  239. self.availableHadoopGroups = ko.observableArray();
  240. self.init = function () {
  241. self.fetchUsers();
  242. }
  243. self.fetchUsers = function () {
  244. $.getJSON('/desktop/api/users/autocomplete', function (data) {
  245. $.each(data.users, function (i, user) {
  246. self.availableHadoopUsers.push(user.username);
  247. });
  248. $.each(data.groups, function (i, group) {
  249. self.availableHadoopGroups.push(group.name);
  250. });
  251. });
  252. }
  253. };
  254. function logGA(page) {
  255. if (typeof trackOnGA == 'function') {
  256. trackOnGA('security/hdfs' + page);
  257. }
  258. }