musare.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. #!/bin/bash
  2. export PATH=/usr/local/bin:/usr/bin:/bin
  3. CYAN='\033[33;36m';
  4. RED='\033[0;31m'
  5. YELLOW='\033[0;93m'
  6. GREEN='\033[0;32m'
  7. NC='\033[0m'
  8. scriptLocation=$(dirname -- "$(readlink -fn -- "$0"; echo x)")
  9. cd "${scriptLocation%x}" || exit
  10. handleServices()
  11. {
  12. validServices=(backend frontend mongo redis)
  13. servicesArray=()
  14. invalidServices=false
  15. for x in "$@"; do
  16. if [[ ${validServices[*]} =~ (^|[[:space:]])"$x"($|[[:space:]]) ]]; then
  17. if ! [[ ${servicesArray[*]} =~ (^|[[:space:]])"$x"($|[[:space:]]) ]]; then
  18. servicesArray+=("${x}")
  19. fi
  20. else
  21. if [[ $invalidServices == false ]]; then
  22. invalidServices="${x}"
  23. else
  24. invalidServices="${invalidServices} ${x}"
  25. fi
  26. fi
  27. done
  28. if [[ $invalidServices == false && ${#servicesArray[@]} -gt 0 ]]; then
  29. echo "1|${servicesArray[*]}"
  30. elif [[ $invalidServices == false ]]; then
  31. echo "1|all"
  32. else
  33. echo "0|Invalid Service(s): ${invalidServices}"
  34. fi
  35. }
  36. if [[ -x "$(command -v docker)" && -x "$(command -v docker-compose)" ]]; then
  37. case $1 in
  38. start)
  39. echo -e "${CYAN}Musare | Start Services${NC}"
  40. servicesString=$(handleServices "${@:2}")
  41. if [[ ${servicesString:0:1} == 1 && ${servicesString:2:4} == "all" ]]; then
  42. docker-compose up -d
  43. elif [[ ${servicesString:0:1} == 1 ]]; then
  44. docker-compose up -d ${servicesString:2}
  45. else
  46. echo -e "${RED}${servicesString:2}\n${YELLOW}Usage: $(basename "$0") start [backend, frontend, mongo, redis]${NC}"
  47. fi
  48. ;;
  49. stop)
  50. echo -e "${CYAN}Musare | Stop Services${NC}"
  51. servicesString=$(handleServices "${@:2}")
  52. if [[ ${servicesString:0:1} == 1 && ${servicesString:2:4} == "all" ]]; then
  53. docker-compose stop
  54. elif [[ ${servicesString:0:1} == 1 ]]; then
  55. docker-compose stop ${servicesString:2}
  56. else
  57. echo -e "${RED}${servicesString:2}\n${YELLOW}Usage: $(basename "$0") stop [backend, frontend, mongo, redis]${NC}"
  58. fi
  59. ;;
  60. restart)
  61. echo -e "${CYAN}Musare | Restart Services${NC}"
  62. servicesString=$(handleServices "${@:2}")
  63. if [[ ${servicesString:0:1} == 1 && ${servicesString:2:4} == "all" ]]; then
  64. docker-compose stop
  65. docker-compose up -d
  66. elif [[ ${servicesString:0:1} == 1 ]]; then
  67. docker-compose stop ${servicesString:2}
  68. docker-compose up -d ${servicesString:2}
  69. else
  70. echo -e "${RED}${servicesString:2}\n${YELLOW}Usage: $(basename "$0") restart [backend, frontend, mongo, redis]${NC}"
  71. fi
  72. ;;
  73. build)
  74. echo -e "${CYAN}Musare | Build Services${NC}"
  75. servicesString=$(handleServices "${@:2}")
  76. if [[ ${servicesString:0:1} == 1 && ${servicesString:2:4} == "all" ]]; then
  77. docker-compose build
  78. elif [[ ${servicesString:0:1} == 1 ]]; then
  79. docker-compose build ${servicesString:2}
  80. else
  81. echo -e "${RED}${servicesString:2}\n${YELLOW}Usage: $(basename "$0") build [backend, frontend, mongo, redis]${NC}"
  82. fi
  83. ;;
  84. reset)
  85. echo -e "${CYAN}Musare | Reset Services${NC}"
  86. servicesString=$(handleServices "${@:2}")
  87. if [[ ${servicesString:0:1} == 1 && ${servicesString:2:4} == "all" ]]; then
  88. echo -e "${GREEN}Are you sure you want to reset all data? ${YELLOW}[y,n]: ${NC}"
  89. read -r confirm
  90. if [[ "${confirm}" == y* ]]; then
  91. docker-compose stop
  92. docker-compose rm -v --force
  93. if [[ -d ".redis" ]]; then
  94. rm -rf .redis
  95. fi
  96. if [[ -d ".db" ]]; then
  97. rm -rf .db
  98. fi
  99. else
  100. echo -e "${RED}Cancelled reset${NC}"
  101. fi
  102. elif [[ ${servicesString:0:1} == 1 ]]; then
  103. echo -e "${GREEN}Are you sure you want to reset all data for $(echo ${servicesString:2} | tr ' ' ',')? ${YELLOW}[y,n]: ${NC}"
  104. read -r confirm
  105. if [[ "${confirm}" == y* ]]; then
  106. docker-compose stop ${servicesString:2}
  107. docker-compose rm -v --force ${servicesString:2}
  108. if [[ "${servicesString:2}" == *redis* && -d ".redis" ]]; then
  109. rm -rf .redis
  110. fi
  111. if [[ "${servicesString:2}" == *mongo* && -d ".db" ]]; then
  112. rm -rf .db
  113. fi
  114. else
  115. echo -e "${RED}Cancelled reset${NC}"
  116. fi
  117. else
  118. echo -e "${RED}${servicesString:2}\n${YELLOW}Usage: $(basename "$0") build [backend, frontend, mongo, redis]${NC}"
  119. fi
  120. ;;
  121. attach)
  122. echo -e "${CYAN}Musare | Attach${NC}"
  123. if [[ $2 == "backend" ]]; then
  124. containerId=$(docker-compose ps -q backend)
  125. if [[ -z $containerId ]]; then
  126. echo -e "${RED}Error: Backend offline, please start to attach.${NC}"
  127. else
  128. echo -e "${YELLOW}Detach with CTRL+P+Q${NC}"
  129. docker attach "$containerId"
  130. fi
  131. else
  132. echo -e "${RED}Invalid service $2\n${YELLOW}Usage: $(basename "$0") attach backend${NC}"
  133. fi
  134. ;;
  135. eslint)
  136. echo -e "${CYAN}Musare | ESLint${NC}"
  137. fix=""
  138. if [[ $2 == "fix" || $3 == "fix" || $2 == "--fix" || $3 == "--fix" ]]; then
  139. fix="--fix"
  140. echo -e "${GREEN}Auto-fix enabled${NC}"
  141. fi
  142. case $2 in
  143. frontend)
  144. docker-compose exec frontend npx eslint app/src --ext .js,.vue $fix
  145. ;;
  146. backend)
  147. docker-compose exec backend npx eslint app/logic $fix
  148. ;;
  149. ""|fix|--fix)
  150. docker-compose exec frontend npx eslint app/src --ext .js,.vue $fix
  151. docker-compose exec backend npx eslint app/logic $fix
  152. ;;
  153. *)
  154. echo -e "${RED}Invalid service $2\n${YELLOW}Usage: $(basename "$0") eslint [backend, frontend] [fix]${NC}"
  155. ;;
  156. esac
  157. ;;
  158. update)
  159. echo -e "${CYAN}Musare | Update${NC}"
  160. git fetch
  161. if [[ $(git rev-parse HEAD) == $(git rev-parse @\{u\}) ]]; then
  162. echo -e "${GREEN}Already up to date${NC}"
  163. else
  164. dbChange=$(git log --name-only --oneline HEAD..origin/"$(git rev-parse --abbrev-ref HEAD)" | grep "backend/logic/db/schemas")
  165. fcChange=$(git log --name-only --oneline HEAD..origin/"$(git rev-parse --abbrev-ref HEAD)" | grep "frontend/dist/config/template.json")
  166. bcChange=$(git log --name-only --oneline HEAD..origin/"$(git rev-parse --abbrev-ref HEAD)" | grep "backend/config/template.json")
  167. if [[ ( $2 == "auto" && -z $dbChange && -z $fcChange && -z $bcChange ) || -z $2 ]]; then
  168. echo -e "${CYAN}Updating...${NC}"
  169. git pull
  170. docker-compose build
  171. docker-compose stop
  172. docker-compose up -d
  173. echo -e "${GREEN}Updated!${NC}"
  174. if [[ -n $dbChange ]]; then
  175. echo -e "${RED}Database schema has changed, please run migration!${NC}"
  176. fi
  177. if [[ -n $fcChange ]]; then
  178. echo -e "${RED}Frontend config has changed, please update!${NC}"
  179. fi
  180. if [[ -n $bcChange ]]; then
  181. echo -e "${RED}Backend config has changed, please update!${NC}"
  182. fi
  183. elif [[ $2 == "auto" ]]; then
  184. echo -e "${RED}Auto Update Failed! Database and/or config has changed!${NC}"
  185. fi
  186. fi
  187. ;;
  188. logs)
  189. echo -e "${CYAN}Musare | Logs${NC}"
  190. docker-compose logs "${@:2}"
  191. ;;
  192. backup)
  193. echo -e "${CYAN}Musare | Backup${NC}"
  194. if [[ -f .env ]]; then
  195. # shellcheck disable=SC1091
  196. source .env
  197. if [[ ! -d "${scriptLocation%x}/backups" ]]; then
  198. echo -e "${YELLOW}Creating backup directory at ${scriptLocation%x}/backups${NC}"
  199. mkdir "${scriptLocation%x}/backups"
  200. fi
  201. echo -e "${YELLOW}Creating backup at ${scriptLocation%x}/backups/musare-$(date +"%Y-%m-%d-%s").dump${NC}"
  202. 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"
  203. else
  204. echo -e "${RED}Error: .env does not exist${NC}"
  205. fi
  206. ;;
  207. restore)
  208. echo -e "${CYAN}Musare | Restore${NC}"
  209. if [[ -f .env ]]; then
  210. # shellcheck disable=SC1091
  211. source .env
  212. if [[ -z $2 ]]; then
  213. echo -e "${GREEN}Please enter the full path of the dump you wish to restore: ${NC}"
  214. read -r restoreFile
  215. else
  216. restoreFile=$2
  217. fi
  218. if [[ -z ${restoreFile} ]]; then
  219. echo -e "${RED}Error: no restore path given, cancelled restoration.${NC}"
  220. elif [[ -d ${restoreFile} ]]; then
  221. echo -e "${RED}Error: restore path given is a directory, cancelled restoration.${NC}"
  222. elif [[ ! -f ${restoreFile} ]]; then
  223. echo -e "${RED}Error: no file at restore path given, cancelled restoration.${NC}"
  224. else
  225. docker-compose exec -T mongo sh -c "mongorestore --authenticationDatabase musare -u ${MONGO_USER_USERNAME} -p ${MONGO_USER_PASSWORD} --archive" < "${restoreFile}"
  226. fi
  227. else
  228. echo -e "${RED}Error: .env does not exist${NC}"
  229. fi
  230. ;;
  231. admin)
  232. echo -e "${CYAN}Musare | Add Admin${NC}"
  233. if [[ -f .env ]]; then
  234. # shellcheck disable=SC1091
  235. source .env
  236. if [[ $2 == "add" ]]; then
  237. if [[ -z $3 ]]; then
  238. echo -e "${GREEN}Please enter the username of the user you wish to make an admin: ${NC}"
  239. read -r adminUser
  240. else
  241. adminUser=$3
  242. fi
  243. if [[ -z $adminUser ]]; then
  244. echo -e "${RED}Error: Username for new admin not provided.${NC}"
  245. else
  246. docker-compose exec mongo mongo musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}" --eval "db.users.update({username: '${adminUser}'}, {\$set: {role: 'admin'}})"
  247. fi
  248. elif [[ $2 == "remove" ]]; then
  249. if [[ -z $3 ]]; then
  250. echo -e "${GREEN}Please enter the username of the user you wish to remove as admin: ${NC}"
  251. read -r adminUser
  252. else
  253. adminUser=$3
  254. fi
  255. if [[ -z $adminUser ]]; then
  256. echo -e "${RED}Error: Username for new admin not provided.${NC}"
  257. else
  258. docker-compose exec mongo mongo musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}" --eval "db.users.update({username: '${adminUser}'}, {\$set: {role: 'default'}})"
  259. fi
  260. else
  261. echo -e "${RED}Invalid command $2\n${YELLOW}Usage: $(basename "$0") admin [add,remove] username${NC}"
  262. fi
  263. else
  264. echo -e "${RED}Error: .env does not exist${NC}"
  265. fi
  266. ;;
  267. "")
  268. echo -e "${CYAN}Musare | Available Commands${NC}"
  269. echo -e "${YELLOW}start - Start services${NC}"
  270. echo -e "${YELLOW}stop - Stop services${NC}"
  271. echo -e "${YELLOW}restart - Restart services${NC}"
  272. echo -e "${YELLOW}logs - View logs for services${NC}"
  273. echo -e "${YELLOW}update - Update Musare${NC}"
  274. echo -e "${YELLOW}attach [backend,mongo] - Attach to backend service or mongo shell${NC}"
  275. echo -e "${YELLOW}build - Build services${NC}"
  276. echo -e "${YELLOW}eslint - Run eslint on frontend and/or backend${NC}"
  277. echo -e "${YELLOW}backup - Backup database data to file${NC}"
  278. echo -e "${YELLOW}restore - Restore database data from backup file${NC}"
  279. echo -e "${YELLOW}reset - Reset service data${NC}"
  280. echo -e "${YELLOW}admin [add,remove] - Assign/unassign admin role to/from a user${NC}"
  281. ;;
  282. *)
  283. echo -e "${CYAN}Musare${NC}"
  284. echo -e "${RED}Error: Invalid Command $1${NC}"
  285. echo -e "${CYAN}Available Commands:${NC}"
  286. echo -e "${YELLOW}start - Start services${NC}"
  287. echo -e "${YELLOW}stop - Stop services${NC}"
  288. echo -e "${YELLOW}restart - Restart services${NC}"
  289. echo -e "${YELLOW}logs - View logs for services${NC}"
  290. echo -e "${YELLOW}update - Update Musare${NC}"
  291. echo -e "${YELLOW}attach [backend,mongo] - Attach to backend service or mongo shell${NC}"
  292. echo -e "${YELLOW}build - Build services${NC}"
  293. echo -e "${YELLOW}eslint - Run eslint on frontend and/or backend${NC}"
  294. echo -e "${YELLOW}backup - Backup database data to file${NC}"
  295. echo -e "${YELLOW}restore - Restore database data from backup file${NC}"
  296. echo -e "${YELLOW}reset - Reset service data${NC}"
  297. echo -e "${YELLOW}admin [add,remove] - Assign/unassign admin role to/from a user${NC}"
  298. ;;
  299. esac
  300. elif [[ -x "$(command -v docker)" && ! -x "$(command -v docker-compose)" ]]; then
  301. echo -e "${RED}Error: docker-compose not installed.${NC}"
  302. elif [[ ! -x "$(command -v docker)" && -x "$(command -v docker-compose)" ]]; then
  303. echo -e "${RED}Error: docker not installed.${NC}"
  304. else
  305. echo -e "${RED}Error: docker and docker-compose not installed.${NC}"
  306. fi