feat: Add Go companion agent for bare metal server management

Implements complete companion agent for Rust servers not on managed panels.

Features:
- NATS integration with token auth and auto-reconnect
- Game server process management (start/stop/restart/monitor)
- File operations (read/write/delete/list) via NATS
- SteamCMD integration for automated updates
- Self-update capability with download and replace
- Heartbeat publishing every 60s with server status
- Graceful shutdown handling (SIGTERM/SIGINT)
- Zombie process prevention via cmd.Wait()
- Cross-platform builds (Linux amd64, Windows amd64)

Structure:
- cmd/agent/main.go: Entry point, config, signal handling
- internal/app/daemon.go: Main loop, NATS subscriptions
- internal/client/nats.go: NATS connection with reconnect
- internal/process/gameserver.go: Process management
- internal/process/steamcmd.go: Steam update execution
- internal/files/operations.go: File system operations
- internal/update/updater.go: Self-update logic
- Makefile: Cross-compilation targets
- README.md: Installation and configuration guide

NATS Subjects:
- Publishes: corrosion.{license_id}.companion.heartbeat
- Publishes: corrosion.{license_id}.files.response
- Subscribes: corrosion.{license_id}.cmd.server
- Subscribes: corrosion.{license_id}.files.{get|put|delete|list}
- Subscribes: corrosion.{license_id}.update.steam
- Subscribes: corrosion.{license_id}.update.companion

Built binaries: 7.0MB (Linux), 7.2MB (Windows)
Total code: 1,356 LOC across 8 files

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Vantz Stockwell
2026-02-15 12:05:23 -05:00
parent 8bea889145
commit a62715409f
13 changed files with 1735 additions and 0 deletions

89
companion-agent/Makefile Normal file
View File

@@ -0,0 +1,89 @@
.PHONY: all build build-linux build-windows clean test run
# Binary names
BINARY_NAME=corrosion-companion
BINARY_LINUX=$(BINARY_NAME)-linux-amd64
BINARY_WINDOWS=$(BINARY_NAME)-windows-amd64.exe
# Build directory
BUILD_DIR=bin
# Go parameters
GOCMD=go
GOBUILD=$(GOCMD) build
GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
GOGET=$(GOCMD) get
GOMOD=$(GOCMD) mod
# Build flags
LDFLAGS=-ldflags "-s -w"
all: clean build
build: build-linux build-windows
build-linux:
@echo "Building for Linux (amd64)..."
@mkdir -p $(BUILD_DIR)
GOOS=linux GOARCH=amd64 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_LINUX) ./cmd/agent
@echo "Built: $(BUILD_DIR)/$(BINARY_LINUX)"
build-windows:
@echo "Building for Windows (amd64)..."
@mkdir -p $(BUILD_DIR)
GOOS=windows GOARCH=amd64 $(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_WINDOWS) ./cmd/agent
@echo "Built: $(BUILD_DIR)/$(BINARY_WINDOWS)"
build-local:
@echo "Building for current platform..."
@mkdir -p $(BUILD_DIR)
$(GOBUILD) $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME) ./cmd/agent
@echo "Built: $(BUILD_DIR)/$(BINARY_NAME)"
clean:
@echo "Cleaning..."
@$(GOCLEAN)
@rm -rf $(BUILD_DIR)
test:
@echo "Running tests..."
@$(GOTEST) -v ./...
deps:
@echo "Downloading dependencies..."
@$(GOMOD) download
@$(GOMOD) tidy
run: build-local
@echo "Running agent..."
@./$(BUILD_DIR)/$(BINARY_NAME)
# Install systemd service (Linux only)
install-service:
@echo "Installing systemd service..."
@sudo cp $(BUILD_DIR)/$(BINARY_LINUX) /usr/local/bin/$(BINARY_NAME)
@sudo cp deployment/corrosion-companion.service /etc/systemd/system/
@sudo systemctl daemon-reload
@sudo systemctl enable corrosion-companion
@echo "Service installed. Configure /etc/corrosion-companion/.env then start with: sudo systemctl start corrosion-companion"
# Development helpers
dev: build-local
@./$(BUILD_DIR)/$(BINARY_NAME)
fmt:
@echo "Formatting code..."
@$(GOCMD) fmt ./...
lint:
@echo "Running linter..."
@golangci-lint run ./...
# Show build info
info:
@echo "Build Information:"
@echo " Binary Name: $(BINARY_NAME)"
@echo " Linux Binary: $(BUILD_DIR)/$(BINARY_LINUX)"
@echo " Windows Binary: $(BUILD_DIR)/$(BINARY_WINDOWS)"
@echo " Go Version: $(shell go version)"