images.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. if (action == "remove") {
  6. let images = req.body.select;
  7. if (typeof(images) == 'string') {
  8. images = [images];
  9. }
  10. for (let i = 0; i < images.length; i++) {
  11. if (images[i] != 'on') {
  12. try {
  13. console.log(`Removing image: ${images[i]}`);
  14. let image = docker.getImage(images[i]);
  15. await image.remove();
  16. } catch (error) {
  17. console.log(`Unable to remove image: ${images[i]}`);
  18. }
  19. }
  20. }
  21. res.redirect("/images");
  22. return;
  23. } else if (action == "add") {
  24. let image = req.body.image;
  25. let tag = req.body.tag || 'latest';
  26. try {
  27. console.log(`Pulling image: ${image}:${tag}`);
  28. await docker.pull(`${image}:${tag}`);
  29. } catch (error) {
  30. console.log(`Unable to pull image: ${image}:${tag}`);
  31. }
  32. res.redirect("/images");
  33. return;
  34. }
  35. let containers = await docker.listContainers({ all: true });
  36. let container_images = [];
  37. for (let i = 0; i < containers.length; i++) {
  38. container_images.push(containers[i].Image);
  39. }
  40. let images = await docker.listImages({ all: true });
  41. let image_list = `
  42. <thead>
  43. <tr>
  44. <th class="w-1"><input class="form-check-input m-0 align-middle" name="select" type="checkbox" aria-label="Select all" onclick="selectAll()"></th>
  45. <th><label class="table-sort" data-sort="sort-name">Name</label></th>
  46. <th><label class="table-sort" data-sort="sort-type">Tag</label></th>
  47. <th><label class="table-sort" data-sort="sort-city">ID</label></th>
  48. <th><label class="table-sort" data-sort="sort-score">Status</label></th>
  49. <th><label class="table-sort" data-sort="sort-date">Created</label></th>
  50. <th><label class="table-sort" data-sort="sort-quantity">Size</label></th>
  51. <th><label class="table-sort" data-sort="sort-progress">Action</label></th>
  52. </tr>
  53. </thead>
  54. <tbody class="table-tbody">`
  55. for (let i = 0; i < images.length; i++) {
  56. let name = '';
  57. let tag = '';
  58. try { name = images[i].RepoTags[0].split(':')[0]; } catch {}
  59. try { tag = images[i].RepoTags[0].split(':')[1]; } catch {}
  60. let date = new Date(images[i].Created * 1000);
  61. let created = date.toLocaleDateString('en-US', { month: 'long', day: 'numeric', year: 'numeric' });
  62. let size = images[i].Size / 1000 / 1000; // to match docker desktop
  63. size = size.toFixed(2);
  64. let status = '';
  65. if (container_images.includes(images[i].RepoTags[0])) {
  66. status = 'In use';
  67. }
  68. let details = `
  69. <tr>
  70. <td><input class="form-check-input m-0 align-middle" name="select" value="${images[i].Id}" type="checkbox" aria-label="Select"></td>
  71. <td class="sort-name">${name}</td>
  72. <td class="sort-type">${tag}</td>
  73. <td class="sort-city">${images[i].Id}</td>
  74. <td class="sort-score text-green">${status}</td>
  75. <td class="sort-date" data-date="1628122643">${created}</td>
  76. <td class="sort-quantity">${size} MB</td>
  77. <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>
  78. </tr>`
  79. image_list += details;
  80. }
  81. image_list += `</tbody>`
  82. res.render("images", {
  83. name: req.session.user,
  84. role: req.session.role,
  85. avatar: req.session.user.charAt(0).toUpperCase(),
  86. image_list: image_list,
  87. image_count: images.length,
  88. alert: '',
  89. });
  90. }