-
Notifications
You must be signed in to change notification settings - Fork 9
/
wait_gossh.go
85 lines (79 loc) · 2.46 KB
/
wait_gossh.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
package cliaws
import (
"context"
"fmt"
"os"
"time"
"github.com/alexflint/go-arg"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/nathants/libaws/lib"
)
func init() {
lib.Commands["ec2-wait-gossh"] = ec2WaitGoSsh
lib.Args["ec2-wait-gossh"] = ec2WaitGoSshArgs{}
}
type ec2WaitGoSshArgs struct {
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:"gossh user if not tagged on instance as 'user'"`
MaxConcurrency int `arg:"-m,--max-concurrency" default:"32" help:"max concurrent waitgossh connections"`
MaxWait int `arg:"-w,--max-wait" help:"after this many seconds, terminate any instances not ready and return instance-id of all ready instances"`
Ed25519PrivKeyFile string `arg:"-e,--ed25519" help:"private key"`
RsaPrivKeyFile string `arg:"-r,--rsa" help:"private key"`
}
func (ec2WaitGoSshArgs) Description() string {
return "\nwait for gossh to succeed then return their instance ids\n"
}
func ec2WaitGoSsh() {
var args ec2WaitGoSshArgs
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")
}
start := time.Now()
var instances []*ec2.Instance
var err error
for {
instances, err = lib.EC2ListInstances(ctx, args.Selectors, "")
if err != nil {
lib.Logger.Fatal("error: ", err)
}
if time.Since(start) > 15*time.Second {
err = fmt.Errorf("no instances found for those selectors")
if err != nil {
lib.Logger.Fatal("error: ", err)
}
}
if len(instances) > 0 {
break
}
}
for _, instance := range instances {
if lib.Contains([]string{ec2.InstanceStateNamePending, ec2.InstanceStateNameRunning}, *instance.State.Name) {
lib.Logger.Println(lib.EC2Name(instance.Tags), *instance.InstanceId)
}
}
rsaBytes, _ := os.ReadFile(args.RsaPrivKeyFile)
edBytes, _ := os.ReadFile(args.Ed25519PrivKeyFile)
readyIDs, err := lib.EC2WaitGoSsh(ctx, &lib.EC2WaitGoSshInput{
Selectors: args.Selectors,
MaxWaitSeconds: args.MaxWait,
User: args.User,
MaxConcurrency: args.MaxConcurrency,
RsaPrivKey: string(rsaBytes),
Ed25519PrivKey: string(edBytes),
})
if err != nil {
lib.Logger.Fatal("error: ", err)
}
for _, id := range readyIDs {
fmt.Println(id)
}
}