musare.sh 23 KB

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