From 5f827b87dd2ada286594ce27e3026e63cd3ae830 Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 8 Sep 2025 21:43:13 +0200 Subject: [PATCH] feat: Add Dockerfile, README.md, docker-compose.yml and update index.html --- Dockerfile | 39 +++++++++++++++ README.md | 121 +++++++++++++++++++++++++++++++++++++++++++++ docker-compose.yml | 21 ++++++++ index.html | 3 +- 4 files changed, 182 insertions(+), 2 deletions(-) create mode 100644 Dockerfile create mode 100644 README.md create mode 100644 docker-compose.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d63140f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,39 @@ +# Stage 1: Builder +FROM golang:1.24.6-alpine AS builder + +WORKDIR /app + +# Copy go.mod and go.sum first to leverage Docker cache +COPY go.mod . +COPY go.sum . + +# Download dependencies +RUN go mod download + +# Copy the rest of the application source code +COPY . . + +# Build the application +# CGO_ENABLED=0 is important for static linking, resulting in a smaller image +# -o manage-servers specifies the output binary name +RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o manage-servers . + +# Stage 2: Runner +FROM alpine:latest + +WORKDIR /root/ + +# Copy the compiled binary from the builder stage +COPY --from=builder /app/manage-servers . + +# Copy the index.html file for the web server +COPY --from=builder /app/index.html . + +# Copy the servers.json file +COPY servers.json . + +# Expose the port the web server listens on +EXPOSE 8080 + +# Command to run the application +ENTRYPOINT ["./manage-servers", "serve"] diff --git a/README.md b/README.md new file mode 100644 index 0000000..c47c078 --- /dev/null +++ b/README.md @@ -0,0 +1,121 @@ +# Server Management Tool + +This is a simple Go application designed to manage your servers, providing functionalities to list, wake up (Wake-on-LAN), shut down, and reboot them. It offers both a command-line interface (CLI) and a web-based interface for convenient management. + +## Features + +* **List Servers:** Display all configured servers with their details. +* **Wake Server:** Send Wake-on-LAN (WoL) magic packets to bring servers online. +* **Shutdown Server:** Remotely shut down servers via SSH. +* **Reboot Server:** Remotely reboot servers via SSH. +* **Server Status:** Check if servers are online using ping. +* **Web Interface:** A user-friendly web interface to perform all management actions. + +## Prerequisites + +Before you begin, ensure you have the following installed: + +* [Go (1.24.6 or newer)](https://golang.org/doc/install) +* [Docker](https://docs.docker.com/get-docker/) +* `git` (for cloning the repository) + +## Getting Started (Local) + +1. **Clone the repository:** + + ```bash + git clone git@git.hnrx.net:hnrx/manage-servers.git + cd manage-servers + ``` + +2. **Configure your servers:** + + Edit the `servers.json` file to include your server details, including MAC addresses for Wake-on-LAN and SSH credentials for shutdown/reboot. + + Example `servers.json`: + ```json + [ + { + "name": "harvester-01", + "mac": "3c:49:37:05:c2:2e", + "ip": "192.168.110.101", + "ssh_user": "rancher", + "ssh_pass": "maddog07" + } + ] + ``` + +3. **Run CLI commands:** + + ```bash + go run . list + go run . wake harvester-01 + go run . status + go run . shutdown harvester-01 + go run . reboot harvester-01 + ``` + +4. **Run the web server locally:** + + ```bash + go run . serve + ``` + + Access the web interface in your browser at `http://localhost:8080`. + +## Getting Started (Docker) + +1. **Build the Docker image:** + + Navigate to the root of the project and run: + + ```bash + docker build -t manage-servers:latest . + ``` + +2. **Run the Docker container:** + + To enable Wake-on-LAN functionality, the Docker container needs to run in `host` network mode. This allows it to send broadcast packets directly onto your physical network. + + ```bash + docker run --network host -p 8080:8080 manage-servers:latest + ``` + + * `--network host`: Essential for Wake-on-LAN to work correctly. + * `-p 8080:8080`: Maps port 8080 from the container to port 8080 on your host. + +3. **Access the web interface:** + + Open your web browser and navigate to `http://localhost:8080`. + +## Configuration + +The `servers.json` file is crucial for the application's functionality. Ensure it's correctly formatted and contains accurate information for all your servers. + +* `name`: A unique identifier for your server. +* `mac`: The MAC address of the server for Wake-on-LAN. +* `ip`: The IP address of the server for SSH connections and status checks. +* `ssh_user`: The username for SSH access. +* `ssh_pass`: The password for SSH access. + +## Troubleshooting + +### Host key verification failed (when pushing to Git) + +If you encounter `Host key verification failed` when pushing to your Git repository, it means your system does not trust the SSH key of the Git server. To resolve this, you need to manually add the server's host key to your `~/.ssh/known_hosts` file. + +From your terminal, attempt to connect to the server via SSH for the first time: + +```bash +ssh git@192.168.200.20 +``` + +When prompted to confirm the host's authenticity, type `yes` and press Enter. This will add the host key to your `known_hosts` file, and you should then be able to push your changes. + +### Error loading servers: open servers.json: no such file or directory (in Docker) + +This error indicates that the `servers.json` file was not found inside the Docker container. Ensure you have the latest `Dockerfile` (which includes copying `servers.json`) and rebuild your Docker image. + +### Sending magic packet from Docker container does not work + +If Wake-on-LAN is not working from within the Docker container, it's likely a networking issue. Ensure you are running the Docker container with the `--network host` flag, as described in the "Run the Docker container" section above. This allows the container to send broadcast packets directly onto your physical network. diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..7058557 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,21 @@ +version: '3.8' + +services: + manage-servers: + image: git.hnrx.net/hnrx/manage-servers:v0.0.1 + container_name: manage-servers + restart: unless-stopped + labels: + - "traefik.enable=true" + - "traefik.http.routers.manage-servers.rule=Host(`server-management.hnrx.net`)" + - "traefik.http.routers.manage-servers.entrypoints=websecure" + - "traefik.http.routers.manage-servers.service=manage-servers" + - "traefik.http.routers.manage-servers.tls=true" + - "traefik.http.routers.manage-servers.tls.certresolver=cloudflare" + - "traefik.http.services.manage-servers.loadbalancer.server.port=8080" + networks: + - my-container-macvlan-200 + +networks: + my-container-macvlan-200: + external: true diff --git a/index.html b/index.html index ed1ff69..ed771af 100644 --- a/index.html +++ b/index.html @@ -1,4 +1,3 @@ - Server Management @@ -94,4 +93,4 @@ - + \ No newline at end of file