Browse Source

Merge branch 'odiffey-owen-musare.sh' into polishing

Kristian Vos 3 years ago
parent
commit
e5f252b978
3 changed files with 87 additions and 45 deletions
  1. 2 0
      .env.example
  2. 19 4
      .wiki/Configuration.md
  3. 66 41
      musare.sh

+ 2 - 0
.env.example

@@ -17,3 +17,5 @@ REDIS_HOST=127.0.0.1
 REDIS_PORT=6379
 REDIS_PASSWORD=PASSWORD
 
+BACKUP_LOCATION=
+BACKUP_NAME=

+ 19 - 4
.wiki/Configuration.md

@@ -48,10 +48,25 @@ Location: `frontend/dist/config/default.json`
 ## Docker Environment
 Location: `.env`
 
+In the table below the container host refers to the IP address that the docker container listens on, setting this to `127.0.0.1` for example will only expose the configured port to localhost, whereas setting to `0.0.0.0` will expose the port on all interfaces.
+
+The container port refers to the external docker container port, used to access services within the container. Changing this does not require any changes to configuration within container. For example setting the `MONGO_PORT` to `21018` will allow you to access the mongo service through that port, even though the application within the container is listening on `21017`.
+
 | Property | Description |
 | --- | --- |
-| Ports | Will be how you access the services on your machine, or what ports you will need to specify in your nginx files when using proxy_pass. |
 | `COMPOSE_PROJECT_NAME` | Should be a unique name for this installation, especially if you have multiple instances of Musare on the same machine. |
-| `FRONTEND_MODE` | Should be either `dev` or `prod` (self-explanatory). |
-| `MONGO_ROOT_PASSWORD` | Password of the root/admin user of MongoDB |
-| `MONGO_USER_USERNAME` | Password for the "musare" user (what the backend uses) of MongoDB |
+| `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`. |
+| `MONGO_HOST` | Mongo container host. |
+| `MONGO_PORT` | Mongo container port. |
+| `MONGO_ROOT_PASSWORD` | Password of the root/admin user for MongoDB. |
+| `MONGO_USER_USERNAME` | Application username for MongoDB. |
+| `MONGO_USER_PASSWORD` | Application password for MongoDB. |
+| `REDIS_HOST` | Redis container host. |
+| `REDIS_PORT` | Redis container port. |
+| `REDIS_PASSWORD` | Redis password. |
+| `BACKUP_LOCATION` | Directory to store musare.sh backups. Defaults to `/backups` in script location. |
+| `BACKUP_NAME` | Name of musare.sh backup files. Defaults to `musare-$(date +"%Y-%m-%d-%s").dump`. |

+ 66 - 41
musare.sh

@@ -38,56 +38,67 @@ handleServices()
     fi
 }
 
+dockerCommand()
+{
+    validCommands=(start stop restart build ps)
+    if [[ ${validCommands[*]} =~ (^|[[:space:]])"$2"($|[[:space:]]) ]]; then
+        servicesString=$(handleServices "${@:3}")
+        if [[ ${servicesString:0:1} == 1 ]]; then
+            if [[ ${servicesString:2:4} == "all" ]]; then
+                servicesString=""
+            else
+                servicesString=${servicesString:2}
+            fi
+            if [[ ${2} == "stop" || ${2} == "restart" ]]; then
+                # shellcheck disable=SC2086
+                docker-compose stop ${servicesString}
+            fi
+            if [[ ${2} == "start" || ${2} == "restart" ]]; then
+                # shellcheck disable=SC2086
+                docker-compose up -d ${servicesString}
+            fi
+            if [[ ${2} == "build" || ${2} == "ps" ]]; then
+                # shellcheck disable=SC2086
+                docker-compose "${2}" ${servicesString}
+            fi
+        else
+            echo -e "${RED}${servicesString:2}\n${YELLOW}Usage: ${1} restart [backend, frontend, mongo, redis]${NC}"
+        fi
+    else
+        echo -e "${RED}Error: Invalid dockerCommand input${NC}"
+    fi
+}
+
 if [[ -x "$(command -v docker)" && -x "$(command -v docker-compose)" ]]; then
     case $1 in
     start)
         echo -e "${CYAN}Musare | Start Services${NC}"
-        servicesString=$(handleServices "${@:2}")
-        if [[ ${servicesString:0:1} == 1 && ${servicesString:2:4} == "all" ]]; then
-            docker-compose up -d
-        elif [[ ${servicesString:0:1} == 1 ]]; then
-            docker-compose up -d ${servicesString:2}
-        else
-            echo -e "${RED}${servicesString:2}\n${YELLOW}Usage: $(basename "$0") start [backend, frontend, mongo, redis]${NC}"
-        fi
+        # shellcheck disable=SC2068
+        dockerCommand "$(basename "$0")" start ${@:2}
         ;;
 
     stop)
         echo -e "${CYAN}Musare | Stop Services${NC}"
-        servicesString=$(handleServices "${@:2}")
-        if [[ ${servicesString:0:1} == 1 && ${servicesString:2:4} == "all" ]]; then
-            docker-compose stop
-        elif [[ ${servicesString:0:1} == 1 ]]; then
-            docker-compose stop ${servicesString:2}
-        else
-            echo -e "${RED}${servicesString:2}\n${YELLOW}Usage: $(basename "$0") stop [backend, frontend, mongo, redis]${NC}"
-        fi
+        # shellcheck disable=SC2068
+        dockerCommand "$(basename "$0")" stop ${@:2}
         ;;
 
     restart)
         echo -e "${CYAN}Musare | Restart Services${NC}"
