hdfs.ko.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. console.log("Fetching", path);
  222. self.fetchPath(path);
  223. }
  224. });
  225. }
  226. self.setPath = function (obj) {
  227. if (self.getTreeAdditionalDataForPath(obj.path()).loaded || (! obj.isExpanded() && ! self.getTreeAdditionalDataForPath(obj.path()).loaded)) {
  228. obj.isExpanded(!obj.isExpanded());
  229. self.updatePathProperty(self.growingTree(), obj.path(), "isExpanded", obj.isExpanded());
  230. }
  231. self.path(obj.path());
  232. }
  233. self.openPath = function (obj) {
  234. window.open("/filebrowser/view" + obj.path(), '_blank');
  235. }
  236. self.loadParents = function (breadcrumbs) {
  237. if (typeof breadcrumbs != "undefined" && breadcrumbs != null) {
  238. breadcrumbs.forEach(function (crumb, idx) {
  239. if (idx < breadcrumbs.length - 1 && crumb.url != "") {
  240. var _item = {
  241. path: crumb.url,
  242. name: crumb.label,
  243. rwx: "",
  244. isDir: true
  245. }
  246. self.convertItemToObject(_item);
  247. }
  248. });
  249. }
  250. }
  251. self.fetchPath = function (optionalPath) {
  252. var _path = typeof optionalPath != "undefined" ? optionalPath : self.path();
  253. $.getJSON('/security/api/hdfs/list' + _path, {
  254. 'pagesize': 15,
  255. 'pagenum': self.pagenum(),
  256. 'format': 'json',
  257. 'doas': vm.doAs(),
  258. 'isDiffMode': self.isDiffMode()
  259. },
  260. function (data) {
  261. self.loadParents(data.breadcrumbs);
  262. if (data['files'] && data['files'][0] && data['files'][0]['type'] == 'dir') { // Hack for now
  263. self.files.removeAll();
  264. $.each(data.files, function (index, item) {
  265. self.convertItemToObject(item);
  266. self.files.push(ko.mapping.fromJS({
  267. 'path': item.path,
  268. 'aclBit': item.rwx.indexOf('+') != -1,
  269. 'striked': item.striked != null
  270. })
  271. );
  272. });
  273. }
  274. else {
  275. self.convertItemToObject(data);
  276. }
  277. self.getTreeAdditionalDataForPath(_path).loaded = true;
  278. self.updatePathProperty(self.growingTree(), _path, "page", data.page);
  279. self.loadData(self.growingTree());
  280. self.getAcls();
  281. }).fail(function (xhr, textStatus, errorThrown) {
  282. $(document).trigger("error", xhr.responseText);
  283. });
  284. };
  285. self.loadMore = function (what) {
  286. self.pagenum(what.page().next_page_number());
  287. self.fetchPath(what.path());
  288. }
  289. self.getAcls = function () {
  290. $(".jHueNotify").hide();
  291. var _isLoading = window.setTimeout(function () {
  292. self.isLoadingAcls(true);
  293. }, 1000);
  294. logGA('get_acls');
  295. $.getJSON('/security/api/hdfs/get_acls', {
  296. 'path': self.path()
  297. }, function (data) {
  298. window.clearTimeout(_isLoading);
  299. if (data != null) {
  300. self.acls.removeAll();
  301. self.originalAcls.removeAll();
  302. $.each(data.entries, function (index, item) {
  303. self.acls.push(parseAcl(item));
  304. self.originalAcls.push(parseAcl(item));
  305. });
  306. self.owner(data.owner);
  307. self.group(data.group);
  308. self.isLoadingAcls(false);
  309. $(document).trigger("loaded.acls");
  310. }
  311. }).fail(function (xhr, textStatus, errorThrown) {
  312. if (xhr.responseText.search('FileNotFoundException') == -1) { // TODO only fetch on existing path
  313. $(document).trigger("error", xhr.responseText);
  314. self.isLoadingAcls(false);
  315. }
  316. });
  317. };
  318. self.updateAcls = function () {
  319. $(".jHueNotify").hide();
  320. logGA('updateAcls');
  321. $.post("/security/api/hdfs/update_acls", {
  322. 'path': self.path(),
  323. 'acls': ko.mapping.toJSON(self.acls()),
  324. 'originalAcls': ko.mapping.toJSON(self.originalAcls())
  325. }, function (data) {
  326. var toDelete = []
  327. $.each(self.acls(), function (index, item) {
  328. if (item.status() == 'deleted') {
  329. toDelete.push(item);
  330. } else {
  331. item.status('');
  332. }
  333. });
  334. $.each(toDelete, function (index, item) {
  335. self.acls.remove(item);
  336. });
  337. $(document).trigger("info", 'Done!');
  338. }
  339. ).fail(function (xhr, textStatus, errorThrown) {
  340. $(document).trigger("error", JSON.parse(xhr.responseText).message);
  341. });
  342. }
  343. }
  344. var HdfsViewModel = function (initial) {
  345. var self = this;
  346. self.assist = new Assist(self, initial);
  347. self.doAs = ko.observable(initial.user);
  348. self.doAs.subscribe(function () {
  349. self.assist.fetchPath();
  350. });
  351. self.availableHadoopUsers = ko.observableArray();
  352. self.availableHadoopGroups = ko.observableArray();
  353. self.init = function (path) {
  354. self.fetchUsers();
  355. self.assist.path(path);
  356. }
  357. self.fetchUsers = function () {
  358. $.getJSON('/desktop/api/users/autocomplete', function (data) {
  359. $.each(data.users, function (i, user) {
  360. self.availableHadoopUsers.push(user.username);
  361. });
  362. $.each(data.groups, function (i, group) {
  363. self.availableHadoopGroups.push(group.name);
  364. });
  365. updateTypeAheads(self);
  366. });
  367. }
  368. };
  369. function logGA(page) {
  370. if (typeof trackOnGA == 'function') {
  371. trackOnGA('security/hdfs' + page);
  372. }
  373. }