Browse Source

refactor: Separate docker container modes

Owen Diffey 2 years ago
parent
commit
c881b05a29
11 changed files with 179 additions and 183 deletions
  1. 1 0
      .env.example
  2. 1 0
      .gitignore
  3. 2 1
      .wiki/Configuration.md
  4. 2 0
      backend/.dockerignore
  5. 3 4
      backend/Dockerfile
  6. 3 3
      backend/index.js
  7. 20 0
      docker-compose.dev.yml
  8. 4 14
      docker-compose.yml
  9. 2 0
      frontend/.dockerignore
  10. 5 8
      frontend/Dockerfile
  11. 136 153
      musare.sh

+ 1 - 0
.env.example

@@ -1,5 +1,6 @@
 COMPOSE_PROJECT_NAME=musare
 RESTART_POLICY=unless-stopped
+CONTAINER_MODE=prod
 
 BACKEND_HOST=127.0.0.1
 BACKEND_PORT=8080

+ 1 - 0
.gitignore

@@ -13,6 +13,7 @@ startMongo.cmd
 .redis
 *.rdb
 backups/
+docker-compose.override.yml
 
 npm-debug.log
 lerna-debug.log

+ 2 - 1
.wiki/Configuration.md

@@ -100,11 +100,12 @@ The container port refers to the external docker container port, used to access
 | --- | --- |
 | `COMPOSE_PROJECT_NAME` | Should be a unique name for this installation, especially if you have multiple instances of Musare on the same machine. |
 | `RESTART_POLICY` | Restart policy for docker containers, values can be found [here](https://docs.docker.com/config/containers/start-containers-automatically/). |
+| `CONTAINER_MODE` | Should be either `prod` or `dev`.  |
 | `BACKEND_HOST` | Backend container host. |
 | `BACKEND_PORT` | Backend container port. |
 | `FRONTEND_HOST` | Frontend container host. |
 | `FRONTEND_PORT` | Frontend container port. |
-| `FRONTEND_MODE` | Should be either `dev` or `prod`. |
+| `FRONTEND_MODE` | Should be either `prod` or `dev`. |
 | `MONGO_HOST` | Mongo container host. |
 | `MONGO_PORT` | Mongo container port. |
 | `MONGO_ROOT_PASSWORD` | Password of the root/admin user for MongoDB. |

+ 2 - 0
backend/.dockerignore

@@ -0,0 +1,2 @@
+node_modules/
+Dockerfile

+ 3 - 4
backend/Dockerfile

@@ -2,10 +2,9 @@ FROM node:16
 
 RUN npm install -g nodemon
 
-RUN mkdir -p /opt
-WORKDIR /opt
-ADD package.json /opt/package.json
-ADD package-lock.json /opt/package-lock.json
+RUN mkdir -p /opt/app
+WORKDIR /opt/app
+ADD ./ /opt/app
 
 RUN npm install
 

+ 3 - 3
backend/index.js

@@ -38,12 +38,12 @@ const printVersion = () => {
 	console.log(`Musare version: ${MUSARE_VERSION}.`);
 
 	try {
-		const head_contents = fs.readFileSync("app/.parent_git/HEAD").toString().replaceAll("\n", "");
+		const head_contents = fs.readFileSync(".parent_git/HEAD").toString().replaceAll("\n", "");
 		const branch = new RegExp("ref: refs/heads/([\.A-Za-z0-9_-]+)").exec(head_contents)[1];
-		const config_contents = fs.readFileSync("app/.parent_git/config").toString().replaceAll("\t", "").split("\n");
+		const config_contents = fs.readFileSync(".parent_git/config").toString().replaceAll("\t", "").split("\n");
 		const remote = new RegExp("remote = (.+)").exec(config_contents[config_contents.indexOf(`[branch "${branch}"]`) + 1])[1];
 		const remote_url = new RegExp("url = (.+)").exec(config_contents[config_contents.indexOf(`[remote "${remote}"]`) + 1])[1];
-		const latest_commit = fs.readFileSync(`app/.parent_git/refs/heads/${branch}`).toString().replaceAll("\n", "");
+		const latest_commit = fs.readFileSync(`.parent_git/refs/heads/${branch}`).toString().replaceAll("\n", "");
 		const latest_commit_short = latest_commit.substr(0, 7);
 
 		console.log(`Git branch: ${remote}/${branch}. Remote url: ${remote_url}. Latest commit: ${latest_commit} (${latest_commit_short}).`);

+ 20 - 0
docker-compose.dev.yml

@@ -0,0 +1,20 @@
+services:
+  backend:
+    ports:
+      - "${BACKEND_HOST}:${BACKEND_PORT}:8080"
+    volumes:
+      - ./backend:/opt/app
+
+  frontend:
+    volumes:
+      - ./frontend:/opt/app
+
+  mongo:
+    ports:
+      - "${MONGO_HOST}:${MONGO_PORT}:${MONGO_PORT}"
+
+  redis:
+    ports:
+      - "${REDIS_HOST}:${REDIS_PORT}:6379"
+    volumes:
+      - ${REDIS_DATA_LOCATION}:/data

+ 4 - 14
docker-compose.yml

@@ -1,15 +1,12 @@
-version: '3'
-services:
+version: "3.8"
 
+services:
   backend:
     build: ./backend
     restart: ${RESTART_POLICY}
-    ports:
-      - "${BACKEND_HOST}:${BACKEND_PORT}:8080"
     volumes:
-      - ./backend:/opt/app
-      - ./log:/opt/log
       - ./.git:/opt/app/.parent_git:ro
+      - /opt/app/node_modules
     links:
       - mongo
       - redis
@@ -22,9 +19,8 @@ services:
     ports:
       - "${FRONTEND_HOST}:${FRONTEND_PORT}:80"
     volumes:
-      - ./frontend:/opt/app
-      - /opt/app/node_modules/
       - ./.git:/opt/app/.parent_git:ro
+      - /opt/app/node_modules
     environment:
       - FRONTEND_MODE=${FRONTEND_MODE}
     links:
@@ -33,8 +29,6 @@ services:
   mongo:
     image: mongo:${MONGO_VERSION}
     restart: ${RESTART_POLICY}
-    ports:
-      - "${MONGO_HOST}:${MONGO_PORT}:${MONGO_PORT}"
     environment:
       - MONGO_INITDB_ROOT_USERNAME=admin
       - MONGO_INITDB_ROOT_PASSWORD=${MONGO_ROOT_PASSWORD}
@@ -50,8 +44,4 @@ services:
   redis:
     image: redis:6.2
     restart: ${RESTART_POLICY}
-    ports:
-      - "${REDIS_HOST}:${REDIS_PORT}:6379"
     command: "--notify-keyspace-events Ex --requirepass ${REDIS_PASSWORD} --appendonly yes"
-    volumes:
-      - ${REDIS_DATA_LOCATION}:/data

+ 2 - 0
frontend/.dockerignore

@@ -0,0 +1,2 @@
+node_modules/
+Dockerfile

+ 5 - 8
frontend/Dockerfile

@@ -3,18 +3,15 @@ FROM node:16
 RUN apt-get update
 RUN apt-get install nginx -y
 
-RUN mkdir -p /opt
-WORKDIR /opt
-ADD package.json /opt/package.json
-ADD package-lock.json /opt/package-lock.json
+RUN mkdir -p /opt/app
+WORKDIR /opt/app
+ADD ./ /opt/app
 
 RUN npm install -g webpack@5.68.0 webpack-cli@4.9.2
-
 RUN npm install
 
 RUN mkdir -p /run/nginx
 
-COPY bootstrap.sh /opt/
-RUN chmod u+x /opt/bootstrap.sh
+RUN chmod u+x bootstrap.sh
 
-CMD bash /opt/bootstrap.sh
+CMD bash /opt/app/bootstrap.sh

+ 136 - 153
musare.sh

@@ -11,6 +11,14 @@ NC='\033[0m'
 scriptLocation=$(dirname -- "$(readlink -fn -- "$0"; echo x)")
 cd "${scriptLocation%x}" || exit
 
+if [[ -f .env ]]; then
+    # shellcheck disable=SC1091
+    source .env
+else
+    echo -e "${RED}Error: .env does not exist${NC}"
+    exit 2
+fi
+
 handleServices()
 {
     validServices=(backend frontend mongo redis)
@@ -49,17 +57,22 @@ dockerCommand()
             else
                 servicesString=${servicesString:2}
             fi
+            if [[ ${CONTAINER_MODE} == "dev" ]]; then
+                composeFiles="-f docker-compose.yml -f docker-compose.dev.yml"
+            else
+                composeFiles="-f docker-compose.yml"
+            fi
             if [[ ${2} == "stop" || ${2} == "restart" ]]; then
                 # shellcheck disable=SC2086
-                docker-compose stop ${servicesString}
+                docker-compose ${composeFiles} stop ${servicesString}
             fi
             if [[ ${2} == "start" || ${2} == "restart" ]]; then
                 # shellcheck disable=SC2086
-                docker-compose up -d ${servicesString}
+                docker-compose ${composeFiles} up -d ${servicesString}
             fi
             if [[ ${2} == "pull" || ${2} == "build" || ${2} == "ps" ]]; then
                 # shellcheck disable=SC2086
-                docker-compose "${2}" ${servicesString}
+                docker-compose ${composeFiles} "${2}" ${servicesString}
             fi
         else
             echo -e "${RED}${servicesString:2}\n${YELLOW}Usage: ${1} restart [backend, frontend, mongo, redis]${NC}"
@@ -105,95 +118,83 @@ if [[ -x "$(command -v docker)" && -x "$(command -v docker-compose)" ]]; then
 
     reset)
         echo -e "${CYAN}Musare | Reset Services${NC}"
-        if [[ -f .env ]]; then
-            # shellcheck disable=SC1091
-            source .env
-            servicesString=$(handleServices "${@:2}")
-            if [[ ${servicesString:0:1} == 1 && ${servicesString:2:4} == "all" ]]; then
-                echo -e "${RED}Resetting will remove the ${REDIS_DATA_LOCATION} and ${MONGO_DATA_LOCATION} directories.${NC}"
-                echo -e "${GREEN}Are you sure you want to reset all data? ${YELLOW}[y,n]: ${NC}"
-                read -r confirm
-                if [[ "${confirm}" == y* ]]; then
-                    docker-compose stop
-                    docker-compose rm -v --force
-                    if [[ -d $REDIS_DATA_LOCATION ]]; then
-                        rm -rf $REDIS_DATA_LOCATION
-                    fi
-                    if [[ -d $MONGO_DATA_LOCATION ]]; then
-                        rm -rf $MONGO_DATA_LOCATION
-                    fi
-                else
-                    echo -e "${RED}Cancelled reset${NC}"
+        servicesString=$(handleServices "${@:2}")
+        if [[ ${servicesString:0:1} == 1 && ${servicesString:2:4} == "all" ]]; then
+            echo -e "${RED}Resetting will remove the ${REDIS_DATA_LOCATION} and ${MONGO_DATA_LOCATION} directories.${NC}"
+            echo -e "${GREEN}Are you sure you want to reset all data? ${YELLOW}[y,n]: ${NC}"
+            read -r confirm
+            if [[ "${confirm}" == y* ]]; then
+                docker-compose stop
+                docker-compose rm -v --force
+                if [[ -d $REDIS_DATA_LOCATION ]]; then
+                    rm -rf "${REDIS_DATA_LOCATION}"
                 fi
-            elif [[ ${servicesString:0:1} == 1 ]]; then
-                if [[ "${servicesString:2}" == *redis* && "${servicesString:2}" == *mongo* ]]; then
-                    echo -e "${RED}Resetting will remove the ${REDIS_DATA_LOCATION} and ${MONGO_DATA_LOCATION} directories.${NC}"
-                elif [[ "${servicesString:2}" == *redis* ]]; then
-                    echo -e "${RED}Resetting will remove the ${REDIS_DATA_LOCATION} directory.${NC}"
-                elif [[ "${servicesString:2}" == *mongo* ]]; then
-                    echo -e "${RED}Resetting will remove the ${MONGO_DATA_LOCATION} directory.${NC}"
+                if [[ -d $MONGO_DATA_LOCATION ]]; then
+                    rm -rf "${MONGO_DATA_LOCATION}"
                 fi
-                echo -e "${GREEN}Are you sure you want to reset all data for $(echo "${servicesString:2}" | tr ' ' ',')? ${YELLOW}[y,n]: ${NC}"
-                read -r confirm
-                if [[ "${confirm}" == y* ]]; then
-                    # shellcheck disable=SC2086
-                    docker-compose stop ${servicesString:2}
-                    # shellcheck disable=SC2086
-                    docker-compose rm -v --force ${servicesString:2}
-                    if [[ "${servicesString:2}" == *redis* && -d $REDIS_DATA_LOCATION ]]; then
-                        rm -rf $REDIS_DATA_LOCATION
-                    fi
-                    if [[ "${servicesString:2}" == *mongo* && -d $MONGO_DATA_LOCATION ]]; then
-                        rm -rf $MONGO_DATA_LOCATION
-                    fi
-                else
-                    echo -e "${RED}Cancelled reset${NC}"
+            else
+                echo -e "${RED}Cancelled reset${NC}"
+            fi
+        elif [[ ${servicesString:0:1} == 1 ]]; then
+            if [[ "${servicesString:2}" == *redis* && "${servicesString:2}" == *mongo* ]]; then
+                echo -e "${RED}Resetting will remove the ${REDIS_DATA_LOCATION} and ${MONGO_DATA_LOCATION} directories.${NC}"
+            elif [[ "${servicesString:2}" == *redis* ]]; then
+                echo -e "${RED}Resetting will remove the ${REDIS_DATA_LOCATION} directory.${NC}"
+            elif [[ "${servicesString:2}" == *mongo* ]]; then
+                echo -e "${RED}Resetting will remove the ${MONGO_DATA_LOCATION} directory.${NC}"
+            fi
+            echo -e "${GREEN}Are you sure you want to reset all data for $(echo "${servicesString:2}" | tr ' ' ',')? ${YELLOW}[y,n]: ${NC}"
+            read -r confirm
+            if [[ "${confirm}" == y* ]]; then
+                # shellcheck disable=SC2086
+                docker-compose stop ${servicesString:2}
+                # shellcheck disable=SC2086
+                docker-compose rm -v --force ${servicesString:2}
+                if [[ "${servicesString:2}" == *redis* && -d $REDIS_DATA_LOCATION ]]; then
+                    rm -rf "${REDIS_DATA_LOCATION}"
+                fi
+                if [[ "${servicesString:2}" == *mongo* && -d $MONGO_DATA_LOCATION ]]; then
+                    rm -rf "${MONGO_DATA_LOCATION}"
                 fi
             else
-                echo -e "${RED}${servicesString:2}\n${YELLOW}Usage: $(basename "$0") build [backend, frontend, mongo, redis]${NC}"
+                echo -e "${RED}Cancelled reset${NC}"
             fi
         else
-            echo -e "${RED}Error: .env does not exist${NC}"
+            echo -e "${RED}${servicesString:2}\n${YELLOW}Usage: $(basename "$0") build [backend, frontend, mongo, redis]${NC}"
         fi
         ;;
 
     attach)
         echo -e "${CYAN}Musare | Attach${NC}"
-        if [[ -f .env ]]; then
-            # shellcheck disable=SC1091
-            source .env
-            if [[ $2 == "backend" ]]; then
-                containerId=$(docker-compose ps -q backend)
-                if [[ -z $containerId ]]; then
-                    echo -e "${RED}Error: Backend offline, please start to attach.${NC}"
-                else
-                    echo -e "${YELLOW}Detach with CTRL+P+Q${NC}"
-                    docker attach "$containerId"
-                fi
-            elif [[ $2 == "mongo" ]]; then
-                MONGO_VERSION_INT=${MONGO_VERSION:0:1}
-                if [[ -z $(docker-compose ps -q mongo) ]]; then
-                    echo -e "${RED}Error: Mongo offline, please start to attach.${NC}"
-                else
-                    echo -e "${YELLOW}Detach with CTRL+D${NC}"
-                    if [[ $MONGO_VERSION_INT -ge 5 ]]; then
-                        docker-compose exec mongo mongosh musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}" --eval "disableTelemetry()" --shell
-                    else
-                        docker-compose exec mongo mongo musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}"
-                    fi
-                fi
-            elif [[ $2 == "redis" ]]; then
-                if [[ -z $(docker-compose ps -q redis) ]]; then
-                    echo -e "${RED}Error: Redis offline, please start to attach.${NC}"
+        if [[ $2 == "backend" ]]; then
+            containerId=$(docker-compose ps -q backend)
+            if [[ -z $containerId ]]; then
+                echo -e "${RED}Error: Backend offline, please start to attach.${NC}"
+            else
+                echo -e "${YELLOW}Detach with CTRL+P+Q${NC}"
+                docker attach "$containerId"
+            fi
+        elif [[ $2 == "mongo" ]]; then
+            MONGO_VERSION_INT=${MONGO_VERSION:0:1}
+            if [[ -z $(docker-compose ps -q mongo) ]]; then
+                echo -e "${RED}Error: Mongo offline, please start to attach.${NC}"
+            else
+                echo -e "${YELLOW}Detach with CTRL+D${NC}"
+                if [[ $MONGO_VERSION_INT -ge 5 ]]; then
+                    docker-compose exec mongo mongosh musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}" --eval "disableTelemetry()" --shell
                 else
