-
Notifications
You must be signed in to change notification settings - Fork 30
/
profile.go
273 lines (244 loc) · 9.99 KB
/
profile.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
package config
import (
"github.com/creativeprojects/clog"
"github.com/creativeprojects/resticprofile/constants"
)
// Profile contains the whole profile configuration
type Profile struct {
config *Config
Name string
Quiet bool `mapstructure:"quiet" argument:"quiet"`
Verbose bool `mapstructure:"verbose" argument:"verbose"`
Repository string `mapstructure:"repository" argument:"repo"`
PasswordFile string `mapstructure:"password-file" argument:"password-file"`
CacheDir string `mapstructure:"cache-dir" argument:"cache-dir"`
CACert string `mapstructure:"cacert" argument:"cacert"`
TLSClientCert string `mapstructure:"tls-client-cert" argument:"tls-client-cert"`
Initialize bool `mapstructure:"initialize"`
Inherit string `mapstructure:"inherit"`
Lock string `mapstructure:"lock"`
RunBefore []string `mapstructure:"run-before"`
RunAfter []string `mapstructure:"run-after"`
RunAfterFail []string `mapstructure:"run-after-fail"`
Environment map[string]string `mapstructure:"env"`
Backup *BackupSection `mapstructure:"backup"`
Retention *RetentionSection `mapstructure:"retention"`
Check *OtherSectionWithSchedule `mapstructure:"check"`
Snapshots map[string]interface{} `mapstructure:"snapshots"`
Forget map[string]interface{} `mapstructure:"forget"`
Mount map[string]interface{} `mapstructure:"mount"`
OtherFlags map[string]interface{} `mapstructure:",remain"`
}
// BackupSection contains the specific configuration to the 'backup' command
type BackupSection struct {
CheckBefore bool `mapstructure:"check-before"`
CheckAfter bool `mapstructure:"check-after"`
RunBefore []string `mapstructure:"run-before"`
RunAfter []string `mapstructure:"run-after"`
UseStdin bool `mapstructure:"stdin" argument:"stdin"`
Source []string `mapstructure:"source"`
ExcludeFile []string `mapstructure:"exclude-file" argument:"exclude-file"`
FilesFrom []string `mapstructure:"files-from" argument:"files-from"`
Schedule []string `mapstructure:"schedule"`
SchedulePermission string `mapstructure:"schedule-permission"`
ScheduleLog string `mapstructure:"schedule-log"`
OtherFlags map[string]interface{} `mapstructure:",remain"`
}
// RetentionSection contains the specific configuration to
// the 'forget' command when running as part of a backup
type RetentionSection struct {
BeforeBackup bool `mapstructure:"before-backup"`
AfterBackup bool `mapstructure:"after-backup"`
Schedule []string `mapstructure:"schedule"`
SchedulePermission string `mapstructure:"schedule-permission"`
ScheduleLog string `mapstructure:"schedule-log"`
OtherFlags map[string]interface{} `mapstructure:",remain"`
}
// OtherSectionWithSchedule is a section containing schedule only specific parameters
// (the other parameters being for restic)
type OtherSectionWithSchedule struct {
Schedule []string `mapstructure:"schedule"`
SchedulePermission string `mapstructure:"schedule-permission"`
ScheduleLog string `mapstructure:"schedule-log"`
OtherFlags map[string]interface{} `mapstructure:",remain"`
}
// NewProfile instantiates a new blank profile
func NewProfile(c *Config, name string) *Profile {
return &Profile{
Name: name,
config: c,
}
}
// SetRootPath changes the path of all the relative paths and files in the configuration
func (p *Profile) SetRootPath(rootPath string) {
p.Lock = fixPath(p.Lock, expandEnv, absolutePrefix(rootPath), unixSpaces)
p.PasswordFile = fixPath(p.PasswordFile, expandEnv, absolutePrefix(rootPath), unixSpaces)
p.CacheDir = fixPath(p.CacheDir, expandEnv, absolutePrefix(rootPath), unixSpaces)
p.CACert = fixPath(p.CACert, expandEnv, absolutePrefix(rootPath), unixSpaces)
p.TLSClientCert = fixPath(p.TLSClientCert, expandEnv, absolutePrefix(rootPath), unixSpaces)
if p.Backup != nil {
if p.Backup.ExcludeFile != nil && len(p.Backup.ExcludeFile) > 0 {
p.Backup.ExcludeFile = fixPaths(p.Backup.ExcludeFile, expandEnv, absolutePrefix(rootPath), unixSpaces)
}
if p.Backup.FilesFrom != nil && len(p.Backup.FilesFrom) > 0 {
p.Backup.FilesFrom = fixPaths(p.Backup.FilesFrom, expandEnv, absolutePrefix(rootPath), unixSpaces)
}
// Backup source is NOT relative to the configuration, but where the script was launched instead
if p.Backup.Source != nil && len(p.Backup.Source) > 0 {
p.Backup.Source = fixPaths(p.Backup.Source, expandEnv, unixSpaces)
}
}
}
// SetHost will replace any host value from a boolean to the hostname
func (p *Profile) SetHost(hostname string) {
if p.Backup != nil && p.Backup.OtherFlags != nil {
replaceTrueValue(p.Backup.OtherFlags, constants.ParameterHost, hostname)
}
if p.Retention != nil && p.Retention.OtherFlags != nil {
replaceTrueValue(p.Retention.OtherFlags, constants.ParameterHost, hostname)
}
if p.Snapshots != nil {
replaceTrueValue(p.Snapshots, constants.ParameterHost, hostname)
}
if p.Forget != nil {
replaceTrueValue(p.Forget, constants.ParameterHost, hostname)
}
if p.Mount != nil {
replaceTrueValue(p.Mount, constants.ParameterHost, hostname)
}
}
// GetCommonFlags returns the flags common to all commands
func (p *Profile) GetCommonFlags() map[string][]string {
// Flags from the profile fields
flags := convertStructToFlags(*p)
flags = addOtherFlags(flags, p.OtherFlags)
return flags
}
// GetCommandFlags returns the flags specific to the command (backup, snapshots, forget, etc.)
func (p *Profile) GetCommandFlags(command string) map[string][]string {
flags := p.GetCommonFlags()
switch command {
case constants.CommandBackup:
if p.Backup == nil {
clog.Warning("No definition for backup command in this profile")
break
}
commandFlags := convertStructToFlags(*p.Backup)
if commandFlags != nil && len(commandFlags) > 0 {
flags = mergeFlags(flags, commandFlags)
}
flags = addOtherFlags(flags, p.Backup.OtherFlags)
case constants.CommandSnapshots:
if p.Snapshots != nil {
flags = addOtherFlags(flags, p.Snapshots)
}
case constants.CommandCheck:
if p.Check != nil && p.Check.OtherFlags != nil {
flags = addOtherFlags(flags, p.Check.OtherFlags)
}
case constants.CommandForget:
if p.Forget != nil {
flags = addOtherFlags(flags, p.Forget)
}
case constants.CommandMount:
if p.Mount != nil {
flags = addOtherFlags(flags, p.Mount)
}
}
return flags
}
// GetRetentionFlags returns the flags specific to the "forget" command being run as part of a backup
func (p *Profile) GetRetentionFlags() map[string][]string {
flags := p.GetCommonFlags()
// Special case of retention: we do copy the "source" from "backup" as "path" if it hasn't been redefined in "retention"
if _, found := p.Retention.OtherFlags[constants.ParameterPath]; !found {
p.Retention.OtherFlags[constants.ParameterPath] = fixPaths(p.Backup.Source, absolutePath)
}
flags = addOtherFlags(flags, p.Retention.OtherFlags)
return flags
}
// GetBackupSource returns the directories to backup
func (p *Profile) GetBackupSource() []string {
if p.Backup == nil {
return nil
}
return p.Backup.Source
}
// Schedules returns a slice of ScheduleConfig that satisfy the schedule.Config interface
func (p *Profile) Schedules() []*ScheduleConfig {
// Default to 3: backup, retention and check
configs := make([]*ScheduleConfig, 0, 3)
// Backup
if p.Backup != nil && p.Backup.Schedule != nil && len(p.Backup.Schedule) > 0 {
config := &ScheduleConfig{
profileName: p.Name,
commandName: constants.CommandBackup,
schedules: p.Backup.Schedule,
permission: p.Backup.SchedulePermission,
environment: p.Environment,
nice: 10, // hard-coded for now
logfile: p.Backup.ScheduleLog,
}
configs = append(configs, config)
}
// Retention (forget)
if p.Retention != nil && p.Retention.Schedule != nil && len(p.Retention.Schedule) > 0 {
config := &ScheduleConfig{
profileName: p.Name,
commandName: constants.SectionConfigurationRetention,
schedules: p.Retention.Schedule,
permission: p.Retention.SchedulePermission,
environment: p.Environment,
nice: 10, // hard-coded for now
logfile: p.Retention.ScheduleLog,
}
configs = append(configs, config)
}
// Check
if p.Check != nil && p.Check.Schedule != nil && len(p.Check.Schedule) > 0 {
config := &ScheduleConfig{
profileName: p.Name,
commandName: constants.CommandCheck,
schedules: p.Check.Schedule,
permission: p.Check.SchedulePermission,
environment: p.Environment,
nice: 10, // hard-coded for now
logfile: p.Check.ScheduleLog,
}
configs = append(configs, config)
}
return configs
}
func addOtherFlags(flags map[string][]string, otherFlags map[string]interface{}) map[string][]string {
if otherFlags == nil || len(otherFlags) == 0 {
return flags
}
// Add other flags
for name, value := range otherFlags {
if convert, ok := stringifyValueOf(value); ok {
flags[name] = convert
}
}
return flags
}
func mergeFlags(flags, newFlags map[string][]string) map[string][]string {
if (flags == nil || len(flags) == 0) && newFlags != nil {
return newFlags
}
if flags != nil && (newFlags == nil || len(newFlags) == 0) {
return flags
}
for key, value := range newFlags {
flags[key] = value
}
return flags
}
func replaceTrueValue(source map[string]interface{}, key, replace string) {
if genericValue, ok := source[key]; ok {
if value, ok := genericValue.(bool); ok {
if value {
source[key] = replace
}
}
}
}