check-texlive-images.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 checkIsServerPro() {
  22. if (process.env.OVERLEAF_IS_SERVER_PRO !== 'true') {
  23. console.log('Running Overleaf Community Edition, skipping TexLive checks')
  24. process.exit(0)
  25. }
  26. }
  27. function checkSandboxedCompilesAreEnabled() {
  28. if (process.env.SANDBOXED_COMPILES !== 'true') {
  29. console.log('Sandboxed compiles disabled, skipping TexLive checks')
  30. process.exit(0)
  31. }
  32. }
  33. function checkTexLiveEnvVariablesAreProvided() {
  34. if (
  35. !process.env.TEX_LIVE_DOCKER_IMAGE ||
  36. !process.env.ALL_TEX_LIVE_DOCKER_IMAGES
  37. ) {
  38. console.error(
  39. 'Sandboxed compiles require TEX_LIVE_DOCKER_IMAGE and ALL_TEX_LIVE_DOCKER_IMAGES being set.'
  40. )
  41. process.exit(1)
  42. }
  43. }
  44. async function main() {
  45. if (process.env.SKIP_TEX_LIVE_CHECK === 'true') {
  46. console.log(`SKIP_TEX_LIVE_CHECK=true, skipping TexLive images check`)
  47. process.exit(0)
  48. }
  49. checkIsServerPro()
  50. checkSandboxedCompilesAreEnabled()
  51. checkTexLiveEnvVariablesAreProvided()
  52. const allTexLiveImages = process.env.ALL_TEX_LIVE_DOCKER_IMAGES.split(',')
  53. if (!allTexLiveImages.includes(process.env.TEX_LIVE_DOCKER_IMAGE)) {
  54. console.error(
  55. `TEX_LIVE_DOCKER_IMAGE must be included in ALL_TEX_LIVE_DOCKER_IMAGES`
  56. )
  57. process.exit(1)
  58. }
  59. const currentImages = await readImagesInUse()
  60. const danglingImages = []
  61. for (const image of currentImages) {
  62. if (!allTexLiveImages.includes(image)) {
  63. danglingImages.push(image)
  64. }
  65. }
  66. if (danglingImages.length > 0) {
  67. danglingImages.forEach(image =>
  68. console.error(
  69. `${image} is currently in use but it's not included in ALL_TEX_LIVE_DOCKER_IMAGES`
  70. )
  71. )
  72. console.error(
  73. `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.`
  74. )
  75. console.error(
  76. `After running the script, remove SKIP_TEX_LIVE_CHECK from config/variables.env and restart the instance.`
  77. )
  78. process.exit(1)
  79. }
  80. console.log('Done.')
  81. }
  82. main()
  83. .then(() => {
  84. process.exit(0)
  85. })
  86. .catch(err => {
  87. console.error(err)
  88. process.exit(1)
  89. })