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

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"text/tabwriter"
     7  
     8  	"github.com/jpignata/fargate/console"
     9  	EC2 "github.com/jpignata/fargate/ec2"
    10  	ECS "github.com/jpignata/fargate/ecs"
    11  	"github.com/jpignata/fargate/util"
    12  	"github.com/spf13/cobra"
    13  )
    14  
    15  type ServiceProcessListOperation struct {
    16  	ServiceName string
    17  }
    18  
    19  var servicePsCmd = &cobra.Command{
    20  	Use:   "ps <service-name>",
    21  	Short: "List running tasks for a service",
    22  	Args:  cobra.ExactArgs(1),
    23  	Run: func(cmd *cobra.Command, args []string) {
    24  		operation := &ServiceProcessListOperation{
    25  			ServiceName: args[0],
    26  		}
    27  
    28  		getServiceProcessList(operation)
    29  	},
    30  }
    31  
    32  func init() {
    33  	serviceCmd.AddCommand(servicePsCmd)
    34  }
    35  
    36  func getServiceProcessList(operation *ServiceProcessListOperation) {
    37  	var eniIds []string
    38  
    39  	ecs := ECS.New(sess, clusterName)
    40  	ec2 := EC2.New(sess)
    41  	tasks := ecs.DescribeTasksForService(operation.ServiceName)
    42  
    43  	for _, task := range tasks {
    44  		if task.EniId != "" {
    45  			eniIds = append(eniIds, task.EniId)
    46  		}
    47  	}
    48  
    49  	if len(tasks) > 0 {
    50  		enis := ec2.DescribeNetworkInterfaces(eniIds)
    51  
    52  		w := new(tabwriter.Writer)
    53  		w.Init(os.Stdout, 0, 8, 1, '\t', 0)
    54  		fmt.Fprintln(w, "ID\tIMAGE\tSTATUS\tRUNNING\tIP\tCPU\tMEMORY\t")
    55  
    56  		for _, t := range tasks {
    57  			fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
    58  				t.TaskId,
    59  				t.Image,
    60  				util.Humanize(t.LastStatus),
    61  				t.RunningFor(),
    62  				enis[t.EniId].PublicIpAddress,
    63  				t.Cpu,
    64  				t.Memory,
    65  			)
    66  		}
    67  
    68  		w.Flush()
    69  	} else {
    70  		console.Info("No tasks found")
    71  	}
    72  }