hdfs.ko.js 12 KB

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