Skip to content
This repository was archived by the owner on Jan 11, 2022. It is now read-only.

Commit 0c94f9e

Browse files
authored
Merge pull request #9 from vend/multiple-services
Add support for multiple services
2 parents 4f5e64a + d305d9f commit 0c94f9e

File tree

2 files changed

+48
-37
lines changed

2 files changed

+48
-37
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Usage of ./go-ecs-deploy:
2525
-C value
2626
Slack channels to post to (can be specified multiple times)
2727
-a string
28-
Application name
28+
Application name (can be specified multiple times)
2929
-c string
3030
Cluster name to deploy to
3131
-d enable Debug output

main.go

+47-36
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@ func (flags *arrayFlag) Set(value string) error {
2626
return nil
2727
}
2828

29+
func (flags *arrayFlag) Specified() bool {
30+
return len(*flags) > 0
31+
}
32+
2933
var (
3034
clusterName = flag.String("c", "", "Cluster name to deploy to")
3135
repoName = flag.String("i", "", "Container repo to pull from e.g. quay.io/username/reponame")
32-
appName = flag.String("a", "", "Application name")
3336
environment = flag.String("e", "", "Application environment, e.g. production")
3437
sha = flag.String("s", "", "Tag, usually short git SHA to deploy")
3538
region = flag.String("r", "", "AWS region")
@@ -39,6 +42,7 @@ var (
3942
)
4043

4144
var channels arrayFlag
45+
var apps arrayFlag
4246

4347
func fail(s string) {
4448
fmt.Printf(s)
@@ -74,22 +78,25 @@ func sendWebhooks(message string) {
7478

7579
func init() {
7680
flag.Var(&channels, "C", "Slack channels to post to (can be specified multiple times)")
81+
flag.Var(&apps, "a", "Application names (can be specified multiple times)")
82+
7783
}
7884

7985
func main() {
8086
flag.Parse()
8187

82-
if *clusterName == "" || *appName == "" || *environment == "" || *region == "" {
88+
if *clusterName == "" || !apps.Specified() || *environment == "" || *region == "" {
8389
flag.Usage()
84-
fail(fmt.Sprintf("Failed deployment of app %s : missing parameters\n", *appName))
90+
fail(fmt.Sprintf("Failed deployment of apps %s : missing parameters\n", apps))
8591
}
8692

8793
if (*repoName == "" || *sha == "") && *targetImage == "" {
8894
flag.Usage()
89-
fail(fmt.Sprintf("Failed deployment %s : no repo name, sha or target image specified\n", *appName))
95+
fail(fmt.Sprintf("Failed deployment %s : no repo name, sha or target image specified\n", apps))
9096
}
9197

92-
serviceName := *appName + "-" + *environment
98+
// Take the first app specified and use it for creating the task definitions for all services.
99+
exemplarServiceName := apps[0] + "-" + *environment
93100
cfg := &aws.Config{
94101
Region: aws.String(*region),
95102
}
@@ -104,26 +111,26 @@ func main() {
104111
} else {
105112
fmt.Printf("Request to deploy target image: %s to %s at %s \n", *targetImage, *environment, *region)
106113
}
107-
fmt.Printf("Describing services for cluster %s and service %s \n", *clusterName, serviceName)
114+
fmt.Printf("Describing services for cluster %s and service %s \n", *clusterName, exemplarServiceName)
108115

109116
serviceDesc, err :=
110117
svc.DescribeServices(
111118
&ecs.DescribeServicesInput{
112119
Cluster: clusterName,
113-
Services: []*string{&serviceName},
120+
Services: []*string{&exemplarServiceName},
114121
})
115122
if err != nil {
116-
fail(fmt.Sprintf("Failed: deployment %s \n`%s`", *appName, err.Error()))
123+
fail(fmt.Sprintf("Failed to describe %s \n`%s`", exemplarServiceName, err.Error()))
117124
}
118125

119126
if len(serviceDesc.Services) < 1 {
120-
msg := fmt.Sprintf("No service %s found on cluster %s", serviceName, *clusterName)
127+
msg := fmt.Sprintf("No service %s found on cluster %s", exemplarServiceName, *clusterName)
121128
fail("Failed: " + msg)
122129
}
123130

124131
service := serviceDesc.Services[0]
125-
if serviceName != *service.ServiceName {
126-
msg := fmt.Sprintf("Found the wrong service when looking for %s found %s \n", serviceName, *service.ServiceName)
132+
if exemplarServiceName != *service.ServiceName {
133+
msg := fmt.Sprintf("Found the wrong service when looking for %s found %s \n", exemplarServiceName, *service.ServiceName)
127134
fail("Failed: " + msg)
128135
}
129136

@@ -134,7 +141,7 @@ func main() {
134141
&ecs.DescribeTaskDefinitionInput{
135142
TaskDefinition: service.TaskDefinition})
136143
if err != nil {
137-
fail(fmt.Sprintf("Failed: deployment %s \n`%s`", *appName, err.Error()))
144+
fail(fmt.Sprintf("Failed: deployment %s \n`%s`", exemplarServiceName, err.Error()))
138145
}
139146

140147
if *debug {
@@ -166,41 +173,45 @@ func main() {
166173
registerRes, err :=
167174
svc.RegisterTaskDefinition(futureDef)
168175
if err != nil {
169-
fail(fmt.Sprintf("Failed: deployment %s for %s to %s \n`%s`", *containerDef.Image, *appName, *clusterName, err.Error()))
176+
fail(fmt.Sprintf("Failed: deployment %s for %s to %s \n`%s`", *containerDef.Image, exemplarServiceName, *clusterName, err.Error()))
170177
}
171178

172179
newArn := registerRes.TaskDefinition.TaskDefinitionArn
173180

174181
fmt.Printf("Registered new task for %s:%s \n", *sha, *newArn)
175182

176-
// update service to use new definition
177-
_, err = svc.UpdateService(
178-
&ecs.UpdateServiceInput{
179-
Cluster: clusterName,
180-
Service: &serviceName,
181-
DesiredCount: service.DesiredCount,
182-
TaskDefinition: newArn,
183-
})
184-
if err != nil {
185-
fail(fmt.Sprintf("Failed: deployment %s for %s to %s as %s \n`%s`", *containerDef.Image, *appName, *clusterName, *newArn, err.Error()))
186-
}
183+
// update services to use new definition
184+
for _, appName := range apps {
185+
serviceName := appName + "-" + *environment
187186

188-
slackMsg := fmt.Sprintf("Deployed %s for *%s* to *%s* as `%s`", *containerDef.Image, *appName, *clusterName, *newArn)
187+
_, err = svc.UpdateService(
188+
&ecs.UpdateServiceInput{
189+
Cluster: clusterName,
190+
Service: &serviceName,
191+
DesiredCount: service.DesiredCount,
192+
TaskDefinition: newArn,
193+
})
194+
if err != nil {
195+
fail(fmt.Sprintf("Failed: deployment %s for %s to %s as %s \n`%s`", *containerDef.Image, appName, *clusterName, *newArn, err.Error()))
196+
}
189197

190-
// extract old image sha, and use it to generate a git compare URL
191-
if *oldImage != "" && *sha != "" {
192-
parts := strings.Split(*oldImage, ":")
193-
if len(parts) == 2 {
194-
// possibly a tagged image "def15c31-php5.5"
195-
parts = strings.Split(parts[1], "-")
196-
if gitURL, err := gitURL(parts[0], *sha); err == nil {
197-
slackMsg += " (<" + gitURL + "|diff>)"
198+
slackMsg := fmt.Sprintf("Deployed %s for *%s* to *%s* as `%s`", *containerDef.Image, appName, *clusterName, *newArn)
199+
200+
// extract old image sha, and use it to generate a git compare URL
201+
if *oldImage != "" && *sha != "" {
202+
parts := strings.Split(*oldImage, ":")
203+
if len(parts) == 2 {
204+
// possibly a tagged image "def15c31-php5.5"
205+
parts = strings.Split(parts[1], "-")
206+
if gitURL, err := gitURL(parts[0], *sha); err == nil {
207+
slackMsg += " (<" + gitURL + "|diff>)"
208+
}
198209
}
199210
}
200-
}
201-
sendWebhooks(slackMsg)
211+
sendWebhooks(slackMsg)
202212

203-
fmt.Printf("Updated %s service to use new ARN: %s \n", serviceName, *newArn)
213+
fmt.Printf("Updated %s service to use new ARN: %s \n", serviceName, *newArn)
214+
}
204215

205216
}
206217

0 commit comments

Comments
 (0)