-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.go
255 lines (216 loc) · 5.8 KB
/
client.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
package awscli
import (
"context"
"fmt"
"github.com/kolesa-team/scylla-octopus/pkg/cmd"
"github.com/kolesa-team/scylla-octopus/pkg/entity"
"github.com/pkg/errors"
"go.uber.org/zap"
"os/exec"
"strings"
)
type Client struct {
options Options
logger *zap.SugaredLogger
}
type Options struct {
Disabled bool
Binary string
Bucket string
EndpointUrl string `yaml:"endpointUrl"`
Profile string
}
func NewClient(opts Options, logger *zap.SugaredLogger) *Client {
if len(opts.Binary) == 0 {
opts.Binary = "aws"
}
opts.Bucket = strings.Trim(opts.Bucket, "/")
return &Client{
options: opts,
logger: logger.Named("awscli").With("bucket", opts.Bucket),
}
}
// Healthcheck ensures awscli executable exists
func (c *Client) Healthcheck(ctx context.Context, cmdExecutor cmd.Executor) error {
if c.options.Disabled {
return nil
}
if len(c.options.Bucket) == 0 {
return errors.New("[healthcheck] bucket is required")
}
c.logger.Debugw(
"[healthcheck] checking aws cli binary",
"path",
c.options.Binary,
)
return cmd.ExecutableFileExists(ctx, cmdExecutor, c.options.Binary)
}
// Upload uploads a given source directory to s3
func (c *Client) Upload(ctx context.Context, cmdExecutor cmd.Executor, source, dest string) (string, error) {
destUrl := c.getDestinationUrl(dest)
err := c.sync(ctx, cmdExecutor, source, destUrl)
return destUrl, err
}
// ListBackups returns backups from a given directory
func (c *Client) ListBackups(ctx context.Context, cmdExecutor cmd.Executor, basePath string) ([]entity.RemoteBackup, error) {
backups := []entity.RemoteBackup{}
// TODO the backups are kept at a 3rd leven of hierarchy, e.g. /basePath/datacenter/scylla-node1/09-07-2021-10-29
// this probably should not be hardcoded
paths, err := c.listDirectoriesRecursive(ctx, cmdExecutor, basePath, 3)
if err != nil {
c.logger.Errorw(
"could not list backups",
"error", err,
)
// the error most probably means there are no files at given path,
// so we allow ourselves ignore it.
return backups, nil
}
for _, path := range paths {
backup, err := entity.NewRemoteBackupFromPath(path)
if err == nil {
backups = append(backups, backup)
}
}
return backups, nil
}
// RemoveBackup removes given backup directory
func (c *Client) RemoveBackup(ctx context.Context, cmdExecutor cmd.Executor, path string) error {
command := cmd.Command(
c.options.Binary,
"s3",
"rm",
fmt.Sprintf("'%s'", c.getDestinationUrl(path)),
"--recursive",
)
c.addCommandFlags(command)
output, err := cmdExecutor.Execute(ctx, command)
if err != nil {
c.logger.Errorw(
"could not remove a backup",
"error", err,
"output", string(output),
"path", path,
)
return errors.Wrapf(
err,
"could not remove a backup at %s. output: %s",
path,
string(output),
)
}
c.logger.Infow("backup removed", "path", path)
return nil
}
// Returns a complete url to a destination directory in s3 format
func (c *Client) getDestinationUrl(dest string) string {
url := fmt.Sprintf(
"s3://%s",
c.options.Bucket,
)
if len(dest) > 0 {
url += "/" + strings.TrimLeft(dest, "/")
}
return url
}
// Runs "aws s3 sync" to sync a directory with a bucket.
// see https://docs.aws.amazon.com/cli/latest/userguide/cli-services-s3-commands.html#using-s3-commands-managing-objects-sync
func (c *Client) sync(ctx context.Context, cmdExecutor cmd.Executor, source, dest string) error {
command := cmd.Command(
c.options.Binary,
"s3",
"sync",
source,
fmt.Sprintf("'%s'", dest),
)
c.addCommandFlags(command)
output, err := cmdExecutor.Execute(ctx, command)
if err != nil {
return errors.Wrapf(
err,
"could not sync files to %s. output: %s",
dest,
string(output),
)
}
return nil
}
// Runs "aws s3 ls" to list directories recursively until it reaches a given depth.
func (c *Client) listDirectoriesRecursive(ctx context.Context, cmdExecutor cmd.Executor, path string, depth int) ([]string, error) {
result := []string{}
if ctx.Err() != nil {
return result, ctx.Err()
}
dirs, err := c.listDirectories(ctx, cmdExecutor, path)
if err != nil || depth == 0 {
return dirs, err
}
for _, dir := range dirs {
tmp, err := c.listDirectoriesRecursive(ctx, cmdExecutor, dir, depth-1)
if err != nil {
return result, err
}
if len(tmp) > 0 {
result = append(result, tmp...)
} else {
result = append(result, dir)
}
}
return result, nil
}
// Runs "aws s3 ls" to get directories at given path
func (c *Client) listDirectories(ctx context.Context, cmdExecutor cmd.Executor, path string) ([]string, error) {
command := cmd.Command(
c.options.Binary,
"s3",
"ls",
fmt.Sprintf("'%s/'", c.getDestinationUrl(path)),
)
c.addCommandFlags(command)
output, err := cmdExecutor.Execute(ctx, command)
if err != nil {
return []string{}, errors.Wrapf(
err,
"could not list files at %s. command: %s\noutput: %s",
path,
command.String(),
string(output),
)
}
return c.parseDirectoryList(path, string(output)), nil
}
// Returns a list of directories from "aws s3 ls" output
func (c *Client) parseDirectoryList(basePath string, output string) []string {
var result []string
lines := strings.Split(output, "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
// the directories have a "PRE" prefix
// see https://docs.aws.amazon.com/cli/latest/reference/s3/ls.html#examples
if !strings.HasPrefix(line, "PRE ") {
continue
}
line = strings.TrimPrefix(line, "PRE ")
result = append(
result,
basePath+"/"+strings.TrimRight(line, "/"),
)
}
return result
}
func (c *Client) addCommandFlags(command *exec.Cmd) {
if len(c.options.EndpointUrl) > 0 {
command.Args = append(
command.Args,
"--endpoint-url",
c.options.EndpointUrl,
)
}
if len(c.options.Profile) > 0 {
command.Args = append(
command.Args,
"--profile",
c.options.Profile,
)
}
}