volumes.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { docker } from '../server.js';
  2. export const Volumes = async function(req, res) {
  3. let container_volumes = [];
  4. let volume_list = '';
  5. // Table header
  6. volume_list = `<thead>
  7. <tr>
  8. <th class="w-1"><input class="form-check-input m-0 align-middle" name="select" type="checkbox" aria-label="Select all" onclick="selectAll()"></th>
  9. <th><label class="table-sort" data-sort="sort-type">Type</label></th>
  10. <th><label class="table-sort" data-sort="sort-name">Name</label></th>
  11. <th><label class="table-sort" data-sort="sort-city">Mount point</label></th>
  12. <th><label class="table-sort" data-sort="sort-score">Status</label></th>
  13. <th><label class="table-sort" data-sort="sort-date">Created</label></th>
  14. <th><label class="table-sort" data-sort="sort-quantity">Size</label></th>
  15. <th><label class="table-sort" data-sort="sort-progress">Action</label></th>
  16. </tr>
  17. </thead>
  18. <tbody class="table-tbody">`
  19. // List all containers
  20. let containers = await docker.listContainers({ all: true });
  21. // Get the first 6 volumes from each container
  22. for (let i = 0; i < containers.length; i++) {
  23. try { container_volumes.push({type: containers[i].Mounts[0].Type, source: containers[i].Mounts[0].Source}); } catch { }
  24. try { container_volumes.push({type: containers[i].Mounts[1].Type, source: containers[i].Mounts[1].Source}); } catch { }
  25. try { container_volumes.push({type: containers[i].Mounts[2].Type, source: containers[i].Mounts[2].Source}); } catch { }
  26. try { container_volumes.push({type: containers[i].Mounts[3].Type, source: containers[i].Mounts[3].Source}); } catch { }
  27. try { container_volumes.push({type: containers[i].Mounts[4].Type, source: containers[i].Mounts[4].Source}); } catch { }
  28. try { container_volumes.push({type: containers[i].Mounts[5].Type, source: containers[i].Mounts[5].Source}); } catch { }
  29. }
  30. // List ALL volumes
  31. let list = await docker.listVolumes({ all: true });
  32. let volumes = list.Volumes;
  33. // Create a table row for each volume
  34. for (let i = 0; i < volumes.length; i++) {
  35. let volume = volumes[i];
  36. let name = "" + volume.Name;
  37. let mount = "" + volume.Mountpoint;
  38. let type = "Bind";
  39. // Check if the volume is being used by any of the containers
  40. let status = '';
  41. if (container_volumes.some(volume => volume.source === mount)) { status = "In use"; }
  42. if (container_volumes.some(volume => volume.source === mount && volume.type === 'volume')) { type = "Volume"; }
  43. let row = `
  44. <tr>
  45. <td><input class="form-check-input m-0 align-middle" name="select" value="${name}" type="checkbox" aria-label="Select"></td>
  46. <td class="sort-type">${type}</td>
  47. <td class="sort-name">${name}</td>
  48. <td class="sort-city">${mount}</td>
  49. <td class="sort-score text-green">${status}</td>
  50. <td class="sort-date" data-date="1628122643">${volume.CreatedAt}</td>
  51. <td class="sort-quantity">MB</td>
  52. <td class="text-end"><a class="btn" href="#">Details</a></td>
  53. </tr>`
  54. volume_list += row;
  55. }
  56. volume_list += `</tbody>`
  57. res.render("volumes", {
  58. name: req.session.user,
  59. role: req.session.role,
  60. avatar: req.session.user.charAt(0).toUpperCase(),
  61. volume_list: volume_list,
  62. volume_count: volumes.length,
  63. alert: '',
  64. });
  65. }
  66. export const addVolume = async function(req, res) {
  67. let volume = req.body.volume;
  68. docker.createVolume({
  69. Name: volume
  70. });
  71. res.redirect("/volumes");
  72. }
  73. export const removeVolume = async function(req, res) {
  74. let volumes = req.body.select;
  75. if (typeof(volumes) == 'string') {
  76. volumes = [volumes];
  77. }
  78. for (let i = 0; i < volumes.length; i++) {
  79. if (volumes[i] != 'on') {
  80. try {
  81. console.log(`Removing volume: ${volumes[i]}`);
  82. let volume = docker.getVolume(volumes[i]);
  83. await volume.remove();
  84. } catch (error) {
  85. console.log(`Unable to remove volume: ${volumes[i]}`);
  86. }
  87. }
  88. }
  89. res.redirect("/volumes");
  90. }
  91. // docker.df(volume.Name).then((data) => {
  92. // for (let key in data) {
  93. // console.log(data[key]);
  94. // }
  95. // });