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
|
package backup
import (
"context"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"gitlab.com/gitlab-org/gitaly/client"
"gitlab.com/gitlab-org/gitaly/internal/storage"
"gitlab.com/gitlab-org/gitaly/proto/go/gitalypb"
"gitlab.com/gitlab-org/gitaly/streamio"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// ErrSkipped means the repository was skipped because there was nothing to backup
var ErrSkipped = errors.New("repository skipped")
// Filesystem strategy for creating and restoring backups
type Filesystem struct {
path string
conns *client.Pool
}
// NewFilesystem creates a new Filesystem strategy
func NewFilesystem(path string) *Filesystem {
return &Filesystem{
path: path,
conns: client.NewPool(),
}
}
// CreateRequest is the request to create a backup
type CreateRequest struct {
Server storage.ServerInfo
Repository *gitalypb.Repository
}
// Create creates a repository backup on a local filesystem
func (fs *Filesystem) Create(ctx context.Context, req *CreateRequest) error {
if isEmpty, err := fs.isEmpty(ctx, req.Server, req.Repository); err != nil {
return fmt.Errorf("filesystem: %w", err)
} else if isEmpty {
return ErrSkipped
}
backupPath := strings.TrimSuffix(filepath.Join(fs.path, req.Repository.RelativePath), ".git")
bundlePath := backupPath + ".bundle"
customHooksPath := filepath.Join(backupPath, "custom_hooks.tar")
if err := os.MkdirAll(backupPath, 0700); err != nil {
return fmt.Errorf("filesystem: %w", err)
}
if err := fs.writeBundle(ctx, bundlePath, req.Server, req.Repository); err != nil {
return fmt.Errorf("filesystem: write bundle: %w", err)
}
if err := fs.writeCustomHooks(ctx, customHooksPath, req.Server, req.Repository); err != nil {
return fmt.Errorf("filesystem: write custom hooks: %w", err)
}
return nil
}
// RestoreRequest is the request to restore from a backup
type RestoreRequest struct {
Server storage.ServerInfo
Repository *gitalypb.Repository
AlwaysCreate bool
}
// Restore restores a repository from a backup on a local filesystem
func (fs *Filesystem) Restore(ctx context.Context, req *RestoreRequest) error {
backupPath := strings.TrimSuffix(filepath.Join(fs.path, req.Repository.RelativePath), ".git")
bundlePath := backupPath + ".bundle"
customHooksPath := filepath.Join(backupPath, "custom_hooks.tar")
if err := fs.removeRepository(ctx, req.Server, req.Repository); err != nil {
return fmt.Errorf("filesystem: %w", err)
}
if err := fs.restoreBundle(ctx, bundlePath, req.Server, req.Repository); err != nil {
// For compatibility with existing backups we need to always create the
// repository even if there's no bundle for project repositories
// (not wiki or snippet repositories). Gitaly does not know which
// repository is which type so here we accept a parameter to tell us
// to employ this behaviour.
if req.AlwaysCreate && errors.Is(err, ErrSkipped) {
if err := fs.createRepository(ctx, req.Server, req.Repository); err != nil {
return fmt.Errorf("filesystem: %w", err)
}
} else {
return fmt.Errorf("filesystem: %w", err)
}
}
if err := fs.restoreCustomHooks(ctx, customHooksPath, req.Server, req.Repository); err != nil {
return fmt.Errorf("filesystem: %w", err)
}
return nil
}
func (fs *Filesystem) isEmpty(ctx context.Context, server storage.ServerInfo, repo *gitalypb.Repository) (bool, error) {
repoClient, err := fs.newRepoClient(ctx, server)
if err != nil {
return false, fmt.Errorf("isEmpty: %w", err)
}
hasLocalBranches, err := repoClient.HasLocalBranches(ctx, &gitalypb.HasLocalBranchesRequest{Repository: repo})
switch {
case status.Code(err) == codes.NotFound:
return true, nil
case err != nil:
return false, fmt.Errorf("isEmpty: %w", err)
}
return !hasLocalBranches.GetValue(), nil
}
func (fs *Filesystem) removeRepository(ctx context.Context, server storage.ServerInfo, repo *gitalypb.Repository) error {
repoClient, err := fs.newRepoClient(ctx, server)
if err != nil {
return fmt.Errorf("remove repository: %w", err)
}
if _, err := repoClient.RemoveRepository(ctx, &gitalypb.RemoveRepositoryRequest{Repository: repo}); err != nil {
return fmt.Errorf("remove repository: %w", err)
}
return nil
}
func (fs *Filesystem) createRepository(ctx context.Context, server storage.ServerInfo, repo *gitalypb.Repository) error {
repoClient, err := fs.newRepoClient(ctx, server)
if err != nil {
return fmt.Errorf("create repository: %w", err)
}
if _, err := repoClient.CreateRepository(ctx, &gitalypb.CreateRepositoryRequest{Repository: repo}); err != nil {
return fmt.Errorf("create repository: %w", err)
}
return nil
}
func (fs *Filesystem) writeBundle(ctx context.Context, path string, server storage.ServerInfo, repo *gitalypb.Repository) error {
repoClient, err := fs.newRepoClient(ctx, server)
if err != nil {
return err
}
stream, err := repoClient.CreateBundle(ctx, &gitalypb.CreateBundleRequest{Repository: repo})
if err != nil {
return err
}
bundle := streamio.NewReader(func() ([]byte, error) {
resp, err := stream.Recv()
return resp.GetData(), err
})
return writeFile(path, bundle)
}
func (fs *Filesystem) restoreBundle(ctx context.Context, path string, server storage.ServerInfo, repo *gitalypb.Repository) error {
f, err := os.Open(path)
if err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("%w: bundle does not exist: %q", ErrSkipped, path)
}
return fmt.Errorf("restore bundle: %w", err)
}
defer f.Close()
repoClient, err := fs.newRepoClient(ctx, server)
if err != nil {
return fmt.Errorf("restore bundle: %q: %w", path, err)
}
stream, err := repoClient.CreateRepositoryFromBundle(ctx)
if err != nil {
return fmt.Errorf("restore bundle: %q: %w", path, err)
}
request := &gitalypb.CreateRepositoryFromBundleRequest{Repository: repo}
bundle := streamio.NewWriter(func(p []byte) error {
request.Data = p
if err := stream.Send(request); err != nil {
return err
}
// Only set `Repository` on the first `Send` of the stream
request = &gitalypb.CreateRepositoryFromBundleRequest{}
return nil
})
if _, err := io.Copy(bundle, f); err != nil {
return fmt.Errorf("restore bundle: %q: %w", path, err)
}
if _, err = stream.CloseAndRecv(); err != nil {
return fmt.Errorf("restore bundle: %q: %w", path, err)
}
return nil
}
func (fs *Filesystem) writeCustomHooks(ctx context.Context, path string, server storage.ServerInfo, repo *gitalypb.Repository) error {
repoClient, err := fs.newRepoClient(ctx, server)
if err != nil {
return err
}
stream, err := repoClient.BackupCustomHooks(ctx, &gitalypb.BackupCustomHooksRequest{Repository: repo})
if err != nil {
return err
}
hooks := streamio.NewReader(func() ([]byte, error) {
resp, err := stream.Recv()
return resp.GetData(), err
})
if err := writeFile(path, hooks); err != nil {
return err
}
return nil
}
func (fs *Filesystem) restoreCustomHooks(ctx context.Context, path string, server storage.ServerInfo, repo *gitalypb.Repository) error {
f, err := os.Open(path)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return fmt.Errorf("restore custom hooks: %w", err)
}
defer f.Close()
repoClient, err := fs.newRepoClient(ctx, server)
if err != nil {
return fmt.Errorf("restore custom hooks, %q: %w", path, err)
}
stream, err := repoClient.RestoreCustomHooks(ctx)
if err != nil {
return fmt.Errorf("restore custom hooks, %q: %w", path, err)
}
request := &gitalypb.RestoreCustomHooksRequest{Repository: repo}
bundle := streamio.NewWriter(func(p []byte) error {
request.Data = p
if err := stream.Send(request); err != nil {
return err
}
// Only set `Repository` on the first `Send` of the stream
request = &gitalypb.RestoreCustomHooksRequest{}
return nil
})
if _, err := io.Copy(bundle, f); err != nil {
return fmt.Errorf("restore custom hooks, %q: %w", path, err)
}
if _, err = stream.CloseAndRecv(); err != nil {
return fmt.Errorf("restore custom hooks, %q: %w", path, err)
}
return nil
}
func (fs *Filesystem) newRepoClient(ctx context.Context, server storage.ServerInfo) (gitalypb.RepositoryServiceClient, error) {
conn, err := fs.conns.Dial(ctx, server.Address, server.Token)
if err != nil {
return nil, err
}
return gitalypb.NewRepositoryServiceClient(conn), nil
}
func writeFile(path string, r io.Reader) (returnErr error) {
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return fmt.Errorf("write file: %w", err)
}
defer func() {
if err := f.Close(); err != nil && returnErr == nil {
returnErr = fmt.Errorf("write file: %w", err)
}
}()
size, err := io.Copy(f, r)
if err != nil {
return fmt.Errorf("write file %q: %w", path, err)
}
if size == 0 {
// If the file is empty means that we received an empty stream, we delete the file
if err := os.Remove(path); err != nil {
return fmt.Errorf("write file: %w", err)
}
return nil
}
return nil
}
|