forked from longhorn/backupstore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
246 lines (217 loc) · 6.77 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
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
package backupstore
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"path/filepath"
"strings"
"time"
"github.com/honestbee/jobq"
"github.com/sirupsen/logrus"
. "github.com/longhorn/backupstore/logging"
"github.com/longhorn/backupstore/util"
)
const (
VOLUME_SEPARATE_LAYER1 = 2
VOLUME_SEPARATE_LAYER2 = 4
VOLUME_DIRECTORY = "volumes"
VOLUME_CONFIG_FILE = "volume.cfg"
BACKUP_DIRECTORY = "backups"
BACKUP_CONFIG_PREFIX = "backup_"
CFG_SUFFIX = ".cfg"
)
func getBackupConfigName(id string) string {
return BACKUP_CONFIG_PREFIX + id + CFG_SUFFIX
}
func loadConfigInBackupStore(filePath string, driver BackupStoreDriver, v interface{}) error {
if !driver.FileExists(filePath) {
return fmt.Errorf("cannot find %v in backupstore", filePath)
}
rc, err := driver.Read(filePath)
if err != nil {
return err
}
defer rc.Close()
log.WithFields(logrus.Fields{
LogFieldReason: LogReasonStart,
LogFieldObject: LogObjectConfig,
LogFieldKind: driver.Kind(),
LogFieldFilepath: filePath,
}).Debug()
if err := json.NewDecoder(rc).Decode(v); err != nil {
return err
}
log.WithFields(logrus.Fields{
LogFieldReason: LogReasonComplete,
LogFieldObject: LogObjectConfig,
LogFieldKind: driver.Kind(),
LogFieldFilepath: filePath,
}).Debug()
return nil
}
func saveConfigInBackupStore(filePath string, driver BackupStoreDriver, v interface{}) error {
j, err := json.Marshal(v)
if err != nil {
return err
}
log.WithFields(logrus.Fields{
LogFieldReason: LogReasonStart,
LogFieldObject: LogObjectConfig,
LogFieldKind: driver.Kind(),
LogFieldFilepath: filePath,
}).Debug()
if err := driver.Write(filePath, bytes.NewReader(j)); err != nil {
return err
}
log.WithFields(logrus.Fields{
LogFieldReason: LogReasonComplete,
LogFieldObject: LogObjectConfig,
LogFieldKind: driver.Kind(),
LogFieldFilepath: filePath,
}).Debug()
return nil
}
func volumeExists(volumeName string, driver BackupStoreDriver) bool {
volumeFile := getVolumeFilePath(volumeName)
return driver.FileExists(volumeFile)
}
func getVolumePath(volumeName string) string {
checksum := util.GetChecksum([]byte(volumeName))
volumeLayer1 := checksum[0:VOLUME_SEPARATE_LAYER1]
volumeLayer2 := checksum[VOLUME_SEPARATE_LAYER1:VOLUME_SEPARATE_LAYER2]
return filepath.Join(backupstoreBase, VOLUME_DIRECTORY, volumeLayer1, volumeLayer2, volumeName) + "/"
}
func getVolumeFilePath(volumeName string) string {
volumePath := getVolumePath(volumeName)
volumeCfg := VOLUME_CONFIG_FILE
return filepath.Join(volumePath, volumeCfg)
}
// getVolumeNames returns all volume names based on the folders on the backupstore
func getVolumeNames(jobQueues *jobq.WorkerDispatcher, jobQueueTimeout time.Duration, driver BackupStoreDriver) ([]string, error) {
names := []string{}
volumePathBase := filepath.Join(backupstoreBase, VOLUME_DIRECTORY)
lv1Dirs, err := driver.List(volumePathBase)
if err != nil {
log.Warnf("failed to list first level dirs for path: %v reason: %v", volumePathBase, err)
return names, err
}
var (
lv1Trackers []jobq.JobTracker
lv2Trackers []jobq.JobTracker
errs []string
)
for _, lv1Dir := range lv1Dirs {
path := filepath.Join(volumePathBase, lv1Dir)
lv1Tracker := jobQueues.QueueTimedFunc(context.Background(), func(ctx context.Context) (interface{}, error) {
lv2Dirs, err := driver.List(path)
if err != nil {
log.Warnf("failed to list second level dirs for path: %v reason: %v", path, err)
return nil, err
}
lv2Paths := make([]string, len(lv2Dirs))
for i := range lv2Dirs {
lv2Paths[i] = filepath.Join(path, lv2Dirs[i])
}
return lv2Paths, nil
}, jobQueueTimeout)
lv1Trackers = append(lv1Trackers, lv1Tracker)
}
for _, lv1Tracker := range lv1Trackers {
payload, err := lv1Tracker.Result()
if err != nil {
errs = append(errs, err.Error())
continue
}
lv2Paths := payload.([]string)
for _, lv2Path := range lv2Paths {
path := lv2Path
lv2Tracker := jobQueues.QueueTimedFunc(context.Background(), func(ctx context.Context) (interface{}, error) {
volumeNames, err := driver.List(path)
if err != nil {
log.Warnf("failed to list volume names for path: %v reason: %v", path, err)
return nil, err
}
return volumeNames, nil
}, jobQueueTimeout)
lv2Trackers = append(lv2Trackers, lv2Tracker)
}
}
for _, lv2Tracker := range lv2Trackers {
payload, err := lv2Tracker.Result()
if err != nil {
errs = append(errs, err.Error())
continue
}
volumeNames := payload.([]string)
names = append(names, volumeNames...)
}
if len(errs) > 0 {
return names, errors.New(strings.Join(errs, "\n"))
}
return names, nil
}
func loadVolume(volumeName string, driver BackupStoreDriver) (*Volume, error) {
v := &Volume{}
file := getVolumeFilePath(volumeName)
if err := loadConfigInBackupStore(file, driver, v); err != nil {
return nil, err
}
return v, nil
}
func saveVolume(v *Volume, driver BackupStoreDriver) error {
file := getVolumeFilePath(v.Name)
if err := saveConfigInBackupStore(file, driver, v); err != nil {
return err
}
return nil
}
func getBackupNamesForVolume(volumeName string, driver BackupStoreDriver) ([]string, error) {
result := []string{}
fileList, err := driver.List(getBackupPath(volumeName))
if err != nil {
// path doesn't exist
return result, nil
}
return util.ExtractNames(fileList, BACKUP_CONFIG_PREFIX, CFG_SUFFIX), nil
}
func getBackupPath(volumeName string) string {
return filepath.Join(getVolumePath(volumeName), BACKUP_DIRECTORY) + "/"
}
func getBackupConfigPath(backupName, volumeName string) string {
path := getBackupPath(volumeName)
fileName := getBackupConfigName(backupName)
return filepath.Join(path, fileName)
}
func isBackupInProgress(backup *Backup) bool {
return backup != nil && backup.CreatedTime == ""
}
func backupExists(backupName, volumeName string, bsDriver BackupStoreDriver) bool {
return bsDriver.FileExists(getBackupConfigPath(backupName, volumeName))
}
func loadBackup(backupName, volumeName string, bsDriver BackupStoreDriver) (*Backup, error) {
backup := &Backup{}
if err := loadConfigInBackupStore(getBackupConfigPath(backupName, volumeName), bsDriver, backup); err != nil {
return nil, err
}
return backup, nil
}
func saveBackup(backup *Backup, bsDriver BackupStoreDriver) error {
if backup.VolumeName == "" {
return fmt.Errorf("missing volume specifier for backup: %v", backup.Name)
}
filePath := getBackupConfigPath(backup.Name, backup.VolumeName)
if err := saveConfigInBackupStore(filePath, bsDriver, backup); err != nil {
return err
}
return nil
}
func removeBackup(backup *Backup, bsDriver BackupStoreDriver) error {
filePath := getBackupConfigPath(backup.Name, backup.VolumeName)
if err := bsDriver.Remove(filePath); err != nil {
return err
}
log.Debugf("Removed %v on backupstore", filePath)
return nil
}