-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
223 lines (188 loc) · 5.36 KB
/
main.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package main
import (
"context"
"fmt"
"log"
"os"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ecs"
"github.com/aws/aws-sdk-go-v2/service/ecs/types"
"github.com/hashicorp/go-plugin"
pb "github.com/whywaita/myshoes/api/proto.go"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type AppConfig struct {
Cluster string
TaskDefinition string
SubnetID string
Region string
NoWait bool
}
func main() {
if err := run(); err != nil {
log.Fatal(err)
}
}
func run() error {
handshake := plugin.HandshakeConfig{
ProtocolVersion: 1,
MagicCookieKey: "SHOES_PLUGIN_MAGIC_COOKIE",
MagicCookieValue: "are_you_a_shoes?",
}
pluginMap := map[string]plugin.Plugin{
"shoes_grpc": &ECSTaskPlugin{},
}
plugin.Serve(&plugin.ServeConfig{
HandshakeConfig: handshake,
Plugins: pluginMap,
GRPCServer: plugin.DefaultGRPCServer,
})
return nil
}
// ECSTaskPlugin is plugin for lxd multi node
type ECSTaskPlugin struct {
plugin.Plugin
}
func loadAppConfig() (*AppConfig, error) {
const (
EnvCluster = "ECS_TASK_CLUSTER"
EnvTaskDefinition = "ECS_TASK_DEFINITION_ARN"
EnvSubnetID = "ECS_TASK_SUBNET_ID"
EnvRegion = "ECS_TASK_REGION"
EnvNoWait = "ECS_TASK_NO_WAIT"
)
if strings.EqualFold(os.Getenv(EnvCluster), "") {
return nil, fmt.Errorf("must set %s", EnvCluster)
}
if strings.EqualFold(os.Getenv(EnvTaskDefinition), "") {
return nil, fmt.Errorf("must set %s", EnvTaskDefinition)
}
if strings.EqualFold(os.Getenv(EnvSubnetID), "") {
return nil, fmt.Errorf("must set %s", EnvSubnetID)
}
if strings.EqualFold(os.Getenv(EnvRegion), "") {
return nil, fmt.Errorf("must set %s", EnvRegion)
}
// Optional
noWait := false
if strings.EqualFold(os.Getenv(EnvNoWait), "true") {
noWait = true
}
return &AppConfig{
Cluster: os.Getenv(EnvCluster),
TaskDefinition: os.Getenv(EnvTaskDefinition),
SubnetID: os.Getenv(EnvSubnetID),
Region: os.Getenv(EnvRegion),
NoWait: noWait,
}, nil
}
// GRPCServer is implement gRPC Server.
func (p *ECSTaskPlugin) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error {
c, err := loadAppConfig()
if err != nil {
return fmt.Errorf("loadAppConfig(): %w", err)
}
client := Client{appConfig: *c}
pb.RegisterShoesServer(s, client)
return nil
}
// GRPCClient is implement gRPC client.
// This function is not have client, so return nil
func (p *ECSTaskPlugin) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c *grpc.ClientConn) (interface{}, error) {
return nil, nil
}
// Client is client of ECS Task
type Client struct {
pb.UnimplementedShoesServer
appConfig AppConfig
}
// AddInstance create an ECS Task.
func (c Client) AddInstance(ctx context.Context, req *pb.AddInstanceRequest) (*pb.AddInstanceResponse, error) {
oneLine := ToOneLine(req.SetupScript)
taskArn, err := runTask(ctx, c.appConfig, oneLine)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to runTask(): %+v", err)
}
return &pb.AddInstanceResponse{
CloudId: taskArn,
ShoesType: "ecs-task-fargate",
IpAddress: "",
}, nil
}
// DeleteInstance delete an ECS Task.
func (c Client) DeleteInstance(ctx context.Context, req *pb.DeleteInstanceRequest) (*pb.DeleteInstanceResponse, error) {
// Deleting ECS task is automatically.
return &pb.DeleteInstanceResponse{}, nil
}
func runTask(ctx context.Context, appConfig AppConfig, oneLineSetupScript string) (string, error) {
cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(appConfig.Region))
if err != nil {
return "", fmt.Errorf("config.LoadDefaultConfig(ctx, config.WithRegion(%s): %w", appConfig.Region, err)
}
runTaskIn := &ecs.RunTaskInput{
TaskDefinition: aws.String(appConfig.TaskDefinition),
Cluster: aws.String(appConfig.Cluster),
LaunchType: types.LaunchTypeFargate,
NetworkConfiguration: &types.NetworkConfiguration{
AwsvpcConfiguration: &types.AwsVpcConfiguration{
AssignPublicIp: types.AssignPublicIpEnabled,
Subnets: []string{
appConfig.SubnetID,
},
},
},
Overrides: &types.TaskOverride{
ContainerOverrides: []types.ContainerOverride{
{
Command: []string{
"bash",
"-c",
oneLineSetupScript,
},
Name: aws.String("runner"),
},
},
},
}
client := ecs.NewFromConfig(cfg)
runOut, err := client.RunTask(ctx, runTaskIn)
if err != nil {
return "", fmt.Errorf("client.RunTask(ctx, runTaskIn): %w", err)
}
taskArn := aws.ToString(runOut.Tasks[0].TaskArn)
if appConfig.NoWait {
return taskArn, nil
}
waiter := ecs.NewTasksRunningWaiter(client)
descTaskIn := &ecs.DescribeTasksInput{
Tasks: []string{taskArn},
Cluster: aws.String(appConfig.Cluster),
}
maxWaitDur := 5 * time.Minute
if err := waiter.Wait(ctx, descTaskIn, maxWaitDur); err != nil {
return "", fmt.Errorf("waiter.Wait(ctx, %+v, %s): %w", descTaskIn, maxWaitDur, err)
}
return taskArn, nil
}
// ToOneLine convert bash script to one line.
func ToOneLine(in string) string {
ll := strings.Split(in, "\n")
var commands []string
for _, line := range ll {
if strings.HasPrefix(line, "#") {
// Remove shebang and commend
continue
}
if strings.EqualFold(line, "") {
// Remove blank comment
continue
}
commands = append(commands, line)
}
return strings.Join(commands, ";")
}