introduce 'ps' command

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2020-05-27 14:38:50 +02:00
parent 564c369c3e
commit da299f59e2
No known key found for this signature in database
GPG Key ID: 9858809D6F8F6E7E
5 changed files with 82 additions and 7 deletions

View File

@ -23,6 +23,7 @@ func ComposeCommand(dockerCli command.Cli) *cobra.Command {
UpCommand(dockerCli, opts),
DownCommand(dockerCli, opts),
LogsCommand(dockerCli, opts),
PsCommand(dockerCli, opts),
)
return cmd
}
@ -87,6 +88,26 @@ func UpCommand(dockerCli command.Cli, projectOpts *compose.ProjectOptions) *cobr
return cmd
}
func PsCommand(dockerCli command.Cli, projectOpts *compose.ProjectOptions) *cobra.Command {
opts := upOptions{}
cmd := &cobra.Command{
Use: "ps",
RunE: compose.WithProject(projectOpts, func(project *compose.Project, args []string) error {
clusteropts, err := docker.GetAwsContext(dockerCli)
if err != nil {
return err
}
client, err := amazon.NewClient(clusteropts.Profile, clusteropts.Cluster, clusteropts.Region)
if err != nil {
return err
}
return client.ComposePs(context.Background(), project)
}),
}
cmd.Flags().StringVar(&opts.loadBalancerArn, "load-balancer", "", "")
return cmd
}
type downOptions struct {
DeleteCluster bool
}

View File

@ -1,15 +1,11 @@
package amazon
import "context"
//go:generate mockgen -destination=./api_mock.go -self_package "github.com/docker/ecs-plugin/pkg/amazon" -package=amazon . API
//go:generate mockgen -destination=./mock/api.go -package=mock . API
type API interface {
downAPI
upAPI
logsAPI
secretsAPI
GetTasks(ctx context.Context, cluster string, name string) ([]string, error)
GetNetworkInterfaces(ctx context.Context, cluster string, arns ...string) ([]string, error)
GetPublicIPs(ctx context.Context, interfaces ...string) ([]string, error)
psAPI
}

55
ecs/pkg/amazon/list.go Normal file
View File

@ -0,0 +1,55 @@
package amazon
import (
"context"
"fmt"
"os"
"strings"
"text/tabwriter"
"github.com/docker/ecs-plugin/pkg/compose"
)
func (c *client) ComposePs(ctx context.Context, project *compose.Project) error {
cluster := c.Cluster
if cluster == "" {
cluster = project.Name
}
w := tabwriter.NewWriter(os.Stdout, 20, 2, 3, ' ', 0)
fmt.Fprintf(w, "Name\tState\tPorts\n")
for _, s := range project.Services {
tasks, err := c.api.GetTasks(ctx, cluster, s.Name)
if err != nil {
return err
}
if len(tasks) == 0 {
continue
}
// TODO get more data from DescribeTask, including tasks status
networkInterfaces, err := c.api.GetNetworkInterfaces(ctx, cluster, tasks...)
if err != nil {
return err
}
if len(networkInterfaces) == 0 {
fmt.Fprintf(w, "%s\t%s\t\n", s.Name, "Provisioning")
continue
}
publicIps, err := c.api.GetPublicIPs(ctx, networkInterfaces...)
if err != nil {
return err
}
ports := []string{}
for _, p := range s.Ports {
ports = append(ports, fmt.Sprintf("%s:%d->%d/%s", strings.Join(publicIps, ","), p.Published, p.Target, p.Protocol))
}
fmt.Fprintf(w, "%s\t%s\t%s\n", s.Name, "Up", strings.Join(ports, ", "))
}
w.Flush()
return nil
}
type psAPI interface {
GetTasks(ctx context.Context, cluster string, name string) ([]string, error)
GetNetworkInterfaces(ctx context.Context, cluster string, arns ...string) ([]string, error)
GetPublicIPs(ctx context.Context, interfaces ...string) ([]string, error)
}

View File

@ -388,7 +388,9 @@ func (s sdk) GetPublicIPs(ctx context.Context, interfaces ...string) ([]string,
}
publicIPs := []string{}
for _, interf := range desc.NetworkInterfaces {
publicIPs = append(publicIPs, *interf.Association.PublicIp)
if interf.Association != nil {
publicIPs = append(publicIPs, *interf.Association.PublicIp)
}
}
return publicIPs, nil
}

View File

@ -17,4 +17,5 @@ type API interface {
InspectSecret(ctx context.Context, id string) (docker.Secret, error)
ListSecrets(ctx context.Context) ([]docker.Secret, error)
DeleteSecret(ctx context.Context, id string, recover bool) error
ComposePs(background context.Context, project *Project) error
}