From aac1ccf12d0076ce7ae2b67ed9cbdf55dc99e14e Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Tue, 30 Mar 2021 14:15:20 -0600 Subject: [PATCH] caddy: Add InstanceID() method Caddy can now generate and persist its own instance ID, a UUID that is stored in the data directory. This makes it possible to differentiate it from other instances in a cluster. --- caddy.go | 21 +++++++++++++++++++++ go.mod | 1 + go.sum | 3 ++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/caddy.go b/caddy.go index fccfeef9..dcbcb2dd 100644 --- a/caddy.go +++ b/caddy.go @@ -33,6 +33,7 @@ import ( "time" "github.com/caddyserver/certmagic" + "github.com/google/uuid" "go.uber.org/zap" ) @@ -662,6 +663,26 @@ func ParseDuration(s string) (time.Duration, error) { return time.ParseDuration(s) } +// InstanceID returns the UUID for this instance, and generates one if it +// does not already exist. The UUID is stored in the local data directory, +// regardless of storage configuration, since each instance is intended to +// have its own unique ID. +func InstanceID() (uuid.UUID, error) { + uuidFilePath := filepath.Join(AppDataDir(), "instance.uuid") + uuidFileBytes, err := os.ReadFile(uuidFilePath) + if os.IsNotExist(err) { + uuid, err := uuid.NewRandom() + if err != nil { + return uuid, err + } + err = ioutil.WriteFile(uuidFilePath, []byte(uuid.String()), 0644) + return uuid, err + } else if err != nil { + return [16]byte{}, err + } + return uuid.ParseBytes(uuidFileBytes) +} + // GoModule returns the build info of this Caddy // build from debug.BuildInfo (requires Go modules). // If no version information is available, a non-nil diff --git a/go.mod b/go.mod index 4e462aba..a1e842ea 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( github.com/dustin/go-humanize v1.0.1-0.20200219035652-afde56e7acac github.com/go-chi/chi v4.1.2+incompatible github.com/google/cel-go v0.6.0 + github.com/google/uuid v1.2.0 github.com/klauspost/compress v1.11.3 github.com/klauspost/cpuid/v2 v2.0.1 github.com/lucas-clemente/quic-go v0.19.3 diff --git a/go.sum b/go.sum index 5268b114..2c803410 100644 --- a/go.sum +++ b/go.sum @@ -304,8 +304,9 @@ github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm4 github.com/google/trillian v1.2.2-0.20190612132142-05461f4df60a/go.mod h1:YPmUVn5NGwgnDUgqlVyFGMTgaWlnSvH7W5p+NdOG8UA= github.com/google/trillian-examples v0.0.0-20190603134952-4e75ba15216c/go.mod h1:WgL3XZ3pA8/9cm7yxqWrZE6iZkESB2ItGxy5Fo6k2lk= github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs= +github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/googleapis/gax-go v2.0.0+incompatible h1:j0GKcs05QVmm7yesiZq2+9cxHkNK9YM6zKx4D2qucQU= github.com/googleapis/gax-go v2.0.0+incompatible/go.mod h1:SFVmujtThgffbyetf+mdk2eWhX2bMyUtNHzFKcPA9HY= github.com/googleapis/gax-go/v2 v2.0.3/go.mod h1:LLvjysVCY1JZeum8Z6l8qUty8fiNwE08qbEPm1M08qg=