-
Notifications
You must be signed in to change notification settings - Fork 106
/
config.go
316 lines (264 loc) · 7.01 KB
/
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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package codetainer
import (
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"io/ioutil"
"os"
"os/user"
"path"
"path/filepath"
"strings"
"github.com/BurntSushi/toml"
docker "github.com/fsouza/go-dockerclient"
version "github.com/hashicorp/go-version"
"github.com/mitchellh/go-homedir"
)
var globalConfigPath string = "/etc/codetainer/config.toml"
var globalDbPath string = "/var/codetainer/codetainer.db"
var (
DefaultConfigFileSettings = `# Docker API server and port
DockerServer = "localhost"
DockerPort = 4500`
GlobalConfig Config
)
//
// detectConfigPath will return the path to the configuration file.
// Use either a global path: /etc/codetainer/config.toml
// Or a local path ~/.codetainer/config.toml
//
func detectConfigPath() (string, error) {
if fileExists(globalConfigPath) {
return globalConfigPath, nil
}
usr, err := user.Current()
if err != nil {
return "", err
}
basePath := path.Join(usr.HomeDir, ".codetainer")
if _, err := os.Stat(basePath); err != nil {
if os.IsNotExist(err) {
err := os.MkdirAll(basePath, 0700)
if err != nil {
return "", err
}
}
}
return path.Join(basePath, "config.toml"), nil
}
// detectDataabsePath will return the path to the database file.
// Use either a global path: /etc/codetainer/codetainer.db
// Or a local path ~/.codetainer/codetainer.db
func detectDatabasePath() (string, error) {
if fileExists(globalDbPath) {
return globalDbPath, nil
}
usr, err := user.Current()
if err != nil {
return "", err
}
basePath := path.Join(usr.HomeDir, ".codetainer")
if _, err := os.Stat(basePath); err != nil {
if os.IsNotExist(err) {
err := os.MkdirAll(basePath, 0700)
if err != nil {
return "", err
}
} else {
return "", err
}
}
return path.Join(basePath, "codetainer.db"), nil
}
type Config struct {
DockerServerUseHttps bool
DockerServer string
DockerPort int
DockerCertPath string
DatabasePath string
database *Database
currentDockerApiVersion string
tlsConfig *tls.Config
}
func (c *Config) Url() string {
// TODO: make this configurable
return "http://localhost:3000"
}
func (c *Config) GetDatabase() (*Database, error) {
// TODO cache db
if c.database != nil {
return c.database, nil
}
db, err := NewDatabase(c.GetDatabasePath())
if err != nil {
return nil, err
}
c.database = db
return c.database, nil
}
func (c *Config) GetDatabasePath() string {
if c.DatabasePath == "" {
p, err := detectDatabasePath()
c.DatabasePath = p
if err != nil {
Log.Fatal("Unable to create database at ~/.codetainer/codetainer.db or "+globalDbPath, err)
}
}
Log.Debugf("Using database path: %s", c.DatabasePath)
return c.DatabasePath
}
func (c *Config) UtilsPath() string {
dir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
return path.Join(dir, "util")
}
func (c *Config) GetDockerClient() (*docker.Client, error) {
endpoint := c.GetDockerEndpoint()
if !c.DockerServerUseHttps {
return docker.NewClient(endpoint)
}
cert, key, ca := c.certFilePaths()
return docker.NewTLSClient(endpoint, cert, key, ca)
}
func (c *Config) testDockerClient() error {
endpoint, err := c.GetDockerClient()
if err != nil {
return err
}
return endpoint.Ping()
}
func (c *Config) testDockerVersion() error {
endpoint, err := c.GetDockerClient()
if err != nil {
return err
}
ev, err := endpoint.Version()
if err != nil {
return err
}
currVersion := ev.Get("ApiVersion")
activeVersion, err := version.NewVersion(currVersion)
supportedVersion, err := version.NewVersion(DockerApiVersion)
if activeVersion.LessThan(supportedVersion) {
return errors.New(currVersion + " version is lower than supported Docker version of " + DockerApiVersion + ". You will need to upgrade docker.")
}
Log.Debug("Found docker API version: ", currVersion)
c.currentDockerApiVersion = currVersion
return nil
}
func (c *Config) GetDockerEndpoint() string {
if c.DockerServerUseHttps {
return fmt.Sprintf("https://%s:%d", c.DockerServer, c.DockerPort)
} else {
return fmt.Sprintf("http://%s:%d", c.DockerServer, c.DockerPort)
}
}
//
// Ensure a configuration is valid and all dependencies are installed.
//
func (c *Config) TestConfig() bool {
err := c.testDockerClient()
if err != nil {
Log.Fatal(`Unable to connect to Docker API. Are you sure you have
configured the Docker API to accept remote HTTP connections?
E.g., your docker service needs to have the following parameters in the
command line in order to use web sockets:
/usr/bin/docker daemon -H tcp://127.0.0.1:4500
Please also check your config.toml has the correct configuration for the DockerServer
and DockerPort:
# Docker API server and port
DockerServer = "localhost"
DockerPort = 4500
`)
}
err = c.testDockerVersion()
if err != nil {
Log.Fatal(err)
}
return true
}
func (c *Config) certFilePaths() (cert, key, ca string) {
certPath := c.DockerCertPath
// expand if the path starts with "~"
if strings.HasPrefix(certPath, "~") {
home, err := homedir.Dir()
if err != nil {
return "", "", ""
}
certPath = filepath.Join(home, certPath[1:])
}
cert = filepath.Join(certPath, "cert.pem")
key = filepath.Join(certPath, "key.pem")
ca = filepath.Join(certPath, "ca.pem")
return cert, key, ca
}
func (c *Config) setTLSConfig() error {
cert, key, ca := c.certFilePaths()
certPEMBlock, err := ioutil.ReadFile(cert)
if err != nil {
return err
}
keyPEMBlock, err := ioutil.ReadFile(key)
if err != nil {
return err
}
caPEMCert, err := ioutil.ReadFile(ca)
if err != nil {
return err
}
if certPEMBlock == nil || keyPEMBlock == nil {
return errors.New("Both cert and key are required")
}
tlsCert, err := tls.X509KeyPair(certPEMBlock, keyPEMBlock)
if err != nil {
return err
}
tlsConfig := &tls.Config{Certificates: []tls.Certificate{tlsCert}}
if caPEMCert == nil {
tlsConfig.InsecureSkipVerify = true
} else {
caPool := x509.NewCertPool()
if !caPool.AppendCertsFromPEM(caPEMCert) {
return errors.New("Could not add RootCA pem")
}
tlsConfig.RootCAs = caPool
}
c.tlsConfig = tlsConfig
return nil
}
func NewConfig(configPath string) (*Config, error) {
var err error
if configPath == "" {
configPath, err = detectConfigPath()
if err != nil {
Log.Fatal("Unable to load config from ~/.codetainer/config.toml or /etc/codetainer/config.toml", err)
}
}
Log.Debugf("Loading %s configurations from %s", Name, configPath)
config := &Config{}
if !IsExist(configPath) {
configData := []byte(DefaultConfigFileSettings)
f, err := os.Create(configPath)
if err != nil {
Log.Error(err)
Log.Fatalf("Unable to create configuration file: %s.", configPath)
}
_, err = f.Write(configData)
if err != nil {
Log.Error(err)
Log.Fatalf("Unable to create configuration file: %s.", configPath)
}
f.Sync()
f.Close()
}
if _, err := toml.DecodeFile(configPath, &config); err != nil {
return config, err
}
if config.DockerServerUseHttps {
// read certificate files and hold it
if err := config.setTLSConfig(); err != nil {
return config, err
}
}
return config, nil
}