-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathupgrade.go
450 lines (374 loc) · 14.5 KB
/
upgrade.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
// 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 upgrade
import (
"context"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/otiai10/copy"
"go.elastic.co/apm"
"github.com/elastic/elastic-agent/internal/pkg/agent/application/info"
"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/upgrade/artifact"
"github.com/elastic/elastic-agent/internal/pkg/agent/application/upgrade/details"
"github.com/elastic/elastic-agent/internal/pkg/agent/configuration"
"github.com/elastic/elastic-agent/internal/pkg/agent/errors"
"github.com/elastic/elastic-agent/internal/pkg/agent/install"
"github.com/elastic/elastic-agent/internal/pkg/config"
"github.com/elastic/elastic-agent/internal/pkg/fleetapi"
"github.com/elastic/elastic-agent/internal/pkg/fleetapi/acker"
fleetclient "github.com/elastic/elastic-agent/internal/pkg/fleetapi/client"
"github.com/elastic/elastic-agent/internal/pkg/release"
"github.com/elastic/elastic-agent/pkg/control/v2/client"
"github.com/elastic/elastic-agent/pkg/control/v2/cproto"
"github.com/elastic/elastic-agent/pkg/core/logger"
)
const (
agentName = "elastic-agent"
hashLen = 6
agentCommitFile = ".elastic-agent.active.commit"
runDirMod = 0770
)
var agentArtifact = artifact.Artifact{
Name: "Elastic Agent",
Cmd: agentName,
Artifact: "beats/" + agentName,
}
// ErrSameVersion error is returned when the upgrade results in the same installed version.
var ErrSameVersion = errors.New("upgrade did not occur because its the same version")
// Upgrader performs an upgrade
type Upgrader struct {
log *logger.Logger
settings *artifact.Config
agentInfo *info.AgentInfo
upgradeable bool
fleetServerURI string
markerWatcher MarkerWatcher
}
// IsUpgradeable when agent is installed and running as a service or flag was provided.
func IsUpgradeable() bool {
// only upgradeable if running from Agent installer and running under the
// control of the system supervisor (or built specifically with upgrading enabled)
return release.Upgradeable() || (info.RunningInstalled() && info.RunningUnderSupervisor())
}
// NewUpgrader creates an upgrader which is capable of performing upgrade operation
func NewUpgrader(log *logger.Logger, settings *artifact.Config, agentInfo *info.AgentInfo) (*Upgrader, error) {
return &Upgrader{
log: log,
settings: settings,
agentInfo: agentInfo,
upgradeable: IsUpgradeable(),
markerWatcher: newMarkerFileWatcher(markerFilePath(), log),
}, nil
}
// SetClient reloads URI based on up to date fleet client
func (u *Upgrader) SetClient(c fleetclient.Sender) {
if c == nil {
u.log.Debug("client nil, resetting Fleet Server URI")
u.fleetServerURI = ""
}
u.fleetServerURI = c.URI()
u.log.Debugf("Set client changed URI to %s", u.fleetServerURI)
}
// Reload reloads the artifact configuration for the upgrader.
func (u *Upgrader) Reload(rawConfig *config.Config) error {
cfg, err := configuration.NewFromConfig(rawConfig)
if err != nil {
return fmt.Errorf("invalid config: %w", err)
}
// the source URI coming from fleet which uses a different naming.
type fleetCfg struct {
// FleetSourceURI: source of the artifacts, e.g https://artifacts.elastic.co/downloads/
FleetSourceURI string `json:"agent.download.source_uri" config:"agent.download.source_uri"`
}
fleetSourceURI := &fleetCfg{}
if err := rawConfig.Unpack(&fleetSourceURI); err != nil {
return errors.New(err, "failed to unpack config during reload")
}
// fleet configuration takes precedence
if fleetSourceURI.FleetSourceURI != "" {
cfg.Settings.DownloadConfig.SourceURI = fleetSourceURI.FleetSourceURI
}
if cfg.Settings.DownloadConfig.SourceURI != "" {
u.log.Infof("Source URI changed from %q to %q",
u.settings.SourceURI,
cfg.Settings.DownloadConfig.SourceURI)
} else {
// source uri unset, reset to default
u.log.Infof("Source URI reset from %q to %q",
u.settings.SourceURI,
artifact.DefaultSourceURI)
cfg.Settings.DownloadConfig.SourceURI = artifact.DefaultSourceURI
}
u.settings = cfg.Settings.DownloadConfig
return nil
}
// Upgradeable returns true if the Elastic Agent can be upgraded.
func (u *Upgrader) Upgradeable() bool {
return u.upgradeable
}
// Upgrade upgrades running agent, function returns shutdown callback that must be called by reexec.
func (u *Upgrader) Upgrade(ctx context.Context, version string, sourceURI string, action *fleetapi.ActionUpgrade, det *details.Details, skipVerifyOverride bool, skipDefaultPgp bool, pgpBytes ...string) (_ reexec.ShutdownCallbackFn, err error) {
u.log.Infow("Upgrading agent", "version", version, "source_uri", sourceURI)
// Inform the Upgrade Marker Watcher that we've started upgrading. Note that this
// is only possible to do in-memory since, today, the process that's initiating
// the upgrade is the same as the Agent process in which the Upgrade Marker Watcher is
// running. If/when, in the future, the process initiating the upgrade is separated
// from the Agent process in which the Upgrade Marker Watcher is running, such in-memory
// communication will need to be replaced with inter-process communication (e.g. via
// a file, e.g. the Upgrade Marker file or something else).
u.markerWatcher.SetUpgradeStarted()
span, ctx := apm.StartSpan(ctx, "upgrade", "app.internal")
defer span.End()
err = cleanNonMatchingVersionsFromDownloads(u.log, u.agentInfo.Version())
if err != nil {
u.log.Errorw("Unable to clean downloads before update", "error.message", err, "downloads.path", paths.Downloads())
}
det.SetState(details.StateDownloading)
sourceURI = u.sourceURI(sourceURI)
archivePath, err := u.downloadArtifact(ctx, version, sourceURI, det, skipVerifyOverride, skipDefaultPgp, pgpBytes...)
if err != nil {
// Run the same pre-upgrade cleanup task to get rid of any newly downloaded files
// This may have an issue if users are upgrading to the same version number.
if dErr := cleanNonMatchingVersionsFromDownloads(u.log, u.agentInfo.Version()); dErr != nil {
u.log.Errorw("Unable to remove file after verification failure", "error.message", dErr)
}
return nil, err
}
det.SetState(details.StateExtracting)
newHash, err := u.unpack(version, archivePath)
if err != nil {
return nil, err
}
if newHash == "" {
return nil, errors.New("unknown hash")
}
if strings.HasPrefix(release.Commit(), newHash) {
u.log.Warn("Upgrade action skipped: upgrade did not occur because its the same version")
return nil, nil
}
if err := copyActionStore(u.log, newHash); err != nil {
return nil, errors.New(err, "failed to copy action store")
}
if err := copyRunDirectory(u.log, newHash); err != nil {
return nil, errors.New(err, "failed to copy run directory")
}
det.SetState(details.StateReplacing)
if err := ChangeSymlink(ctx, u.log, newHash); err != nil {
u.log.Errorw("Rolling back: changing symlink failed", "error.message", err)
rollbackInstall(ctx, u.log, newHash)
return nil, err
}
if err := u.markUpgrade(ctx, u.log, newHash, action, det); err != nil {
u.log.Errorw("Rolling back: marking upgrade failed", "error.message", err)
rollbackInstall(ctx, u.log, newHash)
return nil, err
}
if err := InvokeWatcher(u.log); err != nil {
u.log.Errorw("Rolling back: starting watcher failed", "error.message", err)
rollbackInstall(ctx, u.log, newHash)
return nil, err
}
cb := shutdownCallback(u.log, paths.Home(), release.Version(), version, release.TrimCommit(newHash))
// Clean everything from the downloads dir
u.log.Infow("Removing downloads directory", "file.path", paths.Downloads())
err = os.RemoveAll(paths.Downloads())
if err != nil {
u.log.Errorw("Unable to clean downloads after update", "error.message", err, "file.path", paths.Downloads())
}
return cb, nil
}
// Ack acks last upgrade action
func (u *Upgrader) Ack(ctx context.Context, acker acker.Acker) error {
// get upgrade action
marker, err := LoadMarker()
if err != nil {
return err
}
if marker == nil {
return nil
}
if marker.Acked {
return nil
}
// Action can be nil if the upgrade was called locally.
// Should handle gracefully
// https://github.com/elastic/elastic-agent/issues/1788
if marker.Action != nil {
if err := acker.Ack(ctx, marker.Action); err != nil {
return err
}
if err := acker.Commit(ctx); err != nil {
return err
}
}
marker.Acked = true
return SaveMarker(marker, false)
}
func (u *Upgrader) MarkerWatcher() MarkerWatcher {
return u.markerWatcher
}
func (u *Upgrader) sourceURI(retrievedURI string) string {
if retrievedURI != "" {
return retrievedURI
}
return u.settings.SourceURI
}
func rollbackInstall(ctx context.Context, log *logger.Logger, hash string) {
os.RemoveAll(filepath.Join(paths.Data(), fmt.Sprintf("%s-%s", agentName, hash)))
_ = ChangeSymlink(ctx, log, release.ShortCommit())
}
func copyActionStore(log *logger.Logger, newHash string) error {
// copies legacy action_store.yml, state.yml and state.enc encrypted file if exists
storePaths := []string{paths.AgentActionStoreFile(), paths.AgentStateStoreYmlFile(), paths.AgentStateStoreFile()}
newHome := filepath.Join(filepath.Dir(paths.Home()), fmt.Sprintf("%s-%s", agentName, newHash))
log.Infow("Copying action store", "new_home_path", newHome)
for _, currentActionStorePath := range storePaths {
newActionStorePath := filepath.Join(newHome, filepath.Base(currentActionStorePath))
log.Infow("Copying action store path", "from", currentActionStorePath, "to", newActionStorePath)
currentActionStore, err := os.ReadFile(currentActionStorePath)
if os.IsNotExist(err) {
// nothing to copy
continue
}
if err != nil {
return err
}
if err := os.WriteFile(newActionStorePath, currentActionStore, 0o600); err != nil {
return err
}
}
return nil
}
func copyRunDirectory(log *logger.Logger, newHash string) error {
newRunPath := filepath.Join(filepath.Dir(paths.Home()), fmt.Sprintf("%s-%s", agentName, newHash), "run")
oldRunPath := filepath.Join(filepath.Dir(paths.Home()), fmt.Sprintf("%s-%s", agentName, release.ShortCommit()), "run")
log.Infow("Copying run directory", "new_run_path", newRunPath, "old_run_path", oldRunPath)
if err := os.MkdirAll(newRunPath, runDirMod); err != nil {
return errors.New(err, "failed to create run directory")
}
err := copyDir(log, oldRunPath, newRunPath, true)
if os.IsNotExist(err) {
// nothing to copy, operation ok
log.Infow("Run directory not present", "old_run_path", oldRunPath)
return nil
}
if err != nil {
return errors.New(err, "failed to copy %q to %q", oldRunPath, newRunPath)
}
return nil
}
// shutdownCallback returns a callback function to be executing during shutdown once all processes are closed.
// this goes through runtime directory of agent and copies all the state files created by processes to new versioned
// home directory with updated process name to match new version.
func shutdownCallback(l *logger.Logger, homePath, prevVersion, newVersion, newHash string) reexec.ShutdownCallbackFn {
if release.Snapshot() {
// SNAPSHOT is part of newVersion
prevVersion += "-SNAPSHOT"
}
return func() error {
runtimeDir := filepath.Join(homePath, "run")
processDirs, err := readProcessDirs(runtimeDir)
if err != nil {
return err
}
oldHome := homePath
newHome := filepath.Join(filepath.Dir(homePath), fmt.Sprintf("%s-%s", agentName, newHash))
for _, processDir := range processDirs {
newDir := strings.ReplaceAll(processDir, prevVersion, newVersion)
newDir = strings.ReplaceAll(newDir, oldHome, newHome)
if err := copyDir(l, processDir, newDir, true); err != nil {
return err
}
}
return nil
}
}
func readProcessDirs(runtimeDir string) ([]string, error) {
pipelines, err := readDirs(runtimeDir)
if err != nil {
return nil, err
}
processDirs := make([]string, 0)
for _, p := range pipelines {
dirs, err := readDirs(p)
if err != nil {
return nil, err
}
processDirs = append(processDirs, dirs...)
}
return processDirs, nil
}
// readDirs returns list of absolute paths to directories inside specified path.
func readDirs(dir string) ([]string, error) {
dirEntries, err := os.ReadDir(dir)
if err != nil && !os.IsNotExist(err) {
return nil, err
}
dirs := make([]string, 0, len(dirEntries))
for _, de := range dirEntries {
if !de.IsDir() {
continue
}
dirs = append(dirs, filepath.Join(dir, de.Name()))
}
return dirs, nil
}
func copyDir(l *logger.Logger, from, to string, ignoreErrs bool) error {
var onErr func(src, dst string, err error) error
if ignoreErrs {
onErr = func(src, dst string, err error) error {
if err == nil {
return nil
}
// ignore all errors, just log them
l.Infof("ignoring error: failed to copy %q to %q: %s", src, dst, err.Error())
return nil
}
}
// Try to detect if we are running with SSDs. If we are increase the copy concurrency,
// otherwise fall back to the default.
copyConcurrency := 1
hasSSDs, detectHWErr := install.HasAllSSDs()
if detectHWErr != nil {
l.Infow("Could not determine block storage type, disabling copy concurrency", "error.message", detectHWErr)
}
if hasSSDs {
copyConcurrency = runtime.NumCPU() * 4
}
return copy.Copy(from, to, copy.Options{
OnSymlink: func(_ string) copy.SymlinkAction {
return copy.Shallow
},
Sync: true,
OnError: onErr,
NumOfWorkers: int64(copyConcurrency),
})
}
// IsInProgress checks if an Elastic Agent upgrade is already in progress. It
// returns true if so and false if not.
// `c client.Client` is expected to be a connected client.
func IsInProgress(c client.Client, watcherPIDsFetcher func() ([]int, error)) (bool, error) {
// First we check if any Upgrade Watcher processes are running. If they are,
// it means an upgrade is in progress. We check this before checking the Elastic
// Agent's status because the Elastic Agent GRPC server may briefly be
// unavailable during an upgrade and so the client connection might fail.
watcherPIDs, err := watcherPIDsFetcher()
if err != nil {
return false, fmt.Errorf("failed to determine if upgrade watcher is running: %w", err)
}
if len(watcherPIDs) > 0 {
return true, nil
}
// Next we check the Elastic Agent's status using the GRPC client.
state, err := c.State(context.Background())
if err != nil {
return false, fmt.Errorf("failed to get agent state: %w", err)
}
return state.State == cproto.State_UPGRADING, nil
}