-
Notifications
You must be signed in to change notification settings - Fork 148
/
run.go
506 lines (444 loc) · 14 KB
/
run.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package cmd
import (
"context"
"fmt"
"io/ioutil"
"net/url"
"os"
"os/signal"
"path/filepath"
"syscall"
"github.com/elastic/elastic-agent-libs/logp"
"github.com/spf13/cobra"
"go.elastic.co/apm"
apmtransport "go.elastic.co/apm/transport"
"gopkg.in/yaml.v2"
monitoringLib "github.com/elastic/elastic-agent-libs/monitoring"
"github.com/elastic/elastic-agent-libs/api"
"github.com/elastic/elastic-agent-libs/service"
"github.com/elastic/elastic-agent-system-metrics/report"
"github.com/elastic/elastic-agent/internal/pkg/agent/application"
"github.com/elastic/elastic-agent/internal/pkg/agent/application/coordinator"
"github.com/elastic/elastic-agent/internal/pkg/agent/application/filelock"
"github.com/elastic/elastic-agent/internal/pkg/agent/application/info"
"github.com/elastic/elastic-agent/internal/pkg/agent/application/monitoring"
"github.com/elastic/elastic-agent/internal/pkg/agent/application/paths"
"github.com/elastic/elastic-agent/internal/pkg/agent/application/reexec"
"github.com/elastic/elastic-agent/internal/pkg/agent/application/secret"
"github.com/elastic/elastic-agent/internal/pkg/agent/application/upgrade"
"github.com/elastic/elastic-agent/internal/pkg/agent/configuration"
"github.com/elastic/elastic-agent/internal/pkg/agent/control/v2/server"
"github.com/elastic/elastic-agent/internal/pkg/agent/errors"
"github.com/elastic/elastic-agent/internal/pkg/agent/storage"
"github.com/elastic/elastic-agent/internal/pkg/cli"
"github.com/elastic/elastic-agent/internal/pkg/config"
monitoringCfg "github.com/elastic/elastic-agent/internal/pkg/core/monitoring/config"
"github.com/elastic/elastic-agent/internal/pkg/diagnostics"
"github.com/elastic/elastic-agent/internal/pkg/release"
"github.com/elastic/elastic-agent/pkg/component"
"github.com/elastic/elastic-agent/pkg/core/logger"
"github.com/elastic/elastic-agent/version"
)
const (
agentName = "elastic-agent"
)
type cfgOverrider func(cfg *configuration.Configuration)
func newRunCommandWithArgs(_ []string, streams *cli.IOStreams) *cobra.Command {
return &cobra.Command{
Use: "run",
Short: "Start the elastic-agent.",
Run: func(_ *cobra.Command, _ []string) {
if err := run(nil); err != nil && !errors.Is(err, context.Canceled) {
fmt.Fprintf(streams.Err, "Error: %v\n%s\n", err, troubleshootMessage())
// TODO: remove it. os.Exit will be called on main and if it's called
// too early some goroutines with deferred functions related
// to the shutdown process might not run.
os.Exit(1)
}
},
}
}
func run(override cfgOverrider, modifiers ...component.PlatformModifier) error {
// Windows: Mark service as stopped.
// After this is run, the service is considered by the OS to be stopped.
// This must be the first deferred cleanup task (last to execute).
defer func() {
service.NotifyTermination()
service.WaitExecutionDone()
}()
locker := filelock.NewAppLocker(paths.Data(), paths.AgentLockFileName)
if err := locker.TryLock(); err != nil {
return err
}
defer func() {
_ = locker.Unlock()
}()
service.BeforeRun()
defer service.Cleanup()
// register as a service
stop := make(chan bool)
ctx, cancel := context.WithCancel(context.Background())
var stopBeat = func() {
close(stop)
}
service.HandleSignals(stopBeat, cancel)
cfg, err := loadConfig(override)
if err != nil {
return err
}
logLvl := logger.DefaultLogLevel
if cfg.Settings.LoggingConfig != nil {
logLvl = cfg.Settings.LoggingConfig.Level
}
l, err := logger.NewFromConfig("", cfg.Settings.LoggingConfig, true)
if err != nil {
return err
}
cfg, err = tryDelayEnroll(ctx, l, cfg, override)
if err != nil {
err = errors.New(err, "failed to perform delayed enrollment")
l.Error(err)
return err
}
pathConfigFile := paths.AgentConfigFile()
// agent ID needs to stay empty in bootstrap mode
createAgentID := true
if cfg.Fleet != nil && cfg.Fleet.Server != nil && cfg.Fleet.Server.Bootstrap {
createAgentID = false
}
// Ensure we have the agent secret created.
// The secret is not created here if it exists already from the previous enrollment.
// This is needed for compatibility with agent running in standalone mode,
// that writes the agentID into fleet.enc (encrypted fleet.yml) before even loading the configuration.
err = secret.CreateAgentSecret()
if err != nil {
return fmt.Errorf("failed to read/write secrets: %w", err)
}
agentInfo, err := info.NewAgentInfoWithLog(defaultLogLevel(cfg), createAgentID)
if err != nil {
return errors.New(err,
"could not load agent info",
errors.TypeFilesystem,
errors.M(errors.MetaKeyPath, pathConfigFile))
}
// Ensure that the log level now matches what is configured in the agentInfo.
if agentInfo.LogLevel() != "" {
var lvl logp.Level
err = lvl.Unpack(agentInfo.LogLevel())
if err != nil {
l.Error(errors.New(err, "failed to parse agent information log level"))
} else {
logLvl = lvl
logger.SetLevel(lvl)
}
}
// initiate agent watcher
if err := upgrade.InvokeWatcher(l); err != nil {
// we should not fail because watcher is not working
l.Error(errors.New(err, "failed to invoke rollback watcher"))
}
if allowEmptyPgp, _ := release.PGP(); allowEmptyPgp {
l.Info("Elastic Agent has been built with security disabled. Elastic Agent will not verify signatures of upgrade artifact.")
}
execPath, err := reexecPath()
if err != nil {
return err
}
rexLogger := l.Named("reexec")
rex := reexec.NewManager(rexLogger, execPath)
tracer, err := initTracer(agentName, release.Version(), cfg.Settings.MonitoringConfig)
if err != nil {
return fmt.Errorf("could not initiate APM tracer: %w", err)
}
if tracer != nil {
l.Info("APM instrumentation enabled")
defer func() {
tracer.Flush(nil)
tracer.Close()
}()
} else {
l.Info("APM instrumentation disabled")
}
coord, err := application.New(l, logLvl, agentInfo, rex, tracer, configuration.IsFleetServerBootstrap(cfg.Fleet), modifiers...)
if err != nil {
return err
}
serverStopFn, err := setupMetrics(l, cfg.Settings.DownloadConfig.OS(), cfg.Settings.MonitoringConfig, tracer, coord)
if err != nil {
return err
}
defer func() {
_ = serverStopFn()
}()
diagHooks := diagnostics.GlobalHooks()
diagHooks = append(diagHooks, coord.DiagnosticHooks()...)
control := server.New(l.Named("control"), agentInfo, coord, tracer, diagHooks, cfg.Settings.GRPC)
// start the control listener
if err := control.Start(); err != nil {
return err
}
defer control.Stop()
appDone := make(chan bool)
appErr := make(chan error)
go func() {
err := coord.Run(ctx)
close(appDone)
appErr <- err
}()
// listen for signals
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGHUP)
isRex := false
logShutdown := true
LOOP:
for {
select {
case <-stop:
l.Info("service.HandleSignals invoked stop function. Shutting down")
break LOOP
case <-appDone:
l.Info("application done, coordinator exited")
logShutdown = false
break LOOP
case <-rex.ShutdownChan():
l.Info("reexec shutdown channel triggered")
isRex = true
logShutdown = false
break LOOP
case sig := <-signals:
l.Infof("signal %q received", sig)
if sig == syscall.SIGHUP {
rexLogger.Infof("SIGHUP triggered re-exec")
isRex = true
rex.ReExec(nil)
} else {
break LOOP
}
}
}
if logShutdown {
l.Info("Shutting down Elastic Agent and sending last events...")
}
cancel()
err = <-appErr
if logShutdown {
l.Info("Shutting down completed.")
}
if isRex {
rex.ShutdownComplete()
}
return err
}
func loadConfig(override cfgOverrider) (*configuration.Configuration, error) {
pathConfigFile := paths.ConfigFile()
rawConfig, err := config.LoadFile(pathConfigFile)
if err != nil {
return nil, errors.New(err,
fmt.Sprintf("could not read configuration file %s", pathConfigFile),
errors.TypeFilesystem,
errors.M(errors.MetaKeyPath, pathConfigFile))
}
if err := getOverwrites(rawConfig); err != nil {
return nil, errors.New(err, "could not read overwrites")
}
cfg, err := configuration.NewFromConfig(rawConfig)
if err != nil {
return nil, errors.New(err,
fmt.Sprintf("could not parse configuration file %s", pathConfigFile),
errors.TypeFilesystem,
errors.M(errors.MetaKeyPath, pathConfigFile))
}
if override != nil {
override(cfg)
}
return cfg, nil
}
func reexecPath() (string, error) {
// set executable path to symlink instead of binary
// in case of updated symlinks we should spin up new agent
potentialReexec := filepath.Join(paths.Top(), agentName)
// in case it does not exists fallback to executable
if _, err := os.Stat(potentialReexec); os.IsNotExist(err) {
return os.Executable()
}
return potentialReexec, nil
}
func getOverwrites(rawConfig *config.Config) error {
cfg, err := configuration.NewFromConfig(rawConfig)
if err != nil {
return err
}
if !cfg.Fleet.Enabled {
// overrides should apply only for fleet mode
return nil
}
path := paths.AgentConfigFile()
store := storage.NewEncryptedDiskStore(path)
reader, err := store.Load()
if err != nil && errors.Is(err, os.ErrNotExist) {
// no fleet file ignore
return nil
} else if err != nil {
return errors.New(err, "could not initialize config store",
errors.TypeFilesystem,
errors.M(errors.MetaKeyPath, path))
}
config, err := config.NewConfigFrom(reader)
if err != nil {
return errors.New(err,
fmt.Sprintf("fail to read configuration %s for the elastic-agent", path),
errors.TypeFilesystem,
errors.M(errors.MetaKeyPath, path))
}
err = rawConfig.Merge(config)
if err != nil {
return errors.New(err,
fmt.Sprintf("fail to merge configuration with %s for the elastic-agent", path),
errors.TypeConfig,
errors.M(errors.MetaKeyPath, path))
}
return nil
}
func defaultLogLevel(cfg *configuration.Configuration) string {
if configuration.IsStandalone(cfg.Fleet) {
// for standalone always take the one from config and don't override
return ""
}
defaultLogLevel := logger.DefaultLogLevel.String()
if configuredLevel := cfg.Settings.LoggingConfig.Level.String(); configuredLevel != "" && configuredLevel != defaultLogLevel {
// predefined log level
return configuredLevel
}
return defaultLogLevel
}
func tryDelayEnroll(ctx context.Context, logger *logger.Logger, cfg *configuration.Configuration, override cfgOverrider) (*configuration.Configuration, error) {
enrollPath := paths.AgentEnrollFile()
if _, err := os.Stat(enrollPath); err != nil {
// no enrollment file exists or failed to stat it; nothing to do
return cfg, nil
}
contents, err := ioutil.ReadFile(enrollPath)
if err != nil {
return nil, errors.New(
err,
"failed to read delay enrollment file",
errors.TypeFilesystem,
errors.M("path", enrollPath))
}
var options enrollCmdOption
err = yaml.Unmarshal(contents, &options)
if err != nil {
return nil, errors.New(
err,
"failed to parse delay enrollment file",
errors.TypeConfig,
errors.M("path", enrollPath))
}
options.DelayEnroll = false
options.FleetServer.SpawnAgent = false
c, err := newEnrollCmd(
logger,
&options,
paths.ConfigFile(),
)
if err != nil {
return nil, err
}
err = c.Execute(ctx, cli.NewIOStreams())
if err != nil {
return nil, err
}
err = os.Remove(enrollPath)
if err != nil {
logger.Warn(errors.New(
err,
"failed to remove delayed enrollment file",
errors.TypeFilesystem,
errors.M("path", enrollPath)))
}
logger.Info("Successfully performed delayed enrollment of this Elastic Agent.")
return loadConfig(override)
}
func initTracer(agentName, version string, mcfg *monitoringCfg.MonitoringConfig) (*apm.Tracer, error) {
apm.DefaultTracer.Close()
if !mcfg.Enabled || !mcfg.MonitorTraces {
return nil, nil
}
cfg := mcfg.APM
//nolint:godox // the TODO is intentional
// TODO(stn): Ideally, we'd use apmtransport.NewHTTPTransportOptions()
// but it doesn't exist today. Update this code once we have something
// available via the APM Go agent.
const (
envVerifyServerCert = "ELASTIC_APM_VERIFY_SERVER_CERT"
envServerCert = "ELASTIC_APM_SERVER_CERT"
envCACert = "ELASTIC_APM_SERVER_CA_CERT_FILE"
)
if cfg.TLS.SkipVerify {
os.Setenv(envVerifyServerCert, "false")
defer os.Unsetenv(envVerifyServerCert)
}
if cfg.TLS.ServerCertificate != "" {
os.Setenv(envServerCert, cfg.TLS.ServerCertificate)
defer os.Unsetenv(envServerCert)
}
if cfg.TLS.ServerCA != "" {
os.Setenv(envCACert, cfg.TLS.ServerCA)
defer os.Unsetenv(envCACert)
}
ts, err := apmtransport.NewHTTPTransport()
if err != nil {
return nil, err
}
if len(cfg.Hosts) > 0 {
hosts := make([]*url.URL, 0, len(cfg.Hosts))
for _, host := range cfg.Hosts {
u, err := url.Parse(host)
if err != nil {
return nil, fmt.Errorf("failed parsing %s: %w", host, err)
}
hosts = append(hosts, u)
}
ts.SetServerURL(hosts...)
}
if cfg.APIKey != "" {
ts.SetAPIKey(cfg.APIKey)
} else {
ts.SetSecretToken(cfg.SecretToken)
}
return apm.NewTracerOptions(apm.TracerOptions{
ServiceName: agentName,
ServiceVersion: version,
ServiceEnvironment: cfg.Environment,
Transport: ts,
})
}
func setupMetrics(
logger *logger.Logger,
operatingSystem string,
cfg *monitoringCfg.MonitoringConfig,
tracer *apm.Tracer,
coord *coordinator.Coordinator,
) (func() error, error) {
if err := report.SetupMetrics(logger, agentName, version.GetDefaultVersion()); err != nil {
return nil, err
}
// start server for stats
endpointConfig := api.Config{
Enabled: true,
Host: monitoring.AgentMonitoringEndpoint(operatingSystem, cfg),
}
s, err := monitoring.NewServer(logger, endpointConfig, monitoringLib.GetNamespace, tracer, coord, isProcessStatsEnabled(cfg), operatingSystem)
if err != nil {
return nil, errors.New(err, "could not start the HTTP server for the API")
}
s.Start()
// return server stopper
return s.Stop, nil
}
func isProcessStatsEnabled(cfg *monitoringCfg.MonitoringConfig) bool {
return cfg != nil && cfg.HTTP.Enabled
}