-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
plan.go
213 lines (178 loc) · 5.49 KB
/
plan.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
package config
import (
"encoding/json"
"path/filepath"
"strings"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
type Plan struct {
Name string `yaml:"name"`
Target Target `yaml:"target"`
Scheduler Scheduler `yaml:"scheduler"`
Retry Retry `yaml:"retry"`
Validation *Validation `yaml:"validation"`
Encryption *Encryption `yaml:"encryption"`
S3 *S3 `yaml:"s3"`
GCloud *GCloud `yaml:"gcloud"`
Rclone *Rclone `yaml:"rclone"`
Azure *Azure `yaml:"azure"`
SFTP *SFTP `yaml:"sftp"`
SMTP *SMTP `yaml:"smtp"`
Slack *Slack `yaml:"slack"`
Team *Team `yaml:"team"`
}
type Validation struct {
Database Target `yaml:"database"`
}
type Target struct {
Database string `yaml:"database"`
Host string `yaml:"host"`
Uri string `yaml:"uri"`
Password string `yaml:"password"`
Port int `yaml:"port"`
Username string `yaml:"username"`
NoGzip bool `yaml:"noGzip"`
Params string `yaml:"params"`
}
type Scheduler struct {
Cron string `yaml:"cron"`
Retention int `yaml:"retention"`
Timeout int `yaml:"timeout"`
}
type Retry struct {
Attempts int `yaml:"attempts"`
BackoffFactor float32 `yaml:"backoffFactor"`
}
type Encryption struct {
Gpg *Gpg `yaml:"gpg"`
}
type Gpg struct {
KeyServer string `yaml:"keyServer"`
Recipients []string `yaml:"recipients"`
KeyFile string `yaml:"keyFile"`
}
type S3 struct {
Bucket string `yaml:"bucket"`
AccessKey string `yaml:"accessKey"`
API string `yaml:"api"`
SecretKey string `yaml:"secretKey"`
URL string `yaml:"url"`
KmsKeyId string `yaml:"kmsKeyId"`
StorageClass string `yaml:"storageClass" validate:"omitempty,oneof=STANDARD REDUCED_REDUNDANCY STANDARD_IA ONE-ZONE_IA INTELLIGENT_TIERING GLACIER DEEP_ARCHIVE"`
}
type GCloud struct {
Bucket string `yaml:"bucket"`
KeyFilePath string `yaml:"keyFilePath"`
}
type Rclone struct {
Bucket string `yaml:"bucket"`
ConfigFilePath string `yaml:"configFilePath"`
ConfigSection string `yaml:"configSection"`
}
type Azure struct {
ContainerName string `yaml:"containerName"`
ConnectionString string `yaml:"connectionString"`
}
type SFTP struct {
Dir string `yaml:"dir"`
Host string `yaml:"host"`
Password string `yaml:"password"`
PrivateKey string `yaml:"privateKey"`
Passphrase string `yaml:"passphrase"`
Port int `yaml:"port"`
Username string `yaml:"username"`
}
type SMTP struct {
WarnOnly bool `yaml:"warnOnly"`
TlsEnabled bool `yaml:"tlsEnabled"`
InsecureSkipVerify bool `yaml:"insecureSkipVerify"`
Server string `yaml:"server"`
Port string `yaml:"port"`
Password string `yaml:"password"`
Username string `yaml:"username"`
From string `yaml:"from"`
To []string `yaml:"to"`
}
type Team struct {
WebhookURL string `yaml:"webhookUrl"`
WarnOnly bool `yaml:"warnOnly"`
ThemeColor string `yaml:"themeColor"`
}
type Slack struct {
URL string `yaml:"url"`
Channel string `yaml:"channel"`
Username string `yaml:"username"`
WarnOnly bool `yaml:"warnOnly"`
}
func LoadPlan(dir string, name string) (Plan, error) {
plan := Plan{}
// Set the paths to look for the config file in.
viper.AddConfigPath(dir)
// Set the name of the config file (without extension).
viper.SetConfigName(name)
setupViperEnv(name)
// Try to read the config file.
if err := viper.ReadInConfig(); err != nil {
return plan, errors.Wrapf(err, "Reading %v failed", name)
}
// Unmarshal the read YAML into our struct.
if err := viper.Unmarshal(&plan); err != nil {
return plan, errors.Wrapf(err, "Parsing %v failed", name)
}
plan.Name = name
return plan, nil
}
func LoadPlans(dir string) ([]Plan, error) {
// Use Go's standard lib to list all YAML files.
files, err := filepath.Glob(filepath.Join(dir, "*.y*ml"))
if err != nil {
return nil, errors.Wrapf(err, "Reading from %v failed", dir)
}
plans := make([]Plan, 0, len(files))
names := make(map[string]bool)
for _, path := range files {
var plan Plan
_, filename := filepath.Split(path)
name := strings.TrimSuffix(filename, filepath.Ext(filename))
if names[name] {
continue // Skip duplicate plans
}
names[name] = true
// Set viper to read YAML configurations.
viper.Reset()
viper.SetConfigFile(path)
setupViperEnv(name)
// Try to read the config file.
if err := viper.ReadInConfig(); err != nil {
return nil, errors.Wrapf(err, "Reading %v failed", path)
}
// Unmarshal the read YAML into our struct.
if err := viper.Unmarshal(&plan); err != nil {
return nil, errors.Wrapf(err, "Parsing %v failed", path)
}
plan.Name = name
if log.IsLevelEnabled(logrus.DebugLevel) {
planJSON, err := json.Marshal(plan)
if err != nil {
return nil, errors.Wrapf(err, "Marshaling %v failed", plan)
}
log.Debugf("Loaded plan %v, plan JSON: %s", plan.Name, planJSON)
}
plans = append(plans, plan)
}
if len(plans) == 0 {
return nil, errors.Errorf("No backup plans found in %v", dir)
}
return plans, nil
}
func setupViperEnv(planName string) {
viper.SetConfigType("yaml")
// set upper case plan name as env prefix
envPrefix := strings.ReplaceAll(planName, "-", "_")
viper.SetEnvPrefix(envPrefix + "_") // will be uppercased automatically
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()
}