check-texlive-images.js 2.9 KB

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