hdfs.ko.js 12 KB

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