Skip to content

Commit

Permalink
allow services deploy to deploy multiple services
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafael Gumieri committed Oct 29, 2019
1 parent 6659244 commit 593f4d3
Showing 1 changed file with 72 additions and 68 deletions.
140 changes: 72 additions & 68 deletions cmd/services_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import (
)

func servicesDeployRun(cmd *cobra.Command, args []string) {
service := args[0]
var services []*string
for _, s := range args {
services = append(services, aws.String(s))
}

cluster := viper.GetString("cluster")

clustersDescription, err := ecsI.DescribeClusters(&ecs.DescribeClustersInput{
Expand All @@ -27,100 +31,100 @@ func servicesDeployRun(cmd *cobra.Command, args []string) {
c := clustersDescription.Clusters[0]

servicesDescription, err := ecsI.DescribeServices(&ecs.DescribeServicesInput{
Cluster: c.ClusterName,
Services: []*string{
aws.String(service),
},
Cluster: c.ClusterName,
Services: services,
})

t.Must(err)

if len(servicesDescription.Services) == 0 {
t.Exitf("Service informed not found")
if len(servicesDescription.Failures) != 0 {
t.Exitf("A service informed aws not found") // TODO: handle the `Arn` and `Reason` to a more accurate error message
}

if tag == "" && image == "" {
t.Must(ecsI.UpdateService(&ecs.UpdateServiceInput{
Cluster: c.ClusterName,
Service: aws.String(service),
ForceNewDeployment: aws.Bool(true),
}))
for _, service := range services {
t.Must(ecsI.UpdateService(&ecs.UpdateServiceInput{
Cluster: c.ClusterName,
Service: service,
ForceNewDeployment: aws.Bool(true),
}))
}

t.Exit(nil)
}

s := servicesDescription.Services[0]
for _, s := range servicesDescription.Services {
tdDescription, err := ecsI.DescribeTaskDefinition(&ecs.DescribeTaskDefinitionInput{
TaskDefinition: s.TaskDefinition,
})

tdDescription, err := ecsI.DescribeTaskDefinition(&ecs.DescribeTaskDefinitionInput{
TaskDefinition: s.TaskDefinition,
})
t.Must(err)

t.Must(err)

td := tdDescription.TaskDefinition
td := tdDescription.TaskDefinition

var cdToUpdate *ecs.ContainerDefinition
var cdToUpdate *ecs.ContainerDefinition

// If no Container Definition's name is informed, it get the first one
if containerName == "" {
cdToUpdate = td.ContainerDefinitions[0]
} else {
for _, cd := range td.ContainerDefinitions {
if aws.StringValue(cd.Name) == containerName {
cdToUpdate = cd
break
// If no Container Definition's name is informed, it get the first one
if containerName == "" {
cdToUpdate = td.ContainerDefinitions[0]
} else {
for _, cd := range td.ContainerDefinitions {
if aws.StringValue(cd.Name) == containerName {
cdToUpdate = cd
break
}
}
}
}

if cdToUpdate == nil {
t.Exitf("No container on the Task Family %s", aws.StringValue(td.Family))
}

if tag != "" {
image = strings.Split(aws.StringValue(cdToUpdate.Image), ":")[0] + ":" + tag
}

cdToUpdate.Image = aws.String(image)

newTDDescription, err := ecsI.RegisterTaskDefinition(&ecs.RegisterTaskDefinitionInput{
ContainerDefinitions: td.ContainerDefinitions,
Cpu: td.Cpu,
ExecutionRoleArn: td.ExecutionRoleArn,
Family: td.Family,
IpcMode: td.IpcMode,
Memory: td.Memory,
NetworkMode: td.NetworkMode,
PidMode: td.PidMode,
PlacementConstraints: td.PlacementConstraints,
ProxyConfiguration: td.ProxyConfiguration,
RequiresCompatibilities: td.RequiresCompatibilities,
TaskRoleArn: td.TaskRoleArn,
Volumes: td.Volumes,
})

t.Must(err)
if cdToUpdate == nil {
t.Exitf("No container on the Task Family %s", aws.StringValue(td.Family))
}

newTD := newTDDescription.TaskDefinition
oldFamilyRevision := aws.StringValue(td.Family) + ":" + strconv.FormatInt(aws.Int64Value(td.Revision), 10)
if tag != "" {
image = strings.Split(aws.StringValue(cdToUpdate.Image), ":")[0] + ":" + tag
}

t.Must(ecsI.DeregisterTaskDefinition(&ecs.DeregisterTaskDefinitionInput{
TaskDefinition: aws.String(oldFamilyRevision),
}))
cdToUpdate.Image = aws.String(image)

newTDDescription, err := ecsI.RegisterTaskDefinition(&ecs.RegisterTaskDefinitionInput{
ContainerDefinitions: td.ContainerDefinitions,
Cpu: td.Cpu,
ExecutionRoleArn: td.ExecutionRoleArn,
Family: td.Family,
IpcMode: td.IpcMode,
Memory: td.Memory,
NetworkMode: td.NetworkMode,
PidMode: td.PidMode,
PlacementConstraints: td.PlacementConstraints,
ProxyConfiguration: td.ProxyConfiguration,
RequiresCompatibilities: td.RequiresCompatibilities,
TaskRoleArn: td.TaskRoleArn,
Volumes: td.Volumes,
})

t.Must(err)

newTD := newTDDescription.TaskDefinition
oldFamilyRevision := aws.StringValue(td.Family) + ":" + strconv.FormatInt(aws.Int64Value(td.Revision), 10)

t.Must(ecsI.DeregisterTaskDefinition(&ecs.DeregisterTaskDefinitionInput{
TaskDefinition: aws.String(oldFamilyRevision),
}))

newFamilyRevision := aws.StringValue(newTD.Family) + ":" + strconv.FormatInt(aws.Int64Value(newTD.Revision), 10)
newFamilyRevision := aws.StringValue(newTD.Family) + ":" + strconv.FormatInt(aws.Int64Value(newTD.Revision), 10)

t.Must(ecsI.UpdateService(&ecs.UpdateServiceInput{
Cluster: c.ClusterName,
Service: aws.String(service),
TaskDefinition: aws.String(newFamilyRevision),
}))
t.Must(ecsI.UpdateService(&ecs.UpdateServiceInput{
Cluster: c.ClusterName,
Service: s.ServiceName,
TaskDefinition: aws.String(newFamilyRevision),
}))
}
}

var servicesDeployCmd = &cobra.Command{
Use: "deploy [service]",
Short: "Deploy a service",
Args: cobra.ExactArgs(1),
Args: cobra.MinimumNArgs(1),
Run: servicesDeployRun,
}

Expand Down

0 comments on commit 593f4d3

Please sign in to comment.