forked from cloudpipe/cloudpipe
/
context.go
223 lines (183 loc) · 4.86 KB
/
context.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
package main
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"net/http"
"os"
"path"
log "github.com/Sirupsen/logrus"
docker "github.com/fsouza/go-dockerclient"
"github.com/kelseyhightower/envconfig"
)
// Context provides shared state among individual route handlers.
type Context struct {
// Configuration settings from the environment.
Settings
// Service facades.
Storage
Docker
// Shared clients.
HTTPS *http.Client
AuthService AuthService
}
// Settings contains configuration options loaded from the environment.
type Settings struct {
Port int
LogLevel string
LogColors bool
MongoURL string
AdminName string
AdminKey string
DockerHost string
DockerTLS bool
CACert string
Cert string
Key string
DefaultImage string
Poll int
AuthService string
}
// NewContext loads the active configuration and applies any immediate, global settings like the
// logging level.
func NewContext() (*Context, error) {
c := &Context{}
if err := c.Load(); err != nil {
return c, err
}
// Configure the logging level and formatter.
level, err := log.ParseLevel(c.LogLevel)
if err != nil {
return c, err
}
log.SetLevel(level)
log.SetFormatter(&log.TextFormatter{
ForceColors: c.LogColors,
})
// Summarize the loaded settings.
log.WithFields(log.Fields{
"port": c.Port,
"logging level": c.LogLevel,
"log with color": c.LogColors,
"mongo URL": c.MongoURL,
"admin account": c.AdminName,
"docker host": c.DockerHost,
"docker TLS enabled": c.DockerTLS,
"CA cert": c.CACert,
"cert": c.Cert,
"key": c.Key,
"default image": c.DefaultImage,
"polling interval": c.Poll,
"auth service": c.Settings.AuthService,
}).Info("Initializing with loaded settings.")
// Configure a HTTP(S) client to use the provided TLS credentials.
caCertPool := x509.NewCertPool()
caCertPEM, err := ioutil.ReadFile(c.CACert)
if err != nil {
log.Debug("Hint: if you're running in dev mode, try running script/genkeys first.")
return nil, fmt.Errorf("unable to load CA certificate: %v", err)
}
caCertPool.AppendCertsFromPEM(caCertPEM)
keypair, err := tls.LoadX509KeyPair(c.Cert, c.Key)
if err != nil {
return nil, fmt.Errorf("unable to load TLS keypair: %v", err)
}
tlsConfig := &tls.Config{
RootCAs: caCertPool,
Certificates: []tls.Certificate{keypair},
MinVersion: tls.VersionTLS10,
InsecureSkipVerify: false,
}
transport := &http.Transport{TLSClientConfig: tlsConfig}
c.HTTPS = &http.Client{Transport: transport}
// Connect to MongoDB.
c.Storage, err = NewMongoStorage(c)
if err != nil {
return c, err
}
if err := c.Storage.Bootstrap(); err != nil {
return c, err
}
// Connect to Docker.
if c.DockerTLS {
c.Docker, err = docker.NewTLSClient(c.DockerHost, c.Cert, c.Key, c.CACert)
if err != nil {
log.WithFields(log.Fields{
"docker host": c.DockerHost,
}).Fatal("Unable to connect to Docker with TLS.")
return c, err
}
} else {
c.Docker, err = docker.NewClient(c.DockerHost)
if err != nil {
log.WithFields(log.Fields{
"docker host": c.DockerHost,
"error": err,
}).Error("Unable to connect to Docker.")
return c, err
}
}
// Initialize an appropriate authentication service.
c.AuthService, err = ConnectToAuthService(c, c.Settings.AuthService)
if err != nil {
log.WithFields(log.Fields{
"auth service url": c.Settings.AuthService,
"error": err,
}).Error("Unable to connect to authentication service.")
return c, err
}
return c, nil
}
// Load configuration settings from the environment, apply defaults, and validate them.
func (c *Context) Load() error {
if err := envconfig.Process("PIPE", &c.Settings); err != nil {
return err
}
if c.Port == 0 {
c.Port = 8000
}
if c.LogLevel == "" {
c.LogLevel = "info"
}
if c.MongoURL == "" {
c.MongoURL = "mongo"
}
if c.Poll == 0 {
c.Poll = 500
}
if c.DockerHost == "" {
if host := os.Getenv("DOCKER_HOST"); host != "" {
c.DockerHost = host
} else {
c.DockerHost = "unix:///var/run/docker.sock"
}
}
certRoot := os.Getenv("DOCKER_CERT_PATH")
if certRoot == "" {
certRoot = "/certificates"
}
if c.CACert == "" {
c.CACert = path.Join(certRoot, "ca.pem")
}
if c.Cert == "" {
c.Cert = path.Join(certRoot, "cloudpipe-cert.pem")
}
if c.Key == "" {
c.Key = path.Join(certRoot, "cloudpipe-key.pem")
}
if c.DefaultImage == "" {
c.DefaultImage = "cloudpipe/runner-py2"
}
if c.Settings.AuthService == "" {
c.Settings.AuthService = "https://authstore:9001/v1"
}
if _, err := log.ParseLevel(c.LogLevel); err != nil {
return err
}
return nil
}
// ListenAddr generates an address to bind the net/http server to based on the current settings.
func (c *Context) ListenAddr() string {
return fmt.Sprintf(":%d", c.Port)
}