2020-08-18 11:38:23 +02:00
|
|
|
/*
|
2020-09-22 12:13:00 +02:00
|
|
|
Copyright 2020 Docker Compose CLI authors
|
2020-08-18 11:38:23 +02:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-08-17 17:48:52 +02:00
|
|
|
package ecs
|
2020-04-24 14:19:14 +02:00
|
|
|
|
|
|
|
import (
|
2020-04-28 11:31:41 +02:00
|
|
|
"context"
|
2020-09-07 11:20:41 +02:00
|
|
|
"encoding/json"
|
2020-04-24 14:19:14 +02:00
|
|
|
"fmt"
|
2020-05-27 16:50:04 +02:00
|
|
|
"strings"
|
2020-05-13 12:14:13 +02:00
|
|
|
"time"
|
2020-04-28 10:37:35 +02:00
|
|
|
|
2020-09-07 11:20:41 +02:00
|
|
|
"github.com/aws/aws-sdk-go/service/ssm"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ssm/ssmiface"
|
|
|
|
|
2020-09-07 13:22:08 +02:00
|
|
|
"github.com/docker/compose-cli/api/compose"
|
|
|
|
"github.com/docker/compose-cli/api/secrets"
|
2020-08-18 09:13:58 +02:00
|
|
|
|
2020-04-24 14:19:14 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2020-09-10 16:50:23 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws/request"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
2020-10-02 14:08:23 +02:00
|
|
|
"github.com/aws/aws-sdk-go/service/autoscaling"
|
|
|
|
"github.com/aws/aws-sdk-go/service/autoscaling/autoscalingiface"
|
2020-04-24 14:19:14 +02:00
|
|
|
"github.com/aws/aws-sdk-go/service/cloudformation"
|
|
|
|
"github.com/aws/aws-sdk-go/service/cloudformation/cloudformationiface"
|
|
|
|
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
|
|
|
|
"github.com/aws/aws-sdk-go/service/cloudwatchlogs/cloudwatchlogsiface"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ecs"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ecs/ecsiface"
|
2020-09-08 10:33:06 +02:00
|
|
|
"github.com/aws/aws-sdk-go/service/efs"
|
|
|
|
"github.com/aws/aws-sdk-go/service/efs/efsiface"
|
2020-04-24 14:19:14 +02:00
|
|
|
"github.com/aws/aws-sdk-go/service/elbv2"
|
|
|
|
"github.com/aws/aws-sdk-go/service/elbv2/elbv2iface"
|
|
|
|
"github.com/aws/aws-sdk-go/service/iam"
|
|
|
|
"github.com/aws/aws-sdk-go/service/iam/iamiface"
|
2020-04-29 16:54:24 +02:00
|
|
|
"github.com/aws/aws-sdk-go/service/secretsmanager"
|
|
|
|
"github.com/aws/aws-sdk-go/service/secretsmanager/secretsmanageriface"
|
2020-10-02 14:08:23 +02:00
|
|
|
"github.com/hashicorp/go-multierror"
|
2020-04-24 14:19:14 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type sdk struct {
|
2020-08-18 16:56:42 +02:00
|
|
|
ECS ecsiface.ECSAPI
|
|
|
|
EC2 ec2iface.EC2API
|
2020-09-08 10:33:06 +02:00
|
|
|
EFS efsiface.EFSAPI
|
2020-08-18 16:56:42 +02:00
|
|
|
ELB elbv2iface.ELBV2API
|
|
|
|
CW cloudwatchlogsiface.CloudWatchLogsAPI
|
|
|
|
IAM iamiface.IAMAPI
|
|
|
|
CF cloudformationiface.CloudFormationAPI
|
|
|
|
SM secretsmanageriface.SecretsManagerAPI
|
2020-09-07 11:20:41 +02:00
|
|
|
SSM ssmiface.SSMAPI
|
2020-10-02 14:08:23 +02:00
|
|
|
AG autoscalingiface.AutoScalingAPI
|
2020-04-24 14:19:14 +02:00
|
|
|
}
|
|
|
|
|
2020-09-10 16:50:23 +02:00
|
|
|
func newSDK(sess *session.Session) sdk {
|
|
|
|
sess.Handlers.Build.PushBack(func(r *request.Request) {
|
|
|
|
request.AddToUserAgent(r, "Docker CLI")
|
|
|
|
})
|
2020-04-24 14:19:14 +02:00
|
|
|
return sdk{
|
2020-04-27 09:46:13 +02:00
|
|
|
ECS: ecs.New(sess),
|
|
|
|
EC2: ec2.New(sess),
|
2020-09-08 10:33:06 +02:00
|
|
|
EFS: efs.New(sess),
|
2020-04-27 09:46:13 +02:00
|
|
|
ELB: elbv2.New(sess),
|
|
|
|
CW: cloudwatchlogs.New(sess),
|
|
|
|
IAM: iam.New(sess),
|
|
|
|
CF: cloudformation.New(sess),
|
2020-04-29 16:54:24 +02:00
|
|
|
SM: secretsmanager.New(sess),
|
2020-09-07 11:20:41 +02:00
|
|
|
SSM: ssm.New(sess),
|
2020-10-02 14:08:23 +02:00
|
|
|
AG: autoscaling.New(sess),
|
2020-04-24 14:19:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-03 17:12:38 +02:00
|
|
|
func (s sdk) CheckRequirements(ctx context.Context, region string) error {
|
2020-07-10 09:22:38 +02:00
|
|
|
settings, err := s.ECS.ListAccountSettingsWithContext(ctx, &ecs.ListAccountSettingsInput{
|
|
|
|
EffectiveSettings: aws.Bool(true),
|
|
|
|
Name: aws.String("serviceLongArnFormat"),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
serviceLongArnFormat := settings.Settings[0].Value
|
|
|
|
if *serviceLongArnFormat != "enabled" {
|
2020-08-03 17:12:38 +02:00
|
|
|
return fmt.Errorf("this tool requires the \"new ARN resource ID format\".\n"+
|
|
|
|
"Check https://%s.console.aws.amazon.com/ecs/home#/settings\n"+
|
2020-09-22 12:15:55 +02:00
|
|
|
"Learn more: https://aws.amazon.com/blogs/compute/migrating-your-amazon-ecs-deployment-to-the-new-arn-and-resource-id-format-2", region)
|
2020-07-10 09:22:38 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-28 11:31:41 +02:00
|
|
|
func (s sdk) ClusterExists(ctx context.Context, name string) (bool, error) {
|
2020-07-10 09:22:38 +02:00
|
|
|
logrus.Debug("CheckRequirements if cluster was already created: ", name)
|
2020-04-30 15:42:34 +02:00
|
|
|
clusters, err := s.ECS.DescribeClustersWithContext(ctx, &ecs.DescribeClustersInput{
|
2020-04-24 14:19:14 +02:00
|
|
|
Clusters: []*string{aws.String(name)},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return len(clusters.Clusters) > 0, nil
|
|
|
|
}
|
|
|
|
|
2020-04-28 11:31:41 +02:00
|
|
|
func (s sdk) CreateCluster(ctx context.Context, name string) (string, error) {
|
2020-04-24 14:19:14 +02:00
|
|
|
logrus.Debug("Create cluster ", name)
|
2020-04-30 15:42:34 +02:00
|
|
|
response, err := s.ECS.CreateClusterWithContext(ctx, &ecs.CreateClusterInput{ClusterName: aws.String(name)})
|
2020-04-24 14:19:14 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return *response.Cluster.Status, nil
|
|
|
|
}
|
|
|
|
|
2020-08-28 10:57:31 +02:00
|
|
|
func (s sdk) CheckVPC(ctx context.Context, vpcID string) error {
|
|
|
|
logrus.Debug("CheckRequirements on VPC : ", vpcID)
|
|
|
|
output, err := s.EC2.DescribeVpcAttributeWithContext(ctx, &ec2.DescribeVpcAttributeInput{
|
|
|
|
VpcId: aws.String(vpcID),
|
|
|
|
Attribute: aws.String("enableDnsSupport"),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !*output.EnableDnsSupport.Value {
|
|
|
|
return fmt.Errorf("VPC %q doesn't have DNS resolution enabled", vpcID)
|
|
|
|
}
|
|
|
|
return err
|
2020-04-28 14:33:40 +02:00
|
|
|
}
|
|
|
|
|
2020-04-28 11:31:41 +02:00
|
|
|
func (s sdk) GetDefaultVPC(ctx context.Context) (string, error) {
|
2020-04-24 14:19:14 +02:00
|
|
|
logrus.Debug("Retrieve default VPC")
|
2020-04-30 15:42:34 +02:00
|
|
|
vpcs, err := s.EC2.DescribeVpcsWithContext(ctx, &ec2.DescribeVpcsInput{
|
2020-04-24 14:19:14 +02:00
|
|
|
Filters: []*ec2.Filter{
|
|
|
|
{
|
|
|
|
Name: aws.String("isDefault"),
|
|
|
|
Values: []*string{aws.String("true")},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if len(vpcs.Vpcs) == 0 {
|
|
|
|
return "", fmt.Errorf("account has not default VPC")
|
|
|
|
}
|
|
|
|
return *vpcs.Vpcs[0].VpcId, nil
|
|
|
|
}
|
|
|
|
|
2020-04-28 11:31:41 +02:00
|
|
|
func (s sdk) GetSubNets(ctx context.Context, vpcID string) ([]string, error) {
|
2020-04-24 14:19:14 +02:00
|
|
|
logrus.Debug("Retrieve SubNets")
|
2020-04-30 15:42:34 +02:00
|
|
|
subnets, err := s.EC2.DescribeSubnetsWithContext(ctx, &ec2.DescribeSubnetsInput{
|
2020-04-24 14:19:14 +02:00
|
|
|
DryRun: nil,
|
|
|
|
Filters: []*ec2.Filter{
|
|
|
|
{
|
|
|
|
Name: aws.String("vpc-id"),
|
2020-04-28 11:05:00 +02:00
|
|
|
Values: []*string{aws.String(vpcID)},
|
2020-04-24 14:19:14 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ids := []string{}
|
|
|
|
for _, subnet := range subnets.Subnets {
|
|
|
|
ids = append(ids, *subnet.SubnetId)
|
|
|
|
}
|
|
|
|
return ids, nil
|
|
|
|
}
|
|
|
|
|
2020-04-28 11:31:41 +02:00
|
|
|
func (s sdk) GetRoleArn(ctx context.Context, name string) (string, error) {
|
2020-04-30 15:42:34 +02:00
|
|
|
role, err := s.IAM.GetRoleWithContext(ctx, &iam.GetRoleInput{
|
2020-04-24 14:19:14 +02:00
|
|
|
RoleName: aws.String(name),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return *role.Role.Arn, nil
|
|
|
|
}
|
|
|
|
|
2020-04-28 11:31:41 +02:00
|
|
|
func (s sdk) StackExists(ctx context.Context, name string) (bool, error) {
|
2020-04-30 15:42:34 +02:00
|
|
|
stacks, err := s.CF.DescribeStacksWithContext(ctx, &cloudformation.DescribeStacksInput{
|
2020-04-24 14:19:14 +02:00
|
|
|
StackName: aws.String(name),
|
|
|
|
})
|
|
|
|
if err != nil {
|
2020-09-07 11:20:41 +02:00
|
|
|
if strings.HasPrefix(err.Error(), fmt.Sprintf("ValidationError: Stack with ID %s does not exist", name)) {
|
2020-07-06 14:40:41 +02:00
|
|
|
return false, nil
|
|
|
|
}
|
2020-04-24 14:19:14 +02:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return len(stacks.Stacks) > 0, nil
|
|
|
|
}
|
|
|
|
|
2020-09-24 16:11:08 +02:00
|
|
|
func (s sdk) CreateStack(ctx context.Context, name string, template []byte) error {
|
2020-04-24 14:19:14 +02:00
|
|
|
logrus.Debug("Create CloudFormation stack")
|
|
|
|
|
2020-09-08 10:33:06 +02:00
|
|
|
_, err := s.CF.CreateStackWithContext(ctx, &cloudformation.CreateStackInput{
|
2020-04-24 14:19:14 +02:00
|
|
|
OnFailure: aws.String("DELETE"),
|
|
|
|
StackName: aws.String(name),
|
2020-09-08 10:33:06 +02:00
|
|
|
TemplateBody: aws.String(string(template)),
|
2020-07-27 16:23:46 +02:00
|
|
|
TimeoutInMinutes: nil,
|
2020-05-04 15:09:08 +02:00
|
|
|
Capabilities: []*string{
|
|
|
|
aws.String(cloudformation.CapabilityCapabilityIam),
|
|
|
|
},
|
2020-09-04 13:20:11 +02:00
|
|
|
Tags: []*cloudformation.Tag{
|
|
|
|
{
|
|
|
|
Key: aws.String(compose.ProjectTag),
|
|
|
|
Value: aws.String(name),
|
|
|
|
},
|
|
|
|
},
|
2020-04-24 14:19:14 +02:00
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
2020-06-15 17:48:56 +02:00
|
|
|
|
2020-09-24 16:11:08 +02:00
|
|
|
func (s sdk) CreateChangeSet(ctx context.Context, name string, template []byte) (string, error) {
|
2020-07-16 16:07:26 +02:00
|
|
|
logrus.Debug("Create CloudFormation Changeset")
|
|
|
|
|
|
|
|
update := fmt.Sprintf("Update%s", time.Now().Format("2006-01-02-15-04-05"))
|
|
|
|
changeset, err := s.CF.CreateChangeSetWithContext(ctx, &cloudformation.CreateChangeSetInput{
|
|
|
|
ChangeSetName: aws.String(update),
|
|
|
|
ChangeSetType: aws.String(cloudformation.ChangeSetTypeUpdate),
|
|
|
|
StackName: aws.String(name),
|
2020-09-08 10:33:06 +02:00
|
|
|
TemplateBody: aws.String(string(template)),
|
2020-07-16 16:07:26 +02:00
|
|
|
Capabilities: []*string{
|
|
|
|
aws.String(cloudformation.CapabilityCapabilityIam),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.CF.WaitUntilChangeSetCreateCompleteWithContext(ctx, &cloudformation.DescribeChangeSetInput{
|
|
|
|
ChangeSetName: changeset.Id,
|
|
|
|
})
|
|
|
|
return *changeset.Id, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s sdk) UpdateStack(ctx context.Context, changeset string) error {
|
|
|
|
desc, err := s.CF.DescribeChangeSetWithContext(ctx, &cloudformation.DescribeChangeSetInput{
|
|
|
|
ChangeSetName: aws.String(changeset),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(aws.StringValue(desc.StatusReason), "The submitted information didn't contain changes.") {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = s.CF.ExecuteChangeSet(&cloudformation.ExecuteChangeSetInput{
|
|
|
|
ChangeSetName: aws.String(changeset),
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-18 09:13:58 +02:00
|
|
|
const (
|
2020-08-18 16:56:42 +02:00
|
|
|
stackCreate = iota
|
|
|
|
stackUpdate
|
|
|
|
stackDelete
|
2020-08-18 09:13:58 +02:00
|
|
|
)
|
|
|
|
|
2020-04-30 15:42:34 +02:00
|
|
|
func (s sdk) WaitStackComplete(ctx context.Context, name string, operation int) error {
|
|
|
|
input := &cloudformation.DescribeStacksInput{
|
|
|
|
StackName: aws.String(name),
|
|
|
|
}
|
|
|
|
switch operation {
|
2020-08-18 16:56:42 +02:00
|
|
|
case stackCreate:
|
2020-04-30 15:42:34 +02:00
|
|
|
return s.CF.WaitUntilStackCreateCompleteWithContext(ctx, input)
|
2020-08-18 16:56:42 +02:00
|
|
|
case stackDelete:
|
2020-04-30 15:42:34 +02:00
|
|
|
return s.CF.WaitUntilStackDeleteCompleteWithContext(ctx, input)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("internal error: unexpected stack operation %d", operation)
|
|
|
|
}
|
|
|
|
}
|
2020-04-28 16:48:17 +02:00
|
|
|
|
2020-04-30 15:42:34 +02:00
|
|
|
func (s sdk) GetStackID(ctx context.Context, name string) (string, error) {
|
|
|
|
stacks, err := s.CF.DescribeStacksWithContext(ctx, &cloudformation.DescribeStacksInput{
|
|
|
|
StackName: aws.String(name),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2020-04-28 16:48:17 +02:00
|
|
|
}
|
2020-04-30 15:42:34 +02:00
|
|
|
return *stacks.Stacks[0].StackId, nil
|
2020-04-28 16:48:17 +02:00
|
|
|
}
|
2020-04-24 14:19:14 +02:00
|
|
|
|
2020-09-04 13:20:11 +02:00
|
|
|
func (s sdk) ListStacks(ctx context.Context, name string) ([]compose.Stack, error) {
|
2020-09-07 14:59:02 +02:00
|
|
|
params := cloudformation.DescribeStacksInput{}
|
|
|
|
if name != "" {
|
|
|
|
params.StackName = &name
|
|
|
|
}
|
|
|
|
cfStacks, err := s.CF.DescribeStacksWithContext(ctx, ¶ms)
|
2020-09-04 13:20:11 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
stacks := []compose.Stack{}
|
|
|
|
for _, stack := range cfStacks.Stacks {
|
|
|
|
for _, t := range stack.Tags {
|
|
|
|
if *t.Key == compose.ProjectTag {
|
2020-10-02 11:05:06 +02:00
|
|
|
status := compose.RUNNING
|
|
|
|
switch aws.StringValue(stack.StackStatus) {
|
|
|
|
case "CREATE_IN_PROGRESS":
|
|
|
|
status = compose.STARTING
|
|
|
|
case "DELETE_IN_PROGRESS":
|
|
|
|
status = compose.REMOVING
|
|
|
|
case "UPDATE_IN_PROGRESS":
|
|
|
|
status = compose.UPDATING
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
stacks = append(stacks, compose.Stack{
|
|
|
|
ID: aws.StringValue(stack.StackId),
|
|
|
|
Name: aws.StringValue(stack.StackName),
|
|
|
|
Status: status,
|
|
|
|
})
|
2020-09-04 18:11:02 +02:00
|
|
|
break
|
2020-09-04 13:20:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return stacks, nil
|
|
|
|
}
|
|
|
|
|
2020-10-02 11:05:06 +02:00
|
|
|
func (s sdk) GetStackClusterID(ctx context.Context, stack string) (string, error) {
|
2020-09-30 14:52:53 +02:00
|
|
|
resources, err := s.CF.ListStackResourcesWithContext(ctx, &cloudformation.ListStackResourcesInput{
|
2020-10-02 11:05:06 +02:00
|
|
|
StackName: aws.String(stack),
|
2020-09-30 14:52:53 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
2020-10-02 11:05:06 +02:00
|
|
|
return "", err
|
2020-09-30 14:52:53 +02:00
|
|
|
}
|
|
|
|
for _, r := range resources.StackResourceSummaries {
|
|
|
|
if aws.StringValue(r.ResourceType) == "AWS::ECS::Cluster" {
|
2020-10-02 11:05:06 +02:00
|
|
|
return aws.StringValue(r.PhysicalResourceId), nil
|
2020-09-30 14:52:53 +02:00
|
|
|
}
|
|
|
|
}
|
2020-10-02 11:05:06 +02:00
|
|
|
return "", nil
|
2020-09-30 14:52:53 +02:00
|
|
|
}
|
|
|
|
|
2020-10-02 11:05:06 +02:00
|
|
|
func (s sdk) GetServiceTaskDefinition(ctx context.Context, cluster string, serviceArns []string) (map[string]string, error) {
|
|
|
|
defs := map[string]string{}
|
|
|
|
svc := []*string{}
|
|
|
|
for _, s := range serviceArns {
|
|
|
|
svc = append(svc, aws.String(s))
|
|
|
|
}
|
|
|
|
services, err := s.ECS.DescribeServicesWithContext(ctx, &ecs.DescribeServicesInput{
|
|
|
|
Cluster: aws.String(cluster),
|
|
|
|
Services: svc,
|
2020-09-30 14:52:53 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
2020-10-02 11:05:06 +02:00
|
|
|
return nil, err
|
2020-09-30 14:52:53 +02:00
|
|
|
}
|
2020-10-02 11:05:06 +02:00
|
|
|
for _, s := range services.Services {
|
|
|
|
defs[aws.StringValue(s.ServiceArn)] = aws.StringValue(s.TaskDefinition)
|
2020-09-30 14:52:53 +02:00
|
|
|
}
|
2020-10-02 11:05:06 +02:00
|
|
|
return defs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s sdk) ListStackServices(ctx context.Context, stack string) ([]string, error) {
|
|
|
|
arns := []string{}
|
|
|
|
var nextToken *string
|
|
|
|
for {
|
|
|
|
response, err := s.CF.ListStackResourcesWithContext(ctx, &cloudformation.ListStackResourcesInput{
|
|
|
|
StackName: aws.String(stack),
|
|
|
|
NextToken: nextToken,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, r := range response.StackResourceSummaries {
|
|
|
|
if aws.StringValue(r.ResourceType) == "AWS::ECS::Service" {
|
|
|
|
if r.PhysicalResourceId != nil {
|
|
|
|
arns = append(arns, aws.StringValue(r.PhysicalResourceId))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nextToken = response.NextToken
|
|
|
|
if nextToken == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return arns, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s sdk) GetServiceTasks(ctx context.Context, cluster string, service string, stopped bool) ([]*ecs.Task, error) {
|
|
|
|
state := "RUNNING"
|
|
|
|
if stopped {
|
|
|
|
state = "STOPPED"
|
|
|
|
}
|
|
|
|
tasks, err := s.ECS.ListTasksWithContext(ctx, &ecs.ListTasksInput{
|
2020-09-30 14:52:53 +02:00
|
|
|
Cluster: aws.String(cluster),
|
2020-10-02 11:05:06 +02:00
|
|
|
ServiceName: aws.String(service),
|
|
|
|
DesiredStatus: aws.String(state),
|
2020-09-30 14:52:53 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
2020-10-02 11:05:06 +02:00
|
|
|
return nil, err
|
2020-09-30 14:52:53 +02:00
|
|
|
}
|
|
|
|
if len(tasks.TaskArns) > 0 {
|
|
|
|
taskDescriptions, err := s.ECS.DescribeTasksWithContext(ctx, &ecs.DescribeTasksInput{
|
|
|
|
Cluster: aws.String(cluster),
|
|
|
|
Tasks: tasks.TaskArns,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2020-10-02 11:05:06 +02:00
|
|
|
return nil, err
|
2020-09-30 14:52:53 +02:00
|
|
|
}
|
2020-10-02 11:05:06 +02:00
|
|
|
return taskDescriptions.Tasks, nil
|
2020-09-30 14:52:53 +02:00
|
|
|
}
|
2020-10-02 11:05:06 +02:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s sdk) GetTaskStoppedReason(ctx context.Context, cluster string, taskArn string) (string, error) {
|
|
|
|
taskDescriptions, err := s.ECS.DescribeTasksWithContext(ctx, &ecs.DescribeTasksInput{
|
|
|
|
Cluster: aws.String(cluster),
|
|
|
|
Tasks: []*string{aws.String(taskArn)},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if len(taskDescriptions.Tasks) == 0 {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
task := taskDescriptions.Tasks[0]
|
|
|
|
return fmt.Sprintf(
|
|
|
|
"%s: %s",
|
|
|
|
aws.StringValue(task.StopCode),
|
|
|
|
aws.StringValue(task.StoppedReason)), nil
|
|
|
|
|
2020-09-30 14:52:53 +02:00
|
|
|
}
|
|
|
|
|
2020-04-30 15:42:34 +02:00
|
|
|
func (s sdk) DescribeStackEvents(ctx context.Context, stackID string) ([]*cloudformation.StackEvent, error) {
|
2020-04-24 14:19:14 +02:00
|
|
|
// Fixme implement Paginator on Events and return as a chan(events)
|
2020-04-28 16:48:17 +02:00
|
|
|
events := []*cloudformation.StackEvent{}
|
|
|
|
var nextToken *string
|
|
|
|
for {
|
2020-04-30 15:42:34 +02:00
|
|
|
resp, err := s.CF.DescribeStackEventsWithContext(ctx, &cloudformation.DescribeStackEventsInput{
|
|
|
|
StackName: aws.String(stackID),
|
2020-04-28 16:48:17 +02:00
|
|
|
NextToken: nextToken,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-02 11:05:06 +02:00
|
|
|
|
2020-04-28 16:48:17 +02:00
|
|
|
events = append(events, resp.StackEvents...)
|
|
|
|
if resp.NextToken == nil {
|
|
|
|
return events, nil
|
|
|
|
}
|
|
|
|
nextToken = resp.NextToken
|
|
|
|
}
|
2020-04-24 14:19:14 +02:00
|
|
|
}
|
|
|
|
|
2020-07-15 16:27:24 +02:00
|
|
|
func (s sdk) ListStackParameters(ctx context.Context, name string) (map[string]string, error) {
|
|
|
|
st, err := s.CF.DescribeStacksWithContext(ctx, &cloudformation.DescribeStacksInput{
|
|
|
|
NextToken: nil,
|
|
|
|
StackName: aws.String(name),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
parameters := map[string]string{}
|
|
|
|
for _, parameter := range st.Stacks[0].Parameters {
|
2020-07-22 15:48:04 +02:00
|
|
|
parameters[aws.StringValue(parameter.ParameterKey)] = aws.StringValue(parameter.ParameterValue)
|
2020-07-15 16:27:24 +02:00
|
|
|
}
|
|
|
|
return parameters, nil
|
|
|
|
}
|
|
|
|
|
2020-08-18 16:56:42 +02:00
|
|
|
type stackResource struct {
|
2020-08-18 09:13:58 +02:00
|
|
|
LogicalID string
|
|
|
|
Type string
|
|
|
|
ARN string
|
|
|
|
Status string
|
|
|
|
}
|
2020-08-18 11:38:23 +02:00
|
|
|
|
2020-10-02 14:08:23 +02:00
|
|
|
type stackResourceFn func(r stackResource) error
|
|
|
|
|
|
|
|
type stackResources []stackResource
|
|
|
|
|
|
|
|
func (resources stackResources) apply(awsType string, fn stackResourceFn) error {
|
|
|
|
var errs *multierror.Error
|
|
|
|
for _, r := range resources {
|
|
|
|
if r.Type == awsType {
|
|
|
|
err := fn(r)
|
|
|
|
if err != nil {
|
|
|
|
errs = multierror.Append(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errs.ErrorOrNil()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s sdk) ListStackResources(ctx context.Context, name string) (stackResources, error) {
|
2020-07-03 11:07:08 +02:00
|
|
|
// FIXME handle pagination
|
|
|
|
res, err := s.CF.ListStackResourcesWithContext(ctx, &cloudformation.ListStackResourcesInput{
|
|
|
|
StackName: aws.String(name),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-10-02 14:08:23 +02:00
|
|
|
resources := stackResources{}
|
2020-07-03 11:07:08 +02:00
|
|
|
for _, r := range res.StackResourceSummaries {
|
2020-08-18 16:56:42 +02:00
|
|
|
resources = append(resources, stackResource{
|
2020-07-22 15:48:04 +02:00
|
|
|
LogicalID: aws.StringValue(r.LogicalResourceId),
|
|
|
|
Type: aws.StringValue(r.ResourceType),
|
|
|
|
ARN: aws.StringValue(r.PhysicalResourceId),
|
|
|
|
Status: aws.StringValue(r.ResourceStatus),
|
2020-07-03 11:07:08 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return resources, nil
|
|
|
|
}
|
|
|
|
|
2020-04-28 11:31:41 +02:00
|
|
|
func (s sdk) DeleteStack(ctx context.Context, name string) error {
|
2020-04-24 14:19:14 +02:00
|
|
|
logrus.Debug("Delete CloudFormation stack")
|
2020-04-30 15:42:34 +02:00
|
|
|
_, err := s.CF.DeleteStackWithContext(ctx, &cloudformation.DeleteStackInput{
|
2020-04-24 14:19:14 +02:00
|
|
|
StackName: aws.String(name),
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
2020-04-29 16:54:24 +02:00
|
|
|
|
2020-08-18 09:13:58 +02:00
|
|
|
func (s sdk) CreateSecret(ctx context.Context, secret secrets.Secret) (string, error) {
|
2020-05-05 18:55:03 +02:00
|
|
|
logrus.Debug("Create secret " + secret.Name)
|
|
|
|
secretStr, err := secret.GetCredString()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := s.SM.CreateSecret(&secretsmanager.CreateSecretInput{
|
|
|
|
Name: &secret.Name,
|
|
|
|
SecretString: &secretStr,
|
|
|
|
Description: &secret.Description,
|
|
|
|
})
|
2020-04-29 20:36:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2020-07-22 15:48:04 +02:00
|
|
|
return aws.StringValue(response.ARN), nil
|
2020-04-29 16:54:24 +02:00
|
|
|
}
|
|
|
|
|
2020-08-18 09:13:58 +02:00
|
|
|
func (s sdk) InspectSecret(ctx context.Context, id string) (secrets.Secret, error) {
|
2020-04-29 20:36:00 +02:00
|
|
|
logrus.Debug("Inspect secret " + id)
|
|
|
|
response, err := s.SM.DescribeSecret(&secretsmanager.DescribeSecretInput{SecretId: &id})
|
|
|
|
if err != nil {
|
2020-08-18 09:13:58 +02:00
|
|
|
return secrets.Secret{}, err
|
2020-04-29 20:36:00 +02:00
|
|
|
}
|
|
|
|
labels := map[string]string{}
|
|
|
|
for _, tag := range response.Tags {
|
2020-07-22 15:48:04 +02:00
|
|
|
labels[aws.StringValue(tag.Key)] = aws.StringValue(tag.Value)
|
2020-04-29 20:36:00 +02:00
|
|
|
}
|
2020-08-18 09:13:58 +02:00
|
|
|
secret := secrets.Secret{
|
2020-07-22 15:48:04 +02:00
|
|
|
ID: aws.StringValue(response.ARN),
|
|
|
|
Name: aws.StringValue(response.Name),
|
2020-04-29 20:36:00 +02:00
|
|
|
Labels: labels,
|
|
|
|
}
|
|
|
|
if response.Description != nil {
|
|
|
|
secret.Description = *response.Description
|
|
|
|
}
|
|
|
|
return secret, nil
|
2020-04-29 16:54:24 +02:00
|
|
|
}
|
|
|
|
|
2020-08-18 09:13:58 +02:00
|
|
|
func (s sdk) ListSecrets(ctx context.Context) ([]secrets.Secret, error) {
|
2020-04-29 20:36:00 +02:00
|
|
|
logrus.Debug("List secrets ...")
|
|
|
|
response, err := s.SM.ListSecrets(&secretsmanager.ListSecretsInput{})
|
|
|
|
if err != nil {
|
2020-08-18 09:13:58 +02:00
|
|
|
return nil, err
|
2020-04-29 20:36:00 +02:00
|
|
|
}
|
|
|
|
|
2020-08-18 09:13:58 +02:00
|
|
|
var ls []secrets.Secret
|
2020-04-29 20:36:00 +02:00
|
|
|
for _, sec := range response.SecretList {
|
|
|
|
|
|
|
|
labels := map[string]string{}
|
|
|
|
for _, tag := range sec.Tags {
|
|
|
|
labels[*tag.Key] = *tag.Value
|
|
|
|
}
|
|
|
|
description := ""
|
|
|
|
if sec.Description != nil {
|
|
|
|
description = *sec.Description
|
|
|
|
}
|
2020-08-18 09:13:58 +02:00
|
|
|
ls = append(ls, secrets.Secret{
|
2020-04-29 20:36:00 +02:00
|
|
|
ID: *sec.ARN,
|
|
|
|
Name: *sec.Name,
|
|
|
|
Labels: labels,
|
|
|
|
Description: description,
|
|
|
|
})
|
|
|
|
}
|
2020-08-18 09:13:58 +02:00
|
|
|
return ls, nil
|
2020-04-29 16:54:24 +02:00
|
|
|
}
|
|
|
|
|
2020-04-29 20:36:00 +02:00
|
|
|
func (s sdk) DeleteSecret(ctx context.Context, id string, recover bool) error {
|
|
|
|
logrus.Debug("List secrets ...")
|
|
|
|
force := !recover
|
|
|
|
_, err := s.SM.DeleteSecret(&secretsmanager.DeleteSecretInput{SecretId: &id, ForceDeleteWithoutRecovery: &force})
|
|
|
|
return err
|
2020-04-29 16:54:24 +02:00
|
|
|
}
|
2020-05-13 12:14:13 +02:00
|
|
|
|
2020-08-18 09:13:58 +02:00
|
|
|
func (s sdk) GetLogs(ctx context.Context, name string, consumer func(service, container, message string)) error {
|
2020-05-13 12:14:13 +02:00
|
|
|
logGroup := fmt.Sprintf("/docker-compose/%s", name)
|
|
|
|
var startTime int64
|
|
|
|
for {
|
2020-08-21 15:56:31 +02:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
var hasMore = true
|
|
|
|
var token *string
|
|
|
|
for hasMore {
|
|
|
|
events, err := s.CW.FilterLogEvents(&cloudwatchlogs.FilterLogEventsInput{
|
|
|
|
LogGroupName: aws.String(logGroup),
|
|
|
|
NextToken: token,
|
|
|
|
StartTime: aws.Int64(startTime),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if events.NextToken == nil {
|
|
|
|
hasMore = false
|
|
|
|
} else {
|
|
|
|
token = events.NextToken
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, event := range events.Events {
|
|
|
|
p := strings.Split(aws.StringValue(event.LogStreamName), "/")
|
|
|
|
consumer(p[1], p[2], aws.StringValue(event.Message))
|
|
|
|
startTime = *event.IngestionTime
|
|
|
|
}
|
2020-05-13 12:14:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}
|
2020-05-18 16:01:00 +02:00
|
|
|
|
2020-10-02 11:05:06 +02:00
|
|
|
func (s sdk) DescribeService(ctx context.Context, cluster string, arn string) (compose.ServiceStatus, error) {
|
2020-06-17 10:20:12 +02:00
|
|
|
services, err := s.ECS.DescribeServicesWithContext(ctx, &ecs.DescribeServicesInput{
|
|
|
|
Cluster: aws.String(cluster),
|
2020-10-02 11:05:06 +02:00
|
|
|
Services: []*string{aws.String(arn)},
|
2020-06-29 10:44:40 +02:00
|
|
|
Include: aws.StringSlice([]string{"TAGS"}),
|
2020-05-18 16:01:00 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
2020-10-02 11:05:06 +02:00
|
|
|
return compose.ServiceStatus{}, err
|
2020-05-18 16:01:00 +02:00
|
|
|
}
|
2020-07-21 17:45:16 +02:00
|
|
|
|
2020-10-02 11:05:06 +02:00
|
|
|
service := services.Services[0]
|
|
|
|
var name string
|
|
|
|
for _, t := range service.Tags {
|
|
|
|
if *t.Key == compose.ServiceTag {
|
|
|
|
name = aws.StringValue(t.Value)
|
2020-06-25 08:14:54 +02:00
|
|
|
}
|
|
|
|
}
|
2020-10-02 11:05:06 +02:00
|
|
|
if name == "" {
|
|
|
|
return compose.ServiceStatus{}, fmt.Errorf("service %s doesn't have a %s tag", *service.ServiceArn, compose.ServiceTag)
|
|
|
|
}
|
|
|
|
targetGroupArns := []string{}
|
|
|
|
for _, lb := range service.LoadBalancers {
|
|
|
|
targetGroupArns = append(targetGroupArns, *lb.TargetGroupArn)
|
|
|
|
}
|
|
|
|
// getURLwithPortMapping makes 2 queries
|
|
|
|
// one to get the target groups and another for load balancers
|
|
|
|
loadBalancers, err := s.getURLWithPortMapping(ctx, targetGroupArns)
|
|
|
|
if err != nil {
|
|
|
|
return compose.ServiceStatus{}, err
|
|
|
|
}
|
|
|
|
return compose.ServiceStatus{
|
|
|
|
ID: aws.StringValue(service.ServiceName),
|
|
|
|
Name: name,
|
|
|
|
Replicas: int(aws.Int64Value(service.RunningCount)),
|
|
|
|
Desired: int(aws.Int64Value(service.DesiredCount)),
|
|
|
|
Publishers: loadBalancers,
|
|
|
|
}, nil
|
2020-05-18 16:01:00 +02:00
|
|
|
}
|
|
|
|
|
2020-08-18 11:38:23 +02:00
|
|
|
func (s sdk) getURLWithPortMapping(ctx context.Context, targetGroupArns []string) ([]compose.PortPublisher, error) {
|
2020-07-21 17:45:16 +02:00
|
|
|
if len(targetGroupArns) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
groups, err := s.ELB.DescribeTargetGroups(&elbv2.DescribeTargetGroupsInput{
|
|
|
|
TargetGroupArns: aws.StringSlice(targetGroupArns),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
lbarns := []*string{}
|
|
|
|
for _, tg := range groups.TargetGroups {
|
|
|
|
lbarns = append(lbarns, tg.LoadBalancerArns...)
|
|
|
|
}
|
|
|
|
|
|
|
|
lbs, err := s.ELB.DescribeLoadBalancersWithContext(ctx, &elbv2.DescribeLoadBalancersInput{
|
|
|
|
LoadBalancerArns: lbarns,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
filterLB := func(arn *string, lbs []*elbv2.LoadBalancer) *elbv2.LoadBalancer {
|
2020-07-22 15:48:04 +02:00
|
|
|
if aws.StringValue(arn) == "" {
|
|
|
|
// load balancer arn is nil/""
|
|
|
|
return nil
|
|
|
|
}
|
2020-07-21 17:45:16 +02:00
|
|
|
for _, lb := range lbs {
|
2020-07-22 15:48:04 +02:00
|
|
|
if aws.StringValue(lb.LoadBalancerArn) == aws.StringValue(arn) {
|
2020-07-21 17:45:16 +02:00
|
|
|
return lb
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-08-18 11:38:23 +02:00
|
|
|
loadBalancers := []compose.PortPublisher{}
|
2020-07-21 17:45:16 +02:00
|
|
|
for _, tg := range groups.TargetGroups {
|
|
|
|
for _, lbarn := range tg.LoadBalancerArns {
|
|
|
|
lb := filterLB(lbarn, lbs.LoadBalancers)
|
|
|
|
if lb == nil {
|
|
|
|
continue
|
|
|
|
}
|
2020-08-18 11:38:23 +02:00
|
|
|
loadBalancers = append(loadBalancers, compose.PortPublisher{
|
2020-07-22 15:48:04 +02:00
|
|
|
URL: aws.StringValue(lb.DNSName),
|
|
|
|
TargetPort: int(aws.Int64Value(tg.Port)),
|
|
|
|
PublishedPort: int(aws.Int64Value(tg.Port)),
|
|
|
|
Protocol: aws.StringValue(tg.Protocol),
|
2020-07-21 17:45:16 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return loadBalancers, nil
|
|
|
|
}
|
|
|
|
|
2020-06-17 10:20:12 +02:00
|
|
|
func (s sdk) ListTasks(ctx context.Context, cluster string, family string) ([]string, error) {
|
|
|
|
tasks, err := s.ECS.ListTasksWithContext(ctx, &ecs.ListTasksInput{
|
2020-05-18 16:01:00 +02:00
|
|
|
Cluster: aws.String(cluster),
|
2020-06-17 10:20:12 +02:00
|
|
|
Family: aws.String(family),
|
2020-05-18 16:01:00 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-17 10:20:12 +02:00
|
|
|
arns := []string{}
|
|
|
|
for _, arn := range tasks.TaskArns {
|
|
|
|
arns = append(arns, *arn)
|
2020-05-18 16:01:00 +02:00
|
|
|
}
|
2020-06-17 10:20:12 +02:00
|
|
|
return arns, nil
|
2020-05-18 16:01:00 +02:00
|
|
|
}
|
|
|
|
|
2020-05-28 09:24:21 +02:00
|
|
|
func (s sdk) GetPublicIPs(ctx context.Context, interfaces ...string) (map[string]string, error) {
|
2020-05-18 16:01:00 +02:00
|
|
|
desc, err := s.EC2.DescribeNetworkInterfaces(&ec2.DescribeNetworkInterfacesInput{
|
|
|
|
NetworkInterfaceIds: aws.StringSlice(interfaces),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-28 09:24:21 +02:00
|
|
|
publicIPs := map[string]string{}
|
2020-05-18 16:01:00 +02:00
|
|
|
for _, interf := range desc.NetworkInterfaces {
|
2020-05-27 14:38:50 +02:00
|
|
|
if interf.Association != nil {
|
2020-07-22 15:48:04 +02:00
|
|
|
publicIPs[aws.StringValue(interf.NetworkInterfaceId)] = aws.StringValue(interf.Association.PublicIp)
|
2020-05-27 14:38:50 +02:00
|
|
|
}
|
2020-05-18 16:01:00 +02:00
|
|
|
}
|
|
|
|
return publicIPs, nil
|
|
|
|
}
|
2020-06-04 16:28:11 +02:00
|
|
|
|
2020-09-24 16:11:08 +02:00
|
|
|
func (s sdk) LoadBalancerType(ctx context.Context, arn string) (string, error) {
|
|
|
|
logrus.Debug("Check if LoadBalancer exists: ", arn)
|
2020-06-04 16:28:11 +02:00
|
|
|
lbs, err := s.ELB.DescribeLoadBalancersWithContext(ctx, &elbv2.DescribeLoadBalancersInput{
|
2020-07-03 11:07:08 +02:00
|
|
|
LoadBalancerArns: []*string{aws.String(arn)},
|
2020-06-04 16:28:11 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
2020-09-24 16:11:08 +02:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if len(lbs.LoadBalancers) == 0 {
|
|
|
|
return "", fmt.Errorf("load balancer does not exist: %s", arn)
|
2020-06-04 16:28:11 +02:00
|
|
|
}
|
2020-09-24 16:11:08 +02:00
|
|
|
return aws.StringValue(lbs.LoadBalancers[0].Type), nil
|
2020-06-04 16:28:11 +02:00
|
|
|
}
|
|
|
|
|
2020-07-03 11:07:08 +02:00
|
|
|
func (s sdk) GetLoadBalancerURL(ctx context.Context, arn string) (string, error) {
|
|
|
|
logrus.Debug("Retrieve load balancer URL: ", arn)
|
2020-06-04 16:28:11 +02:00
|
|
|
lbs, err := s.ELB.DescribeLoadBalancersWithContext(ctx, &elbv2.DescribeLoadBalancersInput{
|
2020-07-03 11:07:08 +02:00
|
|
|
LoadBalancerArns: []*string{aws.String(arn)},
|
2020-06-04 16:28:11 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2020-07-22 15:48:04 +02:00
|
|
|
dnsName := aws.StringValue(lbs.LoadBalancers[0].DNSName)
|
|
|
|
if dnsName == "" {
|
|
|
|
return "", fmt.Errorf("Load balancer %s doesn't have a dns name", aws.StringValue(lbs.LoadBalancers[0].LoadBalancerArn))
|
|
|
|
}
|
|
|
|
return dnsName, nil
|
2020-06-04 16:28:11 +02:00
|
|
|
}
|
2020-09-08 10:33:06 +02:00
|
|
|
|
|
|
|
func (s sdk) WithVolumeSecurityGroups(ctx context.Context, id string, fn func(securityGroups []string) error) error {
|
|
|
|
mounts, err := s.EFS.DescribeMountTargetsWithContext(ctx, &efs.DescribeMountTargetsInput{
|
|
|
|
FileSystemId: aws.String(id),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, mount := range mounts.MountTargets {
|
|
|
|
groups, err := s.EFS.DescribeMountTargetSecurityGroupsWithContext(ctx, &efs.DescribeMountTargetSecurityGroupsInput{
|
|
|
|
MountTargetId: mount.MountTargetId,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = fn(aws.StringValueSlice(groups.SecurityGroups))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-09-07 11:20:41 +02:00
|
|
|
|
|
|
|
func (s sdk) GetParameter(ctx context.Context, name string) (string, error) {
|
|
|
|
parameter, err := s.SSM.GetParameterWithContext(ctx, &ssm.GetParameterInput{
|
|
|
|
Name: aws.String(name),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
value := *parameter.Parameter.Value
|
|
|
|
var ami struct {
|
|
|
|
SchemaVersion int `json:"schema_version"`
|
|
|
|
ImageName string `json:"image_name"`
|
|
|
|
ImageID string `json:"image_id"`
|
|
|
|
OS string `json:"os"`
|
|
|
|
ECSRuntimeVersion string `json:"ecs_runtime_verion"`
|
|
|
|
ECSAgentVersion string `json:"ecs_agent_version"`
|
|
|
|
}
|
|
|
|
err = json.Unmarshal([]byte(value), &ami)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ami.ImageID, nil
|
|
|
|
}
|
2020-09-29 08:31:11 +02:00
|
|
|
|
|
|
|
func (s sdk) SecurityGroupExists(ctx context.Context, sg string) (bool, error) {
|
|
|
|
desc, err := s.EC2.DescribeSecurityGroupsWithContext(ctx, &ec2.DescribeSecurityGroupsInput{
|
|
|
|
GroupIds: aws.StringSlice([]string{sg}),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return len(desc.SecurityGroups) > 0, nil
|
|
|
|
}
|
2020-10-02 14:08:23 +02:00
|
|
|
|
|
|
|
func (s sdk) DeleteCapacityProvider(ctx context.Context, arn string) error {
|
|
|
|
_, err := s.ECS.DeleteCapacityProvider(&ecs.DeleteCapacityProviderInput{
|
|
|
|
CapacityProvider: aws.String(arn),
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s sdk) DeleteAutoscalingGroup(ctx context.Context, arn string) error {
|
|
|
|
_, err := s.AG.DeleteAutoScalingGroup(&autoscaling.DeleteAutoScalingGroupInput{
|
|
|
|
AutoScalingGroupName: aws.String(arn),
|
|
|
|
ForceDelete: aws.Bool(true),
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|