-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
184 lines (150 loc) · 4.29 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
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"io"
"os"
"sync"
"github.com/docker/distribution"
"github.com/docker/distribution/configuration"
"github.com/docker/distribution/context"
"github.com/docker/distribution/manifest/schema1"
"github.com/docker/distribution/registry/storage"
"github.com/docker/distribution/registry/storage/cache/memory"
"github.com/docker/distribution/registry/storage/driver/factory"
// fs drivers
_ "github.com/docker/distribution/registry/storage/driver/azure"
_ "github.com/docker/distribution/registry/storage/driver/filesystem"
_ "github.com/docker/distribution/registry/storage/driver/inmemory"
_ "github.com/docker/distribution/registry/storage/driver/middleware/cloudfront"
_ "github.com/docker/distribution/registry/storage/driver/oss"
_ "github.com/docker/distribution/registry/storage/driver/s3"
_ "github.com/docker/distribution/registry/storage/driver/swift"
)
const (
maxRepos = 500000
)
var (
manifestsList []*schema1.SignedManifest
manifestsLock sync.Mutex
)
func checkRepo(registry distribution.Namespace, repoName string) error {
ctx := context.Background()
repo, err := registry.Repository(ctx, repoName)
if err != nil {
return fmt.Errorf("unexpected error getting repository: %v", err)
}
manifests, err := repo.Manifests(ctx)
if err != nil {
return fmt.Errorf("unexpected error getting manifests: %v", err)
}
tags, err := manifests.Tags()
if err != nil {
return fmt.Errorf("unexpected error getting tags: %v", err)
}
fmt.Fprintf(os.Stderr, "checking repo %s (%d tags)\n", repoName)
for _, tag := range tags {
mnfst, err := manifests.GetByTag(tag)
if err != nil {
return fmt.Errorf("unexpected error getting manifest by tag: %v", err)
}
dir := "manifests/" + repoName
os.MkdirAll(dir, 0700)
mnfstFile, err := os.Create(dir + "/" + tag)
if err != nil {
return err
}
rjson, err := json.MarshalIndent(mnfst, "", " ")
if err != nil {
return err
}
mnfstFile.Write(rjson)
mnfstFile.Close()
}
return nil
}
func main() {
var configPath, reposPath string
flag.StringVar(&configPath, "config", "", "path to a config file")
flag.StringVar(&reposPath, "repos", "", "file with a list of repos")
flag.Parse()
if configPath == "" {
fmt.Fprintln(os.Stderr, "must supply a config file with -config")
flag.Usage()
return
}
// Parse config file
configFile, err := os.Open(configPath)
if err != nil {
panic(fmt.Sprintf("error opening config file: %v", err))
}
defer configFile.Close()
config, err := configuration.Parse(configFile)
if err != nil {
panic(fmt.Sprintf("error parsing config file: %v", err))
}
ctx := context.Background()
driver, err := factory.Create(config.Storage.Type(), config.Storage.Parameters())
if err != nil {
panic(fmt.Sprintf("error creating storage driver: %v", err))
}
registry, _ := storage.NewRegistry(ctx, driver, storage.BlobDescriptorCacheProvider(memory.NewInMemoryBlobDescriptorCacheProvider()))
var repos []string
if reposPath != "" {
reposFile, err := os.Open(reposPath)
if err != nil {
panic(fmt.Sprintf("could not open repos file: %v", err))
}
scanner := bufio.NewScanner(reposFile)
for scanner.Scan() {
repoName := scanner.Text()
if len(repoName) > 0 {
if repoName[0] == '+' {
repoName = repoName[1:]
}
repos = append(repos, repoName)
}
}
} else {
repos = make([]string, maxRepos)
n, err := registry.Repositories(ctx, repos, "")
if err != nil && err != io.EOF {
panic(fmt.Sprintf("unexpected error getting repos: %v", err))
}
if n == maxRepos {
panic("too many repositories")
}
repos = repos[:n]
fmt.Fprintln(os.Stderr, "finished listing")
reposFile, err := os.Create("reposlist")
if err != nil {
panic(fmt.Sprintf("could not open reposlist for writing: %v", err))
}
rjson, err := json.MarshalIndent(repos, "", " ")
if err != nil {
panic("could not marshal repos list")
}
reposFile.Write(rjson)
reposFile.Close()
}
var wg sync.WaitGroup
repoChan := make(chan string)
for i := 0; i < 30; i++ {
wg.Add(1)
go func() {
for repoName := range repoChan {
if err := checkRepo(registry, repoName); err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
wg.Done()
}()
}
for _, repoName := range repos {
repoChan <- repoName
}
close(repoChan)
wg.Wait()
}