hdfs.ko.js 10 KB

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