forked from longhorn/backupstore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
244 lines (206 loc) · 7.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
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
package backupbackingimage
import (
"fmt"
"net/url"
"path/filepath"
"strings"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/longhorn/backupstore"
"github.com/longhorn/backupstore/common"
"github.com/longhorn/backupstore/util"
)
const (
BackingImageBlockSeparateLayer1 = 2
BackingImageBlockSeparateLayer2 = 4
BackingImageDirectory = "backing-images"
BackingImageConfigFile = "backing-image.cfg"
BlocksDirectory = "blocks"
BlkSuffix = ".blk"
)
func addBackingImageConfigInBackupStore(driver backupstore.BackupStoreDriver, backupBackingImage *BackupBackingImage) (bool, error) {
log := backupstore.GetLog().WithFields(logrus.Fields{"type": BackingImageLogType, "name": backupBackingImage.Name})
if backingImageExists(driver, backupBackingImage.Name) {
return true, nil
}
if !util.ValidateName(backupBackingImage.Name) {
return false, fmt.Errorf("invalid backing image name %v", backupBackingImage.Name)
}
if err := saveBackingImageConfig(driver, backupBackingImage); err != nil {
return false, errors.Wrap(err, "failed to add backing image config to backupstore")
}
log.Info("Added backing image config to backupstore")
return false, nil
}
func removeBackupBackingImage(backupBackingImage *BackupBackingImage, driver backupstore.BackupStoreDriver) error {
log := backupstore.GetLog().WithFields(logrus.Fields{"type": BackingImageLogType, "name": backupBackingImage.Name})
filePath := getBackingImageFilePath(backupBackingImage.Name)
if err := driver.Remove(filePath); err != nil {
return err
}
log.Infof("Removed backing image on backupstore with filePath: %v", filePath)
return nil
}
func backingImageExists(driver backupstore.BackupStoreDriver, backingImageName string) bool {
return driver.FileExists(getBackingImageFilePath(backingImageName))
}
func getBackingImageFilePath(backingImageName string) string {
backingImagePath := getBackingImagePath(backingImageName)
backingImageCfg := BackingImageConfigFile
return filepath.Join(backingImagePath, backingImageCfg)
}
func getBackingImagePath(backingImageName string) string {
return filepath.Join(backupstore.GetBackupstoreBase(), BackingImageDirectory, BackingImageDirectory, backingImageName) + "/"
}
func saveBackingImageConfig(driver backupstore.BackupStoreDriver, backupBackingImage *BackupBackingImage) error {
return backupstore.SaveConfigInBackupStore(driver, getBackingImageFilePath(backupBackingImage.Name), backupBackingImage)
}
func loadBackingImageConfigInBackupStore(driver backupstore.BackupStoreDriver, backingImageName string) (*BackupBackingImage, error) {
log := backupstore.GetLog()
backupBackingImage := &BackupBackingImage{}
path := getBackingImageFilePath(backingImageName)
if err := backupstore.LoadConfigInBackupStore(driver, path, backupBackingImage); err != nil {
return nil, err
}
if backupBackingImage.CompressionMethod == "" {
log.Infof("Fall back compression method to %v for backing image %v", backupstore.LEGACY_COMPRESSION_METHOD, backupBackingImage.Name)
backupBackingImage.CompressionMethod = backupstore.LEGACY_COMPRESSION_METHOD
}
if backupBackingImage.Blocks == nil {
backupBackingImage.Blocks = []common.BlockMapping{}
}
if backupBackingImage.ProcessingBlocks == nil {
backupBackingImage.ProcessingBlocks = &common.ProcessingBlocks{
Blocks: map[string][]*common.BlockMapping{},
}
}
return backupBackingImage, nil
}
func getTotalBackupBlockCounts(mappings *common.Mappings) (int64, error) {
totalBlockCounts := int64(len(mappings.Mappings))
return totalBlockCounts, nil
}
func getBackingImageBlockFilePath(checksum string) string {
blockSubDirLayer1 := checksum[0:BackingImageBlockSeparateLayer1]
blockSubDirLayer2 := checksum[BackingImageBlockSeparateLayer1:BackingImageBlockSeparateLayer2]
path := filepath.Join(getBackingImageBlockPath(), blockSubDirLayer1, blockSubDirLayer2)
fileName := checksum + BlkSuffix
return filepath.Join(path, fileName)
}
func getBackingImageBlockPath() string {
return filepath.Join(backupstore.GetBackupstoreBase(), BackingImageDirectory, BlocksDirectory) + "/"
}
func EncodeBackupBackingImageURL(backingImageName, destURL string) string {
if destURL == "" || backingImageName == "" {
return ""
}
u, err := url.Parse(destURL)
if err != nil {
log := backupstore.GetLog()
log.WithError(err).Errorf("Failed to parse destURL %v", destURL)
return ""
}
if u.Scheme == "" {
return ""
}
v := url.Values{}
v.Add("backingImage", backingImageName)
prefixChar := "?"
if strings.Contains(destURL, "?") {
prefixChar = "&"
}
return destURL + prefixChar + v.Encode()
}
func DecodeBackupBackingImageURL(backupURL string) (string, string, error) {
u, err := url.Parse(backupURL)
if err != nil {
return "", "", err
}
v := u.Query()
backingImageName := v.Get("backingImage")
if !util.ValidateName(backingImageName) {
return "", "", fmt.Errorf("invalid backing image name parsed: %v", backingImageName)
}
u.RawQuery = ""
destURL := u.String()
return backingImageName, destURL, nil
}
func GetAllBackupBackingImageNames(driver backupstore.BackupStoreDriver) ([]string, error) {
result := []string{}
backingImageConfigBase := filepath.Join(backupstore.GetBackupstoreBase(), BackingImageDirectory, BackingImageDirectory) + "/"
nameList, err := driver.List(backingImageConfigBase)
if err != nil {
return result, nil
}
return nameList, nil
}
func getAllBlockNames(driver backupstore.BackupStoreDriver) ([]string, error) {
names := []string{}
blockPathBase := getBackingImageBlockPath()
lv1Dirs, err := driver.List(blockPathBase)
// Directory doesn't exist
if err != nil {
return names, nil
}
for _, lv1 := range lv1Dirs {
lv1Path := filepath.Join(blockPathBase, lv1)
lv2Dirs, err := driver.List(lv1Path)
if err != nil {
return nil, err
}
for _, lv2 := range lv2Dirs {
lv2Path := filepath.Join(lv1Path, lv2)
blockNames, err := driver.List(lv2Path)
if err != nil {
return nil, err
}
names = append(names, blockNames...)
}
}
return util.ExtractNames(names, "", BlkSuffix), nil
}
func isBackupInProgress(backupBackingImage *BackupBackingImage) bool {
return backupBackingImage != nil && backupBackingImage.CompleteTime == ""
}
type BackupInfo struct {
Name string
URL string
CompleteAt string
Size int64 `json:",string"`
Checksum string
Labels map[string]string
CompressionMethod string `json:",omitempty"`
Secret string
SecretNamespace string
}
func InspectBackupBackingImage(backupURL string) (*BackupInfo, error) {
backupBackingImageName, destURL, err := DecodeBackupBackingImageURL(backupURL)
if err != nil {
return nil, err
}
bsDriver, err := backupstore.GetBackupStoreDriver(destURL)
if err != nil {
return nil, err
}
backupBackingImage, err := loadBackingImageConfigInBackupStore(bsDriver, backupBackingImageName)
if err != nil {
return nil, err
} else if isBackupInProgress(backupBackingImage) {
// for now we don't return in progress backup backing image to the ui
return nil, fmt.Errorf("backup backing image %v is still in progress", backupBackingImage.Name)
}
return fillFullBackupBackingImageInfo(backupBackingImage, bsDriver.GetURL()), nil
}
func fillFullBackupBackingImageInfo(backupBackingImage *BackupBackingImage, destURL string) *BackupInfo {
return &BackupInfo{
Name: backupBackingImage.Name,
URL: EncodeBackupBackingImageURL(backupBackingImage.Name, destURL),
CompleteAt: backupBackingImage.CompleteTime,
Size: backupBackingImage.Size,
Checksum: backupBackingImage.Checksum,
Labels: backupBackingImage.Labels,
CompressionMethod: backupBackingImage.CompressionMethod,
Secret: backupBackingImage.Secret,
SecretNamespace: backupBackingImage.SecretNamespace,
}
}