Skip to content

Commit

Permalink
3rd Party App Repo Support
Browse files Browse the repository at this point in the history

Co-authored-by: Steven Briscoe <me@stevenbriscoe.com>
Co-authored-by: Mayank Chhabra <mayankchhabra9@gmail.com>
  • Loading branch information
3 people authored Oct 24, 2022
1 parent fa8b256 commit 773fa96
Show file tree
Hide file tree
Showing 8 changed files with 381 additions and 117 deletions.
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ services:
UPDATE_LOCK_FILE: "/statuses/update-in-progress"
BACKUP_STATUS_FILE: "/statuses/backup-status.json"
DEBUG_STATUS_FILE: "/statuses/debug-status.json"
REPO_UPDATE_STATUS_FILE: "/statuses/repo-update-status.json"
TOR_HIDDEN_SERVICE_DIR: "/var/lib/tor"
IS_UMBREL_OS: ${IS_UMBREL_OS:-"false"}
UMBREL_APP_REPO_URL: "https://github.com/getumbrel/umbrel-apps.git"
Expand Down
44 changes: 44 additions & 0 deletions events/triggers/repo
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env bash
set -euo pipefail

UMBREL_ROOT="$(readlink -f $(dirname "${BASH_SOURCE[0]}")/../..)"

updateStatus() {
local -r state="${1}"
local -r description="${2}"
local -r url="${3}"

cat <<EOF > "$UMBREL_ROOT"/statuses/repo-update-status.json
{"state": "${state}", "description": "${description}", "url": "${url}"}
EOF
}

signal="${1}"
command=${signal%%"-"*}
signal_file="${UMBREL_ROOT}/events/signals/repo-${command}"
if [[ ! -f "${signal_file}" ]]; then
exit
fi

url=$(cat "${signal_file}")
rm -f "${signal_file}"

updateStatus "running" "Updating local app repos" "${url}"

"${UMBREL_ROOT}/scripts/repo" "${command}" "${url}"

if [[ "${command}" == "add" ]]; then
"${UMBREL_ROOT}/scripts/repo" "update" "${url}"

# Check that the repo was cloned
repo_path=$("${UMBREL_ROOT}/scripts/repo" path "${url}")
if [[ -d "${repo_path}" ]]; then
updateStatus "success" "Successfully added: ${url}" "${url}"
else
updateStatus "error" "Failed to add: ${url}" "${url}"
fi
elif [[ "${command}" == "remove" ]]; then
updateStatus "success" "Successfully removed: ${url}" "${url}"
else
updateStatus "success" "" "${url}"
fi
7 changes: 7 additions & 0 deletions karen
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ fswatch -0 --event=PlatformSpecific $signal_dir | while read -d "" event; do
trigger="$trigger_dir/app"
args="${signal#$app_prefix}"
fi

app_prefix="repo-"
if [[ "$signal" == "$app_prefix"* ]]; then
# This is an repo signal, let the repo trigger handle it
trigger="$trigger_dir/repo"
args="${signal#$app_prefix}"
fi

if test -x "$trigger"; then
echo "karen is getting triggered!"
Expand Down
48 changes: 33 additions & 15 deletions scripts/app
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ VERSION="0.0.3"

UMBREL_ROOT="$(readlink -f $(dirname "${BASH_SOURCE[0]}")/..)"
USER_FILE="${UMBREL_ROOT}/db/user.json"
ACTIVE_APP_REPO_PATH=$($UMBREL_ROOT/scripts/repo path)

APP_PROXY_SERVICE_NAME="app_proxy"
REMOTE_TOR_ACCESS="false"
Expand Down Expand Up @@ -161,7 +160,10 @@ if [ -z ${2+x} ]; then
exit 1
else
app="$2"
app_repo_dir="${ACTIVE_APP_REPO_PATH}/${app}"

repo=$(cat "${USER_FILE}" 2> /dev/null | jq -r ".appOrigin.\"${app}\"" || true)
repo_path=$("${UMBREL_ROOT}/scripts/repo" "path" "${repo}")
app_repo_dir="${repo_path}/${app}"
app_data_dir="${UMBREL_ROOT}/app-data/${app}"

app_hidden_service_file="${UMBREL_ROOT}/tor/data/app-${app}/hostname"
Expand Down Expand Up @@ -252,6 +254,7 @@ compose() {
update_installed_apps() {
local -r action="${1}"
local -r app="${2}"
local -r repo="${3:-null}"

while ! (set -o noclobber; echo "$$" > "${USER_FILE}.lock") 2> /dev/null; do
echo "Waiting for JSON lock to be released for ${app} update..."
Expand All @@ -265,6 +268,13 @@ update_installed_apps() {
updated_json=$(cat "${USER_FILE}" | jq ".installedApps |= (. ${operator} [\"${app}\"] | unique)")
echo "${updated_json}" > "${USER_FILE}"

if [[ "${action}" == "add" ]]; then
updated_json=$(cat "${USER_FILE}" | jq ".appOrigin |= (. ${operator} {\"${app}\":\"${repo}\"})")
else
updated_json=$(cat "${USER_FILE}" | jq "del(.appOrigin.\"${app}\")")
fi
echo "${updated_json}" > "${USER_FILE}"

rm -f "${USER_FILE}.lock"
}

Expand Down Expand Up @@ -377,18 +387,20 @@ must_be_installed_guard() {
fi
}

# Check that the app exists in the local app repo
must_exist_in_app_repo_guard() {
if [[ ! -d "${app_repo_dir}" ]]; then
>&2 echo "Error: \"${app}\" not found in app repo"
# Pulls down images for an app and starts it
if [[ "$command" = "install" ]]; then

repo=$("${UMBREL_ROOT}/scripts/repo" "locate" "${app}")

if [[ -z "${repo}" ]]; then
>&2 echo "Error: \"${app}\" not found in any local app repo"
exit 1
fi
}

# Pulls down images for an app and starts it
if [[ "$command" = "install" ]]; then
must_exist_in_app_repo_guard
app_repo_dir=$("${UMBREL_ROOT}/scripts/repo" "path" "${repo}")
app_repo_dir="${app_repo_dir}/${app}"

echo "Installing '${app}' from: ${repo}"

echo "Setting up data dir for app ${app}..."
mkdir -p "${app_data_dir}"
Expand All @@ -413,7 +425,7 @@ if [[ "$command" = "install" ]]; then
fi

echo "Saving app ${app} in DB..."
update_installed_apps add "${app}"
update_installed_apps add "${app}" "${repo}"

execute_hook "${app}" "post-install"

Expand Down Expand Up @@ -466,6 +478,8 @@ fi
# Stops an installed app
if [[ "$command" = "stop" ]]; then

must_be_installed_guard

execute_hook "${app}" "pre-stop"

echo "Stopping app ${app}..."
Expand Down Expand Up @@ -512,14 +526,18 @@ if [[ "$command" = "update" ]]; then

must_be_installed_guard

must_exist_in_app_repo_guard
# Check that the app folder still exists
# Within the associated local app repo
if [[ ! -d "${app_repo_dir}" ]]; then
>&2 echo "Error: Local app repo no longer exists for ${app}"
exit 1
fi

echo "Updating '${app}' from: ${repo}"
# Save current images to clean up later
app_compose_file="${app_data_dir}/docker-compose.yml"
app_old_images=$(yq e '.services | map(select(.image != null)) | .[].image' "${app_compose_file}")

echo "Updating app ${app}..."

if [[ "$*" != *"--skip-stop"* ]]; then
"${0}" "stop" "${app}"
fi
Expand Down
Loading

0 comments on commit 773fa96

Please sign in to comment.