-
Notifications
You must be signed in to change notification settings - Fork 7
/
rsync.go
86 lines (80 loc) · 2.41 KB
/
rsync.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
package cliaws
import (
"context"
"fmt"
"os"
"sync"
"github.com/alexflint/go-arg"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/nathants/libaws/lib"
)
func init() {
lib.Commands["ec2-rsync"] = ec2Rsync
lib.Args["ec2-rsync"] = ec2RsyncArgs{}
}
type ec2RsyncArgs struct {
Source string `arg:"positional,required"`
Destination string `arg:"positional,required"`
Selectors []string `arg:"positional,required" help:"instance-id | dns-name | private-dns-name | tag | vpc-id | subnet-id | security-group-id | ip-address | private-ip-address"`
User string `arg:"-u,--user" help:"ssh user if not tagged on instance as 'user'"`
PrivateIP bool `arg:"-p,--private-ip" help:"use ec2 private-ip instead of public-dns for host address"`
MaxConcurrency int `arg:"-m,--max-concurrency" default:"32" help:"max concurrent rsync connections"`
Key string `arg:"-k,--key" help:"rsync private key"`
Preview bool `arg:"-p,--preview"`
}
func (ec2RsyncArgs) Description() string {
return "\nrsync to ec2 instances\n"
}
func ec2Rsync() {
var args ec2RsyncArgs
arg.MustParse(&args)
ctx := context.Background()
fail := true
for _, s := range args.Selectors {
if s != "" {
fail = false
break
}
}
if fail {
lib.Logger.Fatal("error: provide some selectors")
}
instances, err := lib.EC2ListInstances(ctx, args.Selectors, ec2.InstanceStateNameRunning)
if err != nil {
lib.Logger.Fatal("error: ", err)
}
for _, instance := range instances {
lib.Logger.Println(lib.PreviewString(args.Preview)+"targeting:", lib.EC2Name(instance.Tags), *instance.InstanceId)
}
if args.Preview {
os.Exit(0)
}
if len(instances) == 0 {
err = fmt.Errorf("no instances found for those selectors")
if err != nil {
lib.Logger.Fatal("error: ", err)
}
}
results, err := lib.EC2Rsync(context.Background(), &lib.EC2RsyncInput{
Source: args.Source,
Destination: args.Destination,
User: args.User,
Instances: instances,
PrivateIP: args.PrivateIP,
MaxConcurrency: args.MaxConcurrency,
Key: args.Key,
PrintLock: sync.RWMutex{},
})
var lastErr error
for _, result := range results {
if result.Err == nil {
fmt.Fprintf(os.Stderr, "success: %s\n", lib.Green(result.InstanceID))
} else {
lastErr = err
fmt.Fprintf(os.Stderr, "failure: %s\n", lib.Red(result.InstanceID))
}
}
if lastErr != nil {
os.Exit(1)
}
}