Refactor code related to getting current version

And set version in CertMagic for User-Agent purposes
This commit is contained in:
Matthew Holt 2019-06-28 19:28:28 -06:00
parent a4bdf249db
commit 31ab737bf2
5 changed files with 61 additions and 48 deletions

View file

@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log" "log"
"runtime/debug"
"strings" "strings"
"sync" "sync"
"time" "time"
@ -12,6 +13,28 @@ import (
"github.com/mholt/certmagic" "github.com/mholt/certmagic"
) )
// Config represents a Caddy configuration.
type Config struct {
Admin *AdminConfig `json:"admin,omitempty"`
StorageRaw json.RawMessage `json:"storage,omitempty"`
storage certmagic.Storage
AppsRaw map[string]json.RawMessage `json:"apps,omitempty"`
// apps stores the decoded Apps values,
// keyed by module name.
apps map[string]App
cancelFunc context.CancelFunc
}
// App is a thing that Caddy runs.
type App interface {
Start() error
Stop() error
}
// Run runs Caddy with the given config. // Run runs Caddy with the given config.
func Run(newCfg *Config) error { func Run(newCfg *Config) error {
currentCfgMu.Lock() currentCfgMu.Lock()
@ -69,7 +92,7 @@ func Run(newCfg *Config) error {
return err return err
} }
// Load, Provision, Validate // Load, Provision, Validate each app and their submodules
err = func() error { err = func() error {
for modName, rawMsg := range newCfg.AppsRaw { for modName, rawMsg := range newCfg.AppsRaw {
val, err := ctx.LoadModule(modName, rawMsg) val, err := ctx.LoadModule(modName, rawMsg)
@ -112,7 +135,7 @@ func Run(newCfg *Config) error {
oldCfg := currentCfg oldCfg := currentCfg
currentCfg = newCfg currentCfg = newCfg
// Stop, Cleanup // Stop, Cleanup each old app
if oldCfg != nil { if oldCfg != nil {
for name, a := range oldCfg.apps { for name, a := range oldCfg.apps {
err := a.Stop() err := a.Stop()
@ -121,35 +144,13 @@ func Run(newCfg *Config) error {
} }
} }
// clean up old modules // clean up all old modules
oldCfg.cancelFunc() oldCfg.cancelFunc()
} }
return nil return nil
} }
// App is a thing that Caddy runs.
type App interface {
Start() error
Stop() error
}
// Config represents a Caddy configuration.
type Config struct {
Admin *AdminConfig `json:"admin,omitempty"`
StorageRaw json.RawMessage `json:"storage,omitempty"`
storage certmagic.Storage
AppsRaw map[string]json.RawMessage `json:"apps,omitempty"`
// apps stores the decoded Apps values,
// keyed by module name.
apps map[string]App
cancelFunc context.CancelFunc
}
// Duration is a JSON-string-unmarshable duration type. // Duration is a JSON-string-unmarshable duration type.
type Duration time.Duration type Duration time.Duration
@ -163,6 +164,29 @@ func (d *Duration) UnmarshalJSON(b []byte) error {
return nil return nil
} }
// GoModule returns the build info of this Caddy
// build from debug.BuildInfo (requires Go modules).
// If no version information is available, a non-nil
// value will still be returned, but with an
// unknown version.
func GoModule() *debug.Module {
bi, ok := debug.ReadBuildInfo()
if ok {
// The recommended way to build Caddy involves
// creating a separate main module, which
// TODO: track related Go issue: https://github.com/golang/go/issues/29228
for _, mod := range bi.Deps {
if mod.Path == goModule {
return mod
}
}
}
return &debug.Module{Version: "unknown"}
}
// goModule is the name of this Go module.
const goModule = "github.com/mholt/caddy"
// CtxKey is a value type for use with context.WithValue. // CtxKey is a value type for use with context.WithValue.
type CtxKey string type CtxKey string

View file

@ -10,8 +10,10 @@ import (
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strings"
"github.com/caddyserver/caddy" "github.com/caddyserver/caddy"
"github.com/mholt/certmagic"
"github.com/mitchellh/go-ps" "github.com/mitchellh/go-ps"
) )
@ -126,6 +128,11 @@ func cmdRun() (int, error) {
} }
} }
// set a fitting User-Agent for ACME requests
goModule := caddy.GoModule()
cleanModVersion := strings.TrimPrefix(goModule.Version, "v")
certmagic.UserAgent = "Caddy/" + cleanModVersion
// start the admin endpoint along with any initial config // start the admin endpoint along with any initial config
err := caddy.StartAdmin(config) err := caddy.StartAdmin(config)
if err != nil { if err != nil {
@ -180,10 +187,10 @@ func cmdStop() (int, error) {
} }
func cmdVersion() (int, error) { func cmdVersion() (int, error) {
goModule := getGoBuildModule() goModule := caddy.GoModule()
if goModule.Sum != "" { if goModule.Sum != "" {
// a build with a known version will also have a checksum // a build with a known version will also have a checksum
fmt.Printf("%s (%s)\n", goModule.Version, goModule.Sum) fmt.Printf("%s %s\n", goModule.Version, goModule.Sum)
} else { } else {
fmt.Println(goModule.Version) fmt.Println(goModule.Version)
} }

View file

@ -9,7 +9,6 @@ import (
"log" "log"
"net" "net"
"os" "os"
"runtime/debug"
) )
// Main executes the main function of the caddy command. // Main executes the main function of the caddy command.
@ -66,23 +65,3 @@ func handlePingbackConn(conn net.Conn, expect []byte) error {
} }
return nil return nil
} }
// getGoBuildModule returns the build info of Caddy
// from debug.BuildInfo (requires Go modules). If
// no version information is available, a non-nil
// value will still be returned, but with an
// unknown version.
func getGoBuildModule() *debug.Module {
bi, ok := debug.ReadBuildInfo()
if ok {
// The recommended way to build Caddy involves
// creating a separate main module, which
// TODO: track related Go issue: https://github.com/golang/go/issues/29228
for _, mod := range bi.Deps {
if mod.Path == "github.com/mholt/caddy" {
return mod
}
}
}
return &debug.Module{Version: "unknown"}
}

