musare.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. if [[ ! -d "${scriptLocation%x}/backups" ]]; then
  175. echo -e "${YELLOW}Creating backup directory at ${scriptLocation%x}/backups${NC}"
  176. mkdir "${scriptLocation%x}/backups"
  177. fi
  178. echo -e "${YELLOW}Creating backup at ${scriptLocation%x}/backups/musare-$(date +"%Y-%m-%d-%s").dump${NC}"
  179. 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"
  180. else
  181. echo -e "${RED}Error: .env does not exist${NC}"
  182. fi
  183. ;;
  184. restore)
  185. echo -e "${CYAN}Musare | Restore${NC}"
  186. if [[ -f .env ]]; then
  187. source .env
  188. if [[ -z $2 ]]; then
  189. echo -e "${GREEN}Please enter the full path of the dump you wish to restore: ${NC}"
  190. read restoreFile
  191. else
  192. restoreFile=$2
  193. fi
  194. if [[ -z ${restoreFile} ]]; then
  195. echo -e "${RED}Error: no restore path given, cancelled restoration.${NC}"
  196. elif [[ -d ${restoreFile} ]]; then
  197. echo -e "${RED}Error: restore path given is a directory, cancelled restoration.${NC}"
  198. elif [[ ! -f ${restoreFile} ]]; then
  199. echo -e "${RED}Error: no file at restore path given, cancelled restoration.${NC}"
  200. else
  201. docker-compose exec -T mongo sh -c "mongorestore --authenticationDatabase musare -u ${MONGO_USER_USERNAME} -p ${MONGO_USER_PASSWORD} --archive" < ${restoreFile}
  202. fi
  203. else
  204. echo -e "${RED}Error: .env does not exist${NC}"
  205. fi
  206. ;;
  207. admin)
  208. echo -e "${CYAN}Musare | Add Admin${NC}"
  209. if [[ -f .env ]]; then
  210. source .env
  211. if [[ $2 == "add" ]]; then
  212. if [[ -z $3 ]]; then
  213. echo -e "${GREEN}Please enter the username of the user you wish to make an admin: ${NC}"
  214. read adminUser
  215. else
  216. adminUser=$3
  217. fi
  218. if [[ -z $adminUser ]]; then
  219. echo -e "${RED}Error: Username for new admin not provided.${NC}"
  220. else
  221. docker-compose exec mongo mongo musare -u ${MONGO_USER_USERNAME} -p ${MONGO_USER_PASSWORD} --eval "db.users.update({username: '${adminUser}'}, {\$set: {role: 'admin'}})"
  222. fi
  223. elif [[ $2 == "remove" ]]; then
  224. if [[ -z $3 ]]; then
  225. echo -e "${GREEN}Please enter the username of the user you wish to remove as admin: ${NC}"
  226. read adminUser
  227. else
  228. adminUser=$3
  229. fi
  230. if [[ -z $adminUser ]]; then
  231. echo -e "${RED}Error: Username for new admin not provided.${NC}"
  232. else
  233. docker-compose exec mongo mongo musare -u ${MONGO_USER_USERNAME} -p ${MONGO_USER_PASSWORD} --eval "db.users.update({username: '${adminUser}'}, {\$set: {role: 'default'}})"
  234. fi
  235. else
  236. echo -e "${RED}Invalid command $2\n${YELLOW}Usage: $(basename $0) admin [add,remove] username${NC}"
  237. fi
  238. else
  239. echo -e "${RED}Error: .env does not exist${NC}"
  240. fi
  241. ;;
  242. "")
  243. echo -e "${CYAN}Musare | Available Commands${NC}"
  244. echo -e "${YELLOW}start - Start services${NC}"
  245. echo -e "${YELLOW}stop - Stop services${NC}"
  246. echo -e "${YELLOW}restart - Restart services${NC}"
  247. echo -e "${YELLOW}logs - View logs for services${NC}"
  248. echo -e "${YELLOW}update - Update Musare${NC}"
  249. echo -e "${YELLOW}attach backend - Attach to backend service${NC}"
  250. echo -e "${YELLOW}build - Build services${NC}"
  251. echo -e "${YELLOW}eslint - Run eslint on frontend and/or backend${NC}"
  252. echo -e "${YELLOW}backup - Backup database data to file${NC}"
  253. echo -e "${YELLOW}restore - Restore database data from backup file${NC}"
  254. echo -e "${YELLOW}reset - Reset service data${NC}"
  255. echo -e "${YELLOW}admin [add,remove] - Assign/unassign admin role to/from a user${NC}"
  256. ;;
  257. *)
  258. echo -e "${CYAN}Musare${NC}"
  259. echo -e "${RED}Error: Invalid Command $1${NC}"
  260. echo -e "${CYAN}Available Commands:${NC}"
  261. echo -e "${YELLOW}start - Start services${NC}"
  262. echo -e "${YELLOW}stop - Stop services${NC}"
  263. echo -e "${YELLOW}restart - Restart services${NC}"
  264. echo -e "${YELLOW}logs - View logs for services${NC}"
  265. echo -e "${YELLOW}update - Update Musare${NC}"
  266. echo -e "${YELLOW}attach backend - Attach to backend service${NC}"
  267. echo -e "${YELLOW}build - Build services${NC}"
  268. echo -e "${YELLOW}eslint - Run eslint on frontend and/or backend${NC}"
  269. echo -e "${YELLOW}backup - Backup database data to file${NC}"
  270. echo -e "${YELLOW}restore - Restore database data from backup file${NC}"
  271. echo -e "${YELLOW}reset - Reset service data${NC}"
  272. echo -e "${YELLOW}admin [add,remove] - Assign/unassign admin role to/from a user${NC}"
  273. ;;
  274. esac
  275. elif [[ -x "$(command -v docker)" && ! -x "$(command -v docker-compose)" ]]; then
  276. echo -e "${RED}Error: docker-compose not installed.${NC}"
  277. elif [[ ! -x "$(command -v docker)" && -x "$(command -v docker-compose)" ]]; then
  278. echo -e "${RED}Error: docker not installed.${NC}"
  279. else
  280. echo -e "${RED}Error: docker and docker-compose not installed.${NC}"
  281. fi