This repository has been archived by the owner on Nov 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.go
161 lines (133 loc) · 3.46 KB
/
index.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
package localcache
import (
"crypto/sha1"
"fmt"
"io"
"os"
"path/filepath"
"strings"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
boshsys "github.com/cloudfoundry/bosh-utils/system"
"github.com/dpb587/boshua/analysis"
"github.com/dpb587/boshua/analysis/datastore"
"github.com/dpb587/boshua/releaseversion"
"github.com/dpb587/boshua/releaseversion/compilation"
"github.com/dpb587/boshua/stemcellversion"
"github.com/dpb587/metalink"
urldefaultloader "github.com/dpb587/metalink/file/url/defaultloader"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
type index struct {
name string
config Config
logger logrus.FieldLogger
storage string
}
var _ datastore.Index = &index{}
func New(name string, config Config, logger logrus.FieldLogger) datastore.Index {
return &index{
name: name,
config: config,
logger: logger,
storage: filepath.Join(os.Getenv("HOME"), ".cache", "boshua", "analysis-localcache"),
}
}
func (i *index) GetName() string {
return i.name
}
func (i *index) GetAnalysisArtifacts(ref analysis.Reference) ([]analysis.Artifact, error) {
cachePath, err := i.cachePath(ref)
if err != nil {
return nil, errors.Wrap(err, "generating cache path")
}
stat, err := os.Stat(cachePath)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, errors.Wrap(err, "checking analysis file")
}
return []analysis.Artifact{
analysis.New(
i.name,
ref,
metalink.File{
Name: fmt.Sprintf("%s.jsonl.gz", ref.Analyzer),
Size: uint64(stat.Size()),
URLs: []metalink.URL{
{
URL: fmt.Sprintf("file://%s", cachePath),
},
},
},
),
}, nil
}
func (i *index) StoreAnalysisResult(ref analysis.Reference, source metalink.Metalink) error {
cachePath, err := i.cachePath(ref)
if err != nil {
return errors.Wrap(err, "generating cache path")
}
err = os.MkdirAll(filepath.Dir(cachePath), 0750)
if err != nil {
return errors.Wrap(err, "creating directories")
}
fh, err := os.OpenFile(cachePath, os.O_CREATE|os.O_WRONLY, 0640)
if err != nil {
return errors.Wrap(err, "opening file for writing")
}
defer fh.Close()
logger := boshlog.NewLogger(boshlog.LevelError)
fs := boshsys.NewOsFileSystem(logger)
urlLoader := urldefaultloader.New(fs)
file := source.Files[0]
local, err := urlLoader.Load(metalink.URL{URL: file.URLs[0].URL})
if err != nil {
return errors.Wrap(err, "parsing origin destination")
}
reader, err := local.Reader()
if err != nil {
return errors.Wrap(err, "open reader")
}
_, err = io.Copy(fh, reader)
if err != nil {
return errors.Wrap(err, "saving analysis")
}
return nil
}
func (i *index) FlushAnalysisCache() error {
return nil
}
func (i *index) cachePath(ref analysis.Reference) (string, error) {
subjectRef := ref.Subject.Reference()
pieces := []string{string(ref.Analyzer)}
switch subjectRef := subjectRef.(type) {
case compilation.Reference:
pieces = append(
pieces,
"compiled-release",
subjectRef.OSVersion.Name,
subjectRef.OSVersion.Version,
subjectRef.ReleaseVersion.UniqueID(),
)
case releaseversion.Reference:
pieces = append(
pieces,
"release",
subjectRef.UniqueID(),
)
case stemcellversion.Reference:
pieces = append(
pieces,
"stemcell",
subjectRef.UniqueID(),
)
default:
return "", errors.New("unsupported analysis subject")
}
hash := sha1.New()
hash.Write([]byte(strings.Join(pieces, "\n")))
key := fmt.Sprintf("%x", hash.Sum(nil))
return filepath.Join(i.storage, key[0:2], key[2:]), nil
}