-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate hardware and software from eachother and sysinfo
- Loading branch information
Showing
100 changed files
with
1,066 additions
and
900 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Package cmdutils provides utility functions for running commands. | ||
package cmdutils | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"os" | ||
"os/exec" | ||
"time" | ||
) | ||
|
||
// Run executes the command specified by cmd with arguments args using the provided context. | ||
// Returns stdout and stderr output and error code. | ||
func Run(ctx context.Context, cmd string, args ...string) (stdout, stderr *bytes.Buffer, err error) { | ||
stdout = &bytes.Buffer{} | ||
stderr = &bytes.Buffer{} | ||
|
||
c := exec.CommandContext(ctx, cmd, args...) | ||
c.Stdout = stdout | ||
c.Stderr = stderr | ||
c.Env = append(c.Env, "LANG=C") | ||
c.Env = append(c.Env, os.Environ()...) | ||
err = c.Run() | ||
|
||
return stdout, stderr, err | ||
} | ||
|
||
// RunWithTimeout calls Run but a timeout is added to the provided context. | ||
func RunWithTimeout(ctx context.Context, timeout time.Duration, cmd string, args ...string) (stdout, stderr *bytes.Buffer, err error) { | ||
c, cancel := context.WithTimeout(ctx, timeout) | ||
defer cancel() | ||
|
||
return Run(c, cmd, args...) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,18 @@ | ||
package sysinfo | ||
|
||
import ( | ||
"log/slog" | ||
"github.com/ubuntu/ubuntu-insights/internal/collector/sysinfo/hardware" | ||
"github.com/ubuntu/ubuntu-insights/internal/collector/sysinfo/software" | ||
) | ||
|
||
// WithLogger overrides the default logger. | ||
func WithLogger(logger slog.Handler) Options { | ||
func WithHardwareCollector(hw hardware.Collector) Options { | ||
return func(o *options) { | ||
o.log = slog.New(logger) | ||
o.hw = hw | ||
} | ||
} | ||
|
||
func WithSoftwareCollector(sw software.Collector) Options { | ||
return func(o *options) { | ||
o.sw = sw | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...al/collector/sysinfo/export_linux_test.go → ...tor/sysinfo/hardware/export_linux_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package hardware | ||
|
||
import ( | ||
"log/slog" | ||
) | ||
|
||
// WithLogger overrides the default logger. | ||
func WithLogger(logger slog.Handler) Options { | ||
return func(o *options) { | ||
o.log = slog.New(logger) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Package hardware handles collecting "common" hardware information for all insight reports. | ||
package hardware | ||
|
||
// Collector describes a type that collects hardware information. | ||
type Collector interface { | ||
Collect() (Info, error) | ||
} | ||
|
||
// Info aggregates hardware info. | ||
type Info struct { | ||
Product product | ||
|
||
CPU cpu | ||
GPUs []gpu | ||
Mem memory | ||
Blks []disk | ||
Screens []screen | ||
} | ||
|
||
type product map[string]string | ||
type cpu map[string]string | ||
type gpu map[string]string | ||
type memory map[string]int | ||
|
||
// DiskInfo contains information of a disk or partition. | ||
type disk struct { | ||
Name string `json:"name"` | ||
Size string `json:"size"` | ||
|
||
Partitions []disk `json:"partitions,omitempty"` | ||
} | ||
|
||
// Screen contains information for a screen. | ||
type screen struct { | ||
Name string `json:"name"` | ||
PhysicalResolution string `json:"physicalResolution"` | ||
Size string `json:"size"` | ||
Resolution string `json:"resolution"` | ||
RefreshRate string `json:"refreshRate"` | ||
} | ||
|
||
// Manager handles dependencies for collecting hardware information. | ||
// Manager implements hardware.Collector. | ||
type Manager struct { | ||
opts options | ||
} | ||
|
||
// Options are the variadic options available to the manager. | ||
type Options func(*options) | ||
|
||
// New returns a new Manager. | ||
func New(args ...Options) Manager { | ||
// options defaults are platform dependent. | ||
opts := defaultOptions() | ||
for _, opt := range args { | ||
opt(opts) | ||
} | ||
|
||
return Manager{ | ||
opts: *opts, | ||
} | ||
} | ||
|
||
// Collect aggregates the data from all the other hardware collect functions. | ||
func (s Manager) Collect() (info Info, err error) { | ||
info.Product, err = s.collectProduct() | ||
if err != nil { | ||
s.opts.log.Warn("failed to collect Product info", "error", err) | ||
info.Product = product{} | ||
} | ||
|
||
info.CPU, err = s.collectCPU() | ||
if err != nil { | ||
s.opts.log.Warn("failed to collect CPU info", "error", err) | ||
info.CPU = cpu{} | ||
} | ||
|
||
info.GPUs, err = s.collectGPUs() | ||
if err != nil { | ||
s.opts.log.Warn("failed to collect GPU info", "error", err) | ||
info.GPUs = []gpu{} | ||
} | ||
|
||
info.Mem, err = s.collectMemory() | ||
if err != nil { | ||
s.opts.log.Warn("failed to collect memory info", "error", err) | ||
info.Mem = memory{} | ||
} | ||
|
||
info.Blks, err = s.collectDisks() | ||
if err != nil { | ||
s.opts.log.Warn("failed to collect disk info", "error", err) | ||
info.Blks = []disk{} | ||
} | ||
|
||
info.Screens, err = s.collectScreens() | ||
if err != nil { | ||
s.opts.log.Warn("failed to collect screen info", "error", err) | ||
info.Screens = []screen{} | ||
} | ||
|
||
return info, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package hardware | ||
|
||
import "log/slog" | ||
|
||
type options struct { | ||
log *slog.Logger | ||
} | ||
|
||
func defaultOptions() *options { | ||
return &options{} | ||
} | ||
|
||
func (s Manager) collectProduct() (product, error) { | ||
return product{}, nil | ||
} | ||
|
||
func (s Manager) collectCPU() (cpu, error) { | ||
return cpu{}, nil | ||
} | ||
|
||
func (s Manager) collectGPUs() ([]gpu, error) { | ||
return []gpu{}, nil | ||
} | ||
|
||
func (s Manager) collectMemory() (memory, error) { | ||
return memory{}, nil | ||
} | ||
|
||
func (s Manager) collectDisks() ([]disk, error) { | ||
return []disk{}, nil | ||
} | ||
|
||
func (s Manager) collectScreens() ([]screen, error) { | ||
return []screen{}, nil | ||
} |
Oops, something went wrong.