check-texlive-images.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. const { waitForDb, db } = require('../../../app/src/infrastructure/mongodb')
  2. async function readImagesInUse() {
  3. await waitForDb()
  4. const projectCount = await db.projects.countDocuments()
  5. if (projectCount === 0) {
  6. return []
  7. }
  8. const images = await db.projects.distinct('imageName')
  9. if (!images || images.length === 0 || images.includes(null)) {
  10. console.error(`'project.imageName' is not set for some projects`)
  11. console.error(
  12. `Set SKIP_TEX_LIVE_CHECK=true in config/variables.env, restart the instance and run 'bin/run-script scripts/backfill_project_image_name.js' to initialise TexLive image in existing projects.`
  13. )
  14. console.error(
  15. `After running the script, remove SKIP_TEX_LIVE_CHECK from config/variables.env and restart the instance.`
  16. )
  17. process.exit(1)
  18. }
  19. return images
  20. }
  21. function checkSandboxedCompilesAreEnabled() {
  22. if (process.env.SANDBOXED_COMPILES !== 'true') {
  23. console.log('Sandboxed compiles disabled, skipping TexLive checks')
  24. process.exit(0)
  25. }
  26. }
  27. function checkTexLiveEnvVariablesAreProvided() {
  28. if (
  29. !process.env.TEX_LIVE_DOCKER_IMAGE ||
  30. !process.env.ALL_TEX_LIVE_DOCKER_IMAGES
  31. ) {
  32. console.error(
  33. 'Sandboxed compiles require TEX_LIVE_DOCKER_IMAGE and ALL_TEX_LIVE_DOCKER_IMAGES being set.'
  34. )
  35. process.exit(1)
  36. }
  37. }
  38. async function main() {
  39. if (process.env.SKIP_TEX_LIVE_CHECK === 'true') {
  40. console.log(`SKIP_TEX_LIVE_CHECK=true, skipping TexLive images check`)
  41. process.exit(0)
  42. }
  43. checkSandboxedCompilesAreEnabled()
  44. checkTexLiveEnvVariablesAreProvided()
  45. const allTexLiveImages = process.env.ALL_TEX_LIVE_DOCKER_IMAGES.split(',')
  46. if (!allTexLiveImages.includes(process.env.TEX_LIVE_DOCKER_IMAGE)) {
  47. console.error(
  48. `TEX_LIVE_DOCKER_IMAGE must be included in ALL_TEX_LIVE_DOCKER_IMAGES`
  49. )
  50. process.exit(1)
  51. }
  52. const currentImages = await readImagesInUse()
  53. const danglingImages = []
  54. for (const image of currentImages) {
  55. if (!allTexLiveImages.includes(image)) {
  56. danglingImages.push(image)
  57. }
  58. }
  59. if (danglingImages.length > 0) {
  60. danglingImages.forEach(image =>
  61. console.error(
  62. `${image} is currently in use but it's not included in ALL_TEX_LIVE_DOCKER_IMAGES`
  63. )
  64. )
  65. console.error(
  66. `Set SKIP_TEX_LIVE_CHECK=true in config/variables.env, restart the instance and run 'bin/run-script scripts/update_project_image_name.js <dangling_image> <new_image>' to update projects to a new image.`
  67. )
  68. console.error(
  69. `After running the script, remove SKIP_TEX_LIVE_CHECK from config/variables.env and restart the instance.`
  70. )
  71. process.exit(1)
  72. }
  73. console.log('Done.')
  74. }
  75. main()
  76. .then(() => {
  77. process.exit(0)
  78. })
  79. .catch(err => {
  80. console.error(err)
  81. process.exit(1)
  82. })