forked from RedHatInsights/config-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
135 lines (113 loc) · 3.86 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
package main
import (
"config-manager/internal/cmd/dispatcherconsumer"
"config-manager/internal/cmd/httpapi"
"config-manager/internal/cmd/inventoryconsumer"
"config-manager/internal/config"
"config-manager/internal/db"
"config-manager/internal/logging/cloudwatch"
"context"
"flag"
"fmt"
"io"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/prometheus/client_golang/prometheus/promhttp"
clowder "github.com/redhatinsights/app-common-go/pkg/api/v1"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/peterbourgon/ff/v3"
"github.com/peterbourgon/ff/v3/ffcli"
_ "github.com/golang-migrate/migrate/v4/source/file"
)
func main() {
root := ffcli.Command{
FlagSet: config.FlagSet("config-manager", flag.ExitOnError),
Options: []ff.Option{
ff.WithEnvVarPrefix("CM"),
},
Subcommands: []*ffcli.Command{
&httpapi.Command,
&inventoryconsumer.Command,
&dispatcherconsumer.Command,
},
Exec: func(ctx context.Context, args []string) error {
modules := map[string]*ffcli.Command{
"dispatcher-consumer": &dispatcherconsumer.Command,
"http-api": &httpapi.Command,
"inventory-consumer": &inventoryconsumer.Command,
}
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGTERM, syscall.SIGINT)
for _, module := range config.DefaultConfig.Modules.Values() {
subcommand := modules[module]
go func() {
if err := subcommand.Exec(ctx, args); err != nil {
log.Fatal().Err(err).Msg("cannot run subcommand")
}
}()
}
<-quit
return nil
},
}
if err := root.Parse(os.Args[1:]); err != nil {
log.Fatal().Err(err).Msg("unable to parse flags")
}
level, err := zerolog.ParseLevel(config.DefaultConfig.LogLevel.Value)
if err != nil {
log.Fatal().Err(err).Str("level", config.DefaultConfig.LogLevel.Value).Msgf("cannot parse log level")
}
zerolog.SetGlobalLevel(level)
if level <= zerolog.DebugLevel {
log.Logger = log.With().Caller().Logger()
}
log.Debug().Interface("config", config.DefaultConfig).Send()
writers := make([]io.Writer, 0)
switch config.DefaultConfig.LogFormat.Value {
case "text":
writers = append(writers, zerolog.ConsoleWriter{Out: os.Stderr})
default:
writers = append(writers, os.Stderr)
}
if clowder.IsClowderEnabled() {
cred := credentials.NewStaticCredentials(config.DefaultConfig.AWSAccessKeyId, config.DefaultConfig.AWSSecretAccessKey, "")
awsCfg := aws.NewConfig().WithRegion(config.DefaultConfig.AWSRegion).WithCredentials(cred)
batchWriter, err := cloudwatch.NewBatchWriter(config.DefaultConfig.LogGroup, config.DefaultConfig.LogStream, awsCfg, config.DefaultConfig.LogBatchFrequency)
if err != nil {
log.Error().Err(err).Msg("cannot create CloudWatch batch writer")
}
if batchWriter != nil {
writers = append(writers, batchWriter)
}
}
log.Logger = log.Output(zerolog.MultiLevelWriter(writers...))
if config.DefaultConfig.MetricsPort > 0 {
go func() {
mux := http.NewServeMux()
mux.Handle(config.DefaultConfig.MetricsPath, promhttp.Handler())
if err := http.ListenAndServe(fmt.Sprintf("0.0.0.0:%v", config.DefaultConfig.MetricsPort), mux); err != nil {
log.Fatal().Err(err).Int("metrics-port", config.DefaultConfig.MetricsPort).Msg("cannot listen on port")
}
}()
}
connectionString := fmt.Sprintf("user=%s password=%s dbname=%s host=%s port=%d sslmode=disable",
config.DefaultConfig.DBUser,
config.DefaultConfig.DBPass,
config.DefaultConfig.DBName,
config.DefaultConfig.DBHost,
config.DefaultConfig.DBPort)
if err := db.Open("pgx", connectionString); err != nil {
log.Fatal().Err(err).Msg("cannot open database")
}
if err := db.Migrate(false); err != nil {
log.Fatal().Err(err).Msg("cannot migrate database")
}
if err := root.Run(context.Background()); err != nil {
log.Fatal().Err(err).Msg("unable to run command")
}
}