Skip to content

Commit

Permalink
fix(os): fetch system manufacturer once for linux
Browse files Browse the repository at this point in the history
Signed-off-by: Gyuho Lee <[email protected]>
  • Loading branch information
gyuho committed Dec 30, 2024
1 parent 687ff0a commit 1657bbb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
27 changes: 20 additions & 7 deletions components/os/component_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,10 @@ type MachineMetadata struct {
OSMachineID string `json:"os_machine_id"`
}

var currentMachineMetadata MachineMetadata
var (
currentMachineMetadata MachineMetadata
currentSystemManufacturer string
)

func init() {
// Linux-specific operations
Expand Down Expand Up @@ -395,6 +398,13 @@ func init() {
if err != nil {
log.Logger.Warnw("failed to get os machine id", "error", err)
}

cctx, ccancel := context.WithTimeout(context.Background(), 20*time.Second)
currentSystemManufacturer, err = pkg_host.SystemManufacturer(cctx)
ccancel()
if err != nil {
log.Logger.Warnw("failed to get system manufacturer", "error", err)
}
}

var (
Expand Down Expand Up @@ -438,13 +448,16 @@ func CreateGet(cfg Config) func(ctx context.Context) (_ any, e error) {
}
o.VirtualizationEnvironment = virtEnv

cctx, ccancel = context.WithTimeout(ctx, 10*time.Second)
manufacturer, err := pkg_host.SystemManufacturer(cctx)
ccancel()
if err != nil {
return nil, err
// for some reason, init failed
if currentSystemManufacturer == "" && runtime.GOOS == "linux" {
cctx, ccancel = context.WithTimeout(ctx, 20*time.Second)
currentSystemManufacturer, err = pkg_host.SystemManufacturer(cctx)
ccancel()
if err != nil {
log.Logger.Warnw("failed to get system manufacturer", "error", err)
}
}
o.SystemManufacturer = manufacturer
o.SystemManufacturer = currentSystemManufacturer

o.MachineMetadata = currentMachineMetadata

Expand Down
2 changes: 1 addition & 1 deletion pkg/host/machine_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func DmidecodeUUID(ctx context.Context) (string, error) {
}
}),
); err != nil {
return "", fmt.Errorf("failed to read dmidecode output: %w\n\noutput:\n%s", err, strings.Join(lines, "\n"))
return "", fmt.Errorf("failed to read dmidecode for uuid: %w\n\noutput:\n%s", err, strings.Join(lines, "\n"))
}

return uuid, nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/host/virt.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func SystemManufacturer(ctx context.Context) (string, error) {
}),
process.WithWaitForCmd(),
); err != nil {
return "", fmt.Errorf("failed to read dmidecode output: %w\n\noutput:\n%s", err, strings.Join(lines, "\n"))
return "", fmt.Errorf("failed to read dmidecode for system manufacturer: %w\n\noutput:\n%s", err, strings.Join(lines, "\n"))
}
out := strings.TrimSpace(strings.Join(lines, "\n"))

Expand Down

0 comments on commit 1657bbb

Please sign in to comment.