musare.sh 23 KB

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