/
couchdbcatalog.go
432 lines (361 loc) · 13.4 KB
/
couchdbcatalog.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
package couchdbcatalog
import (
"crypto/sha1"
"encoding/hex"
"errors"
"fmt"
"strconv"
"strings"
"golang.org/x/crypto/pbkdf2"
"github.com/cloudstax/firecamp/catalog"
"github.com/cloudstax/firecamp/common"
"github.com/cloudstax/firecamp/containersvc"
"github.com/cloudstax/firecamp/dns"
"github.com/cloudstax/firecamp/log"
"github.com/cloudstax/firecamp/manage"
"github.com/cloudstax/firecamp/utils"
)
const (
defaultVersion = "2.1"
// ContainerImage is the main running container.
ContainerImage = common.ContainerNamePrefix + "couchdb:" + defaultVersion
// InitContainerImage initializes the couchdb cluster.
InitContainerImage = common.ContainerNamePrefix + "couchdb-init:" + defaultVersion
// the application access port
httpPort = 5984
sslPort = 6984
// cluster internal ports
localPort = 5986
erlangClusterPort = 4369
erlangClusterListenPortMin = 9100
// couchdb cluster setup suggests tcp range 9100-9200. It would not actually use 100 ports.
//
// AWS ECS allows up to 100 ports at a time for one container. If you map more than 100 ports,
// ECS will not be able to place the task.
// http://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html
//
// 9200 is used by ElasticSearch, and 9160 is used by Cassandra.
// Set the max port as 9159. 59 ports would be enough for couchdb. If not, we could find another range.
erlangClusterListenPortMax = 9159
defaultShards = 8
defaultReadCopies = 2
defaultWriteCopies = 2
passwdSha1OutputLen = 20
passwdIter = 10
vmArgsConfFileName = "vm.args"
localIniConfFileName = "local.ini"
privateKeyConfFileName = "privkey.pem"
certConfFileName = "couchdb.pem"
caCertConfFileName = "ca-certificates.crt"
)
// ValidateRequest checks if the request is valid
func ValidateRequest(r *manage.CatalogCreateCouchDBRequest) error {
if r.Options.Replicas == 2 {
return errors.New("invalid replicas, please create 1, 3 or more replicas")
}
if r.Options.EnableCors {
if r.Options.Credentials && r.Options.Origins == "*" {
return errors.New("can't set origins: * and credentials = true at the same time")
}
if len(r.Options.Origins) == 0 || len(r.Options.Headers) == 0 || len(r.Options.Methods) == 0 {
return errors.New("cors is enabled, please set the origins, headers and methods")
}
}
if r.Options.EnableSSL {
if len(r.Options.CertFileContent) == 0 || len(r.Options.KeyFileContent) == 0 {
return errors.New("ssl is enabled, please set the valid cert and key files")
}
}
return nil
}
// GenDefaultCreateServiceRequest returns the default service creation request.
func GenDefaultCreateServiceRequest(platform string, region string, azs []string, cluster string,
service string, res *common.Resources, opts *manage.CatalogCouchDBOptions) *manage.CreateServiceRequest {
// generate service configs
serviceCfgs := genServiceConfigs(platform, cluster, service, azs, opts)
// generate service ReplicaConfigs
replicaCfgs := genReplicaConfigs(platform, cluster, service, azs, opts)
portMappings := []common.PortMapping{
{ContainerPort: httpPort, HostPort: httpPort, IsServicePort: true},
{ContainerPort: sslPort, HostPort: sslPort, IsServicePort: true},
}
if opts.Replicas != int64(1) {
m := common.PortMapping{ContainerPort: localPort, HostPort: localPort}
portMappings = append(portMappings, m)
m = common.PortMapping{ContainerPort: erlangClusterPort, HostPort: erlangClusterPort}
portMappings = append(portMappings, m)
for i := int64(erlangClusterListenPortMin); i <= int64(erlangClusterListenPortMax); i++ {
m = common.PortMapping{ContainerPort: i, HostPort: i}
portMappings = append(portMappings, m)
}
}
req := &manage.CreateServiceRequest{
Service: &manage.ServiceCommonRequest{
Region: region,
Cluster: cluster,
ServiceName: service,
},
Resource: res,
ServiceType: common.ServiceTypeStateful,
CatalogServiceType: common.CatalogService_CouchDB,
ContainerImage: ContainerImage,
Replicas: opts.Replicas,
PortMappings: portMappings,
RegisterDNS: true,
ServiceConfigs: serviceCfgs,
Volume: opts.Volume,
ContainerPath: common.DefaultContainerMountPath,
ReplicaConfigs: replicaCfgs,
}
return req
}
// genServiceConfigs generates the service configs.
func genServiceConfigs(platform string, cluster string, service string, azs []string, opts *manage.CatalogCouchDBOptions) []*manage.ConfigFileContent {
uuid := utils.GenUUID()
// encrypt password
encryptedPasswd := encryptPasswd(opts.AdminPasswd)
hasCert := false
if len(opts.CACertFileContent) != 0 {
hasCert = true
}
// create the service.conf file
content := fmt.Sprintf(servicefileContent, platform, uuid, opts.Admin, opts.AdminPasswd, encryptedPasswd,
strconv.FormatBool(opts.EnableCors), strconv.FormatBool(opts.Credentials), opts.Origins, opts.Headers,
opts.Methods, strconv.FormatBool(opts.EnableSSL), strconv.FormatBool(hasCert))
serviceCfg := &manage.ConfigFileContent{
FileName: catalog.SERVICE_FILE_NAME,
FileMode: common.DefaultConfigFileMode,
Content: content,
}
// create vm.args file
content = fmt.Sprintf(vmArgsConfigs, erlangClusterListenPortMax)
vmArgsCfg := &manage.ConfigFileContent{
FileName: vmArgsConfFileName,
FileMode: common.DefaultConfigFileMode,
Content: content,
}
// create the local.ini file
couchContent := couchConfigs
// set cluster configs
if opts.Replicas >= int64(3) {
placement := ""
if len(azs) == 1 {
placement = fmt.Sprintf(zonePlacementFmt, azs[0], 3)
} else if len(azs) == 2 {
placement = fmt.Sprintf(zonePlacementFmt, azs[0], 2) + zonePlaceSep + fmt.Sprintf(zonePlacementFmt, azs[1], 1)
} else {
// len(azs) >= 3
placement = fmt.Sprintf(zonePlacementFmt, azs[0], 1) + zonePlaceSep + fmt.Sprintf(zonePlacementFmt, azs[1], 1) +
zonePlaceSep + fmt.Sprintf(zonePlacementFmt, azs[2], 1)
}
clusterContent := fmt.Sprintf(clusterConfigs, opts.Replicas, defaultReadCopies, defaultWriteCopies, placement)
couchContent += clusterContent
}
couchCfg := &manage.ConfigFileContent{
FileName: localIniConfFileName,
FileMode: common.DefaultConfigFileMode,
Content: couchContent,
}
configs := []*manage.ConfigFileContent{serviceCfg, vmArgsCfg, couchCfg}
// add ssl config files
if opts.EnableSSL {
certCfg := &manage.ConfigFileContent{
FileName: certConfFileName,
FileMode: common.ReadOnlyFileMode,
Content: opts.CertFileContent,
}
keyCfg := &manage.ConfigFileContent{
FileName: privateKeyConfFileName,
FileMode: common.ReadOnlyFileMode,
Content: opts.KeyFileContent,
}
configs = append(configs, certCfg, keyCfg)
if len(opts.CACertFileContent) != 0 {
caCfg := &manage.ConfigFileContent{
FileName: caCertConfFileName,
FileMode: common.ReadOnlyFileMode,
Content: opts.CACertFileContent,
}
configs = append(configs, caCfg)
}
}
return configs
}
// genReplicaConfigs generates the replica configs.
func genReplicaConfigs(platform string, cluster string, service string, azs []string, opts *manage.CatalogCouchDBOptions) []*manage.ReplicaConfig {
domain := dns.GenDefaultDomainName(cluster)
replicaCfgs := make([]*manage.ReplicaConfig, opts.Replicas)
for i := int64(0); i < opts.Replicas; i++ {
member := utils.GenServiceMemberName(service, i)
memberHost := dns.GenDNSName(member, domain)
// The CouchDB init task follows the same way to assign the az to each node.
// If the az selection algo is changed here, please also update the init task.
azIndex := int(i) % len(azs)
az := azs[azIndex]
// create the member.conf file
content := fmt.Sprintf(memberfileContent, az, member, memberHost)
memberCfg := &manage.ConfigFileContent{
FileName: catalog.MEMBER_FILE_NAME,
FileMode: common.DefaultConfigFileMode,
Content: content,
}
configs := []*manage.ConfigFileContent{memberCfg}
replicaCfg := &manage.ReplicaConfig{Zone: az, MemberName: member, Configs: configs}
replicaCfgs[i] = replicaCfg
}
return replicaCfgs
}
// couchdb encrypts password using pbkdf2, and store in local.ini with format -pbkdf2-EncryptedPasswd(160bits),entryptionSalt(128bits),Iteration(10)
// example: -pbkdf2-bd261f6d2d30500145a19c502220b02e250a77db,c995aadb9f35d8fc1e0fe9571a07e36b,10
// http://docs.couchdb.org/en/2.1.0/config/auth.html#admins
func encryptPasswd(passwd string) string {
salt := utils.GenUUID()
kd := pbkdf2.Key([]byte(passwd), []byte(salt), passwdIter, passwdSha1OutputLen, sha1.New)
return fmt.Sprintf(pbkdf2Fmt, hex.EncodeToString(kd), salt, passwdIter)
}
// GenDefaultInitTaskRequest returns the default service init task request.
func GenDefaultInitTaskRequest(req *manage.ServiceCommonRequest, logConfig *cloudlog.LogConfig, azs []string,
serviceUUID string, replicas int64, manageurl string, admin string, adminPass string) *containersvc.RunTaskOptions {
envkvs := GenInitTaskEnvKVPairs(req.Region, req.Cluster, manageurl, req.ServiceName, azs, replicas, admin, adminPass)
commonOpts := &containersvc.CommonOptions{
Cluster: req.Cluster,
ServiceName: req.ServiceName,
ServiceUUID: serviceUUID,
ContainerImage: InitContainerImage,
Resource: &common.Resources{
MaxCPUUnits: common.DefaultMaxCPUUnits,
ReserveCPUUnits: common.DefaultReserveCPUUnits,
MaxMemMB: common.DefaultMaxMemoryMB,
ReserveMemMB: common.DefaultReserveMemoryMB,
},
LogConfig: logConfig,
}
taskOpts := &containersvc.RunTaskOptions{
Common: commonOpts,
TaskType: common.TaskTypeInit,
Envkvs: envkvs,
}
return taskOpts
}
// GenInitTaskEnvKVPairs generates the environment key-values for the init task.
func GenInitTaskEnvKVPairs(region string, cluster string, manageurl string, service string,
azs []string, replicas int64, admin string, adminPass string) []*common.EnvKeyValuePair {
kvregion := &common.EnvKeyValuePair{Name: common.ENV_REGION, Value: region}
kvcluster := &common.EnvKeyValuePair{Name: common.ENV_CLUSTER, Value: cluster}
kvmgtserver := &common.EnvKeyValuePair{Name: common.ENV_MANAGE_SERVER_URL, Value: manageurl}
kvservice := &common.EnvKeyValuePair{Name: common.ENV_SERVICE_NAME, Value: service}
kvsvctype := &common.EnvKeyValuePair{Name: common.ENV_SERVICE_TYPE, Value: common.CatalogService_CouchDB}
kvport := &common.EnvKeyValuePair{Name: common.ENV_SERVICE_PORT, Value: strconv.Itoa(httpPort)}
kvop := &common.EnvKeyValuePair{Name: common.ENV_OP, Value: manage.CatalogSetServiceInitOp}
zones := ""
for i, zone := range azs {
if i == 0 {
zones = zone
} else {
zones = zones + common.ENV_VALUE_SEPARATOR + zone
}
}
kvazs := &common.EnvKeyValuePair{Name: common.ENV_AVAILABILITY_ZONES, Value: zones}
kvadmin := &common.EnvKeyValuePair{Name: common.ENV_ADMIN, Value: admin}
// TODO simply pass the password as env variable. The init task should fetch from the manage server.
kvadminpass := &common.EnvKeyValuePair{Name: common.ENV_ADMIN_PASSWORD, Value: adminPass}
domain := dns.GenDefaultDomainName(cluster)
members := dns.GenDNSName(utils.GenServiceMemberName(service, 0), domain)
for i := int64(1); i < replicas; i++ {
members = members + common.ENV_VALUE_SEPARATOR + dns.GenDNSName(utils.GenServiceMemberName(service, i), domain)
}
kvmembers := &common.EnvKeyValuePair{Name: common.ENV_SERVICE_MEMBERS, Value: members}
envkvs := []*common.EnvKeyValuePair{kvregion, kvcluster, kvmgtserver, kvservice, kvsvctype, kvport, kvop, kvazs, kvadmin, kvadminpass, kvmembers}
return envkvs
}
func GetAdminFromServiceConfigs(content string) (admin string, adminPass string) {
lines := strings.Split(content, "\n")
for _, line := range lines {
fields := strings.Split(line, "=")
if fields[0] == "ADMIN" {
admin = fields[1]
} else if fields[0] == "ADMIN_PASSWD" {
adminPass = fields[1]
}
}
return admin, adminPass
}
const (
servicefileContent = `
PLATFORM=%s
UUID=%s
ADMIN=%s
ADMIN_PASSWD=%s
ENCRYPTED_ADMIN_PASSWD=%s
ENABLE_CORS=%s
CREDENTIALS=%s
ORIGINS=%s
HEADERS=%s
METHODS=%s
ENABLE_SSL=%s
HAS_CA_CERT_FILE=%s
`
memberfileContent = `
AVAILABILITY_ZONE=%s
MEMBER_NAME=%s
SERVICE_MEMBER=%s
`
pbkdf2Fmt = "-pbkdf2-%s,%s,%d"
zonePlacementFmt = "%s:%d"
zonePlaceSep = ","
// couchdb@localhost will be replaced at docker entrypoint
vmArgsConfigs = `
-name couchdb@localhost
# All nodes must share the same magic cookie for distributed Erlang to work.
# Comment out this line if you synchronized the cookies by other means (using
# the ~/.erlang.cookie file, for example).
-setcookie monster
# Tell kernel and SASL not to log anything
-kernel error_logger silent
-sasl sasl_error_logger false
# Erlang kernel listen port
-kernel inet_dist_listen_min 9100
-kernel inet_dist_listen_max %d
# Use kernel poll functionality if supported by emulator
+K true
# Start a pool of asynchronous IO threads
+A 16
# Comment this line out to enable the interactive Erlang shell on startup
+Bd -noinput
`
couchConfigs = `
[couchdb]
uuid = uuid-member
database_dir = /data/couchdb
view_index_dir = /data/couchdb
[couch_peruser]
; peruser db is broken in 2.1.0, https://github.com/apache/couchdb/issues/749
;enable = true
;delete_dbs = true
[chttpd]
port = 5984
; has to listen on 0.0.0.0, couchdb could not bind dnsname.
bind_address = 0.0.0.0
require_valid_user = true
[httpd]
port = 5986
bind_address = 0.0.0.0
enable_cors = false
WWW-Authenticate = Basic realm="administrator"
[couch_httpd_auth]
require_valid_user = true
; 'username = password'
[admins]
admin = encryptePasswd
`
clusterConfigs = `
#http://docs.couchdb.org/en/2.1.0/cluster/theory.html
[cluster]
q=%d
r=%d
w=%d
n=3
placement = %s
; placement = metro-dc-a:2,metro-dc-b:1
`
)