-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
205 lines (189 loc) · 4.82 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
package main
import (
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/url"
"os"
"os/exec"
"time"
"github.com/google/shlex"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
"github.com/aws/aws-sdk-go-v2/service/sqs"
)
func send(ctx context.Context, client *sqs.Client, queueURL *url.URL, reader func() io.Reader) (int, error) {
bs, err := ioutil.ReadAll(reader())
if err != nil {
return 0, err
}
_, err = client.SendMessage(ctx, &sqs.SendMessageInput{
MessageBody: aws.String(string(bs)),
QueueUrl: aws.String(queueURL.String()),
DelaySeconds: 0,
})
return len(bs), err
}
func urlFor(ctx context.Context, client *sqs.Client, id string) (queueURL *url.URL, err error) {
if len(id) == 0 {
return nil, fmt.Errorf("empty SQS identifier")
}
queueURL, err = url.Parse(id)
if err == nil && queueURL.IsAbs() {
return
}
var out *sqs.GetQueueUrlOutput
if out, err = client.GetQueueUrl(ctx, &sqs.GetQueueUrlInput{
QueueName: aws.String(id),
}); err != nil {
return
}
return url.Parse(*out.QueueUrl)
}
type localStatus struct {
Hostname string
Timestamp string
Command string
ExecError string
Stdout string
Stderr string
Count int
}
func infoProvider(command string) func() io.Reader {
var count int
hostname, err := os.Hostname()
if err != nil {
hostname = fmt.Sprintf("unknown (%s)", err.Error())
}
return func() io.Reader {
var status = localStatus{
Hostname: hostname,
Timestamp: time.Now().Format(time.RFC1123Z),
Count: count,
}
count++
runCommand(command, &status)
bs, err := json.MarshalIndent(status, "", " ")
if err != nil {
log.Fatalf("creating info %v", err)
}
return bytes.NewReader(bs)
}
}
func runCommand(command string, status *localStatus) {
if command == "" {
return
}
status.Command = command
args, err := shlex.Split(command)
if err != nil {
status.ExecError = fmt.Sprintf("unable to parse command line: %v", err)
return
}
cmd := exec.Command(args[0], args[1:]...)
devnull, err := os.Open(os.DevNull)
if err != nil {
status.ExecError = fmt.Sprintf("unable to open %s: %v", os.DevNull, err)
return
}
defer devnull.Close()
cmd.Stdin = devnull
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
status.ExecError = fmt.Sprintf("unable to exec: %v", err)
} else {
status.Stdout = string(stdout.Bytes())
status.Stderr = string(stderr.Bytes())
}
}
func stdinProvider() func() io.Reader {
bs, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatalf("unable to read from stdin: %v", err)
}
return func() io.Reader {
return bytes.NewReader(bs)
}
}
func fileProvider(fileName string) func() io.Reader {
bs, err := ioutil.ReadFile(fileName)
if err != nil {
log.Fatalf("unable to read file '%s': %v", fileName, err)
}
return func() io.Reader {
return bytes.NewReader(bs)
}
}
// On EC2 instances this will return the default (local) region; on
// other servers will have no effect.
func imdsRegion(ctx context.Context) *string {
cfg, err := config.LoadDefaultConfig(ctx)
if err != nil {
return nil
}
client := imds.NewFromConfig(cfg)
region, err := client.GetRegion(ctx, &imds.GetRegionInput{})
if err != nil {
return nil
}
return ®ion.Region
}
func main() {
var in func() io.Reader
var err error
fileName := flag.String("f", "", "file to send")
count := flag.Int("c", 1, "send the message this many times")
interval := flag.Duration("i", time.Millisecond*200, "how long to wait between sends")
region := flag.String("region", "local", "AWS region")
command := flag.String("command", "", "command to run")
flag.Parse()
if *fileName != "" && *command != "" {
log.Fatalf("cannot specify both -command and -f flags")
}
switch *fileName {
case "":
in = infoProvider(*command)
case "-":
in = stdinProvider()
default:
in = fileProvider(*fileName)
}
ctx := context.Background()
cfg, err := config.LoadDefaultConfig(ctx, func(c *config.LoadOptions) error {
if *region != "local" {
c.Region = *region
} else if localRegion := imdsRegion(ctx); localRegion != nil {
c.Region = *localRegion
}
return nil
})
if err != nil {
log.Fatalf("unable to load AWS configuration, %v", err)
}
sqsClient := sqs.NewFromConfig(cfg)
queueId := flag.Arg(0)
queueURL, err := urlFor(ctx, sqsClient, queueId)
if err != nil {
log.Fatalf("unable to find queue URL for '%s': %v", queueId, err)
}
for i := 0; i < *count; i++ {
start := time.Now()
var sentBytes int
if sentBytes, err = send(ctx, sqsClient, queueURL, in); err != nil {
log.Fatalf("unable to send message: %v", err)
}
diff := time.Now().Sub(start)
fmt.Printf("%d bytes to %s: time=%d ms\n", sentBytes, queueURL, diff.Milliseconds())
if i < (*count - 1) {
time.Sleep(*interval)
}
}
}