musare.sh 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. lint|eslint)
  228. echo -e "${CYAN}Musare | Lint${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. docs)
  244. ${docker} run -v "${scriptLocation}":/workdir ghcr.io/igorshubovych/markdownlint-cli:latest ".wiki" "*.md" $fix
  245. exitValue=$?
  246. ;;
  247. ""|fix|--fix)
  248. ${dockerCompose} exec -T frontend npx eslint src --ext .js,.vue $fix
  249. frontendExitValue=$?
  250. ${dockerCompose} exec -T backend npx eslint logic $fix
  251. backendExitValue=$?
  252. ${docker} run -v "${scriptLocation}":/workdir ghcr.io/igorshubovych/markdownlint-cli:latest ".wiki" "*.md" $fix
  253. docsExitValue=$?
  254. if [[ ${frontendExitValue} -gt 0 || ${backendExitValue} -gt 0 || ${docsExitValue} -gt 0 ]]; then
  255. exitValue=1
  256. else
  257. exitValue=0
  258. fi
  259. ;;
  260. *)
  261. echo -e "${RED}Invalid service $2\n${YELLOW}Usage: $(basename "$0") lint [backend, frontend, docs] [fix]${NC}"
  262. exitValue=1
  263. ;;
  264. esac
  265. if [[ ${exitValue} -gt 0 ]]; then
  266. exit ${exitValue}
  267. fi
  268. ;;
  269. update)
  270. echo -e "${CYAN}Musare | Update${NC}"
  271. git fetch
  272. exitValue=$?
  273. if [[ ${exitValue} -gt 0 ]]; then
  274. exit ${exitValue}
  275. fi
  276. if [[ $(git rev-parse HEAD) == $(git rev-parse @\{u\}) ]]; then
  277. echo -e "${GREEN}Already up to date${NC}"
  278. else
  279. dbChange=$(git log --name-only --oneline HEAD..origin/"$(git rev-parse --abbrev-ref HEAD)" | grep "backend/logic/db/schemas")
  280. fcChange=$(git log --name-only --oneline HEAD..origin/"$(git rev-parse --abbrev-ref HEAD)" | grep "frontend/dist/config/template.json")
  281. bcChange=$(git log --name-only --oneline HEAD..origin/"$(git rev-parse --abbrev-ref HEAD)" | grep "backend/config/template.json")
  282. if [[ ( $2 == "auto" && -z $dbChange && -z $fcChange && -z $bcChange ) || -z $2 ]]; then
  283. echo -e "${CYAN}Updating...${NC}"
  284. git pull
  285. exitValue=$?
  286. if [[ ${exitValue} -gt 0 ]]; then
  287. exit ${exitValue}
  288. fi
  289. runDockerCommand "$(basename "$0")" build
  290. runDockerCommand "$(basename "$0")" restart
  291. echo -e "${GREEN}Updated!${NC}"
  292. if [[ -n $dbChange ]]; then
  293. echo -e "${RED}Database schema has changed, please run migration!${NC}"
  294. fi
  295. if [[ -n $fcChange ]]; then
  296. echo -e "${RED}Frontend config has changed, please update!${NC}"
  297. fi
  298. if [[ -n $bcChange ]]; then
  299. echo -e "${RED}Backend config has changed, please update!${NC}"
  300. fi
  301. elif [[ $2 == "auto" ]]; then
  302. echo -e "${RED}Auto Update Failed! Database and/or config has changed!${NC}"
  303. exit 1
  304. fi
  305. fi
  306. ;;
  307. logs)
  308. echo -e "${CYAN}Musare | Logs${NC}"
  309. # shellcheck disable=SC2068
  310. runDockerCommand "$(basename "$0")" logs ${@:2}
  311. ;;
  312. backup)
  313. echo -e "${CYAN}Musare | Backup${NC}"
  314. if [[ -z "${BACKUP_LOCATION}" ]]; then
  315. backupLocation="${scriptLocation%x}/backups"
  316. else
  317. backupLocation="${BACKUP_LOCATION%/}"
  318. fi
  319. if [[ ! -d "${backupLocation}" ]]; then
  320. echo -e "${YELLOW}Creating backup directory at ${backupLocation}${NC}"
  321. mkdir "${backupLocation}"
  322. fi
  323. if [[ -z "${BACKUP_NAME}" ]]; then
  324. backupLocation="${backupLocation}/musare-$(date +"%Y-%m-%d-%s").dump"
  325. else
  326. backupLocation="${backupLocation}/${BACKUP_NAME}"
  327. fi
  328. echo -e "${YELLOW}Creating backup at ${backupLocation}${NC}"
  329. ${dockerCompose} exec -T mongo sh -c "mongodump --authenticationDatabase musare -u ${MONGO_USER_USERNAME} -p ${MONGO_USER_PASSWORD} -d musare --archive" > "${backupLocation}"
  330. ;;
  331. restore)
  332. echo -e "${CYAN}Musare | Restore${NC}"
  333. if [[ -z $2 ]]; then
  334. echo -e "${GREEN}Please enter the full path of the dump you wish to restore: ${NC}"
  335. read -r restoreFile
  336. else
  337. restoreFile=$2
  338. fi
  339. if [[ -z ${restoreFile} ]]; then
  340. echo -e "${RED}Error: no restore path given, cancelled restoration.${NC}"
  341. exit 1
  342. elif [[ -d ${restoreFile} ]]; then
  343. echo -e "${RED}Error: restore path given is a directory, cancelled restoration.${NC}"
  344. exit 1
  345. elif [[ ! -f ${restoreFile} ]]; then
  346. echo -e "${RED}Error: no file at restore path given, cancelled restoration.${NC}"
  347. exit 1
  348. else
  349. ${dockerCompose} exec -T mongo sh -c "mongorestore --authenticationDatabase musare -u ${MONGO_USER_USERNAME} -p ${MONGO_USER_PASSWORD} --archive" < "${restoreFile}"
  350. fi
  351. ;;
  352. admin)
  353. echo -e "${CYAN}Musare | Add Admin${NC}"
  354. MONGO_VERSION_INT=${MONGO_VERSION:0:1}
  355. if [[ $2 == "add" ]]; then
  356. if [[ -z $3 ]]; then
  357. echo -e "${GREEN}Please enter the username of the user you wish to make an admin: ${NC}"
  358. read -r adminUser
  359. else
  360. adminUser=$3
  361. fi
  362. if [[ -z $adminUser ]]; then
  363. echo -e "${RED}Error: Username for new admin not provided.${NC}"
  364. exit 1
  365. else
  366. if [[ $MONGO_VERSION_INT -ge 5 ]]; then
  367. ${dockerCompose} exec mongo mongosh musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}" --eval "disableTelemetry(); db.users.updateOne({username: '${adminUser}'}, {\$set: {role: 'admin'}})"
  368. else
  369. ${dockerCompose} exec mongo mongo musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}" --eval "db.users.updateOne({username: '${adminUser}'}, {\$set: {role: 'admin'}})"
  370. fi
  371. fi
  372. elif [[ $2 == "remove" ]]; then
  373. if [[ -z $3 ]]; then
  374. echo -e "${GREEN}Please enter the username of the user you wish to remove as admin: ${NC}"
  375. read -r adminUser
  376. else
  377. adminUser=$3
  378. fi
  379. if [[ -z $adminUser ]]; then
  380. echo -e "${RED}Error: Username for new admin not provided.${NC}"
  381. exit 1
  382. else
  383. if [[ $MONGO_VERSION_INT -ge 5 ]]; then
  384. ${dockerCompose} exec mongo mongosh musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}" --eval "disableTelemetry(); db.users.updateOne({username: '${adminUser}'}, {\$set: {role: 'default'}})"
  385. else
  386. ${dockerCompose} exec mongo mongo musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}" --eval "db.users.updateOne({username: '${adminUser}'}, {\$set: {role: 'default'}})"
  387. fi
  388. fi
  389. else
  390. echo -e "${RED}Invalid command $2\n${YELLOW}Usage: $(basename "$0") admin [add,remove] username${NC}"
  391. exit 1
  392. fi
  393. ;;
  394. "")
  395. echo -e "${CYAN}Musare | Available Commands${NC}"
  396. echo -e "${YELLOW}start - Start services${NC}"
  397. echo -e "${YELLOW}stop - Stop services${NC}"
  398. echo -e "${YELLOW}restart - Restart services${NC}"
  399. echo -e "${YELLOW}status - Service status${NC}"
  400. echo -e "${YELLOW}logs - View logs for services${NC}"
  401. echo -e "${YELLOW}update - Update Musare${NC}"
  402. echo -e "${YELLOW}attach [backend,mongo,redis] - Attach to backend service, mongo or redis shell${NC}"
  403. echo -e "${YELLOW}build - Build services${NC}"
  404. echo -e "${YELLOW}lint - Run lint on frontend, backend and/or docs${NC}"
  405. echo -e "${YELLOW}backup - Backup database data to file${NC}"
  406. echo -e "${YELLOW}restore - Restore database data from backup file${NC}"
  407. echo -e "${YELLOW}reset - Reset service data${NC}"
  408. echo -e "${YELLOW}admin [add,remove] - Assign/unassign admin role to/from a user${NC}"
  409. ;;
  410. *)
  411. echo -e "${CYAN}Musare${NC}"
  412. echo -e "${RED}Error: Invalid Command $1${NC}"
  413. echo -e "${CYAN}Available Commands:${NC}"
  414. echo -e "${YELLOW}start - Start services${NC}"
  415. echo -e "${YELLOW}stop - Stop services${NC}"
  416. echo -e "${YELLOW}restart - Restart services${NC}"
  417. echo -e "${YELLOW}status - Service status${NC}"
  418. echo -e "${YELLOW}logs - View logs for services${NC}"
  419. echo -e "${YELLOW}update - Update Musare${NC}"
  420. echo -e "${YELLOW}attach [backend,mongo,redis] - Attach to backend service, mongo or redis shell${NC}"
  421. echo -e "${YELLOW}build - Build services${NC}"
  422. echo -e "${YELLOW}lint - Run lint on frontend, backend and/or docs${NC}"
  423. echo -e "${YELLOW}backup - Backup database data to file${NC}"
  424. echo -e "${YELLOW}restore - Restore database data from backup file${NC}"
  425. echo -e "${YELLOW}reset - Reset service data${NC}"
  426. echo -e "${YELLOW}admin [add,remove] - Assign/unassign admin role to/from a user${NC}"
  427. exit 1
  428. ;;
  429. esac