feat: Add Dockerfile, README.md, docker-compose.yml and update index.html

This commit is contained in:
Matthias
2025-09-08 21:43:13 +02:00
parent 75406bd56c
commit 5f827b87dd
4 changed files with 182 additions and 2 deletions
+39
View File
@@ -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"]
+121
View File
@@ -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.
+21
View File
@@ -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
+1 -2
View File
@@ -1,4 +1,3 @@
<!DOCTYPE html>
<html> <html>
<head> <head>
<title>Server Management</title> <title>Server Management</title>
@@ -94,4 +93,4 @@
</script> </script>
</body> </body>
</html> </html>