-                    echo -e "${YELLOW}Detach with CTRL+C${NC}"
-                    docker-compose exec redis redis-cli -a "${REDIS_PASSWORD}"
+                    docker-compose exec mongo mongo musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}"
                 fi
+            fi
+        elif [[ $2 == "redis" ]]; then
+            if [[ -z $(docker-compose ps -q redis) ]]; then
+                echo -e "${RED}Error: Redis offline, please start to attach.${NC}"
             else
-                echo -e "${RED}Invalid service $2\n${YELLOW}Usage: $(basename "$0") attach [backend,mongo,redis]${NC}"
+                echo -e "${YELLOW}Detach with CTRL+C${NC}"
+                docker-compose exec redis redis-cli -a "${REDIS_PASSWORD}"
             fi
         else
-            echo -e "${RED}Error: .env does not exist${NC}"
+            echo -e "${RED}Invalid service $2\n${YELLOW}Usage: $(basename "$0") attach [backend,mongo,redis]${NC}"
         fi
         ;;
 
@@ -206,14 +207,14 @@ if [[ -x "$(command -v docker)" && -x "$(command -v docker-compose)" ]]; then
         fi
         case $2 in
             frontend)
-                docker-compose exec frontend npx eslint app/src --ext .js,.vue $fix
+                docker-compose exec frontend npx eslint src --ext .js,.vue $fix
                 ;;
             backend)
                 docker-compose exec backend npx eslint app/logic $fix
                 ;;
             ""|fix|--fix)
