musare.sh 18 KB

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