Skip to content

Commit 6c7dd20

Browse files
rcourtmanclaude
andcommitted
feat: restore robust installer self-update using GitHub API
- Added SHA-based version tracking to avoid GitHub CDN caching issues - Uses GitHub API to check latest commit SHA for the installer - Falls back to simple diff check if jq is not available - Updates embedded SHA when downloading new version - Much more reliable than simple file comparison This restores the update method from the original installer that properly handles GitHub's aggressive CDN caching. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 839628e commit 6c7dd20

File tree

1 file changed

+121
-21
lines changed

1 file changed

+121
-21
lines changed

scripts/install-pulse.sh

Lines changed: 121 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,24 @@ OLD_PULSE_DIR="/opt/pulse-proxmox"
99
PULSE_USER="pulse"
1010
SERVICE_NAME="pulse-monitor.service"
1111
REPO_BASE_URL="https://github.com/rcourtman/Pulse"
12+
SCRIPT_NAME="install-pulse.sh"
13+
SCRIPT_RAW_URL="https://raw.githubusercontent.com/rcourtman/Pulse/main/scripts/install-pulse.sh"
14+
CURRENT_SCRIPT_COMMIT_SHA="4348a3d"
1215

1316
MODE_UPDATE=""
1417
INSTALL_MODE=""
1518
SPECIFIED_VERSION_TAG=""
1619
TARGET_TAG=""
20+
INSTALLER_WAS_REEXECUTED=false
1721

