Skip to content

Commit

Permalink
Added get instances cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
mpon committed Jun 30, 2020
1 parent eae3159 commit 580355a
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 41 deletions.
77 changes: 77 additions & 0 deletions internal/command/get_instances.go
Original file line number Diff line number Diff line change
@@ -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
}
42 changes: 1 addition & 41 deletions internal/command/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions internal/command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func Execute(version string) {
cmdGet.AddCommand(NewCmdGetClusters())
cmdGet.AddCommand(NewCmdGetServices())
cmdGet.AddCommand(NewCmdGetTasks())
cmdGet.AddCommand(NewCmdGetInstances())

cmdServices := NewCmdServices()

Expand Down

0 comments on commit 580355a

Please sign in to comment.