Skip to content

Commit 28311d9

Browse files
committed
Made config more flexible
1 parent dc797e8 commit 28311d9

File tree

2 files changed

+90
-8
lines changed

2 files changed

+90
-8
lines changed

service/arangodb.go

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ var confFileTemplate = `# ArangoDB configuration file
168168
[server]
169169
endpoint = tcp://0.0.0.0:%s
170170
threads = %d
171-
authentication = false
171+
%s
172172
173173
[log]
174174
level = %s
@@ -190,20 +190,51 @@ func (s *Service) makeBaseArgs(myHostDir, myContainerDir string, myAddress strin
190190
}
191191

192192
if _, err := os.Stat(hostConfFileName); os.IsNotExist(err) {
193-
out, e := os.Create(hostConfFileName)
194-
if e != nil {
195-
s.log.Fatalf("Could not create configuration file %s, error: %#v", hostConfFileName, e)
196-
}
193+
var threads, v8Contexts string
194+
logLevel := "INFO"
197195
switch mode {
198196
// Parameters are: port, server threads, log level, v8-contexts
199197
case "agent":
200-
fmt.Fprintf(out, confFileTemplate, myPort, 8, "INFO", 1)
198+
threads = "8"
199+
v8Contexts = "1"
201200
case "dbserver":
202-
fmt.Fprintf(out, confFileTemplate, myPort, 4, "INFO", 4)
201+
threads = "4"
202+
v8Contexts = "4"
203203
case "coordinator":
204-
fmt.Fprintf(out, confFileTemplate, myPort, 16, "INFO", 4)
204+
threads = "16"
205+
v8Contexts = "4"
206+
}
207+
config := configFile{
208+
&configSection{
209+
Name: "server",
210+
Settings: map[string]string{
211+
"endpoint": fmt.Sprintf("tcp://0.0.0.0:%s", myPort),
212+
"threads": threads,
213+
"authentication": "false",
214+
},
215+
},
216+
&configSection{
217+
Name: "log",
218+
Settings: map[string]string{
219+
"level": logLevel,
220+
},
221+
},
222+
&configSection{
223+
Name: "javascript",
224+
Settings: map[string]string{
225+
"v8-contexts": v8Contexts,
226+
},
227+
},
205228
}
229+
out, e := os.Create(hostConfFileName)
230+
if e != nil {
231+
s.log.Fatalf("Could not create configuration file %s, error: %#v", hostConfFileName, e)
232+
}
233+
_, err := config.WriteTo(out)
206234
out.Close()
235+
if err != nil {
236+
s.log.Fatalf("Cannot create config file: %v", err)
237+
}
207238
}
208239
args = make([]string, 0, 40)
209240
executable := s.ArangodExecutable

service/config.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package service
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"strings"
7+
)
8+
9+
var confHeader = `# ArangoDB configuration file
10+
#
11+
# Documentation:
12+
# https://docs.arangodb.com/Manual/Administration/Configuration/
13+
#
14+
15+
`
16+
17+
type configFile []*configSection
18+
19+
// WriteTo writes the configuration sections to the given writer.
20+
func (cf configFile) WriteTo(w io.Writer) (int64, error) {
21+
x := int64(0)
22+
n, err := w.Write([]byte(confHeader))
23+
if err != nil {
24+
return x, maskAny(err)
25+
}
26+
x += int64(n)
27+
for _, section := range cf {
28+
n, err := section.WriteTo(w)
29+
if err != nil {
30+
return x, maskAny(err)
31+
}
32+
x += int64(n)
33+
}
34+
return x, nil
35+
}
36+
37+
type configSection struct {
38+
Name string
39+
Settings map[string]string
40+
}
41+
42+
// WriteTo writes the configuration section to the given writer.
43+
func (s *configSection) WriteTo(w io.Writer) (int64, error) {
44+
lines := []string{"[" + s.Name + "]"}
45+
for k, v := range s.Settings {
46+
lines = append(lines, fmt.Sprintf("%s = %s", k, v))
47+
}
48+
lines = append(lines, "")
49+
n, err := w.Write([]byte(strings.Join(lines, "\n")))
50+
return int64(n), maskAny(err)
51+
}

0 commit comments

Comments
 (0)