Skip to content

Commit

Permalink
Merge pull request #1234 from gibmat/add-os-info-to-instance-state
Browse files Browse the repository at this point in the history
Add operating system details to instance state
  • Loading branch information
stgraber committed Sep 24, 2024
2 parents ba93e12 + 977fd74 commit e066350
Show file tree
Hide file tree
Showing 19 changed files with 2,052 additions and 1,608 deletions.
35 changes: 35 additions & 0 deletions cmd/incus-agent/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import (
"strconv"
"strings"

"github.com/lxc/incus/v6/internal/linux"
"github.com/lxc/incus/v6/internal/server/response"
"github.com/lxc/incus/v6/shared/api"
"github.com/lxc/incus/v6/shared/logger"
"github.com/lxc/incus/v6/shared/osarch"
"github.com/lxc/incus/v6/shared/util"
)

Expand All @@ -39,6 +41,7 @@ func renderState() *api.InstanceState {
Network: networkState(),
Pid: 1,
Processes: processesState(),
OSInfo: osState(),
}
}

Expand Down Expand Up @@ -242,3 +245,35 @@ func processesState() int64 {

return int64(len(pids))
}

func osState() *api.InstanceStateOSInfo {
osInfo := &api.InstanceStateOSInfo{}

// Get information about the OS.
lsbRelease, err := osarch.GetLSBRelease()
if err == nil {
osInfo.OS = lsbRelease["NAME"]
osInfo.OSVersion = lsbRelease["VERSION"]
}

// Get information about the kernel version.
uname, err := linux.Uname()
if err == nil {
osInfo.KernelVersion = uname.Release
}

// Get the hostname.
hostname, err := os.Hostname()
if err == nil {
osInfo.Hostname = hostname
}

// Get the FQDN. To avoid needing to run `hostname -f`, do a reverse host lookup for 127.0.1.1, and if found, return the first hostname as the FQDN.
fqdn, err := net.LookupAddr("127.0.1.1")
if err == nil {
// Take the first returned hostname and trim the trailing dot.
osInfo.FQDN = strings.TrimSuffix(fqdn[0], ".")
}

return osInfo
}
11 changes: 11 additions & 0 deletions cmd/incus/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,17 @@ func (c *cmdInfo) instanceInfo(d incus.InstanceServer, remote config.Remote, nam
fmt.Printf(i18n.G("Started: %s")+"\n", inst.State.StartedAt.Local().Format(dateLayout))
}

// Operating System info
if inst.State.OSInfo != nil {
fmt.Println("\n" + i18n.G("Operating System:"))
osInfo := fmt.Sprintf(" %s: %s\n", i18n.G("OS"), inst.State.OSInfo.OS)
osInfo += fmt.Sprintf(" %s: %s\n", i18n.G("OS Version"), inst.State.OSInfo.OSVersion)
osInfo += fmt.Sprintf(" %s: %s\n", i18n.G("Kernel Version"), inst.State.OSInfo.KernelVersion)
osInfo += fmt.Sprintf(" %s: %s\n", i18n.G("Hostname"), inst.State.OSInfo.Hostname)
osInfo += fmt.Sprintf(" %s: %s\n", i18n.G("FQDN"), inst.State.OSInfo.FQDN)
fmt.Print(osInfo)
}

fmt.Println("\n" + i18n.G("Resources:"))
// Processes
fmt.Printf(" "+i18n.G("Processes: %d")+"\n", inst.State.Processes)
Expand Down
4 changes: 4 additions & 0 deletions doc/api-extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2606,3 +2606,7 @@ This implements a new `security.promiscuous` configuration option on OVN NICs.
## `ovn_nic_ip_address_none`

This adds `none` as a value for `ipv4.address` and `ipv6.address` for OVN NICs.

## `instances_state_os_info`

This extension adds a pointer to an `InstanceStateOSInfo` struct to the instance's state API.
32 changes: 32 additions & 0 deletions doc/rest-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2161,6 +2161,8 @@ definitions:
description: Network usage key/value pairs
type: object
x-go-name: Network
os_info:
$ref: '#/definitions/InstanceStateOSInfo'
pid:
description: PID of the runtime
example: 7281
Expand Down Expand Up @@ -2375,6 +2377,36 @@ definitions:
x-go-name: PacketsSent
type: object
x-go-package: github.com/lxc/incus/v6/shared/api
InstanceStateOSInfo:
properties:
fqdn:
description: FQDN of the instance.
example: myhost.mydomain.local
type: string
x-go-name: FQDN
hostname:
description: Hostname of the instance.
example: myhost
type: string
x-go-name: Hostname
kernel_version:
description: Version of the kernel running in the instance.
example: 6.1.0-25-amd64
type: string
x-go-name: KernelVersion
os:
description: Operating system running in the instance.
example: Debian GNU/Linux
type: string
x-go-name: OS
os_version:
description: Version of the operating system.
example: 12 (bookworm)
type: string
x-go-name: OSVersion
title: InstanceStateOSInfo represents the operating system information section of an instance's state.
type: object
x-go-package: github.com/lxc/incus/v6/shared/api
InstanceStatePut:
properties:
action:
Expand Down
1 change: 1 addition & 0 deletions internal/version/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ var APIExtensions = []string{
"storage_lvm_metadatasize",
"ovn_nic_promiscuous",
"ovn_nic_ip_address_none",
"instances_state_os_info",
}

// APIExtensionsCount returns the number of available API extensions.
Expand Down
Loading

0 comments on commit e066350

Please sign in to comment.