forked from buchgr/bazel-remote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
204 lines (187 loc) · 5.5 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
package main
import (
"context"
"fmt"
"log"
"net/http"
"net/url"
"os"
"strconv"
"sync"
"time"
auth "github.com/abbot/go-http-auth"
"github.com/buchgr/bazel-remote/cache"
"github.com/buchgr/bazel-remote/cache/disk"
"github.com/buchgr/bazel-remote/cache/gcs"
cachehttp "github.com/buchgr/bazel-remote/cache/http"
"github.com/buchgr/bazel-remote/config"
"github.com/buchgr/bazel-remote/server"
"github.com/urfave/cli"
)
func main() {
app := cli.NewApp()
app.Description = "A remote build cache for Bazel."
app.Usage = "A remote build cache for Bazel"
app.HideVersion = true
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "config_file",
Value: "",
Usage: "Path to a YAML configuration file. If this flag is specified then all other flags " +
"are ignored.",
EnvVar: "BAZEL_REMOTE_CONFIG_FILE",
},
cli.StringFlag{
Name: "dir",
Value: "",
Usage: "Directory path where to store the cache contents. This flag is required.",
EnvVar: "BAZEL_REMOTE_DIR",
},
cli.Int64Flag{
Name: "max_size",
Value: -1,
Usage: "The maximum size of the remote cache in GiB. This flag is required.",
EnvVar: "BAZEL_REMOTE_MAX_SIZE",
},
cli.StringFlag{
Name: "host",
Value: "",
Usage: "Address to listen on. Listens on all network interfaces by default.",
EnvVar: "BAZEL_REMOTE_HOST",
},
cli.IntFlag{
Name: "port",
Value: 8080,
Usage: "The port the HTTP server listens on.",
EnvVar: "BAZEL_REMOTE_PORT",
},
cli.StringFlag{
Name: "htpasswd_file",
Value: "",
Usage: "Path to a .htpasswd file. This flag is optional. Please read https://httpd.apache.org/docs/2.4/programs/htpasswd.html.",
EnvVar: "BAZEL_REMOTE_HTPASSWD_FILE",
},
cli.BoolFlag{
Name: "tls_enabled",
Usage: "This flag has been deprecated. Specify tls_cert_file and tls_key_file instead.",
EnvVar: "BAZEL_REMOTE_TLS_ENABLED",
},
cli.StringFlag{
Name: "tls_cert_file",
Value: "",
Usage: "Path to a pem encoded certificate file.",
EnvVar: "BAZEL_REMOTE_TLS_CERT_FILE",
},
cli.StringFlag{
Name: "tls_key_file",
Value: "",
Usage: "Path to a pem encoded key file.",
EnvVar: "BAZEL_REMOTE_TLS_KEY_FILE",
},
cli.DurationFlag{
Name: "idle_timeout",
Value: 0,
Usage: "The maximum period of having received no request after which the server will shut itself down. Disabled by default.",
EnvVar: "BAZEL_REMOTE_IDLE_TIMEOUT",
},
}
app.Action = func(ctx *cli.Context) error {
configFile := ctx.String("config_file")
var c *config.Config
var err error
if configFile != "" {
c, err = config.NewFromYamlFile(configFile)
} else {
c, err = config.New(ctx.String("dir"),
ctx.Int("max_size"),
ctx.String("host"),
ctx.Int("port"),
ctx.String("htpasswd_file"),
ctx.String("tls_cert_file"),
ctx.String("tls_key_file"),
ctx.Duration("idle_timeout"))
}
if err != nil {
fmt.Fprintf(ctx.App.Writer, "%v\n\n", err)
cli.ShowAppHelp(ctx)
return nil
}
accessLogger := log.New(os.Stdout, "", log.Ldate|log.Ltime|log.LUTC)
errorLogger := log.New(os.Stderr, "", log.Ldate|log.Ltime|log.LUTC)
diskCache := disk.New(c.Dir, int64(c.MaxSize)*1024*1024*1024)
var proxyCache cache.Cache
if c.GoogleCloudStorage != nil {
proxyCache, err = gcs.New(c.GoogleCloudStorage.Bucket,
c.GoogleCloudStorage.UseDefaultCredentials, c.GoogleCloudStorage.JSONCredentialsFile,
diskCache, accessLogger, errorLogger)
if err != nil {
log.Fatal(err)
}
} else if c.HTTPBackend != nil {
httpClient := &http.Client{}
baseURL, err := url.Parse(c.HTTPBackend.BaseURL)
if err != nil {
log.Fatal(err)
}
proxyCache = cachehttp.New(baseURL, diskCache,
httpClient, accessLogger, errorLogger)
} else {
proxyCache = diskCache
}
mux := http.NewServeMux()
httpServer := &http.Server{
Addr: c.Host + ":" + strconv.Itoa(c.Port),
Handler: mux,
}
h := server.NewHTTPCache(proxyCache, accessLogger, errorLogger)
mux.HandleFunc("/status", h.StatusPageHandler)
cacheHandler := h.CacheHandler
if c.HtpasswdFile != "" {
cacheHandler = wrapAuthHandler(cacheHandler, c.HtpasswdFile, c.Host)
}
if c.IdleTimeout > 0 {
cacheHandler = wrapIdleHandler(cacheHandler, c.IdleTimeout, accessLogger, httpServer)
}
mux.HandleFunc("/", cacheHandler)
if len(c.TLSCertFile) > 0 && len(c.TLSKeyFile) > 0 {
return httpServer.ListenAndServeTLS(c.TLSCertFile, c.TLSKeyFile)
}
return httpServer.ListenAndServe()
}
serverErr := app.Run(os.Args)
if serverErr != nil {
log.Fatal("bazel-remote terminated: ", serverErr)
}
}
func wrapIdleHandler(handler http.HandlerFunc, idleTimeout time.Duration, accessLogger cache.Logger, httpServer *http.Server) http.HandlerFunc {
lastRequest := time.Now()
ticker := time.NewTicker(time.Second)
var m sync.Mutex
go func() {
for {
select {
case now := <-ticker.C:
m.Lock()
elapsed := now.Sub(lastRequest)
m.Unlock()
if elapsed > idleTimeout {
ticker.Stop()
accessLogger.Printf("Shutting down server after having been idle for %v", idleTimeout)
httpServer.Shutdown(context.Background())
}
}
}
}()
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
now := time.Now()
m.Lock()
lastRequest = now
m.Unlock()
handler(w, r)
})
}
func wrapAuthHandler(handler http.HandlerFunc, htpasswdFile string, host string) http.HandlerFunc {
secrets := auth.HtpasswdFileProvider(htpasswdFile)
authenticator := auth.NewBasicAuthenticator(host, secrets)
return auth.JustCheck(authenticator, handler)
}