From 580355a02c36b4f8d5cfe0c92a2631044386524f Mon Sep 17 00:00:00 2001 From: Masato Oshima Date: Wed, 1 Jul 2020 00:51:57 +0900 Subject: [PATCH] Added get instances cmd --- internal/command/get_instances.go | 77 +++++++++++++++++++++++++++++++ internal/command/instances.go | 42 +---------------- internal/command/root.go | 1 + 3 files changed, 79 insertions(+), 41 deletions(-) create mode 100644 internal/command/get_instances.go diff --git a/internal/command/get_instances.go b/internal/command/get_instances.go new file mode 100644 index 0000000..2e99d41 --- /dev/null +++ b/internal/command/get_instances.go @@ -0,0 +1,77 @@ +package command + +import ( + "fmt" + "os" + "text/tabwriter" + + "github.com/aws/aws-sdk-go-v2/service/ecs" + "github.com/mpon/ecswalk/internal/pkg/awsapi" + "github.com/spf13/cobra" +) + +// NewCmdGetInstances represents the get instances command +func NewCmdGetInstances() *cobra.Command { + var getServicesCmdFlagCluster string + return &cobra.Command{ + Use: "instances", + Short: "get ECS container instances", + RunE: func(cmd *cobra.Command, args []string) error { + return runGetInstancesCmd(getServicesCmdFlagCluster) + }, + } +} + +func runGetInstancesCmd(clusterName string) error { + client, err := awsapi.NewClient() + if err != nil { + return err + } + cluster, err := client.GetECSCluster(clusterName) + if err != nil { + return err + } + return runGetInstances(client, cluster) +} + +func runGetInstances(client *awsapi.Client, cluster *ecs.Cluster) error { + containerInstances, err := client.GetAllECSContainerInstances(cluster) + if err != nil { + return err + } + + if len(containerInstances) == 0 { + fmt.Printf("%s has no container instances\n", *cluster.ClusterName) + return nil + } + + cList := awsapi.NewECSContainerInstanceInfoList(containerInstances) + + ec2Instances, err := client.GetEC2Instances(cList.Ec2InstanceIds()) + if err != nil { + return err + } + + cList.SetEC2Instances(ec2Instances) + + w := new(tabwriter.Writer) + w.Init(os.Stdout, 0, 8, 1, '\t', 0) + fmt.Fprintln(w, "ContainerInstance\tEC2Instance\tPrivateIP\tConnected\tStatus\tRunning\tCPU\tMemory\tAgent\tDocker") + for _, info := range cList { + fmt.Fprintf(w, "%s\t%s\t%s\t%v\t%s\t%d\t%d\t%d\t%s\t%s\n", + info.ShortContainerInstanceArn(), + *info.Ec2Instance.InstanceId, + *info.Ec2Instance.PrivateIpAddress, + *info.ContainerInstance.AgentConnected, + *info.ContainerInstance.Status, + *info.ContainerInstance.RunningTasksCount, + *info.CPUAvailable(), + *info.MemoryAvailable(), + *info.ContainerInstance.VersionInfo.AgentVersion, + info.DockerVersion(), + ) + } + w.Flush() + + return nil +} diff --git a/internal/command/instances.go b/internal/command/instances.go index 4273d2a..e4164de 100644 --- a/internal/command/instances.go +++ b/internal/command/instances.go @@ -2,8 +2,6 @@ package command import ( "fmt" - "os" - "text/tabwriter" "github.com/mpon/ecswalk/internal/pkg/awsapi" "github.com/mpon/ecswalk/internal/pkg/fuzzyfinder" @@ -36,45 +34,7 @@ func NewCmdInstances() *cobra.Command { return nil } - containerInstances, err := client.GetAllECSContainerInstances(cluster) - if err != nil { - return err - } - - if len(containerInstances) == 0 { - fmt.Printf("%s has no container instances\n", *cluster.ClusterName) - return nil - } - - cList := awsapi.NewECSContainerInstanceInfoList(containerInstances) - - ec2Instances, err := client.GetEC2Instances(cList.Ec2InstanceIds()) - if err != nil { - return err - } - - cList.SetEC2Instances(ec2Instances) - - w := new(tabwriter.Writer) - w.Init(os.Stdout, 0, 8, 1, '\t', 0) - fmt.Fprintln(w, "ContainerInstance\tEC2Instance\tPrivateIP\tConnected\tStatus\tRunning\tCPU\tMemory\tAgent\tDocker") - for _, info := range cList { - fmt.Fprintf(w, "%s\t%s\t%s\t%v\t%s\t%d\t%d\t%d\t%s\t%s\n", - info.ShortContainerInstanceArn(), - *info.Ec2Instance.InstanceId, - *info.Ec2Instance.PrivateIpAddress, - *info.ContainerInstance.AgentConnected, - *info.ContainerInstance.Status, - *info.ContainerInstance.RunningTasksCount, - *info.CPUAvailable(), - *info.MemoryAvailable(), - *info.ContainerInstance.VersionInfo.AgentVersion, - info.DockerVersion(), - ) - } - w.Flush() - - return nil + return runGetInstances(client, cluster) }, } return cmd diff --git a/internal/command/root.go b/internal/command/root.go index 35a0f5f..4b37730 100644 --- a/internal/command/root.go +++ b/internal/command/root.go @@ -40,6 +40,7 @@ func Execute(version string) { cmdGet.AddCommand(NewCmdGetClusters()) cmdGet.AddCommand(NewCmdGetServices()) cmdGet.AddCommand(NewCmdGetTasks()) + cmdGet.AddCommand(NewCmdGetInstances()) cmdServices := NewCmdServices()