musare.sh 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. #!/bin/bash
  2. export PATH=/usr/local/bin:/usr/bin:/bin
  3. CYAN='\033[33;36m';
  4. RED='\033[0;31m'
  5. YELLOW='\033[0;93m'
  6. GREEN='\033[0;32m'
  7. NC='\033[0m'
  8. scriptLocation=$(dirname -- "$(readlink -fn -- "$0"; echo x)")
  9. cd "${scriptLocation%x}" || exit 1
  10. if [[ -f .env ]]; then
  11. # shellcheck disable=SC1091
  12. source .env
  13. else
  14. echo -e "${RED}Error: .env does not exist${NC}"
  15. exit 2
  16. fi
  17. if [[ -z ${DOCKER_COMMAND} ]]; then
  18. DOCKER_COMMAND="docker"
  19. elif [[ ${DOCKER_COMMAND} != "docker" && ${DOCKER_COMMAND} != "podman" ]]; then
  20. echo -e "${RED}Error: Invalid DOCKER_COMMAND${NC}"
  21. exit 1
  22. fi
  23. docker="${DOCKER_COMMAND}"
  24. dockerCompose="${DOCKER_COMMAND}-compose"
  25. if [[ ! -x "$(command -v ${docker})" || ! -x "$(command -v ${dockerCompose})" ]]; then
  26. if [[ -x "$(command -v ${docker})" && ! -x "$(command -v ${dockerCompose})" ]]; then
  27. echo -e "${RED}Error: ${dockerCompose} not installed.${NC}"
  28. elif [[ ! -x "$(command -v ${docker})" && -x "$(command -v ${dockerCompose})" ]]; then
  29. echo -e "${RED}Error: ${docker} not installed.${NC}"
  30. else
  31. echo -e "${RED}Error: ${docker} and ${dockerCompose} not installed.${NC}"
  32. fi
  33. exit 1
  34. fi
  35. composeFiles="-f docker-compose.yml"
  36. if [[ ${CONTAINER_MODE} == "dev" ]]; then
  37. composeFiles="${composeFiles} -f docker-compose.dev.yml"
  38. fi
  39. if [[ -f docker-compose.override.yml ]]; then
  40. composeFiles="${composeFiles} -f docker-compose.override.yml"
  41. fi
  42. dockerCompose="${dockerCompose} ${composeFiles}"
  43. handleServices()
  44. {
  45. validServices=($1)
  46. servicesArray=()
  47. invalidServices=false
  48. for x in "${@:2}"; do
  49. if [[ ${validServices[*]} =~ (^|[[:space:]])"$x"($|[[:space:]]) ]]; then
  50. if ! [[ ${servicesArray[*]} =~ (^|[[:space:]])"$x"($|[[:space:]]) ]]; then
  51. servicesArray+=("${x}")
  52. fi
  53. else
  54. if [[ $invalidServices == false ]]; then
  55. invalidServices="${x}"
  56. else
  57. invalidServices="${invalidServices} ${x}"
  58. fi
  59. fi
  60. done
  61. if [[ $invalidServices == false && ${#servicesArray[@]} -gt 0 ]]; then
  62. echo "1|${servicesArray[*]}"
  63. elif [[ $invalidServices == false ]]; then
  64. echo "1|all"
  65. else
  66. echo "0|Invalid Service(s): ${invalidServices}"
  67. fi
  68. }
  69. runDockerCommand()
  70. {
  71. validCommands=(start stop restart pull build ps logs)
  72. if [[ ${validCommands[*]} =~ (^|[[:space:]])"$2"($|[[:space:]]) ]]; then
  73. servicesString=$(handleServices "backend frontend mongo redis" "${@:3}")
  74. if [[ ${servicesString:0:1} == 1 ]]; then
  75. if [[ ${servicesString:2:4} == "all" ]]; then
  76. servicesString=""
  77. pullServices="mongo redis"
  78. buildServices="backend frontend"
  79. else
  80. servicesString=${servicesString:2}
  81. pullArray=()
  82. buildArray=()
  83. if [[ "${servicesString}" == *mongo* ]]; then
  84. pullArray+=("mongo")
  85. fi
  86. if [[ "${servicesString}" == *redis* ]]; then
  87. pullArray+=("redis")
  88. fi
  89. if [[ "${servicesString}" == *backend* ]]; then
  90. buildArray+=("backend")
  91. fi
  92. if [[ "${servicesString}" == *frontend* ]]; then
  93. buildArray+=("frontend")
  94. fi
  95. pullServices="${pullArray[*]}"
  96. buildServices="${buildArray[*]}"
  97. fi
  98. if [[ ${2} == "stop" || ${2} == "restart" ]]; then
  99. # shellcheck disable=SC2086
  100. ${dockerCompose} stop ${servicesString}
  101. fi
  102. if [[ ${2} == "start" || ${2} == "restart" ]]; then
  103. # shellcheck disable=SC2086
  104. ${dockerCompose} up -d ${servicesString}
  105. fi
  106. if [[ ${2} == "pull" && ${pullServices} != "" ]]; then
  107. # shellcheck disable=SC2086
  108. ${dockerCompose} "${2}" ${pullServices}
  109. fi
  110. if [[ ${2} == "build" && ${buildServices} != "" ]]; then
  111. # shellcheck disable=SC2086
  112. ${dockerCompose} "${2}" ${buildServices}
  113. fi
  114. if [[ ${2} == "ps" || ${2} == "logs" ]]; then
  115. # shellcheck disable=SC2086
  116. ${dockerCompose} "${2}" ${servicesString}
  117. fi
  118. exitValue=$?
  119. if [[ ${exitValue} -gt 0 ]]; then
  120. exit ${exitValue}
  121. fi
  122. else
  123. echo -e "${RED}${servicesString:2}\n${YELLOW}Usage: ${1} [backend, frontend, mongo, redis]${NC}"
  124. exit 1
  125. fi
  126. else
  127. echo -e "${RED}Error: Invalid runDockerCommand input${NC}"
  128. exit 1
  129. fi
  130. }
  131. getContainerId()
  132. {
  133. if [[ ${DOCKER_COMMAND} == "docker" ]]; then
  134. containerId=$(${dockerCompose} ps -q "${1}")
  135. else
  136. containerId=$(${dockerCompose} ps | sed '0,/CONTAINER/d' | awk "/${1}/ {print \$1;exit}")
  137. fi
  138. echo "${containerId}"
  139. }
  140. case $1 in
  141. start)
  142. echo -e "${CYAN}Musare | Start Services${NC}"
  143. # shellcheck disable=SC2068
  144. runDockerCommand "$(basename "$0") $1" start ${@:2}
  145. ;;
  146. stop)
  147. echo -e "${CYAN}Musare | Stop Services${NC}"
  148. # shellcheck disable=SC2068
  149. runDockerCommand "$(basename "$0") $1" stop ${@:2}
  150. ;;
  151. restart)
  152. echo -e "${CYAN}Musare | Restart Services${NC}"
  153. # shellcheck disable=SC2068
  154. runDockerCommand "$(basename "$0") $1" restart ${@:2}
  155. ;;
  156. build)
  157. echo -e "${CYAN}Musare | Build Services${NC}"
  158. # shellcheck disable=SC2068
  159. runDockerCommand "$(basename "$0") $1" pull ${@:2}
  160. # shellcheck disable=SC2068
  161. runDockerCommand "$(basename "$0") $1" build ${@:2}
  162. ;;
  163. status)
  164. echo -e "${CYAN}Musare | Service Status${NC}"
  165. # shellcheck disable=SC2068
  166. runDockerCommand "$(basename "$0") $1" ps ${@:2}
  167. ;;
  168. reset)
  169. echo -e "${CYAN}Musare | Reset Services${NC}"
  170. servicesString=$(handleServices "backend frontend mongo redis" "${@:2}")
  171. if [[ ${servicesString:0:1} == 1 && ${servicesString:2:4} == "all" ]]; then
  172. echo -e "${RED}Resetting will remove the ${REDIS_DATA_LOCATION} and ${MONGO_DATA_LOCATION} directories.${NC}"
  173. echo -e "${GREEN}Are you sure you want to reset all data? ${YELLOW}[y,n]: ${NC}"
  174. read -r confirm
  175. if [[ "${confirm}" == y* ]]; then
  176. runDockerCommand "$(basename "$0") $1" stop
  177. ${dockerCompose} rm -v --force
  178. if [[ -d $REDIS_DATA_LOCATION ]]; then
  179. rm -rf "${REDIS_DATA_LOCATION}"
  180. fi
  181. if [[ -d $MONGO_DATA_LOCATION ]]; then
  182. rm -rf "${MONGO_DATA_LOCATION}"
  183. fi
  184. else
  185. echo -e "${RED}Cancelled reset${NC}"
  186. fi
  187. elif [[ ${servicesString:0:1} == 1 ]]; then
  188. if [[ "${servicesString:2}" == *redis* && "${servicesString:2}" == *mongo* ]]; then
  189. echo -e "${RED}Resetting will remove the ${REDIS_DATA_LOCATION} and ${MONGO_DATA_LOCATION} directories.${NC}"
  190. elif [[ "${servicesString:2}" == *redis* ]]; then
  191. echo -e "${RED}Resetting will remove the ${REDIS_DATA_LOCATION} directory.${NC}"
  192. elif [[ "${servicesString:2}" == *mongo* ]]; then
  193. echo -e "${RED}Resetting will remove the ${MONGO_DATA_LOCATION} directory.${NC}"
  194. fi
  195. echo -e "${GREEN}Are you sure you want to reset all data for $(echo "${servicesString:2}" | tr ' ' ',')? ${YELLOW}[y,n]: ${NC}"
  196. read -r confirm
  197. if [[ "${confirm}" == y* ]]; then
  198. # shellcheck disable=SC2086
  199. runDockerCommand "$(basename "$0") $1" stop ${servicesString:2}
  200. # shellcheck disable=SC2086
  201. ${dockerCompose} rm -v --force ${servicesString}
  202. if [[ "${servicesString:2}" == *redis* && -d $REDIS_DATA_LOCATION ]]; then
  203. rm -rf "${REDIS_DATA_LOCATION}"
  204. fi
  205. if [[ "${servicesString:2}" == *mongo* && -d $MONGO_DATA_LOCATION ]]; then
  206. rm -rf "${MONGO_DATA_LOCATION}"
  207. fi
  208. else
  209. echo -e "${RED}Cancelled reset${NC}"
  210. fi
  211. else
  212. echo -e "${RED}${servicesString:2}\n${YELLOW}Usage: $(basename "$0") build [backend, frontend, mongo, redis]${NC}"
  213. exit 1
  214. fi
  215. ;;
  216. attach)
  217. echo -e "${CYAN}Musare | Attach${NC}"
  218. if [[ $2 == "backend" ]]; then
  219. containerId=$(getContainerId backend)
  220. if [[ -z $containerId ]]; then
  221. echo -e "${RED}Error: Backend offline, please start to attach.${NC}"
  222. exit 1
  223. else
  224. echo -e "${YELLOW}Detach with CTRL+P+Q${NC}"
  225. ${docker} attach "$containerId"
  226. fi
  227. elif [[ $2 == "mongo" ]]; then
  228. MONGO_VERSION_INT=${MONGO_VERSION:0:1}
  229. if [[ -z $(getContainerId mongo) ]]; then
  230. echo -e "${RED}Error: Mongo offline, please start to attach.${NC}"
  231. exit 1
  232. else
  233. echo -e "${YELLOW}Detach with CTRL+D${NC}"
  234. if [[ $MONGO_VERSION_INT -ge 5 ]]; then
  235. ${dockerCompose} exec mongo mongosh musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}" --eval "disableTelemetry()" --shell
  236. else
  237. ${dockerCompose} exec mongo mongo musare -u "${MONGO_USER_USERNAME}" -p "${MONGO_USER_PASSWORD}"
  238. fi
  239. fi
  240. elif [[ $2 == "redis" ]]; then
  241. if [[ -z $(getContainerId redis) ]]; then
  242. echo -e "${RED}Error: Redis offline, please start to attach.${NC}"
  243. exit 1
  244. else
  245. echo -e "${YELLOW}Detach with CTRL+C${NC}"
  246. ${dockerCompose} exec redis redis-cli -a "${REDIS_PASSWORD}"
  247. fi
  248. else
  249. echo -e "${RED}Invalid service $2\n${YELLOW}Usage: $(basename "$0") attach [backend,mongo,redis]${NC}"
  250. exit 1
  251. fi
  252. ;;
  253. lint|eslint)
  254. echo -e "${CYAN}Musare | Lint${NC}"
  255. services=$(sed "s/\(\ \)\{0,1\}\(-\)\{0,2\}fix//g;t;q1" <<< "${@:2}")
  256. fixFound=$?
  257. if [[ $fixFound -eq 0 ]]; then
  258. fix="--fix"
  259. echo -e "${GREEN}Auto-fix enabled${NC}"
  260. fi
  261. services=$(sed "s/\(\ \)\{0,1\}\(-\)\{0,2\}no-cache//g;t;q1" <<< "${services}")
  262. noCacheFound=$?
  263. cache="--cache"
  264. if [[ $noCacheFound -eq 0 ]]; then
  265. cache=""
  266. echo -e "${YELLOW}ESlint cache disabled${NC}"
  267. fi
  268. # shellcheck disable=SC2068
  269. servicesString=$(handleServices "backend frontend docs" ${services[@]})
  270. if [[ ${servicesString:0:1} == 1 ]]; then
  271. if [[ ${servicesString:2:4} == "all" || "${servicesString:2}" == *frontend* ]]; then
  272. echo -e "${CYAN}Running frontend lint...${NC}"
  273. ${dockerCompose} exec -T frontend npx eslint $cache src --ext .js,.ts,.vue $fix
  274. frontendExitValue=$?
  275. fi
  276. if [[ ${servicesString:2:4} == "all" || "${servicesString:2}" == *backend* ]]; then
  277. echo -e "${CYAN}Running backend lint...${NC}"
  278. ${dockerCompose} exec -T backend npx eslint $cache logic $fix
  279. backendExitValue=$?
  280. fi
  281. if [[ ${servicesString:2:4} == "all" || "${servicesString:2}" == *docs* ]]; then
  282. echo -e "${CYAN}Running docs lint...${NC}"
  283. ${docker} run -v "${scriptLocation}":/workdir ghcr.io/igorshubovych/markdownlint-cli:latest ".wiki" "*.md" $fix
  284. docsExitValue=$?
  285. fi
  286. if [[ ${frontendExitValue} -gt 0 || ${backendExitValue} -gt 0 || ${docsExitValue} -gt 0 ]]; then
  287. exitValue=1
  288. else
  289. exitValue=0
  290. fi
  291. else
  292. echo -e "${RED}${servicesString:2}\n${YELLOW}Usage: $(basename "$0") lint [backend, frontend, docs] [fix]${NC}"
  293. exitValue=1
  294. fi
  295. if [[ ${exitValue} -gt 0 ]]; then
  296. exit ${exitValue}
  297. fi
  298. ;;
  299. typescript|ts)
  300. echo -e "${CYAN}Musare | TypeScript Check${NC}"
  301. servicesString=$(handleServices "backend frontend" "${@:2}")
  302. if [[ ${servicesString:0:1} == 1 ]]; then
  303. if [[ ${servicesString:2:4} == "all" || "${servicesString:2}" == *frontend* ]]; then
  304. echo -e "${CYAN}Running frontend typescript check...${NC}"
  305. ${dockerCompose} exec -T frontend npm run typescript
  306. frontendExitValue=$?
  307. fi
  308. if [[ ${servicesString:2:4} == "all" || "${servicesString:2}" == *backend* ]]; then
  309. echo -e "${CYAN}Running backend typescript check...${NC}"
  310. ${dockerCompose} exec -T backend npm run typescript
  311. backendExitValue=$?
  312. fi
  313. if [[ ${frontendExitValue} -gt 0 || ${backendExitValue} -gt 0 ]]; then
  314. exitValue=1
  315. else
  316. exitValue=0
  317. fi
  318. else
  319. echo -e "${RED}${servicesString:2}\n${YELLOW}Usage: $(basename "$0") typescript [backend, frontend]${NC}"
  320. exitValue=1
  321. fi
  322. if [[ ${exitValue} -gt 0 ]]; then
  323. exit ${exitValue}
  324. fi
  325. ;;
  326. test)
  327. echo -e "${CYAN}Musare | Test${NC}"
  328. servicesString=$(handleServices "frontend" "${@:2}")
  329. if [[ ${servicesString:0:1} == 1 ]]; then
  330. if [[ ${servicesString:2:4} == "all" || "${servicesString:2}" == *frontend* ]]; then
  331. echo -e "${CYAN}Running frontend tests...${NC}"
  332. ${dockerCompose} exec -T frontend npm run test -- --run
  333. frontendExitValue=$?
  334. fi
  335. if [[ ${frontendExitValue} -gt 0 ]]; then
  336. exitValue=1
  337. else
  338. exitValue=0
  339. fi
  340. else
  341. echo -e "${RED}${servicesString:2}\n${YELLOW}Usage: $(basename "$0") test [frontend]${NC}"
  342. exitValue=1
  343. fi
  344. if [[ ${exitValue} -gt 0 ]]; then
  345. exit ${exitValue}
  346. fi
  347. ;;
  348. test:coverage)
  349. echo -e "${CYAN}Musare | Test Coverage${NC}"
  350. servicesString=$(handleServices "frontend" "${@:2}")
  351. if [[ ${servicesString:0:1} == 1 ]]; then
  352. if [[ ${servicesString:2:4} == "all" || "${servicesString:2}" == *frontend* ]]; then
  353. echo -e "${CYAN}Running frontend test coverage report...${NC}"
  354. ${dockerCompose} exec -T frontend npm run coverage
  355. frontendExitValue=$?
  356. fi
  357. if [[ ${frontendExitValue} -gt 0 ]]; then
  358. exitValue=1
  359. else
  360. exitValue=0
  361. fi
  362. else
  363. echo -e "${RED}${servicesString:2}\n${YELLOW}Usage: $(basename "$0") test:coverage [frontend]${NC}"
  364. exitValue=1
  365. fi
  366. if [[ ${exitValue} -gt 0 ]]; then
  367. exit ${exitValue}
  368. fi
  369. ;;
  370. update)
  371. echo -e "${CYAN}Musare | Update${NC}"
  372. musareshModified=$(git diff HEAD -- musare.sh)
  373. git fetch
  374. exitValue=$?
  375. if [[ ${exitValue} -gt 0 ]]; then
  376. exit ${exitValue}
  377. fi
  378. updated=$(git log --name-only --oneline HEAD..@\{u\})
  379. if [[ ${updated} == "" ]]; then
  380. echo -e "${GREEN}Already up to date${NC}"
  381. exit ${exitValue}
  382. fi
  383. musareshChange=$(echo "${updated}" | grep "musare.sh")
  384. dbChange=$(echo "${updated}" | grep "backend/logic/db/schemas")
  385. fcChange=$(echo "${updated}" | grep "frontend/dist/config/template.json")
  386. bcChange=$(echo "${updated}" | grep "backend/config/template.json")
  387. if [[ ( $2 == "auto" && -z $dbChange && -z $fcChange && -z $bcChange && -z $musareshChange ) || -z $2 ]]; then
  388. if [[ -n $musareshChange && $(git diff @\{u\} -- musare.sh) != "" ]]; then
  389. if [[ $musareshModified != "" ]]; then
  390. echo -e "${RED}musare.sh has been modified, please reset these changes and run the update command again to continue.${NC}"
  391. else
  392. git checkout @\{u\} -- musare.sh
  393. echo -e "${YELLOW}musare.sh has been updated, please run the update command again to continue.${NC}"
  394. fi
  395. exit 1
  396. else
  397. git pull
  398. exitValue=$?
  399. if [[ ${exitValue} -gt 0 ]]; then
  400. exit ${exitValue}
  401. fi
  402. echo -e "${CYAN}Updating...${NC}"
  403. runDockerCommand "$(basename "$0") $1" pull
  404. runDockerCommand "$(basename "$0") $1" build
  405. runDockerCommand "$(basename "$0") $1" restart
  406. echo -e "${GREEN}Updated!${NC}"
  407. if [[ -n $dbChange ]]; then
  408. echo -e "${RED}Database schema has changed, please run migration!${NC}"
  409. fi
  410. if [[ -n $fcChange ]]; then
  411. echo -e "${RED}Frontend config has changed, please update!${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