-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-entrypoint.sh
More file actions
145 lines (118 loc) · 3.68 KB
/
docker-entrypoint.sh
File metadata and controls
145 lines (118 loc) · 3.68 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
#!/bin/bash
set -e
# PUID/PGID Configuration (defaults to root if not set)
PUID=${PUID:-0}
PGID=${PGID:-0}
# Data directory configuration
DATA_DIR="/data"
CONFIG_DIR="$DATA_DIR/config"
OPENCODE_DIR="$DATA_DIR/.opencode"
# Export environment variables for the running application
export DATA_DIR
export CONFIG_DIR
export OPENCODE_DIR
# Create the complete directory structure
create_directories() {
echo "Creating OpenCode data directories..."
# Main directories
mkdir -p "$CONFIG_DIR"
mkdir -p "$OPENCODE_DIR"
# .opencode subdirectories for custom agents, commands, modes, plugins, skills, tools, themes
mkdir -p "$OPENCODE_DIR/agents"
mkdir -p "$OPENCODE_DIR/commands"
mkdir -p "$OPENCODE_DIR/modes"
mkdir -p "$OPENCODE_DIR/plugins"
mkdir -p "$OPENCODE_DIR/skills"
mkdir -p "$OPENCODE_DIR/tools"
mkdir -p "$OPENCODE_DIR/themes"
# Symlink for config directory to enable single-volume persistence
mkdir -p /root/.config
if [ ! -L /root/.config/opencode ]; then
ln -sf "$CONFIG_DIR" /root/.config/opencode
fi
# Symlink for opencode data directory (auth.json, logs, etc.)
mkdir -p /home/node/.local/share
if [ ! -L /home/node/.local/share/opencode ]; then
ln -sf "$OPENCODE_DIR" /home/node/.local/share/opencode
fi
echo "Directory structure created successfully."
}
# Set ownership of data directories
set_ownership() {
if [ "$PUID" -ne 0 ] || [ "$PGID" -ne 0 ]; then
echo "Setting ownership to PUID=$PUID, PGID=$PGID..."
chown -R "$PUID:$PGID" "$DATA_DIR"
chown -R "$PUID:$PGID" /root/.config
chown -R "$PUID:$PGID" /home/node/.local
fi
}
# Create user if PUID/PGID are specified
create_user() {
if [ "$PUID" -ne 0 ] || [ "$PGID" -ne 0 ]; then
# Check if group exists, create if not
if ! getent group "$PGID" > /dev/null 2>&1; then
groupadd -g "$PGID" opencode
fi
# Check if user exists, create if not
if ! id -u "$PUID" > /dev/null 2>&1; then
useradd -u "$PUID" -g "$PGID" -d /root -s /bin/bash opencode
fi
fi
}
# Build the opencode serve command
build_command() {
# Start with the command "serve"
local cmd="serve"
# 1. Handle Port
if [ -n "$PORT" ]; then
cmd="$cmd --port $PORT"
fi
# 2. Handle Hostname (default to 0.0.0.0 in Docker)
if [ -n "$HOSTNAME_OVERRIDE" ]; then
cmd="$cmd --hostname $HOSTNAME_OVERRIDE"
else
cmd="$cmd --hostname 0.0.0.0"
fi
# 3. Handle mDNS
if [ "$MDNS" = "true" ]; then
cmd="$cmd --mdns"
fi
# 4. Handle mDNS Domain
if [ -n "$MDNS_DOMAIN" ]; then
cmd="$cmd --mdns-domain $MDNS_DOMAIN"
fi
# 5. Handle CORS (comma-separated list)
if [ -n "$CORS" ]; then
IFS=',' read -ra ORIGINS <<< "$CORS"
for origin in "${ORIGINS[@]}"; do
cmd="$cmd --cors $(echo $origin | xargs)"
done
fi
echo "$cmd"
}
# Main execution
main() {
# Create directory structure
create_directories
# Create user if needed
create_user
# Set ownership
set_ownership
# Build command
COMMAND=$(build_command)
echo "Starting opencode server..."
# Ensure OPENCODE_CONFIG is exported if set
if [ -n "$OPENCODE_CONFIG" ]; then
export OPENCODE_CONFIG
fi
# Execute as the appropriate user
if [ "$PUID" -ne 0 ] || [ "$PGID" -ne 0 ]; then
# Drop privileges using gosu
exec gosu "$PUID:$PGID" opencode $COMMAND "$@"
else
# Run as root (not recommended)
exec opencode $COMMAND "$@"
fi
}
# Run main function
main "$@"