-                docker-compose exec frontend npx eslint app/src --ext .js,.vue $fix
-                docker-compose exec backend npx eslint app/logic $fix
+                docker-compose exec frontend npx eslint src --ext .js,.vue $fix
+                docker-compose exec backend npx eslint logic $fix
                 ;;
             *)
                 echo -e "${RED}Invalid service $2\n${YELLOW}Usage: $(basename "$0") eslint [backend, frontend] [fix]${NC}"
@@ -259,98 +260,80 @@ if [[ -x "$(command -v docker)" && -x "$(command -v docker-compose)" ]]; then
 
     backup)
         echo -e "${CYAN}Musare | Backup${NC}"
-        if [[ -f .env ]]; then
-            # shellcheck disable=SC1091
-            source .env
-            if [[ -z "${BACKUP_LOCATION}" ]]; then
-                backupLocation="${scriptLocation%x}/backups"
-            else
-                backupLocation="${BACKUP_LOCATION%/}"
-            fi
-            if [[ ! -d "${backupLocation}" ]]; then
-                echo -e "${YELLOW}Creating backup directory at ${backupLocation}${NC}"
-                mkdir "${backupLocation}"
-            fi
-            if [[ -z "${BACKUP_NAME}" ]]; then
-                backupLocation="${backupLocation}/musare-$(date +"%Y-%m-%d-%s").dump"
-            else
-                backupLocation="${backupLocation}/${BACKUP_NAME}"
-            fi
-            echo -e "${YELLOW}Creating backup at ${backupLocation}${NC}"
-            docker-compose exec -T mongo sh -c "mongodump --authenticationDatabase musare -u ${MONGO_USER_USERNAME} -p ${MONGO_USER_PASSWORD} -d musare --archive" > "${backupLocation}"
+        if [[ -z "${BACKUP_LOCATION}" ]]; then
+            backupLocation="${scriptLocation%x}/backups"
+        else
+            backupLocation="${BACKUP_LOCATION%/}"
+        fi
+        if [[ ! -d "${backupLocation}" ]]; then
+            echo -e "${YELLOW}Creating backup directory at ${backupLocation}${NC}"
+            mkdir "${backupLocation}"
+        fi
+        if [[ -z "${BACKUP_NAME}" ]]; then
+            backupLocation="${backupLocation}/musare-$(date +"%Y-%m-%d-%s").dump"
         else
