Skip to content

Commit 3d8bdf4

Browse files
authored
Failing to detect SSDs in copyDir should not be a fatal error. (#3653)
* Failing to detect SSDs should not be a fatal error. * ghw.Block errors should not be fatal during install. * Log block HW errors when they occur. * Improve error messages. * Centralize uses of ghw.Block to prevent misuse. Document that errors are not fatal.
1 parent 66e0f95 commit 3d8bdf4

File tree

4 files changed

+39
-22
lines changed

4 files changed

+39
-22
lines changed

internal/pkg/agent/application/upgrade/upgrade.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import (
1212
"runtime"
1313
"strings"
1414

15-
"github.com/jaypipes/ghw"
16-
1715
"github.com/otiai10/copy"
1816
"go.elastic.co/apm"
1917

@@ -386,13 +384,14 @@ func copyDir(l *logger.Logger, from, to string, ignoreErrs bool) error {
386384
}
387385
}
388386

389-
block, err := ghw.Block()
390-
if err != nil {
391-
return fmt.Errorf("ghw.Block() returned error: %w", err)
392-
}
393-
387+
// Try to detect if we are running with SSDs. If we are increase the copy concurrency,
388+
// otherwise fall back to the default.
394389
copyConcurrency := 1
395-
if install.HasAllSSDs(*block) {
390+
hasSSDs, detectHWErr := install.HasAllSSDs()
391+
if detectHWErr != nil {
392+
l.Infow("Could not determine block storage type, disabling copy concurrency", "error.message", detectHWErr)
393+
}
394+
if hasSSDs {
396395
copyConcurrency = runtime.NumCPU() * 4
397396
}
398397

internal/pkg/agent/cmd/install.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ func installCmd(streams *cli.IOStreams, cmd *cobra.Command) error {
177177

178178
cfgFile := paths.ConfigFile()
179179
if status != install.PackageInstall {
180-
err = install.Install(cfgFile, topPath, progBar)
180+
err = install.Install(cfgFile, topPath, progBar, streams)
181181
if err != nil {
182182
return fmt.Errorf("error installing package: %w", err)
183183
}

internal/pkg/agent/install/install.go

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,22 @@ import (
1111
"runtime"
1212
"strings"
1313

14-
"github.com/kardianos/service"
15-
1614
"github.com/jaypipes/ghw"
15+
"github.com/kardianos/service"
1716
"github.com/otiai10/copy"
1817
"github.com/schollz/progressbar/v3"
1918

2019
"github.com/elastic/elastic-agent/internal/pkg/agent/application/paths"
2120
"github.com/elastic/elastic-agent/internal/pkg/agent/errors"
21+
"github.com/elastic/elastic-agent/internal/pkg/cli"
2222
)
2323

2424
const (
2525
darwin = "darwin"
2626
)
2727

2828
// Install installs Elastic Agent persistently on the system including creating and starting its service.
29-
func Install(cfgFile, topPath string, pt *progressbar.ProgressBar) error {
29+
func Install(cfgFile, topPath string, pt *progressbar.ProgressBar, streams *cli.IOStreams) error {
3030
dir, err := findDirectory()
3131
if err != nil {
3232
return errors.New(err, "failed to discover the source directory for installation", errors.TypeFilesystem)
@@ -62,15 +62,18 @@ func Install(cfgFile, topPath string, pt *progressbar.ProgressBar) error {
6262
}
6363

6464
// copy source into install path
65-
block, err := ghw.Block()
66-
if err != nil {
67-
return fmt.Errorf("ghw.Block() returned error: %w", err)
68-
}
69-
65+
//
66+
// Try to detect if we are running with SSDs. If we are increase the copy concurrency,
67+
// otherwise fall back to the default.
7068
copyConcurrency := 1
71-
if HasAllSSDs(*block) {
69+
hasSSDs, detectHWErr := HasAllSSDs()
70+
if detectHWErr != nil {
71+
fmt.Fprintf(streams.Out, "Could not determine block hardware type, disabling copy concurrency: %s\n", detectHWErr)
72+
}
73+
if hasSSDs {
7274
copyConcurrency = runtime.NumCPU() * 4
7375
}
76+
7477
pt.Describe("Copying install files")
7578
err = copy.Copy(dir, topPath, copy.Options{
7679
OnSymlink: func(_ string) copy.SymlinkAction {
@@ -84,7 +87,8 @@ func Install(cfgFile, topPath string, pt *progressbar.ProgressBar) error {
8487
return errors.New(
8588
err,
8689
fmt.Sprintf("failed to copy source directory (%s) to destination (%s)", dir, topPath),
87-
errors.M("source", dir), errors.M("destination", topPath))
90+
errors.M("source", dir), errors.M("destination", topPath),
91+
)
8892
}
8993
pt.Describe("Successfully copied files")
9094

@@ -263,8 +267,22 @@ func verifyDirectory(dir string) error {
263267
}
264268

265269
// HasAllSSDs returns true if the host we are on uses SSDs for
266-
// all its persistent storage; false otherwise or on error
267-
func HasAllSSDs(block ghw.BlockInfo) bool {
270+
// all its persistent storage; false otherwise. Returns any error
271+
// encountered detecting the hardware type for informational purposes.
272+
// Errors from this function are not fatal. Note that errors may be
273+
// returned on some Mac hardware configurations as the ghw package
274+
// does not fully support MacOS.
275+
func HasAllSSDs() (bool, error) {
276+
block, err := ghw.Block()
277+
if err != nil {
278+
return false, err
279+
}
280+
281+
return hasAllSSDs(*block), nil
282+
}
283+
284+
// Internal version of HasAllSSDs for testing.
285+
func hasAllSSDs(block ghw.BlockInfo) bool {
268286
for _, disk := range block.Disks {
269287
switch disk.DriveType {
270288
case ghw.DRIVE_TYPE_FDD, ghw.DRIVE_TYPE_ODD:

internal/pkg/agent/install/install_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func TestHasAllSSDs(t *testing.T) {
5252

5353
for name, test := range cases {
5454
t.Run(name, func(t *testing.T) {
55-
actual := HasAllSSDs(test.block)
55+
actual := hasAllSSDs(test.block)
5656
require.Equal(t, test.expected, actual)
5757
})
5858
}

0 commit comments

Comments
 (0)