id.go 984 B

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