-            echo -e "${RED}Error: .env does not exist${NC}"
+            backupLocation="${backupLocation}/${BACKUP_NAME}"
         fi
+        echo -e "${YELLOW}Creating backup at ${backupLocation}${NC}"
+        docker-compose exec -T mongo sh -c "mongodump --authenticationDatabase musare -u ${MONGO_USER_USERNAME} -p ${MONGO_USER_PASSWORD} -d musare --archive" > "${backupLocation}"
         ;;
 
     restore)
         echo -e "${CYAN}Musare | Restore${NC}"
-        if [[ -f .env ]]; then
-            # shellcheck disable=SC1091
-            source .env
-            if [[ -z $2 ]]; then
-                echo -e "${GREEN}Please enter the full path of the dump you wish to restore: ${NC}"
-                read -r restoreFile
-            else
-                restoreFile=$2
-            fi
-            if [[ -z ${restoreFile} ]]; then
-                echo -e "${RED}Error: no restore path given, cancelled restoration.${NC}"
-            elif [[ -d ${restoreFile} ]]; then
-                echo -e "${RED}Error: restore path given is a directory, cancelled restoration.${NC}"
-            elif [[ ! -f ${restoreFile} ]]; then
-                echo -e "${RED}Error: no file at restore path given, cancelled restoration.${NC}"
-            else
-                docker-compose exec -T mongo sh -c "mongorestore --authenticationDatabase musare -u ${MONGO_USER_USERNAME} -p ${MONGO_USER_PASSWORD} --archive" < "${restoreFile}"
-            fi
+        if [[ -z $2 ]]; then
+            echo -e "${GREEN}Please enter the full path of the dump you wish to restore: ${NC}"
+            read -r restoreFile
+        else
+            restoreFile=$2
+        fi
+        if [[ -z ${restoreFile} ]]; then
+            echo -e "${RED}Error: no restore path given, cancelled restoration.${NC}"
+        elif [[ -d ${restoreFile} ]]; then
+            echo -e "${RED}Error: restore path given is a directory, cancelled restoration.${NC}"
+        elif [[ ! -f ${restoreFile} ]]; then
+            echo -e "${RED}Error: no file at restore path given, cancelled restoration.${NC}"
         else
