-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
146 lines (118 loc) Β· 4.53 KB
/
server.go
File metadata and controls
146 lines (118 loc) Β· 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"fmt"
"os"
"strings"
)
// doServerInit generates a server.yaml template
func doServerInit() {
if _, err := os.Stat("server.yaml"); err == nil {
logFatal("server.yaml already exists")
}
defaultConfig := `host: "vps.example.com"
user: "root"
ssh_port: 22
stack:
traefik:
version: "v3.0"
email: "admin@example.com"
dashboard: true
network_name: "traefik-net"
# Global Auth Provider
auth:
provider: "basic" # or "authelia"
authelia:
subdomain: "auth"
users_file: "users.yml"
watchtower:
schedule: "0 4 * * *" # Every day at 4am
`
if err := os.WriteFile("server.yaml", []byte(defaultConfig), 0644); err != nil {
logFatal("Failed to write server.yaml: %v", err)
}
logSuccess("Created server.yaml. Please edit it with your VPS details.")
}
// doServerProvision installs the stack defined in server.yaml
func doServerProvision() {
cfg := loadServerConfig()
env := Environment{
Host: cfg.Host,
User: cfg.User,
Port: cfg.SSHPort,
SSHKey: cfg.SSHKey,
Dir: "/root", // Default to root home for infrastructure
}
logInfo("π Provisioning Server Stack on %s...", env.Host)
// verify access
if err := runSSH(env, "id"); err != nil {
logFatal("SSH connection failed. Check host/user/key in server.yaml")
}
if !dryRun {
os.MkdirAll("build/stack", 0755)
}
// 1. Setup Traefik
provisionTraefik(env, cfg.Stack.Traefik)
// 2. Setup Authelia (if enabled)
if cfg.Stack.Traefik.Auth.Provider == "authelia" {
provisionAuthelia(env, cfg.Stack.Traefik, cfg.Stack.Authelia)
}
// 3. Setup Watchtower
provisionWatchtower(env, cfg.Stack.Watchtower)
logSuccess("β
Server Provisioning Complete.")
}
func provisionTraefik(env Environment, tCfg TraefikStack) {
logInfo("π¦ Provisioning Traefik...")
netName := tCfg.NetworkName
if netName == "" {
netName = "traefik-net"
}
// Ensure network exists (blindly try to create, ignore if exists)
// Actually better to use a systemd network unit or create it once.
// For simplicity, we'll generate a network unit.
data := TraefikTemplateData{
TraefikConfig: TraefikConfig{
Version: tCfg.Version,
Email: tCfg.Email,
Dashboard: tCfg.Dashboard,
NetworkName: netName,
CertResolver: "myresolver", // Hardcoded standard
},
HostUID: "0", // Infrastructure usually runs as root/podman
}
// We might need to check if user is root vs non-root for UID.
// For now assume root or we need to fetch UID.
if uid := getCmdOutput("ssh", append(getSSHBaseArgs(env), "id -u")...); uid != "" {
data.HostUID = uid
}
// Reuse existing templates from traefik.go (we will need to move/export them)
// For now assuming we refactor traefik.go to export templates or we move them here.
// I will assume we move the logic here or call a shared function.
// Let's implement the generation logic here, duplicating/adapting from traefik.go for now to be safe,
// then we delete traefik.go.
genFile("build/stack/traefik.yml", traefikYmlTmpl, data)
genFile("build/stack/traefik.container", strings.Replace(traefikContainerTmpl, "traefik-net", netName, -1), data)
genFile("build/stack/"+netName+".network", networkTmpl, nil)
// Sync
runSSH(env, "mkdir -p ~/traefik/dynamic_conf ~/traefik/letsencrypt ~/.config/containers/systemd")
runSSH(env, "touch ~/traefik/letsencrypt/acme.json && chmod 600 ~/traefik/letsencrypt/acme.json")
runRsync(env, []string{"build/stack/traefik.yml"}, fmt.Sprintf("%s@%s:~/traefik/", env.User, env.Host))
// Dashboard Auth (Basic)
// logic for dashboard auth... if basic?
// The new config doesn't explicitly allow setting dashboard auth hash in server.yaml yet for simplicity,
// but we can add it or just assume no auth for dashboard or basic.
// For now, skipping explicit dashboard auth setup to keep "zero-config" promise or add it later.
runRsync(env, []string{"build/stack/traefik.container", "build/stack/" + netName + ".network"},
fmt.Sprintf("%s@%s:~/.config/containers/systemd/", env.User, env.Host))
// Reload & Start
runSSH(env, "systemctl --user daemon-reload && systemctl --user restart traefik.service")
}
func provisionAuthelia(env Environment, tCfg TraefikStack, aCfg AutheliaConfig) {
logInfo("π Provisioning Authelia...")
// TODO: Generate authelia configuration.yml, users.yml, and container
// For this task, we will just create placeholders or basic setup.
logWarn("Authelia provisioning is a placeholder in this milestone.")
}
func provisionWatchtower(env Environment, wCfg WatchtowerConfig) {
logInfo("π Provisioning Watchtower...")
// TODO: Watchtower container
}