File: id.go

package info (click to toggle)
docker.io 27.5.1%2Bdfsg4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 67,384 kB
  • sloc: sh: 5,847; makefile: 1,146; ansic: 664; python: 162; asm: 133
file content (36 lines) | stat: -rw-r--r-- 1,082 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package daemon // import "github.com/docker/docker/daemon"

import (
	"os"
	"path/filepath"

	"github.com/docker/docker/pkg/ioutils"
	"github.com/google/uuid"
	"github.com/pkg/errors"
)

const idFilename = "engine-id"

// LoadOrCreateID loads the engine's ID from the given root, or generates a new ID
// if it doesn't exist. It returns the ID, and any error that occurred when
// saving the file.
//
// Note that this function expects the daemon's root directory to already have
// been created with the right permissions and ownership (usually this would
// be done by daemon.CreateDaemonRoot().
func LoadOrCreateID(root string) (string, error) {
	var id string
	idPath := filepath.Join(root, idFilename)
	idb, err := os.ReadFile(idPath)
	if os.IsNotExist(err) {
		id = uuid.New().String()
		if err := ioutils.AtomicWriteFile(idPath, []byte(id), os.FileMode(0o600)); err != nil {
			return "", errors.Wrap(err, "error saving ID file")
		}
	} else if err != nil {
		return "", errors.Wrapf(err, "error loading ID file %s", idPath)
	} else {
		id = string(idb)
	}
	return id, nil
}