-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathtools.go
386 lines (336 loc) · 10.2 KB
/
tools.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
// Copyright 2022 Arduino SA
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package pkgs
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/arduino/arduino-create-agent/gen/tools"
"github.com/arduino/arduino-create-agent/index"
"github.com/arduino/arduino-create-agent/utilities"
"github.com/blang/semver"
"github.com/codeclysm/extract/v3"
)
// public vars to allow override in the tests
var (
OS = runtime.GOOS
Arch = runtime.GOARCH
)
// Tools is a client that implements github.com/arduino/arduino-create-agent/gen/tools.Service interface.
// It saves tools in a specified folder with this structure: packager/name/version
// For example:
//
// folder
// └── arduino
// └── bossac
// ├── 1.6.1-arduino
// │ └── bossac
// └── 1.7.0
// └── bossac
//
// It requires an Index Resource to search for tools
type Tools struct {
index *index.Resource
folder string
behaviour string
}
// New will return a Tool object, allowing the caller to execute operations on it.
// The New function will accept an index as parameter (used to download the indexes)
// and a folder used to download the indexes
func New(index *index.Resource, folder, behaviour string) *Tools {
return &Tools{
index: index,
folder: folder,
behaviour: behaviour,
}
}
// Installedhead is here only because it was required by the front-end.
// Probably when we bumped GOA something changed:
// Before that the frontend was able to perform the HEAD request to `v2/pkgs/tools/installed`.
// After the bump we have to implement it explicitly. Currently I do not know a better way in achieving the same result.
func (t *Tools) Installedhead(ctx context.Context) (err error) {
return nil
}
// Available crawles the downloaded package index files and returns a list of tools that can be installed.
func (t *Tools) Available(ctx context.Context) (res tools.ToolCollection, err error) {
body, err := t.index.Read()
if err != nil {
return nil, err
}
var index Index
json.Unmarshal(body, &index)
for _, packager := range index.Packages {
for _, tool := range packager.Tools {
res = append(res, &tools.Tool{
Packager: packager.Name,
Name: tool.Name,
Version: tool.Version,
})
}
}
return res, nil
}
// Installed crawles the Tools Folder and finds the installed tools.
func (t *Tools) Installed(ctx context.Context) (tools.ToolCollection, error) {
res := tools.ToolCollection{}
// Find packagers
packagers, err := os.ReadDir(t.folder)
if err != nil {
if !strings.Contains(err.Error(), "no such file") {
return nil, err
}
err = os.MkdirAll(t.folder, 0755)
if err != nil {
return nil, err
}
}
for _, packager := range packagers {
if !packager.IsDir() {
continue
}
// Find tools
toolss, err := os.ReadDir(filepath.Join(t.folder, packager.Name()))
if err != nil {
return nil, err
}
for _, tool := range toolss {
// Find versions
path := filepath.Join(t.folder, packager.Name(), tool.Name())
versions, err := os.ReadDir(path)
if err != nil {
continue // we ignore errors because the folders could be dirty
}
for _, version := range versions {
res = append(res, &tools.Tool{
Packager: packager.Name(),
Name: tool.Name(),
Version: version.Name(),
})
}
}
}
return res, nil
}
// Install crawles the Index folder, downloads the specified tool, extracts the archive in the Tools Folder.
// It checks for the Signature specified in the package index.
func (t *Tools) Install(ctx context.Context, payload *tools.ToolPayload) (*tools.Operation, error) {
path := filepath.Join(payload.Packager, payload.Name, payload.Version)
//if URL is defined and is signed we verify the signature and override the name, payload, version parameters
if payload.URL != nil && payload.Signature != nil && payload.Checksum != nil {
err := utilities.VerifyInput(*payload.URL, *payload.Signature)
if err != nil {
return nil, err
}
return t.install(ctx, path, *payload.URL, *payload.Checksum)
}
// otherwise we install from the default index
body, err := t.index.Read()
if err != nil {
return nil, err
}
var index Index
json.Unmarshal(body, &index)
correctTool, correctSystem, found := FindTool(payload.Packager, payload.Name, payload.Version, index)
path = filepath.Join(payload.Packager, correctTool.Name, correctTool.Version)
key := correctTool.Name + "-" + correctTool.Version
// Check if it already exists
if t.behaviour == "keep" && pathExists(t.folder) {
location, ok, err := checkInstalled(t.folder, key)
if err != nil {
return nil, err
}
if ok && pathExists(location) {
// overwrite the default tool with this one
err := writeInstalled(t.folder, path)
if err != nil {
return nil, err
}
return &tools.Operation{Status: "ok"}, nil
}
}
if found {
return t.install(ctx, path, correctSystem.URL, correctSystem.Checksum)
}
return nil, tools.MakeNotFound(
fmt.Errorf("tool not found with packager '%s', name '%s', version '%s'",
payload.Packager, payload.Name, payload.Version))
}
func (t *Tools) install(ctx context.Context, path, url, checksum string) (*tools.Operation, error) {
// Download the archive
res, err := http.Get(url)
if err != nil {
return nil, err
}
defer res.Body.Close()
var buffer bytes.Buffer
// We copy the body of the response to a buffer to calculate the checksum
_, err = io.Copy(&buffer, res.Body)
if err != nil {
return nil, err
}
// Check the checksum
sum := sha256.Sum256(buffer.Bytes())
sumString := "SHA-256:" + hex.EncodeToString(sum[:sha256.Size])
if sumString != checksum {
return nil, errors.New("checksum of downloaded file doesn't match, expected: " + checksum + " got: " + sumString)
}
safePath, err := utilities.SafeJoin(t.folder, path)
if err != nil {
return nil, err
}
// Cleanup
err = os.RemoveAll(safePath)
if err != nil {
return nil, err
}
err = extract.Archive(ctx, &buffer, t.folder, rename(path))
if err != nil {
os.RemoveAll(safePath)
return nil, err
}
// Write installed.json for retrocompatibility with v1
err = writeInstalled(t.folder, path)
if err != nil {
return nil, err
}
return &tools.Operation{Status: "ok"}, nil
}
// Remove deletes the tool folder from Tools Folder
func (t *Tools) Remove(ctx context.Context, payload *tools.ToolPayload) (*tools.Operation, error) {
path := filepath.Join(payload.Packager, payload.Name, payload.Version)
pathToRemove, err := utilities.SafeJoin(t.folder, path)
if err != nil {
return nil, err
}
err = os.RemoveAll(pathToRemove)
if err != nil {
return nil, err
}
return &tools.Operation{Status: "ok"}, nil
}
// rename function is used to rename the path of the extracted files
func rename(base string) extract.Renamer {
// "Rename" the given path adding the "base" and removing the root folder in "path" (if present).
return func(path string) string {
parts := strings.Split(filepath.ToSlash(path), "/")
if len(parts) <= 1 {
// The path does not contain a root folder. This might happen for tool packages (zip files)
// that have an invalid structure. Do not try to remove the root folder in these cases.
return filepath.Join(base, path)
}
// Removes the first part of the path (the root folder).
path = strings.Join(parts[1:], "/")
return filepath.Join(base, path)
}
}
func readInstalled(installedFile string) (map[string]string, error) {
// read installed.json
installed := map[string]string{}
data, err := os.ReadFile(installedFile)
if err == nil {
err = json.Unmarshal(data, &installed)
if err != nil {
return nil, err
}
}
return installed, nil
}
func checkInstalled(folder, key string) (string, bool, error) {
installedFile, err := utilities.SafeJoin(folder, "installed.json")
if err != nil {
return "", false, err
}
installed, err := readInstalled(installedFile)
if err != nil {
return "", false, err
}
location, ok := installed[key]
return location, ok, err
}
func writeInstalled(folder, path string) error {
// read installed.json
installedFile, err := utilities.SafeJoin(folder, "installed.json")
if err != nil {
return err
}
installed, err := readInstalled(installedFile)
if err != nil {
return err
}
parts := strings.Split(path, string(filepath.Separator))
tool := parts[len(parts)-2]
toolWithVersion := fmt.Sprint(tool, "-", parts[len(parts)-1])
toolFile, err := utilities.SafeJoin(folder, path)
if err != nil {
return err
}
installed[tool] = toolFile
installed[toolWithVersion] = toolFile
data, err := json.Marshal(installed)
if err != nil {
return err
}
return os.WriteFile(installedFile, data, 0644)
}
func pathExists(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
}
if os.IsNotExist(err) {
return false
}
return true
}
// FindTool searches the index for the correct tool and system that match the specified tool name and version
func FindTool(pack, name, version string, data Index) (Tool, System, bool) {
var correctTool Tool
correctTool.Version = "0.0"
found := false
for _, p := range data.Packages {
if p.Name != pack {
continue
}
for _, t := range p.Tools {
if version != "latest" {
if t.Name == name && t.Version == version {
correctTool = t
found = true
}
} else {
// Find latest
v1, _ := semver.Make(t.Version)
v2, _ := semver.Make(correctTool.Version)
if t.Name == name && v1.Compare(v2) > 0 {
correctTool = t
found = true
}
}
}
}
// Find the url based on system
correctSystem := correctTool.GetFlavourCompatibleWith(OS, Arch)
return correctTool, correctSystem, found
}