musare.sh 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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 [[ ${CONTAINER_MODE} == "dev" ]]; 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. IFS=" " read -r -a 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. # shellcheck disable=SC2001
  265. services=$(sed "s/\(\ \)\{0,1\}\(-\)\{0,2\}fix//g;t;q1" <<< "${@:2}")
  266. fixFound=$?
  267. if [[ $fixFound -eq 0 ]]; then
  268. fix="--fix"
  269. echo -e "${GREEN}Auto-fix enabled${NC}"
  270. fi
  271. # shellcheck disable=SC2001
  272. services=$(sed "s/\(\ \)\{0,1\}\(-\)\{0,2\}no-cache//g;t;q1" <<< "${services}")
  273. noCacheFound=$?
  274. cache="--cache"
  275. if [[ $noCacheFound -eq 0 ]]; then
  276. cache=""
  277. echo -e "${YELLOW}ESlint cache disabled${NC}"
  278. fi
  279. # shellcheck disable=SC2068
  280. servicesString=$(handleServices "backend frontend docs" ${services[@]})
  281. if [[ ${servicesString:0:1} == 1 ]]; then
  282. if [[ ${servicesString:2:4} == "all" || "${servicesString:2}" == *frontend* ]]; then
  283. echo -e "${CYAN}Running frontend lint...${NC}"
  284. ${dockerCompose} exec -T frontend npx eslint "${cache}" src --ext .js,.ts,.vue "${fix}"
  285. frontendExitValue=$?
  286. fi
  287. if [[ ${servicesString:2:4} == "all" || "${servicesString:2}" == *backend* ]]; then
  288. echo -e "${CYAN}Running backend lint...${NC}"
  289. ${dockerCompose} exec -T backend npx eslint "${cache}" logic "${fix}"
  290. backendExitValue=$?
  291. fi
  292. if [[ ${servicesString:2:4} == "all" || "${servicesString:2}" == *docs* ]]; then
  293. echo -e "${CYAN}Running docs lint...${NC}"
  294. ${docker} run -v "${scriptLocation}":/workdir ghcr.io/igorshubovych/markdownlint-cli:latest ".wiki" "*.md" $fix
  295. docsExitValue=$?
  296. fi
  297. if [[ ${frontendExitValue} -gt 0 || ${backendExitValue} -gt 0 || ${docsExitValue} -gt 0 ]]; then
  298. exitValue=1
  299. else
  300. exitValue=0
  301. fi
  302. else
  303. echo -e "${RED}${servicesString:2}\n${YELLOW}Usage: $(basename "$0") lint [backend, frontend, docs] [fix]${NC}"
  304. exitValue=1
  305. fi
  306. if [[ ${exitValue} -gt 0 ]]; then
  307. exit ${exitValue}
  308. fi
  309. ;;
  310. typescript|ts)
  311. echo -e "${CYAN}Musare | TypeScript Check${NC}"
  312. servicesString=$(handleServices "backend frontend" "${@:2}")
  313. if [[ ${servicesString:0:1} == 1 ]]; then
  314. if [[ ${servicesString:2:4} == "all" || "${servicesString:2}" == *frontend* ]]; then
  315. echo -e "${CYAN}Running frontend typescript check...${NC}"
  316. ${dockerCompose} exec -T frontend npm run typescript
  317. frontendExitValue=$?
  318. fi
  319. if [[ ${servicesString:2:4} == "all" || "${servicesString:2}" == *backend* ]]; then
  320. echo -e "${CYAN}Running backend typescript check...${NC}"
  321. ${dockerCompose} exec -T backend npm run typescript
  322. backendExitValue=$?
  323. fi
  324. if [[ ${frontendExitValue} -gt 0 || ${backendExitValue} -gt 0 ]]; then
  325. exitValue=1
  326. else
  327. exitValue=0
  328. fi
  329. else
  330. echo -e "${RED}${servicesString:2}\n${YELLOW}Usage: $(basename "$0") typescript [backend, frontend]${NC}"
  331. exitValue=1
  332. fi
  333. if [[ ${exitValue} -gt 0 ]]; then
  334. exit ${exitValue}
  335. fi
  336. ;;
  337. test)
  338. echo -e "${CYAN}Musare | Test${NC}"
  339. servicesString=$(handleServices "frontend" "${@:2}")
  340. if [[ ${servicesString:0:1} == 1 ]]; then
  341. if [[ ${servicesString:2:4} == "all" || "${servicesString:2}" == *frontend* ]]; then
  342. echo -e "${CYAN}Running frontend tests...${NC}"
  343. ${dockerCompose} exec -T frontend npm run test -- --run
  344. frontendExitValue=$?
  345. fi
  346. if [[ ${frontendExitValue} -gt 0 ]]; then
  347. exitValue=1
  348. else
  349. exitValue=0
  350. fi
  351. else
  352. echo -e "${RED}${servicesString:2}\n${YELLOW}Usage: $(basename "$0") test [frontend]${NC}"
  353. exitValue=1
  354. fi
  355. if [[ ${exitValue} -gt 0 ]]; then
  356. exit ${exitValue}
  357. fi
  358. ;;
  359. test:coverage)
  360. echo -e "${CYAN}Musare | Test Coverage${NC}"
  361. servicesString=$(handleServices "frontend" "${@:2}")
  362. if [[ ${servicesString:0:1} == 1 ]]; then
  363. if [[ ${servicesString:2:4} == "all" || "${servicesString:2}" == *frontend* ]]; then
  364. echo -e "${CYAN}Running frontend test coverage report...${NC}"
  365. ${dockerCompose} exec -T frontend npm run coverage
  366. frontendExitValue=$?
  367. fi
  368. if [[ ${frontendExitValue} -gt 0 ]]; then
  369. exitValue=1
  370. else
  371. exitValue=0
  372. fi
  373. else
  374. echo -e "${RED}${servicesString:2}\n${YELLOW}Usage: $(basename "$0") test:coverage [frontend]${NC}"
  375. exitValue=1
  376. fi
  377. if [[ ${exitValue} -gt 0 ]]; then
  378. exit ${exitValue}
  379. fi
  380. ;;
  381. update)
  382. echo -e "${CYAN}Musare | Update${NC}"
  383. musareshModified=$(git diff HEAD -- musare.sh)
  384. git fetch
  385. exitValue=$?
  386. if [[ ${exitValue} -gt 0 ]]; then
  387. exit ${exitValue}
  388. fi
  389. updated=$(git log --name-only --oneline HEAD..@\{u\})
  390. if [[ ${updated} == "" ]]; then
  391. echo -e "${GREEN}Already up to date${NC}"
  392. exit ${exitValue}
  393. fi
  394. musareshChange=$(echo "${updated}" | grep "musare.sh")
  395. dbChange=$(echo "${updated}" | grep "backend/logic/db/schemas")
  396. fcChange=$(echo "${updated}" | grep "frontend/dist/config/template.json")
  397. bcChange=$(echo "${updated}" | grep "backend/config/template.json")
  398. if [[ ( $2 == "auto" && -z $dbChange && -z $fcChange && -z $bcChange && -z $musareshChange ) || -z $2 ]]; then
  399. if [[ -n $musareshChange && $(git diff @\{u\} -- musare.sh) != "" ]]; then
  400. if [[ $musareshModified != "" ]]; then
  401. echo -e "${RED}musare.sh has been modified, please reset these changes and run the update command again to continue.${NC}"
  402. else
  403. git checkout @\{u\} -- musare.sh
  404. echo -e "${YELLOW}musare.sh has been updated, please run the update command again to continue.${NC}"
  405. fi
  406. exit 1
  407. else
  408. git pull
  409. exitValue=$?
  410. if [[ ${exitValue} -gt 0 ]]; then
  411. exit ${exitValue}
  412. fi
  413. echo -e "${CYAN}Updating...${NC}"
  414. runDockerCommand "$(basename "$0") $1" pull
  415. runDockerCommand "$(basename "$0") $1" build
  416. runDockerCommand "$(basename "$0") $1" restart
  417. echo -e "${GREEN}Updated!${NC}"
  418. if [[ -n $dbChange ]]; then
  419. echo -e "${RED}Database schema has changed, please run migration!${NC}"
  420. fi
  421. if [[ -n $fcChange ]]; then
  422. echo -e "${RED}Frontend config has changed, please update!${NC}"
  423. fi
  424. if [[ -n $bcChange ]]; then
  425. echo -e "${RED}Backend config has changed, please update!${NC}"
  426. fi
  427. fi
  428. elif [[ $2 == "auto" ]]; then
  429. echo -e "${RED}Auto Update Failed! musare.sh, database and/or config has changed!${NC}"
  430. exit 1
  431. fi
  432. ;;
  433. logs)
  434. echo -e "${CYAN}Musare | Logs${NC}"
  435. # shellcheck disable=SC2068
  436. runDockerCommand "$(basename "$0") $1" logs ${@:2}
  437. ;;
  438. backup)
  439. echo -e "${CYAN}Musare | Backup${NC}"
  440. if [[ -z "${BACKUP_LOCATION}" ]]; then
  441. backupLocation="${scriptLocation%x}/backups"
  442. else
  443. backupLocation="${BACKUP_LOCATION%/}"
  444. fi
  445. if [[ ! -d "${backupLocation}" ]]; then
  446. echo -e "${YELLOW}Creating backup directory at ${backupLocation}${NC}"
  447. mkdir "${backupLocation}"
  448. fi
  449. if [[ -z "${BACKUP_NAME}" ]]; then
  450. backupLocation="${backupLocation}/musare-$(date +"%Y-%m-%d-%s").dump"
  451. else
  452. backupLocation="${backupLocation}/${BACKUP_NAME}"
  453. fi
  454. echo -e "${YELLOW}Creating backup at ${backupLocation}${NC}"
  455. ${dockerCompose} exec -T mongo sh -c "mongodump --authenticationDatabase musare -u ${MONGO_USER_USERNAME} -p ${MONGO_USER_PASSWORD} -d musare --archive" > "${backupLocation}"
  456. ;;
  457. restore)
  458. echo -e "${CYAN}Musare | Restore${NC}"
  459. if [[ -z $2 ]]; then
  460. echo -e "${GREEN}Please enter the full path of the dump you wish to restore: ${NC}"
  461. read -r restoreFile
  462. else
  463. restoreFile=$2
  464. fi
  465. if [[ -z ${restoreFile} ]]; then
  466. echo -e "${RED}Error: no restore path given, cancelled restoration.${NC}"
  467. exit 1
  468. elif [[ -d ${restoreFile} ]]; then
  469. echo -e "${RED}Error: restore path given is a directory, cancelled restoration.${NC}"
  470. exit 1
  471. elif [[ ! -f ${restoreFile} ]]; then
  472. echo -e "${RED}Error: no file at restore path given, cancelled restoration.${NC}"
  473. exit 1
  474. else
  475. ${dockerCompose} exec -T mongo sh -c "mongorestore --authenticationDatabase musare -u ${MONGO_USER_USERNAME} -p ${MONGO_USER_PASSWORD} --archive" < "${restoreFile}"
  476. fi
  477. ;;
  478. admin)
  479. echo -e "${CYAN}Musare | Add Admin${NC}"
  480. MONGO_VERSION_INT=${MONGO_VERSION:0:1}
  481. if [[ $2 == "add" ]]; then
  482. if [[ -z $3 ]]; then
  483. echo -e "${GREEN}Please enter the username of the user you wish to make an admin: ${NC}"
  484. read -r adminUser
  485. else
  486. adminUser=$3
  487. fi
  488. if [[ -z $adminUser ]]; then
  489. echo -e "${RED}Error: Username for new admin not provided.${NC}"
  490. exit 1
  491. else
  492. if [[ $MONGO_VERSION_INT -ge 5 ]]; then
  493. ${dockerCompose} exec mongo mongosh musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}" --eval "disableTelemetry(); db.users.updateOne({username: '${adminUser}'}, {\$set: {role: 'admin'}})"
  494. else
  495. ${dockerCompose} exec mongo mongo musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}" --eval "db.users.updateOne({username: '${adminUser}'}, {\$set: {role: 'admin'}})"
  496. fi
  497. fi
  498. elif [[ $2 == "remove" ]]; then
  499. if [[ -z $3 ]]; then
  500. echo -e "${GREEN}Please enter the username of the user you wish to remove as admin: ${NC}"
  501. read -r adminUser
  502. else
  503. adminUser=$3
  504. fi
  505. if [[ -z $adminUser ]]; then
  506. echo -e "${RED}Error: Username for new admin not provided.${NC}"
  507. exit 1
  508. else
  509. if [[ $MONGO_VERSION_INT -ge 5 ]]; then
  510. ${dockerCompose} exec mongo mongosh musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}" --eval "disableTelemetry(); db.users.updateOne({username: '${adminUser}'}, {\$set: {role: 'default'}})"
  511. else
  512. ${dockerCompose} exec mongo mongo musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}" --eval "db.users.updateOne({username: '${adminUser}'}, {\$set: {role: 'default'}})"
  513. fi
  514. fi
  515. else
  516. echo -e "${RED}Invalid command $2\n${YELLOW}Usage: $(basename "$0") admin [add,remove] username${NC}"
  517. exit 1
  518. fi
  519. ;;
  520. "")
  521. echo -e "${CYAN}Musare | Available Commands${NC}"
  522. echo -e "${YELLOW}start - Start services${NC}"
  523. echo -e "${YELLOW}stop - Stop services${NC}"
  524. echo -e "${YELLOW}restart - Restart services${NC}"
  525. echo -e "${YELLOW}status - Service status${NC}"
  526. echo -e "${YELLOW}logs - View logs for services${NC}"
  527. echo -e "${YELLOW}update - Update Musare${NC}"
  528. echo -e "${YELLOW}attach [backend,mongo,redis] - Attach to backend service, mongo or redis shell${NC}"
  529. echo -e "${YELLOW}build - Build services${NC}"
  530. echo -e "${YELLOW}lint - Run lint on frontend, backend and/or docs${NC}"
  531. echo -e "${YELLOW}backup - Backup database data to file${NC}"
  532. echo -e "${YELLOW}restore - Restore database data from backup file${NC}"
  533. echo -e "${YELLOW}reset - Reset service data${NC}"
  534. echo -e "${YELLOW}admin [add,remove] - Assign/unassign admin role to/from a user${NC}"
  535. echo -e "${YELLOW}typescript - Run typescript checks on frontend and/or backend${NC}"
  536. ;;
  537. *)
  538. echo -e "${CYAN}Musare${NC}"
  539. echo -e "${RED}Error: Invalid Command $1${NC}"
  540. echo -e "${CYAN}Available Commands:${NC}"
  541. echo -e "${YELLOW}start - Start services${NC}"
  542. echo -e "${YELLOW}stop - Stop services${NC}"
  543. echo -e "${YELLOW}restart - Restart services${NC}"
  544. echo -e "${YELLOW}status - Service status${NC}"
  545. echo -e "${YELLOW}logs - View logs for services${NC}"
  546. echo -e "${YELLOW}update - Update Musare${NC}"
  547. echo -e "${YELLOW}attach [backend,mongo,redis] - Attach to backend service, mongo or redis shell${NC}"
  548. echo -e "${YELLOW}build - Build services${NC}"
  549. echo -e "${YELLOW}lint - Run lint on frontend, backend and/or docs${NC}"
  550. echo -e "${YELLOW}backup - Backup database data to file${NC}"
  551. echo -e "${YELLOW}restore - Restore database data from backup file${NC}"
  552. echo -e "${YELLOW}reset - Reset service data${NC}"
  553. echo -e "${YELLOW}admin [add,remove] - Assign/unassign admin role to/from a user${NC}"
  554. echo -e "${YELLOW}typescript - Run typescript checks on frontend and/or backend${NC}"
  555. exit 1
  556. ;;
  557. esac