images.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { docker } from '../server.js';
  2. import { addAlert } from './dashboard.js';
  3. export const Images = async function(req, res) {
  4. let action = req.params.action;
  5. console.log(req.params.host);
  6. if (action == "remove") {
  7. let images = req.body.select;
  8. if (typeof(images) == 'string') {
  9. images = [images];
  10. }
  11. for (let i = 0; i < images.length; i++) {
  12. if (images[i] != 'on') {
  13. try {
  14. console.log(`Removing image: ${images[i]}`);
  15. let image = docker.getImage(images[i]);
  16. await image.remove();
  17. } catch (error) {
  18. console.log(`Unable to remove image: ${images[i]}`);
  19. }
  20. }
  21. }
  22. res.redirect("/images");
  23. return;
  24. } else if (action == "add") {
  25. let image = req.body.image;
  26. let tag = req.body.tag || 'latest';
  27. try {
  28. console.log(`Pulling image: ${image}:${tag}`);
  29. await docker.pull(`${image}:${tag}`);
  30. } catch (error) {
  31. console.log(`Unable to pull image: ${image}:${tag}`);
  32. }
  33. res.redirect("/images");
  34. return;
  35. }
  36. let containers = await docker.listContainers({ all: true });
  37. let container_images = [];
  38. for (let i = 0; i < containers.length; i++) {
  39. container_images.push(containers[i].Image);
  40. }
  41. let images = await docker.listImages({ all: true });
  42. let image_list = `
  43. <thead>
  44. <tr>
  45. <th class="w-1"><input class="form-check-input m-0 align-middle" name="select" type="checkbox" aria-label="Select all" onclick="selectAll()"></th>
  46. <th><label class="table-sort" data-sort="sort-name">Name</label></th>
  47. <th><label class="table-sort" data-sort="sort-type">Tag</label></th>
  48. <th><label class="table-sort" data-sort="sort-city">ID</label></th>
  49. <th><label class="table-sort" data-sort="sort-score">Status</label></th>
  50. <th><label class="table-sort" data-sort="sort-date">Created</label></th>
  51. <th><label class="table-sort" data-sort="sort-quantity">Size</label></th>
  52. <th><label class="table-sort" data-sort="sort-progress">Action</label></th>
  53. </tr>
  54. </thead>
  55. <tbody class="table-tbody">`
  56. for (let i = 0; i < images.length; i++) {
  57. let name = '';
  58. let tag = '';
  59. try { name = images[i].RepoTags[0].split(':')[0]; } catch {}
  60. try { tag = images[i].RepoTags[0].split(':')[1]; } catch {}
  61. let date = new Date(images[i].Created * 1000);
  62. let created = date.toLocaleDateString('en-US', { month: 'long', day: 'numeric', year: 'numeric' });
  63. let size = images[i].Size / 1000 / 1000; // to match docker desktop
  64. size = size.toFixed(2);
  65. let status = '';
  66. try {
  67. if (container_images.includes(images[i].RepoTags[0])) {
  68. status = 'In use';
  69. }
  70. } catch {}
  71. let details = `
  72. <tr>
  73. <td><input class="form-check-input m-0 align-middle" name="select" value="${images[i].Id}" type="checkbox" aria-label="Select"></td>
  74. <td class="sort-name">${name}</td>
  75. <td class="sort-type">${tag}</td>
  76. <td class="sort-city">${images[i].Id}</td>
  77. <td class="sort-score text-green">${status}</td>
  78. <td class="sort-date" data-date="1628122643">${created}</td>
  79. <td class="sort-quantity">${size} MB</td>
  80. <td class="text-end"><a class="btn" href="#"><svg xmlns="http://www.w3.org/2000/svg" class="icon-tabler icon-tabler-player-play" width="24" height="24" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"></path><path d="M7 4v16l13 -8z"></path></svg></a></td>
  81. </tr>`
  82. image_list += details;
  83. }
  84. image_list += `</tbody>`
  85. res.render("images", {
  86. username: req.session.username,
  87. role: req.session.role,
  88. avatar: req.session.username.charAt(0).toUpperCase(),
  89. image_list: image_list,
  90. image_count: images.length,
  91. alert: '',
  92. link1: '',
  93. link2: '',
  94. link3: '',
  95. link4: '',
  96. link5: '',
  97. link6: '',
  98. link7: '',
  99. link8: '',
  100. link9: '',
  101. });
  102. }