-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
248 lines (215 loc) · 6.76 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
package main
import (
"archie/archie"
"archie/client"
"context"
"encoding/json"
"flag"
"github.com/kkyr/fig"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"go.arsenm.dev/pcre"
"path/filepath"
"sync"
"time"
)
func main() {
zerolog.TimeFieldFormat = time.RFC3339Nano
configFile := flag.String("config", "config.yaml", "config file path")
logLevelFlag := flag.String("log-level", LookupEnvOrString("LOG_LEVEL", ""), "set the log level (default: info)")
flag.Parse()
var cfg Config
err := fig.Load(&cfg, fig.File(filepath.Base(*configFile)), fig.Dirs(".", filepath.Dir(*configFile)))
if err != nil {
log.Fatal().Err(err).Msg("Failed to load config file")
}
log.Info().
Str("version", archie.Version).
Str("releaseTag", archie.ReleaseTag).
Str("commit", archie.ShortCommitID).
Str("buildDate", archie.BuildDate).
Msg("Starting archie")
// compile and validate pcre regex exclude patterns
var excludedPathCopyObject, excludedPathRemoveObject []*pcre.Regexp
if len(cfg.ExcludePaths.CopyObject) > 0 || len(cfg.ExcludePaths.RemoveObject) > 0 {
for _, excludedPathPattern := range cfg.ExcludePaths.CopyObject {
excludedPathRegexp, err := pcre.Compile(excludedPathPattern)
if err != nil {
log.Fatal().Err(err).Str("pattern", excludedPathPattern).Msg("Failed to compile CopyObject pcre regex")
}
excludedPathCopyObject = append(excludedPathCopyObject, excludedPathRegexp)
}
for _, excludedPathPattern := range cfg.ExcludePaths.RemoveObject {
excludedPathRegexp, err := pcre.Compile(excludedPathPattern)
if err != nil {
log.Fatal().Err(err).Str("pattern", excludedPathPattern).Msg("Failed to compile RemoveObject pcre regex")
}
excludedPathRemoveObject = append(excludedPathRemoveObject, excludedPathRegexp)
}
log.Info().Msgf("Regex patterns compiled with pcre v%s", pcre.Version())
}
var logLevel string
// prefer cli arg over config
if *logLevelFlag != "" {
logLevel = *logLevelFlag
} else {
logLevel = cfg.LogLevel
}
zerolog.SetGlobalLevel(zerolog.InfoLevel)
if logLevel == "trace" {
log.Info().Msg("Trace logging enabled")
zerolog.SetGlobalLevel(zerolog.TraceLevel)
} else if logLevel == "debug" {
log.Info().Msg("Debug logging enabled")
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
// base context - cancel message processing (give time to let active transfers finish)
baseCtx, baseCancel := context.WithCancel(context.Background())
defer func() {
log.Trace().Msg("Deferred base context canceled")
baseCancel()
}()
// msg context - cancel active transfers (immediately)
msgCtx, msgCancel := context.WithCancel(context.Background())
defer func() {
log.Trace().Msg("Deferred msg context canceled")
msgCancel()
}()
// log all config settings
redactedCfg := cfg
if redactedCfg.Src.AccessKey != "" {
redactedCfg.Src.AccessKey = "REDACTED"
}
if redactedCfg.Src.SecretKey != "" {
redactedCfg.Src.SecretKey = "REDACTED"
}
if redactedCfg.Src.GoogleCredentials != "" {
redactedCfg.Src.GoogleCredentials = "REDACTED"
}
if redactedCfg.Dest.AccessKey != "" {
redactedCfg.Dest.AccessKey = "REDACTED"
}
if redactedCfg.Dest.SecretKey != "" {
redactedCfg.Dest.SecretKey = "REDACTED"
}
if redactedCfg.Dest.GoogleCredentials != "" {
redactedCfg.Dest.GoogleCredentials = "REDACTED"
}
if redactedCfg.Jetstream.Password != "" {
redactedCfg.Jetstream.Password = "REDACTED"
}
redactedCfgJSON, err := json.Marshal(redactedCfg)
if err != nil {
log.Fatal().Err(err).Msg("Failed to marshal config to json")
}
log.Info().RawJSON("cfg", redactedCfgJSON).Msg("Startup configuration")
// archiver
a := archie.Archiver{
BackoffDurationMultiplier: cfg.BackoffDurationMultiplier,
BackoffNumCeiling: cfg.BackoffNumCeiling,
DestBucket: cfg.Dest.Bucket,
DestName: cfg.Dest.Name,
DestPartSize: cfg.Dest.PartSize,
DestThreads: cfg.Dest.Threads,
FetchDone: make(chan string, 1),
HealthCheckDisabled: cfg.HealthCheck.Disabled,
MaxRetries: cfg.MaxRetries,
MsgTimeout: cfg.MsgTimeout,
SkipEventBucketValidation: cfg.SkipEventBucketValidation,
SkipLifecycleExpired: cfg.SkipLifecycleExpired,
SrcBucket: cfg.Src.Bucket,
SrcName: cfg.Src.Name,
WaitForMatchingETag: cfg.WaitForMatchingETag,
WaitGroup: &sync.WaitGroup{},
ExcludePaths: struct {
CopyObject []*pcre.Regexp
RemoveObject []*pcre.Regexp
}{
CopyObject: excludedPathCopyObject,
RemoveObject: excludedPathRemoveObject,
},
}
var srcHealthCheckCancel, destHealthCheckCancel context.CancelFunc
// source
var c client.Client
if cfg.Src.GoogleCredentials != "" {
c = &client.GCS{}
} else {
c = &client.Minio{}
}
srcHealthCheckCancel = c.New(
baseCtx,
cfg.Src.Name,
cfg.Src.Bucket,
cfg.Src.Endpoint,
client.Credentials{
MinioSecretAccessKey: cfg.Src.SecretKey,
MinioAccessKey: cfg.Src.AccessKey,
GoogleCredentials: cfg.Src.GoogleCredentials,
},
cfg.Src.UseSSL,
client.Params{},
zerolog.GlobalLevel(),
)
a.SrcClient = c
defer func() {
log.Trace().Msg("Deferred source health check context canceled")
srcHealthCheckCancel()
}()
// destination
var d client.Client
if cfg.Dest.GoogleCredentials != "" {
d = &client.GCS{}
} else {
d = &client.Minio{}
}
destHealthCheckCancel = d.New(
baseCtx,
cfg.Dest.Name,
cfg.Dest.Bucket,
cfg.Dest.Endpoint,
client.Credentials{
MinioSecretAccessKey: cfg.Dest.SecretKey,
MinioAccessKey: cfg.Dest.AccessKey,
GoogleCredentials: cfg.Dest.GoogleCredentials,
},
cfg.Dest.UseSSL,
client.Params{
Threads: cfg.Dest.Threads,
PartSize: cfg.Dest.PartSize,
},
zerolog.GlobalLevel(),
)
a.DestClient = d
defer func() {
log.Trace().Msg("Deferred destination health check context canceled")
destHealthCheckCancel()
}()
// queue
jetStreamSub, jetStreamConn := client.JetStream(
cfg.Jetstream.URL,
cfg.Jetstream.Subject,
cfg.Jetstream.Stream.Name,
cfg.Jetstream.Consumer.Name,
cfg.Jetstream.Stream.MaxAge,
cfg.Jetstream.RootCA,
cfg.Jetstream.Username,
cfg.Jetstream.Password,
cfg.Jetstream.Stream.Replicas,
cfg.Jetstream.Consumer.MaxAckPending,
cfg.Jetstream.Stream.MaxSize,
cfg.MsgTimeout,
cfg.Jetstream.Stream.Retention,
cfg.Jetstream.Stream.RepublishSubject,
cfg.Jetstream.ProvisioningDisabled,
)
// health check server
healthCheckSrv := a.StartHealthCheckServer(cfg.HealthCheck.Port, jetStreamConn)
// metrics server
metricsSrv := a.StartMetricsServer(cfg.Metrics.Port)
// single-thread message processor
go a.MessageProcessor(baseCtx, msgCtx, jetStreamSub, cfg.Jetstream.BatchSize)
// shutdown manager
a.WaitForSignal(cfg.ShutdownWait, baseCancel, msgCancel, healthCheckSrv, metricsSrv)
log.Info().Msg("Shutdown complete")
}