Skip to content

Commit b836589

Browse files
crdantclaude
andcommitted
Add auto-download functionality to header.sh with fallback mode
- Implement automatic downloading of library files to header.sh installation directory - Add multi-source download support (main branch and feature branch URLs) - Include retry logic with proper error handling and verification - Provide graceful fallback mode with essential functions when downloads fail - Add comprehensive user messaging for troubleshooting download issues - Maintain backward compatibility - existing scripts continue working unchanged - Support both scenarios: remote auto-provisioning and local library files 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent f00df99 commit b836589

File tree

1 file changed

+151
-25
lines changed

1 file changed

+151
-25
lines changed

libs/header.sh

Lines changed: 151 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,43 +14,169 @@ LIBS_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
1414

1515
echo "Loading Replicated Field Labs libraries v${HEADER_VERSION}..."
1616

17-
# Load all Instruqt libraries in dependency order
18-
source "$LIBS_DIR/instruqt-bootstrap.sh" # Core initialization
19-
source "$LIBS_DIR/instruqt-files.sh" # File and directory management
20-
source "$LIBS_DIR/instruqt-ssh.sh" # SSH configuration
21-
source "$LIBS_DIR/instruqt-sessions.sh" # Tmux session management
22-
source "$LIBS_DIR/instruqt-config.sh" # Environment and credentials
23-
source "$LIBS_DIR/instruqt-services.sh" # Service and API management
24-
source "$LIBS_DIR/instruqt-apps.sh" # Application and release management
25-
26-
# Verify all libraries loaded successfully
27-
check_libraries() {
17+
# Download and cache all required library files
18+
download_libraries() {
19+
# Try multiple sources for library files
20+
local base_urls=(
21+
"https://raw.githubusercontent.com/replicatedhq/replicated-field-labs/main/libs"
22+
"https://raw.githubusercontent.com/replicatedhq/replicated-field-labs/refactor/crdant/builds-comprehensive-library/libs"
23+
)
24+
25+
local required_libraries=(
26+
"instruqt-bootstrap.sh"
27+
"instruqt-files.sh"
28+
"instruqt-ssh.sh"
29+
"instruqt-sessions.sh"
30+
"instruqt-config.sh"
31+
"instruqt-services.sh"
32+
"instruqt-apps.sh"
33+
"instruqt-all.sh"
34+
)
35+
36+
local downloaded_any=false
2837
local missing_libraries=()
2938

30-
# Check each library's loaded flag
31-
[[ "$INSTRUQT_BOOTSTRAP_LOADED" != "true" ]] && missing_libraries+=("instruqt-bootstrap")
32-
[[ "$INSTRUQT_FILES_LOADED" != "true" ]] && missing_libraries+=("instruqt-files")
33-
[[ "$INSTRUQT_SSH_LOADED" != "true" ]] && missing_libraries+=("instruqt-ssh")
34-
[[ "$INSTRUQT_SESSIONS_LOADED" != "true" ]] && missing_libraries+=("instruqt-sessions")
35-
[[ "$INSTRUQT_CONFIG_LOADED" != "true" ]] && missing_libraries+=("instruqt-config")
36-
[[ "$INSTRUQT_SERVICES_LOADED" != "true" ]] && missing_libraries+=("instruqt-services")
37-
[[ "$INSTRUQT_APPS_LOADED" != "true" ]] && missing_libraries+=("instruqt-apps")
39+
for lib in "${required_libraries[@]}"; do
40+
local lib_path="$LIBS_DIR/$lib"
41+
42+
# Check if library exists and is not empty
43+
if [[ ! -f "$lib_path" ]] || [[ ! -s "$lib_path" ]]; then
44+
echo "Downloading $lib..."
45+
46+
local download_success=false
47+
48+
# Try each base URL
49+
for base_url in "${base_urls[@]}"; do
50+
local attempts=0
51+
local max_attempts=2
52+
53+
while [[ $attempts -lt $max_attempts ]]; do
54+
if curl -fsSL "$base_url/$lib" -o "$lib_path.tmp"; then
55+
# Verify download was successful and not empty
56+
if [[ -s "$lib_path.tmp" ]]; then
57+
mv "$lib_path.tmp" "$lib_path"
58+
chmod +x "$lib_path"
59+
downloaded_any=true
60+
download_success=true
61+
echo " ✓ Downloaded $lib from $(basename "$base_url")"
62+
break 2 # Break out of both loops
63+
else
64+
rm -f "$lib_path.tmp"
65+
fi
66+
fi
67+
((attempts++))
68+
done
69+
done
70+
71+
if [[ "$download_success" == "false" ]]; then
72+
missing_libraries+=("$lib")
73+
echo " ✗ Failed to download $lib from any source"
74+
fi
75+
fi
76+
done
3877

3978
if [[ ${#missing_libraries[@]} -gt 0 ]]; then
40-
echo "ERROR: Failed to load libraries: ${missing_libraries[*]}"
79+
echo ""
80+
echo "WARNING: Could not download the following libraries:"
81+
for lib in "${missing_libraries[@]}"; do
82+
echo " - $lib"
83+
done
84+
echo ""
85+
echo "This may happen if:"
86+
echo " 1. The libraries haven't been pushed to the main branch yet"
87+
echo " 2. Network connectivity issues"
88+
echo " 3. The repository is private or inaccessible"
89+
echo ""
90+
echo "To resolve this:"
91+
echo " 1. Ensure all library files are in the same directory as header.sh, or"
92+
echo " 2. Wait for the changes to be merged to the main branch, or"
93+
echo " 3. Use the complete library directory from the repository"
94+
echo ""
4195
return 1
4296
fi
4397

98+
if [[ "$downloaded_any" == "true" ]]; then
99+
echo "Library downloads completed successfully"
100+
fi
101+
44102
return 0
45103
}
46104

47-
# Check that all libraries loaded
48-
if ! check_libraries; then
49-
echo "FATAL: Some libraries failed to load. Cannot continue."
50-
exit 1
105+
# Ensure all libraries are available
106+
if ! download_libraries; then
107+
echo "WARNING: Running in fallback mode with essential functions only"
108+
echo "For full functionality, ensure all library files are available locally"
109+
110+
# Provide essential fallback functions from the original header.sh
111+
get_username() {
112+
echo "${INSTRUQT_PARTICIPANT_ID}@replicated-labs.com"
113+
}
114+
115+
get_password() {
116+
local password=$(echo -n "${INSTRUQT_PARTICIPANT_ID}" | sha256sum)
117+
echo "${password::20}"
118+
}
119+
120+
get_slackernews_domain() {
121+
echo "cluster-30443-${INSTRUQT_PARTICIPANT_ID}.env.play.instruqt.com"
122+
}
123+
124+
show_credentials() {
125+
local CYAN='\033[0;36m'
126+
local GREEN='\033[1;32m'
127+
local NC='\033[0m'
128+
local password=$(get_password)
129+
echo -e "${GREEN}Credentials for ${CYAN}https://vendor.replicated.com"
130+
echo -e "${GREEN}Username: ${CYAN}${INSTRUQT_PARTICIPANT_ID}@replicated-labs.com"
131+
echo -e "${GREEN}Password: ${CYAN}${password}${NC}"
132+
}
133+
134+
echo "Fallback mode active - basic functions available"
135+
HEADER_FALLBACK_MODE=true
136+
137+
else
138+
# Load all Instruqt libraries in dependency order
139+
source "$LIBS_DIR/instruqt-bootstrap.sh" # Core initialization
140+
source "$LIBS_DIR/instruqt-files.sh" # File and directory management
141+
source "$LIBS_DIR/instruqt-ssh.sh" # SSH configuration
142+
source "$LIBS_DIR/instruqt-sessions.sh" # Tmux session management
143+
source "$LIBS_DIR/instruqt-config.sh" # Environment and credentials
144+
source "$LIBS_DIR/instruqt-services.sh" # Service and API management
145+
source "$LIBS_DIR/instruqt-apps.sh" # Application and release management
51146
fi
52147

53-
echo "All Replicated Field Labs libraries loaded successfully"
148+
# Verify all libraries loaded successfully (skip in fallback mode)
149+
if [[ "$HEADER_FALLBACK_MODE" != "true" ]]; then
150+
check_libraries() {
151+
local missing_libraries=()
152+
153+
# Check each library's loaded flag
154+
[[ "$INSTRUQT_BOOTSTRAP_LOADED" != "true" ]] && missing_libraries+=("instruqt-bootstrap")
155+
[[ "$INSTRUQT_FILES_LOADED" != "true" ]] && missing_libraries+=("instruqt-files")
156+
[[ "$INSTRUQT_SSH_LOADED" != "true" ]] && missing_libraries+=("instruqt-ssh")
157+
[[ "$INSTRUQT_SESSIONS_LOADED" != "true" ]] && missing_libraries+=("instruqt-sessions")
158+
[[ "$INSTRUQT_CONFIG_LOADED" != "true" ]] && missing_libraries+=("instruqt-config")
159+
[[ "$INSTRUQT_SERVICES_LOADED" != "true" ]] && missing_libraries+=("instruqt-services")
160+
[[ "$INSTRUQT_APPS_LOADED" != "true" ]] && missing_libraries+=("instruqt-apps")
161+
162+
if [[ ${#missing_libraries[@]} -gt 0 ]]; then
163+
echo "ERROR: Failed to load libraries: ${missing_libraries[*]}"
164+
return 1
165+
fi
166+
167+
return 0
168+
}
169+
170+
# Check that all libraries loaded
171+
if ! check_libraries; then
172+
echo "FATAL: Some libraries failed to load. Cannot continue."
173+
exit 1
174+
fi
175+
176+
echo "All Replicated Field Labs libraries loaded successfully"
177+
else
178+
echo "Header.sh loaded in fallback mode"
179+
fi
54180

55181
# Also load the high-level utility functions from instruqt-all.sh
56182
# This ensures header.sh provides everything the old version did plus new utilities

0 commit comments

Comments
 (0)