-
Notifications
You must be signed in to change notification settings - Fork 50
/
server_config.go
130 lines (111 loc) · 2.91 KB
/
server_config.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
package server
import (
"bytes"
"fmt"
"os"
"strconv"
log "github.com/Sirupsen/logrus"
"github.com/akutz/gofig"
apiconfig "github.com/emccode/libstorage/api/utils/config"
)
var (
tlsPath = fmt.Sprintf(
"%s/src/github.com/emccode/libstorage/.tls", os.Getenv("GOPATH"))
serverCrt = fmt.Sprintf("%s/libstorage-server.crt", tlsPath)
serverKey = fmt.Sprintf("%s/libstorage-server.key", tlsPath)
clientCrt = fmt.Sprintf("%s/libstorage-client.crt", tlsPath)
clientKey = fmt.Sprintf("%s/libstorage-client.key", tlsPath)
trustedCerts = fmt.Sprintf("%s/libstorage-ca.crt", tlsPath)
)
const (
/*
libStorageConfigBase is the base config for tests
01 - the host address to server and which the client uses
02 - the executors directory
03 - the client TLS section. use an empty string if TLS is disabled
04 - the server TLS section. use an empty string if TLS is disabled
05 - the services
*/
libStorageConfigBase = `
libstorage:
host: %[1]s
profiles:
enabled: true
groups:
- local=127.0.0.1%[3]s
server:
endpoints:
localhost:
address: %[1]s%[4]s
services:%[5]s
`
libStorageConfigService = `
%[1]s:
libstorage:
storage:
driver: %[2]s
`
libStorageConfigClientTLS = `
tls:
serverName: libstorage-server
certFile: %s
keyFile: %s
trustedCertsFile: %s
`
libStorageConfigServerTLS = `
tls:
serverName: libstorage-server
certFile: %s
keyFile: %s
trustedCertsFile: %s
clientCertRequired: true
`
)
// NewConfig returns a confiburation object from basic inputs
func NewConfig(
host string, tls bool, driversAndServices ...string) gofig.Config {
if host == "" {
host = "tcp://127.0.0.1:7979"
}
config, err := apiconfig.NewConfig()
if err != nil {
panic(err)
}
if debug, _ := strconv.ParseBool(os.Getenv("LIBSTORAGE_DEBUG")); debug {
log.SetLevel(log.DebugLevel)
config.ReadConfig(bytes.NewReader([]byte(`libstorage:
logging:
httpRequests: true
httpResponses: true`)))
}
var clientTLS, serverTLS string
if tls {
clientTLS = fmt.Sprintf(
libStorageConfigClientTLS,
clientCrt, clientKey, trustedCerts)
serverTLS = fmt.Sprintf(
libStorageConfigServerTLS,
serverCrt, serverKey, trustedCerts)
}
services := &bytes.Buffer{}
for i := 0; i < len(driversAndServices); i = i + 2 {
driverName := driversAndServices[i]
serviceName := driverName
if (i + 1) < len(driversAndServices) {
serviceName = driversAndServices[i+1]
}
services.WriteString(
fmt.Sprintf(libStorageConfigService, serviceName, driverName))
}
configYaml := fmt.Sprintf(
libStorageConfigBase,
host, "/tmp/libstorage/executors",
clientTLS, serverTLS,
services.String())
log.Debug(configYaml)
configYamlBuf := []byte(configYaml)
if err := config.ReadConfig(bytes.NewReader(configYamlBuf)); err != nil {
panic(err)
}
return config
}