app.sh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. # use greadlink instead of readlink on osx
  4. if [[ "$(uname)" == "Darwin" ]]; then
  5. rdlk=greadlink
  6. else
  7. rdlk=readlink
  8. fi
  9. ROOT_FOLDER="$($rdlk -f $(dirname "${BASH_SOURCE[0]}")/..)"
  10. STATE_FOLDER="${ROOT_FOLDER}/state"
  11. show_help() {
  12. cat << EOF
  13. app 0.0.1
  14. CLI for managing Tipi apps
  15. Usage: app <command> <app> [<arguments>]
  16. Commands:
  17. install Pulls down images for an app and starts it
  18. uninstall Removes images and destroys all data for an app
  19. stop Stops an installed app
  20. start Starts an installed app
  21. compose Passes all arguments to docker-compose
  22. ls-installed Lists installed apps
  23. EOF
  24. }
  25. # Get field from json file
  26. function get_json_field() {
  27. local json_file="$1"
  28. local field="$2"
  29. echo $(jq -r ".${field}" "${json_file}")
  30. }
  31. list_installed_apps() {
  32. str=$(get_json_field ${STATE_FOLDER}/apps.json installed)
  33. echo $str
  34. }
  35. if [ -z ${1+x} ]; then
  36. command=""
  37. else
  38. command="$1"
  39. fi
  40. # Lists installed apps
  41. if [[ "$command" = "ls-installed" ]]; then
  42. list_installed_apps
  43. exit
  44. fi
  45. if [ -z ${2+x} ]; then
  46. show_help
  47. exit 1
  48. else
  49. app="$2"
  50. app_dir="${ROOT_FOLDER}/apps/${app}"
  51. app_data_dir="${ROOT_FOLDER}/app-data/${app}"
  52. if [[ -z "${app}" ]] || [[ ! -d "${app_dir}" ]]; then
  53. echo "Error: \"${app}\" is not a valid app"
  54. exit 1
  55. fi
  56. fi
  57. if [ -z ${3+x} ]; then
  58. args=""
  59. else
  60. args="${@:3}"
  61. fi
  62. compose() {
  63. local app="${1}"
  64. shift
  65. # App data folder
  66. local env_file="${ROOT_FOLDER}/.env"
  67. local app_compose_file="${app_dir}/docker-compose.yml"
  68. local common_compose_file="${ROOT_FOLDER}/apps/docker-compose.common.yml"
  69. local app_dir="${ROOT_FOLDER}/apps/${app}"
  70. # Vars to use in compose file
  71. export APP_DATA_DIR="${app_data_dir}"
  72. export APP_PASSWORD="password"
  73. export APP_DIR="${app_dir}"
  74. export ROOT_FOLDER="${ROOT_FOLDER}"
  75. # Docker-compose does not support multiple env files
  76. # --env-file "${env_file}" \
  77. docker-compose \
  78. --env-file "${ROOT_FOLDER}/app-data/${app}/app.env" \
  79. --project-name "${app}" \
  80. --file "${app_compose_file}" \
  81. --file "${common_compose_file}" \
  82. "${@}"
  83. }
  84. # Install new app
  85. if [[ "$command" = "install" ]]; then
  86. compose "${app}" pull
  87. # Copy default data dir to app data dir
  88. cp -r "${ROOT_FOLDER}/apps/${app}/data" "${app_data_dir}/data"
  89. compose "${app}" up -d
  90. exit
  91. fi
  92. # Removes images and destroys all data for an app
  93. if [[ "$command" = "uninstall" ]]; then
  94. echo "Removing images for app ${app}..."
  95. compose "${app}" down --rmi all --remove-orphans
  96. echo "Deleting app data for app ${app}..."
  97. if [[ -d "${app_data_dir}" ]]; then
  98. rm -rf "${app_data_dir}"
  99. fi
  100. echo "Successfully uninstalled app ${app}"
  101. exit
  102. fi
  103. # Stops an installed app
  104. if [[ "$command" = "stop" ]]; then
  105. echo "Stopping app ${app}..."
  106. compose "${app}" rm --force --stop
  107. exit
  108. fi
  109. # Starts an installed app
  110. if [[ "$command" = "start" ]]; then
  111. echo "Starting app ${app}..."
  112. compose "${app}" up --detach
  113. exit
  114. fi
  115. # Passes all arguments to docker-compose
  116. if [[ "$command" = "compose" ]]; then
  117. compose "${app}" ${args}
  118. exit
  119. fi
  120. # If we get here it means no valid command was supplied
  121. # Show help and exit
  122. show_help
  123. exit 1