2022-03-29 20:40:04 +00:00
|
|
|
#!/usr/bin/env bash
|
2022-09-27 18:25:56 +00:00
|
|
|
echo "Starting app script"
|
|
|
|
|
|
|
|
source "${BASH_SOURCE%/*}/common.sh"
|
|
|
|
|
2022-03-29 20:40:04 +00:00
|
|
|
set -euo pipefail
|
|
|
|
|
2022-09-27 18:25:56 +00:00
|
|
|
ensure_pwd
|
2022-03-30 19:26:01 +00:00
|
|
|
|
2022-09-21 07:06:38 +00:00
|
|
|
ROOT_FOLDER="${PWD}"
|
2022-09-27 18:25:56 +00:00
|
|
|
STATE_FOLDER="${ROOT_FOLDER}/state"
|
2022-09-21 07:06:38 +00:00
|
|
|
ENV_FILE="${ROOT_FOLDER}/.env"
|
2022-03-29 20:40:04 +00:00
|
|
|
|
2022-09-21 07:06:38 +00:00
|
|
|
# Root folder in host system
|
|
|
|
ROOT_FOLDER_HOST=$(grep -v '^#' "${ENV_FILE}" | xargs -n 1 | grep ROOT_FOLDER_HOST | cut -d '=' -f2)
|
|
|
|
REPO_ID=$(grep -v '^#' "${ENV_FILE}" | xargs -n 1 | grep APPS_REPO_ID | cut -d '=' -f2)
|
2022-09-27 18:25:56 +00:00
|
|
|
STORAGE_PATH=$(grep -v '^#' "${ENV_FILE}" | xargs -n 1 | grep STORAGE_PATH | cut -d '=' -f2)
|
2022-03-29 20:40:04 +00:00
|
|
|
|
2022-09-27 18:25:56 +00:00
|
|
|
write_log "Running app script: ROOT_FOLDER=${ROOT_FOLDER}, ROOT_FOLDER_HOST=${ROOT_FOLDER_HOST}, REPO_ID=${REPO_ID}, STORAGE_PATH=${STORAGE_PATH}"
|
2022-03-29 20:40:04 +00:00
|
|
|
|
|
|
|
if [ -z ${1+x} ]; then
|
|
|
|
command=""
|
|
|
|
else
|
|
|
|
command="$1"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z ${2+x} ]; then
|
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
app="$2"
|
2022-08-09 18:44:07 +00:00
|
|
|
|
|
|
|
app_dir="${ROOT_FOLDER}/apps/${app}"
|
|
|
|
|
|
|
|
if [[ ! -d "${app_dir}" ]]; then
|
|
|
|
# copy from repo
|
|
|
|
echo "Copying app from repo"
|
|
|
|
mkdir -p "${app_dir}"
|
2022-09-21 07:06:38 +00:00
|
|
|
cp -r "${ROOT_FOLDER}/repos/${REPO_ID}/apps/${app}"/* "${app_dir}"
|
2022-08-09 18:44:07 +00:00
|
|
|
fi
|
|
|
|
|
2022-10-03 06:22:04 +00:00
|
|
|
app_data_dir="${STORAGE_PATH}/app-data/${app}"
|
2022-03-29 20:40:04 +00:00
|
|
|
|
|
|
|
if [[ -z "${app}" ]] || [[ ! -d "${app_dir}" ]]; then
|
|
|
|
echo "Error: \"${app}\" is not a valid app"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2023-03-08 17:53:16 +00:00
|
|
|
|
|
|
|
# Function below has been modified from Umbrel
|
|
|
|
# Required Notice: Copyright
|
|
|
|
# Umbrel (https://umbrel.com)
|
2022-03-29 20:40:04 +00:00
|
|
|
compose() {
|
|
|
|
local app="${1}"
|
|
|
|
shift
|
2022-05-13 06:47:18 +00:00
|
|
|
|
2022-09-21 21:18:30 +00:00
|
|
|
arch=$(uname -m)
|
|
|
|
local architecture="${arch}"
|
2022-05-13 06:47:18 +00:00
|
|
|
|
|
|
|
if [[ "$architecture" == "aarch64" ]]; then
|
|
|
|
architecture="arm64"
|
|
|
|
fi
|
|
|
|
|
2022-03-29 20:40:04 +00:00
|
|
|
# App data folder
|
|
|
|
local app_compose_file="${app_dir}/docker-compose.yml"
|
2022-05-13 06:47:18 +00:00
|
|
|
|
2022-05-13 06:53:35 +00:00
|
|
|
# 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
|
2022-05-13 06:47:18 +00:00
|
|
|
app_compose_file="${app_dir}/docker-compose.arm.yml"
|
2022-05-18 18:13:58 +00:00
|
|
|
fi
|
2022-05-13 06:47:18 +00:00
|
|
|
|
2022-09-27 18:25:56 +00:00
|
|
|
# Pick arm architecture if running on arm and if the app has a docker-compose.arm64.yml file
|
|
|
|
if [[ "$architecture" == "arm64" ]] && [[ -f "${app_dir}/docker-compose.arm64.yml" ]]; then
|
|
|
|
app_compose_file="${app_dir}/docker-compose.arm64.yml"
|
|
|
|
fi
|
|
|
|
|
2022-09-21 07:06:38 +00:00
|
|
|
local common_compose_file="${ROOT_FOLDER}/repos/${REPO_ID}/apps/docker-compose.common.yml"
|
2022-03-29 20:40:04 +00:00
|
|
|
|
2023-03-02 19:19:20 +00:00
|
|
|
local user_compose_file="${ROOT_FOLDER}/user-config/${app}/docker-compose.yml"
|
|
|
|
local user_compose_args=
|
|
|
|
if [[ -f ${user_compose_file} ]]; then
|
|
|
|
user_compose_args="--file ${user_compose_file}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
local user_env_file="${ROOT_FOLDER}/user-config/${app}/app.env"
|
|
|
|
local user_env_args=
|
|
|
|
if [[ -f ${user_env_file} ]]; then
|
|
|
|
user_env_args="--env-file ${user_env_file}"
|
|
|
|
fi
|
|
|
|
|
2022-03-29 20:40:04 +00:00
|
|
|
# Vars to use in compose file
|
2022-09-27 18:25:56 +00:00
|
|
|
export APP_DATA_DIR="${STORAGE_PATH}/app-data/${app}"
|
2022-09-21 07:06:38 +00:00
|
|
|
export ROOT_FOLDER_HOST="${ROOT_FOLDER_HOST}"
|
2022-03-29 20:40:04 +00:00
|
|
|
|
2022-09-27 18:25:56 +00:00
|
|
|
write_log "Running docker compose -f ${app_compose_file} -f ${common_compose_file} ${*}"
|
|
|
|
write_log "APP_DATA_DIR=${APP_DATA_DIR}"
|
|
|
|
write_log "ROOT_FOLDER_HOST=${ROOT_FOLDER_HOST}"
|
|
|
|
|
2022-09-19 13:18:24 +00:00
|
|
|
docker compose \
|
2022-09-21 07:06:38 +00:00
|
|
|
--env-file "${app_data_dir}/app.env" \
|
2023-03-02 19:19:20 +00:00
|
|
|
${user_env_args} \
|
2022-03-29 20:40:04 +00:00
|
|
|
--project-name "${app}" \
|
|
|
|
--file "${app_compose_file}" \
|
2022-04-07 08:34:18 +00:00
|
|
|
--file "${common_compose_file}" \
|
2023-03-02 19:19:20 +00:00
|
|
|
${user_compose_args} \
|
2022-03-29 20:40:04 +00:00
|
|
|
"${@}"
|
|
|
|
}
|
|
|
|
|
2022-11-05 21:28:16 +00:00
|
|
|
function ensure_permissions() {
|
|
|
|
local app="${1}"
|
|
|
|
|
|
|
|
# if app_data_dir/data does not exist, create it
|
|
|
|
if [[ ! -d "${app_data_dir}/data" ]]; then
|
|
|
|
mkdir -p "${app_data_dir}/data"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Check if app requires special uid and gid
|
|
|
|
if [[ -f "${app_dir}/config.json" ]]; then
|
|
|
|
uid=$(get_json_field "${app_dir}/config.json" uid)
|
|
|
|
gid=$(get_json_field "${app_dir}/config.json" gid)
|
|
|
|
|
|
|
|
write_log "App requires uid=${uid} and gid=${gid}"
|
|
|
|
|
|
|
|
if [[ "$uid" != "null" ]] && [[ "$gid" != "null" ]]; then
|
|
|
|
write_log "Setting uid and gid to ${uid}:${gid}"
|
|
|
|
if ! chown -R "${uid}:${gid}" "${app_data_dir}/data"; then
|
|
|
|
write_log "Failed to set uid and gid to ${uid}:${gid}"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Remove all .gitkeep files from app data dir
|
|
|
|
find "${app_data_dir}" -name ".gitkeep" -exec rm -f {} \;
|
|
|
|
|
|
|
|
chmod -R a+rwx "${app_data_dir}"
|
|
|
|
}
|
|
|
|
|
|
|
|
function install_app() {
|
|
|
|
local app="${1}"
|
|
|
|
|
2022-09-27 18:25:56 +00:00
|
|
|
# Write to file script.log
|
|
|
|
write_log "Installing app ${app}..."
|
|
|
|
|
2022-10-03 06:22:04 +00:00
|
|
|
if ! compose "${app}" pull; then
|
|
|
|
write_log "Failed to pull app ${app}"
|
|
|
|
exit 1
|
|
|
|
fi
|
2022-04-07 08:34:18 +00:00
|
|
|
|
2022-04-13 21:47:07 +00:00
|
|
|
# Copy default data dir to app data dir if it exists
|
2022-08-09 18:44:07 +00:00
|
|
|
if [[ -d "${app_dir}/data" ]]; then
|
|
|
|
cp -r "${app_dir}/data" "${app_data_dir}/data"
|
2022-04-13 21:47:07 +00:00
|
|
|
fi
|
2022-04-13 18:34:24 +00:00
|
|
|
|
2022-11-05 21:28:16 +00:00
|
|
|
ensure_permissions "${app}"
|
2022-05-17 16:54:04 +00:00
|
|
|
|
2022-10-03 06:22:04 +00:00
|
|
|
if ! compose "${app}" up -d; then
|
|
|
|
write_log "Failed to start app ${app}"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
exit 0
|
2022-11-05 21:28:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function start_app() {
|
|
|
|
local app="${1}"
|
|
|
|
|
|
|
|
write_log "Starting app ${app}..."
|
|
|
|
|
|
|
|
ensure_permissions "${app}"
|
|
|
|
|
2022-12-06 13:20:47 +00:00
|
|
|
# Pull images
|
|
|
|
if ! compose "${app}" pull; then
|
|
|
|
write_log "Failed to pull app ${app}"
|
|
|
|
fi
|
|
|
|
|
2023-03-30 06:11:54 +00:00
|
|
|
if ! compose "${app}" up --detach --force-recreate --remove-orphans; then
|
2022-11-05 21:28:16 +00:00
|
|
|
write_log "Failed to start app ${app}"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
|
|
|
|
function uninstall_app() {
|
|
|
|
local app="${1}"
|
2022-04-07 08:34:18 +00:00
|
|
|
|
2022-10-03 06:22:04 +00:00
|
|
|
write_log "Removing images for app ${app}..."
|
2022-05-17 16:54:04 +00:00
|
|
|
|
2022-10-03 06:22:04 +00:00
|
|
|
if ! compose "${app}" down --rmi all --remove-orphans; then
|
2022-10-19 06:46:25 +00:00
|
|
|
# just stop it if we can't remove the images
|
|
|
|
if ! compose "${app}" rm --force --stop; then
|
|
|
|
write_log "Failed to uninstall app ${app}"
|
|
|
|
exit 1
|
|
|
|
fi
|
2022-10-03 06:22:04 +00:00
|
|
|
fi
|
2022-03-29 20:40:04 +00:00
|
|
|
|
2022-10-03 06:22:04 +00:00
|
|
|
write_log "Deleting app data for app ${app}..."
|
2022-03-29 20:40:04 +00:00
|
|
|
if [[ -d "${app_data_dir}" ]]; then
|
2022-05-17 16:54:04 +00:00
|
|
|
rm -rf "${app_data_dir}"
|
2022-03-29 20:40:04 +00:00
|
|
|
fi
|
|
|
|
|
2022-08-09 18:44:07 +00:00
|
|
|
if [[ -d "${app_dir}" ]]; then
|
|
|
|
rm -rf "${app_dir}"
|
|
|
|
fi
|
|
|
|
|
2022-10-03 06:22:04 +00:00
|
|
|
write_log "Successfully uninstalled app ${app}"
|
2022-03-29 20:40:04 +00:00
|
|
|
exit
|
2022-11-05 21:28:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function update_app() {
|
|
|
|
local app="${1}"
|
2022-03-29 20:40:04 +00:00
|
|
|
|
2023-03-30 06:11:54 +00:00
|
|
|
if ! compose "${app}" up --detach --force-recreate --remove-orphans; then
|
2022-10-03 06:22:04 +00:00
|
|
|
write_log "Failed to update app ${app}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ! compose "${app}" down --rmi all --remove-orphans; then
|
2022-10-19 06:46:25 +00:00
|
|
|
# just stop it if we can't remove the images
|
|
|
|
if ! compose "${app}" rm --force --stop; then
|
2023-03-30 06:11:54 +00:00
|
|
|
write_log "Failed to update app ${app}"
|
2022-10-19 06:46:25 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
2022-10-03 06:22:04 +00:00
|
|
|
fi
|
2022-08-09 18:44:07 +00:00
|
|
|
|
|
|
|
# Remove app
|
|
|
|
if [[ -d "${app_dir}" ]]; then
|
|
|
|
rm -rf "${app_dir}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Copy app from repo
|
2022-09-21 07:06:38 +00:00
|
|
|
cp -r "${ROOT_FOLDER}/repos/${REPO_ID}/apps/${app}" "${app_dir}"
|
2022-08-09 18:44:07 +00:00
|
|
|
|
2022-11-05 21:28:16 +00:00
|
|
|
ensure_permissions "${app}"
|
|
|
|
|
2022-08-04 19:17:21 +00:00
|
|
|
compose "${app}" pull
|
2022-10-03 06:22:04 +00:00
|
|
|
exit 0
|
2022-11-05 21:28:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function stop_app() {
|
|
|
|
local app="${1}"
|
2022-08-04 19:17:21 +00:00
|
|
|
|
2022-10-03 06:22:04 +00:00
|
|
|
write_log "Stopping app ${app}..."
|
|
|
|
|
|
|
|
if ! compose "${app}" rm --force --stop; then
|
|
|
|
write_log "Failed to stop app ${app}"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
exit 0
|
2022-11-05 21:28:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Install new app
|
|
|
|
if [[ "$command" = "install" ]]; then
|
|
|
|
install_app "${app}"
|
2022-03-29 20:40:04 +00:00
|
|
|
fi
|
|
|
|
|
2022-11-05 21:28:16 +00:00
|
|
|
# Removes images and destroys all data for an app
|
|
|
|
if [[ "$command" = "uninstall" ]]; then
|
|
|
|
uninstall_app "${app}"
|
2022-03-29 20:40:04 +00:00
|
|
|
fi
|
|
|
|
|
2022-11-05 21:28:16 +00:00
|
|
|
# Update an app
|
|
|
|
if [[ "$command" = "update" ]]; then
|
|
|
|
update_app "${app}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Stops an installed app
|
|
|
|
if [[ "$command" = "stop" ]]; then
|
|
|
|
stop_app "${app}"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Starts an installed app
|
|
|
|
if [[ "$command" = "start" ]]; then
|
|
|
|
start_app "${app}"
|
2022-03-29 20:40:04 +00:00
|
|
|
fi
|
|
|
|
|
2022-10-19 06:46:25 +00:00
|
|
|
if [[ "$command" = "clean" ]]; then
|
|
|
|
# Remove all stopped containers and unused images
|
|
|
|
write_log "Cleaning up..."
|
|
|
|
docker system prune --all --force
|
|
|
|
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2022-05-18 18:13:58 +00:00
|
|
|
exit 1
|