-
Notifications
You must be signed in to change notification settings - Fork 0
/
info.go
64 lines (56 loc) · 1.66 KB
/
info.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
59
60
61
62
63
64
package cluster
import (
"fmt"
"os"
"path/filepath"
"github.com/lxc/lxd/lxd/db"
"github.com/lxc/lxd/lxd/node"
"github.com/lxc/lxd/shared"
"github.com/lxc/lxd/shared/log15"
"github.com/lxc/lxd/shared/logger"
"github.com/pkg/errors"
)
// Load information about the dqlite node associated with this LXD member
// should have, such as its ID, address and role.
func loadInfo(database *db.Node, cert *shared.CertInfo) (*db.RaftNode, error) {
// Figure out if we actually need to act as dqlite node.
var info *db.RaftNode
err := database.Transaction(func(tx *db.NodeTx) error {
var err error
info, err = node.DetermineRaftNode(tx)
return err
})
if err != nil {
return nil, err
}
// If we're not part of the dqlite cluster, there's nothing to do.
if info == nil {
return nil, nil
}
logger.Debug("Start database node", log15.Ctx{"id": info.ID, "address": info.Address, "role": info.Role})
if info.Address == "" {
// This is a standalone node not exposed to the network.
info.Address = "1"
}
// Rename legacy data directory if needed.
dir := filepath.Join(database.Dir(), "global")
legacyDir := filepath.Join(database.Dir(), "..", "raft")
if shared.PathExists(legacyDir) {
if shared.PathExists(dir) {
return nil, fmt.Errorf("both legacy and new global database directories exist")
}
logger.Info("Renaming global database directory from raft/ to database/global/")
err := os.Rename(legacyDir, dir)
if err != nil {
return nil, errors.Wrap(err, "failed to rename legacy global database directory")
}
}
// Data directory
if !shared.PathExists(dir) {
err := os.Mkdir(dir, 0750)
if err != nil {
return nil, err
}
}
return info, nil
}