Repository navigation
Expand file tree
/
Copy pathoc-ctl.sh
More file actions
executable file
·139 lines (126 loc) · 3.9 KB
/
Copy pathoc-ctl.sh
File metadata and controls
executable file
·139 lines (126 loc) · 3.9 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
#!/data/data/com.termux/files/usr/bin/bash
# Helper to manage the OpenCode Mobile Termux server.
# Usage: oc-ctl {start|stop|restart|status|logs [N]|doctor}
# Optional overrides (shell syntax): ${XDG_CONFIG_HOME:-$HOME/.config}/oc-ctl.conf
# e.g. PORT=4096 PASSWORD_FILE=... LOG_FILE=... LAUNCHER=...
set -euo pipefail
BASE="$HOME/.local/share/opencode-mobile"
LAUNCHER="$HOME/bin/oc-app-server"
PASSWORD_FILE="$BASE/server-password"
LOG_FILE="$BASE/server.log"
PORT=4096
SERVER_USER=opencode
LOG_MAX_BYTES=5242880 # rotate server.log above ~5 MB
STOP_TIMEOUT=15
CONF="${XDG_CONFIG_HOME:-$HOME/.config}/oc-ctl.conf"
if [ -f "$CONF" ]; then
# shellcheck source=/dev/null
. "$CONF"
fi
SERVER_URL="${SERVER_URL:-http://127.0.0.1:$PORT}"
PROC_PATTERN="[o]pencode serve --hostname 127[.]0[.]0[.]1 --port ${PORT}([^0-9]|\$)"
is_running() {
pgrep -f "$PROC_PATTERN" >/dev/null 2>&1
}
# Pass credentials to curl on stdin so the password never appears in `ps`.
curl_auth() {
printf 'user = "%s:%s"\n' "$SERVER_USER" "$(cat "$PASSWORD_FILE")" |
curl -sS --connect-timeout 3 --max-time 10 -K - "$@"
}
rotate_log() {
if [ -f "$LOG_FILE" ] && [ "$(wc -c < "$LOG_FILE")" -gt "$LOG_MAX_BYTES" ]; then
mv -f "$LOG_FILE" "$LOG_FILE.1"
fi
}
cmd_start() {
if [ ! -x "$LAUNCHER" ]; then
echo "Error: launcher not found or not executable at $LAUNCHER" >&2
return 1
fi
if is_running; then
echo "Server is already running."
return 0
fi
if command -v termux-wake-lock >/dev/null 2>&1; then
termux-wake-lock
fi
mkdir -p "$(dirname "$LOG_FILE")"
rotate_log
nohup "$LAUNCHER" </dev/null >>"$LOG_FILE" 2>&1 &
echo "Server starting in background. Log: $LOG_FILE"
echo "Run '$(basename "$0") status' in a few seconds to verify."
}
cmd_stop() {
if ! pkill -f "$PROC_PATTERN"; then
echo "No matching server process found."
else
local i=0
while is_running && [ "$i" -lt "$STOP_TIMEOUT" ]; do
sleep 1
i=$((i + 1))
done
if is_running; then
echo "Error: server still running after ${STOP_TIMEOUT}s." >&2
return 1
fi
echo "Server stopped."
fi
if command -v termux-wake-unlock >/dev/null 2>&1; then
termux-wake-unlock
fi
}
cmd_status() {
if is_running; then
echo "Process: RUNNING"
else
echo "Process: STOPPED"
fi
if [ -s "$PASSWORD_FILE" ]; then
local http_code
http_code=$(curl_auth -o /dev/null -w '%{http_code}' "$SERVER_URL/api/info" 2>/dev/null) || true
echo "API ($SERVER_URL/api/info): HTTP ${http_code:-000}"
else
echo "API: skipped (missing $PASSWORD_FILE)"
fi
}
cmd_logs() {
if [ ! -f "$LOG_FILE" ]; then
echo "No log file found at $LOG_FILE"
return 1
fi
tail -n "${1:-40}" "$LOG_FILE"
}
cmd_doctor() {
echo "=== OpenCode Mobile Termux Doctor ==="
echo "Config: $([ -f "$CONF" ] && echo "$CONF" || echo "(defaults)")"
echo "Launcher: $([ -x "$LAUNCHER" ] && echo "OK ($LAUNCHER)" || echo "MISSING")"
echo "Password file: $([ -s "$PASSWORD_FILE" ] && echo "OK (present, not printed)" || echo "MISSING")"
if command -v proot-distro >/dev/null 2>&1; then
echo "proot-distro: OK"
if proot-distro list 2>&1 | grep -qw ubuntu; then
echo "Ubuntu: installed"
echo "OpenCode: $(proot-distro login ubuntu -- /root/.opencode/bin/opencode --version 2>/dev/null | tail -n 1 || echo 'not found')"
else
echo "Ubuntu: NOT INSTALLED"
fi
else
echo "proot-distro: MISSING"
fi
cmd_status
if [ -s "$PASSWORD_FILE" ]; then
echo "--- /api/info response ---"
curl_auth "$SERVER_URL/api/info" && echo "" || echo "(failed to reach $SERVER_URL/api/info)"
fi
}
case "${1:-}" in
start) cmd_start ;;
stop) cmd_stop ;;
restart) cmd_stop; cmd_start ;;
status) cmd_status ;;
logs) cmd_logs "${2:-40}" ;;
doctor) cmd_doctor ;;
*)
echo "Usage: $(basename "$0") {start|stop|restart|status|logs [N]|doctor}"
exit 1
;;
esac