-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
321 lines (268 loc) · 7.81 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
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
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"strconv"
"strings"
"text/template"
"time"
)
// structs used to unmarshall configuration file
type ConfigFile struct {
Src Config
Dest Config
}
type Config struct {
Host string
User string
Pass string
Transport string
Ssl bool
Api string
}
// registry v2/_catalog api call response
type RegistryCatalog struct {
Repositories []string `json:"repositories"`
}
var (
srcConfig Config
destConfig Config
skopeoSyncCmd string
)
// registry.conf text templates
var (
insecureRegistryTemplate1 = "[registries.insecure]\nregistries = ['{{ .Host }}']\n"
insecureRegistryTemplate2 = "[registries.insecure]\nregistries = ['{{ .Src.Host }}', '{{ .Dest.Host }}']\n"
)
func main() {
err := readConfig()
if err != nil {
log.Fatalf("ERROR: Unable to read Config %v", err)
}
err = prepareSkopeoSyncCmd()
if err != nil {
log.Fatalf("ERROR: Unable to prepare skopeo sync command %v", err)
}
interv, exists := os.LookupEnv("INTERVAL")
if !exists {
interv = "86400"
}
interval, err := strconv.Atoi(interv)
if err != nil {
log.Fatalf("ERROR: Unable to convert interval %v to int %v", interv, err)
}
// infinite loop
// - Get repository list from src registry (v2/_catalog)
// - call syncRepo() for every registry
// - sleep for INTERVAL seconds
for {
repos, err := getRepoList(&srcConfig)
log.Printf("Following repositories will be synced:\n%v", repos)
if err != nil {
log.Fatalf("ERROR: Unable to get source repo list %v", err)
}
for _, r := range repos {
log.Printf("Syncing repository %s", r)
err = syncRepo(r)
if err != nil {
log.Printf("ERROR: unable to sync repo %s %v\n", r, err)
}
}
log.Printf("Finished, sleeping for %d seconds.", interval)
time.Sleep(time.Duration(interval) * time.Second)
}
}
func getRepoList(regConfig *Config) ([]string, error) {
// use http if ssl is set to false in config, otherwise use https
proto := "https"
if !regConfig.Ssl {
proto = "http"
}
// use host from the config for the v2/_catalog api call
// if api is set in config, use that one instead
url := fmt.Sprintf("%s://%s/v2/_catalog", proto, regConfig.Host)
if regConfig.Api != "" {
url = fmt.Sprintf("%s://%s/v2/_catalog", proto, regConfig.Api)
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, errors.New(fmt.Sprintf("Error creating %s request: %v", proto, err))
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, errors.New(fmt.Sprintf("Error executing %s request: %v", proto, err))
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, errors.New(fmt.Sprintf("HTTP response status: %s", resp.Status))
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.New(fmt.Sprintf("Error reading response body: %v", err))
}
var temp RegistryCatalog
err = json.Unmarshal(body, &temp)
if err != nil {
return nil, errors.New(fmt.Sprintf("Error in json unmarshal: %v", err))
}
return temp.Repositories, nil
}
func syncRepo(repo string) error {
// To keep the same repository names (folders) its necessary to set the whole repository
// path, except the image name in the "skopeo sync" destination registry/repo argument
split := strings.Split(repo, "/")
var destRepo string
if len(split) == 1 {
destRepo = repo
} else {
destRepo = strings.Join(split[:len(split)-1], "/")
}
cmd := fmt.Sprintf("%s %s/%s %s/%s", skopeoSyncCmd, srcConfig.Host, repo, destConfig.Host, destRepo)
err := execCmdSh(cmd)
if err != nil {
return err
}
return nil
}
func prepareSkopeoSyncCmd() error {
cmd := "skopeo sync"
// if any of the two registries has "ssl: false" in config
// add the registry.conf as parameter to "skopeo sync"
// registry.conf is created by createSkopeoConfig{1,2}()
// and it holds the insecure registries list
if !(srcConfig.Ssl && destConfig.Ssl) {
if _, err := os.Stat("registry.conf"); err != nil {
return errors.New(fmt.Sprintf("File registry.conf not present"))
}
cmd = fmt.Sprintf("%s --registries-conf=registry.conf", cmd)
}
// set credential arguments if not empty
var cred string
if srcConfig.User != "" {
cred = fmt.Sprintf("--src-creds %s", srcConfig.User)
if srcConfig.Pass != "" {
cred = fmt.Sprintf("%s:%s", cred, srcConfig.Pass)
}
}
if destConfig.User != "" {
cred = fmt.Sprintf("%s --dest-creds %s", cred, destConfig.User)
if destConfig.Pass != "" {
cred = fmt.Sprintf("%s:%s", cred, destConfig.Pass)
}
}
cmd = fmt.Sprintf("%s %s", cmd, cred)
// TODO: Add certs path flag (currently I attach the ca rootchain in the container)
if !srcConfig.Ssl {
cmd = fmt.Sprintf("%s --src-tls-verify=false", cmd)
}
if !destConfig.Ssl {
cmd = fmt.Sprintf("%s --dest-tls-verify=false", cmd)
}
cmd = fmt.Sprintf("%s --src %v --dest %v", cmd, srcConfig.Transport, destConfig.Transport)
skopeoSyncCmd = cmd
return nil
}
func execCmdSh(cmdstr string) error {
// execute cmdstr with exec.Command().Run()
// print Stdout and if error return stderr
c := exec.Command("sh", "-c", cmdstr)
c.Stdin = strings.NewReader("")
var out, outerr bytes.Buffer
c.Stdout = &out
c.Stderr = &outerr
err := c.Run()
if err != nil {
return errors.New(fmt.Sprintf("Error while executing command:\n'%s'\nError: %v\n%s", cmdstr, err, outerr.String()))
}
log.Println(out.String())
return nil
}
func readConfig() error {
// config.yml is mounted to /config.yml in docker-compose file.
// Location can be changed by setting the CONFIG env var,
// docker-compose file should be updated in that case.
configFile, exists := os.LookupEnv("CONFIG")
if !exists {
configFile = "./config.yml"
}
data, err := ioutil.ReadFile(configFile)
if err != nil {
return errors.New(fmt.Sprintf("Error reading file %s: %v", configFile, err))
}
var tmpc ConfigFile
err = yaml.Unmarshal(data, &tmpc)
if err != nil {
return fmt.Errorf("error parsing config file '%s': %v", configFile, err)
}
srcConfig = tmpc.Src
destConfig = tmpc.Dest
// if transport is not defined, use docker as default
if srcConfig.Transport == "" {
srcConfig.Transport = "docker"
}
if destConfig.Transport == "" {
destConfig.Transport = "docker"
}
// if one of the two registries has "ssl: false" a registry.conf file has to be
// created with the insecure registries list.
// the 3 cases are: both are insecure, src is insecure, dest is insecure.
if !srcConfig.Ssl && destConfig.Ssl {
err = createSkopeoConfig1(&srcConfig)
if err != nil {
return errors.New(fmt.Sprintf("error creating Skopeo registries config: %v", err))
}
} else if srcConfig.Ssl && !destConfig.Ssl {
err = createSkopeoConfig1(&destConfig)
if err != nil {
return errors.New(fmt.Sprintf("error creating Skopeo registries config: %v", err))
}
} else if !srcConfig.Ssl && !destConfig.Ssl {
err = createSkopeoConfig2(ConfigFile{Src: srcConfig, Dest: destConfig})
if err != nil {
return errors.New(fmt.Sprintf("error creating Skopeo registries config: %v", err))
}
}
return nil
}
// This two functions differ only by type of the parameter received and template used.
// The first one covers the case of one insecure registry, the second one both insecure
func createSkopeoConfig1(c *Config) error {
tmpl, err := template.New("conf").Parse(insecureRegistryTemplate1)
if err != nil {
return err
}
filename := "./registry.conf"
f, err := os.Create(filename)
if err != nil {
return err
}
err = tmpl.Execute(f, c)
if err != nil {
return err
}
return nil
}
func createSkopeoConfig2(c ConfigFile) error {
tmpl, err := template.New("conf").Parse(insecureRegistryTemplate2)
if err != nil {
return err
}
filename := "./registry.conf"
f, err := os.Create(filename)
if err != nil {
return err
}
err = tmpl.Execute(f, c)
if err != nil {
return err
}
return nil
}