hdfs.ko.js 12 KB

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