-
Notifications
You must be signed in to change notification settings - Fork 785
/
install.go
272 lines (249 loc) · 6.95 KB
/
install.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package packages
import (
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"text/template"
"github.com/pkg/errors"
"github.com/jenkins-x/jx/v2/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/jenkins-x/jx/v2/pkg/versionstream"
"github.com/pborman/uuid"
)
// InstallOrUpdateBinary installs or updates a binary
func InstallOrUpdateBinary(options InstallOrUpdateBinaryOptions) error {
isInstalled, err := IsBinaryWithProperVersionInstalled(options.Binary, options.Version, options.VersionExtractor)
if err != nil {
return err
}
if isInstalled {
return nil
}
downloadUrlTemplate := options.DownloadUrlTemplate
if !options.Archived {
downloadUrlTemplate = BinaryWithExtension(downloadUrlTemplate)
}
urlTemplate, err := template.New(options.Binary).Parse(downloadUrlTemplate)
if err != nil {
return err
}
binDir, err := util.JXBinLocation()
if err != nil {
return err
}
fileName := options.Binary
if !options.SkipPathScan {
flag, err := ShouldInstallBinary(options.Binary)
fileName = options.Binary
if err != nil || !flag {
return err
}
}
if options.Version == "" {
configDir, err := util.ConfigDir()
if err != nil {
return err
}
versionFile := filepath.Join(configDir, "jenkins-x-versions", "packages", options.Binary+".yml")
ver, err := versionstream.LoadStableVersionFile(versionFile)
if err != nil {
return err
}
if ver.Version != "" {
options.Version = ver.Version
}
}
if options.Version == "" {
options.Version, err = util.GetLatestVersionStringFromGitHub(options.GitHubOrganization, options.Binary)
if err != nil {
return err
}
}
extension := "tar.gz"
if runtime.GOOS == "windows" {
extension = "zip"
}
clientUrlBuffer := bytes.NewBufferString("")
variables := map[string]string{"version": options.Version, "os": runtime.GOOS, "osTitle": strings.Title(runtime.GOOS), "arch": runtime.GOARCH, "extension": extension}
err = urlTemplate.Execute(clientUrlBuffer, variables)
if err != nil {
return err
}
fullPath := filepath.Join(binDir, fileName)
tarFile := fullPath
if options.Archived {
tarFile = tarFile + "." + extension
}
downloadUrl := clientUrlBuffer.String()
if options.DownloadUrlTemplateLowerCase {
downloadUrl = strings.ToLower(downloadUrl)
}
err = DownloadFile(downloadUrl, tarFile)
if err != nil {
return err
}
fileNameInArchive := fileName
if options.ArchiveDirectory != "" {
fileNameInArchive = filepath.Join(options.ArchiveDirectory, fileName)
}
if options.Archived {
if extension == "zip" {
zipDir := filepath.Join(binDir, options.Binary+"-tmp-"+uuid.NewUUID().String())
err = os.MkdirAll(zipDir, util.DefaultWritePermissions)
if err != nil {
return err
}
err = util.Unzip(tarFile, zipDir)
if err != nil {
return err
}
f := filepath.Join(zipDir, fileNameInArchive)
exists, err := util.FileExists(f)
if err != nil {
return err
}
if !exists {
return fmt.Errorf("Could not find file %s inside the downloaded file!", f)
}
err = os.Rename(f, fullPath)
if err != nil {
return err
}
err = os.RemoveAll(zipDir)
} else {
err = util.UnTargz(tarFile, binDir, []string{options.Binary, fileNameInArchive})
}
if err != nil {
return err
}
err = os.Remove(tarFile)
if err != nil {
return err
}
}
err = RememberInstalledPackage(options.Binary, options.Version)
if err != nil {
return err
}
return os.Chmod(fullPath, 0755)
}
// AddRequiredBinary add the required binary
func AddRequiredBinary(binName string, deps []string) []string {
d := BinaryShouldBeInstalled(binName)
if d != "" && util.StringArrayIndex(deps, d) < 0 {
deps = append(deps, d)
}
return deps
}
// FilterInstalledDependencies filters installed dependencies
func FilterInstalledDependencies(deps []string) []string {
depsToInstall := []string{}
for _, d := range deps {
binary := BinaryShouldBeInstalled(d)
if binary != "" {
depsToInstall = append(depsToInstall, binary)
}
}
return depsToInstall
}
// InstallOrUpdateBinaryOptions options for install or update binary
type InstallOrUpdateBinaryOptions struct {
Binary string
GitHubOrganization string
DownloadUrlTemplate string
DownloadUrlTemplateLowerCase bool
Version string
SkipPathScan bool
VersionExtractor VersionExtractor
Archived bool
ArchiveDirectory string
}
// ShouldInstallBinary checks if the given binary should be installed
func ShouldInstallBinary(name string) (bool, error) {
fileName := BinaryWithExtension(name)
download := false
binDir, err := util.JXBinLocation()
if err != nil {
return download, errors.Wrapf(err, "unable to find JXBinLocation at %s", binDir)
}
if util.Contains(GlobalBinaryPathWhitelist, name) {
_, err = exec.LookPath(fileName)
if err != nil {
log.Logger().Warnf("%s is not available on your PATH", util.ColorInfo(fileName))
return true, nil
}
return false, nil
}
exists, err := util.FileExists(filepath.Join(binDir, fileName))
if exists {
log.Logger().Debugf("%s is already available in your JXBIN at %s", util.ColorInfo(fileName), util.ColorInfo(binDir))
return download, nil
}
if err != nil {
return download, errors.Wrapf(err, "unable to check files on %s", binDir)
}
download = true
return download, nil
}
// BinaryShouldBeInstalled appends the binary to the deps array if it cannot be found on the $PATH
func BinaryShouldBeInstalled(d string) string {
shouldInstall, err := ShouldInstallBinary(d)
if err != nil {
log.Logger().Warnf("Error detecting if binary should be installed: %s", err.Error())
return ""
}
if shouldInstall {
return d
}
return ""
}
// InstallKubectlWithVersion install a specific version of kubectl
func InstallKubectlWithVersion(version string, skipPathScan bool) error {
return InstallOrUpdateBinary(InstallOrUpdateBinaryOptions{
Binary: "kubectl",
GitHubOrganization: "",
DownloadUrlTemplate: "https://storage.googleapis.com/kubernetes-release/release/v{{.version}}/bin/{{.osTitle}}/{{.arch}}/kubectl",
DownloadUrlTemplateLowerCase: true,
Version: version,
SkipPathScan: skipPathScan,
VersionExtractor: nil,
Archived: false,
})
}
// InstallKubectl installs kubectl
func InstallKubectl(skipPathScan bool) error {
return InstallKubectlWithVersion(KubectlVersion, skipPathScan)
}
// UninstallBinary uninstalls given binary
func UninstallBinary(binDir string, name string) error {
fileName := name
if runtime.GOOS == "windows" {
fileName += ".exe"
}
// try to remove the binary from all paths
var err error
for {
path, err := exec.LookPath(fileName)
if err == nil {
err := os.Remove(path)
if err != nil {
return err
}
} else {
break
}
}
path := filepath.Join(binDir, fileName)
exists, err := util.FileExists(path)
if err != nil {
return nil
}
if exists {
return os.Remove(path)
}
return nil
}