-            echo -e "${RED}Error: .env does not exist${NC}"
+            docker-compose exec -T mongo sh -c "mongorestore --authenticationDatabase musare -u ${MONGO_USER_USERNAME} -p ${MONGO_USER_PASSWORD} --archive" < "${restoreFile}"
         fi
         ;;
 
     admin)
         echo -e "${CYAN}Musare | Add Admin${NC}"
-        if [[ -f .env ]]; then
-            # shellcheck disable=SC1091
-            source .env
-            MONGO_VERSION_INT=${MONGO_VERSION:0:1}
-            if [[ $2 == "add" ]]; then
-                if [[ -z $3 ]]; then
-                    echo -e "${GREEN}Please enter the username of the user you wish to make an admin: ${NC}"
-                    read -r adminUser
-                else
-                    adminUser=$3
-                fi
-                if [[ -z $adminUser ]]; then
-                    echo -e "${RED}Error: Username for new admin not provided.${NC}"
-                else
-                    if [[ $MONGO_VERSION_INT -ge 5 ]]; then
-                        docker-compose exec mongo mongosh musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}" --eval "disableTelemetry(); db.users.updateOne({username: '${adminUser}'}, {\$set: {role: 'admin'}})"
-                    else
-                        docker-compose exec mongo mongo musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}" --eval "db.users.updateOne({username: '${adminUser}'}, {\$set: {role: 'admin'}})"
-                    fi
-                fi
-            elif [[ $2 == "remove" ]]; then
-                if [[ -z $3 ]]; then
-                    echo -e "${GREEN}Please enter the username of the user you wish to remove as admin: ${NC}"
-                    read -r adminUser
+        MONGO_VERSION_INT=${MONGO_VERSION:0:1}
+        if [[ $2 == "add" ]]; then
+            if [[ -z $3 ]]; then
+                echo -e "${GREEN}Please enter the username of the user you wish to make an admin: ${NC}"
+                read -r adminUser
+            else
+                adminUser=$3
+            fi
+            if [[ -z $adminUser ]]; then
+                echo -e "${RED}Error: Username for new admin not provided.${NC}"
+            else
+                if [[ $MONGO_VERSION_INT -ge 5 ]]; then
+                    docker-compose exec mongo mongosh musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}" --eval "disableTelemetry(); db.users.updateOne({username: '${adminUser}'}, {\$set: {role: 'admin'}})"
                 else