1822
# Parse command line arguments
1923
while [[ "$#" -gt 0 ]]; do
2024
case $1 in
2125
--update) MODE_UPDATE="true"; shift ;;
26+
--installer-reexecuted)
27+
INSTALLER_WAS_REEXECUTED=true
28+
shift
29+
;;
2230
--version)
2331
if [[ -n "$2" ]] && [[ "$2" != --* ]]; then
2432
SPECIFIED_VERSION_TAG="$2"
@@ -59,38 +67,130 @@ check_root() {
5967

6068
# Self-update check
6169
self_update_check() {
62-
# Skip if running from pipe or no curl available
63-
if [ ! -t 0 ] || ! command -v curl &>/dev/null; then
70+
# Skip if running from pipe, in update mode, or already reexecuted
71+
if [ ! -t 0 ] || [ -n "$MODE_UPDATE" ] || [ "$INSTALLER_WAS_REEXECUTED" = "true" ]; then
6472
return 0
6573
fi
6674

67-
print_info "Checking for installer updates..."
75+
if ! command -v curl &>/dev/null; then
76+
print_warning "curl not found, skipping installer update check"
77+
return 0
78+
fi
79+
80+
# Install jq if needed for robust SHA checking
81+
if ! command -v jq &>/dev/null; then
82+
print_info "Installing jq for installer update check..."
83+
if command -v apt-get &>/dev/null; then
84+
apt-get update -qq >/dev/null 2>&1
85+
if apt-get install -y -qq jq >/dev/null 2>&1; then
86+
print_success "jq installed"
87+
else
88+
print_warning "Could not install jq, falling back to simple update check"
89+
# Fall back to simple diff check
90+
simple_update_check
91+
return $?
92+
fi
93+
else
94+
print_warning "Could not install jq, falling back to simple update check"
95+
simple_update_check
96+
return $?
97+
fi
98+
fi
99+
100+
print_info "Checking for installer updates (GitHub API)..."
68101

69-
local current_script="$0"
102+
local owner="rcourtman"
103+
local repo="Pulse"
104+
local script_path="scripts/install-pulse.sh"
105+
local branch="main"
106+
local api_url="https://api.github.com/repos/${owner}/${repo}/commits?path=${script_path}&sha=${branch}&per_page=1"
107+
108+
local latest_sha
109+
latest_sha=$(curl -sL -H "Accept: application/vnd.github.v3+json" "$api_url" | jq -r 'if type=="array" and length > 0 then .[0].sha else empty end')
110+
111+
if [ -z "$latest_sha" ] || [ "$latest_sha" = "null" ]; then
112+
print_warning "Could not check for updates via API"
113+
return 0
114+
fi
115+
116+
local current_sha="$CURRENT_SCRIPT_COMMIT_SHA"
117+
if [ -z "$current_sha" ]; then
118+
print_warning "Current version unknown, skipping update check"
119+
return 0
120+
fi
121+
122+
if [ "$latest_sha" != "$current_sha" ]; then
123+
print_warning "New installer version available (${latest_sha:0:7})"
124+
read -p "Update installer and restart? [Y/n]: " confirm
125+
126+
if [[ ! "$confirm" =~ ^[Nn]$ ]]; then
127+
print_info "Downloading new installer..."
128+
local temp_script="/tmp/${SCRIPT_NAME}.tmp"
129+
130+
if ! curl -sL "$SCRIPT_RAW_URL" -o "$temp_script"; then
131+
print_error "Failed to download new installer"
132+
rm -f "$temp_script"
133+
return 1
134+
fi
135+
136+
# Update the SHA in the downloaded script
137+
local updated_script="${temp_script}.updated"
138+
local sha_updated=false
139+
140+
while IFS= read -r line || [[ -n "$line" ]]; do
141+
if [[ "$line" == CURRENT_SCRIPT_COMMIT_SHA=* ]]; then
142+
echo "CURRENT_SCRIPT_COMMIT_SHA=\"${latest_sha:0:7}\"" >> "$updated_script"
143+
sha_updated=true
144+
else
145+
echo "$line" >> "$updated_script"
146+
fi
147+
done < "$temp_script"
148+
149+
if [ "$sha_updated" = false ]; then
150+
print_error "Failed to update version in new installer"
151+
rm -f "$temp_script" "$updated_script"
152+
return 1
153+
fi
154+
155+
# Replace current script
156+
if ! mv "$updated_script" "$0"; then
157+
print_error "Failed to update installer"
158+
rm -f "$temp_script" "$updated_script"
159+
return 1
160+
fi
161+
162+
chmod +x "$0"
163+
rm -f "$temp_script"
164+
165+
print_success "Installer updated to ${latest_sha:0:7}"
166+
print_info "Restarting..."
167+
exec "$0" --installer-reexecuted "$@"
168+
fi
169+
else
170+
print_success "Installer is up to date"
171+
fi
172+
}
173+
174+
# Simple fallback update check using diff
175+
simple_update_check() {
70176
local temp_script="/tmp/install-pulse-new.sh"
71-
local script_url="https://raw.githubusercontent.com/rcourtman/Pulse/main/scripts/install-pulse.sh"
72-
73-
# Download latest version
74-
if curl -sL "$script_url" -o "$temp_script" 2>/dev/null; then
75-
# Compare with current script
76-
if ! diff -q "$current_script" "$temp_script" >/dev/null 2>&1; then
77-
print_warning "A newer version of the installer is available"
78-
read -p "Update installer and restart? [Y/n]: " update_confirm
177+
178+
if curl -sL "$SCRIPT_RAW_URL" -o "$temp_script" 2>/dev/null; then
179+
if ! diff -q "$0" "$temp_script" >/dev/null 2>&1; then
180+
print_warning "New installer version might be available"
181+
read -p "Update installer and restart? [Y/n]: " confirm
79182

80-
if [[ ! "$update_confirm" =~ ^[Nn]$ ]]; then
81-
print_info "Updating installer..."
82-
cp "$temp_script" "$current_script"
83-
chmod +x "$current_script"
183+
if [[ ! "$confirm" =~ ^[Nn]$ ]]; then
184+
cp "$temp_script" "$0"
185+
chmod +x "$0"
84186
rm -f "$temp_script"
85-
86-
print_success "Installer updated, restarting..."
87-
exec "$current_script" "$@"
187+
print_success "Installer updated"
188+
exec "$0" "$@"
88189
fi
89-
else
90-
print_success "Installer is up to date"
91190
fi
92191
rm -f "$temp_script"
93192
fi
193+
return 0
94194
}
95195

96196
# Print welcome banner

0 commit comments

Comments
 (0)