musare.sh 22 KB

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