-
Notifications
You must be signed in to change notification settings - Fork 17
/
hardware.go
150 lines (125 loc) · 4.45 KB
/
hardware.go
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package hardware
import (
"errors"
"fmt"
"reflect"
log "github.com/sirupsen/logrus"
runc "github.com/opencontainers/runc/libcontainer/devices"
"github.com/openshift/assisted-installer-agent/src/inventory"
"github.com/openshift/assisted-installer-agent/src/util"
"github.com/project-flotta/flotta-device-worker/internal/mount"
"github.com/project-flotta/flotta-operator/models"
)
//go:generate mockgen -package=hardware -destination=mock_hardware.go . Hardware
type Hardware interface {
GetHardwareInformation() (*models.HardwareInfo, error)
GetHardwareImmutableInformation(hardwareInfo *models.HardwareInfo) error
CreateHardwareMutableInformation() (*models.HardwareInfo, error)
GetMutableHardwareInfoDelta(hardwareMutableInfoPrevious models.HardwareInfo, hardwareMutableInfoNew models.HardwareInfo) *models.HardwareInfo
}
type HardwareInfo struct {
dependencies util.IDependencies
}
func (s *HardwareInfo) GetHardwareInformation() (*models.HardwareInfo, error) {
hardwareInfo := models.HardwareInfo{}
err := s.GetHardwareImmutableInformation(&hardwareInfo)
if err != nil {
return nil, err
}
err = s.getHardwareMutableInformation(&hardwareInfo)
return &hardwareInfo, err
}
func (s *HardwareInfo) GetHardwareImmutableInformation(hardwareInfo *models.HardwareInfo) error {
if !s.isDependenciesSet() {
return errors.New("HardwareInfo object has not been initialized")
}
cpu := inventory.GetCPU(s.dependencies)
systemVendor := inventory.GetVendor(s.dependencies)
hardwareInfo.CPU = &models.CPU{
Architecture: cpu.Architecture,
ModelName: cpu.ModelName,
Flags: []string{},
}
hardwareInfo.SystemVendor = (*models.SystemVendor)(systemVendor)
if hostDevices, err := s.getHostDevices(); err != nil {
log.Warnf("failed to list host devices: %v", err)
} else {
hardwareInfo.HostDevices = hostDevices
}
return nil
}
func (s *HardwareInfo) CreateHardwareMutableInformation() (*models.HardwareInfo, error) {
hardwareInfo := models.HardwareInfo{}
err := s.getHardwareMutableInformation(&hardwareInfo)
if err != nil {
return nil, err
}
return &hardwareInfo, nil
}
func (s *HardwareInfo) getHardwareMutableInformation(hardwareInfo *models.HardwareInfo) error {
if !s.isDependenciesSet() {
return errors.New("HardwareInfo object has not been initialized")
}
hostname := inventory.GetHostname(s.dependencies)
interfaces := inventory.GetInterfaces(s.dependencies)
hardwareInfo.Hostname = hostname
for _, currInterface := range interfaces {
if len(currInterface.IPV4Addresses) == 0 && len(currInterface.IPV6Addresses) == 0 {
continue
}
newInterface := &models.Interface{
IPV4Addresses: currInterface.IPV4Addresses,
IPV6Addresses: currInterface.IPV6Addresses,
Flags: []string{},
}
hardwareInfo.Interfaces = append(hardwareInfo.Interfaces, newInterface)
}
mounts, _, err := mount.GetMounts(s.dependencies)
if err != nil {
return fmt.Errorf("cannot list mounts: %s", err)
}
hardwareInfo.Mounts = mounts
return nil
}
func (s *HardwareInfo) Init(dep util.IDependencies) {
if dep == nil {
s.dependencies = util.NewDependencies("/")
} else {
s.dependencies = dep
}
}
func (s *HardwareInfo) isDependenciesSet() bool {
return s.dependencies != nil
}
func (s *HardwareInfo) GetMutableHardwareInfoDelta(hardwareMutableInfoPrevious models.HardwareInfo, hardwareMutableInfoNew models.HardwareInfo) *models.HardwareInfo {
return GetMutableHardwareInfoDelta(hardwareMutableInfoPrevious, hardwareMutableInfoNew)
}
func (s *HardwareInfo) getHostDevices() ([]*models.HostDevice, error) {
runcDevices, err := runc.HostDevices()
if err != nil {
return []*models.HostDevice{}, err
}
devices := make([]*models.HostDevice, 0, len(runcDevices))
for _, d := range runcDevices {
device := models.HostDevice{
Path: d.Path,
DeviceType: string(d.Type),
Major: d.Major,
Minor: d.Minor,
UID: int64(d.Uid),
Gid: int64(d.Gid),
}
devices = append(devices, &device)
}
return devices, nil
}
func GetMutableHardwareInfoDelta(hardwareMutableInfoPrevious models.HardwareInfo, hardwareMutableInfoNew models.HardwareInfo) *models.HardwareInfo {
hardwareInfo := &models.HardwareInfo{}
if hardwareMutableInfoPrevious.Hostname != hardwareMutableInfoNew.Hostname {
hardwareInfo.Hostname = hardwareMutableInfoNew.Hostname
}
if !reflect.DeepEqual(hardwareMutableInfoPrevious.Interfaces, hardwareMutableInfoNew.Interfaces) {
hardwareInfo.Interfaces = hardwareMutableInfoNew.Interfaces
}
return hardwareInfo
}