diff --git a/.gitea/workflows/build-push.yml b/.gitea/workflows/build-push.yml new file mode 100644 index 0000000..f80141c --- /dev/null +++ b/.gitea/workflows/build-push.yml @@ -0,0 +1,85 @@ +name: Build & Push to Harbor + +on: + push: + branches: [master] + paths: + - "Server/**" + - ".gitea/workflows/**" + +env: + # TODO: replace with your actual Harbor hostname + REGISTRY: harbor.aeternalisgames.com + # Harbor project name (create this in Harbor UI first) + PROJECT: sor + +jobs: + build-and-push: + # TODO: update this label to match your Gitea runner's label + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v4 + + # Short SHA used as the immutable image tag. + - name: Compute image tag + id: tag + run: echo "SHA=$(echo '${{ github.sha }}' | cut -c1-8)" >> $GITHUB_OUTPUT + + # Log in to Harbor using repository secrets HARBOR_USERNAME / HARBOR_PASSWORD. + - name: Log in to Harbor + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ secrets.HARBOR_USERNAME }} + password: ${{ secrets.HARBOR_PASSWORD }} + + # Enable BuildKit for faster, more efficient multi-stage builds. + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + # ── SOR.Host ──────────────────────────────────────────────────────────── + - name: Build and push sor-host + uses: docker/build-push-action@v5 + with: + context: Server + file: Server/Dockerfile.host + push: true + cache-from: type=registry,ref=${{ env.REGISTRY }}/${{ env.PROJECT }}/sor-host:buildcache + cache-to: type=registry,ref=${{ env.REGISTRY }}/${{ env.PROJECT }}/sor-host:buildcache,mode=max + tags: | + ${{ env.REGISTRY }}/${{ env.PROJECT }}/sor-host:${{ steps.tag.outputs.SHA }} + ${{ env.REGISTRY }}/${{ env.PROJECT }}/sor-host:latest + + # ── SOR.Admin ─────────────────────────────────────────────────────────── + - name: Build and push sor-admin + uses: docker/build-push-action@v5 + with: + context: Server + file: Server/Dockerfile.admin + push: true + cache-from: type=registry,ref=${{ env.REGISTRY }}/${{ env.PROJECT }}/sor-admin:buildcache + cache-to: type=registry,ref=${{ env.REGISTRY }}/${{ env.PROJECT }}/sor-admin:buildcache,mode=max + tags: | + ${{ env.REGISTRY }}/${{ env.PROJECT }}/sor-admin:${{ steps.tag.outputs.SHA }} + ${{ env.REGISTRY }}/${{ env.PROJECT }}/sor-admin:latest + + # ── Update k8s manifests with new image tags ───────────────────────────── + # ArgoCD watches deploy/k8s/ in git; updating these tags triggers a sync. + - name: Update deployment image tags + run: | + SHA=${{ steps.tag.outputs.SHA }} + sed -i "s|image: ${{ env.REGISTRY }}/${{ env.PROJECT }}/sor-host:.*|image: ${{ env.REGISTRY }}/${{ env.PROJECT }}/sor-host:${SHA}|" deploy/k8s/deployment-host.yaml + sed -i "s|image: ${{ env.REGISTRY }}/${{ env.PROJECT }}/sor-admin:.*|image: ${{ env.REGISTRY }}/${{ env.PROJECT }}/sor-admin:${SHA}|" deploy/k8s/deployment-admin.yaml + + - name: Commit updated manifests + run: | + SHA=${{ steps.tag.outputs.SHA }} + git config user.email "ci@aeternalisgames.com" + git config user.name "Gitea CI" + git add deploy/k8s/deployment-host.yaml deploy/k8s/deployment-admin.yaml + # Only commit if there are staged changes; [skip ci] prevents a re-trigger. + git diff --cached --quiet || \ + git commit -m "ci: deploy image tags ${SHA} [skip ci]" + git push diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f966703 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +# ── Secrets ─────────────────────────────────────────────────────────────────── +# Populated secret files are NEVER committed — only *.yaml.template files are. +deploy/secrets/*.yaml +!deploy/secrets/*.yaml.template +!deploy/secrets/README.md diff --git a/Server/Dockerfile.admin b/Server/Dockerfile.admin new file mode 100644 index 0000000..53399af --- /dev/null +++ b/Server/Dockerfile.admin @@ -0,0 +1,36 @@ +# ── Stage 1: Build ──────────────────────────────────────────────────────────── +FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build +WORKDIR /src + +COPY Directory.Build.props Directory.Packages.props ./ +COPY SOR.slnx ./ + +# SOR.Admin only depends on Cdn, Database, and Shared. +COPY src/SOR.Admin/SOR.Admin.csproj src/SOR.Admin/ +COPY src/SOR.Cdn/SOR.Cdn.csproj src/SOR.Cdn/ +COPY src/SOR.Database/SOR.Database.csproj src/SOR.Database/ +COPY src/SOR.Shared/SOR.Shared.csproj src/SOR.Shared/ + +RUN dotnet restore src/SOR.Admin/SOR.Admin.csproj + +# Copy full source (needed for migrations embedded in SOR.Database). +COPY . . +RUN dotnet publish src/SOR.Admin/SOR.Admin.csproj \ + -c Release \ + --no-restore \ + -o /app/publish + +# ── Stage 2: Runtime ────────────────────────────────────────────────────────── +FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime +WORKDIR /app + +RUN adduser --disabled-password --gecos "" appuser +USER appuser + +COPY --from=build --chown=appuser:appuser /app/publish . + +EXPOSE 8080 +ENV ASPNETCORE_URLS=http://+:8080 +ENV ASPNETCORE_ENVIRONMENT=Production + +ENTRYPOINT ["dotnet", "SOR.Admin.dll"] diff --git a/Server/Dockerfile.host b/Server/Dockerfile.host new file mode 100644 index 0000000..65de767 --- /dev/null +++ b/Server/Dockerfile.host @@ -0,0 +1,52 @@ +# ── Stage 1: Build ──────────────────────────────────────────────────────────── +# Uses the full SDK image; output is a self-contained publish artifact. +FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build +WORKDIR /src + +# Copy solution props first — these rarely change so the layer is cached. +COPY Directory.Build.props Directory.Packages.props ./ +COPY SOR.slnx ./ + +# Copy individual csproj files so 'dotnet restore' is a separate cached layer. +# Only invalidated when a dependency changes, not when source changes. +COPY src/SOR.Host/SOR.Host.csproj src/SOR.Host/ +COPY src/SOR.Auth/SOR.Auth.csproj src/SOR.Auth/ +COPY src/SOR.Bazaar/SOR.Bazaar.csproj src/SOR.Bazaar/ +COPY src/SOR.Cdn/SOR.Cdn.csproj src/SOR.Cdn/ +COPY src/SOR.Chat/SOR.Chat.csproj src/SOR.Chat/ +COPY src/SOR.Collection/SOR.Collection.csproj src/SOR.Collection/ +COPY src/SOR.Content/SOR.Content.csproj src/SOR.Content/ +COPY src/SOR.Database/SOR.Database.csproj src/SOR.Database/ +COPY src/SOR.Gateway/SOR.Gateway.csproj src/SOR.Gateway/ +COPY src/SOR.Networking.Abstractions/SOR.Networking.Abstractions.csproj src/SOR.Networking.Abstractions/ +COPY src/SOR.Networking.Nats/SOR.Networking.Nats.csproj src/SOR.Networking.Nats/ +COPY src/SOR.Networking.Redis/SOR.Networking.Redis.csproj src/SOR.Networking.Redis/ +COPY src/SOR.Networking.WebSocket/SOR.Networking.WebSocket.csproj src/SOR.Networking.WebSocket/ +COPY src/SOR.Shared/SOR.Shared.csproj src/SOR.Shared/ +COPY src/SOR.Shared.Proto/SOR.Shared.Proto.csproj src/SOR.Shared.Proto/ +COPY src/SOR.Versioning/SOR.Versioning.csproj src/SOR.Versioning/ + +RUN dotnet restore src/SOR.Host/SOR.Host.csproj + +# Copy full source and publish. +COPY . . +RUN dotnet publish src/SOR.Host/SOR.Host.csproj \ + -c Release \ + --no-restore \ + -o /app/publish + +# ── Stage 2: Runtime ────────────────────────────────────────────────────────── +FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime +WORKDIR /app + +# Run as non-root for security. +RUN adduser --disabled-password --gecos "" appuser +USER appuser + +COPY --from=build --chown=appuser:appuser /app/publish . + +EXPOSE 8080 +ENV ASPNETCORE_URLS=http://+:8080 +ENV ASPNETCORE_ENVIRONMENT=Production + +ENTRYPOINT ["dotnet", "SOR.Host.dll"] diff --git a/deploy/argo/application.yaml b/deploy/argo/application.yaml new file mode 100644 index 0000000..3127184 --- /dev/null +++ b/deploy/argo/application.yaml @@ -0,0 +1,37 @@ +apiVersion: argoproj.io/v1alpha1 +kind: Application +metadata: + name: sor-server + # ArgoCD is typically installed in the argocd namespace. + namespace: argocd + # Ensure ArgoCD deletes child resources when this Application is deleted. + finalizers: + - resources-finalizer.argocd.argoproj.io +spec: + project: default + + source: + # TODO: verify this matches your Gitea repository URL. + repoURL: https://git.aeternalisgames.com/dingo/SOR.git + targetRevision: master + # ArgoCD watches this path for changes and syncs when manifests are updated. + path: deploy/k8s + + destination: + server: https://kubernetes.default.svc + namespace: stone-server + + syncPolicy: + automated: + # Remove resources from the cluster when removed from git. + prune: true + # Re-apply if someone manually changes the cluster state. + selfHeal: true + syncOptions: + # Namespace already exists — don't try to create it. + - CreateNamespace=false + # Apply server-side to avoid annotation length limits. + - ServerSideApply=true + + # Show health status on the ArgoCD dashboard. + revisionHistoryLimit: 5 diff --git a/deploy/k8s/configmap.yaml b/deploy/k8s/configmap.yaml new file mode 100644 index 0000000..f3a332c --- /dev/null +++ b/deploy/k8s/configmap.yaml @@ -0,0 +1,26 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: sor-config + namespace: stone-server +data: + # ── SOR.Host ──────────────────────────────────────────────────────────────── + ENABLED_SERVICES: "all" + ASPNETCORE_URLS: "http://+:8080" + ASPNETCORE_ENVIRONMENT: "Production" + + # WebSocket gateway settings + WebSocket__Path: "/ws" + Gateway__AuthTimeoutMs: "10000" + Gateway__MaxMessagesPerSecond: "20" + + # gRPC: gateway calls auth at this address. + # In single-pod (ENABLED_SERVICES=all) mode both run in the same process on the same port. + Services__Auth: "http://localhost:8080" + + # NATS message bus — update to your NATS service address. + # TODO: replace with your actual NATS service URL. + Nats__Url: "nats://nats.stone-server.svc.cluster.local:4222" + + # ── SOR.Admin ─────────────────────────────────────────────────────────────── + # Admin inherits ASPNETCORE_URLS and ASPNETCORE_ENVIRONMENT from the same configmap. diff --git a/deploy/k8s/deployment-admin.yaml b/deploy/k8s/deployment-admin.yaml new file mode 100644 index 0000000..93a65d1 --- /dev/null +++ b/deploy/k8s/deployment-admin.yaml @@ -0,0 +1,87 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: sor-admin + namespace: stone-server + labels: + app: sor-admin +spec: + replicas: 1 + selector: + matchLabels: + app: sor-admin + template: + metadata: + labels: + app: sor-admin + spec: + imagePullSecrets: + - name: harbor-credentials + + containers: + - name: sor-admin + # CI updates this image tag on every push to master. + # TODO: replace with your actual Harbor hostname/project before first deploy. + image: harbor.aeternalisgames.com/sor/sor-admin:latest + ports: + - containerPort: 8080 + + envFrom: + - configMapRef: + name: sor-config + + env: + # Postgres — same database, admin-specific schema access. + - name: ConnectionStrings__Default + valueFrom: + secretKeyRef: + name: sor-app-secrets + key: ConnectionStrings__Default + + # CDN — needed for asset uploads in the admin portal. + - name: Cdn__PublicBaseUrl + valueFrom: + secretKeyRef: + name: sor-app-secrets + key: Cdn__PublicBaseUrl + - name: Cdn__UploadBaseUrl + valueFrom: + secretKeyRef: + name: sor-app-secrets + key: Cdn__UploadBaseUrl + - name: Cdn__ApiKey + valueFrom: + secretKeyRef: + name: sor-app-secrets + key: Cdn__ApiKey + + # ASP.NET Core Data Protection — persists Blazor auth cookies across restarts. + # TODO: generate a strong random value and store in sor-app-secrets. + - name: DataProtection__ApplicationName + value: "sor-admin" + + livenessProbe: + httpGet: + path: /health + port: 8080 + initialDelaySeconds: 15 + periodSeconds: 30 + timeoutSeconds: 5 + failureThreshold: 3 + + readinessProbe: + httpGet: + path: /health + port: 8080 + initialDelaySeconds: 10 + periodSeconds: 10 + timeoutSeconds: 3 + failureThreshold: 3 + + resources: + requests: + cpu: "100m" + memory: "128Mi" + limits: + cpu: "500m" + memory: "512Mi" diff --git a/deploy/k8s/deployment-host.yaml b/deploy/k8s/deployment-host.yaml new file mode 100644 index 0000000..be4ad61 --- /dev/null +++ b/deploy/k8s/deployment-host.yaml @@ -0,0 +1,95 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: sor-host + namespace: stone-server + labels: + app: sor-host +spec: + replicas: 1 + selector: + matchLabels: + app: sor-host + template: + metadata: + labels: + app: sor-host + spec: + # Pull images from Harbor using the harbor-credentials secret. + imagePullSecrets: + - name: harbor-credentials + + containers: + - name: sor-host + # CI updates this image tag on every push to master. + # TODO: replace with your actual Harbor hostname/project before first deploy. + image: harbor.aeternalisgames.com/sor/sor-host:latest + ports: + - containerPort: 8080 + + # ── Non-secret config from ConfigMap ────────────────────────────── + envFrom: + - configMapRef: + name: sor-config + + # ── Secrets injected as individual env vars ──────────────────────── + env: + # Postgres — from the sor-app-secrets Secret (see deploy/secrets/). + - name: ConnectionStrings__Default + valueFrom: + secretKeyRef: + name: sor-app-secrets + key: ConnectionStrings__Default + + # Redis — from the pre-existing redis-credentials Secret. + # TODO: verify the key name matches your redis-credentials secret. + - name: ConnectionStrings__Redis + valueFrom: + secretKeyRef: + name: redis-credentials + key: connection-string + + # CDN + - name: Cdn__PublicBaseUrl + valueFrom: + secretKeyRef: + name: sor-app-secrets + key: Cdn__PublicBaseUrl + - name: Cdn__UploadBaseUrl + valueFrom: + secretKeyRef: + name: sor-app-secrets + key: Cdn__UploadBaseUrl + - name: Cdn__ApiKey + valueFrom: + secretKeyRef: + name: sor-app-secrets + key: Cdn__ApiKey + + # ── Health checks ────────────────────────────────────────────────── + livenessProbe: + httpGet: + path: /health + port: 8080 + initialDelaySeconds: 15 + periodSeconds: 30 + timeoutSeconds: 5 + failureThreshold: 3 + + readinessProbe: + httpGet: + path: /health + port: 8080 + initialDelaySeconds: 10 + periodSeconds: 10 + timeoutSeconds: 3 + failureThreshold: 3 + + # ── Resources ───────────────────────────────────────────────────── + resources: + requests: + cpu: "250m" + memory: "256Mi" + limits: + cpu: "1000m" + memory: "1Gi" diff --git a/deploy/k8s/ingress.yaml b/deploy/k8s/ingress.yaml new file mode 100644 index 0000000..e74c6a6 --- /dev/null +++ b/deploy/k8s/ingress.yaml @@ -0,0 +1,61 @@ +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: sor-ingress + namespace: stone-server + annotations: + # cert-manager: automatically provision / renew Let's Encrypt certs. + # TODO: verify your cluster-issuer name (run: kubectl get clusterissuer -A). + cert-manager.io/cluster-issuer: "letsencrypt-prod" + + # Nginx: keep WebSocket connections open for up to 1 hour. + # Required so clients don't get disconnected mid-game by proxy timeouts. + nginx.ingress.kubernetes.io/proxy-read-timeout: "3600" + nginx.ingress.kubernetes.io/proxy-send-timeout: "3600" + nginx.ingress.kubernetes.io/proxy-connect-timeout: "60" + + # Nginx will forward HTTP → HTTPS and handle the WebSocket Upgrade header. + nginx.ingress.kubernetes.io/ssl-redirect: "true" + +spec: + ingressClassName: nginx + + tls: + # hub.aeternalisgames.com — game server + WebSocket gateway. + # hub-tls already exists in this namespace (your existing Let's Encrypt cert). + - hosts: + - hub.aeternalisgames.com + secretName: hub-tls + + # admin.aeternalisgames.com — admin portal. + # cert-manager will auto-provision this cert on first deployment. + # TODO: ensure DNS A record for admin.aeternalisgames.com points to your ingress IP. + - hosts: + - admin.aeternalisgames.com + secretName: admin-tls + + rules: + # ── Game server / WebSocket gateway ──────────────────────────────────────── + - host: hub.aeternalisgames.com + http: + paths: + - path: / + pathType: Prefix + backend: + service: + name: sor-host + port: + number: 80 + + # ── Admin portal ─────────────────────────────────────────────────────────── + # Hosted on a separate subdomain to avoid SignalR/Blazor path conflicts. + - host: admin.aeternalisgames.com + http: + paths: + - path: / + pathType: Prefix + backend: + service: + name: sor-admin + port: + number: 80 diff --git a/deploy/k8s/service-admin.yaml b/deploy/k8s/service-admin.yaml new file mode 100644 index 0000000..449155c --- /dev/null +++ b/deploy/k8s/service-admin.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Service +metadata: + name: sor-admin + namespace: stone-server +spec: + selector: + app: sor-admin + ports: + - name: http + protocol: TCP + port: 80 + targetPort: 8080 + type: ClusterIP diff --git a/deploy/k8s/service-host.yaml b/deploy/k8s/service-host.yaml new file mode 100644 index 0000000..93dbc93 --- /dev/null +++ b/deploy/k8s/service-host.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Service +metadata: + name: sor-host + namespace: stone-server +spec: + selector: + app: sor-host + ports: + - name: http + protocol: TCP + port: 80 + targetPort: 8080 + type: ClusterIP diff --git a/deploy/secrets/README.md b/deploy/secrets/README.md new file mode 100644 index 0000000..76885f2 --- /dev/null +++ b/deploy/secrets/README.md @@ -0,0 +1,60 @@ +# SOR Kubernetes Secrets + +These files are **NOT committed to git** (excluded via root `.gitignore`). +The `*.yaml.template` files in this directory ARE committed — copy them, fill in +real values, then apply to the cluster. + +--- + +## Quick Setup + +### 1. Harbor image pull secret +```bash +cp harbor-pull-secret.yaml.template harbor-pull-secret.yaml +# edit harbor-pull-secret.yaml — fill in your Harbor username and password +kubectl apply -f harbor-pull-secret.yaml +``` + +### 2. Application secrets +```bash +cp sor-app-secrets.yaml.template sor-app-secrets.yaml +# edit sor-app-secrets.yaml — fill in Postgres connection string and CDN credentials +kubectl apply -f sor-app-secrets.yaml +``` + +### 3. Gitea Actions repository secrets +In your Gitea repository → Settings → Secrets, add: +| Name | Value | +|-------------------|--------------------------------| +| HARBOR_USERNAME | Your Harbor robot account name | +| HARBOR_PASSWORD | Your Harbor robot account token | + +--- + +## Secrets Reference + +| Secret Name | Key | Used By | Notes | +|--------------------|----------------------------|-----------------|-------------------------------------------| +| sor-app-secrets | ConnectionStrings__Default | host + admin | Postgres connection string | +| sor-app-secrets | Cdn__PublicBaseUrl | host + admin | e.g. https://cdn.aeternalisgames.com | +| sor-app-secrets | Cdn__UploadBaseUrl | host + admin | CDN upload endpoint | +| sor-app-secrets | Cdn__ApiKey | host + admin | CDN API key | +| redis-credentials | connection-string | host only | **Already exists** in stone-server ns | +| harbor-credentials | .dockerconfigjson | both pods | Harbor image pull secret | + +--- + +## Notes + +- `redis-credentials` is assumed to already exist in the `stone-server` namespace. + If the key name inside it is different from `connection-string`, update + `deploy/k8s/deployment-host.yaml` to match. + +- The Redis connection string format expected by StackExchange.Redis: + `HOST:PORT,password=PASSWORD,ssl=true,abortConnect=false` + +- The Postgres connection string format expected by Npgsql: + `Host=HOST;Port=5432;Database=SOR;Username=USER;Password=PASSWORD;` + +- For production, consider enabling SSL on the Postgres connection: + `...;SSL Mode=Require;Trust Server Certificate=true` diff --git a/deploy/secrets/harbor-pull-secret.yaml.template b/deploy/secrets/harbor-pull-secret.yaml.template new file mode 100644 index 0000000..9507163 --- /dev/null +++ b/deploy/secrets/harbor-pull-secret.yaml.template @@ -0,0 +1,28 @@ +apiVersion: v1 +kind: Secret +metadata: + name: harbor-credentials + namespace: stone-server +type: kubernetes.io/dockerconfigjson +stringData: + # Base64-encoded Docker config JSON. + # Easiest way to generate this — run locally then paste the output: + # + # kubectl create secret docker-registry harbor-credentials \ + # --docker-server=harbor.aeternalisgames.com \ + # --docker-username=HARBOR_USERNAME \ + # --docker-password=HARBOR_PASSWORD \ + # --namespace=stone-server \ + # --dry-run=client -o yaml + # + # TODO: replace with output from the command above. + .dockerconfigjson: | + { + "auths": { + "harbor.aeternalisgames.com": { + "username": "CHANGEME", + "password": "CHANGEME", + "auth": "CHANGEME_BASE64_OF_USER:PASS" + } + } + } diff --git a/deploy/secrets/sor-app-secrets.yaml.template b/deploy/secrets/sor-app-secrets.yaml.template new file mode 100644 index 0000000..f9b3a2c --- /dev/null +++ b/deploy/secrets/sor-app-secrets.yaml.template @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Secret +metadata: + name: sor-app-secrets + namespace: stone-server +type: Opaque +stringData: + # Postgres connection string (shared by sor-host and sor-admin). + # Format: Host=HOST;Port=5432;Database=SOR;Username=USER;Password=PASSWORD; + ConnectionStrings__Default: "Host=172.10.1.127;Port=5432;Database=SOR;Username=SOR-DBA;Password=CHANGEME" + + # CDN — public URL served to clients, upload endpoint, and API key. + Cdn__PublicBaseUrl: "https://CHANGEME" + Cdn__UploadBaseUrl: "https://CHANGEME" + Cdn__ApiKey: "CHANGEME"