forked from gluster/glusterd2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyuuid.go
58 lines (49 loc) · 1.28 KB
/
myuuid.go
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package context
import (
"io/ioutil"
"os"
"path"
"github.com/gluster/glusterd2/config"
log "github.com/Sirupsen/logrus"
"github.com/pborman/uuid"
)
var (
myUUIDFile = path.Join(config.LocalStateDir, "uuid")
)
// initMyUUID initializes MyUUID by reading the `<config.LocalStateDir>/uuid` file.
// If the file is not present it generates a new UUID and saves it to the file.
// If the file is not accessible. initMyUUID panics.
func initMyUUID() {
ubytes, err := ioutil.ReadFile(myUUIDFile)
if err != nil {
switch {
case os.IsNotExist(err):
genMyUUID()
return
default:
log.WithFields(log.Fields{
"err": err,
"path": myUUIDFile,
}).Fatal("failed to read MyUUID from file")
}
}
MyUUID = uuid.Parse(string(ubytes))
log.WithField("myuuid", MyUUID.String()).Info("restored MyUUID")
}
func genMyUUID() {
MyUUID = uuid.NewRandom()
writeMyUUIDFile()
log.WithField("myuuid", MyUUID.String()).Info("generated new MyUUID")
}
func writeMyUUIDFile() {
if err := ioutil.WriteFile(myUUIDFile, []byte(MyUUID.String()), os.ModePerm); err != nil {
log.WithFields(log.Fields{
"err": err,
"path": myUUIDFile,
}).Fatal("failed to write MyUUID to file")
}
log.WithFields(log.Fields{
"myuuid": MyUUID.String(),
"path": myUUIDFile,
}).Debug("wrote MyUUID to file")
}