-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·184 lines (156 loc) · 5.31 KB
/
install.sh
File metadata and controls
executable file
·184 lines (156 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env bash
# Variables
REPO_OWNER="sst"
REPO_NAME="opencode"
OPENCODE_VERSION="${VERSION:-"latest"}"
set -euo pipefail
if [ "$(id -u)" -ne 0 ]; then
echo -e 'Script must be run as root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.'
exit 1
fi
# Clean up
rm -rf /var/lib/apt/lists/*
# Checks if packages are installed and installs them if not
check_packages() {
if ! dpkg -s "$@" >/dev/null 2>&1; then
if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then
echo "Running apt-get update..."
apt-get update -y
fi
apt-get -y install --no-install-recommends "$@"
fi
}
# Make sure we have curl, ca-certificates, and tar
check_packages curl ca-certificates tar jq
echo "Installing OpenCode version: $OPENCODE_VERSION"
# Determine the OS and architecture
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
ARCH=$(uname -m)
case "$ARCH" in
x86_64)
ARCH_SUFFIX="x64"
;;
aarch64 | arm64)
ARCH_SUFFIX="arm64"
;;
*)
echo "ERROR: Unsupported architecture: $ARCH"
echo "Supported architectures: x86_64, aarch64/arm64"
exit 1
;;
esac
case "$OS" in
linux)
PLATFORM="linux"
;;
*)
echo "ERROR: Unsupported OS: $OS"
echo "Supported OS: Linux"
exit 1
;;
esac
# Function to resolve the latest version using GitHub API
resolve_latest_version() {
echo "Resolving latest version using GitHub API..." >&2
local api_response
api_response=$(curl -s --max-time 10 "https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME}/releases/latest" 2>/dev/null || echo "")
if [ -n "$api_response" ] && echo "$api_response" | jq -e '.tag_name' >/dev/null 2>&1; then
local version_tag
version_tag=$(echo "$api_response" | jq -r '.tag_name')
# Remove 'v' prefix if present
echo "${version_tag#v}"
return 0
else
echo "GitHub API failed, falling back to HTML parsing..." >&2
return 1
fi
}
# Function to resolve latest version by parsing HTML (fallback)
resolve_latest_version_fallback() {
echo "Attempting to resolve latest version from releases page HTML..." >&2
local releases_page
releases_page=$(curl -s --max-time 10 "https://github.com/${REPO_OWNER}/${REPO_NAME}/releases/latest" 2>/dev/null || echo "")
if [ -n "$releases_page" ]; then
# Look for version tag in the HTML
local version_tag
version_tag=$(echo "$releases_page" | grep -oE 'releases/tag/v?[0-9]+\.[0-9]+\.[0-9]+' | head -1 | sed 's/.*releases\/tag\/v\?\([0-9]\+\.[0-9]\+\.[0-9]\+\).*/\1/')
if [ -n "$version_tag" ] && [[ "$version_tag" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "$version_tag"
return 0
fi
fi
echo "Failed to resolve version from HTML, using known fallback version..." >&2
echo "1.0.107" # Known recent version as last resort
return 1
}
# Resolve version
if [ "$OPENCODE_VERSION" = "latest" ]; then
if ! RESOLVED_VERSION=$(resolve_latest_version); then
RESOLVED_VERSION=$(resolve_latest_version_fallback)
fi
echo "Resolved latest version to: $RESOLVED_VERSION"
OPENCODE_VERSION="$RESOLVED_VERSION"
else
echo "Using specified version: $OPENCODE_VERSION"
# Remove 'v' prefix if present in user input
OPENCODE_VERSION="${OPENCODE_VERSION#v}"
fi
# Construct download URL based on opencode's release pattern
# Asset name format: opencode-{platform}-{arch}.tar.gz
ASSET_NAME="opencode-${PLATFORM}-${ARCH_SUFFIX}.tar.gz"
DOWNLOAD_URL="https://github.com/${REPO_OWNER}/${REPO_NAME}/releases/download/v${OPENCODE_VERSION}/${ASSET_NAME}"
echo "Downloading OpenCode from: ${DOWNLOAD_URL}"
# Create temporary directory
TMP_DIR=$(mktemp -d)
trap 'rm -rf "$TMP_DIR"' EXIT
cd "$TMP_DIR"
# Download with retries and proper error handling
for attempt in 1 2 3; do
if curl -fL --retry 3 --retry-delay 2 -o "${ASSET_NAME}" "${DOWNLOAD_URL}"; then
echo "Download successful on attempt $attempt"
break
else
echo "Download attempt $attempt failed"
if [ $attempt -eq 3 ]; then
echo "ERROR: Failed to download after 3 attempts"
exit 1
fi
sleep 2
fi
done
# Verify the download
if [ ! -f "${ASSET_NAME}" ] || [ ! -s "${ASSET_NAME}" ]; then
echo "ERROR: Downloaded file is missing or empty"
exit 1
fi
echo "Extracting OpenCode..."
tar -xzf "${ASSET_NAME}"
# Find the binary in the extracted contents
# The archive should contain the opencode binary
if [ -f "opencode" ]; then
echo "Installing opencode..."
mv "opencode" /usr/local/bin/
chmod +x /usr/local/bin/opencode
elif [ -f "bin/opencode" ]; then
echo "Installing opencode from bin directory..."
mv "bin/opencode" /usr/local/bin/
chmod +x /usr/local/bin/opencode
else
echo "ERROR: Could not find opencode binary in archive"
echo "Archive contents:"
ls -la
exit 1
fi
# Clean up
cd - >/dev/null
rm -rf /var/lib/apt/lists/*
# Verify installation
echo "Verifying installation..."
if opencode --version >/dev/null 2>&1; then
opencode --version
echo "OpenCode installation completed successfully!"
else
echo "Warning: OpenCode installed but version check failed. This might be expected behavior."
echo "OpenCode installation completed!"
fi
echo "Done!"