-
Notifications
You must be signed in to change notification settings - Fork 287
/
commands.go
129 lines (106 loc) Β· 3.39 KB
/
commands.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
package framework
import (
"os"
"path/filepath"
"strings"
releasev1alpha1 "github.com/aws/eks-anywhere/release/api/v1alpha1"
)
type CommandOpt func(*string, *[]string) (err error)
func appendOpt(new ...string) CommandOpt {
return func(binaryPath *string, args *[]string) (err error) {
*args = append(*args, new...)
return nil
}
}
func withKubeconfig(kubeconfigFile string) CommandOpt {
return appendOpt("--kubeconfig", kubeconfigFile)
}
func WithControlPlaneWaitTimeout(timeout string) CommandOpt {
return appendOpt("--control-plane-wait-timeout", timeout)
}
func WithExternalEtcdWaitTimeout(timeout string) CommandOpt {
return appendOpt("--external-etcd-wait-timeout", timeout)
}
func WithPerMachineWaitTimeout(timeout string) CommandOpt {
return appendOpt("--per-machine-wait-timeout", timeout)
}
func ExecuteWithEksaRelease(release *releasev1alpha1.EksARelease) CommandOpt {
return executeWithBinaryCommandOpt(func() (string, error) {
return getBinary(release)
})
}
// PackagedBinary represents a binary that can be extracted
// executed from local disk.
type PackagedBinary interface {
// BinaryPath returns the local disk path to the binary.
BinaryPath() (string, error)
}
// ExecuteWithBinary executes the command with a binary from an specific path.
func ExecuteWithBinary(eksa PackagedBinary) CommandOpt {
return executeWithBinaryCommandOpt(func() (string, error) {
return eksa.BinaryPath()
})
}
// WithSudo add prefix "sudo" to the command. And preserve PATH.
func WithSudo(user string) CommandOpt {
return func(binaryPath *string, args *[]string) (err error) {
*args = append([]string{*binaryPath}, *args...)
*binaryPath = "sudo"
if user != "" {
*args = append([]string{"-E", "PATH=$PATH", "-u", user}, *args...)
}
return nil
}
}
// WithBundlesOverride modify bundles-override.
func WithBundlesOverride(bundles string) CommandOpt {
return appendOpt("--bundles-override", bundles)
}
type binaryFetcher func() (binaryPath string, err error)
func executeWithBinaryCommandOpt(fetcher binaryFetcher) CommandOpt {
return func(binaryPath *string, args *[]string) (err error) {
b, err := fetcher()
if err != nil {
return err
}
*binaryPath = b
if err = setEksctlVersionEnvVar(); err != nil {
return err
}
// When bundles override is present, the manifest belongs to the current
// build of the CLI and it's intended to be used only with that version
removeFlag("--bundles-override", args)
return nil
}
}
func removeFlag(flag string, args *[]string) {
for i, a := range *args {
if a == flag {
elementsToDelete := 1
// If it's not the last arg and next arg is not a flag,
// that means it's the value for the current flag, remove it as well
if i < len(*args)-1 && !strings.HasPrefix((*args)[i+1], "-") {
elementsToDelete = 2
}
*args = append((*args)[:i], (*args)[i+elementsToDelete:]...)
break
}
}
}
// DefaultLocalEKSABinaryPath returns the full path for the local eks-a binary being tested.
func DefaultLocalEKSABinaryPath() (string, error) {
binDir, err := DefaultLocalEKSABinDir()
if err != nil {
return "", err
}
return filepath.Join(binDir, "eksctl-anywhere"), nil
}
// DefaultLocalEKSABinDir returns the full path for the local directory where
// the tested eks-a binary lives.
func DefaultLocalEKSABinDir() (string, error) {
workDir, err := os.Getwd()
if err != nil {
return "", err
}
return filepath.Join(workDir, "bin"), nil
}