-
Notifications
You must be signed in to change notification settings - Fork 4
/
ecs-alter-service.go
58 lines (54 loc) · 2.33 KB
/
ecs-alter-service.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
package awsecs
import (
"errors"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/aws/aws-sdk-go/service/ecs/ecsiface"
"github.com/aws/aws-sdk-go/service/elbv2/elbv2iface"
"github.com/cenkalti/backoff"
"log"
)
var (
// ErrNothingToRollback nothing to rollback
ErrNothingToRollback = errors.New("nothing to rollback")
// ErrPermanentNothingToRollback permanent nothing to rollback
ErrPermanentNothingToRollback = backoff.Permanent(ErrNothingToRollback)
// ErrSuccessfulRollback successful rollback
ErrSuccessfulRollback = errors.New("successful rollback")
// ErrFailedRollback failed rollback
ErrFailedRollback = errors.New("failed rollback")
)
type validateDeploymentFunc func(ecsiface.ECSAPI, elbv2iface.ELBV2API, ecs.Service, backoff.BackOff) error
func alterServiceOrValidatedRollBack(ecsapi ecsiface.ECSAPI, elbv2api elbv2iface.ELBV2API, cluster, service string, imageMap map[string]string, envMaps map[string]map[string]string, secretMaps map[string]map[string]string, logopts map[string]map[string]map[string]string, logsecrets map[string]map[string]map[string]string, taskRole string, desiredCount *int64, taskdef string, bo backoff.BackOff, validateDeployment validateDeploymentFunc) error {
oldsvc, alterSvcErr := alterServiceValidateDeployment(ecsapi, elbv2api, cluster, service, imageMap, envMaps, secretMaps, logopts, logsecrets, taskRole, desiredCount, taskdef, bo, validateDeployment)
if alterSvcErr != nil {
operation := func() error {
if oldsvc.ServiceName == nil {
return ErrPermanentNothingToRollback
}
log.Printf("attempt rollback %v", alterSvcErr)
rollback, err := ecsapi.UpdateService(&ecs.UpdateServiceInput{Cluster: oldsvc.ClusterArn, Service: oldsvc.ServiceName, TaskDefinition: oldsvc.TaskDefinition, DesiredCount: oldsvc.DesiredCount, ForceNewDeployment: aws.Bool(true)})
if err != nil {
return err
}
var prevErr error
operation := func() error {
err := validateDeployment(ecsapi, elbv2api, *rollback.Service, bo)
if err != prevErr && err != nil {
prevErr = err
log.Print(err)
}
return err
}
return backoff.Retry(operation, bo)
}
if err := backoff.Retry(operation, bo); err != nil {
if err == ErrNothingToRollback {
return alterSvcErr
}
return ErrFailedRollback
}
return ErrSuccessfulRollback
}
return alterSvcErr
}