Skip to content
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ This will create a `out/suseconnect` binary on the host.

You can run all unit tests by running `make test`. If you then want to run unit
tests for a specific package, you can simply run it as you would do for any Go
project, for example: `go test ./internal/collectors/`.
project, for example: `go test ./pkg/collectors/`.

For feature tests you first need to create an `.env` file in the root directory
of the project with the following contents:
Expand Down
1 change: 1 addition & 0 deletions build/packaging/suseconnect-ng.spec
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Requires: ca-certificates
%endif

Requires: coreutils
Requires: pciutils
Requires: zypper
Requires: util-linux
Recommends: systemd
Expand Down
6 changes: 4 additions & 2 deletions cmd/public-api-demo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func runDemo(identifier, version, arch, regcode string) error {

connection.AddRegcodeAuth(request, regcode)

payload, err := conn.Do(request)
_, payload, err := conn.Do(request)
if err != nil {
return err
}
Expand Down Expand Up @@ -109,7 +109,9 @@ func runDemo(identifier, version, arch, regcode string) error {
"instance_data": "<document>{}</document>",
}

status, statusErr := registration.Status(conn, hostname, systemInformation, extraData)
profiles := registration.DataProfiles{}

status, statusErr := registration.Status(conn, hostname, systemInformation, profiles, extraData)
if statusErr != nil {
return statusErr
}
Expand Down
19 changes: 17 additions & 2 deletions cmd/suseconnect/suseconnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/SUSE/connect-ng/internal/util"
"github.com/SUSE/connect-ng/internal/zypper"
"github.com/SUSE/connect-ng/pkg/connection"
"github.com/SUSE/connect-ng/pkg/profiles"
"github.com/SUSE/connect-ng/pkg/registration"
)

