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
// before the main or sub commands are run.
func preload(c *cli.Context) (err error) {
if c.GlobalBool("debug") {
logrus.SetLevel(logrus.DebugLevel)
}
return nil
}
func main() { func main() {
app := cli.NewApp() // Create a new cli program.
app.Name = "reg-server" p := cli.NewProgram()
app.Version = VERSION p.Name = "reg-server"
app.Author = "The Genuinetools Authors" p.Description = "Docker registry v2 static UI server"
app.Email = "no-reply@butts.com"
app.Usage = "Docker registry v2 static UI server." // Set the GitCommit and Version.
app.Before = preload p.GitCommit = version.GITCOMMIT
app.Flags = []cli.Flag{ p.Version = version.VERSION
cli.BoolFlag{
Name: "debug, d", // Setup the global flags.
Usage: "run in debug mode", p.FlagSet = flag.NewFlagSet("global", flag.ExitOnError)
}, p.FlagSet.BoolVar(&insecure, "insecure", false, "do not verify tls certificates")
cli.StringFlag{ p.FlagSet.BoolVar(&insecure, "k", false, "do not verify tls certificates")
Name: "username, u",
Usage: "username for the registry", 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")
cli.StringFlag{
Name: "password, p", p.FlagSet.BoolVar(&skipPing, "skip-ping", false, "skip pinging the registry while establishing connection")
Usage: "password for the registry",
}, p.FlagSet.DurationVar(&interval, "interval", time.Hour, "interval to generate new index.html's at")
cli.StringFlag{ p.FlagSet.DurationVar(&timeout, "timeout", time.Minute, "timeout for HTTP requests")
Name: "registry, r",
Usage: "URL to the private registry (ex. r.j3ss.co)", p.FlagSet.StringVar(&username, "username", "", "username for the registry")
}, p.FlagSet.StringVar(&username, "u", "", "username for the registry")
cli.BoolFlag{
Name: "insecure, k", p.FlagSet.StringVar(&password, "password", "", "password for the registry")
Usage: "do not verify tls certificates of registry", p.FlagSet.StringVar(&password, "p", "", "password for the registry")
},
cli.BoolFlag{ p.FlagSet.StringVar(&registryServer, "registry", "", "URL to the private registry (ex. r.j3ss.co)")
Name: "once, o", p.FlagSet.StringVar(&registryServer, "r", "", "URL to the private registry (ex. r.j3ss.co)")
Usage: "generate an output once and then exit",
}, p.FlagSet.StringVar(&clairServer, "clair", "", "url to clair instance")
cli.StringFlag{
Name: "port", p.FlagSet.StringVar(&cert, "cert", "", "path to ssl cert")
Value: "8080", p.FlagSet.StringVar(&key, "key", "", "path to ssl key")
Usage: "port for server to run on", p.FlagSet.StringVar(&port, "port", "8080", "port for server to run on")
},
cli.StringFlag{ p.FlagSet.BoolVar(&once, "once", false, "generate an output once and then exit")
Name: "cert",
Usage: "path to ssl cert", p.FlagSet.BoolVar(&debug, "d", false, "enable debug logging")
},
cli.StringFlag{ // Set the before function.
Name: "key", p.Before = func(ctx context.Context) error {
Usage: "path to ssl key", // Set the log level.
}, if debug {
cli.DurationFlag{ logrus.SetLevel(logrus.DebugLevel)
Name: "interval", }
Value: time.Hour,
Usage: "interval to generate new index.html's at", return nil
},
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")) // Set the main program action.
p.Action = func(ctx context.Context) error {
auth, err := repoutils.GetAuthConfig(username, password, registryServer)
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()
}
} }