Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/t-kikuc/ecstop
Browse files Browse the repository at this point in the history
Signed-off-by: t-kikuc <[email protected]>
  • Loading branch information
t-kikuc committed Dec 27, 2024
2 parents ef52341 + 4b9c423 commit 4cda5c9
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 19 deletions.
21 changes: 19 additions & 2 deletions pkg/stop/cluster_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package stop

import (
"context"
"strings"

"github.com/spf13/cobra"
"github.com/t-kikuc/ecstop/pkg/client"
Expand All @@ -26,7 +27,7 @@ func addClusterFlags(c *cobra.Command, clusterP *clusterOptions) {
c.MarkFlagsMutuallyExclusive(flag_cluster, flag_allClusters)
}

func (co clusterOptions) DecideClusters(ctx context.Context, cli *client.ECSClient) ([]string, error) {
func (co clusterOptions) DecideClusters(ctx context.Context, cli *client.ECSClient) (clusterNames []string, err error) {
if co.cluster != "" {
return []string{co.cluster}, nil
}
Expand All @@ -36,5 +37,21 @@ func (co clusterOptions) DecideClusters(ctx context.Context, cli *client.ECSClie
if err != nil {
return nil, err
}
return clusters, nil

for _, cluster := range clusters {
clusterNames = append(clusterNames, shortenClusterArn(cluster))
}

return clusterNames, nil
}

// shortenClusterArn transforms arn:aws:ecs:us-west-2:123456789012:cluster/cluster-name -> cluster-name
func shortenClusterArn(arn string) string {
if strings.Contains(arn, "/") {
split := strings.Split(arn, "/")
return split[len(split)-1]
} else {
// Maybe it's already a cluster name
return arn
}
}
29 changes: 29 additions & 0 deletions pkg/stop/cluster_opts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,32 @@ func TestAddClusterFlags(t *testing.T) {
})
}
}

func TestShortenClusterName(t *testing.T) {
t.Parallel()

testcases := []struct {
title string
arn string
want string
}{
{
title: "cluster ARN",
arn: "arn:aws:ecs:us-west-2:123456789012:cluster/cluster-name",
want: "cluster-name",
},
{
title: "cluster name",
arn: "cluster-name",
want: "cluster-name",
},
}

for _, tc := range testcases {
tc := tc
t.Run(tc.title, func(t *testing.T) {
t.Parallel()
assert.Equal(t, tc.want, shortenClusterArn(tc.arn))
})
}
}
13 changes: 7 additions & 6 deletions pkg/stop/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func stopInstances(ctx context.Context, ecsCli *client.ECSClient, ec2Cli *client
return err
}
if len(instanceArns) == 0 {
log.Printf("[%s] No instance found in cluster\n", cluster)
log.Printf("[%s] No instances found in cluster\n", cluster)
return nil
}

Expand All @@ -76,18 +76,19 @@ func stopInstances(ctx context.Context, ecsCli *client.ECSClient, ec2Cli *client
if err := ec2Cli.StopInstances(ctx, instanceArns); err != nil {
return fmt.Errorf("failed to stop instances: %w", err)
}
log.Printf(" -> Successfully stopped %d instances\n", len(instanceArns))
log.Printf(" -> Successfully stopped %d instances\n", len(instanceArns))
return nil
}

func printPreSummaryInstance(cluster string, instanceArns []string) {
log.Printf("[%s] Instances: %d\n", cluster, len(instanceArns))
if len(instanceArns) > 0 {
txt := fmt.Sprintf("[%s] Instances: %d", cluster, len(instanceArns))
if len(instanceArns) <= 0 {
log.Printf("%s -> Not instances to stop", txt)
} else {
log.Println(txt)
log.Println("Instances to stop:")
for i, inst := range instanceArns {
log.Printf(" [%d] %s\n", i+1, inst)
}
} else {
log.Println(" -> No instance to stop")
}
}
13 changes: 7 additions & 6 deletions pkg/stop/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func stopServices(ctx context.Context, cli *client.ECSClient, cluster string) er
return fmt.Errorf("failed to list services of cluster %s: %w", cluster, err)
}
if len(services) == 0 {
log.Printf("[%s] No service found in cluster\n", cluster)
log.Printf("[%s] No services found in cluster\n", cluster)
return nil
}

Expand All @@ -78,7 +78,7 @@ func stopServices(ctx context.Context, cli *client.ECSClient, cluster string) er
if err != nil {
return fmt.Errorf("failed to scale-in [%d]%s: %w", i+1, *s.ServiceName, err)
} else {
log.Printf(" -> successfully scaled-in [%d]%s \n", i+1, *s.ServiceName)
log.Printf(" -> successfully scaled-in [%d]%s \n", i+1, *s.ServiceName)
}
}

Expand All @@ -101,13 +101,14 @@ func printPreSummary(cluster string, services []types.Service, runningServices [
total := len(services)
running := len(runningServices)

log.Printf("[%s] Total Services: %d, Running Services: %d", cluster, total, running)
if running > 0 {
txt := fmt.Sprintf("[%s] Total Services: %d, Running Services: %d", cluster, total, running)
if running <= 0 {
log.Printf("%s -> No services to scale-in\n", txt)
} else {
log.Println(txt)
log.Printf("\nRunning Services:\n")
for i, s := range runningServices {
log.Printf(" [%d] %s) running: %d, desired: %d\n", i+1, *s.ServiceName, s.RunningCount, s.DesiredCount)
}
} else {
log.Printf(" -> No service to scale-in\n")
}
}
12 changes: 7 additions & 5 deletions pkg/stop/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package stop

import (
"context"
"fmt"
"log"
"strings"

Expand Down Expand Up @@ -93,7 +94,7 @@ func (o *taskOptions) stopTasks(ctx context.Context, cli *client.ECSClient, clus
if err = cli.StopTask(ctx, cluster, *task.TaskArn); err != nil {
return err
}
log.Printf(" -> Successfully stopped Task: %s\n", *task.TaskArn)
log.Printf(" -> Successfully stopped Task: %s\n", *task.TaskArn)
}
return nil
}
Expand Down Expand Up @@ -122,13 +123,14 @@ func (o *taskOptions) filterByGroup(tasks []types.Task) []types.Task {
}

func printPreSummaryTask(cluster string, all, matched []types.Task) {
log.Printf("[%s] All Tasks: %d, Tasks to stop: %d", cluster, len(all), len(matched))
if len(matched) > 0 {
txt := fmt.Sprintf("[%s] All Tasks: %d, Tasks to stop: %d", cluster, len(all), len(matched))
if len(matched) <= 0 {
log.Printf("%s -> No tasks to stop\n", txt)
} else {
log.Println(txt)
log.Printf("\nTasks to stop:\n")
for i, task := range matched {
log.Printf(" [%d] Group: %s, Arn: %s\n", i+1, *task.Group, *task.TaskArn)
}
} else {
log.Printf(" -> No tasks to stop\n")
}
}

0 comments on commit 4cda5c9

Please sign in to comment.