Skip to content

Commit

Permalink
refactor(linux): clean up linux device setup code
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuar committed Oct 3, 2023
1 parent d7ce71b commit 14282ae
Showing 1 changed file with 60 additions and 65 deletions.
125 changes: 60 additions & 65 deletions internal/linux/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,11 @@ func getBus(ctx context.Context, e dbusType) (*Bus, bool) {
type LinuxDevice struct {
appName string
appVersion string
appID string
hostname string
hwVendor string
hwModel string
osRelease string
osVersion string
machineID string
}

// Setup returns a new Context that contains the D-Bus API.
func (l *LinuxDevice) Setup(ctx context.Context) context.Context {
return context.WithValue(ctx, linuxCtxKey, newDBusAPI(ctx))
}

func (l *LinuxDevice) AppName() string {
Expand All @@ -73,32 +71,57 @@ func (l *LinuxDevice) AppVersion() string {
}

func (l *LinuxDevice) AppID() string {
return l.appID
// Use the current user's username to construct an app ID.
currentUser, err := user.Current()
if err != nil {
log.Warn().Err(err).
Msg("Could not retrieve current user details.")
return l.appName + "-unknown"
}
return l.appName + "-" + currentUser.Username
}

func (l *LinuxDevice) DeviceName() string {
shortHostname, _, _ := strings.Cut(l.hostname, ".")
shortHostname, _, _ := strings.Cut(getHostname(), ".")
return shortHostname
}

func (l *LinuxDevice) DeviceID() string {
return l.machineID
machineID, err := host.HostID()
if err != nil {
log.Warn().Err(err).
Msg("Could not retrieve a machine ID")
return "unknown"
}
return machineID
}

func (l *LinuxDevice) Manufacturer() string {
return l.hwVendor
return getHWVendor()
}

func (l *LinuxDevice) Model() string {
return l.hwModel
return getHWModel()
}

func (l *LinuxDevice) OsName() string {
return l.osRelease
_, osRelease, _, err := host.PlatformInformation()
if err != nil {
log.Warn().Err(err).
Msg("Could not retrieve distribution details.")
return "Unknown OS"
}
return osRelease
}

func (l *LinuxDevice) OsVersion() string {
return l.osVersion
_, _, osVersion, err := host.PlatformInformation()
if err != nil {
log.Warn().Err(err).
Msg("Could not retrieve version details.")
return "Unknown Version"
}
return osVersion
}

func (l *LinuxDevice) SupportsEncryption() bool {
Expand Down Expand Up @@ -129,72 +152,44 @@ func (l *LinuxDevice) MarshalJSON() ([]byte, error) {
})
}

// Setup returns a new Context that contains the D-Bus API.
func (l *LinuxDevice) Setup(ctx context.Context) context.Context {
return context.WithValue(ctx, linuxCtxKey, newDBusAPI(ctx))
}

func NewDevice(name, version string) *LinuxDevice {
device := &LinuxDevice{
return &LinuxDevice{
appName: name,
appVersion: version,
}
var err error

_, device.osRelease, device.osVersion, err = host.PlatformInformation()
if err != nil {
log.Fatal().Caller().
Msgf("Could not retrieve distribution details: %v", err.Error())
}

device.hostname = getHostname()
device.hwVendor, device.hwModel = getHardwareDetails()

// Use the current user's username to construct an app ID.
currentUser, err := user.Current()
if err != nil {
log.Fatal().Caller().
Msgf("Could not retrieve current user details: %v", err.Error())
}
device.appID = name + "-" + currentUser.Username
}

// Generate a semi-random machine ID.
device.machineID, err = host.HostID()
// getHostname retrieves the hostname of the device running the agent, or
// localhost if that doesn't work
func getHostname() string {
hostname, err := os.Hostname()
if err != nil {
log.Fatal().Caller().
Msgf("Could not retrieve a machine ID: %v", err)
log.Warn().Err(err).Msg("Could not retrieve hostname.")
return "localhost"
}

return device
return hostname
}

// getHardwareDetails will try to read the vendor and model details them from
// the /sys filesystem. If that fails, it returns empty strings for these values
// https://github.com/ansible/ansible/blob/devel/lib/ansible/module_utils/facts/hardware/linux.py
func getHardwareDetails() (string, string) {
var vendor, model string
// getHWVendor will try to retrieve the vendor from the sysfs filesystem. It
// will return "Unknown Vendor" if unsuccessful.
// Reference: https://github.com/ansible/ansible/blob/devel/lib/ansible/module_utils/facts/hardware/linux.py
func getHWVendor() string {
hwVendor, err := os.ReadFile("/sys/devices/virtual/dmi/id/board_vendor")
if err != nil {
vendor = "Unknown Vendor"
return "Unknown Vendor"
} else {
vendor = strings.TrimSpace(string(hwVendor))
return strings.TrimSpace(string(hwVendor))
}
hwModel, err := os.ReadFile("/sys/devices/virtual/dmi/id/product_name")
if err != nil {
model = "Unknown Vendor"
} else {
model = strings.TrimSpace(string(hwModel))
}
return vendor, model
}

// getHostname retrieves the hostname of the device running the agent, or
// localhost if that doesn't work
func getHostname() string {
hostname, err := os.Hostname()
// getHWModel will try to retrieve the hardware model from the sysfs filesystem. It
// will return "Unknown Model" if unsuccessful.
// Reference: https://github.com/ansible/ansible/blob/devel/lib/ansible/module_utils/facts/hardware/linux.py
func getHWModel() string {
hwModel, err := os.ReadFile("/sys/devices/virtual/dmi/id/product_name")
if err != nil {
log.Warn().Err(err).Msg("Could not retrieve hostname.")
return "localhost"
return "Unknown Model"
} else {
return strings.TrimSpace(string(hwModel))
}
return hostname
}

0 comments on commit 14282ae

Please sign in to comment.