-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdipper.go
More file actions
242 lines (181 loc) · 6.05 KB
/
Copy pathdipper.go
File metadata and controls
242 lines (181 loc) · 6.05 KB
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
package main
import (
"archive/tar"
"bytes"
"compress/gzip"
"context"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"go.uber.org/zap"
)
// dipperUploadURL is the Dipper endpoint that receives the image archive.
const dipperUploadURL = "https://dipper.perconatest.com/mizar/pmm"
var (
dipperTokenRe = regexp.MustCompile(`^dipper_[a-zA-Z0-9_-]{41}$`)
dipperProjectIDRe = regexp.MustCompile(`^(CS|RITM|PS)\d+$`)
)
// executeDipperSync archives the images in the supplied directory and uploads
// the resulting .tar.gz to Dipper.
func executeDipperSync(cfg *LumoConfig) {
validateDipperSyncFlags(cfg)
zap.S().Infof("Archiving images from '%s'...", cfg.SyncDir)
archive, count, err := createImageArchive(cfg.SyncDir)
if err != nil {
zap.S().Fatalf("error creating archive: %v", err)
}
zap.S().Infof("Compressed %d image(s) (%d bytes). Uploading to Dipper...", count, len(archive))
msg, err := uploadArchive(cfg, archive)
if err != nil {
zap.S().Fatalf("error uploading archive: %v", err)
}
zap.S().Infof("Successfully uploaded %d image(s) for project '%s' (host '%s')", count, cfg.DipperProjectID, cfg.Hostname)
if msg != "" {
zap.S().Infof("Dipper: %s", msg)
}
}
// validateDipperSyncFlags ensures all required flags and the positional
// argument are present and correctly formatted.
func validateDipperSyncFlags(cfg *LumoConfig) {
if cfg.DipperToken == "" {
zap.S().Fatalf("error: -token %v", ErrFlagRequired)
}
if cfg.DipperProjectID == "" {
zap.S().Fatalf("error: -projectid %v", ErrFlagRequired)
}
if cfg.Hostname == "" {
zap.S().Fatalf("error: -hostname %v", ErrFlagRequired)
}
if cfg.SyncDir == "" {
zap.S().Fatalf("error: %v", ErrSyncDirRequired)
}
if !dipperProjectIDRe.MatchString(cfg.DipperProjectID) {
zap.S().Fatalf("error: %v (got %q)", ErrInvalidDipperProjectID, cfg.DipperProjectID)
}
// Deliberately avoid logging the token value itself
if !dipperTokenRe.MatchString(cfg.DipperToken) {
zap.S().Fatalf("error: %v", ErrInvalidDipperToken)
}
info, err := os.Stat(cfg.SyncDir)
if err != nil {
zap.S().Fatalf("error: cannot access directory '%s': %v", cfg.SyncDir, err)
}
if !info.IsDir() {
zap.S().Fatalf("error: %v: '%s'", ErrNotADirectory, cfg.SyncDir)
}
}
// createImageArchive compresses every image file in dir into a .tar.gz,
// returning the archive bytes and the number of files included.
func createImageArchive(dir string) ([]byte, int, error) {
entries, err := os.ReadDir(dir)
if err != nil {
return nil, 0, fmt.Errorf("%w: %w", ErrReadingDir, err)
}
var buf bytes.Buffer
gw := gzip.NewWriter(&buf)
tw := tar.NewWriter(gw)
count := 0
for _, entry := range entries {
if entry.IsDir() || !strings.EqualFold(filepath.Ext(entry.Name()), ".png") {
continue
}
if err := addFileToArchive(tw, dir, entry.Name()); err != nil {
return nil, 0, err
}
count++
}
// The tar and gzip writers must be closed before the buffer is read
if err := tw.Close(); err != nil {
return nil, 0, fmt.Errorf("%w: %w", ErrArchive, err)
}
if err := gw.Close(); err != nil {
return nil, 0, fmt.Errorf("%w: %w", ErrArchive, err)
}
if count == 0 {
return nil, 0, ErrNoImages
}
return buf.Bytes(), count, nil
}
// addFileToArchive writes a single file into the tar writer.
func addFileToArchive(tw *tar.Writer, dir, name string) error {
data, err := os.ReadFile(filepath.Join(dir, name)) // #nosec
if err != nil {
return fmt.Errorf("%w: %w", ErrReadingFile, err)
}
hdr := &tar.Header{
Name: name,
Mode: 0o600,
Size: int64(len(data)),
ModTime: time.Now(),
}
if err := tw.WriteHeader(hdr); err != nil {
return fmt.Errorf("%w: %w", ErrArchive, err)
}
if _, err := tw.Write(data); err != nil {
return fmt.Errorf("%w: %w", ErrArchive, err)
}
return nil
}
// uploadArchive POSTs the archive to Dipper as multipart/form-data along with
// the project_id and hostname fields and the X-Dipper-Auth header. It parses
// the JSON response ({"ok": bool, "msg": string}) and returns the message,
// treating ok=false as a failure.
func uploadArchive(cfg *LumoConfig, archive []byte) (string, error) {
var body bytes.Buffer
writer := multipart.NewWriter(&body)
if err := writer.WriteField("project_id", cfg.DipperProjectID); err != nil {
return "", fmt.Errorf("%w: %w", ErrArchive, err)
}
if err := writer.WriteField("hostname", cfg.Hostname); err != nil {
return "", fmt.Errorf("%w: %w", ErrArchive, err)
}
part, err := writer.CreateFormFile("pmmData", cfg.Hostname+".tar.gz")
if err != nil {
return "", fmt.Errorf("%w: %w", ErrArchive, err)
}
if _, err := part.Write(archive); err != nil {
return "", fmt.Errorf("%w: %w", ErrArchive, err)
}
if err := writer.Close(); err != nil {
return "", fmt.Errorf("%w: %w", ErrArchive, err)
}
ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodPost, dipperUploadURL, &body)
if err != nil {
return "", fmt.Errorf("%w: %w", ErrCreateRequest, err)
}
req.Header.Set("Content-Type", writer.FormDataContentType())
req.Header.Set("X-Dipper-Auth", cfg.DipperToken)
resp, err := httpClient.Do(req)
if err != nil {
return "", fmt.Errorf("%w: %w", ErrExecRequest, err)
}
defer func() { _ = resp.Body.Close() }()
return parseUploadResponse(resp)
}
// parseUploadResponse reads and interprets the Dipper JSON response body.
func parseUploadResponse(resp *http.Response) (string, error) {
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("%w: %w", ErrReadResponse, err)
}
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices {
return "", fmt.Errorf("%w: HTTP %d: %s", ErrUploadFailed, resp.StatusCode, strings.TrimSpace(string(respBody)))
}
var dr DipperResponse
if err := json.Unmarshal(respBody, &dr); err != nil {
return "", fmt.Errorf("%w: HTTP %d: %s", ErrUploadFailed, resp.StatusCode, strings.TrimSpace(string(respBody)))
}
if !dr.Ok {
return "", fmt.Errorf("%w: %s", ErrUploadFailed, dr.Msg)
}
return dr.Msg, nil
}