forked from kata-containers/kata-containers
/
main.go
295 lines (250 loc) · 7.2 KB
/
main.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
// Copyright (c) 2019 SUSE LLC
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
"flag"
"fmt"
"os"
"strings"
"github.com/marcov/obsgo"
"github.com/mlafeldt/pkgcloud"
"github.com/sirupsen/logrus"
pb "gopkg.in/cheggaaa/pb.v1"
)
//nolint[:gochecknoglobals]
var (
// empty variables are set by "go build -ldflags" option
name = ""
version = ""
commit = ""
defaultConfig = "config.yaml"
defaultOBSDest = "obs-packages"
)
func usage() {
fmt.Fprintf(flag.CommandLine.Output(), `NAME:
%s - Synchronize packages from OBS to Packagecloud
USAGE:
%s [options] [config file]
By default %s reads the configuration from a file named %s. This can be
overridden by passing the path to a config file.
Options:
`, name, name, name, defaultConfig)
flag.PrintDefaults()
}
func getOBSProjects(cfgProjects map[string]CfgOBSProject) []obsgo.Project {
var projects []obsgo.Project
for n, p := range cfgProjects {
proj := obsgo.Project{
User: p.Auth.User,
Password: p.Auth.Password,
}
if len(p.Archs) == 0 {
p.Archs = append(p.Archs, "")
}
if len(p.Releases) == 0 {
p.Releases = append(p.Releases, "")
}
// Kata projects names format is "project:release:architecture"
for _, arch := range p.Archs {
for _, release := range p.Releases {
var fullname strings.Builder
fullname.WriteString(n)
if arch != "" {
fmt.Fprintf(&fullname, ":%s", arch)
}
if release != "" {
fmt.Fprintf(&fullname, ":%s", release)
}
proj.Name = fullname.String()
projects = append(projects, proj)
}
}
}
return projects
}
func getXferBinaries(pkg obsgo.PackageInfo, pcDistro string,
pcPackages []pkgcloud.Package, pcPackagesNeeded *[]bool) []obsgo.PkgBinary {
var xferBins []obsgo.PkgBinary
for _, src := range pkg.Files {
found := false
for i, dst := range pcPackages {
logrus.WithFields(logrus.Fields{
"source-file": src.Filename,
"source-distro": pcDistro,
"destination-file": dst.Filename,
"destination-distro": dst.DistroVersion,
}).Debug("Checking package")
if pcDistro == dst.DistroVersion && src.Filename == dst.Filename {
logrus.WithFields(logrus.Fields{
"filename": src.Filename,
"distro": pcDistro,
}).Debug("Package file already on Packagecloud")
found = true
(*pcPackagesNeeded)[i] = true
break
}
}
if !found {
logrus.WithFields(logrus.Fields{
"filename": src.Filename,
"distro": pcDistro,
}).Debug("Package file NOT on Packagecloud")
xferBins = append(xferBins, src)
}
}
return xferBins
}
func main() {
flag.Usage = usage
verbose := flag.Bool("debug", false, "debug mode")
dlPath := flag.String("dir", defaultOBSDest, "Destination directory of packages download from OBS")
dryRun := flag.Bool("dry-run", false, "dry-run mode (do not download/upload files)")
pcDelete := flag.Bool("delete", false, "Delete Packagecloud packages that are not published on OBS")
showVersion := flag.Bool("version", false, "show the version")
flag.Parse()
if *showVersion {
fmt.Printf("%s %s (commit %v)\n", name, version, commit)
os.Exit(0)
}
if *verbose {
fmt.Println("Starting in debug mode...")
logrus.SetLevel(logrus.DebugLevel)
}
var configFile string
if len(flag.Args()) > 0 {
configFile = flag.Args()[0]
} else {
configFile = defaultConfig
}
logrus.Debugf("Using config file %s", configFile)
cfg, err := getConfig(configFile)
if err != nil {
logrus.WithError(err).Error("Failed to read config")
os.Exit(-1)
}
logrus.Debugf("Configuration file content: %+v", cfg)
var pc PCClient
if err := pc.PackagecloudClient(
cfg.Packagecloud.Auth.User,
cfg.Packagecloud.Auth.Token,
cfg.Packagecloud.Repo); err != nil {
logrus.WithError(err).Error("Failed to create a Packagecloud client instance")
os.Exit(-1)
}
logrus.WithFields(logrus.Fields{
"repo": pc.Repo,
}).Info("Retrieving Packagecloud list of files")
pcPackages, err := pc.PackagecloudList()
if err != nil {
logrus.WithFields(logrus.Fields{
"repo": pc.Repo,
"error": err,
}).Error("Failed to retrieve Packagecloud list of files")
os.Exit(-1)
}
// lookup table for pcPackages packages that should NOT be deleted from Packagecloud
pcPackagesNeeded := make([]bool, len(pcPackages))
projects := getOBSProjects(cfg.OBSProjects)
for _, proj := range projects {
logrus.WithFields(logrus.Fields{
"OBS project": proj.Name,
}).Info("Retrieving packages info")
obsPackages, err := proj.FindAllPackages()
if err != nil {
logrus.WithError(err).WithFields(logrus.Fields{
"project": proj.Name,
}).Error("Failed to get OBS packages")
os.Exit(-1)
}
logrus.WithFields(logrus.Fields{
"OBS project": proj.Name,
}).Infof("Found %d packages", len(obsPackages))
totalXferred := 0
for _, pkg := range obsPackages {
pcDistro, found := cfg.DistroMapping[pkg.Repo]
if !found {
logrus.WithFields(logrus.Fields{
"OBS Repo": pkg.Repo,
}).Warn("No mapped Packagecloud distro specified")
os.Exit(-1)
}
if pcDistro == "" {
logrus.WithFields(logrus.Fields{
"package": pkg.Name,
"OBS Repo": pkg.Repo,
}).Warn("Repo not supported by Packagecloud")
continue
}
xferBins := getXferBinaries(pkg, pcDistro, pcPackages, &pcPackagesNeeded)
if len(xferBins) == 0 {
logrus.WithFields(logrus.Fields{
"package": pkg.Name,
"OBS Repo": pkg.Repo,
}).Infof("All %d files already on Packagecloud", len(pkg.Files))
continue
}
logrus.WithFields(logrus.Fields{
"pkg": pkg.Name,
"repo": pkg.Repo,
"# files": len(xferBins),
}).Info("Downloading from OBS")
if *dryRun {
continue
}
pkg.Files = xferBins
paths, err := proj.DownloadPackageFiles(pkg, *dlPath)
if err != nil {
logrus.WithError(err).Warnf("Failed to download binaries for %s on %s/%s", pkg.Name, pc.Repo, pcDistro)
continue
}
logrus.WithFields(logrus.Fields{
"pkg": pkg.Name,
"distro": pcDistro,
"# files": len(xferBins),
}).Info("Uploading to Packagecloud")
err = pc.PackagecloudPush(paths, pcDistro)
if err != nil {
logrus.WithError(err).WithFields(logrus.Fields{
"package": pkg.Name,
"distro": pcDistro,
}).Error("Failed to push binaries to Packagecloud")
}
totalXferred += len(xferBins)
}
logrus.WithFields(logrus.Fields{
"OBS project": proj.Name,
"Packagecloud Repo": pc.Repo,
}).Infof("Successfully transferred %d files", totalXferred)
}
if !*pcDelete {
return
}
logrus.WithFields(logrus.Fields{
"Repo": pc.Repo,
}).Info("Finding and deleting Packagecloud files not published on OBS")
progressBar := pb.New(len(pcPackages))
progressBar.SetMaxWidth(100)
progressBar.Start()
totalDeleted := 0
for i, pkg := range pcPackages {
if !pcPackagesNeeded[i] {
if *dryRun {
continue
}
if err := pc.PackagecloudDelete(pkg.Filename, pkg.DistroVersion); err != nil {
logrus.WithError(err).WithFields(logrus.Fields{
"name": pkg.Filename,
"distro": pkg.DistroVersion,
}).Error("Failed to delete package on Packagecloud")
}
totalDeleted++
}
progressBar.Increment()
}
progressBar.Finish()
logrus.WithFields(logrus.Fields{
"Packagecloud Repo": pc.Repo,
}).Infof("Deleted %d files", totalDeleted)
}