From 11467e52c7f3922ddd02f1749d453c3dc9fea990 Mon Sep 17 00:00:00 2001 From: Carlos Henrique <84737230+chnacib@users.noreply.github.com> Date: Fri, 28 Jun 2024 18:09:33 -0300 Subject: [PATCH] feat: add list nodes cmd (#11) --- main.go | 3 ++ pkg/ecs/ecsexec.go | 4 +-- pkg/ecs/listnodes.go | 83 ++++++++++++++++++++++++++++++++++++++++++++ pkg/ecs/listtasks.go | 1 - 4 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 pkg/ecs/listnodes.go diff --git a/main.go b/main.go index abd76dc..684719b 100644 --- a/main.go +++ b/main.go @@ -60,6 +60,9 @@ func main() { logsCmd.AddCommand(ecs.ServiceLogs()) deleteCmd.AddCommand(ecs.DeleteService()) + //nodes + getCmd.AddCommand(ecs.ListNodes()) + //taskdefinition getCmd.AddCommand(ecs.ListTaskDefinition()) describeCmd.AddCommand(ecs.DescribeTaskDefinition()) diff --git a/pkg/ecs/ecsexec.go b/pkg/ecs/ecsexec.go index 352fc18..c5d1d7b 100644 --- a/pkg/ecs/ecsexec.go +++ b/pkg/ecs/ecsexec.go @@ -50,7 +50,7 @@ func Exec() *cobra.Command { service = Format(aws.StringValue(response.Tasks[0].Group)) task_def := aws.StringValue(response.Tasks[0].TaskDefinitionArn) - if enable_execute_cmd == false && status == "RUNNING" { + if !enable_execute_cmd && status == "RUNNING" { fmt.Println("Execute command is disabled") input := &ecs.DescribeTaskDefinitionInput{ TaskDefinition: aws.String(task_def), @@ -66,7 +66,7 @@ func Exec() *cobra.Command { UpdateService(service, cluster) } - if enable_execute_cmd == true && status == "RUNNING" { + if enable_execute_cmd && status == "RUNNING" { ExecuteCommand(cluster, task, container, command) } }, diff --git a/pkg/ecs/listnodes.go b/pkg/ecs/listnodes.go new file mode 100644 index 0000000..a71dc7c --- /dev/null +++ b/pkg/ecs/listnodes.go @@ -0,0 +1,83 @@ +package ecs + +import ( + "fmt" + "log" + "os" + "text/tabwriter" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/ecs" + "github.com/spf13/cobra" +) + +func ListNodes() *cobra.Command { + var cluster string + var container_instances []string + + cmd := &cobra.Command{ + Use: "nodes", + Aliases: []string{"node"}, + Short: "List ECS instances in default region", + Run: func(cmd *cobra.Command, args []string) { + sess := session.Must(session.NewSessionWithOptions(session.Options{ + SharedConfigState: session.SharedConfigEnable, + })) + + client := ecs.New(sess) + + list_input := &ecs.ListContainerInstancesInput{ + Cluster: aws.String(cluster), + MaxResults: aws.Int64(100), + } + + list_response, err := client.ListContainerInstances(list_input) + if err != nil { + log.Fatal(err) + } + w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0) + fmt.Fprintln(w, "NAME\tNETWORK\tSIZE\tREGISTERED CPU\tREGISTERED MEMORY\tREMAINING CPU\tREMAINING MEMORY\tSTATUS") + + for _, container_instance_arn := range list_response.ContainerInstanceArns { + container_instance := NameArn(*container_instance_arn) + container_instances = append(container_instances, container_instance) + } + + describe_input := &ecs.DescribeContainerInstancesInput{ + Cluster: aws.String(cluster), + ContainerInstances: aws.StringSlice(container_instances), + } + + describe_response, err := client.DescribeContainerInstances(describe_input) + if err != nil { + log.Fatal(err) + } + + for _, output_container_instance := range describe_response.ContainerInstances { + capacity_provider_name := *output_container_instance.CapacityProviderName + remaining_cpu := *output_container_instance.RemainingResources[0].IntegerValue + remaining_memory := *output_container_instance.RemainingResources[1].IntegerValue + registered_cpu := *output_container_instance.RegisteredResources[0].IntegerValue + registered_memory := *output_container_instance.RegisteredResources[1].IntegerValue + status := *output_container_instance.Status + private_ipv4 := *output_container_instance.Attachments[0].Details[4].Value + var instance_type string + for _, attribute := range output_container_instance.Attributes { + if *attribute.Name == "ecs.instance-type" { + instance_type = *attribute.Value + break + } + } + fmt.Fprintf(w, "%s\t%s\t%s\t%d\t%d\t%d\t%d\t%s\n", capacity_provider_name, private_ipv4, instance_type, registered_cpu, registered_memory, remaining_cpu, remaining_memory, status) + + } + w.Flush() + + }, + } + cmd.Flags().StringVarP(&cluster, "cluster", "c", "string", "ECS Cluster name") + cmd.MarkFlagRequired("cluster") + + return cmd +} diff --git a/pkg/ecs/listtasks.go b/pkg/ecs/listtasks.go index fcb4905..9285eb1 100644 --- a/pkg/ecs/listtasks.go +++ b/pkg/ecs/listtasks.go @@ -51,7 +51,6 @@ func ListTasks() *cobra.Command { } w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0) - //defer w.Flush() fmt.Fprintln(w, "NAME\tREVISION\tSTATUS\tCPU\tMEMORY\tNETWORK\tSTARTED")