/
runner.go
55 lines (45 loc) · 1.33 KB
/
runner.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
package invocation
import (
"fmt"
"time"
"github.com/aws/aws-sdk-go/service/ssm"
"github.com/aws/aws-sdk-go/service/ssm/ssmiface"
)
func GetTargets(client ssmiface.SSMAPI, commandID *string) (targets []*string, err error) {
var out *ssm.ListCommandInvocationsOutput
// Try a few times to get the invocation data, because it takes a little bit to have any information
for i := 0; i < 3; i++ {
time.Sleep(1 * time.Second)
if out, err = client.ListCommandInvocations(&ssm.ListCommandInvocationsInput{
CommandId: commandID,
}); err != nil {
return nil, err
}
if len(out.CommandInvocations) > 0 {
break
}
}
if len(out.CommandInvocations) == 0 {
return nil, fmt.Errorf("API response contained no invocations")
}
for _, inv := range out.CommandInvocations {
targets = append(targets, inv.InstanceId)
}
return targets, nil
}
func GetResult(client ssmiface.SSMAPI, commandID *string, instanceID *string, gci chan *ssm.GetCommandInvocationOutput, ec chan error) {
status, err := client.GetCommandInvocation(&ssm.GetCommandInvocationInput{
CommandId: commandID,
InstanceId: instanceID,
})
switch {
case err != nil:
ec <- fmt.Errorf(
`Error when calling GetCommandInvocation API with args:\n
CommandId: %v\n
InstanceId: %v\n%v`,
*commandID, *instanceID, err)
case status != nil:
gci <- status
}
}