123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- #!/usr/bin/env bash
- set -euo pipefail
- # use greadlink instead of readlink on osx
- if [[ "$(uname)" == "Darwin" ]]; then
- rdlk=greadlink
- else
- rdlk=readlink
- fi
- ROOT_FOLDER="$($rdlk -f $(dirname "${BASH_SOURCE[0]}")/..)"
- STATE_FOLDER="${ROOT_FOLDER}/state"
- show_help() {
- cat << EOF
- app 0.0.1
- CLI for managing Tipi apps
- Usage: app <command> <app> [<arguments>]
- Commands:
- install Pulls down images for an app and starts it
- uninstall Removes images and destroys all data for an app
- stop Stops an installed app
- start Starts an installed app
- compose Passes all arguments to docker-compose
- ls-installed Lists installed apps
- EOF
- }
- # Get field from json file
- function get_json_field() {
- local json_file="$1"
- local field="$2"
- echo $(jq -r ".${field}" "${json_file}")
- }
- list_installed_apps() {
- str=$(get_json_field ${STATE_FOLDER}/apps.json installed)
- echo $str
- }
- if [ -z ${1+x} ]; then
- command=""
- else
- command="$1"
- fi
- # Lists installed apps
- if [[ "$command" = "ls-installed" ]]; then
- list_installed_apps
- exit
- fi
- if [ -z ${2+x} ]; then
- show_help
- exit 1
- else
- app="$2"
- root_folder_host="$3"
- app_dir="${ROOT_FOLDER}/apps/${app}"
- app_data_dir="${ROOT_FOLDER}/app-data/${app}"
- if [[ -z "${app}" ]] || [[ ! -d "${app_dir}" ]]; then
- echo "Error: \"${app}\" is not a valid app"
- exit 1
- fi
- if [[ -z "${root_folder_host}" ]]; then
- echo "Error: Root folder not provided"
- exit 1
- fi
- fi
- if [ -z ${3+x} ]; then
- args=""
- else
- args="${@:3}"
- fi
- compose() {
- local app="${1}"
- shift
- local architecture="$(uname -m)"
- if [[ "$architecture" == "aarch64" ]]; then
- architecture="arm64"
- fi
- # App data folder
- local env_file="${ROOT_FOLDER}/.env"
- local app_compose_file="${app_dir}/docker-compose.yml"
- # Pick arm architecture if running on arm and if the app has a docker-compose.arm.yml file
- if [[ "$architecture" == "arm"* ]] && [[ -f "${app_dir}/docker-compose.arm.yml" ]]; then
- app_compose_file="${app_dir}/docker-compose.arm.yml"
- fi
- local common_compose_file="${ROOT_FOLDER}/apps/docker-compose.common.yml"
- local app_dir="${ROOT_FOLDER}/apps/${app}"
- # Vars to use in compose file
- export APP_DATA_DIR="${root_folder_host}/app-data/${app}"
- export APP_DIR="${app_dir}"
- export ROOT_FOLDER_HOST="${root_folder_host}"
- export ROOT_FOLDER="${ROOT_FOLDER}"
- # Docker-compose does not support multiple env files
- # --env-file "${env_file}" \
- docker-compose \
- --env-file "${ROOT_FOLDER}/app-data/${app}/app.env" \
- --project-name "${app}" \
- --file "${app_compose_file}" \
- --file "${common_compose_file}" \
- "${@}"
- }
- # Install new app
- if [[ "$command" = "install" ]]; then
- compose "${app}" pull
- # Copy default data dir to app data dir if it exists
- if [[ -d "${ROOT_FOLDER}/apps/${app}/data" ]]; then
- cp -r "${ROOT_FOLDER}/apps/${app}/data" "${app_data_dir}/data"
- fi
- # Remove all .gitkeep files from app data dir
- find "${app_data_dir}" -name ".gitkeep" -exec rm -f {} \;
- chown -R "1000:1000" "${app_data_dir}"
- compose "${app}" up -d
- exit
- fi
- # Removes images and destroys all data for an app
- if [[ "$command" = "uninstall" ]]; then
- echo "Removing images for app ${app}..."
- # compose "${app}" down --remove-orphans
- echo "Deleting app data for app ${app}..."
- if [[ -d "${app_data_dir}" ]]; then
- rm -rf "${app_data_dir}"
- fi
- echo "Successfully uninstalled app ${app}"
- exit
- fi
- # Stops an installed app
- if [[ "$command" = "stop" ]]; then
- echo "Stopping app ${app}..."
- compose "${app}" down --remove-orphans --rmi all
- compose "${app}" rm --force --stop
- exit
- fi
- # Starts an installed app
- if [[ "$command" = "start" ]]; then
- echo "Starting app ${app}..."
- compose "${app}" pull
- compose "${app}" up --detach
- exit
- fi
- # Passes all arguments to docker-compose
- if [[ "$command" = "compose" ]]; then
- compose "${app}" ${args}
- exit
- fi
- # If we get here it means no valid command was supplied
- # Show help and exit
- show_help
- exit 1
|