forked from amir20/dozzle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
267 lines (236 loc) · 7.6 KB
/
main.go
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package main
import (
"context"
"embed"
"io"
"io/fs"
"net"
"net/http"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
"github.com/amir20/dozzle/internal/agent"
"github.com/amir20/dozzle/internal/auth"
"github.com/amir20/dozzle/internal/docker"
"github.com/amir20/dozzle/internal/healthcheck"
"github.com/amir20/dozzle/internal/support/cli"
docker_support "github.com/amir20/dozzle/internal/support/docker"
"github.com/amir20/dozzle/internal/web"
log "github.com/sirupsen/logrus"
)
//go:embed all:dist
var content embed.FS
//go:embed shared_cert.pem shared_key.pem
var certs embed.FS
//go:generate protoc --go_out=. --go-grpc_out=. --proto_path=./protos ./protos/rpc.proto ./protos/types.proto
func main() {
cli.ValidateEnvVars(cli.Args{}, cli.AgentCmd{})
args, subcommand := cli.ParseArgs()
if subcommand != nil {
switch subcommand.(type) {
case *cli.AgentCmd:
client, err := docker.NewLocalClient(args.Filter, args.Hostname)
if err != nil {
log.Fatalf("Could not create docker client: %v", err)
}
certs, err := cli.ReadCertificates(certs)
if err != nil {
log.Fatalf("Could not read certificates: %v", err)
}
listener, err := net.Listen("tcp", args.Agent.Addr)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
tempFile, err := os.CreateTemp("./", "agent-*.addr")
if err != nil {
log.Fatalf("failed to create temp file: %v", err)
}
io.WriteString(tempFile, listener.Addr().String())
go cli.StartEvent(args, "", client, "agent")
server := agent.NewServer(client, certs, args.Version())
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
go func() {
log.Infof("Dozzle agent version %s", args.Version())
log.Infof("Agent listening on %s", listener.Addr().String())
if err := server.Serve(listener); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}()
<-ctx.Done()
stop()
log.Info("Shutting down agent")
server.Stop()
log.Debugf("deleting %s", tempFile.Name())
os.Remove(tempFile.Name())
case *cli.HealthcheckCmd:
go cli.StartEvent(args, "", nil, "healthcheck")
files, err := os.ReadDir(".")
if err != nil {
log.Fatalf("Failed to read directory: %v", err)
}
agentAddress := ""
for _, file := range files {
if match, _ := filepath.Match("agent-*.addr", file.Name()); match {
data, err := os.ReadFile(file.Name())
if err != nil {
log.Fatalf("Failed to read file: %v", err)
}
agentAddress = string(data)
break
}
}
if agentAddress == "" {
if err := healthcheck.HttpRequest(args.Addr, args.Base); err != nil {
log.Fatalf("Failed to make request: %v", err)
}
} else {
certs, err := cli.ReadCertificates(certs)
if err != nil {
log.Fatalf("Could not read certificates: %v", err)
}
if err := healthcheck.RPCRequest(agentAddress, certs); err != nil {
log.Fatalf("Failed to make request: %v", err)
}
}
case *cli.GenerateCmd:
go cli.StartEvent(args, "", nil, "generate")
if args.Generate.Username == "" || args.Generate.Password == "" {
log.Fatal("Username and password are required")
}
buffer := auth.GenerateUsers(auth.User{
Username: args.Generate.Username,
Password: args.Generate.Password,
Name: args.Generate.Name,
Email: args.Generate.Email,
}, true)
if _, err := os.Stdout.Write(buffer.Bytes()); err != nil {
log.Fatalf("Failed to write to stdout: %v", err)
}
}
os.Exit(0)
}
if args.AuthProvider != "none" && args.AuthProvider != "forward-proxy" && args.AuthProvider != "simple" {
log.Fatalf("Invalid auth provider %s", args.AuthProvider)
}
log.Infof("Dozzle version %s", args.Version())
var multiHostService *docker_support.MultiHostService
if args.Mode == "server" {
var localClient docker.Client
localClient, multiHostService = cli.CreateMultiHostService(certs, args)
if multiHostService.TotalClients() == 0 {
log.Fatal("Could not connect to any Docker Engines")
} else {
log.Infof("Connected to %d Docker Engine(s)", multiHostService.TotalClients())
}
go cli.StartEvent(args, "server", localClient, "")
} else if args.Mode == "swarm" {
localClient, err := docker.NewLocalClient(args.Filter, args.Hostname)
if err != nil {
log.Fatalf("Could not connect to local Docker Engine: %s", err)
}
certs, err := cli.ReadCertificates(certs)
if err != nil {
log.Fatalf("Could not read certificates: %v", err)
}
manager := docker_support.NewSwarmClientManager(localClient, certs)
multiHostService = docker_support.NewMultiHostService(manager)
log.Infof("Starting in Swarm mode")
listener, err := net.Listen("tcp", ":7007")
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
server := agent.NewServer(localClient, certs, args.Version())
go cli.StartEvent(args, "swarm", localClient, "")
go func() {
log.Infof("Agent listening on %s", listener.Addr().String())
if err := server.Serve(listener); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}()
} else {
log.Fatalf("Invalid mode %s", args.Mode)
}
srv := createServer(args, multiHostService)
go func() {
log.Infof("Accepting connections on %s", srv.Addr)
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
log.Fatal(err)
}
}()
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
<-ctx.Done()
stop()
log.Info("shutting down gracefully, press Ctrl+C again to force")
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatal(err)
}
log.Debug("shutdown complete")
}
func createServer(args cli.Args, multiHostService *docker_support.MultiHostService) *http.Server {
_, dev := os.LookupEnv("DEV")
var provider web.AuthProvider = web.NONE
var authorizer web.Authorizer
if args.AuthProvider == "forward-proxy" {
log.Debug("Using forward proxy authentication")
provider = web.FORWARD_PROXY
authorizer = auth.NewForwardProxyAuth(args.AuthHeaderUser, args.AuthHeaderEmail, args.AuthHeaderName)
} else if args.AuthProvider == "simple" {
log.Debug("Using simple authentication")
provider = web.SIMPLE
path, err := filepath.Abs("./data/users.yml")
if err != nil {
log.Fatalf("Could not find absolute path to users.yml file: %s", err)
}
if _, err := os.Stat(path); os.IsNotExist(err) {
log.Fatalf("Could not find users.yml file at %s", path)
}
log.Debugf("Reading users from %s", path)
db, err := auth.ReadUsersFromFile(path)
if err != nil {
log.Fatalf("Could not read users.yml file at %s: %s", path, err)
}
log.Debugf("Read %d users", len(db.Users))
authorizer = auth.NewSimpleAuth(db)
}
config := web.Config{
Addr: args.Addr,
Base: args.Base,
Version: args.Version(),
Hostname: args.Hostname,
NoAnalytics: args.NoAnalytics,
Dev: dev,
Authorization: web.Authorization{
Provider: provider,
Authorizer: authorizer,
},
EnableActions: args.EnableActions,
}
assets, err := fs.Sub(content, "dist")
if err != nil {
log.Fatalf("Could not open embedded dist folder: %v", err)
}
if _, ok := os.LookupEnv("LIVE_FS"); ok {
if dev {
log.Info("Using live filesystem at ./public")
assets = os.DirFS("./public")
} else {
log.Info("Using live filesystem at ./dist")
assets = os.DirFS("./dist")
}
}
if !dev {
if _, err := assets.Open(".vite/manifest.json"); err != nil {
log.Fatal(".vite/manifest.json not found")
}
if _, err := assets.Open("index.html"); err != nil {
log.Fatal("index.html not found")
}
}
return web.CreateServer(multiHostService, assets, config)
}