-
Notifications
You must be signed in to change notification settings - Fork 13
/
configuration.go
187 lines (159 loc) · 5.77 KB
/
configuration.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright (c) 2014-2018 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"errors"
"fmt"
"github.com/bitmark-inc/bitmarkd/chain"
"github.com/bitmark-inc/bitmarkd/configuration"
"github.com/bitmark-inc/bitmarkd/util"
"github.com/bitmark-inc/logger"
"os"
"path/filepath"
"runtime"
"strings"
)
// basic defaults (directories and files are relative to the "DataDirectory" from Configuration file)
const (
defaultDataDirectory = "" // this will error; use "." for the same directory as the config file
defaultPublicKeyFile = "recorderd.private"
defaultPrivateKeyFile = "recorderd.public"
defaultLogDirectory = "log"
defaultLogFile = "recorderd.log"
defaultLogCount = 10 // number of log files retained
defaultLogSize = 1024 * 1024 // rotate when <logfile> exceeds this size
)
// to hold log levels
type LoglevelMap map[string]string
// path expanded or calculated defaults
var (
defaultLogLevels = LoglevelMap{
logger.DefaultTag: "critical",
}
)
// server public key identification in Z85 (ZeroMQ Base-85 Encoding) see: http://rfc.zeromq.org/spec:32
type Connection struct {
PublicKey string `libucl:"public_key" json:"public_key"`
Blocks string `libucl:"blocks" json:"blocks"`
Submit string `libucl:"submit" json:"submit"`
}
// client keys in Z85 (ZeroMQ Base-85 Encoding) see: http://rfc.zeromq.org/spec:32
type PeerType struct {
PrivateKey string `libucl:"private_key" json:"private_key"`
PublicKey string `libucl:"public_key" json:"public_key"`
Connect []Connection `libucl:"connect" json:"connect"`
}
// type PaymentType struct {
// Account ???? // separate private key field??
// Currency string `libucl:"currency" json:"currency"`
// Address string `libucl:"address" json:"address"`
// //Fee string `libucl:"fee" json:"fee"` // ***** FIX THIS: can miner set its fee(s)
// }
// add to configuration:
// //Payment PaymentType `libucl:"payment" json:"payment"`
type Configuration struct {
DataDirectory string `libucl:"data_directory" json:"data_directory"`
PidFile string `libucl:"pidfile" json:"pidfile"`
Chain string `libucl:"chain" json:"chain"`
Threads int `libucl:"threads" json:"threads"`
Peering PeerType `libucl:"peering" json:"peering"`
Logging logger.Configuration `libucl:"logging" json:"logging"`
}
// will read decode and verify the configuration
func getConfiguration(configurationFileName string, variables map[string]string) (*Configuration, error) {
configurationFileName, err := filepath.Abs(filepath.Clean(configurationFileName))
if nil != err {
return nil, err
}
// absolute path to the main directory
dataDirectory, _ := filepath.Split(configurationFileName)
options := &Configuration{
DataDirectory: defaultDataDirectory,
PidFile: "", // no PidFile by default
Chain: chain.Bitmark,
Threads: 0,
Peering: PeerType{},
Logging: logger.Configuration{
Directory: defaultLogDirectory,
File: defaultLogFile,
Size: defaultLogSize,
Count: defaultLogCount,
Levels: defaultLogLevels,
},
}
if err := configuration.ParseConfigurationFile(configurationFileName, options, variables); err != nil {
return nil, err
}
// if any test mode and the database file was not specified
// switch to appropriate default. Abort if then chain name is
// not recognised.
options.Chain = strings.ToLower(options.Chain)
if !chain.Valid(options.Chain) {
return nil, errors.New(fmt.Sprintf("Chain: %q is not supported", options.Chain))
}
// if threads invalid set number of CPUs
if options.Threads <= 0 {
options.Threads = runtime.NumCPU()
}
// ensure absolute data directory
if "" == options.DataDirectory || "~" == options.DataDirectory {
return nil, errors.New(fmt.Sprintf("Path: %q is not a valid directory", options.DataDirectory))
} else if "." == options.DataDirectory {
options.DataDirectory = dataDirectory // same directory as the configuration file
}
options.DataDirectory = filepath.Clean(options.DataDirectory)
// this directory must exist - i.e. must be created prior to running
if fileInfo, err := os.Stat(options.DataDirectory); nil != err {
return nil, err
} else if !fileInfo.IsDir() {
return nil, errors.New(fmt.Sprintf("Path: %q is not a directory", options.DataDirectory))
}
// force all relevant items to be absolute paths
// if not, assign them to the data directory
mustBeAbsolute := []*string{
&options.Peering.PublicKey,
&options.Peering.PrivateKey,
&options.Logging.Directory,
}
for _, f := range mustBeAbsolute {
*f = util.EnsureAbsolute(options.DataDirectory, *f)
}
// optional absolute paths i.e. blank or an absolute path
optionalAbsolute := []*string{
&options.PidFile,
}
for _, f := range optionalAbsolute {
if "" != *f {
*f = util.EnsureAbsolute(options.DataDirectory, *f)
}
}
// fail if any of these are not simple file names i.e. must
// not contain path seperator, then add the correct directory
// prefix, file item is first and corresponding directory is
// second (or nil if no prefix can be added)
mustNotBePaths := [][2]*string{
{&options.Logging.File, nil},
}
for _, f := range mustNotBePaths {
switch filepath.Dir(*f[0]) {
case "", ".":
if nil != f[1] {
*f[0] = util.EnsureAbsolute(*f[1], *f[0])
}
default:
return nil, errors.New(fmt.Sprintf("Files: %q is not plain name", *f[0]))
}
}
// make absolute and create directories if they do not already exist
for _, d := range []*string{
&options.Logging.Directory,
} {
*d = util.EnsureAbsolute(options.DataDirectory, *d)
if err := os.MkdirAll(*d, 0700); nil != err {
return nil, err
}
}
// done
return options, nil
}