-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wagon.go
261 lines (218 loc) · 7.14 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
/*
Copyright © 2022 Macaroni OS Linux
See AUTHORS and LICENSE for the license details and contributors.
*/
package repository
import (
"fmt"
"os"
"path/filepath"
"strconv"
"time"
artifact "github.com/geaaru/luet/pkg/compiler/types/artifact"
"github.com/geaaru/luet/pkg/config"
fileHelper "github.com/geaaru/luet/pkg/helpers/file"
"github.com/geaaru/luet/pkg/installer/client"
. "github.com/geaaru/luet/pkg/logger"
"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) (*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) {
// 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.Search(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.
//
// 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.
err = fileHelper.CopyFile(file, filepath.Join(repobasedir, REPOSITORY_SPECFILE))
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
} 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) Client() Client {
switch w.Identity.GetType() {
case DiskRepositoryType:
return client.NewLocalClient(client.RepoData{Urls: w.Identity.GetUrls()})
case HttpRepositoryType:
return client.NewHttpClient(
client.RepoData{
Urls: w.Identity.GetUrls(),
Authentication: w.Identity.GetAuthentication(),
})
case DockerRepositoryType:
return client.NewDockerClient(
client.RepoData{
Urls: w.Identity.GetUrls(),
Authentication: w.Identity.GetAuthentication(),
Verify: w.Identity.Verify,
})
}
return nil
}