-        servicesString=$(handleServices "${@:2}")
-        if [[ ${servicesString:0:1} == 1 && ${servicesString:2:4} == "all" ]]; then
-            docker-compose stop
-            docker-compose up -d
-        elif [[ ${servicesString:0:1} == 1 ]]; then
-            docker-compose stop ${servicesString:2}
-            docker-compose up -d ${servicesString:2}
-        else
-            echo -e "${RED}${servicesString:2}\n${YELLOW}Usage: $(basename "$0") restart [backend, frontend, mongo, redis]${NC}"
-        fi
+        # shellcheck disable=SC2068
+        dockerCommand "$(basename "$0")" restart ${@:2}
         ;;
 
     build)
         echo -e "${CYAN}Musare | Build Services${NC}"
-        servicesString=$(handleServices "${@:2}")
-        if [[ ${servicesString:0:1} == 1 && ${servicesString:2:4} == "all" ]]; then
-            docker-compose build
-        elif [[ ${servicesString:0:1} == 1 ]]; then
-            docker-compose build ${servicesString:2}
-        else
-            echo -e "${RED}${servicesString:2}\n${YELLOW}Usage: $(basename "$0") build [backend, frontend, mongo, redis]${NC}"
-        fi
+        # shellcheck disable=SC2068
+        dockerCommand "$(basename "$0")" build ${@:2}
+        ;;
+
+    status)
+        echo -e "${CYAN}Musare | Service Status${NC}"
+        # shellcheck disable=SC2068
+        dockerCommand "$(basename "$0")" ps ${@:2}
         ;;
 
     reset)
@@ -109,10 +120,12 @@ if [[ -x "$(command -v docker)" && -x "$(command -v docker-compose)" ]]; then
                 echo -e "${RED}Cancelled reset${NC}"
             fi
         elif [[ ${servicesString:0:1} == 1 ]]; then
-            echo -e "${GREEN}Are you sure you want to reset all data for $(echo ${servicesString:2} | tr ' ' ',')? ${YELLOW}[y,n]: ${NC}"
+            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" ]]; then
                     rm -rf .redis
@@ -146,7 +159,7 @@ if [[ -x "$(command -v docker)" && -x "$(command -v docker-compose)" ]]; then
                     echo -e "${RED}Error: Mongo offline, please start to attach.${NC}"
                 else
                     echo -e "${YELLOW}Detach with CTRL+C${NC}"
-                    docker-compose exec mongo mongo musare -u ${MONGO_USER_USERNAME} -p ${MONGO_USER_PASSWORD}
+                    docker-compose exec mongo mongo musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}"
                 fi
             else
                 echo -e "${RED}Error: .env does not exist${NC}"
@@ -221,12 +234,22 @@ if [[ -x "$(command -v docker)" && -x "$(command -v docker-compose)" ]]; then
         if [[ -f .env ]]; then
             # shellcheck disable=SC1091
             source .env
-            if [[ ! -d "${scriptLocation%x}/backups" ]]; then
-                echo -e "${YELLOW}Creating backup directory at ${scriptLocation%x}/backups${NC}"
-                mkdir "${scriptLocation%x}/backups"
+            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 ${scriptLocation%x}/backups/musare-$(date +"%Y-%m-%d-%s").dump${NC}"
-            docker-compose exec -T mongo sh -c "mongodump --authenticationDatabase musare -u ${MONGO_USER_USERNAME} -p ${MONGO_USER_PASSWORD} -d musare --archive" > "${scriptLocation%x}/backups/musare-$(date +"%Y-%m-%d-%s").dump"
+            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}"
         else
             echo -e "${RED}Error: .env does not exist${NC}"
         fi
@@ -299,6 +322,7 @@ if [[ -x "$(command -v docker)" && -x "$(command -v docker-compose)" ]]; then
         echo -e "${YELLOW}start - Start services${NC}"
         echo -e "${YELLOW}stop - Stop services${NC}"
         echo -e "${YELLOW}restart - Restart services${NC}"
+        echo -e "${YELLOW}status - Service status${NC}"
         echo -e "${YELLOW}logs - View logs for services${NC}"
         echo -e "${YELLOW}update - Update Musare${NC}"
         echo -e "${YELLOW}attach [backend,mongo] - Attach to backend service or mongo shell${NC}"
@@ -317,6 +341,7 @@ if [[ -x "$(command -v docker)" && -x "$(command -v docker-compose)" ]]; then
         echo -e "${YELLOW}start - Start services${NC}"
         echo -e "${YELLOW}stop - Stop services${NC}"
         echo -e "${YELLOW}restart - Restart services${NC}"
+        echo -e "${YELLOW}status - Service status${NC}"
         echo -e "${YELLOW}logs - View logs for services${NC}"
         echo -e "${YELLOW}update - Update Musare${NC}"
         echo -e "${YELLOW}attach [backend,mongo] - Attach to backend service or mongo shell${NC}"