-
Notifications
You must be signed in to change notification settings - Fork 147
/
preflight.go
113 lines (93 loc) · 3.01 KB
/
preflight.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
package utils
import (
"os"
"regexp"
"runtime"
"strings"
"github.com/AlecAivazis/survey/v2"
"github.com/defenseunicorns/zarf/cli/config"
"github.com/sirupsen/logrus"
)
func CheckHostName(hostname string) bool {
expression := regexp.MustCompile(`^[a-zA-Z0-9\-.]+$`)
return expression.MatchString(hostname)
}
func IsValidHostName() bool {
logrus.Info("Preflight check: validating hostname")
// Quick & dirty character validation instead of a complete RFC validation since the OS is already allowing it
hostname, err := os.Hostname()
if err != nil {
return false
}
return CheckHostName(hostname)
}
func IsUserRoot() bool {
logrus.Info("Preflight check: validating user is root")
return os.Getuid() == 0
}
func IsAMD64() bool {
logrus.Info("Preflight check: validating AMD64 arch")
return runtime.GOARCH == "amd64"
}
func IsLinux() bool {
logrus.Info("Preflight check: validating os type")
return runtime.GOOS == "linux"
}
func IsRHEL() bool {
return !InvalidPath("/etc/redhat-release")
}
func GetValidComponents(allComponents []config.ZarfComponent, requestedComponentNames []string) []config.ZarfComponent {
validComponentsList := []config.ZarfComponent{}
confirmedCompoonents := make([]bool, len(requestedComponentNames))
for _, component := range allComponents {
confirmComponent := component.Required
// If the component is not required check if the user wants it deployed
if !confirmComponent {
// Check if this is one of the components that has been requested
if len(requestedComponentNames) > 0 {
for index, requestedComponent := range requestedComponentNames {
if strings.ToLower(requestedComponent) == component.Name {
confirmComponent = true
confirmedCompoonents[index] = true
}
}
} else {
// Since no requested components were provided, prompt the user
prompt := &survey.Confirm{
Message: "Deploy the " + component.Name + " component?",
Default: component.Default,
Help: component.Description,
}
_ = survey.AskOne(prompt, &confirmComponent)
}
}
if confirmComponent {
validComponentsList = append(validComponentsList, component)
}
}
// Verify that we were able to successfully identify all of the requested components
nonMatchedComponents := []string{}
for requestedComponentIndex, componentMatched := range confirmedCompoonents {
if componentMatched == false {
nonMatchedComponents = append(nonMatchedComponents, requestedComponentNames[requestedComponentIndex])
}
}
if len(nonMatchedComponents) > 0 {
logrus.Fatalf("Unable to find these components to deploy: %v.", nonMatchedComponents)
}
return validComponentsList
}
func RunPreflightChecks() {
if !IsLinux() {
logrus.Fatal("This program requires a Linux OS")
}
if !IsAMD64() {
logrus.Fatal("This program currently only runs on AMD64 architectures")
}
if !IsUserRoot() {
logrus.Fatal("You must run this program as root.")
}
if !IsValidHostName() {
logrus.Fatal("Please ensure this hostname is valid according to https://www.ietf.org/rfc/rfc1123.txt.")
}
}