musare.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. elif [[ $2 == "mongo" ]]; then
  132. if [[ -f .env ]]; then
  133. # shellcheck disable=SC1091
  134. source .env
  135. if [[ -z $(docker-compose ps -q mongo) ]]; then
  136. echo -e "${RED}Error: Mongo offline, please start to attach.${NC}"
  137. else
  138. echo -e "${YELLOW}Detach with CTRL+C${NC}"
  139. docker-compose exec mongo mongo musare -u ${MONGO_USER_USERNAME} -p ${MONGO_USER_PASSWORD}
  140. fi
  141. else
  142. echo -e "${RED}Error: .env does not exist${NC}"
  143. fi
  144. else
  145. echo -e "${RED}Invalid service $2\n${YELLOW}Usage: $(basename "$0") attach [backend,mongo]${NC}"
  146. fi
  147. ;;
  148. eslint)
  149. echo -e "${CYAN}Musare | ESLint${NC}"
  150. fix=""
  151. if [[ $2 == "fix" || $3 == "fix" || $2 == "--fix" || $3 == "--fix" ]]; then
  152. fix="--fix"
  153. echo -e "${GREEN}Auto-fix enabled${NC}"
  154. fi
  155. case $2 in
  156. frontend)
  157. docker-compose exec frontend npx eslint app/src --ext .js,.vue $fix
  158. ;;
  159. backend)
  160. docker-compose exec backend npx eslint app/logic $fix
  161. ;;
  162. ""|fix|--fix)
  163. docker-compose exec frontend npx eslint app/src --ext .js,.vue $fix
  164. docker-compose exec backend npx eslint app/logic $fix
  165. ;;
  166. *)
  167. echo -e "${RED}Invalid service $2\n${YELLOW}Usage: $(basename "$0") eslint [backend, frontend] [fix]${NC}"
  168. ;;
  169. esac
  170. ;;
  171. update)
  172. echo -e "${CYAN}Musare | Update${NC}"
  173. git fetch
  174. if [[ $(git rev-parse HEAD) == $(git rev-parse @\{u\}) ]]; then
  175. echo -e "${GREEN}Already up to date${NC}"
  176. else
  177. dbChange=$(git log --name-only --oneline HEAD..origin/"$(git rev-parse --abbrev-ref HEAD)" | grep "backend/logic/db/schemas")
  178. fcChange=$(git log --name-only --oneline HEAD..origin/"$(git rev-parse --abbrev-ref HEAD)" | grep "frontend/dist/config/template.json")
  179. bcChange=$(git log --name-only --oneline HEAD..origin/"$(git rev-parse --abbrev-ref HEAD)" | grep "backend/config/template.json")
  180. if [[ ( $2 == "auto" && -z $dbChange && -z $fcChange && -z $bcChange ) || -z $2 ]]; then
  181. echo -e "${CYAN}Updating...${NC}"
  182. git pull
  183. docker-compose build
  184. docker-compose stop
  185. docker-compose up -d
  186. echo -e "${GREEN}Updated!${NC}"
  187. if [[ -n $dbChange ]]; then
  188. echo -e "${RED}Database schema has changed, please run migration!${NC}"
  189. fi
  190. if [[ -n $fcChange ]]; then
  191. echo -e "${RED}Frontend config has changed, please update!${NC}"
  192. fi
  193. if [[ -n $bcChange ]]; then
  194. echo -e "${RED}Backend config has changed, please update!${NC}"
  195. fi
  196. elif [[ $2 == "auto" ]]; then
  197. echo -e "${RED}Auto Update Failed! Database and/or config has changed!${NC}"
  198. fi
  199. fi
  200. ;;
  201. logs)
  202. echo -e "${CYAN}Musare | Logs${NC}"
  203. docker-compose logs "${@:2}"
  204. ;;
  205. backup)
  206. echo -e "${CYAN}Musare | Backup${NC}"
  207. if [[ -f .env ]]; then
  208. # shellcheck disable=SC1091
  209. source .env
  210. if [[ ! -d "${scriptLocation%x}/backups" ]]; then
  211. echo -e "${YELLOW}Creating backup directory at ${scriptLocation%x}/backups${NC}"
  212. mkdir "${scriptLocation%x}/backups"
  213. fi
  214. echo -e "${YELLOW}Creating backup at ${scriptLocation%x}/backups/musare-$(date +"%Y-%m-%d-%s").dump${NC}"
  215. 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"
  216. else
  217. echo -e "${RED}Error: .env does not exist${NC}"
  218. fi
  219. ;;
  220. restore)
  221. echo -e "${CYAN}Musare | Restore${NC}"
  222. if [[ -f .env ]]; then
  223. # shellcheck disable=SC1091
  224. source .env
  225. if [[ -z $2 ]]; then
  226. echo -e "${GREEN}Please enter the full path of the dump you wish to restore: ${NC}"
  227. read -r restoreFile
  228. else
  229. restoreFile=$2
  230. fi
  231. if [[ -z ${restoreFile} ]]; then
  232. echo -e "${RED}Error: no restore path given, cancelled restoration.${NC}"
  233. elif [[ -d ${restoreFile} ]]; then
  234. echo -e "${RED}Error: restore path given is a directory, cancelled restoration.${NC}"
  235. elif [[ ! -f ${restoreFile} ]]; then
  236. echo -e "${RED}Error: no file at restore path given, cancelled restoration.${NC}"
  237. else
  238. docker-compose exec -T mongo sh -c "mongorestore --authenticationDatabase musare -u ${MONGO_USER_USERNAME} -p ${MONGO_USER_PASSWORD} --archive" < "${restoreFile}"
  239. fi
  240. else
  241. echo -e "${RED}Error: .env does not exist${NC}"
  242. fi
  243. ;;
  244. admin)
  245. echo -e "${CYAN}Musare | Add Admin${NC}"
  246. if [[ -f .env ]]; then
  247. # shellcheck disable=SC1091
  248. source .env
  249. if [[ $2 == "add" ]]; then
  250. if [[ -z $3 ]]; then
  251. echo -e "${GREEN}Please enter the username of the user you wish to make an admin: ${NC}"
  252. read -r adminUser
  253. else
  254. adminUser=$3
  255. fi
  256. if [[ -z $adminUser ]]; then
  257. echo -e "${RED}Error: Username for new admin not provided.${NC}"
  258. else
  259. docker-compose exec mongo mongo musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}" --eval "db.users.update({username: '${adminUser}'}, {\$set: {role: 'admin'}})"
  260. fi
  261. elif [[ $2 == "remove" ]]; then
  262. if [[ -z $3 ]]; then
  263. echo -e "${GREEN}Please enter the username of the user you wish to remove as admin: ${NC}"
  264. read -r adminUser
  265. else
  266. adminUser=$3
  267. fi
  268. if [[ -z $adminUser ]]; then
  269. echo -e "${RED}Error: Username for new admin not provided.${NC}"
  270. else
  271. docker-compose exec mongo mongo musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}" --eval "db.users.update({username: '${adminUser}'}, {\$set: {role: 'default'}})"
  272. fi
  273. else
  274. echo -e "${RED}Invalid command $2\n${YELLOW}Usage: $(basename "$0") admin [add,remove] username${NC}"
  275. fi
  276. else
  277. echo -e "${RED}Error: .env does not exist${NC}"
  278. fi
  279. ;;
  280. "")
  281. echo -e "${CYAN}Musare | Available Commands${NC}"
  282. echo -e "${YELLOW}start - Start services${NC}"
  283. echo -e "${YELLOW}stop - Stop services${NC}"
  284. echo -e "${YELLOW}restart - Restart services${NC}"
  285. echo -e "${YELLOW}logs - View logs for services${NC}"
  286. echo -e "${YELLOW}update - Update Musare${NC}"
  287. echo -e "${YELLOW}attach [backend,mongo] - Attach to backend service or mongo shell${NC}"
  288. echo -e "${YELLOW}build - Build services${NC}"
  289. echo -e "${YELLOW}eslint - Run eslint on frontend and/or backend${NC}"
  290. echo -e "${YELLOW}backup - Backup database data to file${NC}"
  291. echo -e "${YELLOW}restore - Restore database data from backup file${NC}"
  292. echo -e "${YELLOW}reset - Reset service data${NC}"
  293. echo -e "${YELLOW}admin [add,remove] - Assign/unassign admin role to/from a user${NC}"
  294. ;;
  295. *)
  296. echo -e "${CYAN}Musare${NC}"
  297. echo -e "${RED}Error: Invalid Command $1${NC}"
  298. echo -e "${CYAN}Available Commands:${NC}"
  299. echo -e "${YELLOW}start - Start services${NC}"
  300. echo -e "${YELLOW}stop - Stop services${NC}"
  301. echo -e "${YELLOW}restart - Restart services${NC}"
  302. echo -e "${YELLOW}logs - View logs for services${NC}"
  303. echo -e "${YELLOW}update - Update Musare${NC}"
  304. echo -e "${YELLOW}attach [backend,mongo] - Attach to backend service or mongo shell${NC}"
  305. echo -e "${YELLOW}build - Build services${NC}"
  306. echo -e "${YELLOW}eslint - Run eslint on frontend and/or backend${NC}"
  307. echo -e "${YELLOW}backup - Backup database data to file${NC}"
  308. echo -e "${YELLOW}restore - Restore database data from backup file${NC}"
  309. echo -e "${YELLOW}reset - Reset service data${NC}"
  310. echo -e "${YELLOW}admin [add,remove] - Assign/unassign admin role to/from a user${NC}"
  311. ;;
  312. esac
  313. elif [[ -x "$(command -v docker)" && ! -x "$(command -v docker-compose)" ]]; then
  314. echo -e "${RED}Error: docker-compose not installed.${NC}"
  315. elif [[ ! -x "$(command -v docker)" && -x "$(command -v docker-compose)" ]]; then
  316. echo -e "${RED}Error: docker not installed.${NC}"
  317. else
  318. echo -e "${RED}Error: docker and docker-compose not installed.${NC}"
  319. fi