Skip to content

Commit

Permalink
Add exponential back-off waiting until /etc/machine-id is populated
Browse files Browse the repository at this point in the history
Fixes kairos-io/kairos#3038

Signed-off-by: Dimitris Karakasilis <[email protected]>
  • Loading branch information
jimmykarily committed Dec 12, 2024
1 parent 87b55bb commit b59f2d5
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions pkg/plugins/hostname.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package plugins
import (
"bufio"
"fmt"
"math/rand"
"strings"
"syscall"
"time"
Expand All @@ -28,9 +27,21 @@ func Hostname(l logger.Interface, s schema.Stage, fs vfs.FS, console Console) er

// Template the input string with random generated strings and UUID.
// Those can be used to e.g. generate random node names based on patterns "foo-{{.UUID}}"
rand.Seed(time.Now().UnixNano())
// Exponential back-off for machineid generation.
// Systemd might take a while before it populates /etc/machine-id.
var id string
var err error
for i := 0; i < 4; i++ {
id, err = machineid.ID()
if err == nil {
break
}
time.Sleep(time.Duration(2<<i) * time.Second) // Exponential back-off starting at 2 seconds
}
if err != nil {
return fmt.Errorf("failed to get machine id: %w", err)
}

id, _ := machineid.ID()
myuuid, err := uuid.NewV4()
if err != nil {
return err
Expand Down

0 comments on commit b59f2d5

Please sign in to comment.