id.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. package daemon // import "github.com/docker/docker/daemon"
  2. import (
  3. "os"
  4. "path/filepath"
  5. "github.com/docker/docker/pkg/ioutils"
  6. "github.com/google/uuid"
  7. "github.com/pkg/errors"
  8. )
  9. const idFilename = "engine-id"
  10. // LoadOrCreateID loads the engine's ID from the given root, or generates a new ID
  11. // if it doesn't exist. It returns the ID, and any error that occurred when
  12. // saving the file.
  13. //
  14. // Note that this function expects the daemon's root directory to already have
  15. // been created with the right permissions and ownership (usually this would
  16. // be done by daemon.CreateDaemonRoot().
  17. func LoadOrCreateID(root string) (string, error) {
  18. var id string
  19. idPath := filepath.Join(root, idFilename)
  20. idb, err := os.ReadFile(idPath)
  21. if os.IsNotExist(err) {
  22. id = uuid.New().String()
  23. if err := ioutils.AtomicWriteFile(idPath, []byte(id), os.FileMode(0o600)); err != nil {
  24. return "", errors.Wrap(err, "error saving ID file")
  25. }
  26. } else if err != nil {
  27. return "", errors.Wrapf(err, "error loading ID file %s", idPath)
  28. } else {
  29. id = string(idb)
  30. }
  31. return id, nil
  32. }