Skip to content

Commit cc91535

Browse files
committed
some collective
1 parent 298b670 commit cc91535

29 files changed

+500
-307
lines changed

build-docker/alpine/Dockerfile

Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -19,55 +19,22 @@ RUN cargo build -p router-api --release
1919
FROM alpine:latest
2020
WORKDIR /app
2121

22-
# Install OpenRC and other dependencies
22+
# Install minimal runtime dependencies
2323
RUN apk add --no-cache \
24-
openrc \
2524
ca-certificates \
26-
procps \
27-
bash \
28-
logrotate
25+
bash
2926

3027
# Copy binaries from builder
3128
COPY --from=builder /app/target/release/router-core /usr/local/bin/router-core
3229
COPY --from=builder /app/target/release/router-api /usr/local/bin/router-api
3330

34-
# Create necessary directories
35-
RUN mkdir -p /opt/gwrs/bin && \
36-
mkdir -p /opt/gwrs/conf && \
37-
mkdir -p /tmp/gwrs/log && \
38-
mkdir -p /etc/gwrs/logrotate
31+
COPY build-docker/alpine/entrypoint.sh /usr/local/bin/entrypoint.sh
3932

40-
# Create symlinks to binaries
41-
RUN ln -sf /usr/local/bin/router-core /opt/gwrs/bin/router-core && \
42-
ln -sf /usr/local/bin/router-api /opt/gwrs/bin/router-api
33+
# Make everything executable
34+
RUN chmod +x /usr/local/bin/*
4335

44-
# Create service files directory
45-
RUN mkdir -p /etc/init.d
46-
47-
# Copy service files
48-
COPY build-docker/alpine/router-core.initd /etc/init.d/gwrs-core
49-
COPY build-docker/alpine/router-api.initd /etc/init.d/gwrs-api
50-
51-
# Copy logrotate configuration
52-
COPY build-docker/alpine/logrotate/gwrs /etc/gwrs/logrotate/
53-
COPY build-docker/alpine/logrotate/setup.sh /etc/gwrs/logrotate/
54-
55-
# Make the init scripts and logrotate setup executable
56-
RUN chmod +x /etc/init.d/gwrs-core && \
57-
chmod +x /etc/init.d/gwrs-api && \
58-
chmod +x /etc/gwrs/logrotate/setup.sh && \
59-
/etc/gwrs/logrotate/setup.sh
60-
61-
# Add services to default runlevel
62-
RUN rc-update add gwrs-core default
63-
RUN rc-update add gwrs-api default
64-
65-
# Setup entrypoint
66-
COPY build-docker/alpine/entrypoint.sh /entrypoint.sh
67-
RUN chmod +x /entrypoint.sh
68-
69-
# Expose API
36+
# Expose API port
7037
EXPOSE 24042
7138

72-
ENTRYPOINT ["/entrypoint.sh"]
73-
CMD ["/sbin/init"]
39+
# Set entrypoint
40+
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]

build-docker/alpine/entrypoint.sh

Lines changed: 128 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,132 @@
11
#!/bin/bash
22
set -e
33

4-
# Prepare OpenRC for container environment
5-
if [ ! -d /run/openrc ]; then
6-
mkdir -p /run/openrc
7-
touch /run/openrc/softlevel
8-
fi
9-
10-
# Setup necessary directories for OpenRC
11-
mkdir -p /tmp/gwrs/log
12-
mkdir -p /run
13-
mkdir -p /var/run
14-
15-
# Prevent init scripts from running during install
16-
echo 'rc_provide="loopback net"' >> /etc/rc.conf
17-
18-
# Setup basic mounts expected by OpenRC
19-
mount -t proc none /proc
20-
mount -t sysfs none /sys
21-
mount -t tmpfs none /run
22-
23-
# Start OpenRC
24-
if [ "$1" = "/sbin/init" ]; then
25-
# Start services directly
26-
/sbin/rc-service gwrs-core start
27-
/sbin/rc-service gwrs-api start
4+
# Simple configuration
5+
LOG_DIR="/tmp/gwrs/log"
6+
PID_DIR="/tmp/gwrs/pids"
7+
CHECK_INTERVAL=5
8+
9+
# Create directories
10+
mkdir -p "$LOG_DIR" "$PID_DIR"
11+
12+
# Logging
13+
log() {
14+
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_DIR/manager.log"
15+
}
16+
17+
# Start core service
18+
start_core() {
19+
log "Starting router-core..."
20+
nohup /usr/local/bin/router-core > "$LOG_DIR/core.log" 2> "$LOG_DIR/core.error" &
21+
echo $! > "$PID_DIR/core.pid"
22+
log "router-core started (PID: $!)"
23+
}
24+
25+
# Start API service
26+
start_api() {
27+
log "Starting router-api..."
28+
nohup /usr/local/bin/router-api > "$LOG_DIR/api.log" 2> "$LOG_DIR/api.error" &
29+
echo $! > "$PID_DIR/api.pid"
30+
log "router-api started (PID: $!)"
31+
}
32+
33+
# Stop a service
34+
stop_service() {
35+
local service=$1
36+
local pid_file="$PID_DIR/${service}.pid"
37+
38+
if [ -f "$pid_file" ]; then
39+
local pid=$(cat "$pid_file")
40+
if kill -0 $pid 2>/dev/null; then
41+
log "Stopping $service (PID: $pid)"
42+
kill $pid
43+
sleep 2
44+
# Force kill if still running
45+
if kill -0 $pid 2>/dev/null; then
46+
kill -9 $pid
47+
fi
48+
fi
49+
rm -f "$pid_file"
50+
fi
51+
}
52+
53+
# Check if service is running
54+
is_running() {
55+
local service=$1
56+
local pid_file="$PID_DIR/${service}.pid"
57+
58+
if [ -f "$pid_file" ]; then
59+
local pid=$(cat "$pid_file")
60+
if kill -0 $pid 2>/dev/null; then
61+
return 0 # Running
62+
else
63+
rm -f "$pid_file" # Clean up stale PID
64+
fi
65+
fi
66+
return 1 # Not running
67+
}
68+
69+
# Restart core (and then API)
70+
restart_core() {
71+
log "Core is down! Restarting core and API..."
72+
73+
# Stop both services
74+
stop_service "api"
75+
stop_service "core"
2876

29-
# Keep container running
30-
echo "Services started. Container is now running..."
31-
exec tail -f /tmp/gwrs/log/core.log /tmp/gwrs/log/api.log
32-
else
33-
# Run command as specified
34-
exec "$@"
35-
fi
77+
sleep 2
78+
79+
# Start core first
80+
start_core
81+
sleep 3
82+
83+
# Then start API
84+
start_api
85+
}
86+
87+
# Restart API only
88+
restart_api() {
89+
log "API is down! Restarting API..."
90+
stop_service "api"
91+
sleep 2
92+
start_api
93+
}
94+
95+
# Monitor services
96+
monitor() {
97+
log "Starting service monitor..."
98+
99+
while true; do
100+
# Check core
101+
if ! is_running "core"; then
102+
restart_core
103+
# Check API (only if core is running)
104+
elif ! is_running "api"; then
105+
restart_api
106+
fi
107+
108+
sleep $CHECK_INTERVAL
109+
done
110+
}
111+
112+
# Cleanup on exit
113+
cleanup() {
114+
log "Shutting down services..."
115+
stop_service "api"
116+
stop_service "core"
117+
exit 0
118+
}
119+
120+
# Handle signals
121+
trap cleanup SIGTERM SIGINT
122+
123+
# Main execution
124+
log "=== Router Process Manager Starting ==="
125+
126+
# Start both services
127+
start_core
128+
sleep 3 # Give core time to start
129+
start_api
130+
131+
# Monitor forever
132+
monitor

build-docker/alpine/logrotate/gwrs

Lines changed: 0 additions & 21 deletions
This file was deleted.

build-docker/alpine/logrotate/setup.sh

Lines changed: 0 additions & 28 deletions
This file was deleted.

build-docker/alpine/router-api.initd

Lines changed: 0 additions & 19 deletions
This file was deleted.

build-docker/alpine/router-core.initd

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)