musare.sh 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. dockerCommand()
  37. {
  38. validCommands=(start stop restart pull build ps)
  39. if [[ ${validCommands[*]} =~ (^|[[:space:]])"$2"($|[[:space:]]) ]]; then
  40. servicesString=$(handleServices "${@:3}")
  41. if [[ ${servicesString:0:1} == 1 ]]; then
  42. if [[ ${servicesString:2:4} == "all" ]]; then
  43. servicesString=""
  44. else
  45. servicesString=${servicesString:2}
  46. fi
  47. if [[ ${2} == "stop" || ${2} == "restart" ]]; then
  48. # shellcheck disable=SC2086
  49. docker-compose stop ${servicesString}
  50. fi
  51. if [[ ${2} == "start" || ${2} == "restart" ]]; then
  52. # shellcheck disable=SC2086
  53. docker-compose up -d ${servicesString}
  54. fi
  55. if [[ ${2} == "pull" || ${2} == "build" || ${2} == "ps" ]]; then
  56. # shellcheck disable=SC2086
  57. docker-compose "${2}" ${servicesString}
  58. fi
  59. else
  60. echo -e "${RED}${servicesString:2}\n${YELLOW}Usage: ${1} restart [backend, frontend, mongo, redis]${NC}"
  61. fi
  62. else
  63. echo -e "${RED}Error: Invalid dockerCommand input${NC}"
  64. fi
  65. }
  66. if [[ -x "$(command -v docker)" && -x "$(command -v docker-compose)" ]]; then
  67. case $1 in
  68. start)
  69. echo -e "${CYAN}Musare | Start Services${NC}"
  70. # shellcheck disable=SC2068
  71. dockerCommand "$(basename "$0")" start ${@:2}
  72. ;;
  73. stop)
  74. echo -e "${CYAN}Musare | Stop Services${NC}"
  75. # shellcheck disable=SC2068
  76. dockerCommand "$(basename "$0")" stop ${@:2}
  77. ;;
  78. restart)
  79. echo -e "${CYAN}Musare | Restart Services${NC}"
  80. # shellcheck disable=SC2068
  81. dockerCommand "$(basename "$0")" restart ${@:2}
  82. ;;
  83. build)
  84. echo -e "${CYAN}Musare | Build Services${NC}"
  85. # shellcheck disable=SC2068
  86. dockerCommand "$(basename "$0")" pull ${@:2}
  87. # shellcheck disable=SC2068
  88. dockerCommand "$(basename "$0")" build ${@:2}
  89. ;;
  90. status)
  91. echo -e "${CYAN}Musare | Service Status${NC}"
  92. # shellcheck disable=SC2068
  93. dockerCommand "$(basename "$0")" ps ${@:2}
  94. ;;
  95. reset)
  96. echo -e "${CYAN}Musare | Reset Services${NC}"
  97. if [[ -f .env ]]; then
  98. # shellcheck disable=SC1091
  99. source .env
  100. servicesString=$(handleServices "${@:2}")
  101. if [[ ${servicesString:0:1} == 1 && ${servicesString:2:4} == "all" ]]; then
  102. echo -e "${RED}Resetting will remove the ${REDIS_DATA_LOCATION} and ${MONGO_DATA_LOCATION} directories.${NC}"
  103. echo -e "${GREEN}Are you sure you want to reset all data? ${YELLOW}[y,n]: ${NC}"
  104. read -r confirm
  105. if [[ "${confirm}" == y* ]]; then
  106. docker-compose stop
  107. docker-compose rm -v --force
  108. if [[ -d $REDIS_DATA_LOCATION ]]; then
  109. rm -rf $REDIS_DATA_LOCATION
  110. fi
  111. if [[ -d $MONGO_DATA_LOCATION ]]; then
  112. rm -rf $MONGO_DATA_LOCATION
  113. fi
  114. else
  115. echo -e "${RED}Cancelled reset${NC}"
  116. fi
  117. elif [[ ${servicesString:0:1} == 1 ]]; then
  118. if [[ "${servicesString:2}" == *redis* && "${servicesString:2}" == *mongo* ]]; then
  119. echo -e "${RED}Resetting will remove the ${REDIS_DATA_LOCATION} and ${MONGO_DATA_LOCATION} directories.${NC}"
  120. elif [[ "${servicesString:2}" == *redis* ]]; then
  121. echo -e "${RED}Resetting will remove the ${REDIS_DATA_LOCATION} directory.${NC}"
  122. elif [[ "${servicesString:2}" == *mongo* ]]; then
  123. echo -e "${RED}Resetting will remove the ${MONGO_DATA_LOCATION} directory.${NC}"
  124. fi
  125. echo -e "${GREEN}Are you sure you want to reset all data for $(echo "${servicesString:2}" | tr ' ' ',')? ${YELLOW}[y,n]: ${NC}"
  126. read -r confirm
  127. if [[ "${confirm}" == y* ]]; then
  128. # shellcheck disable=SC2086
  129. docker-compose stop ${servicesString:2}
  130. # shellcheck disable=SC2086
  131. docker-compose rm -v --force ${servicesString:2}
  132. if [[ "${servicesString:2}" == *redis* && -d $REDIS_DATA_LOCATION ]]; then
  133. rm -rf $REDIS_DATA_LOCATION
  134. fi
  135. if [[ "${servicesString:2}" == *mongo* && -d $MONGO_DATA_LOCATION ]]; then
  136. rm -rf $MONGO_DATA_LOCATION
  137. fi
  138. else
  139. echo -e "${RED}Cancelled reset${NC}"
  140. fi
  141. else
  142. echo -e "${RED}${servicesString:2}\n${YELLOW}Usage: $(basename "$0") build [backend, frontend, mongo, redis]${NC}"
  143. fi
  144. else
  145. echo -e "${RED}Error: .env does not exist${NC}"
  146. fi
  147. ;;
  148. attach)
  149. echo -e "${CYAN}Musare | Attach${NC}"
  150. if [[ $2 == "backend" ]]; then
  151. containerId=$(docker-compose ps -q backend)
  152. if [[ -z $containerId ]]; then
  153. echo -e "${RED}Error: Backend offline, please start to attach.${NC}"
  154. else
  155. echo -e "${YELLOW}Detach with CTRL+P+Q${NC}"
  156. docker attach "$containerId"
  157. fi
  158. elif [[ $2 == "mongo" ]]; then
  159. if [[ -f .env ]]; then
  160. # shellcheck disable=SC1091
  161. source .env
  162. MONGO_VERSION_INT=${MONGO_VERSION:0:1}
  163. if [[ -z $(docker-compose ps -q mongo) ]]; then
  164. echo -e "${RED}Error: Mongo offline, please start to attach.${NC}"
  165. else
  166. echo -e "${YELLOW}Detach with CTRL+C${NC}"
  167. if [[ $MONGO_VERSION_INT -ge 5 ]]; then
  168. docker-compose exec mongo mongosh musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}" --eval "disableTelemetry()" --shell
  169. else
  170. docker-compose exec mongo mongo musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}"
  171. fi
  172. fi
  173. else
  174. echo -e "${RED}Error: .env does not exist${NC}"
  175. fi
  176. else
  177. echo -e "${RED}Invalid service $2\n${YELLOW}Usage: $(basename "$0") attach [backend,mongo]${NC}"
  178. fi
  179. ;;
  180. eslint)
  181. echo -e "${CYAN}Musare | ESLint${NC}"
  182. fix=""
  183. if [[ $2 == "fix" || $3 == "fix" || $2 == "--fix" || $3 == "--fix" ]]; then
  184. fix="--fix"
  185. echo -e "${GREEN}Auto-fix enabled${NC}"
  186. fi
  187. case $2 in
  188. frontend)
  189. docker-compose exec frontend npx eslint app/src --ext .js,.vue $fix
  190. ;;
  191. backend)
  192. docker-compose exec backend npx eslint app/logic $fix
  193. ;;
  194. ""|fix|--fix)
  195. docker-compose exec frontend npx eslint app/src --ext .js,.vue $fix
  196. docker-compose exec backend npx eslint app/logic $fix
  197. ;;
  198. *)
  199. echo -e "${RED}Invalid service $2\n${YELLOW}Usage: $(basename "$0") eslint [backend, frontend] [fix]${NC}"
  200. ;;
  201. esac
  202. ;;
  203. update)
  204. echo -e "${CYAN}Musare | Update${NC}"
  205. git fetch
  206. if [[ $(git rev-parse HEAD) == $(git rev-parse @\{u\}) ]]; then
  207. echo -e "${GREEN}Already up to date${NC}"
  208. else
  209. dbChange=$(git log --name-only --oneline HEAD..origin/"$(git rev-parse --abbrev-ref HEAD)" | grep "backend/logic/db/schemas")
  210. fcChange=$(git log --name-only --oneline HEAD..origin/"$(git rev-parse --abbrev-ref HEAD)" | grep "frontend/dist/config/template.json")
  211. bcChange=$(git log --name-only --oneline HEAD..origin/"$(git rev-parse --abbrev-ref HEAD)" | grep "backend/config/template.json")
  212. if [[ ( $2 == "auto" && -z $dbChange && -z $fcChange && -z $bcChange ) || -z $2 ]]; then
  213. echo -e "${CYAN}Updating...${NC}"
  214. git pull
  215. docker-compose build
  216. docker-compose stop
  217. docker-compose up -d
  218. echo -e "${GREEN}Updated!${NC}"
  219. if [[ -n $dbChange ]]; then
  220. echo -e "${RED}Database schema has changed, please run migration!${NC}"
  221. fi
  222. if [[ -n $fcChange ]]; then
  223. echo -e "${RED}Frontend config has changed, please update!${NC}"
  224. fi
  225. if [[ -n $bcChange ]]; then
  226. echo -e "${RED}Backend config has changed, please update!${NC}"
  227. fi
  228. elif [[ $2 == "auto" ]]; then
  229. echo -e "${RED}Auto Update Failed! Database and/or config has changed!${NC}"
  230. fi
  231. fi
  232. ;;
  233. logs)
  234. echo -e "${CYAN}Musare | Logs${NC}"
  235. docker-compose logs "${@:2}"
  236. ;;
  237. backup)
  238. echo -e "${CYAN}Musare | Backup${NC}"
  239. if [[ -f .env ]]; then
  240. # shellcheck disable=SC1091
  241. source .env
  242. if [[ -z "${BACKUP_LOCATION}" ]]; then
  243. backupLocation="${scriptLocation%x}/backups"
  244. else
  245. backupLocation="${BACKUP_LOCATION%/}"
  246. fi
  247. if [[ ! -d "${backupLocation}" ]]; then
  248. echo -e "${YELLOW}Creating backup directory at ${backupLocation}${NC}"
  249. mkdir "${backupLocation}"
  250. fi
  251. if [[ -z "${BACKUP_NAME}" ]]; then
  252. backupLocation="${backupLocation}/musare-$(date +"%Y-%m-%d-%s").dump"
  253. else
  254. backupLocation="${backupLocation}/${BACKUP_NAME}"
  255. fi
  256. echo -e "${YELLOW}Creating backup at ${backupLocation}${NC}"
  257. docker-compose exec -T mongo sh -c "mongodump --authenticationDatabase musare -u ${MONGO_USER_USERNAME} -p ${MONGO_USER_PASSWORD} -d musare --archive" > "${backupLocation}"
  258. else
  259. echo -e "${RED}Error: .env does not exist${NC}"
  260. fi
  261. ;;
  262. restore)
  263. echo -e "${CYAN}Musare | Restore${NC}"
  264. if [[ -f .env ]]; then
  265. # shellcheck disable=SC1091
  266. source .env
  267. if [[ -z $2 ]]; then
  268. echo -e "${GREEN}Please enter the full path of the dump you wish to restore: ${NC}"
  269. read -r restoreFile
  270. else
  271. restoreFile=$2
  272. fi
  273. if [[ -z ${restoreFile} ]]; then
  274. echo -e "${RED}Error: no restore path given, cancelled restoration.${NC}"
  275. elif [[ -d ${restoreFile} ]]; then
  276. echo -e "${RED}Error: restore path given is a directory, cancelled restoration.${NC}"
  277. elif [[ ! -f ${restoreFile} ]]; then
  278. echo -e "${RED}Error: no file at restore path given, cancelled restoration.${NC}"
  279. else
  280. docker-compose exec -T mongo sh -c "mongorestore --authenticationDatabase musare -u ${MONGO_USER_USERNAME} -p ${MONGO_USER_PASSWORD} --archive" < "${restoreFile}"
  281. fi
  282. else
  283. echo -e "${RED}Error: .env does not exist${NC}"
  284. fi
  285. ;;
  286. admin)
  287. echo -e "${CYAN}Musare | Add Admin${NC}"
  288. if [[ -f .env ]]; then
  289. # shellcheck disable=SC1091
  290. source .env
  291. MONGO_VERSION_INT=${MONGO_VERSION:0:1}
  292. if [[ $2 == "add" ]]; then
  293. if [[ -z $3 ]]; then
  294. echo -e "${GREEN}Please enter the username of the user you wish to make an admin: ${NC}"
  295. read -r adminUser
  296. else
  297. adminUser=$3
  298. fi
  299. if [[ -z $adminUser ]]; then
  300. echo -e "${RED}Error: Username for new admin not provided.${NC}"
  301. else
  302. if [[ $MONGO_VERSION_INT -ge 5 ]]; then
  303. docker-compose exec mongo mongosh musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}" --eval "disableTelemetry(); db.users.updateOne({username: '${adminUser}'}, {\$set: {role: 'admin'}})"
  304. else
  305. docker-compose exec mongo mongo musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}" --eval "db.users.updateOne({username: '${adminUser}'}, {\$set: {role: 'admin'}})"
  306. fi
  307. fi
  308. elif [[ $2 == "remove" ]]; then
  309. if [[ -z $3 ]]; then
  310. echo -e "${GREEN}Please enter the username of the user you wish to remove as admin: ${NC}"
  311. read -r adminUser
  312. else
  313. adminUser=$3
  314. fi
  315. if [[ -z $adminUser ]]; then
  316. echo -e "${RED}Error: Username for new admin not provided.${NC}"
  317. else
  318. if [[ $MONGO_VERSION_INT -ge 5 ]]; then
  319. docker-compose exec mongo mongosh musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}" --eval "disableTelemetry(); db.users.updateOne({username: '${adminUser}'}, {\$set: {role: 'default'}})"
  320. else
  321. docker-compose exec mongo mongo musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}" --eval "db.users.updateOne({username: '${adminUser}'}, {\$set: {role: 'default'}})"
  322. fi
  323. fi
  324. else
  325. echo -e "${RED}Invalid command $2\n${YELLOW}Usage: $(basename "$0") admin [add,remove] username${NC}"
  326. fi
  327. else
  328. echo -e "${RED}Error: .env does not exist${NC}"
  329. fi
  330. ;;
  331. "")
  332. echo -e "${CYAN}Musare | Available Commands${NC}"
  333. echo -e "${YELLOW}start - Start services${NC}"
  334. echo -e "${YELLOW}stop - Stop services${NC}"
  335. echo -e "${YELLOW}restart - Restart services${NC}"
  336. echo -e "${YELLOW}status - Service status${NC}"
  337. echo -e "${YELLOW}logs - View logs for services${NC}"
  338. echo -e "${YELLOW}update - Update Musare${NC}"
  339. echo -e "${YELLOW}attach [backend,mongo] - Attach to backend service or mongo shell${NC}"
  340. echo -e "${YELLOW}build - Build services${NC}"
  341. echo -e "${YELLOW}eslint - Run eslint on frontend and/or backend${NC}"
  342. echo -e "${YELLOW}backup - Backup database data to file${NC}"
  343. echo -e "${YELLOW}restore - Restore database data from backup file${NC}"
  344. echo -e "${YELLOW}reset - Reset service data${NC}"
  345. echo -e "${YELLOW}admin [add,remove] - Assign/unassign admin role to/from a user${NC}"
  346. ;;
  347. *)
  348. echo -e "${CYAN}Musare${NC}"
  349. echo -e "${RED}Error: Invalid Command $1${NC}"
  350. echo -e "${CYAN}Available Commands:${NC}"
  351. echo -e "${YELLOW}start - Start services${NC}"
  352. echo -e "${YELLOW}stop - Stop services${NC}"
  353. echo -e "${YELLOW}restart - Restart services${NC}"
  354. echo -e "${YELLOW}status - Service status${NC}"
  355. echo -e "${YELLOW}logs - View logs for services${NC}"
  356. echo -e "${YELLOW}update - Update Musare${NC}"
  357. echo -e "${YELLOW}attach [backend,mongo] - Attach to backend service or mongo shell${NC}"
  358. echo -e "${YELLOW}build - Build services${NC}"
  359. echo -e "${YELLOW}eslint - Run eslint on frontend and/or backend${NC}"
  360. echo -e "${YELLOW}backup - Backup database data to file${NC}"
  361. echo -e "${YELLOW}restore - Restore database data from backup file${NC}"
  362. echo -e "${YELLOW}reset - Reset service data${NC}"
  363. echo -e "${YELLOW}admin [add,remove] - Assign/unassign admin role to/from a user${NC}"
  364. ;;
  365. esac
  366. elif [[ -x "$(command -v docker)" && ! -x "$(command -v docker-compose)" ]]; then
  367. echo -e "${RED}Error: docker-compose not installed.${NC}"
  368. elif [[ ! -x "$(command -v docker)" && -x "$(command -v docker-compose)" ]]; then
  369. echo -e "${RED}Error: docker not installed.${NC}"
  370. else
  371. echo -e "${RED}Error: docker and docker-compose not installed.${NC}"
  372. fi