diff --git a/README.md b/README.md index bfd0564..6abfc5a 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,21 @@ test-service: container_port: 80 ``` +#### Keep desired_count at updating service + +If you modify value of `desired_count` by AWS Management Console or aws-cli, you'll fear override value of `desired_count` by ecs-formation. This value should be flexibly changed in the operation. + +If `keep_desired_count` is `true`, keep current `desired_count` at updating service. + +```bash +(path-to-path/test-ecs-formation/service) $ vim test-cluster.yml +test-service: + task_definition: test-definition + desired_count: 1 + keep_desired_count: true +``` + + #### Manage Task Definitions Show update plan. diff --git a/operation/service.go b/operation/service.go index 7060859..8635124 100644 --- a/operation/service.go +++ b/operation/service.go @@ -120,6 +120,7 @@ func createClusterPlans(controller *service.ServiceController, projectDir string util.PrintlnYellow(fmt.Sprintf(" ----------[%s]----------", add.Name)) util.PrintlnYellow(fmt.Sprintf(" TaskDefinition = %s", add.TaskDefinition)) util.PrintlnYellow(fmt.Sprintf(" DesiredCount = %d", add.DesiredCount)) + util.PrintlnYellow(fmt.Sprintf(" KeepDesiredCount = %t", add.KeepDesiredCount)) for _, lb := range add.LoadBalancers { util.PrintlnYellow(fmt.Sprintf(" ELB:%s", lb.Name)) } diff --git a/service/schema.go b/service/schema.go index b52e099..3ff4f17 100644 --- a/service/schema.go +++ b/service/schema.go @@ -5,25 +5,24 @@ import ( ) type Cluster struct { - - Name string - Services map[string]Service + Name string + Services map[string]Service } type Service struct { - - Name string - TaskDefinition string `yaml:"task_definition"` - DesiredCount int64 `yaml:"desired_count"` - LoadBalancers []LoadBalancer `yaml:"load_balancers"` - Role string `yaml:"role"` + Name string + TaskDefinition string `yaml:"task_definition"` + DesiredCount int64 `yaml:"desired_count"` + KeepDesiredCount bool `yaml:"keep_desired_count"` + LoadBalancers []LoadBalancer `yaml:"load_balancers"` + Role string `yaml:"role"` } func (self *Service) FindLoadBalancerByContainer(conname string, port int64) *LoadBalancer { for _, lb := range self.LoadBalancers { if lb.ContainerName == conname && - lb.ContainerPort == port { + lb.ContainerPort == port { return &lb } } @@ -42,10 +41,9 @@ func (self *Service) FindLoadBalancerByName(name string) *LoadBalancer { } type LoadBalancer struct { - - Name string `yaml:"name"` - ContainerName string `yaml:"container_name"` - ContainerPort int64 `yaml:"container_port"` + Name string `yaml:"name"` + ContainerName string `yaml:"container_name"` + ContainerPort int64 `yaml:"container_port"` } func CreateServiceMap(data []byte) (map[string]Service, error) { @@ -59,4 +57,4 @@ func CreateServiceMap(data []byte) (map[string]Service, error) { } return servicesMap, err -} \ No newline at end of file +} diff --git a/service/service.go b/service/service.go index dc0f69c..a5db770 100644 --- a/service/service.go +++ b/service/service.go @@ -181,7 +181,10 @@ func (self *ServiceController) ApplyServicePlan(plan *ServiceUpdatePlan) error { api := self.manager.EcsApi() + currentSizeMap := make(map[string]int64, 0) + for _, current := range plan.CurrentServices { + currentSizeMap[*current.ServiceName] = *current.DesiredCount // set desired_count = 0 if _, err := api.UpdateService(plan.Name, *current.ServiceName, 0, *current.TaskDefinition); err != nil { @@ -210,7 +213,19 @@ func (self *ServiceController) ApplyServicePlan(plan *ServiceUpdatePlan) error { for _, add := range plan.NewServices { - result, err := api.CreateService(plan.Name, add.Name, add.DesiredCount, toLoadBalancers(&add.LoadBalancers), add.TaskDefinition, add.Role) + var nextDesiredCount int64 + if add.KeepDesiredCount { + if dc, ok := currentSizeMap[add.Name]; ok { + nextDesiredCount = dc + logger.Main.Infof("Keep DesiredCount %d at '%s'", add.Name, nextDesiredCount) + } else { + nextDesiredCount = add.DesiredCount + } + } else { + nextDesiredCount = add.DesiredCount + } + + result, err := api.CreateService(plan.Name, add.Name, nextDesiredCount, toLoadBalancers(&add.LoadBalancers), add.TaskDefinition, add.Role) if err != nil { return err }