-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
90 lines (74 loc) · 2.12 KB
/
Copy pathmain.go
File metadata and controls
90 lines (74 loc) · 2.12 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
package main
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/noderax/noderax-agent/internal/agent"
"github.com/noderax/noderax-agent/internal/agentctl"
"github.com/noderax/noderax-agent/internal/api"
"github.com/noderax/noderax-agent/internal/brand"
"github.com/noderax/noderax-agent/internal/config"
"github.com/noderax/noderax-agent/internal/logger"
)
var (
version = "dev"
commit = "unknown"
buildDate = "unknown"
)
func main() {
baseLog := logger.New("info")
cli := agentctl.CLI{
Logger: baseLog,
Version: version,
Commit: commit,
BuildDate: buildDate,
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
}
cliCtx, cliStop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cliStop()
if handled, err := cli.Handle(cliCtx, os.Args[1:]); handled {
if err != nil {
baseLog.Error("command failed", "error", err)
os.Exit(1)
}
return
}
cfg, err := config.Load()
if err != nil {
fmt.Fprintf(os.Stderr, "load config: %v\n", err)
os.Exit(1)
}
log := logger.New(cfg.LogLevel)
log.Info("starting noderax agent", "version", version, "commit", commit, "build_date", buildDate)
client, err := api.NewClient(cfg.APIURL, cfg.RequestTimeout, cfg.APITLSCAFile)
if err != nil {
log.Error("configure API client", "error", err)
os.Exit(1)
}
if cfg.AgentToken != "" {
client.SetAgentToken(cfg.AgentToken)
}
if len(os.Args) > 1 && os.Args[1] == "enroll" {
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
brand.PrintLogo(os.Stdout)
if err := agent.RunInteractiveEnrollment(ctx, cfg, client, log, version, os.Stdin, os.Stdout); err != nil {
log.Error("interactive enrollment failed", "error", err)
os.Exit(1)
}
log.Info("interactive enrollment completed")
return
}
svc := agent.NewService(cfg, client, log, version)
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
if err := svc.Run(ctx); err != nil {
log.Error("agent exited with error", "error", err)
os.Exit(1)
}
log.Info("agent stopped")
}