musare.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 restart
  65. elif [[ ${services:0:1} == 1 ]]; then
  66. docker-compose restart ${services:2}
  67. else
  68. echo -e "${RED}${services:2}\n${YELLOW}Usage: $(basename $0) restart [backend, frontend, mongo, redis]${NC}"
  69. fi
  70. ;;
  71. build)
  72. echo -e "${CYAN}Musare | Build Services${NC}"
  73. services=$(handleServices "${@:2}")
  74. if [[ ${services:0:1} == 1 && ${services:2:4} == "all" ]]; then
  75. docker-compose build
  76. elif [[ ${services:0:1} == 1 ]]; then
  77. docker-compose build ${services:2}
  78. else
  79. echo -e "${RED}${services:2}\n${YELLOW}Usage: $(basename $0) build [backend, frontend, mongo, redis]${NC}"
  80. fi
  81. ;;
  82. reset)
  83. echo -e "${CYAN}Musare | Reset Services${NC}"
  84. services=$(handleServices "${@:2}")
  85. if [[ ${services:0:1} == 1 && ${services:2:4} == "all" ]]; then
  86. echo -e "${GREEN}Are you sure you want to reset all data? ${YELLOW}[y,n]: ${NC}"
  87. read confirm
  88. if [[ "${confirm}" == y* ]]; then
  89. docker-compose stop
  90. docker-compose rm -v --force
  91. if [[ -d ".redis" ]]; then
  92. rm -rf .redis
  93. fi
  94. if [[ -d ".db" ]]; then
  95. rm -rf .db
  96. fi
  97. else
  98. echo -e "${RED}Cancelled reset${NC}"
  99. fi
  100. elif [[ ${services:0:1} == 1 ]]; then
  101. echo -e "${GREEN}Are you sure you want to reset all data for $(echo ${services:2} | tr ' ' ',')? ${YELLOW}[y,n]: ${NC}"
  102. read confirm
  103. if [[ "${confirm}" == y* ]]; then
  104. docker-compose stop ${services:2}
  105. docker-compose rm -v --force ${services:2}
  106. if [[ "${services:2}" == *redis* && -d ".redis" ]]; then
  107. rm -rf .redis
  108. fi
  109. if [[ "${services:2}" == *mongo* && -d ".db" ]]; then
  110. rm -rf .db
  111. fi
  112. else
  113. echo -e "${RED}Cancelled reset${NC}"
  114. fi
  115. else
  116. echo -e "${RED}${services:2}\n${YELLOW}Usage: $(basename $0) build [backend, frontend, mongo, redis]${NC}"
  117. fi
  118. ;;
  119. attach)
  120. echo -e "${CYAN}Musare | Attach${NC}"
  121. if [[ $2 == "backend" ]]; then
  122. containerId=$(docker-compose ps -q backend)
  123. if [[ -z $containerId ]]; then
  124. echo -e "${RED}Error: Backend offline, please start to attach.${NC}"
  125. else
  126. docker attach $containerId
  127. fi
  128. else
  129. echo -e "${RED}Invalid service $2\n${YELLOW}Usage: $(basename $0) attach backend${NC}"
  130. fi
  131. ;;
  132. eslint)
  133. echo -e "${CYAN}Musare | ESLint${NC}"
  134. fix=""
  135. if [[ $2 == "fix" || $3 == "fix" || $2 == "--fix" || $3 == "--fix" ]]; then
  136. fix="--fix"
  137. echo -e "${GREEN}Auto-fix enabled${NC}"
  138. fi
  139. case $2 in
  140. frontend)
  141. docker-compose exec frontend npx eslint app/src --ext .js,.vue $fix
  142. ;;
  143. backend)
  144. docker-compose exec backend npx eslint app/logic $fix
  145. ;;
  146. ""|fix|--fix)
  147. docker-compose exec frontend npx eslint app/src --ext .js,.vue $fix
  148. docker-compose exec backend npx eslint app/logic $fix
  149. ;;
  150. *)
  151. echo -e "${RED}Invalid service $2\n${YELLOW}Usage: $(basename $0) eslint [backend, frontend] [fix]${NC}"
  152. ;;
  153. esac
  154. ;;
  155. update)
  156. echo -e "${CYAN}Musare | Update${NC}"
  157. git fetch
  158. if [[ $(git rev-parse HEAD) == $(git rev-parse @{u}) ]]; then
  159. echo -e "${GREEN}Already up to date${NC}"
  160. else
  161. git pull
  162. docker-compose build
  163. docker-compose up -d
  164. fi
  165. ;;
  166. logs)
  167. echo -e "${CYAN}Musare | Logs${NC}"
  168. docker-compose logs ${@:2}
  169. ;;
  170. backup)
  171. echo -e "${CYAN}Musare | Backup${NC}"
  172. if [[ -f .env ]]; then
  173. source .env
  174. echo -e "${YELLOW}Creating backup at ${PWD}/musare-$(date +"%Y-%m-%d-%s").dump${NC}"
  175. docker-compose exec -T mongo sh -c "mongodump --authenticationDatabase musare -u ${MONGO_USER_USERNAME} -p ${MONGO_USER_PASSWORD} -d musare --archive" > musare-$(date +"%Y-%m-%d-%s").dump
  176. else
  177. echo -e "${RED}Error: .env does not exist${NC}"
  178. fi
  179. ;;
  180. restore)
  181. echo -e "${CYAN}Musare | Restore${NC}"
  182. if [[ -f .env ]]; then
  183. source .env
  184. if [[ -z $2 ]]; then
  185. echo -e "${GREEN}Please enter the full path of the dump you wish to restore: ${NC}"
  186. read restoreFile
  187. else
  188. restoreFile=$2
  189. fi
  190. if [[ -z ${restoreFile} ]]; then
  191. echo -e "${RED}Error: no restore path given, cancelled restoration.${NC}"
  192. elif [[ -d ${restoreFile} ]]; then
  193. echo -e "${RED}Error: restore path given is a directory, cancelled restoration.${NC}"
  194. elif [[ ! -f ${restoreFile} ]]; then
  195. echo -e "${RED}Error: no file at restore path given, cancelled restoration.${NC}"
  196. else
  197. docker-compose exec -T mongo sh -c "mongorestore --authenticationDatabase musare -u ${MONGO_USER_USERNAME} -p ${MONGO_USER_PASSWORD} --archive" < ${restoreFile}
  198. fi
  199. else
  200. echo -e "${RED}Error: .env does not exist${NC}"
  201. fi
  202. ;;
  203. admin)
  204. echo -e "${CYAN}Musare | Add Admin${NC}"
  205. if [[ -f .env ]]; then
  206. source .env
  207. if [[ $2 == "add" ]]; then
  208. if [[ -z $3 ]]; then
  209. echo -e "${GREEN}Please enter the username of the user you wish to make an admin: ${NC}"
  210. read adminUser
  211. else
  212. adminUser=$3
  213. fi
  214. if [[ -z $adminUser ]]; then
  215. echo -e "${RED}Error: Username for new admin not provided.${NC}"
  216. else
  217. docker-compose exec mongo mongo musare -u ${MONGO_USER_USERNAME} -p ${MONGO_USER_PASSWORD} --eval "db.users.update({username: '${adminUser}'}, {\$set: {role: 'admin'}})"
  218. fi
  219. elif [[ $2 == "remove" ]]; then
  220. if [[ -z $3 ]]; then
  221. echo -e "${GREEN}Please enter the username of the user you wish to remove as admin: ${NC}"
  222. read adminUser
  223. else
  224. adminUser=$3
  225. fi
  226. if [[ -z $adminUser ]]; then
  227. echo -e "${RED}Error: Username for new admin not provided.${NC}"
  228. else
  229. docker-compose exec mongo mongo musare -u ${MONGO_USER_USERNAME} -p ${MONGO_USER_PASSWORD} --eval "db.users.update({username: '${adminUser}'}, {\$set: {role: 'default'}})"
  230. fi
  231. else
  232. echo -e "${RED}Invalid command $2\n${YELLOW}Usage: $(basename $0) admin [add,remove] username${NC}"
  233. fi
  234. else
  235. echo -e "${RED}Error: .env does not exist${NC}"
  236. fi
  237. ;;
  238. "")
  239. echo -e "${CYAN}Musare | Available Commands${NC}"
  240. echo -e "${YELLOW}start - Start services${NC}"
  241. echo -e "${YELLOW}stop - Stop services${NC}"
  242. echo -e "${YELLOW}restart - Restart services${NC}"
  243. echo -e "${YELLOW}logs - View logs for services${NC}"
  244. echo -e "${YELLOW}update - Update Musare${NC}"
  245. echo -e "${YELLOW}attach backend - Attach to backend service${NC}"
  246. echo -e "${YELLOW}build - Build services${NC}"
  247. echo -e "${YELLOW}eslint - Run eslint on frontend and/or backend${NC}"
  248. echo -e "${YELLOW}backup - Backup database data to file${NC}"
  249. echo -e "${YELLOW}restore - Restore database data from backup file${NC}"
  250. echo -e "${YELLOW}reset - Reset service data${NC}"
  251. echo -e "${YELLOW}admin [add,remove] - Assign/unassign admin role to/from a user${NC}"
  252. ;;
  253. *)
  254. echo -e "${CYAN}Musare${NC}"
  255. echo -e "${RED}Error: Invalid Command $1${NC}"
  256. echo -e "${CYAN}Available Commands:${NC}"
  257. echo -e "${YELLOW}start - Start services${NC}"
  258. echo -e "${YELLOW}stop - Stop services${NC}"
  259. echo -e "${YELLOW}restart - Restart services${NC}"
  260. echo -e "${YELLOW}logs - View logs for services${NC}"
  261. echo -e "${YELLOW}update - Update Musare${NC}"
  262. echo -e "${YELLOW}attach backend - Attach to backend service${NC}"
  263. echo -e "${YELLOW}build - Build services${NC}"
  264. echo -e "${YELLOW}eslint - Run eslint on frontend and/or backend${NC}"
  265. echo -e "${YELLOW}backup - Backup database data to file${NC}"
  266. echo -e "${YELLOW}restore - Restore database data from backup file${NC}"
  267. echo -e "${YELLOW}reset - Reset service data${NC}"
  268. echo -e "${YELLOW}admin [add,remove] - Assign/unassign admin role to/from a user${NC}"
  269. ;;
  270. esac
  271. elif [[ -x "$(command -v docker)" && ! -x "$(command -v docker-compose)" ]]; then
  272. echo -e "${RED}Error: docker-compose not installed.${NC}"
  273. elif [[ ! -x "$(command -v docker)" && -x "$(command -v docker-compose)" ]]; then
  274. echo -e "${RED}Error: docker not installed.${NC}"
  275. else
  276. echo -e "${RED}Error: docker and docker-compose not installed.${NC}"
  277. fi