-                    adminUser=$3
+                    docker-compose exec mongo mongo musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}" --eval "db.users.updateOne({username: '${adminUser}'}, {\$set: {role: 'admin'}})"
                 fi
-                if [[ -z $adminUser ]]; then
-                    echo -e "${RED}Error: Username for new admin not provided.${NC}"
+            fi
+        elif [[ $2 == "remove" ]]; then
+            if [[ -z $3 ]]; then
+                echo -e "${GREEN}Please enter the username of the user you wish to remove as admin: ${NC}"
+                read -r adminUser
+            else
+                adminUser=$3
+            fi
+            if [[ -z $adminUser ]]; then
+                echo -e "${RED}Error: Username for new admin not provided.${NC}"
+            else
+                if [[ $MONGO_VERSION_INT -ge 5 ]]; then
+                    docker-compose exec mongo mongosh musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}" --eval "disableTelemetry(); db.users.updateOne({username: '${adminUser}'}, {\$set: {role: 'default'}})"
                 else
-                    if [[ $MONGO_VERSION_INT -ge 5 ]]; then
-                        docker-compose exec mongo mongosh musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}" --eval "disableTelemetry(); db.users.updateOne({username: '${adminUser}'}, {\$set: {role: 'default'}})"
-                    else
-                        docker-compose exec mongo mongo musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}" --eval "db.users.updateOne({username: '${adminUser}'}, {\$set: {role: 'default'}})"
-                    fi
+                    docker-compose exec mongo mongo musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}" --eval "db.users.updateOne({username: '${adminUser}'}, {\$set: {role: 'default'}})"
                 fi
-            else
-                echo -e "${RED}Invalid command $2\n${YELLOW}Usage: $(basename "$0") admin [add,remove] username${NC}"
             fi
         else
-            echo -e "${RED}Error: .env does not exist${NC}"
+            echo -e "${RED}Invalid command $2\n${YELLOW}Usage: $(basename "$0") admin [add,remove] username${NC}"
         fi
         ;;