forked from longhorn/backupstore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
198 lines (173 loc) · 5.27 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
package backupstore
import (
"bytes"
"encoding/json"
"fmt"
"path/filepath"
"github.com/longhorn/backupstore/util"
"github.com/sirupsen/logrus"
. "github.com/longhorn/backupstore/logging"
)
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)
}
func getVolumeNames(driver BackupStoreDriver) ([]string, error) {
names := []string{}
volumePathBase := filepath.Join(backupstoreBase, VOLUME_DIRECTORY)
lv1Dirs, err := driver.List(volumePathBase)
// Directory doesn't exist
if err != nil {
return names, nil
}
for _, lv1 := range lv1Dirs {
lv1Path := filepath.Join(volumePathBase, lv1)
lv2Dirs, err := driver.List(lv1Path)
if err != nil {
return nil, err
}
for _, lv2 := range lv2Dirs {
lv2Path := filepath.Join(lv1Path, lv2)
volumeNames, err := driver.List(lv2Path)
if err != nil {
return nil, err
}
names = append(names, volumeNames...)
}
}
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
}