/
config.go
161 lines (140 loc) · 3.73 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
//
// s3sync-service - Realtime S3 synchronisation tool
// Copyright (c) 2020 Yevgeniy Valeyev
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
package service
import (
"os"
"runtime"
"strings"
"time"
yaml "gopkg.in/yaml.v3"
)
// Config structure - contains lst of Site options
type Config struct {
AccessKey string `yaml:"access_key"`
SecretAccessKey string `yaml:"secret_access_key"`
AwsRegion string `yaml:"aws_region"`
LogLevel string `yaml:"loglevel"`
UploadQueueBuffer int `yaml:"upload_queue_buffer"`
UploadWorkers int `yaml:"upload_workers"`
ChecksumWorkers int `yaml:"checksum_workers"`
WatchInterval time.Duration `yaml:"watch_interval"`
S3OpsRetries int `yaml:"s3_ops_retries"`
Sites []Site `yaml:",flow"`
}
func (cfg *Config) setDefaults() {
if cfg.LogLevel == "" {
cfg.LogLevel = "info"
}
if cfg.UploadWorkers == 0 {
cfg.UploadWorkers = 10
}
if cfg.ChecksumWorkers == 0 {
// If in k8s then run 2 workers
// otherwise run 2 workers per core
if inK8s {
cfg.ChecksumWorkers = 2
} else {
cfg.ChecksumWorkers = runtime.NumCPU() * 2
}
}
if cfg.WatchInterval == 0 {
cfg.WatchInterval = time.Millisecond * 1000
}
if cfg.S3OpsRetries == 0 {
cfg.S3OpsRetries = 5
}
}
func (site *Site) setDefaults(cfg *Config) {
// Remove leading slash from the BucketPath
site.BucketPath = strings.TrimPrefix(site.BucketPath, "/")
// Set site name
if site.Name == "" {
site.Name = site.Bucket + "/" + site.BucketPath
site.Name = strings.TrimSuffix(site.Name, "/")
}
// Set site AccessKey
if site.AccessKey == "" {
site.AccessKey = cfg.AccessKey
}
// Set site SecretAccessKey
if site.SecretAccessKey == "" {
site.SecretAccessKey = cfg.SecretAccessKey
}
// Set site BucketRegion
if site.BucketRegion == "" {
site.BucketRegion = cfg.AwsRegion
}
// Set default value for StorageClass
if site.StorageClass == "" {
site.StorageClass = "STANDARD"
}
// Set site WatchInterval
if site.WatchInterval == 0 {
site.WatchInterval = cfg.WatchInterval
}
// Set site S3OpsRetries
if site.S3OpsRetries == 0 {
site.S3OpsRetries = cfg.S3OpsRetries
}
// Set site S3OpsRetries
if len(site.Inclusions) == 0 {
site.Inclusions = []string{".*"}
}
}
func configProcessError(err error) {
logger.Error(err)
osExit(2)
}
func getConfig() (bool, *Config) {
var emptyConfig *Config
empty := true
if inK8s && configmap != "" {
cfg := k8sGetCm(clientset, configmap)
config = readConfigString(cfg)
} else {
config = readConfigFile(configpath)
}
if config != emptyConfig {
empty = false
}
return empty, config
}
func readConfigFile(configpath string) *Config {
f, err := os.Open(configpath)
if err != nil {
configProcessError(err)
}
decoder := yaml.NewDecoder(f)
err = decoder.Decode(&config)
f.Close()
if err != nil {
configProcessError(err)
} else {
config.setDefaults()
}
return config
}
func readConfigString(cfgData string) *Config {
err := yaml.Unmarshal([]byte(cfgData), &config)
if err != nil {
configProcessError(err)
} else {
config.setDefaults()
}
return config
}