update server and makefile

Signed-off-by: Jess Frazelle <acidburn@microsoft.com>
This commit is contained in:
Jess Frazelle 2018-07-15 18:29:44 -04:00
parent 3089f164d4
commit 1b4fbf176f
No known key found for this signature in database
GPG key ID: 18F3685C0022BFF3
2 changed files with 107 additions and 105 deletions

View file

@ -43,7 +43,7 @@ static: ## Builds a static executable
-tags "$(BUILDTAGS) static_build" \ -tags "$(BUILDTAGS) static_build" \
${GO_LDFLAGS_STATIC} -o $(NAME) . ${GO_LDFLAGS_STATIC} -o $(NAME) .
all: clean build fmt lint test staticcheck vet install ## Runs a clean, build, fmt, lint, test, staticcheck, vet and install all: clean build fmt lint test staticcheck vet install build-server ## Runs a clean, build, fmt, lint, test, staticcheck, vet and install
.PHONY: fmt .PHONY: fmt
fmt: ## Verifies all files have been `gofmt`ed fmt: ## Verifies all files have been `gofmt`ed
@ -207,6 +207,20 @@ snakeoil: ## Update snakeoil certs for testing
mv $(CURDIR)/key.pem $(CURDIR)/testutils/snakeoil/key.pem mv $(CURDIR)/key.pem $(CURDIR)/testutils/snakeoil/key.pem
mv $(CURDIR)/cert.pem $(CURDIR)/testutils/snakeoil/cert.pem mv $(CURDIR)/cert.pem $(CURDIR)/testutils/snakeoil/cert.pem
.PHONY: build-server
build-server: $(NAME)-server ## Builds a dynamic executable for reg-server
$(NAME)-server: $(wildcard */*.go) VERSION.txt
@echo "+ $@"
$(GO) build -tags "$(BUILDTAGS)" ${GO_LDFLAGS} -o $(NAME)-server ./server/...
.PHONY: static-server
static-server: ## Builds a static reg-server executable
@echo "+ $@"
CGO_ENABLED=0 $(GO) build \
-tags "$(BUILDTAGS) static_build" \
${GO_LDFLAGS_STATIC} -o $(NAME)-server ./server
.PHONY: help .PHONY: help
help: help:
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

View file

@ -1,6 +1,8 @@
package main package main
import ( import (
"context"
"flag"
"html/template" "html/template"
"net/http" "net/http"
"os" "os"
@ -8,119 +10,107 @@ import (
"strings" "strings"
"time" "time"
"github.com/genuinetools/pkg/cli"
"github.com/genuinetools/reg/clair" "github.com/genuinetools/reg/clair"
"github.com/genuinetools/reg/registry" "github.com/genuinetools/reg/registry"
"github.com/genuinetools/reg/repoutils" "github.com/genuinetools/reg/repoutils"
"github.com/genuinetools/reg/version"
"github.com/gorilla/mux" "github.com/gorilla/mux"
wordwrap "github.com/mitchellh/go-wordwrap" wordwrap "github.com/mitchellh/go-wordwrap"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
const (
// VERSION is the binary version.
VERSION = "v0.2.0"
) )
var ( var (
updating = false insecure bool
forceNonSSL bool
skipPing bool
interval time.Duration
timeout time.Duration
username string
password string
registryServer string
clairServer string
once bool
cert string
key string
port string
debug bool
updating bool
r *registry.Registry r *registry.Registry
cl *clair.Clair cl *clair.Clair
tmpl *template.Template tmpl *template.Template
) )
// preload initializes any global options and configuration func main() {
// before the main or sub commands are run. // Create a new cli program.
func preload(c *cli.Context) (err error) { p := cli.NewProgram()
if c.GlobalBool("debug") { p.Name = "reg-server"
p.Description = "Docker registry v2 static UI server"
// Set the GitCommit and Version.
p.GitCommit = version.GITCOMMIT
p.Version = version.VERSION
// Setup the global flags.
p.FlagSet = flag.NewFlagSet("global", flag.ExitOnError)
p.FlagSet.BoolVar(&insecure, "insecure", false, "do not verify tls certificates")
p.FlagSet.BoolVar(&insecure, "k", false, "do not verify tls certificates")
p.FlagSet.BoolVar(&forceNonSSL, "force-non-ssl", false, "force allow use of non-ssl")
p.FlagSet.BoolVar(&forceNonSSL, "f", false, "force allow use of non-ssl")
p.FlagSet.BoolVar(&skipPing, "skip-ping", false, "skip pinging the registry while establishing connection")
p.FlagSet.DurationVar(&interval, "interval", time.Hour, "interval to generate new index.html's at")
p.FlagSet.DurationVar(&timeout, "timeout", time.Minute, "timeout for HTTP requests")
p.FlagSet.StringVar(&username, "username", "", "username for the registry")
p.FlagSet.StringVar(&username, "u", "", "username for the registry")
p.FlagSet.StringVar(&password, "password", "", "password for the registry")
p.FlagSet.StringVar(&password, "p", "", "password for the registry")
p.FlagSet.StringVar(&registryServer, "registry", "", "URL to the private registry (ex. r.j3ss.co)")
p.FlagSet.StringVar(&registryServer, "r", "", "URL to the private registry (ex. r.j3ss.co)")
p.FlagSet.StringVar(&clairServer, "clair", "", "url to clair instance")
p.FlagSet.StringVar(&cert, "cert", "", "path to ssl cert")
p.FlagSet.StringVar(&key, "key", "", "path to ssl key")
p.FlagSet.StringVar(&port, "port", "8080", "port for server to run on")
p.FlagSet.BoolVar(&once, "once", false, "generate an output once and then exit")
p.FlagSet.BoolVar(&debug, "d", false, "enable debug logging")
// Set the before function.
p.Before = func(ctx context.Context) error {
// Set the log level.
if debug {
logrus.SetLevel(logrus.DebugLevel) logrus.SetLevel(logrus.DebugLevel)
} }
return nil return nil
} }
func main() { // Set the main program action.
app := cli.NewApp() p.Action = func(ctx context.Context) error {
app.Name = "reg-server" auth, err := repoutils.GetAuthConfig(username, password, registryServer)
app.Version = VERSION
app.Author = "The Genuinetools Authors"
app.Email = "no-reply@butts.com"
app.Usage = "Docker registry v2 static UI server."
app.Before = preload
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "debug, d",
Usage: "run in debug mode",
},
cli.StringFlag{
Name: "username, u",
Usage: "username for the registry",
},
cli.StringFlag{
Name: "password, p",
Usage: "password for the registry",
},
cli.StringFlag{
Name: "registry, r",
Usage: "URL to the private registry (ex. r.j3ss.co)",
},
cli.BoolFlag{
Name: "insecure, k",
Usage: "do not verify tls certificates of registry",
},
cli.BoolFlag{
Name: "once, o",
Usage: "generate an output once and then exit",
},
cli.StringFlag{
Name: "port",
Value: "8080",
Usage: "port for server to run on",
},
cli.StringFlag{
Name: "cert",
Usage: "path to ssl cert",
},
cli.StringFlag{
Name: "key",
Usage: "path to ssl key",
},
cli.DurationFlag{
Name: "interval",
Value: time.Hour,
Usage: "interval to generate new index.html's at",
},
cli.StringFlag{
Name: "clair",
Usage: "url to clair instance",
},
cli.BoolFlag{
Name: "skip-ping",
Usage: "skip pinging the registry while establishing connection",
},
cli.StringFlag{
Name: "timeout",
Value: "1m",
Usage: "timeout for HTTP requests",
},
}
app.Action = func(c *cli.Context) error {
auth, err := repoutils.GetAuthConfig(c.GlobalString("username"), c.GlobalString("password"), c.GlobalString("registry"))
if err != nil { if err != nil {
logrus.Fatal(err) logrus.Fatal(err)
} }
// Parse the timeout.
timeout, err := time.ParseDuration(c.GlobalString("timeout"))
if err != nil {
logrus.Fatalf("parsing %s as duration failed: %v", c.GlobalString("timeout"), err)
}
// Create the registry client. // Create the registry client.
r, err = registry.New(auth, registry.Opt{ r, err = registry.New(auth, registry.Opt{
Insecure: c.GlobalBool("insecure"), Insecure: insecure,
Debug: c.GlobalBool("debug"), Debug: debug,
SkipPing: c.GlobalBool("skip-ping"), SkipPing: skipPing,
Timeout: timeout, Timeout: timeout,
}) })
if err != nil { if err != nil {
@ -128,10 +118,10 @@ func main() {
} }
// create a clair instance if needed // create a clair instance if needed
if c.GlobalString("clair") != "" { if len(clairServer) < 1 {
cl, err = clair.New(c.String("clair"), clair.Opt{ cl, err = clair.New(clairServer, clair.Opt{
Insecure: c.GlobalBool("insecure"), Insecure: insecure,
Debug: c.GlobalBool("debug"), Debug: debug,
Timeout: timeout, Timeout: timeout,
}) })
if err != nil { if err != nil {
@ -202,12 +192,12 @@ func main() {
logrus.Fatalf("Error creating index: %v", err) logrus.Fatalf("Error creating index: %v", err)
} }
if c.GlobalBool("once") { if once {
logrus.Info("Output generated") logrus.Info("Output generated")
return nil return nil
} }
ticker := time.NewTicker(c.Duration("interval")) ticker := time.NewTicker(interval)
go func() { go func() {
// create more indexes every X minutes based off interval // create more indexes every X minutes based off interval
@ -219,7 +209,7 @@ func main() {
updating = false updating = false
} }
} else { } else {
logrus.Warnf("skipping timer based static index update for %s", c.String("interval")) logrus.Warnf("skipping timer based static index update for %s", interval.String())
} }
} }
}() }()
@ -241,14 +231,13 @@ func main() {
mux.Handle("/", staticHandler) mux.Handle("/", staticHandler)
// set up the server // set up the server
port := c.String("port")
server := &http.Server{ server := &http.Server{
Addr: ":" + port, Addr: ":" + port,
Handler: mux, Handler: mux,
} }
logrus.Infof("Starting server on port %q", port) logrus.Infof("Starting server on port %q", port)
if c.String("cert") != "" && c.String("key") != "" { if len(cert) > 0 && len(key) > 0 {
logrus.Fatal(server.ListenAndServeTLS(c.String("cert"), c.String("key"))) logrus.Fatal(server.ListenAndServeTLS(cert, key))
} else { } else {
logrus.Fatal(server.ListenAndServe()) logrus.Fatal(server.ListenAndServe())
} }
@ -256,7 +245,6 @@ func main() {
return nil return nil
} }
if err := app.Run(os.Args); err != nil { // Run our program.
logrus.Fatal(err) p.Run()
}
} }