Expand Down Expand Up @@ -118,6 +119,7 @@ func main() {
flag.BoolVar(&info, "i", false, "")

flag.Parse()

if version {
fmt.Println(connect.GetShortenedVersion())
os.Exit(0)
Expand Down Expand Up @@ -263,6 +265,8 @@ func main() {
fmt.Println(output)
os.Exit(0)
} else if deRegister {
// Clear ProfileCache on deregister even if dereg does not succeed.
profiles.DeleteProfileCache()
err := connect.Deregister(api, opts)
if jsonFlag && err != nil {
out := connect.RegisterOut{Success: false, Message: err.Error()}
Expand All @@ -276,6 +280,8 @@ func main() {
if jsonFlag {
exitOnError(errors.New("cannot use the json option with the 'cleanup' command"), api, opts)
}
// Clear ProfileCache on cleanup even if cleanup does not succeed.
profiles.DeleteProfileCache()
err := connect.Cleanup(opts.BaseURL, opts.FsRoot)
exitOnError(err, api, opts)
} else if rollback {
Expand All @@ -285,13 +291,19 @@ func main() {
err := connect.Rollback(api.GetConnection(), opts)
exitOnError(err, api, opts)
} else if info {
sysInfo, err := connect.FetchSystemInformation()
sysInfo, err := connect.FetchSystemInformation("")
exitOnError(err, api, opts)

profileInfo, err := connect.FetchSystemProfiles("", false)
exitOnError(err, api, opts)

out, err := json.Marshal(sysInfo)
exitOnError(err, api, opts)

fmt.Print(string(out))
out1, err := json.Marshal(profileInfo)
exitOnError(err, api, opts)

fmt.Print(string(out) + string(out1))
} else {
if instanceDataFile != "" && opts.IsScc() {
fmt.Print("Please use --instance-data only in combination ")
Expand All @@ -316,6 +328,8 @@ func main() {

err := connect.Register(api, opts)
if err != nil {
// Clear profile cache on registration errors
profiles.DeleteProfileCache()
if jsonFlag {
out := connect.RegisterOut{Success: false, Message: err.Error()}
str, _ := json.Marshal(&out)
Expand Down Expand Up @@ -345,6 +359,7 @@ func main() {
}

func exitOnError(err error, api connect.WrappedAPI, opts *connect.Options) {
util.Debug.Println("exitOnError err: ", err)
if err == nil {
return
}
Expand Down
8 changes: 4 additions & 4 deletions internal/connect/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ func IsOutdatedRegProxy(conn connection.Connection, opts *Options) bool {
return true
}

_, err = conn.Do(req)
_, _, err = conn.Do(req)
if err == nil {
return true
}
Expand Down Expand Up @@ -418,7 +418,7 @@ func InstallerUpdates(conn connection.Connection, product registration.Product)
}
req = connection.AddQuery(req, product.ToQuery())

resp, err := conn.Do(req)
_, resp, err := conn.Do(req)
if err != nil {
return repos, err
}
Expand Down Expand Up @@ -451,7 +451,7 @@ func SyncProducts(conn connection.Connection, products []registration.Product) (

connection.AddSystemAuth(request, login, password)

response, doErr := conn.Do(request)
_, response, doErr := conn.Do(request)
if doErr != nil {
return remoteProducts, doErr
}
Expand Down Expand Up @@ -500,7 +500,7 @@ func updateMigrations(conn connection.Connection, url string, payload any) ([]Mi

connection.AddSystemAuth(request, login, password)

response, doErr := conn.Do(request)
_, response, doErr := conn.Do(request)
if doErr != nil {
return migrations, doErr
}
Expand Down
64 changes: 45 additions & 19 deletions internal/connect/collectors.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,57 @@
package connect

import (
"github.com/SUSE/connect-ng/internal/collectors"
"github.com/SUSE/connect-ng/pkg/collectors"
"github.com/SUSE/connect-ng/pkg/profiles"
)

// Collectors which are to be used when fetching the system information.
var usedCollectors = []collectors.Collector{
collectors.CPU{},
collectors.Hostname{},
collectors.Memory{},
collectors.UUID{},
collectors.Virtualization{},
collectors.CloudProvider{},
collectors.Architecture{},
collectors.ContainerRuntime{},

// Optional collectors
collectors.Uname{},
collectors.SAP{},
}

// Fetch system information based on the available collectors for the system.
func FetchSystemInformation() (collectors.Result, error) {
arch, err := collectors.DetectArchitecture()
func FetchSystemInformation(arch string) (collectors.Result, error) {
var usedCollectors = []collectors.Collector{
collectors.CPU{},
collectors.Hostname{},
collectors.Memory{},
collectors.UUID{},
collectors.Virtualization{},
collectors.CloudProvider{},
collectors.Architecture{},
collectors.ContainerRuntime{},

// Optional collectors
collectors.Uname{},
collectors.SAP{},
}

var err error = nil
if arch == "" {
arch, err = collectors.DetectArchitecture()
}

if err != nil {
return collectors.NoResult, err
}
return collectors.CollectInformation(arch, usedCollectors)
}

// Fetch system profile information
func FetchSystemProfiles(arch string, updateCache bool) (collectors.Result, error) {
var usedCollectors = []collectors.Collector{
collectors.PCI{UpdateDataIDs: updateCache},
collectors.LSMOD{UpdateDataIDs: updateCache},
}

var err error = nil
if arch == "" {
arch, err = collectors.DetectArchitecture()
}

if err != nil {
return collectors.NoResult, err
}
profile, err := collectors.CollectInformation(arch, usedCollectors)
if err != nil {
profiles.DeleteProfileCache()
return collectors.NoResult, err
}
return profile, err
}
34 changes: 29 additions & 5 deletions internal/connect/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (
"os"
"strings"

"github.com/SUSE/connect-ng/internal/collectors"
"github.com/SUSE/connect-ng/internal/credentials"
"github.com/SUSE/connect-ng/internal/util"
"github.com/SUSE/connect-ng/pkg/collectors"
"github.com/SUSE/connect-ng/pkg/connection"
"github.com/SUSE/connect-ng/pkg/labels"
"github.com/SUSE/connect-ng/pkg/profiles"
"github.com/SUSE/connect-ng/pkg/registration"
)

Expand Down Expand Up @@ -105,7 +106,8 @@ func NewWrappedAPI(opts *Options) WrappedAPI {
// Submit a keepalive request to the server pointed by the configured
// connection.
func (w Wrapper) KeepAlive(uptimeTracking bool) error {
hwinfo, err := FetchSystemInformation()
arch, _ := collectors.DetectArchitecture()
hwinfo, err := FetchSystemInformation(arch)
if err != nil {
return fmt.Errorf("could not fetch system's information: %v", err)
}
Expand All @@ -122,15 +124,28 @@ func (w Wrapper) KeepAlive(uptimeTracking bool) error {
extraData["online_at"] = data
}

code, err := registration.Status(w.Connection, hostname, hwinfo, extraData)
if code != registration.Registered {
profileData, err := FetchSystemProfiles(arch, true)
if err != nil {
profiles.DeleteProfileCache()
return fmt.Errorf("could not fetch system's profiles: %v", err)
}

code, err := registration.Status(w.Connection, hostname, hwinfo, profileData, extraData)
if code == registration.ClearCache {
profiles.DeleteProfileCache()
} else if code == registration.Unregistered {
profiles.DeleteProfileCache()
return fmt.Errorf("trying to send a keepalive from a system not yet registered. Register this system first")
} else if err != nil {
profiles.DeleteProfileCache()
}

return err
}

func (w Wrapper) Register(regcode, instanceDataFile string) error {
hwinfo, err := FetchSystemInformation()
arch, _ := collectors.DetectArchitecture()
hwinfo, err := FetchSystemInformation(arch)
if err != nil {
return fmt.Errorf("could not fetch system's information: %v", err)
}
Expand All @@ -148,9 +163,18 @@ func (w Wrapper) Register(regcode, instanceDataFile string) error {
extraData["instance_data"] = string(data)
}

// Clear profile data before registration
profiles.DeleteProfileCache()
profileData, err := FetchSystemProfiles(arch, true)
extraData["data_profiles"] = profileData

// NOTE: we are not interested in the code. Hence, we don't save it
// anywhere.
_, err = registration.Register(w.Connection, regcode, hostname, hwinfo, extraData)

if err != nil {
profiles.DeleteProfileCache()
}
return err
}

Expand Down
2 changes: 1 addition & 1 deletion internal/connect/wrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func TestAssignLabelsWithSpacesAndNewlines(t *testing.T) {
assert := assert.New(t)

conn, _ := connection.NewMockConnectionWithCredentials()
_, conn, _ := connection.NewMockConnectionWithCredentials()
wrapper := Wrapper{
Connection: conn,
Registered: true,
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ import (
"maps"

"github.com/SUSE/connect-ng/internal/util"
"github.com/SUSE/connect-ng/pkg/profiles"
)

type Result = map[string]interface{}
type Result = profiles.Result

const (
ARCHITECTURE_X86_64 = "x86_64"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
55 changes: 55 additions & 0 deletions pkg/collectors/lsmod.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package collectors

import (
"bufio"
"bytes"
"sort"
"strings"

"github.com/SUSE/connect-ng/internal/util"
"github.com/SUSE/connect-ng/pkg/profiles"
)

type LSMOD struct {
UpdateDataIDs bool
}

const kernModChecksumFile = "kernel-modules.txt"
const lsmodTag = "mod_list"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd just use kernel_modules.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can make the change, but should wait for next office hours, since mod_list is what is expected by the RMT code. Not sure about SCC.


// Process output from lsmod to remove dups and get sorted list of kernel mods.
func getKernelMods(inputStream []byte) ([]string, error) {
scanner := bufio.NewScanner(bytes.NewReader(inputStream))

itemMap := make(map[string]bool)
list := []string{}
// Skip header line
scanner.Scan()
for scanner.Scan() {
item := strings.Fields(scanner.Text())[0]
if _, exists := itemMap[item]; !exists {
itemMap[item] = true
list = append(list, item)
}
}

if err := scanner.Err(); err != nil {
return nil, err
}

sort.Strings(list)
return list, nil
}

func (lsmod LSMOD) run(arch string) (Result, error) {
util.Debug.Print("lsmod.UpdateDataIDs: ", lsmod.UpdateDataIDs)
modInfo, err := util.Execute([]string{"lsmod"}, nil)
if err != nil {
return Result{}, err
}

sortedMods, _ := getKernelMods(modInfo)
result, _ := profiles.BuildProfile(lsmod.UpdateDataIDs, lsmodTag, kernModChecksumFile, sortedMods)

return result, nil
}
Loading