-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wagon.go
357 lines (297 loc) · 10 KB
/
wagon.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
/*
Copyright © 2022 Macaroni OS Linux
See AUTHORS and LICENSE for the license details and contributors.
*/
package repository
import (
"fmt"
"os"
"path/filepath"
"strconv"
"time"
"github.com/geaaru/luet/pkg/config"
fileHelper "github.com/geaaru/luet/pkg/helpers/file"
. "github.com/geaaru/luet/pkg/logger"
artifact "github.com/geaaru/luet/pkg/v2/compiler/types/artifact"
"github.com/geaaru/luet/pkg/v2/repository/client"
"github.com/pkg/errors"
)
const (
REPOSITORY_METAFILE = "repository.meta.yaml"
REPOSITORY_SPECFILE = "repository.yaml"
TREE_TARBALL = "tree.tar"
COMPILERTREE_TARBALL = "compilertree.tar"
REPOFILE_TREE_KEY = "tree"
REPOFILE_COMPILER_TREE_KEY = "compilertree"
REPOFILE_META_KEY = "meta"
// TODO: To move on a specific package
DiskRepositoryType = "disk"
HttpRepositoryType = "http"
DockerRepositoryType = "docker"
)
type Client interface {
DownloadArtifact(*artifact.PackageArtifact) error
DownloadFile(string) (string, error)
}
type WagonRepository struct {
Identity *WagonIdentity
Stones *WagonStones
}
func NewWagonRepository(l *config.LuetRepository) *WagonRepository {
return &WagonRepository{
Identity: NewWagonIdentify(l),
Stones: NewWagonStones(),
}
}
func (w *WagonRepository) SearchStones(opts *StonesSearchOpts) (*[]*Stone, error) {
repobasedir := config.LuetCfg.GetSystem().GetRepoDatabaseDirPath(w.Identity.Name)
return w.Stones.Search(opts, w.Identity.Name, repobasedir)
}
func (w *WagonRepository) SearchStonesFromCatalog(opts *StonesSearchOpts) (*[]*Stone, error) {
// Load catalog if not loaded yet
if w.Stones.Catalog == nil {
_, err := w.Stones.LoadCatalog(w.Identity)
if err != nil {
return nil, err
}
}
return w.Stones.SearchFromCatalog(opts, w.Identity.Name)
}
func (w *WagonRepository) SearchArtifacts(opts *StonesSearchOpts) (*[]*artifact.PackageArtifact, error) {
repobasedir := config.LuetCfg.GetSystem().GetRepoDatabaseDirPath(w.Identity.Name)
return w.Stones.SearchArtifacts(opts, w.Identity.Name, repobasedir)
}
func (w *WagonRepository) SearchArtifactsFromCatalog(opts *StonesSearchOpts) (*[]*artifact.PackageArtifact, error) {
return w.Stones.SearchArtifactsFromCatalog(opts, w.Identity.Name)
}
func (w *WagonRepository) HasLocalWagonIdentity(wdir string) bool {
file := filepath.Join(wdir, REPOSITORY_SPECFILE)
if fileHelper.Exists(file) {
return true
}
return false
}
func (w *WagonRepository) ReadWagonIdentify(wdir string) error {
file := filepath.Join(wdir, REPOSITORY_SPECFILE)
repoName := w.Identity.Name
repoUrls := w.Identity.Urls
repoPriority := w.Identity.Priority
repoAuthentication := w.Identity.Authentication
err := w.Identity.Load(file)
if err != nil {
return err
}
// Ensure that we use urls from config
w.Identity.Name = repoName
w.Identity.Urls = repoUrls
w.Identity.Priority = repoPriority
w.Identity.Authentication = repoAuthentication
return nil
}
func (w *WagonRepository) GetRevision() int {
return w.Identity.LuetRepository.Revision
}
func (w *WagonRepository) GetLastUpdate() string {
return w.Identity.LuetRepository.LastUpdate
}
func (w *WagonRepository) SetLastUpdate(u string) {
w.Identity.LuetRepository.LastUpdate = u
}
func (w *WagonRepository) IncrementRevision() {
w.Identity.LuetRepository.Revision++
}
func (w *WagonRepository) ClearCatalog() {
w.Stones.Catalog = nil
}
// The Sync method update the repository.
//
// In particular, follow these steps:
// * download the main repository.yaml file to a temporary directory
// * load the new repository.yaml as WagonIdentity and compare revision
// and last update date with the current status.
// * if there is a new revision download the meta and tree file
// and unpack them to the local cache.
//
// NOTE:
// Until I will implement the new metadata tarball
// i need process metadata and write under the treefs
// the artifacts information.
//
// If force is true the download of the meta and tree files are done always.
func (w *WagonRepository) Sync(force bool) error {
var treefs, metafs string
aurora := GetAurora()
Debug("Sync of the repository", w.Identity.Name, "in progress...")
c := w.Client()
if c == nil {
return errors.New("no client could be generated from repository")
}
// Retrieve remote repository.yaml for retrieve revision and date
file, err := c.DownloadFile(REPOSITORY_SPECFILE)
if err != nil {
return errors.Wrap(err, "While downloading "+REPOSITORY_SPECFILE)
}
repobasedir := config.LuetCfg.GetSystem().GetRepoDatabaseDirPath(w.Identity.Name)
newIdentity := NewWagonIdentify(w.Identity.LuetRepository.Clone())
err = newIdentity.Load(file)
if err != nil {
return err
}
if !newIdentity.Valid() {
return errors.New("Corrupted remote repository.yaml file")
}
// Remove temporary file that contains repository.yaml
// Example: /tmp/HttpClient236052003
defer os.RemoveAll(file)
toUpdate := w.Identity.Is2Update(newIdentity)
if w.Identity.GetTreePath() == "" {
treefs = filepath.Join(repobasedir, "treefs")
} else {
treefs = w.Identity.GetTreePath()
}
if w.Identity.GetMetaPath() == "" {
metafs = filepath.Join(repobasedir, "metafs")
} else {
metafs = w.Identity.GetMetaPath()
}
newIdentity.LuetRepository.MetaPath = metafs
newIdentity.LuetRepository.TreePath = treefs
// treeFile and metaFile must be present, they aren't optional
if toUpdate || force {
treeFileArtifact, err := newIdentity.DownloadDocument(c, REPOFILE_TREE_KEY)
if err != nil {
return errors.Wrapf(err, "while fetching '%s'", REPOFILE_TREE_KEY)
}
defer os.Remove(treeFileArtifact.Path)
Debug("Tree tarball for the repository " + w.Identity.GetName() + " downloaded correctly.")
metaFileArtifact, err := newIdentity.DownloadDocument(c, REPOFILE_META_KEY)
if err != nil {
return errors.Wrapf(err, "while fetching '%s'", REPOFILE_META_KEY)
}
defer os.Remove(metaFileArtifact.Path)
Debug("Metadata tarball for the repository " + w.Identity.GetName() + " downloaded correctly.")
// Copy updated repository.yaml file to repo dir now that the tree is synced.
newIdentity.IdentityFile = filepath.Join(repobasedir, REPOSITORY_SPECFILE)
err = fileHelper.CopyFile(file, newIdentity.IdentityFile)
if err != nil {
return errors.Wrap(err, "Error on update "+REPOSITORY_SPECFILE)
}
// Remove previous tree
os.RemoveAll(treefs)
// Remove previous meta dir
os.RemoveAll(metafs)
Debug("Decompress tree of the repository " + w.Identity.GetName() + "...")
err = treeFileArtifact.Unpack(treefs, false)
if err != nil {
return errors.Wrap(err, "Error met while unpacking tree")
}
// FIXME: It seems that tar with only one file doesn't create destination
// directory. I create directory directly for now.
os.MkdirAll(metafs, os.ModePerm)
err = metaFileArtifact.Unpack(metafs, false)
if err != nil {
return errors.Wrap(err, "Error met while unpacking metadata")
}
tsec, _ := strconv.ParseInt(newIdentity.GetLastUpdate(), 10, 64)
InfoC(
aurora.Bold(
aurora.Red(fmt.Sprintf(
":house:Repository: %30s Revision: ",
w.Identity.GetName()))).String() +
aurora.Bold(aurora.Green(fmt.Sprintf("%3d", newIdentity.GetRevision()))).String() + " - " +
aurora.Bold(aurora.Green(time.Unix(tsec, 0).String())).String(),
)
w.Identity = newIdentity
// Build metadata for package. This will be handled from a new tarball
// in the near future. In particolar, i will write a metadata.yaml file
// under <cache_dir>/repos/treefs/<cat>/<name>/<version>/metadata.yaml
// to avoid the parsing of metadata stream every time. It's something
// that consume too memory when i have more of >2k packages on a repo.
err = w.ExplodeMetadata()
if err != nil {
return err
}
w.ClearCatalog()
} else {
InfoC(
aurora.Magenta(":information_source: Repository: ").String() +
aurora.Bold(
aurora.Green(fmt.Sprintf("%30s", w.Identity.GetName())).String()+
" is already up to date.",
).String(),
)
}
return nil
}
func (w *WagonRepository) ExplodeMetadata() error {
w.ClearCatalog()
Debug(
fmt.Sprintf(
"\n:house:Repository: %30s unpacking metadata. ",
w.Identity.GetName()),
)
_, err := w.Stones.LoadCatalog(w.Identity)
if err != nil {
return err
}
catalog := *w.Stones.Catalog
for idx, _ := range catalog.Index {
pkg := catalog.Index[idx].GetPackage()
if pkg == nil {
return errors.New(
fmt.Sprintf("Unexpected status on parse stone at pos %d", idx))
}
metaFile := filepath.Join(w.Identity.LuetRepository.TreePath,
pkg.Category,
pkg.Name,
pkg.Version,
"metadata.yaml",
)
pkgDir := filepath.Dir(metaFile)
// TODO: On ArtifactIndex provides are insert in the index!
// For now just ignoring the artifacts if the directory is not
// present.
if fileHelper.Exists(pkgDir) {
//Debug(fmt.Sprintf("Creating file %s", metaFile))
err = catalog.Index[idx].WriteMetadataYaml(metaFile)
if err != nil {
Warning(fmt.Sprintf(
"[%s] Error on creating metadata file for package %s: %s",
w.Identity.Name,
pkg.HumanReadableString(),
err.Error()),
)
}
}
}
return nil
}
func (w *WagonRepository) GetTreePath(repobasedir string) string {
if w.Identity.GetTreePath() == "" {
// NOTE: repobasedir must be the value of
// LuetCfg.GetSystem().GetSystemReposDirPath
repobase := filepath.Join(repobasedir, w.Identity.GetName())
return filepath.Join(repobase, "treefs")
}
return w.Identity.GetTreePath()
}
func (w *WagonRepository) GetMetaPath(repobasedir string) string {
if w.Identity.GetTreePath() == "" {
// NOTE: repobasedir must be the value of
// LuetCfg.GetSystem().GetSystemReposDirPath
repobase := filepath.Join(repobasedir, w.Identity.GetName())
return filepath.Join(repobase, "metafs")
}
return w.Identity.GetTreePath()
}
func (w *WagonRepository) Client() Client {
switch w.Identity.GetType() {
case DiskRepositoryType:
return client.NewLocalClient(w.Identity.LuetRepository)
case HttpRepositoryType:
return client.NewHttpClient(w.Identity.LuetRepository)
case DockerRepositoryType:
return client.NewDockerClient(w.Identity.LuetRepository)
}
return nil
}