musare.sh 24 KB

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