1
go.mod
View file

@ -17,6 +17,7 @@ require (
github.com/klauspost/compress v1.7.1-0.20190613161414-0b31f265a57b github.com/klauspost/compress v1.7.1-0.20190613161414-0b31f265a57b
github.com/klauspost/cpuid v1.2.1 github.com/klauspost/cpuid v1.2.1
github.com/mholt/certmagic v0.6.2-0.20190624175158-6a42ef9fe8c2 github.com/mholt/certmagic v0.6.2-0.20190624175158-6a42ef9fe8c2
github.com/mitchellh/go-ps v0.0.0-20170309133038-4fdf99ab2936
github.com/rs/cors v1.6.0 github.com/rs/cors v1.6.0
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/starlight-go/starlight v0.0.0-20181207205707-b06f321544f3 github.com/starlight-go/starlight v0.0.0-20181207205707-b06f321544f3

2
go.sum
View file

@ -36,6 +36,8 @@ github.com/mholt/certmagic v0.6.2-0.20190624175158-6a42ef9fe8c2 h1:xKE9kZ5C8gelJ
github.com/mholt/certmagic v0.6.2-0.20190624175158-6a42ef9fe8c2/go.mod h1:g4cOPxcjV0oFq3qwpjSA30LReKD8AoIfwAY9VvG35NY= github.com/mholt/certmagic v0.6.2-0.20190624175158-6a42ef9fe8c2/go.mod h1:g4cOPxcjV0oFq3qwpjSA30LReKD8AoIfwAY9VvG35NY=
github.com/miekg/dns v1.1.3 h1:1g0r1IvskvgL8rR+AcHzUA+oFmGcQlaIm4IqakufeMM= github.com/miekg/dns v1.1.3 h1:1g0r1IvskvgL8rR+AcHzUA+oFmGcQlaIm4IqakufeMM=
github.com/miekg/dns v1.1.3/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= github.com/miekg/dns v1.1.3/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/mitchellh/go-ps v0.0.0-20170309133038-4fdf99ab2936 h1:kw1v0NlnN+GZcU8Ma8CLF2Zzgjfx95gs3/GN3vYAPpo=
github.com/mitchellh/go-ps v0.0.0-20170309133038-4fdf99ab2936/go.mod h1:r1VsdOzOPt1ZSrGZWFoNhsAedKnEd6r9Np1+5blZCWk=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rs/cors v1.6.0 h1:G9tHG9lebljV9mfp9SNPDL36nCDxmo3zTlAf1YgvzmI= github.com/rs/cors v1.6.0 h1:G9tHG9lebljV9mfp9SNPDL36nCDxmo3zTlAf1YgvzmI=