Compare commits
7 Commits
1f899cc5e9
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| fea9f9c677 | |||
| 280d423f89 | |||
| 3143f49d9c | |||
| 30ceffee1c | |||
| b912f45472 | |||
| 5f4561aa87 | |||
| b73f51faa4 |
@@ -17,6 +17,11 @@ services:
|
||||
ACC_IMAGE: acc-wine:latest
|
||||
ACC_SERVERS_PATH: /data/servers
|
||||
ACC_SERVERS_HOST_PATH: /opt/acc-servers
|
||||
# ACC_CPUSET: Pin all ACC containers to specific CPU cores (e.g., "0-11").
|
||||
# Reduces simulation timing misses ("not enough CPU power" in accServer.exe).
|
||||
# Set the CPU governor to 'performance' on the host for best results:
|
||||
# echo performance | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
|
||||
# ACC_CPUSET: "0-11"
|
||||
env_file:
|
||||
- .env
|
||||
volumes:
|
||||
|
||||
@@ -1,27 +1,32 @@
|
||||
# ACC Server container: Wine + accServer.exe
|
||||
# ACC Server container: Wine Staging + accServer.exe
|
||||
# Game files are bind-mounted from the host at /acc/game.
|
||||
FROM debian:bookworm-slim
|
||||
|
||||
# Install Wine, Xvfb (headless display for Wine), and minimal runtime deps.
|
||||
# Install Wine Staging from WineHQ (newer than Debian's packages, includes fsync support).
|
||||
RUN dpkg --add-architecture i386 && \
|
||||
apt-get update && \
|
||||
apt-get install -y --no-install-recommends \
|
||||
wine \
|
||||
wine32 \
|
||||
wine64 \
|
||||
xvfb \
|
||||
ca-certificates \
|
||||
apt-get install -y --no-install-recommends wget ca-certificates && \
|
||||
mkdir -pm755 /etc/apt/keyrings && \
|
||||
wget -q -O /etc/apt/keyrings/winehq-archive.key https://dl.winehq.org/wine-builds/winehq.key && \
|
||||
echo "deb [arch=amd64,i386 signed-by=/etc/apt/keyrings/winehq-archive.key] https://dl.winehq.org/wine-builds/debian/ bookworm main" \
|
||||
> /etc/apt/sources.list.d/winehq.list && \
|
||||
apt-get update && \
|
||||
apt-get install -y --install-recommends \
|
||||
winehq-staging \
|
||||
tcpdump \
|
||||
&& \
|
||||
rm -rf /var/lib/apt/lists/*
|
||||
|
||||
RUN mkdir -p /tmp/.X11-unix && chmod 1777 /tmp/.X11-unix
|
||||
|
||||
WORKDIR /root
|
||||
|
||||
ENV WINEARCH=win64
|
||||
ENV WINEPREFIX=/root/.wine
|
||||
ENV WINEDEBUG=-all
|
||||
# fsync uses Linux futex2 for Wine synchronization — reduces timing jitter under load.
|
||||
# Falls back to esync (eventfd) if fsync is unavailable; falls back to standard if neither.
|
||||
ENV WINEFSYNC=1
|
||||
ENV WINEESYNC=1
|
||||
|
||||
# Copy entrypoint script.
|
||||
COPY entrypoint.sh /entrypoint.sh
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
|
||||
# Use a display number derived from the server port to avoid conflicts
|
||||
# between containers (each server gets its own unique display).
|
||||
DISPLAY_NUM=$((SERVER_PORT % 1000 + 100))
|
||||
export DISPLAY=:${DISPLAY_NUM}
|
||||
# Wine Staging looks for PulseAudio via XDG_RUNTIME_DIR. There is no audio
|
||||
# in this container, so point it at a valid (empty) directory to silence
|
||||
# the "XDG_RUNTIME_DIR is invalid" errors.
|
||||
export XDG_RUNTIME_DIR=/tmp/runtime-${SERVER_PORT}
|
||||
mkdir -p "${XDG_RUNTIME_DIR}"
|
||||
|
||||
# Clean up any stale X lock from a previous container run.
|
||||
rm -f /tmp/.X${DISPLAY_NUM}-lock /tmp/.X11-unix/X${DISPLAY_NUM} 2>/dev/null || true
|
||||
|
||||
Xvfb ${DISPLAY} -screen 0 1024x768x16 -nolisten tcp &
|
||||
XVFB_PID=$!
|
||||
# Use Wine's headless (null) display driver — accServer.exe has no GUI so
|
||||
# there is no need for Xvfb at all. This avoids the X11 client limit and
|
||||
# removes the Xvfb process entirely.
|
||||
export DISPLAY=
|
||||
export WINEDLLOVERRIDES="winex11.drv="
|
||||
|
||||
cleanup() {
|
||||
echo "Stopping ACC server..."
|
||||
kill $XVFB_PID 2>/dev/null || true
|
||||
}
|
||||
trap cleanup SIGTERM SIGINT
|
||||
|
||||
|
||||
@@ -1,39 +1,70 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"acc-server-manager/local/middleware"
|
||||
"acc-server-manager/local/model"
|
||||
"acc-server-manager/local/service"
|
||||
"acc-server-manager/local/utl/common"
|
||||
"acc-server-manager/local/utl/configs"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"go.uber.org/dig"
|
||||
)
|
||||
|
||||
type SystemController struct {
|
||||
systemService *service.SystemService
|
||||
}
|
||||
|
||||
// NewSystemController
|
||||
// Initializes SystemController.
|
||||
//
|
||||
// Args:
|
||||
// *services.SystemService: Service control service
|
||||
// *Fiber.RouterGroup: Fiber Router Group
|
||||
// Returns:
|
||||
// *SystemController: Controller for service control interactions
|
||||
func NewSystemController(routeGroups *common.RouteGroups) *SystemController {
|
||||
ac := &SystemController{}
|
||||
type SystemControllerParams struct {
|
||||
dig.In
|
||||
RouteGroups *common.RouteGroups
|
||||
AuthMiddleware *middleware.AuthMiddleware
|
||||
SystemService *service.SystemService `optional:"true"`
|
||||
}
|
||||
|
||||
apiGroup := routeGroups.System
|
||||
apiGroup.Get("/health", ac.getFirst)
|
||||
// NewSystemController initializes SystemController.
|
||||
func NewSystemController(p SystemControllerParams) *SystemController {
|
||||
ac := &SystemController{systemService: p.SystemService}
|
||||
|
||||
apiGroup := p.RouteGroups.System
|
||||
apiGroup.Get("/health", ac.getHealth)
|
||||
|
||||
if p.SystemService != nil {
|
||||
apiGroup.Post("/migrate-image", p.AuthMiddleware.Authenticate, ac.migrateImage)
|
||||
}
|
||||
|
||||
return ac
|
||||
}
|
||||
|
||||
// getFirst returns service control status
|
||||
// getHealth returns the current version string.
|
||||
//
|
||||
// @Summary Return service control status
|
||||
// @Description Return service control status
|
||||
// @Summary Return service health / version
|
||||
// @Description Return service health / version
|
||||
// @Tags system
|
||||
// @Success 200 {array} string
|
||||
// @Success 200 {string} string
|
||||
// @Router /system/health [get]
|
||||
func (ac *SystemController) getFirst(c *fiber.Ctx) error {
|
||||
func (ac *SystemController) getHealth(c *fiber.Ctx) error {
|
||||
return c.SendString(configs.Version)
|
||||
}
|
||||
|
||||
// migrateImage godoc
|
||||
//
|
||||
// @Summary Migrate all Docker server containers to current ACC image
|
||||
// @Description Pulls the current ACC_IMAGE, removes old containers, optionally stops running servers
|
||||
// @Tags system
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body model.MigrateImageRequest true "Migration options"
|
||||
// @Success 200 {object} model.MigrationResult
|
||||
// @Router /system/migrate-image [post]
|
||||
func (ac *SystemController) migrateImage(c *fiber.Ctx) error {
|
||||
var req model.MigrateImageRequest
|
||||
if err := c.BodyParser(&req); err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "invalid request"})
|
||||
}
|
||||
result, err := ac.systemService.MigrateImage(c.Context(), req.StopRunning)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
|
||||
}
|
||||
return c.JSON(result)
|
||||
}
|
||||
|
||||
24
local/model/system.go
Normal file
24
local/model/system.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package model
|
||||
|
||||
type MigrateImageRequest struct {
|
||||
StopRunning bool `json:"stopRunning"`
|
||||
}
|
||||
|
||||
type ServerMigrationEntry struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
WasRunning bool `json:"wasRunning"`
|
||||
}
|
||||
|
||||
type ServerMigrationError struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
type MigrationResult struct {
|
||||
Image string `json:"image"`
|
||||
Migrated []ServerMigrationEntry `json:"migrated"`
|
||||
Skipped []ServerMigrationEntry `json:"skipped"`
|
||||
Failed []ServerMigrationError `json:"failed"`
|
||||
}
|
||||
@@ -62,19 +62,24 @@ func (r *DockerRuntime) Create(ctx context.Context, server *model.Server) error
|
||||
fmt.Sprintf("%s:/acc/game:rw", hostServerPath),
|
||||
}
|
||||
|
||||
hostConfig := &container.HostConfig{
|
||||
Binds: binds,
|
||||
PortBindings: portBindings,
|
||||
RestartPolicy: container.RestartPolicy{Name: "unless-stopped"},
|
||||
// NET_RAW is required for tcpdump packet capture in debug mode (DEBUG_MODE=1).
|
||||
CapAdd: []string{"NET_RAW"},
|
||||
}
|
||||
if cpuset := env.GetACCCpuset(); cpuset != "" {
|
||||
hostConfig.CpusetCpus = cpuset
|
||||
}
|
||||
|
||||
resp, err := r.client.ContainerCreate(ctx, &container.Config{
|
||||
Image: image,
|
||||
ExposedPorts: exposedPorts,
|
||||
Env: []string{
|
||||
fmt.Sprintf("SERVER_PORT=%d", server.Port),
|
||||
},
|
||||
}, &container.HostConfig{
|
||||
Binds: binds,
|
||||
PortBindings: portBindings,
|
||||
RestartPolicy: container.RestartPolicy{Name: "unless-stopped"},
|
||||
// NET_RAW is required for tcpdump packet capture in debug mode (DEBUG_MODE=1).
|
||||
CapAdd: []string{"NET_RAW"},
|
||||
}, nil, nil, server.ServiceName)
|
||||
}, hostConfig, nil, nil, server.ServiceName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create container %s: %v", server.ServiceName, err)
|
||||
}
|
||||
|
||||
@@ -100,4 +100,7 @@ func registerDockerProviders(c *dig.Container) {
|
||||
c.Provide(func(client *dockerclient.Client) platform.LogStreamer {
|
||||
return NewDockerLogStreamer(client)
|
||||
})
|
||||
|
||||
c.Provide(NewSystemService)
|
||||
|
||||
}
|
||||
|
||||
112
local/service/system_service.go
Normal file
112
local/service/system_service.go
Normal file
@@ -0,0 +1,112 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"acc-server-manager/local/model"
|
||||
"acc-server-manager/local/platform"
|
||||
"acc-server-manager/local/repository"
|
||||
"acc-server-manager/local/utl/env"
|
||||
"acc-server-manager/local/utl/logging"
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/docker/docker/api/types/container"
|
||||
dockerclient "github.com/docker/docker/client"
|
||||
)
|
||||
|
||||
type SystemService struct {
|
||||
dockerClient *dockerclient.Client
|
||||
repo *repository.ServerRepository
|
||||
runtime platform.ServerRuntime
|
||||
serverService *ServerService
|
||||
}
|
||||
|
||||
func NewSystemService(
|
||||
dockerClient *dockerclient.Client,
|
||||
repo *repository.ServerRepository,
|
||||
runtime platform.ServerRuntime,
|
||||
serverService *ServerService,
|
||||
) *SystemService {
|
||||
return &SystemService{
|
||||
dockerClient: dockerClient,
|
||||
repo: repo,
|
||||
runtime: runtime,
|
||||
serverService: serverService,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SystemService) MigrateImage(ctx context.Context, stopRunning bool) (*model.MigrationResult, error) {
|
||||
image := env.GetACCImage()
|
||||
|
||||
servers, err := s.repo.GetAll(ctx, &model.ServerFilter{})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to list servers: %v", err)
|
||||
}
|
||||
|
||||
result := &model.MigrationResult{Image: image}
|
||||
|
||||
for _, server := range *servers {
|
||||
statusStr, _ := s.runtime.Status(ctx, server.ID)
|
||||
isRunning := statusStr == model.StatusRunning.String()
|
||||
|
||||
if isRunning && !stopRunning {
|
||||
result.Skipped = append(result.Skipped, model.ServerMigrationEntry{
|
||||
ID: server.ID.String(),
|
||||
Name: server.Name,
|
||||
WasRunning: true,
|
||||
})
|
||||
continue
|
||||
}
|
||||
|
||||
entry := model.ServerMigrationEntry{
|
||||
ID: server.ID.String(),
|
||||
Name: server.Name,
|
||||
WasRunning: isRunning,
|
||||
}
|
||||
|
||||
if isRunning {
|
||||
if _, err := s.runtime.Stop(ctx, server.ID); err != nil {
|
||||
result.Failed = append(result.Failed, model.ServerMigrationError{
|
||||
ID: server.ID.String(),
|
||||
Name: server.Name,
|
||||
Error: fmt.Sprintf("stop failed: %v", err),
|
||||
})
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
if server.ContainerID != "" {
|
||||
if err := s.dockerClient.ContainerRemove(ctx, server.ContainerID, container.RemoveOptions{Force: true}); err != nil {
|
||||
logging.Warn("Failed to remove container %s for server %s: %v", server.ContainerID, server.ID, err)
|
||||
}
|
||||
server.ContainerID = ""
|
||||
}
|
||||
|
||||
if err := s.runtime.Create(ctx, &server); err != nil {
|
||||
result.Failed = append(result.Failed, model.ServerMigrationError{
|
||||
ID: server.ID.String(),
|
||||
Name: server.Name,
|
||||
Error: fmt.Sprintf("recreate failed: %v", err),
|
||||
})
|
||||
continue
|
||||
}
|
||||
if err := s.repo.Update(ctx, &server); err != nil {
|
||||
logging.Warn("Failed to persist new ContainerID for server %s: %v", server.ID, err)
|
||||
}
|
||||
|
||||
if isRunning {
|
||||
if _, err := s.runtime.Start(ctx, server.ID); err != nil {
|
||||
result.Failed = append(result.Failed, model.ServerMigrationError{
|
||||
ID: server.ID.String(),
|
||||
Name: server.Name,
|
||||
Error: fmt.Sprintf("restart failed: %v", err),
|
||||
})
|
||||
continue
|
||||
}
|
||||
s.serverService.StartAccServerRuntime(&server)
|
||||
}
|
||||
|
||||
result.Migrated = append(result.Migrated, entry)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
4
local/utl/env/env.go
vendored
4
local/utl/env/env.go
vendored
@@ -65,6 +65,10 @@ func GetACCServersPath() string {
|
||||
return DefaultACCServersPath
|
||||
}
|
||||
|
||||
func GetACCCpuset() string {
|
||||
return os.Getenv("ACC_CPUSET")
|
||||
}
|
||||
|
||||
func GetACCServersHostPath() string {
|
||||
if p := os.Getenv("ACC_SERVERS_HOST_PATH"); p != "" {
|
||||
return p
|
||||
|
||||
116
scripts/migrate-wine.sh
Executable file
116
scripts/migrate-wine.sh
Executable file
@@ -0,0 +1,116 @@
|
||||
#!/usr/bin/env bash
|
||||
# migrate-wine.sh — Pull the latest acc-wine image and migrate all server containers.
|
||||
#
|
||||
# Usage:
|
||||
# ./scripts/migrate-wine.sh -u <username> -p <password> -b <base_url> [--stop-running]
|
||||
#
|
||||
# Options:
|
||||
# -u, --username API username
|
||||
# -p, --password API password
|
||||
# -b, --base-url Base URL of the manager (e.g. http://192.168.0.177:3000)
|
||||
# --stop-running Also migrate currently running servers (stop → migrate → restart)
|
||||
#
|
||||
# Examples:
|
||||
# ./scripts/migrate-wine.sh -u admin -p secret -b http://192.168.0.177:3000
|
||||
# ./scripts/migrate-wine.sh -u admin -p secret -b http://192.168.0.177:3000 --stop-running
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
# ── Argument parsing ────────────────────────────────────────────────────────────
|
||||
USERNAME=""
|
||||
PASSWORD=""
|
||||
BASE_URL=""
|
||||
STOP_RUNNING=false
|
||||
|
||||
usage() {
|
||||
grep '^#' "$0" | sed 's/^# \{0,1\}//' | tail -n +2
|
||||
exit 1
|
||||
}
|
||||
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case "$1" in
|
||||
-u|--username) USERNAME="$2"; shift 2 ;;
|
||||
-p|--password) PASSWORD="$2"; shift 2 ;;
|
||||
-b|--base-url) BASE_URL="$2"; shift 2 ;;
|
||||
--stop-running) STOP_RUNNING=true; shift ;;
|
||||
-h|--help) usage ;;
|
||||
*) echo "Unknown option: $1"; usage ;;
|
||||
esac
|
||||
done
|
||||
|
||||
[[ -z "$USERNAME" ]] && { echo "Error: --username is required"; usage; }
|
||||
[[ -z "$PASSWORD" ]] && { echo "Error: --password is required"; usage; }
|
||||
[[ -z "$BASE_URL" ]] && { echo "Error: --base-url is required"; usage; }
|
||||
|
||||
BASE_URL="${BASE_URL%/}" # strip trailing slash
|
||||
BASE_URL="${BASE_URL/#http:\/\//https://}" # upgrade http → https
|
||||
|
||||
# ── Check dependencies ──────────────────────────────────────────────────────────
|
||||
for cmd in curl jq; do
|
||||
command -v "$cmd" &>/dev/null || { echo "Error: '$cmd' is required but not installed."; exit 1; }
|
||||
done
|
||||
|
||||
# ── Login ───────────────────────────────────────────────────────────────────────
|
||||
echo "Logging in as '$USERNAME'..."
|
||||
|
||||
LOGIN_RESPONSE=$(curl -sL --post301 --post302 -X POST "${BASE_URL}/v1/auth/login" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "$(jq -n --arg u "$USERNAME" --arg p "$PASSWORD" '{username: $u, password: $p}')")
|
||||
|
||||
TOKEN=$(echo "$LOGIN_RESPONSE" | jq -r '.token // empty' 2>/dev/null || true)
|
||||
if [[ -z "$TOKEN" ]]; then
|
||||
echo "Error: Login failed — no token in response."
|
||||
exit 1
|
||||
fi
|
||||
echo "Login successful."
|
||||
|
||||
# ── Migrate ─────────────────────────────────────────────────────────────────────
|
||||
echo "Starting image migration (stopRunning=${STOP_RUNNING})..."
|
||||
|
||||
MIGRATE_RESPONSE=$(curl -sL --post301 --post302 -X POST "${BASE_URL}/v1/system/migrate-image" \
|
||||
-H "Content-Type: application/json" \
|
||||
-H "Authorization: Bearer ${TOKEN}" \
|
||||
-d "{\"stopRunning\":${STOP_RUNNING}}")
|
||||
|
||||
# ── Results ──────────────────────────────────────────────────────────────────────
|
||||
SERVER_ERROR=$(echo "$MIGRATE_RESPONSE" | jq -r '.error // empty' 2>/dev/null || true)
|
||||
if [[ -n "$SERVER_ERROR" ]]; then
|
||||
echo "Error: Migration failed — $SERVER_ERROR"
|
||||
echo "Full response: $MIGRATE_RESPONSE"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
IMAGE=$(echo "$MIGRATE_RESPONSE" | jq -r '.image')
|
||||
MIGRATED=$(echo "$MIGRATE_RESPONSE" | jq -r '.migrated | length')
|
||||
SKIPPED=$(echo "$MIGRATE_RESPONSE" | jq -r '.skipped | length')
|
||||
FAILED=$(echo "$MIGRATE_RESPONSE" | jq -r '.failed | length')
|
||||
|
||||
echo ""
|
||||
echo "Image: ${IMAGE}"
|
||||
echo "────────────────────────────────"
|
||||
printf " Migrated : %s\n" "$MIGRATED"
|
||||
printf " Skipped : %s\n" "$SKIPPED"
|
||||
printf " Failed : %s\n" "$FAILED"
|
||||
echo "────────────────────────────────"
|
||||
|
||||
if [[ "$MIGRATED" -gt 0 ]]; then
|
||||
echo ""
|
||||
echo "Migrated servers:"
|
||||
echo "$MIGRATE_RESPONSE" | jq -r '.migrated[] | " - \(.name) (wasRunning: \(.wasRunning))"'
|
||||
fi
|
||||
|
||||
if [[ "$SKIPPED" -gt 0 ]]; then
|
||||
echo ""
|
||||
echo "Skipped (running, use --stop-running to include):"
|
||||
echo "$MIGRATE_RESPONSE" | jq -r '.skipped[] | " - \(.name)"'
|
||||
fi
|
||||
|
||||
if [[ "$FAILED" -gt 0 ]]; then
|
||||
echo ""
|
||||
echo "Failed:"
|
||||
echo "$MIGRATE_RESPONSE" | jq -r '.failed[] | " - \(.name): \(.error)"'
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo "Done."
|
||||
Reference in New Issue
Block a user