forked from peak/s5cmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
228 lines (191 loc) · 6.18 KB
/
main.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
package main
import (
"context"
"flag"
"fmt"
"log"
"math"
"os"
"os/signal"
"runtime"
"strings"
"syscall"
"time"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/google/gops/agent"
"github.com/peak/s5cmd/complete"
"github.com/peak/s5cmd/core"
"github.com/peak/s5cmd/stats"
"github.com/peak/s5cmd/version"
)
//go:generate go run version/cmd/generate.go
var (
GitSummary = version.GitSummary
GitBranch = version.GitBranch
)
func printOps(name string, counter uint64, elapsed time.Duration, extra string) {
if counter == 0 {
return
}
secs := elapsed.Seconds()
if secs == 0 {
secs = 1
}
ops := uint64(math.Floor((float64(counter) / secs) + 0.5))
log.Printf("# Stats: %-7s %10d %4d ops/sec%s", name, counter, ops, extra)
}
func main() {
const (
bytesInMb = float64(1024 * 1024)
minNumWorkers = 2
defaultWorkerCount = 256
defaultUploadConcurrency = s3manager.DefaultUploadConcurrency
defaultDownloadConcurrency = s3manager.DefaultDownloadConcurrency
)
var (
flagCommandFile = flag.String("f", "", "Commands-file or - for stdin")
flagEndpointURL = flag.String("endpoint-url", "", "Override default URL with the given one")
flagWorkerCount = flag.Int("numworkers", defaultWorkerCount, fmt.Sprintf("Number of worker goroutines. Negative numbers mean multiples of the CPU core count."))
flagDownloadConcurrency = flag.Int("dw", defaultDownloadConcurrency, "Download concurrency for each file")
flagDownloadPartSize = flag.Int("ds", 50, "Multipart chunk size in MB for downloads")
flagUploadConcurrency = flag.Int("uw", defaultUploadConcurrency, "Upload concurrency for each file")
flagUploadPartSize = flag.Int("us", 50, "Multipart chunk size in MB for uploads")
flagRetryCount = flag.Int("r", 10, "Retry S3 operations N times before failing")
flagPrintStats = flag.Bool("stats", false, "Always print stats")
flagShowVersion = flag.Bool("version", false, "Prints current version")
flagEnableGops = flag.Bool("gops", false, "Initialize gops agent")
flagVerbose = flag.Bool("vv", false, "Verbose output")
flagNoVerifySSL = flag.Bool("no-verify-ssl", false, "Don't verify SSL certificates")
)
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "%v\n\n", core.UsageLine())
fmt.Fprint(os.Stderr, "Options:\n")
flag.PrintDefaults()
cl := core.CommandList()
fmt.Fprint(os.Stderr, "\nCommands:")
fmt.Fprintf(os.Stderr, "\n %v\n", strings.Join(cl, ", "))
fmt.Fprintf(os.Stderr, "\nTo get help on a specific command, run \"%v <command> -h\"\n", os.Args[0])
}
if done, err := complete.ParseFlagsAndRun(); err != nil {
log.Fatal("-ERR " + err.Error())
} else if done {
os.Exit(0)
}
if *flagEnableGops || os.Getenv("S5CMD_GOPS") != "" {
if err := agent.Listen(&agent.Options{NoShutdownCleanup: true}); err != nil {
log.Fatal("-ERR", err)
}
}
if *flagShowVersion {
fmt.Printf("s5cmd version %s", GitSummary)
if GitBranch != "" {
fmt.Printf(" (from branch %s)", GitBranch)
}
fmt.Print("\n")
os.Exit(0)
}
if flag.Arg(0) == "" && *flagCommandFile == "" {
flag.Usage()
os.Exit(2)
}
cmd := strings.Join(flag.Args(), " ")
if cmd != "" && *flagCommandFile != "" {
log.Fatal("-ERR Only specify -f or command, not both")
}
if (cmd == "" && *flagCommandFile == "") || *flagWorkerCount == 0 || *flagUploadPartSize < 1 || *flagRetryCount < 0 {
log.Fatal("-ERR Please specify all arguments.")
}
ulPartSizeBytes := int64(*flagUploadPartSize * int(bytesInMb))
if ulPartSizeBytes < s3manager.MinUploadPartSize {
log.Fatalf("-ERR Multipart chunk size should be greater than %d", int(math.Ceil(float64(s3manager.MinUploadPartSize)/bytesInMb)))
}
dlPartSizeBytes := int64(*flagDownloadPartSize * int(bytesInMb))
if dlPartSizeBytes < int64(5*bytesInMb) {
log.Fatalf("-ERR Download part size should be greater than 5")
}
if *flagDownloadConcurrency < 1 || *flagUploadConcurrency < 1 {
log.Fatalf("-ERR Download/Upload concurrency should be greater than 1")
}
var cmdMode bool
if cmd != "" {
cmdMode = true
}
if *flagWorkerCount < 0 {
*flagWorkerCount = runtime.NumCPU() * -*flagWorkerCount
}
if *flagWorkerCount < minNumWorkers {
*flagWorkerCount = minNumWorkers
}
*flagEndpointURL = strings.TrimSpace(*flagEndpointURL)
startTime := time.Now()
if !cmdMode {
log.Printf("# Using %d workers", *flagWorkerCount)
}
parentCtx, cancelFunc := context.WithCancel(context.Background())
exitCode := -1
exitFunc := func(code int) {
exitCode = code
cancelFunc()
}
ctx := context.WithValue(
context.WithValue(
parentCtx,
core.ExitFuncKey,
exitFunc,
),
core.CancelFuncKey,
cancelFunc,
)
go func() {
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
<-ch
log.Print("# Got signal, cleaning up...")
cancelFunc()
}()
s := stats.Stats{}
core.Verbose = *flagVerbose
wp := core.NewWorkerPool(ctx,
&core.WorkerPoolParams{
NumWorkers: *flagWorkerCount,
UploadChunkSizeBytes: ulPartSizeBytes,
UploadConcurrency: *flagUploadConcurrency,
DownloadChunkSizeBytes: dlPartSizeBytes,
DownloadConcurrency: *flagDownloadConcurrency,
Retries: *flagRetryCount,
EndpointURL: *flagEndpointURL,
NoVerifySSL: *flagNoVerifySSL,
}, &s)
if cmdMode {
wp.RunCmd(cmd)
} else {
wp.Run(*flagCommandFile)
}
elapsed := time.Since(startTime)
failops := s.Get(stats.Fail)
// if exitCode is -1 (default) and if we have at least one absolute-fail,
// exit with code 127
if exitCode == -1 {
if failops > 0 {
exitCode = 127
} else {
exitCode = 0
}
}
if !cmdMode {
log.Printf("# Exiting with code %d", exitCode)
}
if !cmdMode || *flagPrintStats {
s3ops := s.Get(stats.S3Op)
fileops := s.Get(stats.FileOp)
shellops := s.Get(stats.ShellOp)
retryops := s.Get(stats.RetryOp)
printOps("S3", s3ops, elapsed, "")
printOps("File", fileops, elapsed, "")
printOps("Shell", shellops, elapsed, "")
printOps("Retried", retryops, elapsed, "")
printOps("Failed", failops, elapsed, "")
printOps("Total", s3ops+fileops+shellops+failops, elapsed, fmt.Sprintf(" %v", elapsed))
}
os.Exit(exitCode)
}