@@ -211,9 +211,10 @@ func (s *Service) Start() error {
211
211
}
212
212
213
213
// Up creates the task definition and service and starts the containers if necessary.
214
- // It does so by calling DescribeService to see if its present, else's calls Create() and Start()
215
- // If the compose file had changed, it would update the service with the new task definition
216
- // by calling UpdateService with the new task definition
214
+ // It does so by calling DescribeService to see if it exists, then calls Create() and Start().
215
+ // Otherwise, if the compose or ecs-params files have changed, it will update
216
+ // the existing service with the new task definition by calling UpdateService
217
+ // with the new task definition and service parameters.
217
218
func (s * Service ) Up () error {
218
219
// describe service to get the task definition and count running
219
220
ecsService , err := s .describeService ()
@@ -252,8 +253,8 @@ func (s *Service) Up() error {
252
253
return s .startService ()
253
254
}
254
255
255
- // Update Service
256
- if err = s .updateExistingService (ecsService , newTaskDefinition ); err != nil {
256
+ // Update Existing Service
257
+ if err = s .updateService (ecsService , newTaskDefinition ); err != nil {
257
258
return err
258
259
}
259
260
@@ -265,7 +266,39 @@ func (s *Service) Up() error {
265
266
return nil
266
267
}
267
268
268
- func (s * Service ) updateExistingService (ecsService * ecs.Service , newTaskDefinition * ecs.TaskDefinition ) error {
269
+ func (s * Service ) buildUpdateServiceInput (count int64 , serviceName , taskDefinition string ) (* ecs.UpdateServiceInput , error ) {
270
+ cluster := s .Context ().CommandConfig .Cluster
271
+ deploymentConfig := s .DeploymentConfig ()
272
+ forceDeployment := s .Context ().CLIContext .Bool (flags .ForceDeploymentFlag )
273
+ networkConfig , err := composeutils .ConvertToECSNetworkConfiguration (s .ecsContext .ECSParams )
274
+ if err != nil {
275
+ return nil , err
276
+ }
277
+
278
+ input := & ecs.UpdateServiceInput {
279
+ DesiredCount : aws .Int64 (count ),
280
+ Service : aws .String (serviceName ),
281
+ Cluster : aws .String (cluster ),
282
+ DeploymentConfiguration : deploymentConfig ,
283
+ ForceNewDeployment : & forceDeployment ,
284
+ }
285
+
286
+ if s .healthCheckGP != nil {
287
+ input .HealthCheckGracePeriodSeconds = aws .Int64 (* s .healthCheckGP )
288
+ }
289
+
290
+ if networkConfig != nil {
291
+ input .NetworkConfiguration = networkConfig
292
+ }
293
+
294
+ if taskDefinition != "" {
295
+ input .TaskDefinition = aws .String (taskDefinition )
296
+ }
297
+
298
+ return input , nil
299
+ }
300
+
301
+ func (s * Service ) updateService (ecsService * ecs.Service , newTaskDefinition * ecs.TaskDefinition ) error {
269
302
if s .Context ().CLIContext .Bool (flags .EnableServiceDiscoveryFlag ) {
270
303
return fmt .Errorf ("Service Discovery can not be enabled on an existing ECS Service" )
271
304
}
@@ -288,41 +321,26 @@ func (s *Service) updateExistingService(ecsService *ecs.Service, newTaskDefiniti
288
321
newTaskDefinitionId := entity .GetIdFromArn (newTaskDefinition .TaskDefinitionArn )
289
322
290
323
if oldTaskDefinitionId == newTaskDefinitionId {
291
- return s .updateService (newCount )
324
+ return s .updateServiceCount (newCount )
292
325
}
293
326
294
- deploymentConfig := s .DeploymentConfig ()
295
327
// if the task definitions were different, updateService with new task definition
296
328
// this creates a deployment in ECS and slowly takes down the containers with old ones and starts new ones
297
329
298
- networkConfig , err := composeutils . ConvertToECSNetworkConfiguration ( s . ecsContext . ECSParams )
330
+ updateServiceInput , err := s . buildUpdateServiceInput ( newCount , ecsServiceName , newTaskDefinitionId )
299
331
if err != nil {
300
332
return err
301
333
}
302
334
303
- forceDeployment := s .Context ().CLIContext .Bool (flags .ForceDeploymentFlag )
304
-
305
- err = s .Context ().ECSClient .UpdateService (ecsServiceName , newTaskDefinitionId , newCount , deploymentConfig , networkConfig , s .healthCheckGP , forceDeployment )
335
+ err = s .Context ().ECSClient .UpdateService (updateServiceInput )
306
336
if err != nil {
307
337
return err
308
338
}
309
- fields := log.Fields {
310
- "serviceName" : ecsServiceName ,
311
- "taskDefinition" : newTaskDefinitionId ,
312
- "desiredCount" : newCount ,
313
- }
314
- if deploymentConfig != nil && deploymentConfig .MaximumPercent != nil {
315
- fields ["deployment-max-percent" ] = aws .Int64Value (deploymentConfig .MaximumPercent )
316
- }
317
- if deploymentConfig != nil && deploymentConfig .MinimumHealthyPercent != nil {
318
- fields ["deployment-min-healthy-percent" ] = aws .Int64Value (deploymentConfig .MinimumHealthyPercent )
319
- }
320
- if s .healthCheckGP != nil {
321
- fields ["health-check-grace-period" ] = * s .healthCheckGP
322
- }
323
339
324
- log .WithFields (fields ).Info ("Updated the ECS service with a new task definition. " +
325
- "Old containers will be stopped automatically, and replaced with new ones" )
340
+ message := "Updated the ECS service with a new task definition. " +
341
+ "Old containers will be stopped automatically, and replaced with new ones"
342
+ s .logUpdateService (updateServiceInput , message )
343
+
326
344
return waitForServiceTasks (s , ecsServiceName )
327
345
}
328
346
@@ -336,13 +354,13 @@ func (s *Service) Info(filterProjectTasks bool) (project.InfoSet, error) {
336
354
337
355
// Scale the service desired count to be the specified count
338
356
func (s * Service ) Scale (count int ) error {
339
- return s .updateService (int64 (count ))
357
+ return s .updateServiceCount (int64 (count ))
340
358
}
341
359
342
360
// Stop stops all the containers in the service by calling ECS.UpdateService(count=0)
343
361
// TODO, Store the current desiredCount in a cache, so that number of tasks(group of containers) can be started again
344
362
func (s * Service ) Stop () error {
345
- return s .updateService (int64 (0 ))
363
+ return s .updateServiceCount (int64 (0 ))
346
364
}
347
365
348
366
// Down stops any running containers(tasks) by calling Stop() and deletes an active ECS Service
@@ -527,7 +545,7 @@ func (s *Service) createService() error {
527
545
defer s .logCreateService (serviceName , taskDefName )
528
546
529
547
// Call ECS Client
530
- err = s .Context ().ECSClient .CreateService (serviceName , createServiceInput )
548
+ err = s .Context ().ECSClient .CreateService (createServiceInput )
531
549
if err != nil {
532
550
return err
533
551
}
@@ -574,7 +592,7 @@ func (s *Service) startService() error {
574
592
"desiredCount" : desiredCount ,
575
593
"force-deployment" : strconv .FormatBool (forceDeployment ),
576
594
}).Info ("Forcing new deployment of running ECS Service" )
577
- return s .updateService (desiredCount )
595
+ return s .updateServiceCount (desiredCount )
578
596
}
579
597
//NoOp
580
598
log .WithFields (log.Fields {
@@ -584,43 +602,47 @@ func (s *Service) startService() error {
584
602
585
603
return waitForServiceTasks (s , serviceName )
586
604
}
587
- return s .updateService (int64 (1 ))
605
+ return s .updateServiceCount (int64 (1 ))
588
606
}
589
607
590
- // updateService calls the underlying ECS.UpdateService with the specified count
591
- func (s * Service ) updateService (count int64 ) error {
608
+ // updateServiceCount calls the underlying ECS.UpdateService with the specified count
609
+ // NOTE: If network configuration has changed in ECS Params, this will also be updated
610
+ func (s * Service ) updateServiceCount (count int64 ) error {
592
611
serviceName := entity .GetServiceName (s )
593
- deploymentConfig := s .DeploymentConfig ()
594
- networkConfig , err := composeutils .ConvertToECSNetworkConfiguration (s .ecsContext .ECSParams )
595
- forceDeployment := s .Context ().CLIContext .Bool (flags .ForceDeploymentFlag )
596
612
613
+ updateServiceInput , err := s .buildUpdateServiceInput (count , serviceName , "" )
597
614
if err != nil {
598
615
return err
599
616
}
600
617
601
- if err = s .Context ().ECSClient .UpdateService (serviceName , "" , count , deploymentConfig , networkConfig , s . healthCheckGP , forceDeployment ); err != nil {
618
+ if err = s .Context ().ECSClient .UpdateService (updateServiceInput ); err != nil {
602
619
return err
603
620
}
604
621
622
+ s .logUpdateService (updateServiceInput , "Updated ECS service successfully" )
623
+
624
+ return waitForServiceTasks (s , serviceName )
625
+ }
626
+
627
+ func (s * Service ) logUpdateService (input * ecs.UpdateServiceInput , message string ) {
605
628
fields := log.Fields {
606
- "serviceName " : serviceName ,
607
- "desiredCount" : count ,
629
+ "service " : aws . StringValue ( input . Service ) ,
630
+ "desiredCount" : aws . Int64Value ( input . DesiredCount ) ,
608
631
}
609
- if deploymentConfig != nil && deploymentConfig .MaximumPercent != nil {
610
- fields ["deployment-max-percent" ] = aws .Int64Value (deploymentConfig .MaximumPercent )
632
+ if s . deploymentConfig != nil && s . deploymentConfig .MaximumPercent != nil {
633
+ fields ["deployment-max-percent" ] = aws .Int64Value (s . deploymentConfig .MaximumPercent )
611
634
}
612
- if deploymentConfig != nil && deploymentConfig .MinimumHealthyPercent != nil {
613
- fields ["deployment-min-healthy-percent" ] = aws .Int64Value (deploymentConfig .MinimumHealthyPercent )
635
+ if s . deploymentConfig != nil && s . deploymentConfig .MinimumHealthyPercent != nil {
636
+ fields ["deployment-min-healthy-percent" ] = aws .Int64Value (s . deploymentConfig .MinimumHealthyPercent )
614
637
}
615
638
if s .healthCheckGP != nil {
616
639
fields ["health-check-grace-period" ] = * s .healthCheckGP
617
640
}
618
- if forceDeployment {
619
- fields ["force-deployment" ] = forceDeployment
641
+ if input . ForceNewDeployment != nil {
642
+ fields ["force-deployment" ] = aws . BoolValue ( input . ForceNewDeployment )
620
643
}
621
644
622
- log .WithFields (fields ).Info ("Updated ECS service successfully" )
623
- return waitForServiceTasks (s , serviceName )
645
+ log .WithFields (fields ).Info (message )
624
646
}
625
647
626
648
func getSDSIDFromArn (sdsARN string ) string {
0 commit comments