volumes.js 4.7 KB

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