Просмотр исходного кода

Merge pull request #20685 from overleaf/jpa-fix-local-e2e

[server-ce] use database init script for setting up mongo replica set

GitOrigin-RevId: cf5c820a8c26da43de2f5c66677dba2c40a3667e
Jakob Ackermann 1 год назад
Родитель
Сommit
2b72d89171

+ 9 - 10
docker-compose.yml

@@ -13,9 +13,6 @@ services:
                 condition: service_started
         ports:
             - 80:80
-        links:
-            - mongo
-            - redis
         stop_grace_period: 60s
         volumes:
             - ~/sharelatex_data:/var/lib/overleaf
@@ -107,10 +104,16 @@ services:
         restart: always
         image: mongo:5.0
         container_name: mongo
-        expose:
-            - 27017
+        command: '--replSet overleaf'
         volumes:
             - ~/mongo_data:/data/db
+            - ./mongodb-init-replica-set.js:/docker-entrypoint-initdb.d/mongodb-init-replica-set.js
+        environment:
+          MONGO_INITDB_DATABASE: sharelatex
+        extra_hosts:
+          # Required when using the automatic database setup for initializing the replica set.
+          # This override is not needed when running the setup after starting up mongo.
+          - mongo:127.0.0.1
         healthcheck:
             test: echo 'db.stats().ok' | mongo localhost:27017/test --quiet
             interval: 10s
@@ -121,8 +124,6 @@ services:
         restart: always
         image: redis:6.2
         container_name: redis
-        expose:
-            - 6379
         volumes:
             - ~/redis_data:/data
 
@@ -130,8 +131,6 @@ services:
     #    restart: always
     #    image: rroemhild/test-openldap
     #    container_name: ldap
-    #    expose:
-    #        - 389
 
     # See https://github.com/jwilder/nginx-proxy for documentation on how to configure the nginx-proxy container,
     # and https://github.com/overleaf/overleaf/wiki/HTTPS-reverse-proxy-using-Nginx for an example of some recommended
@@ -142,7 +141,7 @@ services:
     #     image: jwilder/nginx-proxy
     #     container_name: nginx-proxy
     #     ports:
-    #       #- "80:80"
+    #       - "80:80"
     #       - "443:443"
     #     volumes:
     #       - /var/run/docker.sock:/tmp/docker.sock:ro

+ 1 - 0
server-ce/mongodb-init-replica-set.js

@@ -0,0 +1 @@
+rs.initiate({ _id: "overleaf", members: [ { _id: 0, host: "mongo:27017" } ] })

+ 8 - 3
server-ce/test/docker-compose.yml

@@ -8,9 +8,6 @@ services:
         condition: service_healthy
       redis:
         condition: service_started
-      host-admin:
-        # The host-admin service initiates the mongo replica set
-        condition: service_healthy
       mailtrap:
         condition: service_started
     environment:
@@ -40,6 +37,14 @@ services:
   mongo:
     image: mongo:5.0.17
     command: '--replSet overleaf'
+    volumes:
+      - ../mongodb-init-replica-set.js:/docker-entrypoint-initdb.d/mongodb-init-replica-set.js
+    environment:
+      MONGO_INITDB_DATABASE: sharelatex
+    extra_hosts:
+      # Required when using the automatic database setup for initializing the replica set.
+      # This override is not needed when running the setup after starting up mongo.
+      - mongo:127.0.0.1
     healthcheck:
       test: echo 'db.stats().ok' | mongo localhost:27017/test --quiet
       interval: 3s

+ 11 - 53
server-ce/test/host-admin.js

@@ -30,7 +30,6 @@ const IMAGES = {
 }
 
 let previousConfig = ''
-let mongoIsInitialized = false
 
 function readDockerComposeOverride() {
   try {
@@ -245,42 +244,12 @@ app.post(
   (req, res) => {
     const { cmd } = req.params
     const { args } = req.body
-    if (['stop', 'down'].includes(cmd)) {
-      mongoIsInitialized = false
-    }
     runDockerCompose(cmd, args, (error, stdout, stderr) => {
       res.json({ error, stdout, stderr })
     })
   }
 )
 
-function maybeMongoInit(mongoInit, callback) {
-  if (!mongoInit) return callback()
-  runDockerCompose(
-    'up',
-    ['--detach', '--wait', 'mongo'],
-    (error, stdout, stderr) => {
-      if (error) return callback(error, stdout, stderr)
-
-      runDockerCompose(
-        'exec',
-        [
-          'mongo',
-          'mongo',
-          '--eval',
-          'rs.initiate({ _id: "overleaf", members: [ { _id: 0, host: "mongo:27017" } ] })',
-        ],
-        (error, stdout, stderr) => {
-          if (!error) {
-            mongoIsInitialized = true
-          }
-          callback(error, stdout, stderr)
-        }
-      )
-    }
-  )
-}
-
 function maybeResetData(resetData, callback) {
   if (!resetData) return callback()
 
@@ -297,7 +266,6 @@ function maybeResetData(resetData, callback) {
       }
 
       previousConfig = ''
-      mongoIsInitialized = false
       runDockerCompose(
         'down',
         ['--timeout=0', '--volumes', 'mongo', 'redis'],
@@ -332,18 +300,16 @@ app.post(
         return res.json({ error })
       }
 
-      maybeMongoInit(!mongoIsInitialized, (error, stdout, stderr) => {
-        if (error) return res.json({ error, stdout, stderr })
-        runDockerCompose(
-          'up',
-          ['--detach', '--wait', 'sharelatex'],
-          (error, stdout, stderr) => {
-            const previousConfigServer = previousConfig
-            previousConfig = JSON.stringify(req.body)
-            res.json({ error, stdout, stderr, previousConfigServer })
-          }
-        )
-      })
+      if (error) return res.json({ error, stdout, stderr })
+      runDockerCompose(
+        'up',
+        ['--detach', '--wait', 'sharelatex'],
+        (error, stdout, stderr) => {
+          const previousConfigServer = previousConfig
+          previousConfig = JSON.stringify(req.body)
+          res.json({ error, stdout, stderr, previousConfigServer })
+        }
+      )
     })
   }
 )
@@ -362,12 +328,4 @@ app.use(handleValidationErrors())
 
 purgeDataDir()
 
-// Init on startup
-maybeMongoInit(true, err => {
-  if (err) {
-    console.error('mongo init failed', err)
-    process.exit(1)
-  }
-
-  app.listen(80)
-})
+app.listen(80)