-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfigure.go
More file actions
334 lines (278 loc) · 9.51 KB
/
configure.go
File metadata and controls
334 lines (278 loc) · 9.51 KB
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package main
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"math/big"
"os"
"path/filepath"
"time"
"github.com/rs/zerolog/log"
)
type ConfigureCmd struct {
EnvDir string `help:"Directory to create the environment in. default: './env'" short:"d"`
Name string `help:"Name of the environment to create. default: 'default'" short:"n"`
}
type configGenFunc func(cfg EnvironmentConfig, path string) error
func (c *ConfigureCmd) Run() error {
rc := RuntimeConfigFromFlags(c.EnvDir, c.Name)
envPath := filepath.Join(rc.EnvDir, rc.EnvName)
configPath := filepath.Join(envPath, "values.yaml")
// Load the global configuration
cfg, err := LoadEnvironmentConfig(configPath)
if err != nil {
return fmt.Errorf("failed to load config: %w", err)
}
if err := configureEnv(cfg, envPath); err != nil {
return fmt.Errorf("failed to configure environment: %w", err)
}
log.Info().Msg("Configuration files generated successfully")
return nil
}
func createLogDirectories(envDir string) error {
logDirs := []string{
filepath.Join(envDir, "logs"),
filepath.Join(envDir, "logs", "cloudserver"),
filepath.Join(envDir, "logs", "scuba"),
filepath.Join(envDir, "logs", "backbeat"),
filepath.Join(envDir, "logs", "migration-tools"),
filepath.Join(envDir, "logs", "clickhouse-shard-1"),
filepath.Join(envDir, "logs", "clickhouse-shard-2"),
filepath.Join(envDir, "logs", "fluentbit"),
}
for _, dir := range logDirs {
if err := os.MkdirAll(dir, 0777); err != nil {
return fmt.Errorf("failed to create log directory %s: %w", dir, err)
}
}
return nil
}
func configureEnv(cfg EnvironmentConfig, envDir string) error {
log.Info().Msgf("Configuring environment %s", envDir)
if err := createLogDirectories(envDir); err != nil {
return fmt.Errorf("failed to create log directories: %w", err)
}
if err := generateDefaultsEnv(cfg, envDir); err != nil {
return fmt.Errorf("failed to generate defaults.env: %w", err)
}
components := []configGenFunc{
generateCloudserverConfig,
generateBackbeatConfig,
generateVaultConfig,
generateScubaConfig,
generateS3MetadataConfig,
generateScubaMetadataConfig,
generateKafkaConfig,
generateUtapiConfig,
generateMigrationToolsConfig,
generateClickhouseConfig,
generateFluentbitConfig,
generateNginxConfig,
}
configDir := filepath.Join(envDir, "config")
// Create output directory if it doesn't exist
if err := os.MkdirAll(configDir, 0755); err != nil {
return fmt.Errorf("failed to create output directory: %w", err)
}
for _, component := range components {
if err := component(cfg, configDir); err != nil {
return fmt.Errorf("failed to generate config: %w", err)
}
}
return nil
}
func generateDefaultsEnv(cfg EnvironmentConfig, envDir string) error {
defaultsEnvPath := filepath.Join(envDir, "defaults.env")
return renderTemplateToFile(getTemplates(), "templates/global/defaults.env", cfg, defaultsEnvPath)
}
func generateCloudserverConfig(cfg EnvironmentConfig, path string) error {
version := detectCloudserverVersion(cfg.Cloudserver.Image)
configTemplate := fmt.Sprintf("templates/cloudserver/config-%s.json", version)
if f, err := getTemplates().Open(configTemplate); err != nil {
return fmt.Errorf("no configuration template found for cloudserver version %s (image: %s): %w",
version, cfg.Cloudserver.Image, err)
} else {
if closeErr := f.Close(); closeErr != nil {
return fmt.Errorf("failed to close template file: %w", closeErr)
}
}
err := renderTemplateToFile(
getTemplates(),
configTemplate,
cfg,
filepath.Join(path, "cloudserver", "config.json"),
)
if err != nil {
return err
}
return renderTemplateToFile(
getTemplates(),
"templates/cloudserver/locationConfig.json",
cfg,
filepath.Join(path, "cloudserver", "locationConfig.json"),
)
}
func generateBackbeatConfig(cfg EnvironmentConfig, path string) error {
templates := []string{
"env",
"supervisord.conf",
"config.json",
"config.notification.json",
"notificationCredentials.json",
}
return renderTemplates(cfg, "templates/backbeat", filepath.Join(path, "backbeat"), templates)
}
func generateVaultConfig(cfg EnvironmentConfig, path string) error {
templates := []string{
"config.json",
"create-management-account.sh",
"Dockerfile.setup",
"management-creds.json",
}
return renderTemplates(cfg, "templates/vault", filepath.Join(path, "vault"), templates)
}
func generateScubaConfig(cfg EnvironmentConfig, path string) error {
templates := []string{
"config.json",
"create-service-user.sh",
"Dockerfile.setup",
"supervisord.conf",
"env",
}
return renderTemplates(cfg, "templates/scuba", filepath.Join(path, "scuba"), templates)
}
func generateMetadataConfig(cfg MetadataConfig, path string) error {
return renderTemplateToFile(getTemplates(), "templates/metadata/config.json", cfg, filepath.Join(path, "config.json"))
}
func generateS3MetadataConfig(cfg EnvironmentConfig, path string) error {
cfgPath := filepath.Join(path, "metadata-s3")
return generateMetadataConfig(cfg.S3Metadata, cfgPath)
}
func generateScubaMetadataConfig(cfg EnvironmentConfig, path string) error {
cfgPath := filepath.Join(path, "metadata-scuba")
return generateMetadataConfig(cfg.ScubaMetadata, cfgPath)
}
func generateKafkaConfig(cfg EnvironmentConfig, path string) error {
templates := []string{
"Dockerfile",
"setup.sh",
"server.backbeat.properties",
"server.destination.properties",
"config.properties",
"zookeeper.properties",
}
return renderTemplates(cfg, "templates/kafka", filepath.Join(path, "kafka"), templates)
}
func generateUtapiConfig(cfg EnvironmentConfig, path string) error {
return renderTemplateToFile(getTemplates(), "templates/utapi/config.json", cfg, filepath.Join(path, "utapi", "config.json"))
}
func generateMigrationToolsConfig(cfg EnvironmentConfig, path string) error {
templates := []string{
"supervisord.conf",
"migration.yml",
"env",
}
return renderTemplates(cfg, "templates/migration-tools", filepath.Join(path, "migration-tools"), templates)
}
func generateClickhouseConfig(cfg EnvironmentConfig, path string) error {
templates := []string{
"Dockerfile.shard",
"Dockerfile.setup",
"entrypoint.sh",
"cluster-config.xml",
"ports-shard-1.xml",
"ports-shard-2.xml",
"init-schema.sh",
"init.d/01-create-database.sql",
"init.d/02-create-ingest-table.sql",
"init.d/03-create-storage-table.sql",
"init.d/04-create-offsets-table.sql",
"init.d/05-create-distributed-tables.sql",
"init.d/06-create-materialized-view.sql",
}
return renderTemplates(cfg, "templates/clickhouse", filepath.Join(path, "clickhouse"), templates)
}
func generateFluentbitConfig(cfg EnvironmentConfig, path string) error {
templates := []string{
"fluent-bit.conf",
"parsers.conf",
}
return renderTemplates(cfg, "templates/fluentbit", filepath.Join(path, "fluentbit"), templates)
}
func generateNginxConfig(cfg EnvironmentConfig, path string) error {
if !cfg.Features.S3Frontend.Enabled {
return nil
}
nginxDir := filepath.Join(path, "nginx")
if err := renderTemplateToFile(
getTemplates(),
"templates/nginx/nginx.conf",
cfg,
filepath.Join(nginxDir, "nginx.conf"),
); err != nil {
return err
}
return generateTLSCertificate(nginxDir, "s3-frontend.key", "s3-frontend.crt")
}
// generateTLSCertificate creates a self-signed TLS certificate and key pair.
func generateTLSCertificate(dir, keyName, certName string) error {
keyPath := filepath.Join(dir, keyName)
certPath := filepath.Join(dir, certName)
// Skip if both files already exist
_, keyErr := os.Stat(keyPath)
_, certErr := os.Stat(certPath)
if keyErr == nil && certErr == nil {
log.Debug().Str("dir", dir).Msg("TLS certificate already exists, skipping generation")
return nil
}
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return fmt.Errorf("failed to generate TLS key: %w", err)
}
serialNumber, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 128))
if err != nil {
return fmt.Errorf("failed to generate serial number: %w", err)
}
template := x509.Certificate{
SerialNumber: serialNumber,
Subject: pkix.Name{
Organization: []string{"Scality Workbench"},
},
DNSNames: []string{"localhost", "s3.docker.test"},
NotBefore: time.Now(),
NotAfter: time.Now().Add(10 * 365 * 24 * time.Hour),
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
}
certDER, err := x509.CreateCertificate(rand.Reader, &template, &template, &key.PublicKey, key)
if err != nil {
return fmt.Errorf("failed to create TLS certificate: %w", err)
}
certFile, err := os.Create(certPath)
if err != nil {
return fmt.Errorf("failed to create cert file: %w", err)
}
defer func() { _ = certFile.Close() }()
if err := pem.Encode(certFile, &pem.Block{Type: "CERTIFICATE", Bytes: certDER}); err != nil {
return fmt.Errorf("failed to write TLS certificate: %w", err)
}
keyDER, err := x509.MarshalECPrivateKey(key)
if err != nil {
return fmt.Errorf("failed to marshal TLS key: %w", err)
}
keyFile, err := os.OpenFile(keyPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return fmt.Errorf("failed to create key file: %w", err)
}
defer func() { _ = keyFile.Close() }()
if err := pem.Encode(keyFile, &pem.Block{Type: "EC PRIVATE KEY", Bytes: keyDER}); err != nil {
return fmt.Errorf("failed to write TLS key: %w", err)
}
log.Info().Str("dir", dir).Msg("Generated self-signed TLS certificate")
return nil
}