forked from cloudfoundry/consuladapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configfile.go
118 lines (103 loc) · 3.22 KB
/
configfile.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package consulrunner
import (
"encoding/json"
"fmt"
"os"
"path"
"time"
. "github.com/onsi/gomega"
)
const defaultLogLevel = "info"
const defaultProtocolVersion = 2
const (
portOffsetDNS = iota
PortOffsetHTTP
portOffsetClientRPC
portOffsetSerfLAN
portOffsetSerfWAN
portOffsetServerRPC
PortOffsetLength
)
type configFile struct {
Performace map[string]int `json:"performance,omitempty"`
BootstrapExpect int `json:"bootstrap_expect"`
Datacenter string `json:"datacenter"`
DataDir string `json:"data_dir"`
LogLevel string `json:"log_level"`
NodeName string `json:"node_name"`
Server bool `json:"server"`
Ports map[string]int `json:"ports"`
BindAddr string `json:"bind_addr"`
ProtocolVersion int `json:"protocol"`
StartJoin []string `json:"start_join"`
RetryJoin []string `json:"retry_join"`
RejoinAfterLeave bool `json:"rejoin_after_leave"`
DisableRemoteExec bool `json:"disable_remote_exec"`
DisableUpdateCheck bool `json:"disable_update_check"`
SessionTTL string `json:"session_ttl_min"`
}
func newConfigFile(
includePerformanceConfig bool,
dataDir string,
nodeName string,
clusterStartingPort int,
index int,
numNodes int,
sessionTTL time.Duration,
) configFile {
startingPort := clusterStartingPort + PortOffsetLength*index
ports := map[string]int{
"dns": startingPort + portOffsetDNS,
"http": startingPort + PortOffsetHTTP,
"rpc": startingPort + portOffsetClientRPC,
"serf_lan": startingPort + portOffsetSerfLAN,
"serf_wan": startingPort + portOffsetSerfWAN,
"server": startingPort + portOffsetServerRPC,
}
joinAddresses := make([]string, numNodes)
for i := 0; i < numNodes; i++ {
joinAddresses[i] = fmt.Sprintf("127.0.0.1:%d", clusterStartingPort+i*PortOffsetLength+portOffsetSerfLAN)
}
config := configFile{
BootstrapExpect: numNodes,
DataDir: dataDir,
LogLevel: defaultLogLevel,
NodeName: nodeName,
Server: true,
Ports: ports,
BindAddr: "127.0.0.1",
ProtocolVersion: defaultProtocolVersion,
StartJoin: joinAddresses,
RetryJoin: joinAddresses,
RejoinAfterLeave: true,
DisableRemoteExec: true,
DisableUpdateCheck: true,
SessionTTL: sessionTTL.String(),
}
if includePerformanceConfig {
config.Performace = map[string]int{"raft_multiplier": 1}
}
return config
}
func writeConfigFile(
includePerformanceConfig bool,
configDir string,
dataDir string,
nodeName string,
clusterStartingPort int,
index int,
numNodes int,
sessionTTL time.Duration,
) string {
filePath := path.Join(configDir, fmt.Sprintf("%s.json", nodeName))
file, err := os.Create(filePath)
Expect(err).NotTo(HaveOccurred())
config := newConfigFile(includePerformanceConfig, dataDir, nodeName, clusterStartingPort, index, numNodes, sessionTTL)
configJSON, err := json.Marshal(config)
Expect(err).NotTo(HaveOccurred())
_, err = file.Write(configJSON)
Expect(err).NotTo(HaveOccurred())
err = file.Close()
Expect(err).NotTo(HaveOccurred())
return filePath
}