diff --git a/go.mod b/go.mod index 8d376de..52977a5 100644 --- a/go.mod +++ b/go.mod @@ -1,13 +1,41 @@ module github.com/eraclitux/ecsundo +go 1.21 + require ( - github.com/BurntSushi/toml v0.3.1 // indirect - github.com/aws/aws-sdk-go v1.15.88 - github.com/inconshreveable/mousetrap v1.0.0 // indirect + github.com/aws/aws-sdk-go-v2 v1.21.0 + github.com/aws/aws-sdk-go-v2/config v1.18.38 + github.com/aws/aws-sdk-go-v2/service/ecs v1.30.0 github.com/mitchellh/go-homedir v1.0.0 - github.com/pmezard/go-difflib v1.0.0 // indirect github.com/spf13/cobra v0.0.3 github.com/spf13/viper v1.2.1 +) + +require ( + github.com/BurntSushi/toml v0.3.1 // indirect + github.com/aws/aws-sdk-go-v2/credentials v1.13.36 // indirect + github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.13.11 // indirect + github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.41 // indirect + github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.35 // indirect + github.com/aws/aws-sdk-go-v2/internal/ini v1.3.42 // indirect + github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.35 // indirect + github.com/aws/aws-sdk-go-v2/service/sso v1.13.6 // indirect + github.com/aws/aws-sdk-go-v2/service/ssooidc v1.15.5 // indirect + github.com/aws/aws-sdk-go-v2/service/sts v1.21.5 // indirect + github.com/aws/smithy-go v1.14.2 // indirect + github.com/fsnotify/fsnotify v1.4.7 // indirect + github.com/hashicorp/hcl v1.0.0 // indirect + github.com/inconshreveable/mousetrap v1.0.0 // indirect + github.com/jmespath/go-jmespath v0.4.0 // indirect + github.com/magiconair/properties v1.8.0 // indirect + github.com/mitchellh/mapstructure v1.0.0 // indirect + github.com/pelletier/go-toml v1.2.0 // indirect + github.com/spf13/afero v1.1.2 // indirect + github.com/spf13/cast v1.2.0 // indirect + github.com/spf13/jwalterweatherman v1.0.0 // indirect + github.com/spf13/pflag v1.0.2 // indirect github.com/stretchr/testify v1.2.2 // indirect - golang.org/x/net v0.0.0-20181201002055-351d144fa1fc // indirect + golang.org/x/sys v0.0.0-20180906133057-8cf3aee42992 // indirect + golang.org/x/text v0.3.0 // indirect + gopkg.in/yaml.v2 v2.2.8 // indirect ) diff --git a/go.sum b/go.sum index 02f2e94..ebab13f 100644 --- a/go.sum +++ b/go.sum @@ -1,17 +1,46 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= -github.com/aws/aws-sdk-go v1.15.88 h1:Om0MayFrixOds/PrbBey2Cg/lkNEIyOrAF2RFXLwmnE= -github.com/aws/aws-sdk-go v1.15.88/go.mod h1:es1KtYUFs7le0xQ3rOihkuoVD90z7D0fR2Qm4S00/gU= +github.com/aws/aws-sdk-go-v2 v1.21.0 h1:gMT0IW+03wtYJhRqTVYn0wLzwdnK9sRMcxmtfGzRdJc= +github.com/aws/aws-sdk-go-v2 v1.21.0/go.mod h1:/RfNgGmRxI+iFOB1OeJUyxiU+9s88k3pfHvDagGEp0M= +github.com/aws/aws-sdk-go-v2/config v1.18.38 h1:CByQCELMgm2tM1lAehx3XNg0R/pfeXsYzqn0Aq2chJQ= +github.com/aws/aws-sdk-go-v2/config v1.18.38/go.mod h1:vNm9Hf5VgG2fSUWhT3zFrqN/RosGcabFMYgiSoxKFU8= +github.com/aws/aws-sdk-go-v2/credentials v1.13.36 h1:ps0cPswZjpsOk6sLwG6fdXTzrYjCplgPEyG3OUbbdqE= +github.com/aws/aws-sdk-go-v2/credentials v1.13.36/go.mod h1:sY2phUzxbygoyDtTXhqi7GjGjCQ1S5a5Rj8u3ksBxCg= +github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.13.11 h1:uDZJF1hu0EVT/4bogChk8DyjSF6fof6uL/0Y26Ma7Fg= +github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.13.11/go.mod h1:TEPP4tENqBGO99KwVpV9MlOX4NSrSLP8u3KRy2CDwA8= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.41 h1:22dGT7PneFMx4+b3pz7lMTRyN8ZKH7M2cW4GP9yUS2g= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.41/go.mod h1:CrObHAuPneJBlfEJ5T3szXOUkLEThaGfvnhTf33buas= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.35 h1:SijA0mgjV8E+8G45ltVHs0fvKpTj8xmZJ3VwhGKtUSI= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.35/go.mod h1:SJC1nEVVva1g3pHAIdCp7QsRIkMmLAgoDquQ9Rr8kYw= +github.com/aws/aws-sdk-go-v2/internal/ini v1.3.42 h1:GPUcE/Yq7Ur8YSUk6lVkoIMWnJNO0HT18GUzCWCgCI0= +github.com/aws/aws-sdk-go-v2/internal/ini v1.3.42/go.mod h1:rzfdUlfA+jdgLDmPKjd3Chq9V7LVLYo1Nz++Wb91aRo= +github.com/aws/aws-sdk-go-v2/service/ecs v1.30.0 h1:HCjnFv5l8kMrqpUmJQ33f6krEUwcNfFgfTPq7gwvSmM= +github.com/aws/aws-sdk-go-v2/service/ecs v1.30.0/go.mod h1:cxbA26Kf4UlTb40f5FON22ZPNMyEVmMS82KUJZC1E1w= +github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.35 h1:CdzPW9kKitgIiLV1+MHobfR5Xg25iYnyzWZhyQuSlDI= +github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.35/go.mod h1:QGF2Rs33W5MaN9gYdEQOBBFPLwTZkEhRwI33f7KIG0o= +github.com/aws/aws-sdk-go-v2/service/sso v1.13.6 h1:2PylFCfKCEDv6PeSN09pC/VUiRd10wi1VfHG5FrW0/g= +github.com/aws/aws-sdk-go-v2/service/sso v1.13.6/go.mod h1:fIAwKQKBFu90pBxx07BFOMJLpRUGu8VOzLJakeY+0K4= +github.com/aws/aws-sdk-go-v2/service/ssooidc v1.15.5 h1:dnInJb4S0oy8aQuri1mV6ipLlnZPfnsDNB9BGO9PDNY= +github.com/aws/aws-sdk-go-v2/service/ssooidc v1.15.5/go.mod h1:yygr8ACQRY2PrEcy3xsUI357stq2AxnFM6DIsR9lij4= +github.com/aws/aws-sdk-go-v2/service/sts v1.21.5 h1:CQBFElb0LS8RojMJlxRSo/HXipvTZW2S44Lt9Mk2aYQ= +github.com/aws/aws-sdk-go-v2/service/sts v1.21.5/go.mod h1:VC7JDqsqiwXukYEDjoHh9U0fOJtNWh04FPQz4ct4GGU= +github.com/aws/smithy-go v1.14.2 h1:MJU9hqBGbvWZdApzpvoF2WAIJDbtjK2NDJSiJP7HblQ= +github.com/aws/smithy-go v1.14.2/go.mod h1:Tg+OJXh4MB2R/uN61Ko2f6hTZwB/ZYGOtib8J3gBHzA= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= +github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= -github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8 h1:12VvqtR6Aowv3l/EQUlocDHW2Cp4G9WJVH7uyH8QFJE= -github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= +github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= +github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= +github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= +github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/mitchellh/go-homedir v1.0.0 h1:vKb8ShqSby24Yrqr/yDYkuFz8d0WUjys40rvnGC8aR0= @@ -34,15 +63,15 @@ github.com/spf13/pflag v1.0.2 h1:Fy0orTDgHdbnzHcsOgfCN4LtHf0ec3wwtiwJqwvf3Gc= github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/spf13/viper v1.2.1 h1:bIcUwXqLseLF3BDAZduuNfekWG87ibtFxi59Bq+oI9M= github.com/spf13/viper v1.2.1/go.mod h1:P4AexN0a+C9tGAnUFNwDMYYZv3pjFuvmeiMyKRaNVlI= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= -golang.org/x/net v0.0.0-20181201002055-351d144fa1fc h1:a3CU5tJYVj92DY2LaA1kUkrsqD5/3mLDhx2NcNqyW+0= -golang.org/x/net v0.0.0-20181201002055-351d144fa1fc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/sys v0.0.0-20180906133057-8cf3aee42992 h1:BH3eQWeGbwRU2+wxxuuPOdFBmaiBH81O8BugSjHeTFg= golang.org/x/sys v0.0.0-20180906133057-8cf3aee42992/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/internal/platform/aws/ecs.go b/internal/platform/aws/ecs.go index 25b224e..6a27092 100644 --- a/internal/platform/aws/ecs.go +++ b/internal/platform/aws/ecs.go @@ -21,17 +21,17 @@ package aws import ( + "context" "fmt" - "net/http" + "log" "os" "strconv" "strings" "time" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/aws/awserr" - "github.com/aws/aws-sdk-go/aws/session" - "github.com/aws/aws-sdk-go/service/ecs" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/config" + "github.com/aws/aws-sdk-go-v2/service/ecs" ) const awsApisErrorFmt = "error on AWS request: %s" @@ -45,7 +45,7 @@ type ServiceInfo struct { // ECSService implements cli.ecsProvider. type ECSService struct { verbose bool - client *ecs.ECS + client *ecs.Client } // ServicePreviousVersion returns previous task version as ARN string. @@ -76,23 +76,21 @@ func (es *ECSService) ServiceRollback(serviceName, clusterName, taskARN string) Service: aws.String(serviceName), TaskDefinition: aws.String(taskARN), } - _, err := es.client.UpdateService(updateInput) - switch e := err.(type) { - case nil: - return nil - case awserr.Error: - if e.Message() != "TaskDefinition is inactive" { - return e + _, err := es.client.UpdateService(context.TODO(), updateInput) + if err != nil { + if err.Error() != "TaskDefinition is inactive" { + // return e } - default: - return fmt.Errorf(awsApisErrorFmt, err) } + // default: + // return fmt.Errorf(awsApisErrorFmt, err) + // } // At this point task is INACTIVE, register a new one with the same // configuration and update service with this. describeInput := &ecs.DescribeTaskDefinitionInput{ TaskDefinition: aws.String(taskARN), } - out, err := es.client.DescribeTaskDefinition(describeInput) + out, err := es.client.DescribeTaskDefinition(context.TODO(), describeInput) if err != nil { return fmt.Errorf(awsApisErrorFmt, err) } @@ -109,7 +107,7 @@ func (es *ECSService) ServiceRollback(serviceName, clusterName, taskARN string) TaskRoleArn: taskDef.TaskRoleArn, Volumes: taskDef.Volumes, } - registerOut, err := es.client.RegisterTaskDefinition(registerInput) + registerOut, err := es.client.RegisterTaskDefinition(context.TODO(), registerInput) if err != nil { return fmt.Errorf(awsApisErrorFmt, err) } @@ -117,7 +115,7 @@ func (es *ECSService) ServiceRollback(serviceName, clusterName, taskARN string) fmt.Printf("%q new task definition registered with configuration from %q\n", *registerOut.TaskDefinition.TaskDefinitionArn, taskARN) } updateInput.TaskDefinition = registerOut.TaskDefinition.TaskDefinitionArn - _, err = es.client.UpdateService(updateInput) + _, err = es.client.UpdateService(context.TODO(), updateInput) if err != nil { return fmt.Errorf(awsApisErrorFmt, err) } @@ -133,7 +131,7 @@ func (es *ECSService) ClusterRollback(clusterName string) error { } servicesInfo := make([]ServiceInfo, 0, len(serviceARNptrs)) for _, serviceARNptr := range serviceARNptrs { - servicesInfo = append(servicesInfo, ServiceInfo{ARN: *serviceARNptr, TaskARN: ""}) + servicesInfo = append(servicesInfo, ServiceInfo{ARN: serviceARNptr, TaskARN: ""}) } return es.rollbackServices(servicesInfo, clusterName) } @@ -155,7 +153,7 @@ func (es *ECSService) ClusterSnapshot(clusterName string) ([]ServiceInfo, error) return } resultsCh <- ServiceInfo{ARN: serviceARN, TaskARN: taskARN} - }(*serviceARNptr) + }(serviceARNptr) } l := len(serviceARNptrs) servicesInfo := make([]ServiceInfo, 0, l) @@ -178,18 +176,18 @@ func (es *ECSService) ClusterRestore(serviceSnapshots []ServiceInfo, clusterName return es.rollbackServices(serviceSnapshots, clusterName) } -func (es *ECSService) listServices(clusterName string) ([]*string, error) { +func (es *ECSService) listServices(clusterName string) ([]string, error) { listInput := &ecs.ListServicesInput{ Cluster: aws.String(clusterName), } - listOut, err := es.client.ListServices(listInput) + listOut, err := es.client.ListServices(context.TODO(), listInput) if err != nil { return nil, err } serviceARNs := listOut.ServiceArns for listOut.NextToken != nil { listInput.NextToken = listOut.NextToken - listOut, err = es.client.ListServices(listInput) + listOut, err = es.client.ListServices(context.TODO(), listInput) if err != nil { return nil, err } @@ -201,11 +199,11 @@ func (es *ECSService) listServices(clusterName string) ([]*string, error) { func (es *ECSService) getCurrentTask(serviceName, clusterName string) (string, error) { input := &ecs.DescribeServicesInput{ Cluster: aws.String(clusterName), - Services: []*string{ - aws.String(serviceName), + Services: []string{ + serviceName, }, } - result, err := es.client.DescribeServices(input) + result, err := es.client.DescribeServices(context.TODO(), input) if err != nil { return "", err } @@ -281,15 +279,18 @@ func NewECSClient(verbose bool) *ECSService { os.Setenv("AWS_REGION", region) } } - session := session.New( - &aws.Config{ - HTTPClient: &http.Client{ - Timeout: time.Second * 20, - }, - }, - ) + ctx := context.TODO() + ctx, cancel := context.WithTimeout(ctx, 20*time.Second) + defer cancel() + + cfg, err := config.LoadDefaultConfig(ctx) + if err != nil { + log.Fatalf("unable to load SDK config, %v", err.Error()) + } + // create a new context from the previous ctx with a timeout, e.g. 5 seconds + return &ECSService{ verbose: verbose, - client: ecs.New(session), + client: ecs.NewFromConfig(cfg), } }