Skip to content

Commit

Permalink
agent: Refactor OS information
Browse files Browse the repository at this point in the history
  • Loading branch information
ish-hcc committed Sep 15, 2024
1 parent fa7e5eb commit 1775856
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 77 deletions.
46 changes: 32 additions & 14 deletions agent/driver/infra/compute_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"bufio"
"errors"
"github.com/shirou/gopsutil/v3/disk"
"regexp"
"strings"
"time"

Expand All @@ -22,10 +23,12 @@ import (
"github.com/yumaojun03/dmidecode/parser/memory"
)

func getOSVersion() (string, error) {
func getOSProperties() (infra.OS, error) {
var OS infra.OS

content, err := fileutil.ReadFile("/etc/os-release")
if err != nil {
return "", err
return OS, errors.New("failed to read os-release file")
}
content = strings.TrimSpace(content)
scanner := bufio.NewScanner(strings.NewReader(content))
Expand All @@ -38,12 +41,32 @@ func getOSVersion() (string, error) {
}
name := strings.TrimSpace(split[0])
value := strings.Replace(strings.TrimSpace(split[1]), "\"", "", -1)
if name == "VERSION" {
return value, nil
if name == "PRETTY_NAME" {
OS.PrettyName = value
} else if name == "NAME" {
OS.Name = value
} else if name == "VERSION_ID" {
OS.VersionID = value
} else if name == "VERSION" {
OS.Version = value
} else if name == "VERSION_CODENAME" {
OS.VersionCodename = value
} else if name == "ID" {
OS.ID = value
} else if name == "ID_LIKE" {
OS.IDLike = value
}
}

if OS.VersionCodename == "" {
re := regexp.MustCompile(`\(([^)]+)\)`)
matches := re.FindStringSubmatch(OS.Version)
if len(matches) > 1 {
OS.VersionCodename = matches[1]
}
}

return "", errors.New("failed to parse os version")
return OS, nil
}
func getKernelVersion() (string, error) {
output, err := cmd.RunCMD("uname -v")
Expand All @@ -59,12 +82,13 @@ func getKernelVersion() (string, error) {
func GetComputeInfo() (infra.Compute, error) {
var compute infra.Compute

// host information
osVersion, err := getOSVersion()
// OS information
os, err := getOSProperties()
if err != nil {
return compute, err
}

// host information
h, err := host.Info()
if err != nil {
return compute, err
Expand Down Expand Up @@ -185,13 +209,7 @@ func GetComputeInfo() (infra.Compute, error) {
// All of compute information
compute = infra.Compute{
OS: infra.System{
OS: infra.OS{
Name: h.OS,
Vendor: h.Platform,
Version: osVersion,
Release: h.PlatformVersion,
Architecture: h.KernelArch,
},
OS: os,
Kernel: infra.Kernel{
Release: h.KernelVersion,
Version: kernelVersion,
Expand Down
98 changes: 68 additions & 30 deletions agent/driver/infra/compute_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,92 @@ package infra
import (
"errors"
"fmt"
"github.com/shirou/gopsutil/v3/disk"
"io"
"os"
"strings"
"syscall"
"time"
"unsafe"

"github.com/cloud-barista/cm-honeybee/agent/pkg/api/rest/model/onprem/infra"
"github.com/jaypipes/ghw"
"github.com/jollaman999/utils/logger"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/disk"
"github.com/shirou/gopsutil/v3/host"
"github.com/shirou/gopsutil/v3/mem"
"github.com/yumaojun03/dmidecode"
"github.com/yumaojun03/dmidecode/parser/memory"
"golang.org/x/sys/windows"
"golang.org/x/sys/windows/registry"
"io"
"os"
"strconv"
"strings"
"syscall"
"time"
)

func getWindowsReleaseVersion() (string, error) {
var h windows.Handle // like HostIDWithContext(), we query the registry using the raw windows.RegOpenKeyEx/RegQueryValueEx
err := windows.RegOpenKeyEx(windows.HKEY_LOCAL_MACHINE, windows.StringToUTF16Ptr(`SOFTWARE\Microsoft\Windows NT\CurrentVersion`), 0, windows.KEY_READ|windows.KEY_WOW64_64KEY, &h)
func readRegistryKey(baseKey registry.Key, path string, keyName string) (string, error) {
key, err := registry.OpenKey(baseKey, path, registry.QUERY_VALUE)
if err != nil {
return "", err
}
defer func() {
_ = windows.RegCloseKey(h)
}()
var bufLen uint32
var valType uint32
err = windows.RegQueryValueEx(h, windows.StringToUTF16Ptr(`DisplayVersion`), nil, &valType, nil, &bufLen)
defer key.Close()

val, _, err := key.GetStringValue(keyName)
if err != nil {
return "", err
}
regBuf := make([]uint16, bufLen/2+1)
err = windows.RegQueryValueEx(h, windows.StringToUTF16Ptr(`DisplayVersion`), nil, &valType, (*byte)(unsafe.Pointer(&regBuf[0])), &bufLen)

return val, nil
}

func getWindowsVersion(productName string, buildNumber int) string {
if strings.Contains(productName, "Windows 10") && buildNumber >= 22000 {
productName = strings.Replace(productName, "Windows 10", "Windows 11", 1)
}

return productName
}

func getOSProperties() (infra.OS, error) {
productName, err := readRegistryKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, "ProductName")
if err != nil {
return "", err
return infra.OS{}, err
}

return windows.UTF16ToString(regBuf[:]), nil
buildNumberStr, err := readRegistryKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, "CurrentBuild")
if err != nil {
return infra.OS{}, err
}

buildNumber, err := strconv.Atoi(buildNumberStr)
if err != nil {
return infra.OS{}, err
}

prettyName := getWindowsVersion(productName, buildNumber)

versionID, err := readRegistryKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, "ReleaseId")
if err != nil {
return infra.OS{}, err
}

version, err := readRegistryKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, "CurrentVersion")
if err != nil {
return infra.OS{}, err
}

versionCodename, err := readRegistryKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, "DisplayVersion")
if err != nil {
versionCodename = "N/A"
}

osInfo := infra.OS{
PrettyName: prettyName,
Name: "Windows",
VersionID: versionID,
Version: version,
VersionCodename: versionCodename,
ID: "windows",
IDLike: "windows",
}

return osInfo, nil
}

func getKernelLastModifiedDate() (string, error) {
Expand Down Expand Up @@ -296,9 +340,9 @@ func GetComputeInfo() (infra.Compute, error) {
var compute infra.Compute

// OS information
releaseVersion, err := getWindowsReleaseVersion()
os, err := getOSProperties()
if err != nil {
return infra.Compute{}, err
return compute, err
}

// host information
Expand Down Expand Up @@ -428,13 +472,7 @@ func GetComputeInfo() (infra.Compute, error) {
// All of compute information
compute = infra.Compute{
OS: infra.System{
OS: infra.OS{
Name: h.OS,
Vendor: h.Platform,
Version: h.PlatformVersion,
Release: releaseVersion,
Architecture: h.KernelArch,
},
OS: os,
Kernel: infra.Kernel{
Release: h.KernelVersion,
Version: kernelLastModifiedDate,
Expand Down
38 changes: 27 additions & 11 deletions agent/pkg/api/rest/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,24 +517,40 @@ const docTemplate = `{
"github_com_cloud-barista_cm-honeybee_agent_pkg_api_rest_model_onprem_infra.OS": {
"type": "object",
"required": [
"release",
"vendor"
"name",
"pretty_name",
"version"
],
"properties": {
"architecture": {
"type": "string"
"id": {
"type": "string",
"example": "ubuntu"
},
"name": {
"type": "string"
"id_like": {
"type": "string",
"example": "debian"
},
"release": {
"type": "string"
"name": {
"type": "string",
"example": "Ubuntu"
},
"vendor": {
"type": "string"
"pretty_name": {
"description": "Pretty name",
"type": "string",
"example": "Ubuntu 22.04.3 LTS"
},
"version": {
"type": "string"
"description": "Full version string",
"type": "string",
"example": "22.04.3 LTS (Jammy Jellyfish)"
},
"version_codename": {
"type": "string",
"example": "jammy"
},
"version_id": {
"type": "string",
"example": "22.04"
}
}
},
Expand Down
38 changes: 27 additions & 11 deletions agent/pkg/api/rest/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -510,24 +510,40 @@
"github_com_cloud-barista_cm-honeybee_agent_pkg_api_rest_model_onprem_infra.OS": {
"type": "object",
"required": [
"release",
"vendor"
"name",
"pretty_name",
"version"
],
"properties": {
"architecture": {
"type": "string"
"id": {
"type": "string",
"example": "ubuntu"
},
"name": {
"type": "string"
"id_like": {
"type": "string",
"example": "debian"
},
"release": {
"type": "string"
"name": {
"type": "string",
"example": "Ubuntu"
},
"vendor": {
"type": "string"
"pretty_name": {
"description": "Pretty name",
"type": "string",
"example": "Ubuntu 22.04.3 LTS"
},
"version": {
"type": "string"
"description": "Full version string",
"type": "string",
"example": "22.04.3 LTS (Jammy Jellyfish)"
},
"version_codename": {
"type": "string",
"example": "jammy"
},
"version_id": {
"type": "string",
"example": "22.04"
}
}
},
Expand Down
26 changes: 20 additions & 6 deletions agent/pkg/api/rest/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -236,19 +236,33 @@ definitions:
type: object
github_com_cloud-barista_cm-honeybee_agent_pkg_api_rest_model_onprem_infra.OS:
properties:
architecture:
id:
example: ubuntu
type: string
name:
id_like:
example: debian
type: string
release:
name:
example: Ubuntu
type: string
vendor:
pretty_name:
description: Pretty name
example: Ubuntu 22.04.3 LTS
type: string
version:
description: Full version string
example: 22.04.3 LTS (Jammy Jellyfish)
type: string
version_codename:
example: jammy
type: string
version_id:
example: "22.04"
type: string
required:
- release
- vendor
- name
- pretty_name
- version
type: object
github_com_cloud-barista_cm-honeybee_agent_pkg_api_rest_model_onprem_infra.Storage:
properties:
Expand Down
12 changes: 7 additions & 5 deletions agent/pkg/api/rest/model/onprem/infra/compute.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package infra

type OS struct {
Name string `json:"name"`
Vendor string `json:"vendor" validate:"required"`
Version string `json:"version"`
Release string `json:"release" validate:"required"`
Architecture string `json:"architecture"`
PrettyName string `json:"pretty_name" validate:"required" example:"Ubuntu 22.04.3 LTS"` // Pretty name
Name string `json:"name,omitempty" validate:"required" example:"Ubuntu"`
VersionID string `json:"version_id,omitempty" example:"22.04"`
Version string `json:"version,omitempty" validate:"required" example:"22.04.3 LTS (Jammy Jellyfish)"` // Full version string
VersionCodename string `json:"version_codename,omitempty" example:"jammy"`
ID string `json:"id,omitempty" example:"ubuntu"`
IDLike string `json:"id_like,omitempty" example:"debian"`
}

type Kernel struct {
Expand Down

0 comments on commit 1775856

Please sign in to comment.