forked from DataDog/nikos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cos.go
52 lines (43 loc) · 1.23 KB
/
cos.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
package cos
import (
"context"
"errors"
"fmt"
"cloud.google.com/go/storage"
"github.com/jeffweiss/nikos/extract"
"github.com/jeffweiss/nikos/types"
"google.golang.org/api/option"
)
type Backend struct {
buildID string
logger types.Logger
client *storage.Client
}
func (b *Backend) GetKernelHeaders(directory string) error {
filename := "kernel-headers.tgz"
bucketHandle := b.client.Bucket("cos-tools")
objectHandle := bucketHandle.Object(b.buildID + "/" + filename)
reader, err := objectHandle.NewReader(context.Background())
if err != nil {
return fmt.Errorf("failed to download bucket object: %w", err)
}
defer reader.Close()
extract.ExtractTarball(reader, filename, directory, b.logger)
return err
}
func (b *Backend) Close() {}
func NewBackend(target *types.Target, logger types.Logger) (*Backend, error) {
buildID := target.OSRelease["BUILD_ID"]
if buildID == "" {
return nil, errors.New("failed to detect COS version, missing BUILD_ID in /etc/os-release")
}
client, err := storage.NewClient(context.Background(), option.WithoutAuthentication())
if err != nil {
return nil, fmt.Errorf("failed to creating COS backend: %w", err)
}
return &Backend{
client: client,
logger: logger,
buildID: buildID,
}, nil
}