-
Notifications
You must be signed in to change notification settings - Fork 321
/
main.go
314 lines (277 loc) · 9.47 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
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"
"github.com/rs/zerolog/log"
)
//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.Fatal().Err(err).Msg("Could not create docker client")
}
certs, err := cli.ReadCertificates(certs)
if err != nil {
log.Fatal().Err(err).Msg("Could not read certificates")
}
listener, err := net.Listen("tcp", args.Agent.Addr)
if err != nil {
log.Fatal().Err(err).Msg("failed to listen")
}
tempFile, err := os.CreateTemp("./", "agent-*.addr")
if err != nil {
log.Fatal().Err(err).Msg("failed to create temp file")
}
io.WriteString(tempFile, listener.Addr().String())
go cli.StartEvent(args, "", client, "agent")
server, err := agent.NewServer(client, certs, args.Version())
if err != nil {
log.Fatal().Err(err).Msg("failed to create agent server")
}
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
go func() {
log.Info().Msgf("Dozzle agent version %s", args.Version())
log.Info().Msgf("Agent listening on %s", listener.Addr().String())
if err := server.Serve(listener); err != nil {
log.Error().Err(err).Msg("failed to serve")
}
}()
<-ctx.Done()
stop()
log.Info().Msg("Shutting down agent")
server.Stop()
log.Debug().Str("file", tempFile.Name()).Msg("Removing temp file")
os.Remove(tempFile.Name())
case *cli.HealthcheckCmd:
files, err := os.ReadDir(".")
if err != nil {
log.Fatal().Err(err).Msg("Failed to read directory")
}
agentAddress := ""
for _, file := range files {
if match, _ := filepath.Match("agent-*.addr", file.Name()); match {
data, err := os.ReadFile(file.Name())
if err != nil {
log.Fatal().Err(err).Msg("Failed to read file")
}
agentAddress = string(data)
break
}
}
if agentAddress == "" {
if err := healthcheck.HttpRequest(args.Addr, args.Base); err != nil {
log.Fatal().Err(err).Msg("Failed to make request")
}
} else {
certs, err := cli.ReadCertificates(certs)
if err != nil {
log.Fatal().Err(err).Msg("Could not read certificates")
}
ctx, cancel := context.WithTimeout(context.Background(), args.Timeout)
defer cancel()
if err := healthcheck.RPCRequest(ctx, agentAddress, certs); err != nil {
log.Fatal().Err(err).Msg("Failed to make request")
}
}
case *cli.GenerateCmd:
cli.StartEvent(args, "", nil, "generate")
if args.Generate.Username == "" || args.Generate.Password == "" {
log.Fatal().Msg("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.Fatal().Err(err).Msg("Failed to write to stdout")
}
case *cli.AgentTestCmd:
certs, err := cli.ReadCertificates(certs)
if err != nil {
log.Fatal().Err(err).Msg("Could not read certificates")
}
log.Info().Str("endpoint", args.AgentTest.Address).Msg("Connecting to agent")
agent, err := agent.NewClient(args.AgentTest.Address, certs)
if err != nil {
log.Fatal().Err(err).Str("endpoint", args.AgentTest.Address).Msg("error connecting to agent")
}
ctx, cancel := context.WithTimeout(context.Background(), args.Timeout)
defer cancel()
host, err := agent.Host(ctx)
if err != nil {
log.Fatal().Err(err).Str("endpoint", args.AgentTest.Address).Msg("error fetching host info for agent")
}
log.Info().Str("endpoint", args.AgentTest.Address).Str("version", host.AgentVersion).Str("name", host.Name).Str("id", host.ID).Msg("Successfully connected to agent")
}
os.Exit(0)
}
if args.AuthProvider != "none" && args.AuthProvider != "forward-proxy" && args.AuthProvider != "simple" {
log.Fatal().Str("provider", args.AuthProvider).Msg("Invalid auth provider")
}
log.Info().Msgf("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().Msg("Could not connect to any Docker Engine")
} else {
log.Info().Int("clients", multiHostService.TotalClients()).Msg("Connected to Docker")
}
go cli.StartEvent(args, "server", localClient, "")
} else if args.Mode == "swarm" {
localClient, err := docker.NewLocalClient(args.Filter, args.Hostname)
if err != nil {
log.Fatal().Err(err).Msg("Could not create docker client")
}
certs, err := cli.ReadCertificates(certs)
if err != nil {
log.Fatal().Err(err).Msg("Could not read certificates")
}
agentManager := docker_support.NewRetriableClientManager(args.RemoteAgent, args.Timeout, certs)
manager := docker_support.NewSwarmClientManager(localClient, certs, args.Timeout, agentManager)
multiHostService = docker_support.NewMultiHostService(manager, args.Timeout)
log.Info().Msg("Starting in swarm mode")
listener, err := net.Listen("tcp", ":7007")
if err != nil {
log.Fatal().Err(err).Msg("failed to listen")
}
server, err := agent.NewServer(localClient, certs, args.Version())
if err != nil {
log.Fatal().Err(err).Msg("failed to create agent")
}
go cli.StartEvent(args, "swarm", localClient, "")
go func() {
log.Info().Msgf("Dozzle agent version %s", args.Version())
if err := server.Serve(listener); err != nil {
log.Error().Err(err).Msg("failed to serve")
}
}()
} else {
log.Fatal().Str("mode", args.Mode).Msg("Invalid mode")
}
srv := createServer(args, multiHostService)
go func() {
log.Info().Msgf("Accepting connections on %s", args.Addr)
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
log.Fatal().Err(err).Msg("failed to listen")
}
}()
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
<-ctx.Done()
stop()
log.Info().Msg("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.Error().Err(err).Msg("failed to shut down")
}
log.Debug().Msg("shut down 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().Msg("Using forward proxy authentication")
provider = web.FORWARD_PROXY
authorizer = auth.NewForwardProxyAuth(args.AuthHeaderUser, args.AuthHeaderEmail, args.AuthHeaderName)
} else if args.AuthProvider == "simple" {
log.Debug().Msg("Using simple authentication")
provider = web.SIMPLE
path, err := filepath.Abs("./data/users.yml")
if err != nil {
log.Fatal().Err(err).Msg("Could not get absolute path")
}
if _, err := os.Stat(path); os.IsNotExist(err) {
log.Fatal().Msg("users.yml file does not exist")
}
log.Debug().Str("path", path).Msg("Reading users.yml file")
db, err := auth.ReadUsersFromFile(path)
if err != nil {
log.Fatal().Err(err).Msg("Could not read users.yml file")
}
log.Debug().Int("users", len(db.Users)).Msg("Loaded users")
ttl := time.Duration(0)
if args.AuthTTL != "session" {
ttl, err = time.ParseDuration(args.AuthTTL)
if err != nil {
log.Fatal().Err(err).Msg("Could not parse auth ttl")
}
}
authorizer = auth.NewSimpleAuth(db, ttl)
}
authTTL := time.Duration(0)
if args.AuthTTL != "session" {
ttl, err := time.ParseDuration(args.AuthTTL)
if err != nil {
log.Fatal().Err(err).Msg("Could not parse auth ttl")
}
authTTL = ttl
}
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,
TTL: authTTL,
},
EnableActions: args.EnableActions,
}
assets, err := fs.Sub(content, "dist")
if err != nil {
log.Fatal().Err(err).Msg("Could not get sub filesystem")
}
if _, ok := os.LookupEnv("LIVE_FS"); ok {
if dev {
log.Info().Msg("Using live filesystem at ./public")
assets = os.DirFS("./public")
} else {
log.Info().Msg("Using live filesystem at ./dist")
assets = os.DirFS("./dist")
}
}
if !dev {
if _, err := assets.Open(".vite/manifest.json"); err != nil {
log.Fatal().Msg("manifest.json not found")
}
if _, err := assets.Open("index.html"); err != nil {
log.Fatal().Msg("index.html not found")
}
}
return web.CreateServer(multiHostService, assets, config)
}