musare.sh 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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. ${docker} --version > /dev/null 2>&1
  25. dockerInstalled=$?
  26. dockerCompose="${docker} compose"
  27. ${dockerCompose} version > /dev/null 2>&1
  28. composeInstalled=$?
  29. if [[ ${composeInstalled} -gt 0 ]]; then
  30. dockerCompose="${docker}-compose"
  31. ${dockerCompose} --version > /dev/null 2>&1
  32. composeInstalled=$?
  33. fi
  34. if [[ ${dockerInstalled} -gt 0 || ${composeInstalled} -gt 0 ]]; then
  35. if [[ ${dockerInstalled} -eq 0 && ${composeInstalled} -gt 0 ]]; then
  36. echo -e "${RED}Error: ${dockerCompose} not installed.${NC}"
  37. elif [[ ${dockerInstalled} -gt 0 && ${composeInstalled} -eq 0 ]]; then
  38. echo -e "${RED}Error: ${docker} not installed.${NC}"
  39. else
  40. echo -e "${RED}Error: ${docker} and ${dockerCompose} not installed.${NC}"
  41. fi
  42. exit 1
  43. fi
  44. composeFiles="-f docker-compose.yml"
  45. if [[ ${APP_ENV} == "development" ]]; then
  46. composeFiles="${composeFiles} -f docker-compose.dev.yml"
  47. fi
  48. if [[ -f docker-compose.override.yml ]]; then
  49. composeFiles="${composeFiles} -f docker-compose.override.yml"
  50. fi
  51. dockerCompose="${dockerCompose} ${composeFiles}"
  52. handleServices()
  53. {
  54. validServices=($1)
  55. servicesArray=()
  56. invalidServices=false
  57. for x in "${@:2}"; do
  58. if [[ ${validServices[*]} =~ (^|[[:space:]])"$x"($|[[:space:]]) ]]; then
  59. if ! [[ ${servicesArray[*]} =~ (^|[[:space:]])"$x"($|[[:space:]]) ]]; then
  60. servicesArray+=("${x}")
  61. fi
  62. else
  63. if [[ $invalidServices == false ]]; then
  64. invalidServices="${x}"
  65. else
  66. invalidServices="${invalidServices} ${x}"
  67. fi
  68. fi
  69. done
  70. if [[ $invalidServices == false && ${#servicesArray[@]} -gt 0 ]]; then
  71. echo "1|${servicesArray[*]}"
  72. elif [[ $invalidServices == false ]]; then
  73. echo "1|all"
  74. else
  75. echo "0|Invalid Service(s): ${invalidServices}"
  76. fi
  77. }
  78. runDockerCommand()
  79. {
  80. validCommands=(start stop restart pull build ps logs)
  81. if [[ ${validCommands[*]} =~ (^|[[:space:]])"$2"($|[[:space:]]) ]]; then
  82. servicesString=$(handleServices "backend frontend mongo redis" "${@:3}")
  83. if [[ ${servicesString:0:1} == 1 ]]; then
  84. if [[ ${servicesString:2:4} == "all" ]]; then
  85. servicesString=""
  86. pullServices="mongo redis"
  87. buildServices="backend frontend"
  88. else
  89. servicesString=${servicesString:2}
  90. pullArray=()
  91. buildArray=()
  92. if [[ "${servicesString}" == *mongo* ]]; then
  93. pullArray+=("mongo")
  94. fi
  95. if [[ "${servicesString}" == *redis* ]]; then
  96. pullArray+=("redis")
  97. fi
  98. if [[ "${servicesString}" == *backend* ]]; then
  99. buildArray+=("backend")
  100. fi
  101. if [[ "${servicesString}" == *frontend* ]]; then
  102. buildArray+=("frontend")
  103. fi
  104. pullServices="${pullArray[*]}"
  105. buildServices="${buildArray[*]}"
  106. fi
  107. if [[ ${2} == "stop" || ${2} == "restart" ]]; then
  108. # shellcheck disable=SC2086
  109. ${dockerCompose} stop ${servicesString}
  110. fi
  111. if [[ ${2} == "start" || ${2} == "restart" ]]; then
  112. # shellcheck disable=SC2086
  113. ${dockerCompose} up -d ${servicesString}
  114. fi
  115. if [[ ${2} == "pull" && ${pullServices} != "" ]]; then
  116. # shellcheck disable=SC2086
  117. ${dockerCompose} "${2}" ${pullServices}
  118. fi
  119. if [[ ${2} == "build" && ${buildServices} != "" ]]; then
  120. # shellcheck disable=SC2086
  121. ${dockerCompose} "${2}" ${buildServices}
  122. fi
  123. if [[ ${2} == "ps" || ${2} == "logs" ]]; then
  124. # shellcheck disable=SC2086
  125. ${dockerCompose} "${2}" ${servicesString}
  126. fi
  127. exitValue=$?
  128. if [[ ${exitValue} -gt 0 ]]; then
  129. exit ${exitValue}
  130. fi
  131. else
  132. echo -e "${RED}${servicesString:2}\n${YELLOW}Usage: ${1} [backend, frontend, mongo, redis]${NC}"
  133. exit 1
  134. fi
  135. else
  136. echo -e "${RED}Error: Invalid runDockerCommand input${NC}"
  137. exit 1
  138. fi
  139. }
  140. getContainerId()
  141. {
  142. if [[ ${DOCKER_COMMAND} == "docker" ]]; then
  143. containerId=$(${dockerCompose} ps -q "${1}")
  144. else
  145. containerId=$(${dockerCompose} ps | sed '0,/CONTAINER/d' | awk "/${1}/ {print \$1;exit}")
  146. fi
  147. echo "${containerId}"
  148. }
  149. case $1 in
  150. start)
  151. echo -e "${CYAN}Musare | Start Services${NC}"
  152. # shellcheck disable=SC2068
  153. runDockerCommand "$(basename "$0") $1" start ${@:2}
  154. ;;
  155. stop)
  156. echo -e "${CYAN}Musare | Stop Services${NC}"
  157. # shellcheck disable=SC2068
  158. runDockerCommand "$(basename "$0") $1" stop ${@:2}
  159. ;;
  160. restart)
  161. echo -e "${CYAN}Musare | Restart Services${NC}"
  162. # shellcheck disable=SC2068
  163. runDockerCommand "$(basename "$0") $1" restart ${@:2}
  164. ;;
  165. build)
  166. echo -e "${CYAN}Musare | Build Services${NC}"
  167. # shellcheck disable=SC2068
  168. runDockerCommand "$(basename "$0") $1" pull ${@:2}
  169. # shellcheck disable=SC2068
  170. runDockerCommand "$(basename "$0") $1" build ${@:2}
  171. ;;
  172. status)
  173. echo -e "${CYAN}Musare | Service Status${NC}"
  174. # shellcheck disable=SC2068
  175. runDockerCommand "$(basename "$0") $1" ps ${@:2}
  176. ;;
  177. reset)
  178. echo -e "${CYAN}Musare | Reset Services${NC}"
  179. servicesString=$(handleServices "backend frontend mongo redis" "${@:2}")
  180. if [[ ${servicesString:0:1} == 1 && ${servicesString:2:4} == "all" ]]; then
  181. echo -e "${RED}Resetting will remove the ${REDIS_DATA_LOCATION} and ${MONGO_DATA_LOCATION} directories.${NC}"
  182. echo -e "${GREEN}Are you sure you want to reset all data? ${YELLOW}[y,n]: ${NC}"
  183. read -r confirm
  184. if [[ "${confirm}" == y* ]]; then
  185. runDockerCommand "$(basename "$0") $1" stop
  186. ${dockerCompose} rm -v --force
  187. if [[ -d $REDIS_DATA_LOCATION ]]; then
  188. rm -rf "${REDIS_DATA_LOCATION}"
  189. fi
  190. if [[ -d $MONGO_DATA_LOCATION ]]; then
  191. rm -rf "${MONGO_DATA_LOCATION}"
  192. fi
  193. else
  194. echo -e "${RED}Cancelled reset${NC}"
  195. fi
  196. elif [[ ${servicesString:0:1} == 1 ]]; then
  197. if [[ "${servicesString:2}" == *redis* && "${servicesString:2}" == *mongo* ]]; then
  198. echo -e "${RED}Resetting will remove the ${REDIS_DATA_LOCATION} and ${MONGO_DATA_LOCATION} directories.${NC}"
  199. elif [[ "${servicesString:2}" == *redis* ]]; then
  200. echo -e "${RED}Resetting will remove the ${REDIS_DATA_LOCATION} directory.${NC}"
  201. elif [[ "${servicesString:2}" == *mongo* ]]; then
  202. echo -e "${RED}Resetting will remove the ${MONGO_DATA_LOCATION} directory.${NC}"
  203. fi
  204. echo -e "${GREEN}Are you sure you want to reset all data for $(echo "${servicesString:2}" | tr ' ' ',')? ${YELLOW}[y,n]: ${NC}"
  205. read -r confirm
  206. if [[ "${confirm}" == y* ]]; then
  207. # shellcheck disable=SC2086
  208. runDockerCommand "$(basename "$0") $1" stop ${servicesString:2}
  209. # shellcheck disable=SC2086
  210. ${dockerCompose} rm -v --force ${servicesString}
  211. if [[ "${servicesString:2}" == *redis* && -d $REDIS_DATA_LOCATION ]]; then
  212. rm -rf "${REDIS_DATA_LOCATION}"
  213. fi
  214. if [[ "${servicesString:2}" == *mongo* && -d $MONGO_DATA_LOCATION ]]; then
  215. rm -rf "${MONGO_DATA_LOCATION}"
  216. fi
  217. else
  218. echo -e "${RED}Cancelled reset${NC}"
  219. fi
  220. else
  221. echo -e "${RED}${servicesString:2}\n${YELLOW}Usage: $(basename "$0") build [backend, frontend, mongo, redis]${NC}"
  222. exit 1
  223. fi
  224. ;;
  225. attach)
  226. echo -e "${CYAN}Musare | Attach${NC}"
  227. if [[ $2 == "backend" ]]; then
  228. containerId=$(getContainerId backend)
  229. if [[ -z $containerId ]]; then
  230. echo -e "${RED}Error: Backend offline, please start to attach.${NC}"
  231. exit 1
  232. else
  233. echo -e "${YELLOW}Detach with CTRL+P+Q${NC}"
  234. ${docker} attach "$containerId"
  235. fi
  236. elif [[ $2 == "mongo" ]]; then
  237. MONGO_VERSION_INT=${MONGO_VERSION:0:1}
  238. if [[ -z $(getContainerId mongo) ]]; then
  239. echo -e "${RED}Error: Mongo offline, please start to attach.${NC}"
  240. exit 1
  241. else
  242. echo -e "${YELLOW}Detach with CTRL+D${NC}"
  243. if [[ $MONGO_VERSION_INT -ge 5 ]]; then
  244. ${dockerCompose} exec mongo mongosh musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}" --eval "disableTelemetry()" --shell
  245. else
  246. ${dockerCompose} exec mongo mongo musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}"
  247. fi
  248. fi
  249. elif [[ $2 == "redis" ]]; then
  250. if [[ -z $(getContainerId redis) ]]; then
  251. echo -e "${RED}Error: Redis offline, please start to attach.${NC}"
  252. exit 1
  253. else
  254. echo -e "${YELLOW}Detach with CTRL+C${NC}"
  255. ${dockerCompose} exec redis redis-cli -a "${REDIS_PASSWORD}"
  256. fi
  257. else
  258. echo -e "${RED}Invalid service $2\n${YELLOW}Usage: $(basename "$0") attach [backend,mongo,redis]${NC}"
  259. exit 1
  260. fi
  261. ;;
  262. lint|eslint)
  263. echo -e "${CYAN}Musare | Lint${NC}"
  264. services=$(sed "s/\(\ \)\{0,1\}\(-\)\{0,2\}fix//g;t;q1" <<< "${@:2}")
  265. fixFound=$?
  266. if [[ $fixFound -eq 0 ]]; then
  267. fix="--fix"
  268. echo -e "${GREEN}Auto-fix enabled${NC}"
  269. fi
  270. services=$(sed "s/\(\ \)\{0,1\}\(-\)\{0,2\}no-cache//g;t;q1" <<< "${services}")
  271. noCacheFound=$?
  272. cache="--cache"
  273. if [[ $noCacheFound -eq 0 ]]; then
  274. cache=""
  275. echo -e "${YELLOW}ESlint cache disabled${NC}"
  276. fi
  277. # shellcheck disable=SC2068
  278. servicesString=$(handleServices "backend frontend docs" ${services[@]})
  279. if [[ ${servicesString:0:1} == 1 ]]; then
  280. if [[ ${servicesString:2:4} == "all" || "${servicesString:2}" == *frontend* ]]; then
  281. echo -e "${CYAN}Running frontend lint...${NC}"
  282. ${dockerCompose} exec -T frontend npm run lint -- $cache $fix
  283. frontendExitValue=$?
  284. fi
  285. if [[ ${servicesString:2:4} == "all" || "${servicesString:2}" == *backend* ]]; then
  286. echo -e "${CYAN}Running backend lint...${NC}"
  287. ${dockerCompose} exec -T backend npm run lint -- $cache $fix
  288. backendExitValue=$?
  289. fi
  290. if [[ ${servicesString:2:4} == "all" || "${servicesString:2}" == *docs* ]]; then
  291. echo -e "${CYAN}Running docs lint...${NC}"
  292. ${docker} run --rm -v "${scriptLocation}":/workdir ghcr.io/igorshubovych/markdownlint-cli:latest ".wiki" "*.md" $fix
  293. docsExitValue=$?
  294. fi
  295. if [[ ${frontendExitValue} -gt 0 || ${backendExitValue} -gt 0 || ${docsExitValue} -gt 0 ]]; then
  296. exitValue=1
  297. else
  298. exitValue=0
  299. fi
  300. else
  301. echo -e "${RED}${servicesString:2}\n${YELLOW}Usage: $(basename "$0") lint [backend, frontend, docs] [fix]${NC}"
  302. exitValue=1
  303. fi
  304. if [[ ${exitValue} -gt 0 ]]; then
  305. exit ${exitValue}
  306. fi
  307. ;;
  308. typescript|ts)
  309. echo -e "${CYAN}Musare | TypeScript Check${NC}"
  310. services=$(sed "s/\(\ \)\{0,1\}\(-\)\{0,2\}strict//g;t;q1" <<< "${@:2}")
  311. strictFound=$?
  312. if [[ $strictFound -eq 0 ]]; then
  313. strict="--strict"
  314. echo -e "${GREEN}Strict mode enabled${NC}"
  315. fi
  316. # shellcheck disable=SC2068
  317. servicesString=$(handleServices "backend frontend" ${services[@]})
  318. if [[ ${servicesString:0:1} == 1 ]]; then
  319. if [[ ${servicesString:2:4} == "all" || "${servicesString:2}" == *frontend* ]]; then
  320. echo -e "${CYAN}Running frontend typescript check...${NC}"
  321. ${dockerCompose} exec -T frontend npm run typescript -- $strict
  322. frontendExitValue=$?
  323. fi
  324. if [[ ${servicesString:2:4} == "all" || "${servicesString:2}" == *backend* ]]; then
  325. echo -e "${CYAN}Running backend typescript check...${NC}"
  326. ${dockerCompose} exec -T backend npm run typescript -- $strict
  327. backendExitValue=$?
  328. fi
  329. if [[ ${frontendExitValue} -gt 0 || ${backendExitValue} -gt 0 ]]; then
  330. exitValue=1
  331. else
  332. exitValue=0
  333. fi
  334. else
  335. echo -e "${RED}${servicesString:2}\n${YELLOW}Usage: $(basename "$0") typescript [backend, frontend] [strict]${NC}"
  336. exitValue=1
  337. fi
  338. if [[ ${exitValue} -gt 0 ]]; then
  339. exit ${exitValue}
  340. fi
  341. ;;
  342. test)
  343. echo -e "${CYAN}Musare | Test${NC}"
  344. servicesString=$(handleServices "backend frontend" "${@:2}")
  345. if [[ ${servicesString:0:1} == 1 ]]; then
  346. if [[ ${servicesString:2:4} == "all" || "${servicesString:2}" == *backend* ]]; then
  347. echo -e "${CYAN}Running backend tests...${NC}"
  348. ${dockerCompose} exec -T backend npm run test
  349. backendExitValue=$?
  350. fi
  351. if [[ ${servicesString:2:4} == "all" || "${servicesString:2}" == *frontend* ]]; then
  352. echo -e "${CYAN}Running frontend tests...${NC}"
  353. ${dockerCompose} exec -T frontend npm run test -- --run
  354. frontendExitValue=$?
  355. fi
  356. if [[ ${backendExitValue} -gt 0 || ${frontendExitValue} -gt 0 ]]; then
  357. exitValue=1
  358. else
  359. exitValue=0
  360. fi
  361. else
  362. echo -e "${RED}${servicesString:2}\n${YELLOW}Usage: $(basename "$0") test [backend, frontend]${NC}"
  363. exitValue=1
  364. fi
  365. if [[ ${exitValue} -gt 0 ]]; then
  366. exit ${exitValue}
  367. fi
  368. ;;
  369. test:coverage)
  370. echo -e "${CYAN}Musare | Test Coverage${NC}"
  371. servicesString=$(handleServices "frontend" "${@:2}")
  372. if [[ ${servicesString:0:1} == 1 ]]; then
  373. if [[ ${servicesString:2:4} == "all" || "${servicesString:2}" == *frontend* ]]; then
  374. echo -e "${CYAN}Running frontend test coverage report...${NC}"
  375. ${dockerCompose} exec -T frontend npm run coverage
  376. frontendExitValue=$?
  377. fi
  378. if [[ ${frontendExitValue} -gt 0 ]]; then
  379. exitValue=1
  380. else
  381. exitValue=0
  382. fi
  383. else
  384. echo -e "${RED}${servicesString:2}\n${YELLOW}Usage: $(basename "$0") test:coverage [frontend]${NC}"
  385. exitValue=1
  386. fi
  387. if [[ ${exitValue} -gt 0 ]]; then
  388. exit ${exitValue}
  389. fi
  390. ;;
  391. update)
  392. echo -e "${CYAN}Musare | Update${NC}"
  393. musareshModified=$(git diff HEAD -- musare.sh)
  394. git fetch
  395. exitValue=$?
  396. if [[ ${exitValue} -gt 0 ]]; then
  397. exit ${exitValue}
  398. fi
  399. updated=$(git log --name-only --oneline HEAD..@\{u\})
  400. if [[ ${updated} == "" ]]; then
  401. echo -e "${GREEN}Already up to date${NC}"
  402. exit ${exitValue}
  403. fi
  404. breakingConfigChange=$(git rev-list "$(git rev-parse HEAD)" | grep d8b73be1de231821db34c677110b7b97e413451f)
  405. if [[ -f backend/config/default.json && -z $breakingConfigChange ]]; then
  406. echo -e "${RED}Configuration has breaking changes. Please rename or remove 'backend/config/default.json' and run the update command again to continue.${NC}"
  407. exit 1
  408. fi
  409. musareshChange=$(echo "${updated}" | grep "musare.sh")
  410. dbChange=$(echo "${updated}" | grep "backend/logic/db/schemas")
  411. bcChange=$(echo "${updated}" | grep "backend/config/default.json")
  412. if [[ ( $2 == "auto" && -z $dbChange && -z $bcChange && -z $musareshChange ) || -z $2 ]]; then
  413. if [[ -n $musareshChange && $(git diff @\{u\} -- musare.sh) != "" ]]; then
  414. if [[ $musareshModified != "" ]]; then
  415. echo -e "${RED}musare.sh has been modified, please reset these changes and run the update command again to continue.${NC}"
  416. else
  417. git checkout @\{u\} -- musare.sh
  418. echo -e "${YELLOW}musare.sh has been updated, please run the update command again to continue.${NC}"
  419. fi
  420. exit 1
  421. else
  422. git pull
  423. exitValue=$?
  424. if [[ ${exitValue} -gt 0 ]]; then
  425. exit ${exitValue}
  426. fi
  427. echo -e "${CYAN}Updating...${NC}"
  428. runDockerCommand "$(basename "$0") $1" pull
  429. runDockerCommand "$(basename "$0") $1" build
  430. runDockerCommand "$(basename "$0") $1" restart
  431. echo -e "${GREEN}Updated!${NC}"
  432. if [[ -n $dbChange ]]; then
  433. echo -e "${RED}Database schema has changed, please run migration!${NC}"
  434. fi
  435. if [[ -n $bcChange ]]; then
  436. echo -e "${RED}Backend config has changed, please update!${NC}"
  437. fi
  438. fi
  439. elif [[ $2 == "auto" ]]; then
  440. echo -e "${RED}Auto Update Failed! musare.sh, database and/or config has changed!${NC}"
  441. exit 1
  442. fi
  443. ;;
  444. logs)
  445. echo -e "${CYAN}Musare | Logs${NC}"
  446. # shellcheck disable=SC2068
  447. runDockerCommand "$(basename "$0") $1" logs ${@:2}
  448. ;;
  449. backup)
  450. echo -e "${CYAN}Musare | Backup${NC}"
  451. if [[ -z "${BACKUP_LOCATION}" ]]; then
  452. backupLocation="${scriptLocation%x}/backups"
  453. else
  454. backupLocation="${BACKUP_LOCATION%/}"
  455. fi
  456. if [[ ! -d "${backupLocation}" ]]; then
  457. echo -e "${YELLOW}Creating backup directory at ${backupLocation}${NC}"
  458. mkdir "${backupLocation}"
  459. fi
  460. if [[ -z "${BACKUP_NAME}" ]]; then
  461. backupLocation="${backupLocation}/musare-$(date +"%Y-%m-%d-%s").dump"
  462. else
  463. backupLocation="${backupLocation}/${BACKUP_NAME}"
  464. fi
  465. echo -e "${YELLOW}Creating backup at ${backupLocation}${NC}"
  466. ${dockerCompose} exec -T mongo sh -c "mongodump --authenticationDatabase musare -u ${MONGO_USER_USERNAME} -p ${MONGO_USER_PASSWORD} -d musare --archive" > "${backupLocation}"
  467. ;;
  468. restore)
  469. echo -e "${CYAN}Musare | Restore${NC}"
  470. if [[ -z $2 ]]; then
  471. echo -e "${GREEN}Please enter the full path of the dump you wish to restore: ${NC}"
  472. read -r restoreFile
  473. else
  474. restoreFile=$2
  475. fi
  476. if [[ -z ${restoreFile} ]]; then
  477. echo -e "${RED}Error: no restore path given, cancelled restoration.${NC}"
  478. exit 1
  479. elif [[ -d ${restoreFile} ]]; then
  480. echo -e "${RED}Error: restore path given is a directory, cancelled restoration.${NC}"
  481. exit 1
  482. elif [[ ! -f ${restoreFile} ]]; then
  483. echo -e "${RED}Error: no file at restore path given, cancelled restoration.${NC}"
  484. exit 1
  485. else
  486. ${dockerCompose} exec -T mongo sh -c "mongorestore --authenticationDatabase musare -u ${MONGO_USER_USERNAME} -p ${MONGO_USER_PASSWORD} --archive" < "${restoreFile}"
  487. fi
  488. ;;
  489. admin)
  490. echo -e "${CYAN}Musare | Add Admin${NC}"
  491. MONGO_VERSION_INT=${MONGO_VERSION:0:1}
  492. if [[ $2 == "add" ]]; then
  493. if [[ -z $3 ]]; then
  494. echo -e "${GREEN}Please enter the username of the user you wish to make an admin: ${NC}"
  495. read -r adminUser
  496. else
  497. adminUser=$3
  498. fi
  499. if [[ -z $adminUser ]]; then
  500. echo -e "${RED}Error: Username for new admin not provided.${NC}"
  501. exit 1
  502. else
  503. if [[ $MONGO_VERSION_INT -ge 5 ]]; then
  504. ${dockerCompose} exec mongo mongosh musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}" --eval "disableTelemetry(); db.users.updateOne({username: '${adminUser}'}, {\$set: {role: 'admin'}})"
  505. else
  506. ${dockerCompose} exec mongo mongo musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}" --eval "db.users.updateOne({username: '${adminUser}'}, {\$set: {role: 'admin'}})"
  507. fi
  508. fi
  509. elif [[ $2 == "remove" ]]; then
  510. if [[ -z $3 ]]; then
  511. echo -e "${GREEN}Please enter the username of the user you wish to remove as admin: ${NC}"
  512. read -r adminUser
  513. else
  514. adminUser=$3
  515. fi
  516. if [[ -z $adminUser ]]; then
  517. echo -e "${RED}Error: Username for new admin not provided.${NC}"
  518. exit 1
  519. else
  520. if [[ $MONGO_VERSION_INT -ge 5 ]]; then
  521. ${dockerCompose} exec mongo mongosh musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}" --eval "disableTelemetry(); db.users.updateOne({username: '${adminUser}'}, {\$set: {role: 'default'}})"
  522. else
  523. ${dockerCompose} exec mongo mongo musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}" --eval "db.users.updateOne({username: '${adminUser}'}, {\$set: {role: 'default'}})"
  524. fi
  525. fi
  526. else
  527. echo -e "${RED}Invalid command $2\n${YELLOW}Usage: $(basename "$0") admin [add,remove] username${NC}"
  528. exit 1
  529. fi
  530. ;;
  531. "")
  532. echo -e "${CYAN}Musare | Available Commands${NC}"
  533. echo -e "${YELLOW}start - Start services${NC}"
  534. echo -e "${YELLOW}stop - Stop services${NC}"
  535. echo -e "${YELLOW}restart - Restart services${NC}"
  536. echo -e "${YELLOW}status - Service status${NC}"
  537. echo -e "${YELLOW}logs - View logs for services${NC}"
  538. echo -e "${YELLOW}update - Update Musare${NC}"
  539. echo -e "${YELLOW}attach [backend,mongo,redis] - Attach to backend service, mongo or redis shell${NC}"
  540. echo -e "${YELLOW}build - Build services${NC}"
  541. echo -e "${YELLOW}lint - Run lint on frontend, backend and/or docs${NC}"
  542. echo -e "${YELLOW}backup - Backup database data to file${NC}"
  543. echo -e "${YELLOW}restore - Restore database data from backup file${NC}"
  544. echo -e "${YELLOW}reset - Reset service data${NC}"
  545. echo -e "${YELLOW}admin [add,remove] - Assign/unassign admin role to/from a user${NC}"
  546. echo -e "${YELLOW}typescript - Run typescript checks on frontend and/or backend${NC}"
  547. ;;
  548. *)
  549. echo -e "${CYAN}Musare${NC}"
  550. echo -e "${RED}Error: Invalid Command $1${NC}"
  551. echo -e "${CYAN}Available Commands:${NC}"
  552. echo -e "${YELLOW}start - Start services${NC}"
  553. echo -e "${YELLOW}stop - Stop services${NC}"
  554. echo -e "${YELLOW}restart - Restart services${NC}"
  555. echo -e "${YELLOW}status - Service status${NC}"
  556. echo -e "${YELLOW}logs - View logs for services${NC}"
  557. echo -e "${YELLOW}update - Update Musare${NC}"
  558. echo -e "${YELLOW}attach [backend,mongo,redis] - Attach to backend service, mongo or redis shell${NC}"
  559. echo -e "${YELLOW}build - Build services${NC}"
  560. echo -e "${YELLOW}lint - Run lint on frontend, backend and/or docs${NC}"
  561. echo -e "${YELLOW}backup - Backup database data to file${NC}"
  562. echo -e "${YELLOW}restore - Restore database data from backup file${NC}"
  563. echo -e "${YELLOW}reset - Reset service data${NC}"
  564. echo -e "${YELLOW}admin [add,remove] - Assign/unassign admin role to/from a user${NC}"
  565. echo -e "${YELLOW}typescript - Run typescript checks on frontend and/or backend${NC}"
  566. exit 1
  567. ;;
  568. esac