-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathrelease.go
More file actions
84 lines (70 loc) · 2.56 KB
/
release.go
File metadata and controls
84 lines (70 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package os
import (
"context"
"fmt"
"os"
"github.com/NVIDIA/aicr/pkg/collector/file"
"github.com/NVIDIA/aicr/pkg/errors"
"github.com/NVIDIA/aicr/pkg/measurement"
)
var (
filePathReleasePrimary = "/etc/os-release"
filePathReleaseFallback = "/usr/lib/os-release"
fileKVDelRelease = "="
)
// collectRelease gathers OS release information from /etc/os-release.
// It returns a measurement subtype containing key-value pairs of release data.
// Example keys include NAME, VERSION, ID, PRETTY_NAME, etc.
// Per freedesktop.org spec, falls back to /usr/lib/os-release if primary file doesn't exist.
//
// NAME="Ubuntu"
// ID=ubuntu
// VERSION_ID="22.04"
// PRETTY_NAME="Ubuntu 22.04.4 LTS"
func (c *Collector) collectRelease(ctx context.Context) (*measurement.Subtype, error) {
// Check if context is canceled
if err := ctx.Err(); err != nil {
return nil, errors.Wrap(errors.ErrCodeTimeout, "release collection cancelled", err)
}
// Try primary location first, fall back to alternative per freedesktop.org spec
root := filePathReleasePrimary
if _, err := os.Stat(root); os.IsNotExist(err) {
root = filePathReleaseFallback
}
parser := file.NewParser(
file.WithKVDelimiter(fileKVDelRelease),
file.WithVTrimChars(`"'`),
// Remove surrounding quotes if any per freedesktop.org spec
file.WithSkipComments(true),
// Skip malformed lines (lines without '=' that got empty default value)
// Empty values from GetMap with empty vDefault indicate malformed lines
file.WithSkipEmptyValues(true),
)
params, err := parser.GetMap(root)
if err != nil {
return nil, errors.Wrap(errors.ErrCodeInternal, fmt.Sprintf("failed to read os release from %s", root), err)
}
// Pre-allocate with typical capacity (most files have 10-15 fields)
readings := make(map[string]measurement.Reading, 15)
for key, value := range params {
readings[key] = measurement.Str(value)
}
res := &measurement.Subtype{
Name: "release",
Data: readings,
}
return res, nil
}