Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix host release name nil pointer #2250

Merged
merged 3 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 3 additions & 26 deletions nodes/host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ package host
import (
"bytes"
"context"
"os"
"path/filepath"
"regexp"

osexec "os/exec"

Expand All @@ -19,6 +16,7 @@ import (
"github.com/srl-labs/containerlab/nodes/state"
"github.com/srl-labs/containerlab/runtime"
"github.com/srl-labs/containerlab/types"
"github.com/srl-labs/containerlab/utils"
)

var kindnames = []string{"host"}
Expand Down Expand Up @@ -59,30 +57,9 @@ func (*host) WithMgmtNet(*types.MgmtNet) {}
// UpdateConfigWithRuntimeInfo is a noop for hosts.
func (*host) UpdateConfigWithRuntimeInfo(_ context.Context) error { return nil }

// getOSRelease returns the OS release of the host by inspecting /etc/*-release.
func getOSRelease() string {
image := "N/A"

matches, err := filepath.Glob("/etc/*-release")
if err != nil {
return image
}
dat, err := os.ReadFile(matches[0])
if err != nil {
return image
}
// DISTRIB_DESCRIPTION exists in lsb-release, but not os-release.
// the lsb-release is coming first in the glob, so it works.
re := regexp.MustCompile(`DISTRIB_DESCRIPTION="(.*)"`)

regexres := re.FindSubmatch(dat)

return string(regexres[1])
}

// GetContainers returns a basic skeleton of a container to enable graphing of hosts kinds.
func (*host) GetContainers(_ context.Context) ([]runtime.GenericContainer, error) {
image := getOSRelease()
func (h *host) GetContainers(_ context.Context) ([]runtime.GenericContainer, error) {
image := utils.GetOSRelease()

return []runtime.GenericContainer{
{
Expand Down
2 changes: 2 additions & 0 deletions tests/01-smoke/03-linux-nodes-to-bridge-and-host.clab.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ topology:
cmd: ash -c "sleep 9999"
br-01-03-clab:
kind: bridge
hostnode: # is not used, it is here for coverage
kind: host

links:
- endpoints: ["l1:eth1", "br-01-03-clab:l1-eth1"]
Expand Down
35 changes: 35 additions & 0 deletions utils/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"os/exec"
"os/user"
"path/filepath"
"regexp"
"strconv"
"strings"

Expand Down Expand Up @@ -471,3 +472,37 @@ func recursiveChown(path string, uid, gid int) error {
return err
})
}

var osRelease string
hellt marked this conversation as resolved.
Show resolved Hide resolved

// GetOSRelease returns the OS release of the host by inspecting /etc/*-release files.
func GetOSRelease() string {
// return cached result
if osRelease != "" {
return osRelease
}
osRelease = "N/A"

matches, err := filepath.Glob("/etc/*-release")
if err != nil {
return osRelease
}

re := regexp.MustCompile(`(DISTRIB_DESCRIPTION|PRETTY_NAME)="(.*)"`)

for _, match := range matches {
data, err := os.ReadFile(match)
if err != nil {
log.Error(err)
}

match := re.FindSubmatch(data)
// [0] = whole line match, [1] = left side of "=", [2] = right side of "="
if len(match) >= 3 {
osRelease = string(match[2])
break
}
}

return osRelease
}
Loading