-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
228 lines (198 loc) · 5.17 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
package main
import (
"errors"
"fmt"
"net"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/flavio/collage/config"
"github.com/flavio/collage/handlers"
"github.com/docker/distribution/reference"
gorilla_handlers "github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/opencontainers/go-digest"
log "github.com/sirupsen/logrus"
"gopkg.in/urfave/cli.v1"
)
const VERSION = "0.1.0"
func cleanup(socket string) {
if socket != "" {
os.Remove(socket)
}
}
func main() {
var port int
var configData, configFile, extraCerts, cert, key, socket string
var debug bool
app := cli.NewApp()
app.Name = "collage"
app.Usage = "read-only registry made by repositories coming from multiple external registries"
app.Version = VERSION
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "cert",
Usage: "Path to the certificate to use",
EnvVar: "COLLAGE_CERTIFICATE",
Destination: &cert,
},
cli.StringFlag{
Name: "key",
Usage: "Path to the key to use",
EnvVar: "COLLAGE_KEY",
Destination: &key,
},
cli.IntFlag{
Name: "port, p",
Value: 5000,
Usage: "Listen to port",
EnvVar: "COLLAGE_PORT",
Destination: &port,
},
cli.StringFlag{
Name: "socket",
Usage: "Bind to a unix socket",
EnvVar: "COLLAGE_SOCKET",
Destination: &socket,
},
cli.StringFlag{
Name: "config-file",
Value: "",
Usage: "Configuration file",
EnvVar: "COLLAGE_CONFIG_FILE",
Destination: &configFile,
},
cli.StringFlag{
Name: "config, c",
Value: "",
Usage: "JSON configuration",
EnvVar: "COLLAGE_CONFIG",
Destination: &configData,
},
cli.StringFlag{
Name: "extra-certs",
Value: "",
Usage: "Directory containing certificate files (.pem files) to be trusted",
EnvVar: "COLLAGE_EXTRA_CERTS",
Destination: &extraCerts,
},
cli.BoolFlag{
Name: "debug, d",
Usage: "Enable extra debugging",
EnvVar: "COLLAGE_DEBUG",
Destination: &debug,
},
}
app.Action = func(c *cli.Context) error {
// ensure cleanup is done when the program is terminated
sigChannel := make(chan os.Signal)
signal.Notify(sigChannel, os.Interrupt, syscall.SIGTERM, syscall.SIGKILL)
go func() {
<-sigChannel
cleanup(socket)
os.Exit(0)
}()
if debug {
log.SetLevel(log.DebugLevel)
}
if (cert != "" && key == "") || (cert == "" && key != "") {
return cli.NewExitError(
errors.New("cert and key have to be specified at the same time"),
1)
}
if configFile != "" && configData != "" {
return cli.NewExitError(
errors.New("'--config-file' and '--config' cannot be specified at the same time"),
1)
}
if configFile == "" && configData == "" {
return cli.NewExitError(
errors.New("You must specify either '--config-file' or '--config'"),
1)
}
var cfg config.Config
var err error
if configFile != "" {
cfg, err = config.NewFromFile(configFile)
} else {
cfg, err = config.NewFromData(configData)
}
if err != nil {
return cli.NewExitError(err, 1)
}
// set config attributes influenced by cli flags
cfg.Debug = debug
if err = cfg.LoadExtraCerts(extraCerts); err != nil {
return cli.NewExitError(err, 1)
}
hApp, err := handlers.NewApp(&cfg)
if err != nil {
return cli.NewExitError(err, 1)
}
r := defineRoutes(hApp)
loggedRouter := gorilla_handlers.LoggingHandler(os.Stdout, r)
if socket != "" {
_, err := os.Stat(socket)
if err == nil {
return cli.NewExitError(
fmt.Errorf(
"Cannot create socket %q, file already in place. Is another instance already running?",
socket),
1)
} else if !os.IsNotExist(err) {
return cli.NewExitError(err, 1)
}
unixListener, err := net.Listen("unix", socket)
if err != nil {
return cli.NewExitError(err, 1)
}
defer func() {
unixListener.Close()
os.Remove(socket)
}()
err = http.Serve(unixListener, loggedRouter)
} else {
if cert == "" {
fmt.Printf("Starting insecure server on :%d\n", port)
err = http.ListenAndServe(fmt.Sprintf(":%d", port), loggedRouter)
} else {
fmt.Printf("Starting secure server on :%d\n", port)
err = http.ListenAndServeTLS(
fmt.Sprintf(":%d", port),
cert,
key,
loggedRouter)
}
}
if err != nil {
return cli.NewExitError(err, 1)
}
return nil
}
app.Run(os.Args)
}
func defineRoutes(app handlers.App) *mux.Router {
r := mux.NewRouter()
// GET API version check
r.HandleFunc(
"/v2/",
app.GetApiVersionCheck).Methods("GET")
//GET CATALOG
r.HandleFunc(
"/v2/_catalog",
app.GetCatalog).Methods("GET")
//GET TAGS
r.HandleFunc(
"/v2/{name:"+reference.NameRegexp.String()+"}/tags/list",
app.GetRepositoryTags).Methods("GET", "HEAD")
//GET MANIFEST
r.HandleFunc(
"/v2/{name:"+reference.NameRegexp.String()+"}/manifests/{reference:"+reference.TagRegexp.String()+"|"+digest.DigestRegexp.String()+"}",
app.GetManifest).Methods("GET", "HEAD")
//GET LAYER
r.HandleFunc(
"/v2/{name:"+reference.NameRegexp.String()+"}/blobs/{digest:"+digest.DigestRegexp.String()+"}",
app.GetLayer).Methods("GET", "HEAD")
return r
}