musare.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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}"
  10. handleServices()
  11. {
  12. validServices=(backend frontend mongo redis)
  13. services=()
  14. invalidServices=false
  15. for x in $@; do
  16. if [[ ${validServices[@]} =~ (^|[[:space:]])"$x"($|[[:space:]]) ]]; then
  17. if ! [[ ${services[@]} =~ (^|[[:space:]])"$x"($|[[:space:]]) ]]; then
  18. services+=("${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 && ${#services[@]} > 0 ]]; then
  29. echo "1|${services[@]}"
  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. services=$(handleServices "${@:2}")
  41. if [[ ${services:0:1} == 1 && ${services:2:4} == "all" ]]; then
  42. docker-compose up -d
  43. elif [[ ${services:0:1} == 1 ]]; then
  44. docker-compose up -d ${services:2}
  45. else
  46. echo -e "${RED}${services: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. services=$(handleServices "${@:2}")
  52. if [[ ${services:0:1} == 1 && ${services:2:4} == "all" ]]; then
  53. docker-compose stop
  54. elif [[ ${services:0:1} == 1 ]]; then
  55. docker-compose stop ${services:2}
  56. else
  57. echo -e "${RED}${services: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. services=$(handleServices "${@:2}")
  63. if [[ ${services:0:1} == 1 && ${services:2:4} == "all" ]]; then
  64. docker-compose stop
  65. docker-compose up -d
  66. elif [[ ${services:0:1} == 1 ]]; then
  67. docker-compose stop ${services:2}
  68. docker-compose up -d ${services:2}
  69. else
  70. echo -e "${RED}${services: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. services=$(handleServices "${@:2}")
  76. if [[ ${services:0:1} == 1 && ${services:2:4} == "all" ]]; then
  77. docker-compose build
  78. elif [[ ${services:0:1} == 1 ]]; then
  79. docker-compose build ${services:2}
  80. else
  81. echo -e "${RED}${services: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. services=$(handleServices "${@:2}")
  87. if [[ ${services:0:1} == 1 && ${services:2:4} == "all" ]]; then
  88. echo -e "${GREEN}Are you sure you want to reset all data? ${YELLOW}[y,n]: ${NC}"
  89. read 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 [[ ${services:0:1} == 1 ]]; then
  103. echo -e "${GREEN}Are you sure you want to reset all data for $(echo ${services:2} | tr ' ' ',')? ${YELLOW}[y,n]: ${NC}"
  104. read confirm
  105. if [[ "${confirm}" == y* ]]; then
  106. docker-compose stop ${services:2}
  107. docker-compose rm -v --force ${services:2}
  108. if [[ "${services:2}" == *redis* && -d ".redis" ]]; then
  109. rm -rf .redis
  110. fi
  111. if [[ "${services: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}${services: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. docker attach $containerId
  129. fi
  130. else
  131. echo -e "${RED}Invalid service $2\n${YELLOW}Usage: $(basename $0) attach backend${NC}"
  132. fi
  133. ;;
  134. eslint)
  135. echo -e "${CYAN}Musare | ESLint${NC}"
  136. fix=""
  137. if [[ $2 == "fix" || $3 == "fix" || $2 == "--fix" || $3 == "--fix" ]]; then
  138. fix="--fix"
  139. echo -e "${GREEN}Auto-fix enabled${NC}"
  140. fi
  141. case $2 in
  142. frontend)
  143. docker-compose exec frontend npx eslint app/src --ext .js,.vue $fix
  144. ;;
  145. backend)
  146. docker-compose exec backend npx eslint app/logic $fix
  147. ;;
  148. ""|fix|--fix)
  149. docker-compose exec frontend npx eslint app/src --ext .js,.vue $fix
  150. docker-compose exec backend npx eslint app/logic $fix
  151. ;;
  152. *)
  153. echo -e "${RED}Invalid service $2\n${YELLOW}Usage: $(basename $0) eslint [backend, frontend] [fix]${NC}"
  154. ;;
  155. esac
  156. ;;
  157. update)
  158. echo -e "${CYAN}Musare | Update${NC}"
  159. git fetch
  160. if [[ $(git rev-parse HEAD) == $(git rev-parse @{u}) ]]; then
  161. echo -e "${GREEN}Already up to date${NC}"
  162. else
  163. dbChange=$(git log --name-only --oneline HEAD..origin/$(git rev-parse --abbrev-ref HEAD) | grep "backend/logic/db/schemas")
  164. fcChange=$(git log --name-only --oneline HEAD..origin/$(git rev-parse --abbrev-ref HEAD) | grep "frontend/dist/config/template.json")
  165. bcChange=$(git log --name-only --oneline HEAD..origin/$(git rev-parse --abbrev-ref HEAD) | grep "backend/config/template.json")
  166. if [[ ( $2 == "auto" && -z $dbChange && -z $fcChange && -z $bcChange ) || -z $2 ]]; then
  167. echo -e "${CYAN}Updating...${NC}"
  168. git pull
  169. docker-compose build
  170. docker-compose stop
  171. docker-compose up -d
  172. echo -e "${GREEN}Updated!${NC}"
  173. if [[ -n $dbChange ]]; then
  174. echo -e "${RED}Database schema has changed, please run migration!${NC}"
  175. fi
  176. if [[ -n $fcChange ]]; then
  177. echo -e "${RED}Frontend config has changed, please update!${NC}"
  178. fi
  179. if [[ -n $bcChange ]]; then
  180. echo -e "${RED}Backend config has changed, please update!${NC}"
  181. fi
  182. elif [[ $2 == "auto" ]]; then
  183. echo -e "${RED}Auto Update Failed! Database and/or config has changed!${NC}"
  184. fi
  185. fi
  186. ;;
  187. logs)
  188. echo -e "${CYAN}Musare | Logs${NC}"
  189. docker-compose logs ${@:2}
  190. ;;
  191. backup)
  192. echo -e "${CYAN}Musare | Backup${NC}"
  193. if [[ -f .env ]]; then
  194. source .env
  195. if [[ ! -d "${scriptLocation%x}/backups" ]]; then
  196. echo -e "${YELLOW}Creating backup directory at ${scriptLocation%x}/backups${NC}"
  197. mkdir "${scriptLocation%x}/backups"
  198. fi
  199. echo -e "${YELLOW}Creating backup at ${scriptLocation%x}/backups/musare-$(date +"%Y-%m-%d-%s").dump${NC}"
  200. 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"
  201. else
  202. echo -e "${RED}Error: .env does not exist${NC}"
  203. fi
  204. ;;
  205. restore)
  206. echo -e "${CYAN}Musare | Restore${NC}"
  207. if [[ -f .env ]]; then
  208. source .env
  209. if [[ -z $2 ]]; then
  210. echo -e "${GREEN}Please enter the full path of the dump you wish to restore: ${NC}"
  211. read restoreFile
  212. else
  213. restoreFile=$2
  214. fi
  215. if [[ -z ${restoreFile} ]]; then
  216. echo -e "${RED}Error: no restore path given, cancelled restoration.${NC}"
  217. elif [[ -d ${restoreFile} ]]; then
  218. echo -e "${RED}Error: restore path given is a directory, cancelled restoration.${NC}"
  219. elif [[ ! -f ${restoreFile} ]]; then
  220. echo -e "${RED}Error: no file at restore path given, cancelled restoration.${NC}"
  221. else
  222. docker-compose exec -T mongo sh -c "mongorestore --authenticationDatabase musare -u ${MONGO_USER_USERNAME} -p ${MONGO_USER_PASSWORD} --archive" < ${restoreFile}
  223. fi
  224. else
  225. echo -e "${RED}Error: .env does not exist${NC}"
  226. fi
  227. ;;
  228. admin)
  229. echo -e "${CYAN}Musare | Add Admin${NC}"
  230. if [[ -f .env ]]; then
  231. source .env
  232. if [[ $2 == "add" ]]; then
  233. if [[ -z $3 ]]; then
  234. echo -e "${GREEN}Please enter the username of the user you wish to make an admin: ${NC}"
  235. read adminUser
  236. else
  237. adminUser=$3
  238. fi
  239. if [[ -z $adminUser ]]; then
  240. echo -e "${RED}Error: Username for new admin not provided.${NC}"
  241. else
  242. docker-compose exec mongo mongo musare -u ${MONGO_USER_USERNAME} -p ${MONGO_USER_PASSWORD} --eval "db.users.update({username: '${adminUser}'}, {\$set: {role: 'admin'}})"
  243. fi
  244. elif [[ $2 == "remove" ]]; then
  245. if [[ -z $3 ]]; then
  246. echo -e "${GREEN}Please enter the username of the user you wish to remove as admin: ${NC}"
  247. read adminUser
  248. else
  249. adminUser=$3
  250. fi
  251. if [[ -z $adminUser ]]; then
  252. echo -e "${RED}Error: Username for new admin not provided.${NC}"
  253. else
  254. docker-compose exec mongo mongo musare -u ${MONGO_USER_USERNAME} -p ${MONGO_USER_PASSWORD} --eval "db.users.update({username: '${adminUser}'}, {\$set: {role: 'default'}})"
  255. fi
  256. else
  257. echo -e "${RED}Invalid command $2\n${YELLOW}Usage: $(basename $0) admin [add,remove] username${NC}"
  258. fi
  259. else
  260. echo -e "${RED}Error: .env does not exist${NC}"
  261. fi
  262. ;;
  263. "")
  264. echo -e "${CYAN}Musare | Available Commands${NC}"
  265. echo -e "${YELLOW}start - Start services${NC}"
  266. echo -e "${YELLOW}stop - Stop services${NC}"
  267. echo -e "${YELLOW}restart - Restart services${NC}"
  268. echo -e "${YELLOW}logs - View logs for services${NC}"
  269. echo -e "${YELLOW}update - Update Musare${NC}"
  270. echo -e "${YELLOW}attach backend - Attach to backend service${NC}"
  271. echo -e "${YELLOW}build - Build services${NC}"
  272. echo -e "${YELLOW}eslint - Run eslint on frontend and/or backend${NC}"
  273. echo -e "${YELLOW}backup - Backup database data to file${NC}"
  274. echo -e "${YELLOW}restore - Restore database data from backup file${NC}"
  275. echo -e "${YELLOW}reset - Reset service data${NC}"
  276. echo -e "${YELLOW}admin [add,remove] - Assign/unassign admin role to/from a user${NC}"
  277. ;;
  278. *)
  279. echo -e "${CYAN}Musare${NC}"
  280. echo -e "${RED}Error: Invalid Command $1${NC}"
  281. echo -e "${CYAN}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 - Attach to backend service${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. esac
  296. elif [[ -x "$(command -v docker)" && ! -x "$(command -v docker-compose)" ]]; then
  297. echo -e "${RED}Error: docker-compose not installed.${NC}"
  298. elif [[ ! -x "$(command -v docker)" && -x "$(command -v docker-compose)" ]]; then
  299. echo -e "${RED}Error: docker not installed.${NC}"
  300. else
  301. echo -e "${RED}Error: docker and docker-compose not installed.${NC}"
  302. fi