-
Notifications
You must be signed in to change notification settings - Fork 3
/
rollback_gcp.go
324 lines (264 loc) · 8.72 KB
/
rollback_gcp.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// Copyright 2021 VILT Group
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package appcmds
import (
"context"
"fmt"
"github.com/viltgroup/bucket-restore/internal/brestore/gcprestore"
"sync"
"time"
"cloud.google.com/go/storage"
gcp_history "github.com/viltgroup/bucket-restore/internal/brestore/gcprestore/history"
gcp_generations "github.com/viltgroup/bucket-restore/internal/brestore/gcprestore/history/generations"
)
func doRestoreGCP(keyfile string, bucketName string, path string, timestamp time.Time, quiet bool) error {
client, _, err := gcprestore.GetStorageClientFromFile(keyfile)
if err != nil {
return fmt.Errorf("getting storage client for key file '%v': %w", keyfile, err)
}
bucket := client.Bucket(bucketName)
var created, deleted, noAction int64
var errors []error
actions := gcp_history.FileActions{}
listingStarted := time.Now()
allGens, err := gcp_generations.OfPathByName(bucket, path)
if err != nil {
return fmt.Errorf("listing contents of bucket: %v", err)
}
listingElapsed := time.Since(listingStarted)
decisionsStarted := time.Now()
for _, fileGens := range allGens {
fileGens.SortByCreatedDateAsc()
desiredState, lastState := gcp_history.StateDiffAtTime(fileGens, timestamp)
action := gcp_history.ActionForStateChange(lastState, desiredState)
if action.Action != gcp_history.NO_ACTION {
actions = append(actions, action)
} else {
noAction++
}
}
decisionsElapsed := time.Since(decisionsStarted)
actionsStarted := time.Now()
ctx := context.Background()
resChan := make(chan RunActionResultGCP, 1024)
nActions := len(actions)
parts := 1
if nActions > 4 {
parts = *maxConcurrencyFlag
}
go concurrentActionsGCP(ctx, bucket, actions, parts, resChan)
i := 1
for result := range resChan {
if result.Err != nil {
errors = append(errors, result.Err)
fmt.Printf("[%d/%d] Error for %s '%s': %v\n",
i, nActions, result.Action.String(), result.Action.Source.Name, result.Err)
} else {
switch result.Action.Action {
case gcp_history.CREATE:
if !quiet {
fmt.Printf("[%d/%d] Created %s(#%d) from #%d\n",
i,
nActions,
result.NewObj.Name,
result.NewObj.Generation,
result.Action.Source.Generation)
}
created++
case gcp_history.DELETE:
if !quiet {
fmt.Printf("[%d/%d] Deleted %s(#%d)\n",
i,
nActions,
result.Action.Source.Name,
result.Action.Source.Generation)
}
deleted++
default:
}
}
i++
}
actionsElapsed := time.Since(actionsStarted)
fmt.Printf("\n")
fmt.Printf("Bucket restored to %v:\n", timestamp)
fmt.Printf(" %d objects created\n", created)
fmt.Printf(" %d objects deleted\n", deleted)
fmt.Printf(" %d objects did not need any action\n", noAction)
fmt.Printf(" %d errors\n", errors)
fmt.Printf(""+
"Elapsed time: %v\n"+
" Retrieving object info: %v\n"+
" Action decision: %v\n"+
" Action execution: %v\n",
listingElapsed+decisionsElapsed+actionsElapsed,
listingElapsed, decisionsElapsed, actionsElapsed)
if len(errors) > 0 {
err = saveErrorsToFile("errors.log", errors)
if err != nil {
return fmt.Errorf("writing errors to 'error.log': %w", err)
}
fmt.Printf("" +
"There were errors running the restore command.\n" +
"A file 'errors.log' was created with the error details\n")
}
return nil
}
// RunActionResultGCP contains info about the execution of an action
type RunActionResultGCP struct {
Action gcp_history.FileAction
NewObj *storage.ObjectAttrs
Err error
}
func concurrentActionsGCP(ctx context.Context, bucket *storage.BucketHandle, actions gcp_history.FileActions, parts int, resultChan chan<- RunActionResultGCP) {
var wg sync.WaitGroup
chunks := divideActionsGCP(actions, parts)
wg.Add(len(chunks))
for _, chunk := range chunks {
chunk := chunk
go func() {
runActionsGCP(ctx, bucket, chunk, resultChan)
wg.Done()
}()
}
wg.Wait()
close(resultChan)
}
func divideActionsGCP(actions gcp_history.FileActions, parts int) []gcp_history.FileActions {
var divided []gcp_history.FileActions
nActions := len(actions)
chunkSize := (nActions + parts - 1) / parts
for i := 0; i < nActions; i += chunkSize {
end := i + chunkSize
if end > nActions {
end = nActions
}
divided = append(divided, actions[i:end])
}
return divided
}
func runActionsGCP(ctx context.Context, bucket *storage.BucketHandle, actions gcp_history.FileActions, resultChan chan<- RunActionResultGCP) {
for _, action := range actions {
res := RunActionResultGCP{Action: action}
switch action.Action {
case gcp_history.CREATE:
newObject, err := doCreateGCP(ctx, bucket, action)
if err != nil {
res.Err = fmt.Errorf("creating object '%s': %v", action.Source.Name, err)
} else {
res.NewObj = newObject
}
case gcp_history.DELETE:
err := doDeleteGCP(ctx, bucket, action)
if err != nil {
res.Err = fmt.Errorf("deleting object '%s': %v", action.Source.Name, err)
}
default:
}
resultChan <- res
}
}
func doDryRunExplainGCP(keyfile string, bucketName string, path string, time time.Time) error {
client, _, err := gcprestore.GetStorageClientFromFile(keyfile)
if err != nil {
return fmt.Errorf("getting storage client for key file '%v': %w", keyfile, err)
}
bucket := client.Bucket(bucketName)
actions := gcp_history.FileActions{}
allGens, err := gcp_generations.OfPathByName(bucket, path)
if err != nil {
return fmt.Errorf("listing contents of bucket: %w", err)
}
for _, fileGens := range allGens {
fileGens.SortByCreatedDateAsc()
desiredState, lastState := gcp_history.StateDiffAtTime(fileGens, time)
action := gcp_history.ActionForStateChange(lastState, desiredState)
actions = append(actions, action)
fmt.Printf(""+
"%s: %s\n"+
" Current state: %s\n"+
" Restore state: %s\n",
action.Source.Name,
formatActionGCP(action),
formatStateGCP(lastState),
formatStateGCP(desiredState))
}
return nil
}
func doDryRunGCP(keyfile string, bucketName string, path string, time time.Time) error {
client, _, err := gcprestore.GetStorageClientFromFile(keyfile)
if err != nil {
return fmt.Errorf("getting storage client for key file '%v': %w", keyfile, err)
}
bucket := client.Bucket(bucketName)
var toCreate, toDelete, noAction int64
allGens, err := gcp_generations.OfPathByName(bucket, path)
if err != nil {
return fmt.Errorf("listing contents of bucket: %w", err)
}
for _, fileGens := range allGens {
fileGens.SortByCreatedDateAsc()
desiredState, lastState := gcp_history.StateDiffAtTime(fileGens, time)
action := gcp_history.ActionForStateChange(lastState, desiredState)
switch action.Action {
case gcp_history.CREATE:
toCreate++
case gcp_history.DELETE:
toDelete++
case gcp_history.NO_ACTION:
noAction++
}
}
fmt.Printf("To create: %d objects\n", toCreate)
fmt.Printf("To delete %d objects\n", toDelete)
fmt.Printf("No action: %d objects\n", noAction)
return nil
}
func doCreateGCP(ctx context.Context, bucket *storage.BucketHandle, action gcp_history.FileAction) (*storage.ObjectAttrs, error) {
toObject := bucket.Object(action.Source.Name)
fromObject := toObject.Generation(action.Source.Generation)
if action.GenerationPreCondition != 0 {
toObject = toObject.If(storage.Conditions{GenerationMatch: action.GenerationPreCondition})
}
copier := toObject.CopierFrom(fromObject)
return copier.Run(ctx)
}
func doDeleteGCP(ctx context.Context, bucket *storage.BucketHandle, action gcp_history.FileAction) error {
obj := bucket.Object(action.Source.Name).If(storage.Conditions{GenerationMatch: action.GenerationPreCondition})
return obj.Delete(ctx)
}
func formatStateGCP(state gcp_history.PathState) string {
switch state.PathStatus {
case gcp_history.NOT_EXISTENT:
return "Not Existent"
case gcp_history.EXISTS:
return fmt.Sprintf("Exists at generation #%d, md5: %x", state.Generation, state.MD5)
case gcp_history.DELETED:
return fmt.Sprintf("Deleted on generation #%d, md5: %x", state.Generation, state.MD5)
default:
return "Unknown Status"
}
}
func formatActionGCP(action gcp_history.FileAction) string {
switch action.Action {
case gcp_history.DELETE:
return "Delete"
case gcp_history.CREATE:
return fmt.Sprintf("Create from #%d", action.Source.Generation)
case gcp_history.NO_ACTION:
return "No Action"
default:
return "Unknown Status"
}
}