Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·294 lines (249 loc) · 7.94 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·294 lines (249 loc) · 7.94 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/usr/bin/env bash
#
# OpenCode Systemd - One-line Installer
# Quick install script for opencode-systemd services
#
# Usage: curl -fsSL https://ocsd.nbr.st/install.sh | bash
#
set -euo pipefail
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
BOLD='\033[1m'
NC='\033[0m'
# Repository URL
REPO_URL="https://github.com/grikomsn/opencode-systemd"
RAW_URL="https://ocsd.nbr.st"
# Version
VERSION="1.0.0"
log_info() {
echo -e "${BLUE}[INFO]${NC} $1" >&2
}
log_success() {
echo -e "${GREEN}[OK]${NC} $1" >&2
}
log_warn() {
echo -e "${YELLOW}[WARN]${NC} $1" >&2
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1" >&2
}
log_step() {
echo -e "${CYAN}→${NC} $1" >&2
}
print_banner() {
echo -e "${CYAN}"
cat << 'EOF'
____ _____ _ _
/ __ \ / ____| | | | |
| | | |_ __ ___ _ __ | | ___ __| | ___ ___| |_ ___
| | | | '_ \ / _ \ '_ \ | | / _ \ / _` |/ _ \/ __| __/ __|
| |__| | |_) | __/ | | | | |___| (_) | (_| | __/\__ \ |_\__ \
\____/| .__/ \___|_| |_| \_____\___/ \__,_|\___||___/\__|___/
| |
|_| Systemd Install
EOF
echo -e "${NC}"
echo -e "${BOLD}Version ${VERSION}${NC}"
echo -e "${CYAN}${REPO_URL}${NC}"
echo
}
# Check prerequisites
check_prerequisites() {
log_step "Checking prerequisites..."
# Check if opencode is installed
if [[ ! -x "$HOME/.opencode/bin/opencode" ]]; then
log_error "OpenCode is not installed!"
echo
log_info "Please install OpenCode first:"
echo
echo " ${BOLD}curl -fsSL https://get.opencode.ai | bash${NC}"
echo
log_info "Then run this installer again."
exit 1
fi
log_success "OpenCode is installed"
# Check if systemd is available
if ! command -v systemctl &> /dev/null; then
log_error "systemd is not available on this system."
exit 1
fi
log_success "systemd is available"
# Check for required tools
for cmd in curl wget; do
if command -v $cmd &> /dev/null; then
DOWNLOAD_CMD=$cmd
break
fi
done
if [[ -z "${DOWNLOAD_CMD:-}" ]]; then
log_error "Neither curl nor wget found. Please install one of them."
exit 1
fi
log_success "Download tool available: $DOWNLOAD_CMD"
}
# Download wizard script
download_wizard() {
local wizard_path="$HOME/.opencode/bin/opencode-systemd-wizard"
local temp_file
temp_file=$(mktemp)
log_step "Downloading wizard script..."
if [[ "$DOWNLOAD_CMD" == "curl" ]]; then
if curl -fsSL "${RAW_URL}/wizard.sh" -o "$temp_file" 2>/dev/null; then
log_success "Downloaded wizard.sh"
else
log_error "Failed to download wizard script"
rm -f "$temp_file"
exit 1
fi
else
if wget -q "${RAW_URL}/wizard.sh" -O "$temp_file" 2>/dev/null; then
log_success "Downloaded wizard.sh"
else
log_error "Failed to download wizard script"
rm -f "$temp_file"
exit 1
fi
fi
# Install wizard
mkdir -p "$HOME/.opencode/bin"
mv "$temp_file" "$wizard_path"
chmod +x "$wizard_path"
log_success "Installed wizard to $wizard_path"
# Create symlink if not exists
local bin_dir="$HOME/.local/bin"
if [[ -d "$bin_dir" ]] && [[ ! -L "$bin_dir/opencode-systemd" ]]; then
ln -sf "$wizard_path" "$bin_dir/opencode-systemd"
log_success "Created symlink: $bin_dir/opencode-systemd"
fi
echo "$wizard_path"
}
# Install systemd services
install_services() {
local wizard_path="$1"
local auto_confirm="${AUTO_CONFIRM:-false}"
local upgrade_time="${UPGRADE_TIME:-05:00:00}"
local web_host="${WEB_HOST:-127.0.0.1}"
local web_port="${WEB_PORT:-4096}"
log_step "Installing systemd services..."
if $auto_confirm; then
"$wizard_path" install --yes --time "$upgrade_time" --host "$web_host" --port "$web_port"
else
"$wizard_path" install --time "$upgrade_time" --host "$web_host" --port "$web_port"
fi
}
# Print usage
usage() {
cat << EOF
OpenCode Systemd Installer
Usage:
curl -fsSL ${RAW_URL}/install.sh | bash
curl -fsSL ${RAW_URL}/install.sh | bash -s -- [OPTIONS]
Options:
-y, --yes Auto-confirm all prompts
--time HH:MM:SS Set auto-upgrade time (default: 05:00:00)
--host HOST Set web service host (default: 127.0.0.1)
--port PORT Set web service port (default: 4096)
-h, --help Show this help message
Examples:
# Install with defaults
curl -fsSL ${RAW_URL}/install.sh | bash
# Install without prompts
curl -fsSL ${RAW_URL}/install.sh | bash -s -- --yes
# Install with custom time
curl -fsSL ${RAW_URL}/install.sh | bash -s -- --time 03:00:00
# Install with custom host and port
curl -fsSL ${RAW_URL}/install.sh | bash -s -- --host 0.0.0.0 --port 8080
After installation:
- Web UI: http://127.0.0.1:4096 (or your custom host:port)
- Auto-upgrade: Daily at configured time
Run 'opencode-systemd status' to check status
Run 'opencode-systemd --help' for more commands
EOF
}
# Parse arguments
parse_args() {
AUTO_CONFIRM=false
UPGRADE_TIME="05:00:00"
WEB_HOST="127.0.0.1"
WEB_PORT="4096"
SHOW_HELP=false
while [[ $# -gt 0 ]]; do
case $1 in
-y|--yes)
AUTO_CONFIRM=true
shift
;;
--time)
UPGRADE_TIME="$2"
shift 2
;;
--host)
WEB_HOST="$2"
shift 2
;;
--port)
WEB_PORT="$2"
shift 2
;;
-h|--help)
SHOW_HELP=true
shift
;;
*)
log_error "Unknown option: $1"
usage
exit 1
;;
esac
done
}
# Main installation
main() {
# Always parse args first (handles both pipe and direct execution)
parse_args "$@"
# Show help and exit if requested
if $SHOW_HELP; then
usage
exit 0
fi
print_banner
log_info "Starting installation..."
echo
check_prerequisites
local wizard_path
wizard_path=$(download_wizard)
install_services "$wizard_path"
# Read actual config from generated service files
local service_file="$HOME/.config/systemd/user/opencode-web.service"
if [[ -f "$service_file" ]]; then
local desc
desc=$(grep "Description=" "$service_file" | head -1 || true)
WEB_HOST=$(echo "$desc" | sed 's/.*bound to //' || echo "$WEB_HOST")
fi
local timer_file="$HOME/.config/systemd/user/opencode-upgrade.timer"
if [[ -f "$timer_file" ]]; then
local cal
cal=$(grep "^OnCalendar=\*-\*-\*" "$timer_file" | head -1 || true)
UPGRADE_TIME=$(echo "$cal" | sed 's/^OnCalendar=\*-\*-\* //' || echo "$UPGRADE_TIME")
fi
echo
echo -e "${GREEN}${BOLD}═══════════════════════════════════════════════════${NC}"
echo -e "${GREEN}${BOLD} ✓ OpenCode Systemd installed successfully!${NC}"
echo -e "${GREEN}${BOLD}═══════════════════════════════════════════════════${NC}"
echo
log_info "Quick commands:"
echo " opencode-systemd status # Check service status"
echo " opencode-systemd upgrade # Manual upgrade"
echo " opencode-systemd --help # Show all commands"
echo
log_info "Web UI: http://${WEB_HOST}:${WEB_PORT}"
log_info "Auto-upgrade: Daily at ${UPGRADE_TIME}"
echo
log_info "For issues: ${REPO_URL}/issues"
}
# Run main
main "$@"