-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
240 lines (226 loc) · 5.78 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
package main
import (
"fmt"
"math/rand"
"os"
"path/filepath"
"runtime"
"time"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/mholt/archiver/v3"
"github.com/mudler/containerbay/api"
"github.com/mudler/containerbay/internal"
terminal "github.com/mudler/go-isterminal"
"github.com/mudler/luet/pkg/api/core/image"
"github.com/pkg/errors"
"github.com/pterm/pterm"
"github.com/urfave/cli"
)
var flags = []cli.Flag{
&cli.BoolFlag{
Name: "gzip",
Usage: "enable gzip",
EnvVar: "CONTAINERBAY_GZIP",
},
&cli.BoolFlag{
Name: "debug",
Usage: "enable debug messages",
EnvVar: "DEBUG",
},
&cli.StringFlag{
Name: "address",
Usage: "listening address",
EnvVar: "CONTAINERBAY_LISTENADDR",
Value: ":8080",
},
&cli.StringFlag{
Name: "dns",
Usage: "magic dns address",
EnvVar: "CONTAINERBAY_MAGICDNS",
},
&cli.StringFlag{
Name: "default-image",
Usage: "Default image to use",
EnvVar: "CONTAINERBAY_DEFAULTIMAGE",
Value: "ghcr.io/containerbay/containerbay.io:latest",
},
&cli.StringSliceFlag{
Name: "whitelist",
Usage: "Regex list of images allowed",
EnvVar: "WHITELIST",
},
&cli.StringFlag{
Name: "cleanup",
Usage: "store cleanup interval",
EnvVar: "CONTAINERBAY_CLEANUPINTERVAL",
},
&cli.StringFlag{
Name: "store",
Usage: "cachestore directory",
EnvVar: "CONTAINERBAY_CACHEDIR",
Value: "/tmp/containerbay",
},
&cli.StringFlag{
Name: "max-size",
Usage: "Max imagesize to serve",
EnvVar: "CONTAINERBAY_MAXSIZE",
},
&cli.IntFlag{
Name: "workers",
Usage: "download workers",
Value: 10,
EnvVar: "CONTAINERBAY_WORKERS",
},
&cli.IntFlag{
Name: "pool",
Usage: "Workers pool size",
Value: 0,
EnvVar: "CONTAINERBAY_POOLSIZE",
},
}
type ptermWriter struct {
Printer pterm.PrefixPrinter
}
func (p *ptermWriter) Write(b []byte) (int, error) {
p.Printer.Println(string(b))
return len(b), nil
}
func echoConfig(c *cli.Context) func(e *echo.Echo) error {
return func(e *echo.Echo) error {
if c.Bool("gzip") {
e.Use(middleware.GzipWithConfig(middleware.GzipConfig{
Level: 5,
}))
}
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{Output: &ptermWriter{Printer: pterm.Info}}))
return nil
}
}
func startBanner() {
pterm.Info.Println("Starting Containerbay")
}
func main() {
rand.Seed(time.Now().UTC().UnixNano())
if !terminal.IsTerminal(os.Stdout) {
pterm.DisableColor()
pterm.DisableStyling()
}
app := &cli.App{
Name: "containerbay",
Author: "Ettore Di Giacinto",
Usage: "containerbay",
Version: fmt.Sprintf("%s-g%s", internal.Version, internal.Commit),
Commands: []cli.Command{
{
Name: "pack",
Aliases: []string{"p"},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "destination",
Usage: "Destination image tar",
Value: "output.tar",
},
&cli.StringFlag{
Name: "os",
Value: runtime.GOOS,
Usage: "Overrides default image OS",
},
&cli.StringFlag{
Name: "arch",
Value: runtime.GOARCH,
Usage: "Overrides default image ARCH",
},
},
UsageText: `
Packs files inside a tar which is consumable by docker.
E.g.
$ containerbay --destination out.tar foo/image:tar srcfile1 srcfile2 srcdir1 ...
$ docker load -i out.tar
$ docker push foo/image:tar ...
`,
Usage: "pack a directory as a container image",
Action: func(c *cli.Context) error {
if !c.Args().Present() {
return errors.New("need an image and source files to include inside the tar")
}
if c.Bool("debug") {
pterm.EnableDebugMessages()
}
dst := c.String("destination")
img := c.Args().First()
src := c.Args().Tail()
dir, err := os.MkdirTemp("", "containerbay")
if err != nil {
return err
}
defer os.RemoveAll(dir)
err = archiver.Archive(src, filepath.Join(dir, "archive.tar"))
if err != nil {
return err
}
pterm.Info.Printfln("Creating '%s' as '%s' from %v", dst, img, src)
return image.CreateTar(filepath.Join(dir, "archive.tar"), dst, img, c.String("arch"), c.String("os"))
},
},
{
Name: "standalone",
Aliases: []string{"s"},
Flags: flags,
UsageText: `
Runs an image over API
E.g.
$ containerbay standalone foo/image:tag
`,
Usage: "run the daemon to serve only a single container image",
Action: func(c *cli.Context) error {
if !c.Args().Present() {
return errors.New("need an image")
}
if c.Bool("debug") {
pterm.EnableDebugMessages()
}
startBanner()
return api.New(
api.WithListeningAddress(c.String("address")),
api.WithMagicDNS(c.String("dns")),
api.WithCacheStore(c.String("store")),
api.Standalone(c.Args().First()),
api.WithMaxSize(c.String("max-size")),
api.WithWorkers(c.Int("workers")),
api.WithCleanupInterval(c.String("cleanup")),
api.WithPoolSize(c.Int("pool")),
api.WithDefaultImage(c.String("default-image")),
).Start(echoConfig(c))
},
},
{
Name: "run",
Aliases: []string{"r"},
Flags: flags,
Usage: "run the api to serve multiple container images",
Action: func(c *cli.Context) error {
if c.Bool("debug") {
pterm.EnableDebugMessages()
}
startBanner()
return api.New(
api.WithWhitelist(c.StringSlice("whitelist")...),
api.WithListeningAddress(c.String("address")),
api.WithCacheStore(c.String("store")),
api.WithMagicDNS(c.String("dns")),
api.WithMaxSize(c.String("max-size")),
api.WithWorkers(c.Int("workers")),
api.WithCleanupInterval(c.String("cleanup")),
api.WithPoolSize(c.Int("pool")),
api.WithDefaultImage(c.String("default-image")),
).Start(echoConfig(c))
},
},
},
}
err := app.Run(os.Args)
if err != nil {
pterm.Fatal.Println(err)
}
}