-
Notifications
You must be signed in to change notification settings - Fork 42
/
ecs.go
168 lines (143 loc) · 4.02 KB
/
ecs.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
package aws
import (
"fmt"
"log"
"os"
"path"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/innogames/slack-bot/v2/bot"
"github.com/innogames/slack-bot/v2/bot/matcher"
"github.com/innogames/slack-bot/v2/bot/msg"
)
// command to trigger/start jenkins jobs
type ecsCommand struct {
awsCommand
}
var _ecs *ecs.ECS
// NewAwsCommand is a command to interact with aws resources
func newEcsCommands(base awsCommand) bot.Command {
return &ecsCommand{base}
}
func (c *ecsCommand) GetMatcher() matcher.Matcher {
return matcher.NewGroupMatcher(
matcher.NewRegexpMatcher(`ecs ls (?P<CLUSTER>.*)`, c.list),
matcher.NewRegexpMatcher(`ecs restart (?P<CLUSTER>.*) (?P<SERVICE>.*)`, c.restart),
)
}
// We want to retrieve all services from an ecs cluster and throw them as a slack block
func (c *ecsCommand) list(match matcher.Result, message msg.Message) {
cluster := match.GetString("CLUSTER")
svc, err := ListServices(cluster)
if err != nil {
log.Println(err.Error())
c.SendEphemeralMessage(message, err.Error())
return
}
var text string
text += "Hello <@" + message.User + ">, current services:. \n"
for _, v := range svc {
text += fmt.Sprintf("• %s", v)
text += "\n"
}
c.SendEphemeralMessage(message, text)
}
// restart service
func (c *ecsCommand) restart(match matcher.Result, message msg.Message) {
cluster := match.GetString("CLUSTER")
svc := match.GetString("SERVICE")
log.Println("restart requested: ", svc)
err := ForceNewDeployment(cluster, svc)
if err != nil {
log.Println(err.Error())
c.SendEphemeralMessage(message, err.Error())
return
}
// TODO let user know when restart is completed
// message := fmt.Sprintf("restart completed")
c.SendMessage(message, svc)
}
func (c *ecsCommand) GetHelp() []bot.Help {
examples := []string{
"ecs ls my-cluster-name // to list services in a cluster",
"ecs restart my-cluster-name service // to restart a service in a cluster",
}
help := make([]bot.Help, 0)
help = append(help, bot.Help{
Command: "ecs <sub command>",
Description: "interact with aws ecs resources",
Examples: examples,
Category: category,
})
return help
}
func ListServices(cluster string) ([]string, error) {
svc := assertECS()
params := &ecs.ListServicesInput{Cluster: aws.String(cluster)}
result := make([]*string, 0)
err := svc.ListServicesPages(params, func(services *ecs.ListServicesOutput, lastPage bool) bool {
result = append(result, services.ServiceArns...)
return !lastPage
})
if err != nil {
return nil, err
}
out := make([]string, len(result))
for i, s := range result {
out[i] = path.Base(*s)
}
log.Println(out)
return out, nil
}
func assertECS() *ecs.ECS {
if _ecs == nil {
sess, _ := session.NewSession(getServiceConfiguration())
_ecs = ecs.New(sess)
}
return _ecs
}
func getServiceConfiguration() *aws.Config {
region := os.Getenv("AWS_REGION")
if region == "" {
region = "us-east-1"
}
log.Println("Using AWS region ", region)
return &aws.Config{Region: aws.String(region)}
}
func ForceNewDeployment(clusterName string, serviceName string) error {
sess, err := session.NewSession()
if err != nil {
return err
}
ecsSvc := ecs.New(sess, &aws.Config{Region: aws.String(os.Getenv("AWS_REGION"))})
serviceParams := &ecs.DescribeServicesInput{
Services: []*string{
aws.String(serviceName),
},
Cluster: aws.String(clusterName),
}
result, err := ecsSvc.DescribeServices(serviceParams)
if err != nil {
return err
}
if len(result.Services) == 0 {
return fmt.Errorf("could not find service %s in cluster %s", serviceName, clusterName)
}
// Update Service
for i := range result.Services {
service := result.Services[i]
if *service.ServiceName == serviceName {
newServiceParams := &ecs.UpdateServiceInput{
Service: service.ServiceName,
Cluster: aws.String(clusterName),
ForceNewDeployment: aws.Bool(true),
}
_, err := ecsSvc.UpdateService(newServiceParams)
if err != nil {
return err
}
}
}
return nil
}