github.com/kolanos/fargate@v0.2.3/cmd/service_list.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"text/tabwriter"
     7  
     8  	"github.com/jpignata/fargate/console"
     9  	ECS "github.com/jpignata/fargate/ecs"
    10  	ELBV2 "github.com/jpignata/fargate/elbv2"
    11  	"github.com/spf13/cobra"
    12  )
    13  
    14  var serviceListCmd = &cobra.Command{
    15  	Use:   "list",
    16  	Short: "List services",
    17  	Run: func(cmd *cobra.Command, args []string) {
    18  		listServices()
    19  	},
    20  }
    21  
    22  func init() {
    23  	serviceCmd.AddCommand(serviceListCmd)
    24  }
    25  
    26  func listServices() {
    27  	var targetGroupArns []string
    28  	var loadBalancerArns []string
    29  
    30  	targetGroups := make(map[string]ELBV2.TargetGroup)
    31  	loadBalancers := make(map[string]ELBV2.LoadBalancer)
    32  
    33  	ecs := ECS.New(sess, clusterName)
    34  	elbv2 := ELBV2.New(sess)
    35  	services := ecs.ListServices()
    36  
    37  	for _, service := range services {
    38  		if service.TargetGroupArn != "" {
    39  			targetGroupArns = append(targetGroupArns, service.TargetGroupArn)
    40  		}
    41  	}
    42  
    43  	if len(targetGroupArns) > 0 {
    44  		for _, targetGroup := range elbv2.DescribeTargetGroups(targetGroupArns) {
    45  			targetGroups[targetGroup.Arn] = targetGroup
    46  
    47  			if targetGroup.LoadBalancerArn != "" {
    48  				loadBalancerArns = append(loadBalancerArns, targetGroup.LoadBalancerArn)
    49  			}
    50  		}
    51  	}
    52  
    53  	if len(loadBalancerArns) > 0 {
    54  		for _, loadBalancer := range elbv2.DescribeLoadBalancers(ELBV2.DescribeLoadBalancersInput{Arns: loadBalancerArns}) {
    55  			loadBalancers[loadBalancer.Arn] = loadBalancer
    56  		}
    57  	}
    58  
    59  	if len(services) > 0 {
    60  		w := new(tabwriter.Writer)
    61  		w.Init(os.Stdout, 0, 8, 1, '\t', 0)
    62  		fmt.Fprintln(w, "NAME\tIMAGE\tCPU\tMEMORY\tLOAD BALANCER\tDESIRED\tRUNNING\tPENDING\t")
    63  
    64  		for _, service := range services {
    65  			var loadBalancer string
    66  
    67  			if service.TargetGroupArn != "" {
    68  				tg := targetGroups[service.TargetGroupArn]
    69  				lb := loadBalancers[tg.LoadBalancerArn]
    70  
    71  				loadBalancer = lb.Name
    72  			}
    73  
    74  			fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%d\t%d\t%d\t\n",
    75  				service.Name,
    76  				service.Image,
    77  				service.Cpu,
    78  				service.Memory,
    79  				loadBalancer,
    80  				service.DesiredCount,
    81  				service.RunningCount,
    82  				service.PendingCount,
    83  			)
    84  		}
    85  
    86  		w.Flush()
    87  	} else {
    88  		console.Info("No services found")
    89  	}
    90  }