check-mongodb.mjs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import mongodb from 'mongodb-legacy'
  2. import {
  3. connectionPromise,
  4. db,
  5. } from '../../../app/src/infrastructure/mongodb.mjs'
  6. const { ObjectId } = mongodb
  7. const MIN_MONGO_VERSION = [8, 0]
  8. const MIN_MONGO_FEATURE_COMPATIBILITY_VERSION = [7, 0]
  9. // Allow ignoring admin check failures via an environment variable
  10. const OVERRIDE_ENV_VAR_NAME = 'ALLOW_MONGO_ADMIN_CHECK_FAILURES'
  11. function shouldSkipAdminChecks() {
  12. return process.env[OVERRIDE_ENV_VAR_NAME] === 'true'
  13. }
  14. function handleUnauthorizedError(err, feature) {
  15. if (
  16. err instanceof mongodb.MongoServerError &&
  17. err.codeName === 'Unauthorized'
  18. ) {
  19. console.warn(`Warning: failed to check ${feature} (not authorised)`)
  20. if (!shouldSkipAdminChecks()) {
  21. console.error(
  22. `Please ensure the MongoDB user has the required permissions, for more information see
  23. https://docs.overleaf.com/on-premises/maintenance/updating-mongodb#creating-a-custom-role
  24. or set the environment variable ${OVERRIDE_ENV_VAR_NAME}=true to ignore this check.`
  25. )
  26. process.exit(1)
  27. }
  28. console.warn(
  29. `Ignoring ${feature} check failure (${OVERRIDE_ENV_VAR_NAME}=${process.env[OVERRIDE_ENV_VAR_NAME]})`
  30. )
  31. } else {
  32. throw err
  33. }
  34. }
  35. async function main() {
  36. let mongoClient
  37. try {
  38. mongoClient = await connectionPromise
  39. } catch (err) {
  40. console.error('Cannot connect to mongodb')
  41. throw err
  42. }
  43. try {
  44. await checkMongoVersion(mongoClient)
  45. } catch (err) {
  46. handleUnauthorizedError(err, 'MongoDB version')
  47. }
  48. try {
  49. await checkFeatureCompatibilityVersion(mongoClient)
  50. } catch (err) {
  51. handleUnauthorizedError(err, 'MongoDB feature compatibility version')
  52. }
  53. try {
  54. await testTransactions(mongoClient)
  55. } catch (err) {
  56. console.error("Mongo instance doesn't support transactions")
  57. throw err
  58. }
  59. }
  60. async function testTransactions(mongoClient) {
  61. const session = mongoClient.startSession()
  62. try {
  63. await session.withTransaction(async () => {
  64. await db.users.findOne({ _id: new ObjectId() }, { session })
  65. })
  66. } finally {
  67. await session.endSession()
  68. }
  69. }
  70. async function checkMongoVersion(mongoClient) {
  71. const buildInfo = await mongoClient.db().admin().buildInfo()
  72. const [major, minor] = buildInfo.versionArray
  73. const [minMajor, minMinor] = MIN_MONGO_VERSION
  74. if (major < minMajor || (major === minMajor && minor < minMinor)) {
  75. const version = buildInfo.version
  76. const minVersion = MIN_MONGO_VERSION.join('.')
  77. console.error(
  78. `The MongoDB server has version ${version}, but Overleaf requires at least version ${minVersion}. Aborting.`
  79. )
  80. process.exit(1)
  81. }
  82. }
  83. async function checkFeatureCompatibilityVersion(mongoClient) {
  84. const {
  85. featureCompatibilityVersion: { version },
  86. } = await mongoClient
  87. .db()
  88. .admin()
  89. .command({ getParameter: 1, featureCompatibilityVersion: 1 })
  90. const [major, minor] = version.split('.').map(v => parseInt(v))
  91. const [minMajor, minMinor] = MIN_MONGO_FEATURE_COMPATIBILITY_VERSION
  92. if (major < minMajor || (major === minMajor && minor < minMinor)) {
  93. const minVersion = MIN_MONGO_FEATURE_COMPATIBILITY_VERSION.join('.')
  94. console.error(`
  95. The MongoDB server has featureCompatibilityVersion=${version}, but Overleaf requires at least version ${minVersion}.
  96. Open a mongo shell:
  97. - Overleaf Toolkit deployments: $ bin/mongo
  98. - Legacy docker-compose.yml deployments: $ docker exec -it mongo mongosh localhost/sharelatex
  99. In the mongo shell:
  100. > db.adminCommand( { setFeatureCompatibilityVersion: "${minMajor}.${minMinor}" } )
  101. Verify the new value:
  102. > db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } )
  103. ...
  104. {
  105. featureCompatibilityVersion: { version: ${minMajor}.${minMinor}' },
  106. ...
  107. Aborting.
  108. `)
  109. process.exit(1)
  110. }
  111. }
  112. main()
  113. .then(() => {
  114. console.error('Mongodb is up.')
  115. process.exit(0)
  116. })
  117. .catch(err => {
  118. console.error(err)
  119. process.exit(1)
  120. })