-
Notifications
You must be signed in to change notification settings - Fork 15
/
root.go
216 lines (173 loc) · 5.5 KB
/
root.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
package cmd
import (
"fmt"
"net/mail"
"os"
"github.com/docker/go-units"
"github.com/schollz/progressbar/v3"
uploader "github.com/yugabyte/yb-tools/yb-support-tool/sendsafelyuploader"
"github.com/spf13/cobra"
)
var Version = "pre-release"
// uploader variables
var (
verbose bool
// dropzone details
dropzoneID string
uploaderURL string
// uploader variables
caseNum int
email string
// package settings
concurrency int
retries uint
partSize int64
partSizeString string
)
const (
defaultRetries = 5
// testing shows these to be a good balance of speed and lower memory / CPU usage
defaultConcurency = 10
defaultPartSize = 10 * units.MiB
)
// globals
const (
YBUploaderURL = "https://secure-upload.yugabyte.com"
YBDropzoneID = "BdFZz_JoZqtqPVueANkspD86KZ_PJsW1kIf_jVHeCO0"
)
var (
rootCmd = &cobra.Command{
Use: "yb-support-tool",
Short: "Yugabyte Support Tool",
}
uploadCmd = &cobra.Command{
Use: "upload -c [case number] -e [email] [files]",
Short: "Upload attachment to a support case",
Long: `Uploads a file or files up to a limit of 100GB to a support ticket. Requires exising ticket to be created`,
Args: cobra.RangeArgs(1, 10),
Run: func(cmd *cobra.Command, args []string) {
if err := Upload(email, caseNum, args); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(2)
}
},
}
versionCmd = &cobra.Command{
Use: "version",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(Version)
os.Exit(0)
},
}
)
func Execute() {
if err := rootCmd.Execute(); err != nil {
_, err := fmt.Fprintln(os.Stderr, err)
if err != nil {
return
}
os.Exit(1)
}
}
// nolint: errcheck
func init() {
// root command flags
//subcommands
rootCmd.AddCommand(uploadCmd)
rootCmd.AddCommand(versionCmd)
rootCmd.PersistentFlags().BoolVar(&verbose, "verbose", false, "Print verbose logs")
// upload command flags
uploadCmd.Flags().IntVarP(&caseNum, "case", "c", 0, "Zendesk case number to attach files to (required)")
uploadCmd.Flags().StringVarP(&email, "email", "e", "", "Email address of submitter (required)")
uploadCmd.Flags().IntVarP(&concurrency, "parallelism", "p", defaultConcurency, "parallelism")
uploadCmd.Flags().UintVar(&retries, "retries", defaultRetries, "number file upload retry attempts")
uploadCmd.Flags().StringVar(&partSizeString, "partSize", "", "Part size used to upload file. Must be between 1KB and 2.5MB")
uploadCmd.MarkFlagRequired("case")
uploadCmd.MarkFlagRequired("email")
// default dropzone ID is set to Yugabyte Support's anonymous dropzone
// this can be overridden with the --dropzone_id flag
uploadCmd.Flags().StringVar(&dropzoneID, "dropzone_id", YBDropzoneID, "Override default dropzone ID")
// hide the dropzone_id flag from the help menu
// default uploader URL is set to Yugabyte Support's instance of sendsafely
// this can be overridden with the --secure_upload_url flag
uploadCmd.Flags().StringVar(&uploaderURL, "secure_upload_url", YBUploaderURL, "Override default secure uploader URL")
// Hidden flags: partSize, retries, parallelism, url
uploadCmd.Flags().MarkHidden("partSize")
uploadCmd.Flags().MarkHidden("secure_upload_url")
uploadCmd.Flags().MarkHidden("parallelism")
}
func Upload(email string, caseNum int, files []string) error {
validateArgs()
u := uploader.CreateUploader(uploaderURL, dropzoneID, "DROP_ZONE")
p, err := u.CreateDropzonePackage(uploader.WithConcurrency(concurrency), uploader.WithRetries(retries), uploader.WithChunkSize(partSize))
if err != nil {
return fmt.Errorf("Unable to create dropzone package: %s", err)
}
for _, fileName := range files {
file, err := os.Open(fileName)
if err != nil {
return err
}
fmt.Printf("Preparing to upload %s\n", fileName)
f, err := p.AddFileToPackage(file)
if err != nil {
return fmt.Errorf("Unable to add file to package: %s", err)
}
bar := progressbar.Default(int64(f.Info.Parts), fmt.Sprintf("Uploading file: %s", f.Info.Name))
progressbar.OptionSetItsString("part")(bar)
go func() {
for i := range f.Status {
_ = bar.Add(i)
}
}()
if err := p.UploadFileParts(f); err != nil {
return fmt.Errorf("Unable to upload file parts: %s", err)
}
if err := p.MarkFileComplete(f); err != nil {
return err
}
}
fmt.Printf("File uploads complete\nFinalizing Package...\n")
err = p.FinalizePackage()
if err != nil {
return fmt.Errorf("Unable to finalize package: %s", err)
}
if verbose {
fmt.Printf("Package available at: %s\n", p.URL)
}
// Step 7 - Invoke the Hosted Dropzone Submission Endpoint
if err := p.SubmitHostedDropzone(fmt.Sprint(caseNum), email); err != nil {
return fmt.Errorf("Unable to push file to Hosted Dropzone: %s", err)
}
return nil
}
func validateArgs() {
var err error
if partSizeString != "" {
partSize, err = units.FromHumanSize(partSizeString)
if err != nil {
printErrorAndExit("Unable to parse --partSize flag: %s\n", err)
}
} else {
partSize = defaultPartSize
}
if concurrency < 1 || concurrency > 100 {
fmt.Printf("Parallism must be a value between 1 and 100, reverting to default of %d\n", defaultConcurency)
concurrency = defaultConcurency
}
if retries > 100 {
fmt.Fprintf(os.Stderr, "Setting retries to the max of 100\n")
retries = defaultRetries
}
if caseNum < 1 {
printErrorAndExit("Case number must be a positive value\n")
}
_, err = mail.ParseAddress(email)
if err != nil {
printErrorAndExit("Invalid email provided: %s\n", email)
}
}
func printErrorAndExit(format string, a ...any) {
fmt.Fprintf(os.Stderr, format, a...)
os.Exit(1)
}