-
Notifications
You must be signed in to change notification settings - Fork 9
/
packager.go
298 lines (263 loc) · 7.39 KB
/
packager.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
// Copyright 2021 PingCAP, Inc.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package packager
import (
"archive/tar"
"bytes"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
json "github.com/json-iterator/go"
"github.com/klauspost/compress/zstd"
"github.com/pingcap/diag/pkg/crypto"
"github.com/pingcap/tiup/pkg/tui"
tiuputils "github.com/pingcap/tiup/pkg/utils"
)
type PackageOptions struct {
InputDir string // source directory of collected data
OutputFile string // target file to store packaged data
Cert string // crt to encrypt data
CertPath string // crt file to encrypt data
Rebuild bool
Meta map[string]interface{}
}
const (
TypeNoCompress = 0
TypeGZ = 01
TypeZST = 02
TypeRaw = 020
TypeEncryption = 030
)
// meta not compress
func GenerateD1agHeader(meta map[string]interface{}, compress byte, cert *x509.Certificate) ([]byte, error) {
header := []byte("D1ag")
packageType := compress & 070
var w io.Writer
metaBuf := new(bytes.Buffer)
if cert == nil {
packageType |= TypeRaw
w = metaBuf
} else {
// encryption meta information
packageType |= TypeEncryption
publicKey := cert.PublicKey.(*rsa.PublicKey)
w, _ = crypto.NewEncryptWriter(publicKey, metaBuf)
}
j, err := json.Marshal(meta)
if err != nil {
return nil, err
}
w.Write(j)
if metaBuf.Len() > 0xFFFFFF {
return nil, fmt.Errorf("the meta is too big")
}
header = append(header, packageType, byte(metaBuf.Len()>>16), byte(metaBuf.Len()>>8), byte(metaBuf.Len()))
header = append(header, metaBuf.Bytes()...)
return header, nil
}
func ParserD1agHeader(r io.Reader) (meta []byte, format, compress string, offset int, err error) {
buf := make([]byte, 8)
_, err = r.Read(buf)
if err != nil {
return nil, "", "", 0, err
}
if string(buf[0:4]) != "D1ag" {
// return nil, "legacy", "zstd", 0, nil
return nil, "", "", 0, fmt.Errorf("input is not a diag package, please use diag v0.7.0 or newer version to package and upload")
}
// byte 3~5
switch buf[4] & 070 {
case TypeRaw:
format = "unknown"
case TypeEncryption:
format = "diag"
default:
return nil, "", "", 0, fmt.Errorf("unknown type: %x", buf[4])
}
// byte 6~8
switch buf[4] & 007 {
case TypeNoCompress:
compress = "none"
case TypeGZ:
compress = "gzip"
case TypeZST:
compress = "zstd"
default:
return nil, "", "", 0, fmt.Errorf("unknown type: %x", buf[4])
}
metaLen := int(buf[5])<<16 + int(buf[6])<<8 + int(buf[7])
meta = make([]byte, metaLen)
_, err = r.Read(meta)
return meta, format, compress, metaLen + 8, err
}
func PackageCollectedData(pOpt *PackageOptions, skipConfirm bool) (string, error) {
if tiuputils.IsNotExist(filepath.Dir(pOpt.OutputFile)) {
os.MkdirAll(filepath.Dir(pOpt.OutputFile), 0755)
}
input, err := selectInputDir(pOpt.InputDir, skipConfirm)
if err != nil {
return "", err
}
output, err := selectOutputFile(input, pOpt.OutputFile)
if err != nil {
return "", err
}
if strings.HasPrefix(output, input+"/") {
return "", fmt.Errorf("the target path of the package(%s) cannot be within the given data directory", output)
}
block, _ := pem.Decode([]byte(pOpt.Cert))
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return "", err
}
publicKey := cert.PublicKey.(*rsa.PublicKey)
fileW, err := os.Create(output)
if err != nil {
return "", err
}
defer fileW.Close()
encryptW, _ := crypto.NewEncryptWriter(publicKey, fileW)
compressW, _ := zstd.NewWriter(encryptW)
defer compressW.Close()
tarW := tar.NewWriter(compressW)
defer tarW.Close()
// read cluster name and id
body, err := os.ReadFile(filepath.Join(input, "cluster.json"))
if err != nil {
return "", err
}
clusterJSON := make(map[string]interface{})
d := json.NewDecoder(bytes.NewBuffer(body))
d.UseNumber()
err = d.Decode(&clusterJSON)
if err != nil {
return "", err
}
meta := make(map[string]interface{})
meta["cluster_id"], meta["cluster_type"], err = validateClusterID(clusterJSON)
if err != nil {
return "", err
}
meta["cluster_name"], meta["begin_time"], meta["end_time"] = clusterJSON["cluster_name"], clusterJSON["begin_time"], clusterJSON["end_time"]
if topo, ok := clusterJSON["topology"].(map[string]interface{}); ok {
meta["k8s_namespace"] = topo["namespace"]
}
meta["rebuild"] = pOpt.Rebuild
var size int64
filepath.Walk(input, func(path string, info fs.FileInfo, err error) error {
if !info.IsDir() {
size += info.Size()
}
return nil
})
meta["dir_size"] = size
header, err := GenerateD1agHeader(meta, TypeZST, cert)
if err != nil {
return "", err
}
fileW.Write(header)
err = filepath.Walk(input, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
header, _ := tar.FileInfoHeader(info, "")
header.Name, _ = filepath.Rel(input, path)
//skip "."
if header.Name == "." {
return nil
}
err = tarW.WriteHeader(header)
if err != nil {
return err
}
if !info.IsDir() {
fd, err := os.Open(path)
if err != nil {
return err
}
defer fd.Close()
_, err = io.Copy(tarW, fd)
return err
}
return nil
})
return output, err
}
func selectInputDir(dir string, skipConfirm bool) (string, error) {
// choose latest diag directory if not specify
if dir == "" {
fileInfos, err := os.ReadDir(".")
if err != nil {
return dir, err
}
for _, fileInfo := range fileInfos {
info, err := os.Stat(filepath.Join(".", fileInfo.Name()))
if err != nil {
return dir, err
}
if info.IsDir() && strings.HasPrefix(fileInfo.Name(), "diag-") {
dir = fileInfo.Name()
}
}
if dir == "" {
return "", fmt.Errorf("input directory not specified and can not be auto detected")
}
fmt.Printf("found possible input directory: %s\n", dir)
if skipConfirm {
return dir, nil
}
err = tui.PromptForConfirmOrAbortError("Do you want to use it? [y/N]: ")
if err != nil {
return dir, err
}
}
_, err := os.Stat(filepath.Join(dir, "cluster.json"))
if err != nil {
return "", fmt.Errorf("%s is not a diag collected data directory", dir)
}
return filepath.Abs(dir)
}
func selectOutputFile(input, output string) (string, error) {
if output == "" {
output = filepath.Base(input) + ".diag"
}
_, err := os.Stat(output)
if err == nil {
return output, fmt.Errorf("%s already exists", output)
}
return filepath.Abs(output)
}
func validateClusterID(clusterJSON map[string]interface{}) (clusterID string, clusterType string, err error) {
clusterID, ok := clusterJSON["cluster_id"].(string)
if !ok {
clsID, ok := clusterJSON["cluster_id"].(json.Number)
if !ok {
return "", "", fmt.Errorf("cluster_id must exist in cluster.json")
}
clusterID = clsID.String()
}
clusterType, ok = clusterJSON["cluster_type"].(string)
if !ok {
return "", "", fmt.Errorf("cluster_type must exist in cluster.json")
}
if clusterType == "tidb-cluster" && clusterID == "" {
return "", "", fmt.Errorf("cluster_id should not be empty for tidb cluster, please check if PD is up when collect data")
}
return clusterID, clusterType, nil
}