-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
287 lines (237 loc) · 7.02 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
// SPDX-FileCopyrightText: 2023 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/alecthomas/kong"
"github.com/goschtalt/goschtalt"
"github.com/xmidt-org/sallust"
"github.com/xmidt-org/xmidt-agent/internal/adapters/libparodus"
"github.com/xmidt-org/xmidt-agent/internal/credentials"
"github.com/xmidt-org/xmidt-agent/internal/loglevel"
"github.com/xmidt-org/xmidt-agent/internal/metadata"
"github.com/xmidt-org/xmidt-agent/internal/websocket"
"github.com/xmidt-org/xmidt-agent/internal/wrphandlers/qos"
"go.uber.org/fx"
"go.uber.org/fx/fxevent"
"go.uber.org/zap"
)
const (
applicationName = "xmidt-agent"
)
// These match what goreleaser provides.
var (
commit = "undefined"
version = "undefined"
date = "undefined"
builtBy = "undefined"
)
// CLI is the structure that is used to capture the command line arguments.
type CLI struct {
Dev bool `optional:"" short:"d" help:"Run in development mode."`
Show bool `optional:"" short:"s" help:"Show the configuration and exit."`
Graph string `optional:"" short:"g" help:"Output the dependency graph to the specified file."`
Files []string `optional:"" short:"f" help:"Specific configuration files or directories."`
}
type LifeCycleIn struct {
fx.In
Logger *zap.Logger
LC fx.Lifecycle
Shutdowner fx.Shutdowner
WS *websocket.Websocket
LibParodus *libparodus.Adapter
QOS *qos.Handler
Cred *credentials.Credentials
WaitUntilFetched time.Duration `name:"wait_until_fetched"`
Cancels []func() `group:"cancels"`
}
// xmidtAgent is the main entry point for the program. It is responsible for
// setting up the dependency injection framework and returning the app object.
func xmidtAgent(args []string) (*fx.App, error) {
app := fx.New(provideAppOptions(args))
if err := app.Err(); err != nil {
return nil, err
}
return app, nil
}
// provideAppOptions returns all fx options required to start the xmidt agent fx app.
func provideAppOptions(args []string) fx.Option {
var (
gscfg *goschtalt.Config
// Capture the dependency tree in case we need to debug something.
g fx.DotGraph
// Capture the command line arguments.
cli *CLI
)
opts := fx.Options(
fx.Supply(cliArgs(args)),
fx.Populate(&g),
fx.Populate(&gscfg),
fx.Populate(&cli),
fx.WithLogger(func(log *zap.Logger) fxevent.Logger {
return &fxevent.ZapLogger{Logger: log}
}),
fx.Provide(
provideCLI,
provideLogger,
provideConfig,
provideCredentials,
provideInstructions,
provideWS,
provideLibParodus,
goschtalt.UnmarshalFunc[sallust.Config]("logger", goschtalt.Optional()),
goschtalt.UnmarshalFunc[Identity]("identity"),
goschtalt.UnmarshalFunc[OperationalState]("operational_state"),
goschtalt.UnmarshalFunc[XmidtCredentials]("xmidt_credentials"),
goschtalt.UnmarshalFunc[XmidtService]("xmidt_service"),
goschtalt.UnmarshalFunc[Storage]("storage"),
goschtalt.UnmarshalFunc[Websocket]("websocket"),
goschtalt.UnmarshalFunc[MockTr181]("mock_tr_181"),
goschtalt.UnmarshalFunc[Pubsub]("pubsub"),
goschtalt.UnmarshalFunc[Metadata]("metadata"),
goschtalt.UnmarshalFunc[NetworkService]("network_service"),
goschtalt.UnmarshalFunc[QOS]("qos"),
goschtalt.UnmarshalFunc[LibParodus]("lib_parodus"),
provideNetworkService,
provideMetadataProvider,
loglevel.New,
metadata.NewInterfaceUsedProvider,
),
fsProvide(),
provideWRPHandlers(),
fx.Invoke(
lifeCycle,
),
)
if cli != nil && cli.Graph != "" {
_ = os.WriteFile(cli.Graph, []byte(g), 0600)
}
return opts
}
func main() {
app, err := xmidtAgent(os.Args[1:])
if err == nil {
app.Run()
return
}
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}
// Provides a named type so it's a bit easier to flow through & use in fx.
type cliArgs []string
// Handle the CLI processing and return the processed input.
func provideCLI(args cliArgs) (*CLI, error) {
return provideCLIWithOpts(args, false)
}
func provideCLIWithOpts(args cliArgs, testOpts bool) (*CLI, error) {
var cli CLI
// Create a no-op option to satisfy the kong.New() call.
var opt kong.Option = kong.OptionFunc(
func(*kong.Kong) error {
return nil
},
)
if testOpts {
opt = kong.Writers(nil, nil)
}
parser, err := kong.New(&cli,
kong.Name(applicationName),
kong.Description("The cpe agent for Xmidt service.\n"+
fmt.Sprintf("\tVersion: %s\n", version)+
fmt.Sprintf("\tDate: %s\n", date)+
fmt.Sprintf("\tCommit: %s\n", commit)+
fmt.Sprintf("\tBuilt By: %s\n", builtBy),
),
kong.UsageOnError(),
opt,
)
if err != nil {
return nil, err
}
if testOpts {
parser.Exit = func(_ int) { panic("exit") }
}
_, err = parser.Parse(args)
if err != nil {
parser.FatalIfErrorf(err)
}
return &cli, nil
}
type LoggerIn struct {
fx.In
CLI *CLI
Cfg sallust.Config
}
// Create the logger and configure it based on if the program is in
// debug mode or normal mode.
func provideLogger(in LoggerIn) (*zap.AtomicLevel, *zap.Logger, error) {
if in.CLI.Dev {
in.Cfg.EncoderConfig.EncodeLevel = "capitalColor"
in.Cfg.EncoderConfig.EncodeTime = "RFC3339"
in.Cfg.Level = "DEBUG"
in.Cfg.Development = true
in.Cfg.Encoding = "console"
in.Cfg.OutputPaths = append(in.Cfg.OutputPaths, "stderr")
in.Cfg.ErrorOutputPaths = append(in.Cfg.ErrorOutputPaths, "stderr")
}
zcfg, err := in.Cfg.NewZapConfig()
if err != nil {
return nil, nil, err
}
logger, err := in.Cfg.Build()
return &zcfg.Level, logger, err
}
func onStart(cred *credentials.Credentials, ws *websocket.Websocket, libParodus *libparodus.Adapter, qos *qos.Handler, waitUntilFetched time.Duration, logger *zap.Logger) func(context.Context) error {
logger = logger.Named("on_start")
return func(ctx context.Context) (err error) {
if err = ctx.Err(); err != nil {
return err
}
if ws == nil {
logger.Debug("websocket disabled")
return err
}
// Allow operations where no credentials are desired (cred will be nil).
if cred != nil {
ctx, cancel := context.WithTimeout(ctx, waitUntilFetched)
defer cancel()
// blocks until an attempt to fetch the credentials has been made or the context is canceled
cred.WaitUntilFetched(ctx)
}
ws.Start()
err = libParodus.Start()
qos.Start()
return err
}
}
func onStop(ws *websocket.Websocket, libParodus *libparodus.Adapter, qos *qos.Handler, shutdowner fx.Shutdowner, cancels []func(), logger *zap.Logger) func(context.Context) error {
logger = logger.Named("on_stop")
return func(context.Context) (err error) {
if ws == nil {
logger.Debug("websocket disabled")
return nil
}
ws.Stop()
libParodus.Stop()
qos.Stop()
for _, c := range cancels {
if c == nil {
continue
}
c()
}
return nil
}
}
func lifeCycle(in LifeCycleIn) {
logger := in.Logger.Named("fx_lifecycle")
in.LC.Append(
fx.Hook{
OnStart: onStart(in.Cred, in.WS, in.LibParodus, in.QOS, in.WaitUntilFetched, logger),
OnStop: onStop(in.WS, in.LibParodus, in.QOS, in.Shutdowner, in.Cancels, logger),
},
)
}