Skip to content

Commit

Permalink
Frrist/meta/instanceid (#4411)
Browse files Browse the repository at this point in the history
Co-authored-by: frrist <forrest@expanso.io>
  • Loading branch information
frrist and frrist authored Sep 16, 2024
1 parent fa041b9 commit ac24db9
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/bmatcuk/doublestar/v4 v4.6.1
github.com/c2h5oh/datasize v0.0.0-20220606134207-859f65c6625b
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
github.com/denisbrodbeck/machineid v1.0.1
github.com/docker/docker v27.1.1+incompatible
github.com/dylibso/observe-sdk/go v0.0.0-20231201014635-141351c24659
github.com/fatih/structs v1.1.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,8 @@ github.com/decred/dcrd/crypto/blake256 v1.0.1 h1:7PltbUIQB7u/FfZ39+DGa/ShuMyJ5il
github.com/decred/dcrd/crypto/blake256 v1.0.1/go.mod h1:2OfgNZ5wDpcsFmHmCK5gZTPcCXqlm2ArzUIkw9czNJo=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 h1:8UrgZ3GkP4i/CLijOJx79Yu+etlyjdBU4sfcs2WYQMs=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0/go.mod h1:v57UDF4pDQJcEfFUCRop3lJL149eHGSe9Jvczhzjo/0=
github.com/denisbrodbeck/machineid v1.0.1 h1:geKr9qtkB876mXguW2X6TU4ZynleN6ezuMSRhl4D7AQ=
github.com/denisbrodbeck/machineid v1.0.1/go.mod h1:dJUwb7PTidGDeYyUBmXZ2GphQBbjJCrnectwCyxcUSI=
github.com/dgraph-io/badger v1.6.2 h1:mNw0qs90GVgGGWylh0umH5iag1j6n/PeJtNvL6KY/x8=
github.com/dgraph-io/badger v1.6.2/go.mod h1:JW2yswe3V058sS0kZ2h/AXeDSqFjxnZcRrVH//y2UQE=
github.com/dgraph-io/badger/v3 v3.2103.5 h1:ylPa6qzbjYRQMU6jokoj4wzcaweHylt//CH0AKt0akg=
Expand Down
15 changes: 15 additions & 0 deletions pkg/repo/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ func (fsr *FsRepo) Init() error {
// TODO this should be a part of the config.
telemetry.SetupFromEnvs()

// never fail here as this isn't critical to node start up.
if err := fsr.WriteInstanceID(GenerateInstanceID()); err != nil {
log.Trace().Err(err).Msgf("failed to write instanceID")
}

if err := fsr.WriteVersion(Version4); err != nil {
return fmt.Errorf("failed to persist repo version: %w", err)
}
Expand All @@ -120,6 +125,16 @@ func (fsr *FsRepo) Open() error {
}
}

// check if an instanceID exists persisting one if not found.
// never fail here as this isn't critical to node start up.
if instanceID, err := fsr.ReadInstanceID(); err != nil {
log.Trace().Err(err).Msgf("failed to read instanceID")
} else if instanceID == "" {
if err := fsr.WriteInstanceID(GenerateInstanceID()); err != nil {
log.Trace().Err(err).Msgf("failed to write instanceID")
}
}

// TODO this should be a part of the config.
telemetry.SetupFromEnvs()

Expand Down
24 changes: 24 additions & 0 deletions pkg/repo/instanceid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package repo

import (
"crypto/sha256"
"encoding/hex"

"github.com/denisbrodbeck/machineid"
"github.com/google/uuid"
)

// GenerateInstanceID creates a unique, anonymous identifier for the instance of bacalhau.
func GenerateInstanceID() string {
// Get machine ID, which provides a consistent identifier for the device
machineID, err := machineid.ID()
if err != nil {
// if we fail to read a machineID, generate a UUID
machineID = uuid.NewString()
}

// hash the machineID to make it anonymous.
hash := sha256.New()
hash.Write([]byte(machineID))
return hex.EncodeToString(hash.Sum(nil))
}
19 changes: 19 additions & 0 deletions pkg/repo/sysmeta.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const SystemMetadataFile = "system_metadata.yaml"
type SystemMetadata struct {
RepoVersion int `yaml:"RepoVersion"`
InstallationID string `yaml:"InstallationID"`
InstanceID string `yaml:"InstanceID"`
LastUpdateCheck time.Time `yaml:"LastUpdateCheck"`
NodeName string `yaml:"NodeName"`
}
Expand Down Expand Up @@ -98,6 +99,24 @@ func (fsr *FsRepo) WriteInstallationID(id string) error {
})
}

// ReadInstanceID reads the InstanceID in the metadata.
// It fails if the metadata file doesn't exist.
func (fsr *FsRepo) ReadInstanceID() (string, error) {
sysmeta, err := fsr.readMetadata()
if err != nil {
return "", err
}
return sysmeta.InstanceID, nil
}

// WriteInstanceID updates the InstanceID in the metadata.
// It fails if the metadata file doesn't exist.
func (fsr *FsRepo) WriteInstanceID(id string) error {
return fsr.updateExistingMetadata(func(sysmeta *SystemMetadata) {
sysmeta.InstanceID = id
})
}

func (fsr *FsRepo) ReadNodeName() (string, error) {
sysmeta, err := fsr.readMetadata()
if err != nil {
Expand Down

0 comments on commit ac24db9

Please sign in to comment.