musare.sh 18 KB

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