github.com/kolanos/fargate@v0.2.3/cmd/task_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 TaskProcessListOperation struct {
    16  	TaskName string
    17  }
    18  
    19  var taskPsCmd = &cobra.Command{
    20  	Use:   "ps <task name>",
    21  	Short: "List running tasks",
    22  	Args:  cobra.ExactArgs(1),
    23  	Run: func(cmd *cobra.Command, args []string) {
    24  		operation := &TaskProcessListOperation{
    25  			TaskName: args[0],
    26  		}
    27  
    28  		getTaskProcessList(operation)
    29  	},
    30  }
    31  
    32  func init() {
    33  	taskCmd.AddCommand(taskPsCmd)
    34  }
    35  
    36  func getTaskProcessList(operation *TaskProcessListOperation) {
    37  	var eniIds []string
    38  
    39  	ecs := ECS.New(sess, clusterName)
    40  	ec2 := EC2.New(sess)
    41  	tasks := ecs.DescribeTasksForTaskGroup(operation.TaskName)
    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  		console.InfoExit("No tasks found")
    51  	}
    52  
    53  	enis := ec2.DescribeNetworkInterfaces(eniIds)
    54  
    55  	w := new(tabwriter.Writer)
    56  	w.Init(os.Stdout, 0, 8, 1, '\t', 0)
    57  	fmt.Fprintln(w, "ID\tIMAGE\tSTATUS\tRUNNING\tIP\tCPU\tMEMORY\t")
    58  
    59  	for _, t := range tasks {
    60  		fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
    61  			t.TaskId,
    62  			t.Image,
    63  			util.Humanize(t.LastStatus),
    64  			t.RunningFor(),
    65  			enis[t.EniId].PublicIpAddress,
    66  			t.Cpu,
    67  			t.Memory,
    68  		)
    69  	}
    70  
    71  	w.Flush()
    72  }