-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
exec.go
183 lines (169 loc) · 4.45 KB
/
exec.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
package ecsta
import (
"context"
"encoding/json"
"fmt"
"io"
"log/slog"
"os"
"os/exec"
"os/signal"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ecs"
"github.com/aws/aws-sdk-go-v2/service/ecs/types"
"github.com/creack/pty"
"github.com/mattn/go-isatty"
)
const SessionManagerPluginBinary = "session-manager-plugin"
type ExecOption struct {
ID string `help:"task ID"`
Command string `help:"command to execute" default:"sh"`
Container string `help:"container name"`
Family *string `help:"task definition family name"`
Service *string `help:"ECS service name"`
catchSignal bool
stdout io.Writer
stderr io.Writer
}
func (app *Ecsta) RunExec(ctx context.Context, opt *ExecOption) error {
if opt.stdout == nil {
opt.stdout = os.Stdout
}
if opt.stderr == nil {
opt.stderr = os.Stderr
}
if err := app.SetCluster(ctx); err != nil {
return err
}
task, err := app.findTask(ctx, &optionFindTask{
id: opt.ID, family: opt.Family, service: opt.Service,
selectFunc: selectFuncExcludeStopped,
})
if err != nil {
return fmt.Errorf("failed to select tasks: %w", err)
}
name, err := app.findContainerName(ctx, task, opt.Container)
if err != nil {
return fmt.Errorf("failed to select containers: %w", err)
}
opt.Container = name
out, err := app.ecs.ExecuteCommand(ctx, &ecs.ExecuteCommandInput{
Cluster: task.ClusterArn,
Interactive: true,
Task: task.TaskArn,
Command: optional(opt.Command),
Container: optional(opt.Container),
})
if err != nil {
return fmt.Errorf("failed to execute command. %w See also https://github.com/aws-containers/amazon-ecs-exec-checker", err)
}
target, err := ssmRequestTarget(task, opt.Container)
if err != nil {
return fmt.Errorf("failed to build ssm request parameters: %w", err)
}
if !opt.catchSignal {
signal.Ignore(os.Interrupt)
}
return app.runSessionManagerPlugin(ctx, task, out.Session, target, opt.stdout, opt.stderr)
}
func (app *Ecsta) runSessionManagerPlugin(ctx context.Context, task types.Task, session *types.Session, target string, stdout, stderr io.Writer) error {
endpoint, err := app.Endpoint(ctx)
if err != nil {
return fmt.Errorf("failed to get endpoint: %w", err)
}
sess, err := json.Marshal(session)
if err != nil {
return fmt.Errorf("failed to marshal session: %w", err)
}
ssmreq, err := json.Marshal(map[string]string{
"Target": target,
})
if err != nil {
return fmt.Errorf("failed to marshal ssm request parameters: %w", err)
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
cmd := exec.CommandContext(
ctx,
SessionManagerPluginBinary,
string(sess),
app.region,
"StartSession",
"",
string(ssmreq),
endpoint,
)
// send SIGINT to session manager plugin the context is canceled.
cmd.Cancel = func() error {
slog.Info(fmt.Sprintf("sending SIGINT to %s", SessionManagerPluginBinary))
return cmd.Process.Signal(os.Interrupt)
}
// send SIGKILL after 3 seconds if SIGINT is ignored.
cmd.WaitDelay = 3 * time.Second
go func() {
if err := app.watchTaskUntilStopping(ctx, *task.TaskArn); err != nil {
slog.Info(err.Error())
cancel()
}
}()
if isatty.IsTerminal(os.Stdout.Fd()) {
cmd.Stdin = os.Stdin
cmd.Stdout = stdout
cmd.Stderr = stderr
return cmd.Run()
} else {
slog.Info("running in non-interactive mode (tty is not available)")
ptmx, err := pty.Start(cmd)
if err != nil {
return fmt.Errorf("failed to start pty: %w", err)
}
defer ptmx.Close()
go func() {
io.Copy(stdout, ptmx)
}()
return cmd.Wait()
}
}
func (app *Ecsta) watchTaskUntilStopping(ctx context.Context, taskID string) error {
ticker := time.NewTicker(10 * time.Second) // TODO: configurable
defer ticker.Stop()
var lastStatus string
for {
select {
case <-ctx.Done():
return nil
case <-ticker.C:
}
tasks, err := app.describeTasks(ctx, &optionDescribeTasks{
ids: []string{taskID},
})
if err != nil {
continue
}
if len(tasks) == 0 {
return fmt.Errorf("task not found: %s", taskID)
}
status := aws.ToString(tasks[0].LastStatus)
switch status {
case "STOPPING", "DEPROVISIONING", "STOPPED", "DELETED":
return fmt.Errorf(
"%s is %s: %s (%s)",
taskID,
status,
tasks[0].StopCode,
aws.ToString(tasks[0].StoppedReason),
)
case "DEACTIVATING":
if lastStatus != status {
slog.Warn(
"the task will be stopped",
"task_id", taskID,
"status", status,
"stop_code", tasks[0].StopCode,
)
}
}
lastStatus = status
}
}