forked from elastic/elastic-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollback.go
231 lines (193 loc) · 7.08 KB
/
rollback.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
// 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"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/hashicorp/go-multierror"
"google.golang.org/grpc"
"github.com/elastic/elastic-agent/internal/pkg/agent/application/paths"
"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/core/backoff"
"github.com/elastic/elastic-agent/pkg/control"
"github.com/elastic/elastic-agent/pkg/control/v2/client"
"github.com/elastic/elastic-agent/pkg/core/logger"
"github.com/elastic/elastic-agent/pkg/utils"
)
const (
watcherSubcommand = "watch"
maxRestartCount = 5
restartBackoffInit = 5 * time.Second
restartBackoffMax = 90 * time.Second
)
// Rollback rollbacks to previous version which was functioning before upgrade.
func Rollback(ctx context.Context, log *logger.Logger, c client.Client, topDirPath, prevVersionedHome, prevHash string) error {
symlinkPath := filepath.Join(topDirPath, agentName)
var symlinkTarget string
if prevVersionedHome != "" {
symlinkTarget = paths.BinaryPath(filepath.Join(topDirPath, prevVersionedHome), agentName)
} else {
// fallback for upgrades that didn't use the manifest and path remapping
hashedDir := fmt.Sprintf("%s-%s", agentName, prevHash)
// paths.BinaryPath properly derives the binary directory depending on the platform. The path to the binary for macOS is inside of the app bundle.
symlinkTarget = paths.BinaryPath(filepath.Join(paths.DataFrom(topDirPath), hashedDir), agentName)
}
// change symlink
if err := changeSymlink(log, topDirPath, symlinkPath, symlinkTarget); err != nil {
return err
}
// revert active commit
if err := UpdateActiveCommit(log, topDirPath, prevHash); err != nil {
return err
}
// Restart
log.Info("Restarting the agent after rollback")
if err := restartAgent(ctx, log, c); err != nil {
return err
}
// cleanup everything except version we're rolling back into
return Cleanup(log, topDirPath, prevVersionedHome, prevHash, true, true)
}
// Cleanup removes all artifacts and files related to a specified version.
func Cleanup(log *logger.Logger, topDirPath, currentVersionedHome, currentHash string, removeMarker, keepLogs bool) error {
log.Infow("Cleaning up upgrade", "hash", currentHash, "remove_marker", removeMarker)
<-time.After(afterRestartDelay)
// data directory path
dataDirPath := paths.DataFrom(topDirPath)
// remove upgrade marker
if removeMarker {
if err := CleanMarker(log, dataDirPath); err != nil {
return err
}
}
// remove data/elastic-agent-{hash}
dataDir, err := os.Open(dataDirPath)
if err != nil {
return err
}
defer func(dataDir *os.File) {
err := dataDir.Close()
if err != nil {
log.Errorw("Error closing data directory", "file.directory", dataDirPath)
}
}(dataDir)
subdirs, err := dataDir.Readdirnames(0)
if err != nil {
return err
}
// remove symlink to avoid upgrade failures, ignore error
prevSymlink := prevSymlinkPath(topDirPath)
log.Infow("Removing previous symlink path", "file.path", prevSymlinkPath(topDirPath))
_ = os.Remove(prevSymlink)
dirPrefix := fmt.Sprintf("%s-", agentName)
var currentDir string
if currentVersionedHome != "" {
currentDir, err = filepath.Rel("data", currentVersionedHome)
if err != nil {
return fmt.Errorf("extracting elastic-agent path relative to data directory from %s: %w", currentVersionedHome, err)
}
} else {
currentDir = fmt.Sprintf("%s-%s", agentName, currentHash)
}
for _, dir := range subdirs {
if dir == currentDir {
continue
}
if !strings.HasPrefix(dir, dirPrefix) {
continue
}
hashedDir := filepath.Join(dataDirPath, dir)
log.Infow("Removing hashed data directory", "file.path", hashedDir)
var ignoredDirs []string
if keepLogs {
ignoredDirs = append(ignoredDirs, "logs")
}
if cleanupErr := install.RemoveBut(hashedDir, true, ignoredDirs...); cleanupErr != nil {
err = multierror.Append(err, cleanupErr)
}
}
return err
}
// InvokeWatcher invokes an agent instance using watcher argument for watching behavior of
// agent during upgrade period.
func InvokeWatcher(log *logger.Logger, agentExecutable string) (*exec.Cmd, error) {
if !IsUpgradeable() {
log.Info("agent is not upgradable, not starting watcher")
return nil, nil
}
cmd := invokeCmd(agentExecutable)
log.Infow("Starting upgrade watcher", "path", cmd.Path, "args", cmd.Args, "env", cmd.Env, "dir", cmd.Dir)
if err := cmd.Start(); err != nil {
return nil, fmt.Errorf("failed to start Upgrade Watcher: %w", err)
}
upgradeWatcherPID := cmd.Process.Pid
agentPID := os.Getpid()
go func() {
if err := cmd.Wait(); err != nil {
log.Infow("Upgrade Watcher exited with error", "agent.upgrade.watcher.process.pid", "agent.process.pid", agentPID, upgradeWatcherPID, "error.message", err)
}
}()
log.Infow("Upgrade Watcher invoked", "agent.upgrade.watcher.process.pid", upgradeWatcherPID, "agent.process.pid", agentPID)
return cmd, nil
}
func restartAgent(ctx context.Context, log *logger.Logger, c client.Client) error {
restartViaDaemonFn := func(ctx context.Context) error {
connectCtx, connectCancel := context.WithTimeout(ctx, 3*time.Second)
defer connectCancel()
err := c.Connect(connectCtx, grpc.WithBlock(), grpc.WithDisableRetry())
if err != nil {
return errors.New(err, "failed communicating to running daemon", errors.TypeNetwork, errors.M("socket", control.Address()))
}
defer c.Disconnect()
err = c.Restart(ctx)
if err != nil {
return errors.New(err, "failed trigger restart of daemon")
}
return nil
}
restartViaServiceFn := func(ctx context.Context) error {
topPath := paths.Top()
err := install.RestartService(topPath)
if err != nil {
return fmt.Errorf("failed to restart agent via service: %w", err)
}
return nil
}
signal := make(chan struct{})
backExp := backoff.NewExpBackoff(signal, restartBackoffInit, restartBackoffMax)
root, _ := utils.HasRoot() // error ignored
for restartAttempt := 1; restartAttempt <= maxRestartCount; restartAttempt++ {
backExp.Wait()
log.Infof("Restarting Agent via control protocol; attempt %d of %d", restartAttempt, maxRestartCount)
// First, try to restart Agent by sending a restart command
// to its daemon (via GRPC).
err := restartViaDaemonFn(ctx)
if err == nil {
break
}
log.Warnf("Failed to restart agent via control protocol: %s", err.Error())
// Next, try to restart Agent via the service. (only if root)
if root {
log.Infof("Restarting Agent via service; attempt %d of %d", restartAttempt, maxRestartCount)
err = restartViaServiceFn(ctx)
if err == nil {
break
}
log.Warnf("Failed to restart agent via service: %s", err.Error())
}
if restartAttempt == maxRestartCount {
log.Error("Failed to restart agent after final attempt")
return err
}
log.Warnf("Failed to restart agent; will try again in %v", backExp.NextWait())
}
close(signal)
return nil
}