-
Notifications
You must be signed in to change notification settings - Fork 2
/
run.go
134 lines (113 loc) · 2.92 KB
/
run.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
package main
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ssm"
"github.com/glassechidna/awscredcache"
"github.com/glassechidna/gossm/pkg/gossm"
"github.com/glassechidna/gossm/pkg/gossm/printer"
"github.com/pquerna/otp/totp"
"io"
"os"
"path/filepath"
"time"
)
func doit(sess *session.Session, shellType, command string, files, quiet bool, timeout int64, tagPairs, instanceIds []string) {
client := gossm.New(sess)
printer := printer.New()
printer.Quiet = quiet
docName := "AWS-RunShellScript"
if shellType == "powershell" {
docName = "AWS-RunPowerShellScript"
}
targets := gossm.MakeTargets(tagPairs, instanceIds)
input := &ssm.SendCommandInput{
DocumentName: &docName,
Targets: targets,
TimeoutSeconds: &timeout,
CloudWatchOutputConfig: &ssm.CloudWatchOutputConfig{
CloudWatchOutputEnabled: aws.Bool(true),
},
Parameters: map[string][]*string{
"commands": aws.StringSlice([]string{command}),
},
}
resp, err := client.Doit(context.Background(), input)
if err != nil {
panic(err)
}
printer.PrintInfo(command, resp)
if files {
printToFiles(resp)
} else {
for msg := range resp.Channel {
printer.Print(msg)
}
}
}
func printToFiles(resp *gossm.DoitResponse) {
dir, err := filepath.Abs(resp.CommandId)
if err != nil {
panic(err)
}
err = os.Mkdir(dir, 0755)
if err != nil {
panic(err)
}
files := map[string]io.WriteCloser{}
for _, id := range resp.InstanceIds.InstanceIds {
fpath := filepath.Join(dir, fmt.Sprintf("%s.txt", id))
file, err := os.Create(fpath)
if err != nil {
panic(err)
}
files[id] = file
}
for msg := range resp.Channel {
file := files[msg.InstanceId]
_, err = file.Write([]byte(msg.StdoutChunk))
if err != nil {
panic(err)
}
_, err = file.Write([]byte(msg.StderrChunk))
if err != nil {
panic(err)
}
}
for _, file := range files {
err = file.Close()
if err != nil {
panic(err)
}
}
}
func AwsSession(profile, region string) *session.Session {
provider := awscredcache.NewAwsCacheCredProvider(profile)
provider.MfaCodeProvider = func(mfaSecret string) (string, error) {
if len(mfaSecret) == 0 {
return stscreds.StdinTokenProvider()
} else {
return totp.GenerateCode(mfaSecret, time.Now())
}
}
chain := provider.WrapInChain()
creds := credentials.NewCredentials(chain)
sessOpts := session.Options{
SharedConfigState: session.SharedConfigEnable,
AssumeRoleTokenProvider: stscreds.StdinTokenProvider,
Config: aws.Config{Credentials: creds},
}
if len(profile) > 0 {
sessOpts.Profile = profile
}
sess, _ := session.NewSessionWithOptions(sessOpts)
//sess.Config.LogLevel = aws.LogLevel(aws.LogDebugWithHTTPBody)
if len(region) > 0 {
sess.Config.Region = ®ion
}
return sess
}