mirror of
https://github.com/kiddin9/op-packages.git
synced 2026-11-18 06:23:02 +08:00
544 lines
18 KiB
Bash
Executable File
544 lines
18 KiB
Bash
Executable File
#!/bin/sh
|
|
|
|
. /usr/share/libubox/jshn.sh
|
|
|
|
readonly state_dir='/var/run/argon-update'
|
|
readonly cache_file='/var/run/argon-update/release.json'
|
|
readonly sources_file='/usr/share/argon/update-sources.json'
|
|
readonly repository='jerrykuku/luci-theme-argon'
|
|
readonly i18n_dir='/usr/lib/lua/luci/i18n'
|
|
|
|
wget_bin=/usr/libexec/wget-ssl
|
|
[ -x "$wget_bin" ] || wget_bin="$(command -v uclient-fetch || command -v wget)"
|
|
|
|
opkg_query() (
|
|
# OPKG locks its database even for status and comparison commands. Keep our
|
|
# RPC reads from racing the worker's version checks against that native lock.
|
|
prepare_state || exit 1
|
|
exec 6>"$state_dir/package-query.lock"
|
|
flock -w 2 6 || exit 1
|
|
opkg "$@"
|
|
)
|
|
|
|
installed_version() {
|
|
current_version=''
|
|
package_manager=''
|
|
if command -v apk >/dev/null 2>&1; then
|
|
current_version="$(apk list --installed luci-theme-argon 2>/dev/null |
|
|
awk '$1 ~ /^luci-theme-argon-/ { sub(/^luci-theme-argon-/, "", $1); print $1; exit }')"
|
|
[ -z "$current_version" ] || package_manager=apk
|
|
fi
|
|
if [ -z "$current_version" ] && command -v opkg >/dev/null 2>&1; then
|
|
current_version="$(opkg_query status luci-theme-argon 2>/dev/null | awk '
|
|
/^Version: / { version=$2 }
|
|
/^Status: .* installed$/ { installed=1 }
|
|
END { if (installed) print version }')"
|
|
[ -z "$current_version" ] || package_manager=opkg
|
|
fi
|
|
}
|
|
|
|
valid_tag() {
|
|
[ "${#1}" -le 48 ] && printf '%s\n' "$1" | grep -Eq '^v?[0-9]+(\.[0-9]+){1,3}$'
|
|
}
|
|
|
|
read_cache() {
|
|
[ -f "$cache_file" ] && [ ! -L "$cache_file" ] &&
|
|
[ "$(wc -c < "$cache_file")" -le 1048576 ] || return 1
|
|
json_load_file "$cache_file" || return 1
|
|
json_get_var schema schema
|
|
json_get_var cached_repository repository
|
|
json_get_var tag tag
|
|
json_get_var checked_at checked_at
|
|
[ "$schema" = 1 ] && [ "$cached_repository" = "$repository" ] &&
|
|
valid_tag "$tag" && printf '%s\n' "$checked_at" | grep -Eq '^[0-9]{1,11}$'
|
|
}
|
|
|
|
valid_source() {
|
|
[ "${#1}" -le 512 ] && printf '%s\n' "$1" |
|
|
grep -Eq '^https://[A-Za-z0-9]([A-Za-z0-9.-]*[A-Za-z0-9])?(/[A-Za-z0-9._~-]+)+$'
|
|
}
|
|
|
|
load_sources() {
|
|
primary_url=''
|
|
fallback_url='https://jerrykuku.github.io/luci-theme-argon/updates/stable.json'
|
|
if [ -f "$sources_file" ] && [ ! -L "$sources_file" ] &&
|
|
[ "$(wc -c < "$sources_file")" -le 4096 ] && json_load_file "$sources_file"; then
|
|
json_get_var configured_primary primary
|
|
json_get_var configured_fallback fallback
|
|
valid_source "$configured_primary" && primary_url="$configured_primary"
|
|
valid_source "$configured_fallback" && fallback_url="$configured_fallback"
|
|
fi
|
|
}
|
|
|
|
valid_language() {
|
|
printf '%s\n' "$1" | grep -Eq '^[a-z]{2,3}(-[a-z0-9]{2,8})?$'
|
|
}
|
|
|
|
valid_translation_asset() {
|
|
[ "$1" = "$4" ] && printf '%s\n' "$2" | grep -Eq '^[a-f0-9]{64}$' &&
|
|
printf '%s\n' "$3" | grep -Eq '^[1-9][0-9]{0,6}$' && [ "$3" -le 1048576 ]
|
|
}
|
|
|
|
translation_languages() (
|
|
json_get_type kind translations
|
|
[ -n "$kind" ] || exit 0
|
|
[ "$kind" = object ] && json_select translations || exit 1
|
|
json_get_keys language_keys
|
|
count=0
|
|
for language_key in $language_keys; do
|
|
# jshn's reader converts hyphens to underscores. Recover the package
|
|
# suffix, then require filenames that exactly match that language.
|
|
language="$(printf '%s' "$language_key" | tr '_' '-')"
|
|
count=$((count + 1))
|
|
[ "$count" -le 32 ] && valid_language "$language" || exit 1
|
|
json_get_type kind "$language_key"
|
|
[ "$kind" = object ] && json_select "$language_key" || exit 1
|
|
for kind in ipk apk; do
|
|
json_get_var file "$kind"
|
|
json_get_var checksum "${kind}_sha256"
|
|
json_get_var bytes "${kind}_size"
|
|
case "$kind" in
|
|
ipk) expected="luci-i18n-argon-config-${language}_v${ipk#luci-theme-argon_}" ;;
|
|
apk) expected="luci-i18n-argon-config-${language}-${apk#luci-theme-argon-}" ;;
|
|
esac
|
|
valid_translation_asset "$file" "$checksum" "$bytes" "$expected" || exit 1
|
|
done
|
|
json_select ..
|
|
printf '%s\n' "$language"
|
|
done
|
|
)
|
|
|
|
read_manifest_fields() {
|
|
json_load_file "$1" || return 1
|
|
json_get_var schema schema
|
|
json_get_var manifest_repository repository
|
|
json_get_var tag tag
|
|
[ "$schema" = 1 ] && [ "$manifest_repository" = "$repository" ] && valid_tag "$tag" || return 1
|
|
# Both formats must be complete. Never combine fields from different sources.
|
|
package_manager=apk
|
|
select_package || return 1
|
|
package_manager=opkg
|
|
select_package || return 1
|
|
for field in ipk apk ipk_sha256 apk_sha256 ipk_size apk_size published_at notes; do
|
|
json_get_var "$field" "$field"
|
|
done
|
|
printf '%s\n' "$published_at" | grep -Eq '^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$' || return 1
|
|
[ "${#notes}" -le 131072 ] || return 1
|
|
languages="$(translation_languages)" || return 1
|
|
}
|
|
|
|
normalize_manifest() {
|
|
local manifest_namespace language field
|
|
json_set_namespace manifest_input manifest_namespace
|
|
if ! read_manifest_fields "$1"; then
|
|
json_set_namespace "$manifest_namespace"
|
|
return 1
|
|
fi
|
|
json_set_namespace "$manifest_namespace"
|
|
# Copy only the contract fields, not remote status, URLs or local timestamps.
|
|
json_init
|
|
json_add_int schema 1
|
|
json_add_string repository "$repository"
|
|
json_add_string tag "$tag"
|
|
json_add_string published_at "$published_at"
|
|
json_add_string notes "$notes"
|
|
json_add_string ipk "$ipk"
|
|
json_add_string apk "$apk"
|
|
json_add_string ipk_sha256 "$ipk_sha256"
|
|
json_add_string apk_sha256 "$apk_sha256"
|
|
json_add_int ipk_size "$ipk_size"
|
|
json_add_int apk_size "$apk_size"
|
|
if [ -n "$languages" ]; then
|
|
json_add_object translations
|
|
for language in $languages; do
|
|
json_set_namespace manifest_input
|
|
json_select translations
|
|
json_select "$language"
|
|
for field in ipk apk ipk_sha256 apk_sha256 ipk_size apk_size; do
|
|
json_get_var "translation_$field" "$field"
|
|
done
|
|
json_select ..
|
|
json_select ..
|
|
json_set_namespace "$manifest_namespace"
|
|
json_add_object "$language"
|
|
json_add_string ipk "$translation_ipk"
|
|
json_add_string apk "$translation_apk"
|
|
json_add_string ipk_sha256 "$translation_ipk_sha256"
|
|
json_add_string apk_sha256 "$translation_apk_sha256"
|
|
json_add_int ipk_size "$translation_ipk_size"
|
|
json_add_int apk_size "$translation_apk_size"
|
|
json_close_object
|
|
done
|
|
json_close_object
|
|
fi
|
|
json_add_int checked_at "$(date +%s)"
|
|
}
|
|
|
|
check_failure() {
|
|
read_cache || json_init
|
|
json_add_int retry_at "$retry_at"
|
|
respond 1
|
|
}
|
|
|
|
check_updates() {
|
|
prepare_state || { fail 1; return; }
|
|
# Separate from the installation lock. Concurrent tabs reuse the first check.
|
|
exec 7>"$state_dir/check.lock"
|
|
flock -w 15 7 || { retry_at="$(($(date +%s) + 5))"; check_failure; return; }
|
|
now="$(date +%s)"
|
|
if read_cache && [ "$now" -ge "$checked_at" ] && [ "$((now - checked_at))" -lt 300 ]; then
|
|
respond 0
|
|
return
|
|
fi
|
|
retry_at=0
|
|
if [ -f "$state_dir/retry_at" ] && [ ! -L "$state_dir/retry_at" ] &&
|
|
[ "$(wc -c < "$state_dir/retry_at")" -le 12 ]; then
|
|
retry_at="$(cat "$state_dir/retry_at")"
|
|
fi
|
|
if printf '%s\n' "$retry_at" | grep -Eq '^[0-9]{1,11}$' &&
|
|
[ "$retry_at" -gt "$now" ] && [ "$((retry_at - now))" -le 60 ]; then
|
|
check_failure
|
|
return
|
|
fi
|
|
load_sources
|
|
work="$(mktemp -d "$state_dir/check.XXXXXX")" || { fail 1; return; }
|
|
trap 'rm -rf "$work"' EXIT
|
|
trap 'exit 1' HUP INT TERM
|
|
for source_url in "$primary_url" "$fallback_url"; do
|
|
[ -n "$source_url" ] || continue
|
|
# The two attempts together stay below rpcd's request timeout.
|
|
if (ulimit -f 2048; timeout -k 1 5 "$wget_bin" -T 4 -q -U 'luci-theme-argon' \
|
|
-O "$work/manifest.json" "$source_url") 2>/dev/null &&
|
|
[ "$(wc -c < "$work/manifest.json")" -le 1048576 ] && normalize_manifest "$work/manifest.json"; then
|
|
json_dump > "$work/cache.json"
|
|
[ ! -L "$cache_file" ] && mv -f "$work/cache.json" "$cache_file" 2>/dev/null || :
|
|
rm -f "$state_dir/retry_at"
|
|
respond 0
|
|
return
|
|
fi
|
|
[ "$primary_url" != "$fallback_url" ] || break
|
|
done
|
|
retry_at="$(($(date +%s) + 60))"
|
|
printf '%s\n' "$retry_at" > "$state_dir/retry_at"
|
|
check_failure
|
|
}
|
|
|
|
select_package() {
|
|
case "$package_manager" in apk) format=apk ;; opkg) format=ipk ;; *) return 1 ;; esac
|
|
json_get_var package "$format"
|
|
json_get_var digest "${format}_sha256"
|
|
json_get_var size "${format}_size"
|
|
json_get_var tag tag
|
|
valid_tag "$tag" || return 1
|
|
base="${tag#v}"
|
|
case "$package_manager:$package" in
|
|
apk:luci-theme-argon-"$base"-r*.apk)
|
|
target_version="${package#luci-theme-argon-}"
|
|
target_version="${target_version%.apk}"
|
|
revision="${target_version#"$base"-r}" ;;
|
|
opkg:luci-theme-argon_"$base"*_all.ipk)
|
|
target_version="${package#luci-theme-argon_}"
|
|
target_version="${target_version%_all.ipk}"
|
|
case "$target_version" in "$base") revision=1 ;; "$base"-*) revision="${target_version#"$base"-}" ;; *) return 1 ;; esac ;;
|
|
*) return 1 ;;
|
|
esac
|
|
printf '%s\n' "$revision" | grep -Eq '^[0-9]{1,8}$' &&
|
|
printf '%s\n' "$digest" | grep -Eq '^[a-f0-9]{64}$' &&
|
|
printf '%s\n' "$size" | grep -Eq '^[1-9][0-9]{0,7}$' &&
|
|
[ "$size" -gt 0 ] && [ "$size" -le 8388608 ]
|
|
}
|
|
|
|
installed_translations() {
|
|
{
|
|
for catalog in "$i18n_dir"/argon-config.*.lmo "$i18n_dir"/argon.*.lmo; do
|
|
[ -f "$catalog" ] && [ ! -L "$catalog" ] || continue
|
|
language="${catalog##*/}"
|
|
language="${language#*.}"
|
|
printf '%s\n' "${language%.lmo}"
|
|
done
|
|
case "$package_manager" in
|
|
apk) apk list --installed 'luci-i18n-argon-config-*' 2>/dev/null |
|
|
sed -n 's/^luci-i18n-argon-config-\([a-z][a-z0-9-]*\)-[0-9].*/\1/p' ;;
|
|
opkg) opkg_query list-installed 'luci-i18n-argon-config-*' 2>/dev/null |
|
|
awk '$1 ~ /^luci-i18n-argon-config-/ { sub(/^luci-i18n-argon-config-/, "", $1); print $1 }' ;;
|
|
esac
|
|
} | sort -u | while IFS= read -r language; do
|
|
valid_language "$language" && printf '%s\n' "$language"
|
|
done
|
|
}
|
|
|
|
# Emit a private, validated download plan. Never install the theme first and
|
|
# discover afterwards that the language package is missing or corrupt.
|
|
package_plan() (
|
|
printf '%s %s %s\n' "$package" "$digest" "$size"
|
|
wanted="$(installed_translations)"
|
|
configured="$(uci -q get luci.main.lang 2>/dev/null | tr 'A-Z_' 'a-z-')"
|
|
case "$configured" in zh-cn|zh-hans) configured=zh-cn ;; zh-tw|zh-hant) configured=zh-tw ;; nb-no) configured=no ;; esac
|
|
if ! json_select translations; then
|
|
[ -z "$wanted" ]
|
|
exit $?
|
|
fi
|
|
if valid_language "$configured"; then
|
|
json_get_type kind "$configured"
|
|
[ "$kind" != object ] || wanted="$(printf '%s\n%s\n' "$wanted" "$configured" | sort -u)"
|
|
fi
|
|
for language in $wanted; do
|
|
json_select "$language" || exit 1
|
|
json_get_var file "$format"
|
|
json_get_var checksum "${format}_sha256"
|
|
json_get_var bytes "${format}_size"
|
|
case "$format" in
|
|
ipk) expected="luci-i18n-argon-config-${language}_v${target_version}_all.ipk" ;;
|
|
apk) expected="luci-i18n-argon-config-${language}-${target_version}.apk" ;;
|
|
esac
|
|
valid_translation_asset "$file" "$checksum" "$bytes" "$expected" || exit 1
|
|
printf '%s %s %s\n' "$file" "$checksum" "$bytes"
|
|
json_select ..
|
|
done
|
|
)
|
|
|
|
verify_translation_versions() {
|
|
local file checksum bytes name expected actual
|
|
while read -r file checksum bytes; do
|
|
case "$file" in luci-i18n-argon-config-*) ;; *) continue ;; esac
|
|
case "$package_manager" in
|
|
apk)
|
|
name="${file%-${target_version}.apk}"
|
|
expected="$target_version"
|
|
actual="$(apk list --installed "$name" 2>/dev/null | awk -v prefix="$name-" '
|
|
index($1, prefix) == 1 { print substr($1, length(prefix) + 1); exit }')" ;;
|
|
opkg)
|
|
name="${file%_v${target_version}_all.ipk}"
|
|
expected="v$target_version"
|
|
actual="$(opkg_query status "$name" 2>/dev/null | awk '
|
|
/^Version: / { version=$2 }
|
|
/^Status: .* installed$/ { installed=1 }
|
|
END { if (installed) print version }')" ;;
|
|
esac
|
|
[ "$actual" = "$expected" ] || return 1
|
|
done <<EOF
|
|
$1
|
|
EOF
|
|
}
|
|
|
|
is_upgrade() {
|
|
# Reject unknown/custom version syntax before asking the native comparator.
|
|
printf '%s\n' "$current_version" | grep -Eq '^[0-9]+(\.[0-9]+){1,3}((~|_)(alpha|beta|rc)[0-9]+|-(alpha|beta|rc)\.[0-9]+)?(-r?[0-9]+)?$' || return 1
|
|
case "$package_manager" in
|
|
apk) [ "$(apk version -t "$current_version" "$target_version" 2>/dev/null)" = '<' ] ;;
|
|
opkg) opkg_query compare-versions "$current_version" '<<' "$target_version" ;;
|
|
*) return 1 ;;
|
|
esac
|
|
}
|
|
|
|
prepare_state() {
|
|
[ ! -L "$state_dir" ] && mkdir -p -m 700 "$state_dir" && [ ! -L "$state_dir" ] &&
|
|
command -v flock >/dev/null 2>&1 && command -v sha256sum >/dev/null 2>&1 && command -v timeout >/dev/null 2>&1
|
|
}
|
|
|
|
add_status() {
|
|
# Keep release data in the caller's namespace while loading the small job file.
|
|
json_set_namespace upgrade_status previous_namespace
|
|
json_init
|
|
phase=idle
|
|
job_error=''
|
|
job_version=''
|
|
if [ -f "$state_dir/status.json" ] && [ ! -L "$state_dir/status.json" ] &&
|
|
[ "$(wc -c < "$state_dir/status.json")" -le 4096 ] && json_load_file "$state_dir/status.json"; then
|
|
json_get_var phase phase
|
|
json_get_var job_error error
|
|
json_get_var job_version version
|
|
fi
|
|
active=0
|
|
case "$phase" in
|
|
starting|downloading|verifying|installing)
|
|
# A kernel lock survives the initiating RPC and is released on worker exit.
|
|
if command -v flock >/dev/null 2>&1; then
|
|
exec 8>"$state_dir/lock"
|
|
if flock -n 8; then
|
|
# The worker may have finished between our snapshot and lock probe.
|
|
# Read its final state while holding the lock against a new start.
|
|
json_load_file "$state_dir/status.json"
|
|
json_get_var phase phase
|
|
json_get_var job_error error
|
|
json_get_var job_version version
|
|
case "$phase" in starting|downloading|verifying|installing) phase=failed; job_error=interrupted ;; esac
|
|
else
|
|
active=1
|
|
fi
|
|
exec 8>&-
|
|
else
|
|
phase=failed
|
|
job_error=interrupted
|
|
fi ;;
|
|
esac
|
|
json_set_namespace "$previous_namespace"
|
|
json_add_object upgrade
|
|
json_add_string phase "$phase"
|
|
json_add_string error "$job_error"
|
|
json_add_string version "$job_version"
|
|
json_add_boolean active "$active"
|
|
if [ -f "$state_dir/log" ] && [ ! -L "$state_dir/log" ]; then
|
|
json_add_string log "$(tail -c 8192 "$state_dir/log")"
|
|
fi
|
|
json_close_object
|
|
}
|
|
|
|
respond() {
|
|
add_status
|
|
# Read after the job snapshot so a completed job reports its installed version.
|
|
installed_version
|
|
can_upgrade=0
|
|
now="$(date +%s)"
|
|
json_get_var checked_at checked_at
|
|
if [ "$1" = 0 ] && printf '%s\n' "$checked_at" | grep -Eq '^[0-9]{1,11}$' &&
|
|
[ "$now" -ge "$checked_at" ] && [ "$((now - checked_at))" -lt 300 ] &&
|
|
select_package && is_upgrade && package_plan >/dev/null && command -v flock >/dev/null 2>&1 && command -v sha256sum >/dev/null 2>&1 && command -v timeout >/dev/null 2>&1; then
|
|
can_upgrade=1
|
|
fi
|
|
json_add_int result "$1"
|
|
json_add_string current_version "$current_version"
|
|
json_add_string package_manager "$package_manager"
|
|
json_add_boolean can_upgrade "$can_upgrade"
|
|
json_dump
|
|
}
|
|
|
|
fail() {
|
|
json_init
|
|
respond "$1"
|
|
}
|
|
|
|
write_status() {
|
|
json_init
|
|
json_add_string phase "$1"
|
|
json_add_string error "${2:-}"
|
|
json_add_string version "$target_version"
|
|
json_dump > "$state_dir/status.$$" && mv -f "$state_dir/status.$$" "$state_dir/status.json"
|
|
}
|
|
|
|
run_upgrade() {
|
|
local plan file checksum bytes total_size
|
|
# Run a private copy, since installing the theme replaces this RPC script.
|
|
work="${0%/*}"
|
|
case "$work" in "$state_dir"/job.??????) ;; *) return 1 ;; esac
|
|
[ -d "$work" ] && [ ! -L "$work" ] && [ -f "$work/release.json" ] || return 1
|
|
trap 'rm -rf "$work"' EXIT
|
|
trap 'write_status failed interrupted; exit 1' INT TERM
|
|
json_load_file "$work/release.json" && select_package || { write_status failed metadata; return; }
|
|
installed_version
|
|
is_upgrade || { write_status failed version; return; }
|
|
plan="$(package_plan)" || { write_status failed metadata; return; }
|
|
total_size="$(printf '%s\n' "$plan" | awk '{ size += $3 } END { print size }')"
|
|
# Leave room for download metadata and unpacking. The package manager also
|
|
# performs its own dependency and storage checks; never force those checks.
|
|
tmp_free="$(df -Pk "$state_dir" | awk 'END { print $4 }')"
|
|
root_path=/
|
|
[ ! -d /overlay ] || root_path=/overlay
|
|
root_free="$(df -Pk "$root_path" | awk 'END { print $4 }')"
|
|
if [ "${tmp_free:-0}" -lt "$((total_size / 1024 + 2048))" ] ||
|
|
[ "${root_free:-0}" -lt "$((total_size * 3 / 1024 + 1024))" ]; then
|
|
write_status failed space
|
|
return
|
|
fi
|
|
write_status downloading
|
|
set --
|
|
while read -r file checksum bytes; do
|
|
url="https://github.com/jerrykuku/luci-theme-argon/releases/download/$tag/$file"
|
|
(ulimit -f 16384; timeout -k 2 90 "$wget_bin" -T 20 -q -U 'luci-theme-argon' \
|
|
-O "$work/$file" "$url") 2>/dev/null || { write_status failed network; return; }
|
|
set -- "$@" "$work/$file"
|
|
done <<EOF
|
|
$plan
|
|
EOF
|
|
write_status verifying
|
|
while read -r file checksum bytes; do
|
|
if [ "$(wc -c < "$work/$file")" -ne "$bytes" ] ||
|
|
[ "$(sha256sum "$work/$file" | awk '{ print $1 }')" != "$checksum" ]; then
|
|
write_status failed checksum
|
|
return
|
|
fi
|
|
done <<EOF
|
|
$plan
|
|
EOF
|
|
# Recheck immediately before modifying the package database.
|
|
installed_version
|
|
is_upgrade || { write_status failed version; return; }
|
|
write_status installing
|
|
# Bound captured output without imposing a file-size limit on the installer.
|
|
(
|
|
case "$package_manager" in
|
|
apk) apk add --allow-untrusted "$@" ;;
|
|
opkg) opkg install "$@" ;;
|
|
esac
|
|
printf '%s\n' "$?" > "$work/exit"
|
|
) 2>&1 | tail -c 8192 > "$state_dir/log"
|
|
if [ "$(cat "$work/exit" 2>/dev/null)" != 0 ]; then
|
|
write_status failed install
|
|
return
|
|
fi
|
|
installed_version
|
|
if [ "$current_version" != "$target_version" ] || ! verify_translation_versions "$plan"; then
|
|
write_status failed install
|
|
return
|
|
fi
|
|
write_status complete
|
|
}
|
|
|
|
start_upgrade() {
|
|
read -r request
|
|
json_load "$request" || { fail 3; return; }
|
|
json_get_var requested_tag tag
|
|
json_get_var requested_package package
|
|
now="$(date +%s)"
|
|
read_cache && [ "$now" -ge "$checked_at" ] && [ "$((now - checked_at))" -lt 300 ] &&
|
|
select_package && [ "$tag" = "$requested_tag" ] && [ "$package" = "$requested_package" ] || { fail 3; return; }
|
|
is_upgrade || { fail 5; return; }
|
|
package_plan >/dev/null || { fail 3; return; }
|
|
prepare_state || { fail 1; return; }
|
|
exec 9>"$state_dir/lock"
|
|
flock -n 9 || { fail 4; return; }
|
|
work="$(mktemp -d "$state_dir/job.XXXXXX")" || { fail 1; return; }
|
|
if ! cp "$0" "$work/worker.sh" || ! json_dump > "$work/release.json"; then
|
|
rm -rf "$work"
|
|
fail 1
|
|
return
|
|
fi
|
|
: > "$state_dir/log"
|
|
write_status starting
|
|
# FD 9 is inherited by the worker, keeping the lock until all installation
|
|
# work finishes. Detach every stdio stream so rpcd can reply immediately.
|
|
(trap '' HUP; exec sh "$work/worker.sh" run "$package_manager") </dev/null >/dev/null 2>&1 &
|
|
json_init
|
|
respond 0
|
|
}
|
|
|
|
umask 077
|
|
case "$1" in
|
|
list)
|
|
printf '%s\n' '{"get":{},"check":{},"status":{},"start":{"tag":"String","package":"String"}}'
|
|
;;
|
|
run)
|
|
package_manager="$2"
|
|
run_upgrade
|
|
;;
|
|
call)
|
|
case "$2" in get|check|status|start) ;; *) exit 1 ;; esac
|
|
if [ "$2" = status ]; then
|
|
json_init
|
|
respond 0
|
|
exit 0
|
|
fi
|
|
if [ "$2" = start ]; then
|
|
installed_version
|
|
start_upgrade
|
|
exit 0
|
|
fi
|
|
if [ "$2" = get ]; then
|
|
read_cache || json_init
|
|
respond 0
|
|
exit 0
|
|
fi
|
|
check_updates
|
|
;;
|
|
esac
|