-
Notifications
You must be signed in to change notification settings - Fork 153
/
product.go
214 lines (169 loc) · 4.17 KB
/
product.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
package fluxinstall
import (
"archive/tar"
"bytes"
"compress/gzip"
"context"
"crypto/sha256"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/weaveworks/weave-gitops/pkg/fluxinstall/internal/httpclient"
isrc "github.com/weaveworks/weave-gitops/pkg/fluxinstall/internal/src"
)
type HTTPGetter interface {
Get(url string) (resp *http.Response, err error)
}
type Product struct {
Version string
cli HTTPGetter
}
func NewProduct(version string) *Product {
return &Product{
Version: version,
cli: httpclient.NewHTTPClient(),
}
}
func NewProductWithHTTPClient(version string, cli HTTPGetter) *Product {
return &Product{
Version: version,
cli: cli,
}
}
func (p *Product) IsSourceImpl() isrc.InstallSrcSigil {
return isrc.InstallSrcSigil{}
}
func (p *Product) Install(ctx context.Context) (string, error) {
gitopsCacheFluxDir, err := getFluxBinaryDir(p.Version)
if err != nil {
return "", err
}
// check if the dir not found
if _, err := os.Stat(gitopsCacheFluxDir); os.IsNotExist(err) {
if err := os.MkdirAll(gitopsCacheFluxDir, 0755); err != nil {
return "", err
}
}
// TODO enable windows support
extension := "tar.gz"
binaryURL := fmt.Sprintf("https://github.com/fluxcd/flux2/releases/download/v%s/flux_%s_%s_%s.%s", p.Version, p.Version, runtime.GOOS, runtime.GOARCH, extension)
client := p.cli
resp, err := client.Get(binaryURL)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
h := sha256.New()
h.Write(body)
sha256sum := fmt.Sprintf("%x", h.Sum(nil))
filename := fmt.Sprintf("flux_%s_%s_%s.%s", p.Version, runtime.GOOS, runtime.GOARCH, extension)
if err := p.verifyChecksum(filename, sha256sum); err != nil {
return "", err
}
binary, err := extractTarGz(body)
if err != nil {
return "", err
}
binaryPath := filepath.Join(gitopsCacheFluxDir, "flux")
if err := os.WriteFile(binaryPath, binary, 0744); err != nil {
return "", err
}
return binaryPath, nil
}
func (p *Product) Remove(ctx context.Context) error {
dir, err := getFluxBinaryDir(p.Version)
if err != nil {
return err
}
return os.RemoveAll(dir)
}
func (p *Product) Find(ctx context.Context) (string, error) {
dir, err := getFluxBinaryDir(p.Version)
if err != nil {
return "", err
}
fluxPath := filepath.Join(dir, "flux")
if _, err := os.Stat(fluxPath); os.IsNotExist(err) {
return "", err
}
return fluxPath, nil
}
func (p *Product) verifyChecksum(filename string, sum string) error {
checkSumURL := fmt.Sprintf("https://github.com/fluxcd/flux2/releases/download/v%s/flux_%s_checksums.txt", p.Version, p.Version)
client := p.cli
resp, err := client.Get(checkSumURL)
if err != nil {
return err
}
// read string from response body
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
checksum := map[string]string{}
lines := strings.Split(string(body), "\n")
for _, line := range lines {
parts := strings.SplitN(line, " ", 2)
if len(parts) != 2 {
continue
}
checksum[parts[1]] = parts[0]
}
if checksum[filename] != sum {
return fmt.Errorf("checksum mismatch for %s", filename)
}
return nil
}
// extractTarGz accepts an io.Reader of Flux's .tar.gz and extract it to a byte array
func extractTarGz(b []byte) ([]byte, error) {
// ungzip
gr, err := gzip.NewReader(bytes.NewReader(b))
if err != nil {
return nil, err
}
defer gr.Close()
// untar the "flux" entry
tr := tar.NewReader(gr)
for {
hdr, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
if hdr.Typeflag != tar.TypeReg {
continue
}
if hdr.Name != "flux" {
continue
}
var bytesBuffer bytes.Buffer
if _, err := io.Copy(&bytesBuffer, tr); err != nil {
return nil, err
}
if err != nil {
return nil, err
}
return bytesBuffer.Bytes(), nil
}
return nil, fmt.Errorf("no flux binary found in archive")
}
func getFluxBinaryDir(version string) (string, error) {
// get cache dir
cacheDir, err := os.UserCacheDir()
if err != nil {
return "", err
}
gitopsCacheFluxDir := filepath.Join(cacheDir, ".gitops", "flux", version)
return gitopsCacheFluxDir, nil
}