-
Notifications
You must be signed in to change notification settings - Fork 207
/
host_utils.go
216 lines (182 loc) · 6.33 KB
/
host_utils.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package hostutil
import (
"encoding/json"
"fmt"
"net/http"
"regexp"
"github.com/go-openapi/swag"
"github.com/jinzhu/gorm"
"github.com/openshift/assisted-service/internal/common"
"github.com/openshift/assisted-service/models"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/thoas/go-funk"
)
const (
MaxHostnameLength = 253
)
func GetCurrentHostName(host *models.Host) (string, error) {
var inventory models.Inventory
if host.RequestedHostname != "" {
return host.RequestedHostname, nil
}
err := json.Unmarshal([]byte(host.Inventory), &inventory)
if err != nil {
return "", err
}
return inventory.Hostname, nil
}
func GetHostnameForMsg(host *models.Host) string {
hostName, err := GetCurrentHostName(host)
// An error here probably indicates that the agent didn't send inventory yet, fall back to UUID
if err != nil || hostName == "" {
return host.ID.String()
}
return hostName
}
func GetEventSeverityFromHostStatus(status string) string {
switch status {
case models.HostStatusDisconnected:
return models.EventSeverityWarning
case models.HostStatusInstallingPendingUserAction:
return models.EventSeverityWarning
case models.HostStatusInsufficient:
return models.EventSeverityWarning
case models.HostStatusError:
return models.EventSeverityError
default:
return models.EventSeverityInfo
}
}
func ValidateHostname(hostname string) error {
if len(hostname) > MaxHostnameLength {
return common.NewApiError(http.StatusBadRequest, errors.New("hostname is too long"))
}
pattern := "^[a-z0-9][a-z0-9-]{0,62}(?:[.][a-z0-9-]{1,63})*$"
b, err := regexp.MatchString(pattern, hostname)
if err != nil {
return common.NewApiError(http.StatusInternalServerError, errors.Wrapf(err, "Matching hostname"))
}
if !b {
return common.NewApiError(http.StatusBadRequest, errors.Errorf("Hostname does not pass required regex validation: %s. Hostname: %s", pattern, hostname))
}
return nil
}
// determineDefaultInstallationDisk considers both the previously set installation disk and the current list of valid
// disks to determine the current required installation disk.
//
// Once the installation disk has been set, we usually no longer change it, even when an inventory update occurs
// that contains new disks that might be better "fit" for installation. This is because this field can also be set by
// the user via the API, and we don't want inventory updates to override the user's choice. However, if the disk that
// was set is no longer part of the inventory, the new installation disk is re-evaluated because it is not longer
// a valid choice.
func DetermineInstallationDisk(disks []*models.Disk, installationPath string) *models.Disk {
if len(disks) == 0 {
return nil
}
installationDisk := GetDiskByInstallationPath(disks, installationPath)
if installationDisk == nil {
installationDisk = disks[0]
}
return installationDisk
}
func GetDeviceIdentifier(installationDisk *models.Disk) string {
// We changed the host.installationDiskPath to contain the disk id instead of the disk path.
// Here we updates the old installationDiskPath to the disk id.
// (That's the reason we return the disk.ID instead of the previousInstallationDisk if exist)
if installationDisk.ID != "" {
return installationDisk.ID
}
// Old inventory or a bug
return GetDeviceFullName(installationDisk)
}
func GetDeviceFullName(installationDisk *models.Disk) string {
return fmt.Sprintf("/dev/%s", installationDisk.Name)
}
func GetHostInstallationPath(host *models.Host) string {
if host.InstallationDiskID != "" {
return host.InstallationDiskID
}
return host.InstallationDiskPath
}
func GetHostInstallationDisk(host *models.Host) (*models.Disk, error) {
inventory, err := common.UnmarshalInventory(host.Inventory)
if err != nil {
return nil, err
}
return GetDiskByInstallationPath(inventory.Disks, GetHostInstallationPath(host)), nil
}
func GetDiskByInstallationPath(disks []*models.Disk, installationPath string) *models.Disk {
if len(disks) == 0 || installationPath == "" {
return nil
}
// We changed the host.installationDiskPath to contain the disk id instead of the disk path.
// We will search for the installation path in the disk.Id and the disk.Path field for backward compatibility.
result := funk.Find(disks, func(disk *models.Disk) bool {
return disk.ID == installationPath || GetDeviceFullName(disk) == installationPath
})
if result == nil {
return nil
}
return result.(*models.Disk)
}
func IgnitionFileName(host *models.Host) string {
return fmt.Sprintf("%s-%s.ign", common.GetEffectiveRole(host), host.ID)
}
var allowedFlags = []string{"--append-karg", "--delete-karg", "-n", "--copy-network", "--network-dir", "--save-partlabel", "--save-partindex", "--image-url"}
func ValidateInstallerArgs(args []string) error {
re := regexp.MustCompile("^-+.*")
for _, arg := range args {
if !re.MatchString(arg) {
continue
}
found := false
for _, f := range allowedFlags {
if arg == f {
found = true
break
}
}
if !found {
return fmt.Errorf("found unexpected flag %s for installer - allowed flags are %v", arg, allowedFlags)
}
}
return nil
}
func IsDay2Host(h *models.Host) bool {
return swag.StringValue(h.Kind) == models.HostKindAddToExistingClusterHost
}
func IsUnboundHost(h *models.Host) bool {
return h.ClusterID == nil
}
func MarshalConnectivityReport(report *models.ConnectivityReport) (string, error) {
if data, err := json.Marshal(report); err != nil {
return "", err
} else {
return string(data), nil
}
}
func UnmarshalConnectivityReport(reportStr string) (*models.ConnectivityReport, error) {
var report models.ConnectivityReport
if err := json.Unmarshal([]byte(reportStr), &report); err != nil {
return nil, err
}
return &report, nil
}
func GetHostCluster(log logrus.FieldLogger, db *gorm.DB, host *models.Host) (*common.Cluster, error) {
var cluster common.Cluster
err := db.First(&cluster, "id = ?", host.ClusterID).Error
if err != nil {
log.WithError(err).Errorf("Failed to find cluster %s", host.ClusterID)
return nil, errors.Errorf("Failed to find cluster %s", host.ClusterID)
}
return &cluster, nil
}
func IsSingleNode(log logrus.FieldLogger, db *gorm.DB, host *models.Host) bool {
cluster, err := GetHostCluster(log, db, host)
if err != nil {
log.Debug("Can't check if host is part of single node OpenShift")
return false
}
return common.IsSingleNodeCluster(cluster)
}