From 3525abf84f9b300e54a7b976810ff3146f3ae91c Mon Sep 17 00:00:00 2001 From: Dalton Hubble Date: Tue, 26 Apr 2016 13:40:01 -0700 Subject: [PATCH] bootcfg/version: Add version package and HTTP version output * bootcfg/version package provides the linker's GIT SHA based version to bootcfg components * Display the bootcfg version via the HTTP server / path for convenience and for validating a deployed version easily * Log requests to the / rooted subtree paths to surface machines which are making requests containing typos and mistakes that do not match HTTP API endpoints --- Documentation/config.md | 2 +- bootcfg/http/handlers.go | 15 +++++++++++++++ bootcfg/http/server.go | 2 ++ bootcfg/version/version.go | 5 +++++ build | 2 +- cmd/bootcfg/main.go | 7 +++---- 6 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 bootcfg/version/version.go diff --git a/Documentation/config.md b/Documentation/config.md index 9b1c9775..f2e255e8 100644 --- a/Documentation/config.md +++ b/Documentation/config.md @@ -20,7 +20,7 @@ Configuration arguments can be provided as flags or as environment variables. | data | /var/lib/bootcfg/{profiles,groups,ignition,cloud} | | assets | /var/lib/bootcfg/assets | -## Check Version +## Version ./bin/bootcfg -version sudo rkt --insecure-options=image run quay.io/coreos/bootcfg:latest -- -version diff --git a/bootcfg/http/handlers.go b/bootcfg/http/handlers.go index fd62946d..1be8c59c 100644 --- a/bootcfg/http/handlers.go +++ b/bootcfg/http/handlers.go @@ -1,12 +1,14 @@ package http import ( + "fmt" "net/http" "golang.org/x/net/context" "github.com/coreos/coreos-baremetal/bootcfg/server" pb "github.com/coreos/coreos-baremetal/bootcfg/server/serverpb" + "github.com/coreos/coreos-baremetal/bootcfg/version" ) // requireGET requires requests to be an HTTP GET. Otherwise, it responds with @@ -31,6 +33,19 @@ func logRequests(next http.Handler) http.Handler { return http.HandlerFunc(fn) } +// versionHandler shows the server name and version for root requests. +// Otherwise, a 404 is returned. +func versionHandler() http.Handler { + fn := func(w http.ResponseWriter, req *http.Request) { + if req.URL.Path != "/" { + http.NotFound(w, req) + return + } + fmt.Fprintf(w, "bootcfg version: %s", version.Version) + } + return http.HandlerFunc(fn) +} + // selectGroup selects the Group whose selectors match the query parameters, // adds the Group to the ctx, and calls the next handler. The next handler // should handle a missing Group. diff --git a/bootcfg/http/server.go b/bootcfg/http/server.go index 113a60c8..4ed12054 100644 --- a/bootcfg/http/server.go +++ b/bootcfg/http/server.go @@ -46,6 +46,8 @@ func (s *Server) HTTPHandler() http.Handler { mux := http.NewServeMux() srv := server.NewServer(&server.Config{s.store}) + // bootcfg version + mux.Handle("/", logRequests(versionHandler())) // Boot via GRUB mux.Handle("/grub", logRequests(NewHandler(selectProfile(srv, grubHandler())))) // Boot via iPXE diff --git a/bootcfg/version/version.go b/bootcfg/version/version.go new file mode 100644 index 00000000..e58b2e12 --- /dev/null +++ b/bootcfg/version/version.go @@ -0,0 +1,5 @@ +// Package version provides the bootcfg version. +package version + +// Version provided by compile time -ldflags. +var Version = "was not built properly" diff --git a/build b/build index 335144f5..d0b027f6 100755 --- a/build +++ b/build @@ -1,6 +1,6 @@ #!/bin/bash -e -LD_FLAGS="-w -X main.version=$(./git-version)" +LD_FLAGS="-w -X github.com/coreos/coreos-baremetal/bootcfg/version.Version=$(./git-version)" CGO_ENABLED=0 go build -o bin/bootcfg -ldflags "$LD_FLAGS" -a -tags netgo github.com/coreos/coreos-baremetal/cmd/bootcfg # bootcmd CLI binary diff --git a/cmd/bootcfg/main.go b/cmd/bootcfg/main.go index 1daf1426..85665f73 100644 --- a/cmd/bootcfg/main.go +++ b/cmd/bootcfg/main.go @@ -17,12 +17,11 @@ import ( "github.com/coreos/coreos-baremetal/bootcfg/server" "github.com/coreos/coreos-baremetal/bootcfg/sign" "github.com/coreos/coreos-baremetal/bootcfg/storage" + "github.com/coreos/coreos-baremetal/bootcfg/version" ) var ( - // version provided by compile time flag: -ldflags "-X main.version $GIT_SHA" - version = "was not built properly" - log = capnslog.NewPackageLogger("github.com/coreos/coreos-baremetal/cmd/bootcfg", "main") + log = capnslog.NewPackageLogger("github.com/coreos/coreos-baremetal/cmd/bootcfg", "main") ) func main() { @@ -56,7 +55,7 @@ func main() { passphrase := os.Getenv("BOOTCFG_PASSPHRASE") if flags.version { - fmt.Println(version) + fmt.Println(version.Version) return }